Click here to Skip to main content
15,867,568 members
Articles / Desktop Programming / Win32

APIHooking

Rate me:
Please Sign up or sign in to vote.
4.83/5 (14 votes)
2 May 2011CPOL5 min read 79.4K   2.6K   90   28
Snoop network using API hooking

Introduction

Network snoop introduces the basics of building a network sniffer to pick up all information being sent using TCP socket via send and recv API, obviously, these will be the two APIs to hook to get information about data being sent/received. This code is more about introducing the readers to API hooking using Network snoop as an example. The reader can modify the code to hook APIs related to socket using UDP (which is why readers require knowledge of sockets, networking).

Background

Before we start, reader will require basic knowledge of socket programming, windows hooks and a tad bit of assembly level programming.

Using the Code

The attached code is built using VS2010 express edition. The code must be referred to while going through this article. Also 64-bit API hooking code has been attached, but not tested.

We will start with the introduction of sockets APIs used to send and receive information, these are send and recv. The attached code will try to hook/Hijack these APIs and attempt to log all information passed through them.

The following code is mentioned in the DLL module:

C++
struct NetSnoop{
NetSnoop()
{
_beginthread(sendThread,0,0);
_beginthread(recvThread,0,0);
}
...
}
g_NetSnoop;

Notice the variable g_NetSnoop declared globally, this object will be created (and its constructor will be called which will call sendThread and recvThread) every time the DLL is loaded into a new/different process. To get the DLL to load in a different process, we use Windows hook API (mentioned in the code: NetSnoop.cpp).

C++
SetWindowsHookEx(WH_CBT,hookFunction,h,0);

Here the variable h holds the module handle of the DLL mentioned earlier. Calling SetWindowsHookEx will cause all threads that belong to the caller's desktop to load the DLL whose module is passed to it, in this case:

C++
HMODULE h=LoadLibraryA("NetSnoopDll.dll"); 

Reader will have to be aware of Windows hook.

Another way is to call (without using hook) CreateRemoteThread, you can call the above function sendThread and recvThread, please read up CreateRemoteThread on MSDN. (This method is not used in code.)

Now comes the best part: API hooking (APIHook.cpp).

We will discuss API hooking for send API, the rest are all the same, refer to the code at all times, this is important.

The function void sendThread(void*) will perform API hack, notice that this function is called from constructor of struct NetSnoop (via different thread) or via API CreateRemoteThread.

Follow the code.

We first start by getting the address of the function and use VirtualProtect.

Be sure to use VirtualProtect to alter the protection of a committed page, else you will get an exception (386 memory segment protection!). Look up MSDN for VirtualProtect.

C++
VirtualProtect(send,sizeof(Trap_Send),PAGE_EXECUTE_READWRITE,&dPermission); 

The code after that is self explanatory.

Do use disassembly of function call to better understand how it works, you will notice the following code added at the function address:

ASM
MOV EAX,function adress; 
JMP EAX; 

We are injecting the following code at the function call address using opcodes:

64 bit code is mentioned in attachment. Before we tamper with the instructions at the function address, we first store the original instructions so as to restore them when needed.

Remember: 32-bit DLL cannot be loaded/injected in 64-bit process space and vice-versa which is why you would require a separate 64-bit APP/DLL to snoop network traffic for 64-bit processes.

How It Works

When the hooked function (in this case send) is called, the instruction pointer (EIP) is taken to the function address and will start executing from there (well...this how any function call works).

We have decisively placed a JMP instruction at that very location. This will cause the EIP to move to the function Mysend, notice that this function has the very same signature, calling convection as the original function, this is to avoid any stack corruption.

Once the EIP is routed to function Mysend (as done in attached code):

  • Log whatever is needed
  • Restore the function's original instruction
  • Call the original function to complete the functionality

In API hooking of send function, please note that send function is called twice by the application:

  • Called by the application, through the JMP instruction, EIP is routed to Mysend
  • Inside Mysend, after restoring the original instructions (shown below)

