Click here to Skip to main content
15,886,799 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 390.3K   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

 
AnswerRe: how to fix a mistake in WindowsXP Pin
Anonymous23-Apr-05 14:02
Anonymous23-Apr-05 14:02 
GeneralRe: how to fix a mistake in WindowsXP Pin
tungpn26-Apr-05 23:04
tungpn26-Apr-05 23:04 
QuestionIs something Wrong? Pin
Anonymous20-Apr-05 16:06
Anonymous20-Apr-05 16:06 
AnswerRe: Is something Wrong? Pin
Anonymous23-Apr-05 13:41
Anonymous23-Apr-05 13:41 
AnswerRe: Is something Wrong? Pin
manguonden5-Sep-06 23:43
manguonden5-Sep-06 23:43 
Generaldoesnt EVEN work on 9x!!! Pin
dongochka1-Apr-05 7:21
dongochka1-Apr-05 7:21 
GeneralRe: doesnt EVEN work on 9x!!! Pin
Anonymous1-Apr-05 13:12
Anonymous1-Apr-05 13:12 
GeneralRe: doesnt EVEN work on 9x!!! Pin
dongochka2-Apr-05 0:29
dongochka2-Apr-05 0:29 
I have VC6 SP5 with latest platform sdk.
---
DLL Compilation:
--------------------Configuration: HookAPI - Win32 Windows 9x Release--------------------
Compiling...
ApiInfo.cpp
HookAPI.cpp
injlib.cpp
main.cpp
P:\Paladin_demo(1)\HookAPI1.62\DLL\main.cpp(81) : error C2065: 'InjectLib' : undeclared identifier
ProcessModule.cpp
ProcessToHook.cpp
Ring0.cpp
util.cpp
Error executing cl.exe.

HookAPI9x.dll - 1 error(s), 0 warning(s)

ok, commented InjectLib() or defined #WINNT and compiled successfully.

---
HookInet DLL Compilation (linking with wininet.lib):
--------------------Configuration: mydll - Win32 Win32 Release for Win95--------------------
Linking...
Creating library mydll___Win32_Win32_Release_for_Win95/mydll_9x.lib and object mydll___Win32_Win32_Release_for_Win95/mydll_9x.exp
LINK : warning LNK4089: all references to "USER32.dll" discarded by /OPT:REF
LINK : warning LNK4092: shared section "_INIT" contains relocations; image may not run correctly
LINK : warning LNK4092: shared section ".text" contains relocations; image may not run correctly
LINK : warning LNK4092: shared section ".rdata" contains relocations; image may not run correctly
LINK : warning LNK4092: shared section ".data" contains relocations; image may not run correctly

mydll_9x.dll - 0 error(s), 5 warning(s)


BOOM!
HOOKAPI caused an invalid page fault in
module HOOKAPI9X.DLL at 0167:82d45446.
Registers:
EAX=ffc07544 CS=0167 EIP=82d45446 EFLGS=00010206
EBX=82d44000 SS=016f ESP=0063f774 EBP=0063f77c
ECX=75fa1145 DS=016f ESI=75fa1145 FS=1a9f
EDX=00000004 ES=016f EDI=82d4df30 GS=0000
Bytes at CS:EIP:
ff 5d f8 fb 89 45 08 a1 54 83 d9 82 33 d2 89 10
Stack dump:
00000000 bffc004b bff777d0 82d45471 82d45370 75fa1145 00000004 82d4b68e 75fa1145 00000000 00000000 8294106c 75fa1145 82941030 bff76da8 00000000

What am I doing wrong?
Oh, I forgot to say that I use VMWare (virtual OS-emulator) to test program on clean installed WIN98SE ENG and WINME ENG.
Thank you. Ivan Donga.
GeneralRe: doesnt EVEN work on 9x!!! Pin
Anonymous3-Apr-05 22:12
Anonymous3-Apr-05 22:12 
GeneralWH_KEYBOARD_LL not capture all key events Pin
XBSANTOS28-Mar-05 8:32
XBSANTOS28-Mar-05 8:32 
GeneralRe: WH_KEYBOARD_LL not capture all key events Pin
Anonymous28-Mar-05 13:36
Anonymous28-Mar-05 13:36 
GeneralDocumentation for the code Pin
BigMomma24-Mar-05 12:27
BigMomma24-Mar-05 12:27 
GeneralRe: Documentation for the code Pin
Anonymous25-Mar-05 15:27
Anonymous25-Mar-05 15:27 
GeneralWH_JOURNALRECORD Windows CE Global Hook is blocked! Pin
XBSANTOS24-Mar-05 6:31
XBSANTOS24-Mar-05 6:31 
GeneralRe: WH_JOURNALRECORD Windows CE Global Hook is blocked! Pin
Anonymous25-Mar-05 15:12
Anonymous25-Mar-05 15:12 
Questionhow can i pass all the parameter of any message to my application from hook dll?? Pin
Jagdish Vasani18-Mar-05 20:12
Jagdish Vasani18-Mar-05 20:12 
AnswerRe: how can i pass all the parameter of any message to my application from hook dll?? Pin
Anonymous20-Mar-05 12:43
Anonymous20-Mar-05 12:43 
GeneralRe: how can i pass all the parameter of any message to my application from hook dll?? Pin
Jagdish Vasani20-Mar-05 18:27
Jagdish Vasani20-Mar-05 18:27 
GeneralRe: how can i pass all the parameter of any message to my application from hook dll?? Pin
Anonymous22-Mar-05 21:21
Anonymous22-Mar-05 21:21 
AnswerRe: how can i pass all the parameter of any message to my application from hook dll?? Pin
Anton Bassov21-Aug-05 2:53
Anton Bassov21-Aug-05 2:53 
GeneralI would Test it First Pin
ThatsAlok16-Mar-05 18:30
ThatsAlok16-Mar-05 18:30 
GeneralGlobal hooks for PocketPC Pin
XBSANTOS11-Mar-05 6:07
XBSANTOS11-Mar-05 6:07 
GeneralRe: Global hooks for PocketPC Pin
Anonymous20-Mar-05 12:41
Anonymous20-Mar-05 12:41 
Questionwhat api function for copying file in CMD Pin
tungpn22-Feb-05 19:58
tungpn22-Feb-05 19:58 
AnswerRe: what api function for copying file in CMD Pin
Anonymous5-Mar-05 15:19
Anonymous5-Mar-05 15:19 

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.