Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C++
Article

HookAPI source code

Rate me:
Please Sign up or sign in to vote.
3.09/5 (36 votes)
31 Jan 20052 min read 388.2K   9.6K   117   132
A system wide api source code for windows api hook developpers

Introduction

HookAPI is the API SDK that sets up system wide hooks for all windows platforms. It could easily hook 32-bit windows system APIs or 32-bit user-defined DLL. It could be used easily and all you need to do is write a DLL file named mydll.dll or mydll_9x.dll. It is based on ApiSpy32 by Yariv Kaplan.

The code injects two DLLs into the destination application. The first DLL, HookAPIxx.dll, updates the API's first 5 bytes:

papi[0] =0xE8;
*(DWORD *)&papi[1] =(DWORD)ProcessCall -(DWORD)papi -CALL_BYTES_SIZE;

The nother DLL mydllxxx.dll, runs the new API instead of the old API, like this sample to hook the socket function:
int WINAPI mysocket(int af, int type, int protocol)
{
   WriteLog("debug mysocket, af=%d, type=%d, protocol=%d", af, type, protocol);

   return socket(af, type, protocol);
}

And HookAPIxx.dll hooks the CreateProcessW/CreateProcessA functions, so it can catch the creation of new processes and inject the two DLLs:

#ifdef WINNT
   if(!strcmp(pinfo->api_name, "CreateProcessW") || 
      !strcmp(pinfo->api_name, "CreateProcessA") )
   {
      pi =(PROCESS_INFORMATION *)pdwParam[9];
      if(pi->hProcess)
      {
          InjectLib(pi->hProcess, fname);  // hook new process<CODE>
</CODE>      }
   }
#endif

If you want to use it, then load the first DLL HookAPIxx.dll. If it's an NT system(WinNT/XP/200x), you should call function HookAllProcess() in the DLL and call UnhookAllProcess when you exit. There are other functions in the DLL, like HookOneProcess, HookOneProcess2 to hook one application on NT system.

mydllxx.dll is loaded by HookAPIxx.dll when HookAPIxx.dll is initialized, and then makes the hook:

CHookAPI::CHookAPI()
{
   LoadMyDll(); 
   Init();
   HookAllAPI();
}
It includes the following parts:
  • HookAPI SDK full source codes
  • many examples source codes, such as;

  1. Hook socket functions like socket, send, recv, connect, ...

  2. Hook file functions like CreateFile, ReadFile, ...

  3. Hook registry functions like RegOpenKey, RegQueryValue, RegQueryValueEx, ...

  4. Delphi sample for Hook socket function

  5. Delphi sample for Hook file function

  6. Hook ExitWindowsEx

  7. Hook LoadLibrary and GetProcAddress

  8. Hook GDI functions like TextOut, ExtTextOut

  9. Hook Shell API function like SHBrowseForFolder, SHGetFileInfo, ...

  10. Hiden Processes sample, it can hide processes, task managers cannot find it

  11. Filter Advertisement bar sample, it can filter AD bar of IE or other network application, or filter the data from some ports of TCP/UDP

  12. Message Filter sample, it can filter some messages of the windows

  13. Execute file manager sample, it can forbide some files open, execute, and hidden some folders or files

  14. Net encrypt sample, it can encrypt all the application that wrriten with socket. With this, you will not need encrypt in your application.

  15. hook a ship game to auto drop bomb and auto elude bullet

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
China China
An old C programmer in China.

Comments and Discussions

 
GeneralFull source Code Pin
galaad_g_breizh21-Feb-06 4:27
galaad_g_breizh21-Feb-06 4:27 
GeneralRe: Full source Code Pin
Achernar27-Feb-06 20:37
Achernar27-Feb-06 20:37 
GeneralRe: Full source Code Pin
programsalon27-Jun-06 17:47
programsalon27-Jun-06 17:47 
GeneralRe: Full source Code Pin
programsalon27-Jun-06 18:30
programsalon27-Jun-06 18:30 
Generalusing CHookApi class Pin
vtalau6-Feb-06 17:30
vtalau6-Feb-06 17:30 
GeneralWSARecv() is missing hook Pin
Pine19796-Dec-05 23:22
Pine19796-Dec-05 23:22 
GeneralRe: WSARecv() is missing hook Pin
vtalau7-Feb-06 18:01
vtalau7-Feb-06 18:01 
GeneralRe: WSARecv() is missing hook Pin
programsalon27-Jun-06 18:07
programsalon27-Jun-06 18:07 
GeneralNew proccess that start after my Hook APP run is not Hooked. Pin
tamtt31-Oct-05 4:10
tamtt31-Oct-05 4:10 
GeneralRe: New proccess that start after my Hook APP run is not Hooked. Pin
programsalon27-Jun-06 18:17
programsalon27-Jun-06 18:17 
GeneralError : outstanding allocaions, I checked Devpartner memory leaking tool Pin
inbakumar.G20-Jul-05 5:54
inbakumar.G20-Jul-05 5:54 
GeneralRe: Error : outstanding allocaions, I checked Devpartner memory leaking tool Pin
programsalon27-Jun-06 18:20
programsalon27-Jun-06 18:20 
General[Q]CreateRemoteThread is NULL... Pin
hsbtg25-May-05 14:39
hsbtg25-May-05 14:39 
GeneralRe: [Q]CreateRemoteThread is NULL... Pin
hsbtg25-May-05 14:56
hsbtg25-May-05 14:56 
GeneralRe: [Q]CreateRemoteThread is NULL... Pin
Anonymous8-Jun-05 5:14
Anonymous8-Jun-05 5:14 
GeneralRe: [Q]CreateRemoteThread is NULL... Pin
hsbtg15-Jun-05 14:46
hsbtg15-Jun-05 14:46 
GeneralRe: [Q]CreateRemoteThread is NULL... Pin
programsalon27-Jun-06 18:10
programsalon27-Jun-06 18:10 
GeneralRe: [Q]CreateRemoteThread is NULL... Pin
wangk070519-Dec-07 15:00
wangk070519-Dec-07 15:00 
Generalhooking BitBlt - crashing problem Pin
gauri_ag24-May-05 20:52
gauri_ag24-May-05 20:52 
GeneralRe: hooking BitBlt - crashing problem Pin
Anonymous8-Jun-05 5:10
Anonymous8-Jun-05 5:10 
GeneralMonitorize mouse global events in WINDOWS-CE; Pin
XBSANTOS3-May-05 21:34
XBSANTOS3-May-05 21:34 
Generaldoesn't work serveral programs Pin
ykish28-Apr-05 22:49
sussykish28-Apr-05 22:49 
GeneralRe: doesn't work serveral programs Pin
programsalon27-Jun-06 18:22
programsalon27-Jun-06 18:22 
GeneralRe: doesn't work serveral programs Pin
zantoy5-Oct-07 1:30
zantoy5-Oct-07 1:30 
Questionhow to fix a mistake in WindowsXP Pin
tungpn20-Apr-05 21:25
tungpn20-Apr-05 21:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.