Code snippet from APIHook.cpp, int WSAAPI Mysend(...)...

C++
DWORD dPermission=0;
VirtualProtect(send,sizeof(Trap_Send),PAGE_EXECUTE_READWRITE,&dPermission); 
memcpy(send,StoreOriginal_Send,sizeof(StoreOriginal_Send)); //restore it
int ret=send(s,buf,len,flags);  //we call the original function.

if(bExit==false)     //repatch the function (to trap the next call to it),
        //bExit is set to true to indicate exit. 
{
 memcpy(send, Trap_Send, sizeof(Trap_Send)); //repatch
}

Hence to maintain the stack, RET instruction is also called twice:

  • After it returns from the original send function called from inside Mysend
  • After it returns from Mysend
Restore (done in destructor)

Another important thing is to restore everything when the application exits.

This is important, when the application (NetworkSnoop) exits, the Hook will be released by code (or by OS if hook is active when application is closed/terminated).

When applications are being unhooked, the injected DLL is removed, if code is not restored, the JMP instruction will cause the EIP to jump to an invalid memory location (since DLL is removed) causing a crash.

The application restores all in a destructor of a global object, the destructor is called during DLL unload, refer to the code.

C++
~NetSnoop()
{
bExit=true;       //set this flag to true to indicate unloading is in progress 
        //(to avoid repatch indicated in previous code snippet)
while(uiInUse!=0)     //this maintains a count of trap function being called 
        //from different threads
    Sleep(500);
//restore original
DWORD dPermission=0;
VirtualProtect(send,sizeof(Trap_Send),PAGE_EXECUTE_READWRITE,&dPermission); 
memcpy(send,StoreOriginal_Send,sizeof(StoreOriginal_Send)); //restore it
VirtualProtect(recv,sizeof(Trap_recv),PAGE_EXECUTE_READWRITE,&dPermission); 
memcpy(recv,StoreOriginal_recv,sizeof(StoreOriginal_recv)); //restore it
}

Debug the Call

The reader will have to write a simple console based application mentioned below and debug the send call via disassembly to better understand the introduction of a JMP instruction.

C++
#include<windows.h>
#include<WinSock2.h>
int main()
{
MessageBoxA(0,"","",0); //provide a message processing loop via message box 
        //(for DLL injection via Hook, a message processing loop is required)
int z=send(0,0,0,0);     //function from Ws2_32.lib (add this to your lib section),
            //pass any dummy values, the idea is to understand via 
            //disassembly the introduction of JMP instruction 
            //after the hook has injected the DLL
return z;
}

Points of Interest

The best part is that NetworkSnoop introduces API hooking using Windows Hook. This allows you to hook any function in any process provided your hook injects the DLL into it, for the hook to work, the targeted thread must have a message loop. I do hope you enjoy this short article on Network snoop. API hook technique is widely used, most famous is FRAPS (www.fraps.com), it uses the technique to hook DirectX calls to get information about game performance.

History

  • 11th March, 2011: Initial post
  • 2nd May, 2011: Correction in code to perform logging after recv API is called

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Instructor / Trainer
India India
Hi,
I have been working with computers since my eight grade, programming the ZX Spectrum. I have always had an interest in assembly language and computer theory (and is still the reason for taking tons of online courses), actively code using C/C++ on Windows (using VS) and Linux (using QT).

I also provide training on data structures, algorithms, parallel patterns library , Graphics (DX11), GPGPUs (DX11-CS,AMP) and programming for performance on x86.
Feel free to call me at 0091-9823018914 (UTC +5:30)



(All views expressed here do not reflect the views of my employer).

