home contact keylogger.org add keylogger.org to favorites set keylogger.org as homepage Google Translate from English into Chinese (Simplified) Google Translate from English into French Google Translate from English into German Google Translate from English into Italian Google Translate from English into Japanese Google Translate from English into Portuguese Google Translate from English into Russian Google Translate from English into Spanish  Anti-Keylogger.org
Monitoring Software
Keylogger home Keylogger testing policy Press-releases Keylogger developers Keylogger articles Links Sponsorship & services Keylogger forum
Search for software: Powered by RegNow
PC Activity Monitor Pro (PC Acme Pro)
Current section
Site News
DISCLAIMER: Logging other people's keystrokes or breaking into other people's computer without their permission can be considered illegal by the courts of many countries. The monitoring software reviewed here is ONLY for authorized system administrators and/or owners of computers. We assume no liability and are not responsible for any misuse or damage caused by the keylogging software. The end user of this software is obliged to obey all applicable local, state, federal and other laws in his country of residence.
Home / Articles / Keyboard Hook

Keyboard Hook
by Arkon
Hook is a mechanism, by which a function can intercept events before they reach an application. The function can act on events, modify or discard them. Functions which receive the events are called Filter Functions, every Filter Function is classified by its type. Hooks provide powerful capabilities: Procces or modify every message; Record or play back keyboard and mouse events; Prevent another filter from being called; And many more capabilities... Generally, there are two types of hooks: System-wide, and Thread-specific. The System-wide hook is used for filtering messages of all applications(IE: when writing a key logger). And the Thread-specific hook is used for filtering messages of a specific thread. In this tutorial, I'll cover just System-wide keyboard hook. To set a System-wide hook we need a DLL.

A DLL is an indirectly executable which doesn't have a message loop to receive user input. DLLs are seperate files containing functions(not only) that can be called by programs and other DLLs. To the end-user a DLL is a program that can't be executed directly from the Program Manger(or other Shells), but from the system's point of view, there are two differences between DLLs and applications: DLLs cannot have multiple running instances loaded in memory. and DLLs attach themselves to processes, only application instances are processes. DLL stands for Dynamic-Link Library. Dynamic-Link is a mechanism to link libraries to applications at run time. These libraries(DLLs) reside in their own executable files(.dll) and are not copied into applications' executable files(.exe) as with Static-Link libraries. It's important to understand that a .DLL is loaded into the address space of the specified linking application and not into the global heap! The advantages of using dynamic linking method are:
They can be updated without requiring applications to be recompiled or relinked.
When several applications use the same .DLL, the .DLL is just loaded once for all applications(reducing memory and disk space).

The reason we need a DLL for a System-wide hook is because we want the Filter Function to be in any application address space. So when you set the hook message filter function which lies in the .dll Windows maps the .dll automatically into all applications' address space. Thus you get your filter function called for every process! Therefore when we dynamic linking the hook which is in a .DLL it becomes a System-wide hook(ofcourse it depends on the type of Filter Function too). Okay, now after the important theoretical material..
Let's start coding the .DLL for the System-wide keyboard hook, and then I'll explain how to dynamic linking the .DLL into our application.
Please download the source code in order to see how it's all written and done.

// This is our keyboard callback proc:
// _declspec(dllexport) means that we want to let other
// applications/DLLs call this function, in same cases you don't
// need this prefix, but for safety we'll use it.
_declspec(dllexport) LRESULT CALLBACK KBHookProc(int Code,
WPARAM wParam, LPARAM lParam)
{
// If code is less than 0 we call the next hook,
// this is how hooks work, nothing special
if (Code < 0) return(CallNextHookEx(hhook, Code, wParam, lParam));

// Info about what happens with the keyboard
if (lParam & (1 << 31)) // Bit 31 tells whether a key is down

// Call the next hook.
return(CallNextHookEx(hhook, Code, wParam, lParam));
}

// This is same as WinMain/Main but for DLLs,
// you get the DLL instance, Reason of how your .DLL is called, reserved.
BOOL APIENTRY DllMain(HINSTANCE hInstance, DWORD dwReason,
LPVOID lpReserved)
{
// There are actually 4 reasons, we'll process just 2 of them,
// because it's enough for our prog.
if (dwReason == DLL_PROCESS_ATTACH) // Loading the .DLL
{
if(pcount == 0)
{
// Init // First loading time
// Set hook..
}
pcount++;
}
else if (dwReason == DLL_PROCESS_DETACH)
{
// UnInit before the system frees the .DLL from memory.
if(pcount == 1)
{
// Free
// Kill hook..
}
pcount--;
}
return(1);
}

