Post
by Phantal » Thu Oct 31, 2002 6:07 pm
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
It's just that statements like: "The Detours Library intercepts target functions by re-writing their in-process binary image." (from the Detour's docs) scare the **** out of me.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Well, think about it this way. A computer program is made up of instructions, yes? These instructions are a series of bytes 'instructing' the processor on how to behave.
So if we have a set of instructions that looks something like this:
0050 mov eax, somevalue
0056 call someaddress
(i don't remmeber how to determine how many bytes that mov instruction would be, so no yelling at me for the wrong value there ;p)
Let's just pretend for a minute that 0050 is the beginning of another function's instructions. Somewhere else, there's a 'call 0050' sitting around, and that is what detours is intercepting.
First, it makes a byte sequence that will jump to your new function you want to be calling instead of this existing function, and determines the length (in bytes) of this new jmp or call ... if the length in bytes of the new instruction is say, 8 bytes, that's going to fill the space of that mov eax, somevalue, and ALSO fill 2 bytes of the call someaddress line ... which will really screw things up someth'n fierce ...
So, to compensate, they copy the mov and call instructions somewhere else, put the new sequence in, then replace the last bytes of the call instruction with nop's (which mean 'do nothing', or 'no operation'). Then, it jumps out to your new function, executes what you wanted it to do, when that is done it executes the mov eax, somevalue, executes the mov operation, and then sets the stack pointer (forgive me if i used the wrong term there, been awhile since i did any asm) up such that when the call someaddress line is executed & return's from someaddress, it returns where it originally would have had detours not interfered.
It may sound complicated and maybe scary, but it's really about as scary as getting your oil changed -- that is to say, as long as the person changing the oil/filter know what they're doing and don't put in the wrong kind of oil, then it's perfectly safe.
-Phantal