Comments and Discussions

 
GeneralThread safety/ recursive functions Pin
Asif Bahrainwala20-Jul-12 1:34
Asif Bahrainwala20-Jul-12 1:34 
QuestionFlushInstructionCache has no effect Pin
leongao00715-Feb-12 16:15
leongao00715-Feb-12 16:15 
AnswerRe: FlushInstructionCache has no effect Pin
Asif Bahrainwala15-Feb-12 21:58
Asif Bahrainwala15-Feb-12 21:58 
GeneralRe: FlushInstructionCache has no effect Pin
leongao00716-Feb-12 15:50
leongao00716-Feb-12 15:50 
GeneralRe: FlushInstructionCache has no effect Pin
Asif Bahrainwala16-Feb-12 19:22
Asif Bahrainwala16-Feb-12 19:22 
GeneralRe: FlushInstructionCache has no effect Pin
leongao00717-Feb-12 18:57
leongao00717-Feb-12 18:57 
QuestionDo not forget to call FlushInstructionCache Pin
Asif Bahrainwala17-Jan-12 3:04
Asif Bahrainwala17-Jan-12 3:04 
http://msdn.microsoft.com/en-us/library/windows/desktop/ms679350(v=vs.85).aspx[^]

This may be required to to flush CPU cache and keep code changes (in memory) coherent with CPU cache
AnswerRe: Do not forget to call FlushInstructionCache Pin
xwlan6-Apr-12 8:21
xwlan6-Apr-12 8:21 
QuestionIE and Chrome on Windows Vista and 7 Pin
Maurizio Margiotta18-Dec-11 7:33
Maurizio Margiotta18-Dec-11 7:33 
AnswerRe: IE and Chrome on Windows Vista and 7 Pin
Asif Bahrainwala18-Dec-11 8:09
Asif Bahrainwala18-Dec-11 8:09 
GeneralSend Ok, no Recev logs Pin
mark pinnuck9-May-11 12:53
mark pinnuck9-May-11 12:53 
GeneralRe: Send Ok, no Recev logs Pin
mark pinnuck10-May-11 10:51
mark pinnuck10-May-11 10:51 
GeneralRe: Send Ok, no Recev logs [modified] Pin
pepecho2006uy12-May-11 15:54
pepecho2006uy12-May-11 15:54 
GeneralRe: Send Ok, no Recev logs Pin
mark pinnuck25-May-11 13:10
mark pinnuck25-May-11 13:10 
GeneralVery interesting... Does it exist in the managed world ? Pin
Nicolas Dorier2-May-11 5:44
professionalNicolas Dorier2-May-11 5:44 
GeneralRe: Very interesting... Does it exist in the managed world ? Pin
Asif Bahrainwala2-May-11 5:53
Asif Bahrainwala2-May-11 5:53 
GeneralSend OK, but not Recv Pin
pepecho2006uy1-May-11 21:47
pepecho2006uy1-May-11 21:47 
GeneralRe: Send OK, but not Recv [modified] Pin
Asif Bahrainwala1-May-11 22:15
Asif Bahrainwala1-May-11 22:15 
GeneralRe: Send OK, but not Recv Pin
pepecho2006uy2-May-11 6:05
pepecho2006uy2-May-11 6:05 
Generalwithout using ::SetWindowsHookEx. Using CreateRemoteThread Pin
Asif Bahrainwala7-Apr-11 21:28
Asif Bahrainwala7-Apr-11 21:28 
Generalstill one confusing thing about API hooking... Pin
Lars P.Wadefalk14-Mar-11 12:02
Lars P.Wadefalk14-Mar-11 12:02 
GeneralRe: still one confusing thing about API hooking... Pin
Asif Bahrainwala14-Mar-11 19:42
Asif Bahrainwala14-Mar-11 19:42 
GeneralRe: still one confusing thing about API hooking... Pin
Lars P.Wadefalk14-Mar-11 21:16
Lars P.Wadefalk14-Mar-11 21:16 
GeneralRe: still one confusing thing about API hooking... Pin
Asif Bahrainwala14-Mar-11 23:22
Asif Bahrainwala14-Mar-11 23:22 
GeneralRe: still one confusing thing about API hooking... Pin
Asif Bahrainwala28-Mar-11 20:27
Asif Bahrainwala28-Mar-11 20:27 

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.