Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2014
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 3.24 KB | None | 0 0
  1. module detour;
  2.  
  3. import std.stdio;
  4. import core.sys.windows.windows;
  5. import core.memory;
  6.  
  7. void main() {
  8.     hook(&testHook, &testTarget);
  9.     testTarget();
  10. }
  11.  
  12. void testHook() {
  13.     writeln("hook");
  14. }
  15.  
  16. void testTarget() {
  17.     writeln("target");
  18. }
  19.  
  20. HookHandle hook(void *hook, void *target) {
  21.     auto returnAddr = new void *;
  22.     ubyte *head = cast(ubyte *)GC.malloc(7);
  23.     head [0..7] = cast(ubyte[])target[0 .. 7];
  24.  
  25.     // Write the trampoline code.
  26.     ubyte *trampoline = cast(ubyte *)VirtualAlloc(null, 1024, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  27.  
  28.     /* Temporarily unhook the target function. */
  29.  
  30.     // mov eax, head
  31.     trampoline[0 .. 3] = cast(ubyte[])[0x67, 0x66, 0xA1];
  32.     *cast(uint *)(cast(ubyte *)trampoline + 3) = cast(uint)head;
  33.  
  34.     // mov target, eax
  35.     trampoline[7 .. 10] = cast(ubyte[])[0x67, 0x66, 0xA3];
  36.     *cast(uint *)(cast(ubyte *)trampoline + 10) = cast(uint)target;
  37.  
  38.     // inc eax
  39.     trampoline[14 .. 16] = cast(ubyte[])[0x66, 0x40];
  40.  
  41.     // mov target + 1, eax
  42.     trampoline[16 .. 19] = cast(ubyte[])[0x67, 0x66, 0xA3];
  43.     *cast(uint *)(cast(ubyte *)trampoline + 19) = cast(uint)target;
  44.    
  45.     // inc eax
  46.     trampoline[23 .. 25] = cast(ubyte[])[0x66, 0x40];
  47.  
  48.     // mov target + 2, eax
  49.     trampoline[25 .. 28] = cast(ubyte[])[0x67, 0x66, 0xA3];
  50.     *cast(uint *)(cast(ubyte *)trampoline + 28) = cast(uint)target;
  51.    
  52.     // inc eax
  53.     trampoline[32 .. 34] = cast(ubyte[])[0x66, 0x40];
  54.  
  55.     // mov target + 3, eax
  56.     trampoline[34 .. 37] = cast(ubyte[])[0x67, 0x66, 0xA3];
  57.     *cast(uint *)(cast(ubyte *)trampoline + 37) = cast(uint)target;
  58.    
  59.     // inc eax
  60.     trampoline[41 .. 43] = cast(ubyte[])[0x66, 0x40];
  61.  
  62.     // mov target + 4, eax
  63.     trampoline[43 .. 46] = cast(ubyte[])[0x67, 0x66, 0xA3];
  64.     *cast(uint *)(cast(ubyte *)trampoline + 46) = cast(uint)target;
  65.    
  66.     // inc eax
  67.     trampoline[50 .. 52] = cast(ubyte[])[0x66, 0x40];
  68.  
  69.     // mov target + 5, eax
  70.     trampoline[52 .. 55] = cast(ubyte[])[0x67, 0x66, 0xA3];
  71.     *cast(uint *)(cast(ubyte *)trampoline + 55) = cast(uint)target;
  72.    
  73.     // inc eax
  74.     trampoline[59 .. 61] = cast(ubyte[])[0x66, 0x40];
  75.  
  76.     // mov target + 6, eax
  77.     trampoline[61 .. 64] = cast(ubyte[])[0x67, 0x66, 0xA3];
  78.     *cast(uint *)(cast(ubyte *)trampoline + 64) = cast(uint)target;
  79.    
  80.     // inc eax
  81.     trampoline[68 .. 70] = cast(ubyte[])[0x66, 0x40];
  82.  
  83.     /* Prepare to call. */
  84.  
  85.     // mov eax, [esp + 8]
  86.     trampoline[70 .. 76] = cast(ubyte[])[0x67, 0x66, 0x8B, 0x44, 0x24, 0x08];
  87.  
  88.     // mov returnAddr, eax
  89.     trampoline[76 .. 79] = cast(ubyte[])[0x67, 0x66, 0xA3];
  90.     *cast(uint *)(cast(ubyte *)trampoline + 79) = cast(uint)returnAddr;
  91.  
  92.     // add esp, 4
  93.     target[83 .. 87] = cast(ubyte[])[0x66, 0x83, 0xC4, 0x04];
  94.  
  95.     /* Call the hook. */
  96.  
  97.     // call hook
  98.     target[87 .. 90] = cast(ubyte[])[0x67, 0xFF, 0x1D];
  99.     *cast(uint *)(cast(ubyte *)trampoline + 90) = cast(uint)hook;
  100.  
  101.     /* Rehook the target function. */
  102.  
  103.     /* End */
  104.  
  105.     // Hook the target function.
  106.     target[0 .. 3] = cast(ubyte[])[0x67, 0xFF, 0x2D];
  107.     *cast(uint *)(cast(ubyte *)target + 3) = trampoline - cast(ubyte *)target;
  108.  
  109.     // Save to handle.
  110.     return HookHandle(trampoline, head, returnAddr);
  111. }
  112.  
  113. void unhook() {
  114. }
  115.  
  116. public struct HookHandle {
  117.     public ubyte *trampoline;
  118.     public ubyte *head;
  119.     public void **returnAddr;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement