News for hackers. Stuff that matters.
Re:Super Hexagon Bot by byuu on Sunday March 01, 2015 @10:00AM (#9127215)
(I initially posted this on the Reddit thread yesterday, but I thought I'd repeat it here since there are many programmer types who might attempt this here.)

From the article:

> Of course, because we replaced the function with our own, we have to actually swap the buffers by explicitly calling the original glutSwapBuffer() function. I did this by undoing the hook, calling the original function and hooking it again.

Background qualification: I do this sort of debug launcher + DLL injection thing a lot. For one example with source, see my tool header magic ( http://www.romhacking.net/utilities/796/ )

As someone with experience, please take it from me: this is a really bad idea. If you rewrite functions to call the originals, it becomes possible for another thread to call that function in the middle of your rewrite, and end up executing illegal instructions. Not only is this theoretically possible, it routinely happens all the time. Even when you think for sure that your program is not multi-threaded. Even something like hooking a Windows file size function, you'll learn that the open file dialog on Windows Vista+ is threaded, and will bomb out on you with alarming frequency.

The proper way to do this is to create "trampolines" instead. The idea is that you copy the first few instructions from the original routine, and put them somewhere else. And at the end, you jump back into the original function after your patched jump. The address of where you put the original instructions is your pointer to the "original function" that you can use if you want.

So basically, something like this:

    originalFunction:
      opcode1
      opcode2
      opcode3
Becomes:

    originalFunction:
      jmp detourFunction
      nop <padding>
    resumeAddress:
      opcode3
    
    trampolineFunction:
      opcode1
      opcode2
      jmp resumeAddress
    
    detourFunction:
      <do your function hooking magic here; modify arguments; whatever>
      call trampolineFunction <only if you want to>
      <do whatever you want with the results from the original function call>
Now, I know this sounds horrifyingly complicated: you probably think you'll need a full-on x86/amd64 disassembler+assembler. And it's theoretically possible to make a near-impossible function to trampoline. But keep in mind that 99% of APIs are built in higher level languages, and as such, have very consistent function prologues (usually saving the stack frame.) So in practice, you can write trampoline installers using one or two "signatures" and hook nearly every function you'd ever want to.

But if that seems too challenging, there is also a library called Detours ( http://research.microsoft.com/en-us/projects/detours/ ) that you could try instead. I didn't find it necessary for my own purposes.

Re:Super Hexagon Bot by MisterNegative on Sunday March 01, 2015 @12:14PM (#9127876)
Minihook is a great minimalistic opensource alternative if you cant pay 10000$ for Detours. http://www.codeproject.com/Articles/44326/MinHook-The-Minima...
Re:Super Hexagon Bot by byuu on Sunday March 01, 2015 @02:06PM (#9128408)
Detours really costs $10,000??? Unbelievable. I hadn't even looked into it. Thanks for the alternative suggestion!
Re:Super Hexagon Bot by mh- on Sunday March 01, 2015 @02:52PM (#9128584)