Hook process functions via dll injection Cpp

On lunes, 27 de diciembre de 2010 0 comentarios

by Specific

This tutorial on how to hook process functions via dll injection is not for the beginning programmer.  I assume that you have knowledge of creating DLL (Dynamic Link Libraries) files in C++, as I do not cover this.  If you do not know how to inject a dll into a process, please read my tutorial "Inject DLL into running process with CPP."
Some of you may remember a release I had for MSN Gaming Zone called ZoneHook, this very code was used to hook the functions inside it's inner workings.  We start out planning how we will implement this code. Because ZoneHook was reasonably complex, I made a class so that implementing was easy as possible.
What we want to accomplish here is overwrite the address of the function to either jmp, or call our own function in the dll file.  Sounds really complicated, but it's not and with little knowledge of pointers and how programs work we can do this.
How do we accomplish what was just said?  We create a function that will render an instruction to jmp or call a specified offset.  With this instruction we then assign appropriate permissions to access and write to the original function call, we then write our new instruction in place of the original. 
Here is the basic structure (Redirect.h):
  1. #define CALL(a) _asm call [a]  
  2. #define JMP(a) _asm jmp [a]  
  3.   
  4. class Redirect    
  5. {  
  6. public:  
  7.     void RenderJMPInstruction(LPVOID address, LPVOID jumpto, char *buf);  
  8.     void JMPFunction(DWORD address, DWORD jumpto);  
  9.     void RenderCALLInstruction(LPVOID address, LPVOID jumpto, char *buf);  
  10.     void CALLFunction(DWORD address, DWORD jumpto);  
  11.     Redirect();  
  12.     virtual ~Redirect();  
  13.   
  14. };  


The class consists of one constructor and four methods. 

With the structure there, there is nothing more to do but code what we wanted to do.
  1. #include "Redirect.h"  
  2.   
  3. //////////////////////////////////////////////////////////////////////  
  4. // Construction/Destruction  
  5. //////////////////////////////////////////////////////////////////////  
  6.   
  7. Redirect::Redirect()  
  8. {  
  9.   
  10. }  
  11.   
  12. Redirect::~Redirect()  
  13. {  
  14.   
  15. }  
  16.   
  17. void Redirect::CALLFunction(DWORD address, DWORD jumpto)  
  18. {  
  19.     char instruction[5];  
  20.     RenderCALLInstruction((LPVOID)address,(LPVOID)jumpto,instruction);  
  21.     DWORD oldprot, dummy;  
  22.     VirtualProtect((void*)address,5,PAGE_EXECUTE_READWRITE,&oldprot);  
  23.     memcpy((LPVOID)address,(LPVOID)instruction,5);  
  24.     VirtualProtect((void*)address,5,oldprot,&dummy);  
  25. }  
  26.   
  27. void Redirect::RenderCALLInstruction(LPVOID address, LPVOID jumpto, char *buf)  
  28. {  
  29.     int offset = (int)jumpto - ((int)address + 5);  
  30.     buf[0] = (char)0xE8;  
  31.     *(DWORD*)(buf+1) = offset;  
  32. }  
  33.   
  34. void Redirect::JMPFunction(DWORD address, DWORD jumpto)  
  35. {  
  36.     char instruction[5];  
  37.     RenderJMPInstruction((LPVOID)address,(LPVOID)jumpto,instruction);  
  38.     DWORD oldprot, dummy;  
  39.     VirtualProtect((void*)address,5,PAGE_EXECUTE_READWRITE,&oldprot);  
  40.     memcpy((LPVOID)address,(LPVOID)instruction,5);  
  41.     VirtualProtect((void*)address,5,oldprot,&dummy);  
  42. }  
  43.   
  44. void Redirect::RenderJMPInstruction(LPVOID address, LPVOID jumpto, char *buf)  
  45. {  
  46.     int offset = (int)jumpto - ((int)address + 5);  
  47.     buf[0] = (char)0xE9;  
  48.     *(DWORD*)(buf+1) = offset;  
  49. }  
Redirecting the function is simple:
  1. /* This is just an example 
  2.  * 0x600542A5 would be the offset of the call in the original program you would like to redirect. 
  3.  */  
  4.   
  5. Redirect Hook;  
  6. Hook.CALLFunction(0x600542A5,(DWORD)OnZoneSend);  
Not done yet though, we still have to create the OnZoneSend function.  This is where people can run into problems if they don't know instruction flow in applications, it may require a little knowledge of assembly.
  1. int WINAPI OnZoneSend(...) {  
  2.    // ... your code here  
  3.    return ZoneSend(...);  
  4. }  
We now call a naked function which will jmp to the original function entry point.
  1. DWORD lpZoneSend = 0x6005484F; // original function entry point.  
  2. int __declspec(naked) WINAPI ZoneSend(...)  
  3. {  
  4.     JMP(lpZoneSend)  
  5. }  
Thats it!  Enjoy.

0 comentarios:

Publicar un comentario