You see, it's pretty easy, I would say. Anyways, pcount is our process counter, it says how many times the .DLL has been called. pcount is a special variable, it has to be in the global heap, because if it won't then everytime our .DLL is called it will be initialized to 0 again and again...so in order to prevent that, we tell the compiler to put the variable in the global heap. We do that for the hook handle too, because we set the hook only once. If we set the hook everytime the .DLL is called, it will be a messed hook and it wouldn't work properly and as we wish! In order to tell the compiler we want it to put the variable in global heap we do this:

#pragma data_seg("SharedBlock")
HHOOK hhook = NULL;
int pcount = 0;
#pragma data_seg()

Notice every variable has to be initialized. ok we are going to HHOOK hhook = NULL; unsigned long keystrokes = 0; int pcount = 0; #pragma data_seg() We soon finish writing the .DLL, every .DLL file has to have a .DEF file to direct the compiler. In the .DEF file we tell the compiler the functions we want to export, yes, those with the _declspec(dllexport), and we tell the compiler about the shared data block. (See the source code for full understanding the .DEF file). Once the .DLL file is ready, you have to copy it to the application which loads the .DLL directory or to copy it to %windir%\system.

OK, that's it for the .DLL stuff, now let's load the .DLL we just created! First we have to declare pointers to functions with the parameters they receive.

typedef void (*LPFunction)(void);
LPFunction function = NULL;

// This is a pointer to a function which has no
// parameters and which returns a void.

HMODULE DLLmodule = LoadLibrary("mydll.dll");
if (!DLLmodule) // .DLL file is not found!

// This function gets the code of the specific wanted function from memory,
// Notice the upper case letters, the wanted function name is to be exactly,
// as in the .DEF file and .CPP file of the DLL we wrote.

function = (LPFucntion)GetProcAddress(DLLmodule, "Function");
if (!function) // Function implementation code is not found!

// Now you can call function.
function();

// After finish using the .DLL, don't forget to free it!
FreeLibrary(DLLmodule);

Woohoo, that's it!! Well, this was just the basics and it won't work if you copy paste, so download the source code!

Source Code:
Visual C++ 6 - kbhook.zip

Home / Articles / Keyboard Hook


DONATION: www.Anti-Keylogger.Org and www.Keylogger.Org is an independent research projects supported by a team of enthusiasts. If you find this project useful and would like to help foster its continued development, please consider making a donation.

Thanks in advance for your support!


Computer monitoring spy software |  Employee monitoring |  Internet activity everywhere |  Invisible keylogger surveillance |  Invisible keystroke recorder |  Keylogger |  Monitor kids |  Monitoring solution |  Network sniffer |  Parental control |  Password protected |  Powerful spy tool |  Powerful surveillance tool |  Record all keystrokes typed |  Record every action |  Records users activity |  Remote installation |  Remote spy software |  Remotely monitor |  Screenshot recorder |  Security tools |  Spy software |  Spying on employees |  Visited web pages | 
Keylogger.Org Site News

March 05th, 2010

The Best Keylogger Review added!

All In One Keylogger
Security World News
Keylogger.Org Security World News

September 18th, 2009

Microsoft Internet Explorer SSL security hole lingers

Conservatives call for DNA databases to be reduced

McAfee warns of bogus security suite

Security market remains buoyant in choppy waters

The good and bad of government in the cloud

Vista, Windows 7 Are More Secure than Snow Leopard

Will Google's Buy of reCAPTCHA Hurt Internet Security?

HHS guts health-care breach notification law, groups warn

Man gets 15 months for E-Trade skimming scam

Sophisticated botnet causing a surge in click fraud

Microsoft sues scareware scammers

Software company fined for trading with the enemy

Misdirected spyware infects Ohio hospital

Firefox's Flash check drives 10M to Adobe's download

Microsoft, Yahoo in informal talks with EU over search deal

Free online TV and internet radio
Voting

We are planning to redesign our site. We would like You to express your opinion in this respect. Would you like to leave the site as it is? What changes would you like to suggest?

Yes, I like the site as it is.
It's ok, but some changes are necessary.
It should be changed completely.
VotingView results
Top | home | testing and reviews | testing policy | press releases | developers |

| articles | contest | chat | forum | sponsorship & services | contacts | links |
Top
Copyright © 2003-2010, Keylogger.Org Team. All Rights Reserved.
Use of any information from this website is permitted only with hypertext link to www.keylogger.org.