Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module detour;
- import std.stdio;
- import core.sys.windows.windows;
- import core.memory;
- void main() {
- hook(&testHook, &testTarget);
- testTarget();
- }
- void testHook() {
- writeln("hook");
- }
- void testTarget() {
- writeln("target");
- }
- HookHandle hook(void *hook, void *target) {
- auto returnAddr = new void *;
- ubyte *head = cast(ubyte *)GC.malloc(7);
- head [0..7] = cast(ubyte[])target[0 .. 7];
- // Write the trampoline code.
- ubyte *trampoline = cast(ubyte *)VirtualAlloc(null, 1024, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
- /* Temporarily unhook the target function. */
- // mov eax, head
- trampoline[0 .. 3] = cast(ubyte[])[0x67, 0x66, 0xA1];
- *cast(uint *)(cast(ubyte *)trampoline + 3) = cast(uint)head;
- // mov target, eax
- trampoline[7 .. 10] = cast(ubyte[])[0x67, 0x66, 0xA3];
- *cast(uint *)(cast(ubyte *)trampoline + 10) = cast(uint)target;
- // inc eax
- trampoline[14 .. 16] = cast(ubyte[])[0x66, 0x40];
- // mov target + 1, eax
- trampoline[16 .. 19] = cast(ubyte[])[0x67, 0x66, 0xA3];
- *cast(uint *)(cast(ubyte *)trampoline + 19) = cast(uint)target;
- // inc eax
- trampoline[23 .. 25] = cast(ubyte[])[0x66, 0x40];
- // mov target + 2, eax
- trampoline[25 .. 28] = cast(ubyte[])[0x67, 0x66, 0xA3];
- *cast(uint *)(cast(ubyte *)trampoline + 28) = cast(uint)target;
- // inc eax
- trampoline[32 .. 34] = cast(ubyte[])[0x66, 0x40];
- // mov target + 3, eax
- trampoline[34 .. 37] = cast(ubyte[])[0x67, 0x66, 0xA3];
- *cast(uint *)(cast(ubyte *)trampoline + 37) = cast(uint)target;
- // inc eax
- trampoline[41 .. 43] = cast(ubyte[])[0x66, 0x40];
- // mov target + 4, eax
- trampoline[43 .. 46] = cast(ubyte[])[0x67, 0x66, 0xA3];
- *cast(uint *)(cast(ubyte *)trampoline + 46) = cast(uint)target;
- // inc eax
- trampoline[50 .. 52] = cast(ubyte[])[0x66, 0x40];
- // mov target + 5, eax
- trampoline[52 .. 55] = cast(ubyte[])[0x67, 0x66, 0xA3];
- *cast(uint *)(cast(ubyte *)trampoline + 55) = cast(uint)target;
- // inc eax
- trampoline[59 .. 61] = cast(ubyte[])[0x66, 0x40];
- // mov target + 6, eax
- trampoline[61 .. 64] = cast(ubyte[])[0x67, 0x66, 0xA3];
- *cast(uint *)(cast(ubyte *)trampoline + 64) = cast(uint)target;
- // inc eax
- trampoline[68 .. 70] = cast(ubyte[])[0x66, 0x40];
- /* Prepare to call. */
- // mov eax, [esp + 8]
- trampoline[70 .. 76] = cast(ubyte[])[0x67, 0x66, 0x8B, 0x44, 0x24, 0x08];
- // mov returnAddr, eax
- trampoline[76 .. 79] = cast(ubyte[])[0x67, 0x66, 0xA3];
- *cast(uint *)(cast(ubyte *)trampoline + 79) = cast(uint)returnAddr;
- // add esp, 4
- target[83 .. 87] = cast(ubyte[])[0x66, 0x83, 0xC4, 0x04];
- /* Call the hook. */
- // call hook
- target[87 .. 90] = cast(ubyte[])[0x67, 0xFF, 0x1D];
- *cast(uint *)(cast(ubyte *)trampoline + 90) = cast(uint)hook;
- /* Rehook the target function. */
- /* End */
- // Hook the target function.
- target[0 .. 3] = cast(ubyte[])[0x67, 0xFF, 0x2D];
- *cast(uint *)(cast(ubyte *)target + 3) = trampoline - cast(ubyte *)target;
- // Save to handle.
- return HookHandle(trampoline, head, returnAddr);
- }
- void unhook() {
- }
- public struct HookHandle {
- public ubyte *trampoline;
- public ubyte *head;
- public void **returnAddr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement