Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Magic;
  6. using System.Threading;
  7. using System.Diagnostics;
  8.  
  9. namespace WoWBot
  10. {
  11.     public class Hook
  12.     {
  13.         // Addresse Inection code:
  14.         uint injected_code = 0;
  15.         uint addresseInjection = 0;
  16.         public bool threadHooked = false;
  17.         uint retnInjectionAsm = 0;
  18.         bool InjectionUsed = false;
  19.         public BlackMagic Memory = new BlackMagic();
  20.         public uint _processId = 0;
  21.         public Hook(uint processId)
  22.         {
  23.             _processId = processId;
  24.             Hooking();
  25.         }
  26.  
  27.         public void Hooking()
  28.         {
  29.             // Offset:
  30.             uint DX_DEVICE = 0x971094;
  31.             uint DX_DEVICE_IDX = 0x27B4;
  32.             uint ENDSCENE_IDX = 0xAC;
  33.             var proc = Process.GetProcessesByName("Wow");
  34.            
  35.             IntPtr WoWBase = proc[0].MainModule.BaseAddress;
  36.             // Process Connect:
  37.             if (!Memory.IsProcessOpen)
  38.             {
  39.                 Memory.OpenProcessAndThread((IntPtr)_processId);
  40.             }
  41.  
  42.             if (Memory.IsProcessOpen)
  43.             {
  44.                 // Get address of EndScene
  45.                 uint pDevice = Memory.ReadUInt((uint)WoWBase+ DX_DEVICE);
  46.                 uint pEnd = Memory.ReadUInt(pDevice + DX_DEVICE_IDX);
  47.                 uint pScene = Memory.ReadUInt(pEnd);
  48.                 uint pEndScene = Memory.ReadUInt(pScene + ENDSCENE_IDX);
  49.  
  50.                 if (Memory.ReadByte(pEndScene) == 0xE9 && (injected_code == 0 || addresseInjection == 0)) // check if wow is already hooked and dispose Hook
  51.                 {
  52.                     DisposeHooking();
  53.                 }
  54.  
  55.                 if (Memory.ReadByte(pEndScene) != 0xE9) // check if wow is already hooked
  56.                 {
  57.                     try
  58.                     {
  59.                         threadHooked = false;
  60.                         // allocate memory to store injected code:
  61.                         injected_code = Memory.AllocateMemory(2048);
  62.                         // allocate memory the new injection code pointer:
  63.                         addresseInjection = Memory.AllocateMemory(0x4);
  64.                         Memory.WriteInt(addresseInjection, 0);
  65.                         // allocate memory the pointer return value:
  66.                         retnInjectionAsm = Memory.AllocateMemory(0x4);
  67.                         Memory.WriteInt(retnInjectionAsm, 0);
  68.  
  69.                         // Generate the STUB to be injected
  70.                         Memory.Asm.Clear(); // $Asm
  71.  
  72.                         // save regs
  73.                         Memory.Asm.AddLine("pushad");
  74.                         Memory.Asm.AddLine("pushfd");
  75.  
  76.                         // Test if you need launch injected code:
  77.                         Memory.Asm.AddLine("mov eax, [" + addresseInjection + "]");
  78.                         Memory.Asm.AddLine("test eax, ebx");
  79.                         Memory.Asm.AddLine("je @out");
  80.  
  81.                         // Launch Fonction:
  82.                         Memory.Asm.AddLine("mov eax, [" + addresseInjection + "]");
  83.                         Memory.Asm.AddLine("call eax");
  84.  
  85.                         // Copie pointer return value:
  86.                         Memory.Asm.AddLine("mov [" + retnInjectionAsm + "], eax");
  87.  
  88.                         // Enter value 0 of addresse func inject
  89.                         Memory.Asm.AddLine("mov edx, " + addresseInjection);
  90.                         Memory.Asm.AddLine("mov ecx, 0");
  91.                         Memory.Asm.AddLine("mov [edx], ecx");
  92.  
  93.                         // Close func
  94.                         Memory.Asm.AddLine("@out:");
  95.  
  96.                         // load reg
  97.                         Memory.Asm.AddLine("popfd");
  98.                         Memory.Asm.AddLine("popad");
  99.  
  100.  
  101.                         // injected code
  102.                         uint sizeAsm = (uint)(Memory.Asm.Assemble().Length);
  103.                         Memory.Asm.Inject(injected_code);
  104.  
  105.                         // Size asm jumpback
  106.                         int sizeJumpBack = 5;
  107.  
  108.                         // copy and save original instructions
  109.                         Memory.Asm.Clear();
  110.                         Memory.Asm.AddLine("mov edi, edi");
  111.                         Memory.Asm.AddLine("push ebp");
  112.                         Memory.Asm.AddLine("mov ebp, esp");
  113.                         Memory.Asm.Inject(injected_code + sizeAsm);
  114.  
  115.                         // create jump back stub
  116.                         Memory.Asm.Clear();
  117.                         Memory.Asm.AddLine("jmp " + (pEndScene + sizeJumpBack));
  118.                         Memory.Asm.Inject(injected_code + sizeAsm + (uint)sizeJumpBack);
  119.  
  120.                         // create hook jump
  121.                         Memory.Asm.Clear(); // $jmpto
  122.                         Memory.Asm.AddLine("jmp " + (injected_code));
  123.                         Memory.Asm.Inject(pEndScene);
  124.                     }
  125.                     catch { threadHooked = false; return; }
  126.                 }
  127.                 threadHooked = true;
  128.             }
  129.  
  130.         }
  131.  
  132.         public void DisposeHooking()
  133.         {
  134.             try
  135.             {
  136.                 // Offset:
  137.                 uint DX_DEVICE = 0xC5DF88;
  138.                 uint DX_DEVICE_IDX = 0x397C;
  139.                 uint ENDSCENE_IDX = 0xA8;
  140.  
  141.                 // Get address of EndScene:
  142.                 uint pDevice = Memory.ReadUInt(DX_DEVICE);
  143.                 uint pEnd = Memory.ReadUInt(pDevice + DX_DEVICE_IDX);
  144.                 uint pScene = Memory.ReadUInt(pEnd);
  145.                 uint pEndScene = Memory.ReadUInt(pScene + ENDSCENE_IDX);
  146.  
  147.                 if (Memory.ReadByte(pEndScene) == 0xE9) // check if wow is already hooked and dispose Hook
  148.                 {
  149.                     // Restore origine endscene:
  150.                     Memory.Asm.Clear();
  151.                     Memory.Asm.AddLine("mov edi, edi");
  152.                     Memory.Asm.AddLine("push ebp");
  153.                     Memory.Asm.AddLine("mov ebp, esp");
  154.                     Memory.Asm.Inject(pEndScene);
  155.                 }
  156.  
  157.                 // free memory:
  158.                 Memory.FreeMemory(injected_code);
  159.                 Memory.FreeMemory(addresseInjection);
  160.                 Memory.FreeMemory(retnInjectionAsm);
  161.  
  162.             }
  163.             catch { }
  164.         }
  165.  
  166.         public byte[] InjectAndExecute(string[] asm, int returnLength = 0)
  167.         {
  168.             while (InjectionUsed)
  169.             { Thread.Sleep(5); }
  170.             InjectionUsed = true;
  171.  
  172.             // Hook Wow:
  173.             Hooking();
  174.  
  175.             byte[] tempsByte = new byte[0];
  176.  
  177.             // reset return value pointer
  178.             Memory.WriteInt(retnInjectionAsm, 0);
  179.  
  180.             if (Memory.IsProcessOpen && threadHooked)
  181.             {
  182.                 // Write the asm stuff
  183.                 Memory.Asm.Clear();
  184.                 foreach (string tempLineAsm in asm)
  185.                 {
  186.                     Memory.Asm.AddLine(tempLineAsm);
  187.                 }
  188.  
  189.                 // Allocation Memory
  190.                 uint injectionAsm_Codecave = Memory.AllocateMemory(Memory.Asm.Assemble().Length);
  191.  
  192.  
  193.                 try
  194.                 {
  195.                     // Inject
  196.                     Memory.Asm.Inject(injectionAsm_Codecave);
  197.                     Memory.WriteInt(addresseInjection, (int)injectionAsm_Codecave);
  198.                     while (Memory.ReadInt(addresseInjection) > 0) { Thread.Sleep(5); } // Wait to launch code
  199.  
  200.  
  201.                     if (returnLength > 0)
  202.                     {
  203.                         tempsByte = Memory.ReadBytes(Memory.ReadUInt(retnInjectionAsm), returnLength);
  204.                     }
  205.                     else
  206.                     {
  207.                         byte Buf = new Byte();
  208.                         List<byte> retnByte = new List<byte>();
  209.                         uint dwAddress = Memory.ReadUInt(retnInjectionAsm);
  210.                         Buf = Memory.ReadByte(dwAddress);
  211.                         while (Buf != 0)
  212.                         {
  213.                             retnByte.Add(Buf);
  214.                             dwAddress = dwAddress + 1;
  215.                             Buf = Memory.ReadByte(dwAddress);
  216.                         }
  217.                         tempsByte = retnByte.ToArray();
  218.                     }
  219.                 }
  220.                 catch { }
  221.  
  222.                 // Free memory allocated
  223.                 Memory.FreeMemory(injectionAsm_Codecave);
  224.             }
  225.             InjectionUsed = false;
  226.             // return
  227.             return tempsByte;
  228.         }
  229.  
  230.     }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement