Ad:
 
  • C++ Discussion Thread
  • Marked Solved
  • Views: 8931
  • C++ RSS
Similar Threads
Mar 27th, 2004
0

Preventing Hooks

Expand Post »
Does anyone know of a good way to prevent a process from being hooked by another, or at least by detecting the hook?
Reputation Points: 28
Solved Threads: 9
Code Guru
BountyX is offline Offline
222 posts
since Mar 2004
Mar 27th, 2004
0

Re: Preventing Hooks

i have no idea what u r talkin about.. care to elaborate? 'hooked'?
Reputation Points: 47
Solved Threads: 2
Junior Poster in Training
infamous is offline Offline
77 posts
since Mar 2004
Mar 27th, 2004
0

Re: Preventing Hooks

a hook is a prorgam that attaches to another process by

1) Inserting its code into the program's memory
2) Accesses known resources (usually edits) that the program will require usage of

a hook is very general, so It's hard to give a better definition, look around on msdn.com they support hooks in various forms (DirectX hook, Mouse Hook, Keyboard Hook, etc).

A hook is genrally used by hackers to attack a program (usually games so they can cheat, most aimbots [a program that aims for the user] uses some sort of hook)

I need to prevent this, but honestly I don't know how, but I know theres a solution, PunkBuster has it.
Reputation Points: 28
Solved Threads: 9
Code Guru
BountyX is offline Offline
222 posts
since Mar 2004
Mar 27th, 2004
0

Re: Preventing Hooks

ohh ok i see what you r talking about now. my windows knowledge is rather limited so i have nothin constructive 2 add, sorry. i can tell u in linux how tho, the method of 'attaching' to a process's address space is by using the ptrace() system call. if a process sets the PTRACE_NO_TRACE flag (or something to that effect), then other process's cannot attach themselves to it. i'd imagine windows has slightly similar thing.
Reputation Points: 47
Solved Threads: 2
Junior Poster in Training
infamous is offline Offline
77 posts
since Mar 2004
Mar 27th, 2004
0

Re: Preventing Hooks

thats very helpful, i can detect the API call thnx
Reputation Points: 28
Solved Threads: 9
Code Guru
BountyX is offline Offline
222 posts
since Mar 2004
Mar 30th, 2004
0

Re: Preventing Hooks

****. I wrote a big essay on how to do it only to be confronted by a screen asking me to logon, then I lost it all.:evil:
Here I go again..

API hooking is usually done by placing a jmp opcode at the start of the function the programmer wants to hook. This jmp would push the flow of code over to a function of his own, where he could check the arguments of the call, etc, then could either carry on executing the API, or return back to the callee function without having done such.

There are two basic ways to prevent this that I can think of from the top of my mind at the moment. The first being this, using the api function GetProcAddress as an example:
C++ Syntax (Toggle Plain Text)
  1. _asm {
  2. mov eax, GetProcAddress
  3. cmp dword ptr ds:[eax], 0xE9 // 0xE9 is the jmp opcode
  4. je badboy
  5. }

Unfortunatly, this one would probably be easily detectable by crackers. The alternative to that would be to do this, using GetProcAddress as an example again:

C++ Syntax (Toggle Plain Text)
  1. DWORD FirstParam = (DWORD)LoadLibrary("kernel32.dll");
  2. char* SecondParam = "GetModuleHandleA";
  3.  
  4. DWORD Result;
  5.  
  6. _asm {
  7. mov eax, GetProcAddress
  8. add eax, 5
  9. push SecondParam
  10. push FirstParam
  11. push OFFSET returnhere
  12. push ebp
  13. mov ebp, esp
  14. push ecx
  15. push ecx
  16. jmp eax
  17. returnhere:
  18. mov Result, eax
  19. }
  20.  
  21. // result contains the location of GetModuleHandleA()

If you're wondering why I have
C++ Syntax (Toggle Plain Text)
  1. push ebp
  2. mov ebp, esp
  3. push ecx
  4. push ecx
in the middle of that code snippet, this is why.

If you look at the GetProcAddress export in kernel32.dll with a disassembler, it'll present you with something similar to the following code:
C++ Syntax (Toggle Plain Text)
  1. 77E7B332 > 55 push ebp
  2. 77E7B333 8BEC mov ebp, esp
  3. 77E7B335 51 push ecx
  4. 77E7B336 51 push ecx
  5. 77E7B337 53 push ebx
  6. 77E7B338 57 push edi

Now, since the jmp opcode takes up 5 bytes (hence add eax, 5), and you're going to want to jump over it, you're going to need to make up for the opcodes that you have skipped. i.e the first five bytes worth.

There is fundamental flaw with this technique, though. The opcodes that you jump over could vary according to the version of the dll that you're working with. This would severely limit the compatability scope of your application.

If you wanted to fix this, you'd have to dynamically read the opcodes from the dll with ReadProcessMemory for example, and parse the bytes read with a reiterating select() (not all opcodes are the same size, you'd have to account for this). You could then append a jmp command to the opcodes which were read, and simply call the function. For example:

C++ Syntax (Toggle Plain Text)
  1. class FunctionStub
  2. {
  3. public:
  4. FunctionStub(char* FunctionName, char* LibraryName)
  5. {
  6. DWORD FunctionAddress = (DWORD)GetProcAddress(LoadLibrary(LibraryName), FunctionName);
  7. CodeCave = new BYTE[128];
  8.  
  9. ReadProcessMemory(...); // Read the required bytes into the codecave
  10.  
  11. ParseData();
  12. AppendJump();
  13. }
  14.  
  15. ~FunctionStub() { delete [] CodeCave; }
  16.  
  17. DWORD ExecuteFunction(...);
  18. private:
  19. ParseData();
  20. AppendJump();
  21. BYTE* CodeCave;
  22. }
  23.  
  24. void Example()
  25. {
  26. FunctionStub GetProcStub("GetProcAddress", "kernel32.dll");
  27.  
  28. GetProcStub->ExecuteFunction(LoadLibrary("kernel32.dll"), "GetModuleHandleA");
  29. }

I hope this helped.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Aaerox is offline Offline
8 posts
since Mar 2004
Mar 30th, 2004
0

Re: Preventing Hooks

hell yeah that helped
Reputation Points: 28
Solved Threads: 9
Code Guru
BountyX is offline Offline
222 posts
since Mar 2004
Mar 31st, 2004
0

Re: Preventing Hooks

np~ :]
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Aaerox is offline Offline
8 posts
since Mar 2004
This thread is solved. Perhaps start a new thread instead?
This thread is more than three months old. Perhaps start a new thread instead?
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: Pi Approximation
Next Thread in C++ Forum Timeline: Telnet Automation How to?





About Us | Contact Us | Advertise | Acceptable Use Policy
Build Custom RSS Feed


Follow us on Twitter


© 2010 DaniWeb® LLC