This is a mirror of official site: http://jasper-net.blogspot.com/

APIHooking

| Sunday, March 13, 2011
Introduction

Network snoop introduces 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 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 

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

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 callers desktop to load the DLL whose module is passed to it, in this case: 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 .

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:- 

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.

Read more: Codeproject

Posted via email from Jasper-net

0 comments: