Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2014
639
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Magic;
  5. using Fasm;
  6. using System.Diagnostics;
  7. using System.Threading;
  8.  
  9. namespace WowMemoryHook
  10. {
  11.     public class Hook
  12.     {
  13.         public BlackMagic BlackMagic = new BlackMagic();
  14.        
  15.         private uint codeCavePtr;
  16.         private uint detourPtr;
  17.         private uint dwAddress;
  18.         public byte[] overwrittenBytes;
  19.         private Random random = new Random();
  20.  
  21.         private int CurrentProcess;
  22.         private int BaseAddress;
  23.  
  24.         public BlackMagic Memory = new BlackMagic(); //public memory reading.
  25.  
  26.         public uint DetourAddress;
  27.         public uint FrameScript_ExecuteBuffer;
  28.         public uint FrameScript_GetLocalizedText;
  29.         public uint FrameScript_RegisterFunction = 0x43B030;
  30.         public uint ClntObjMgrGetActivePlayerObjAddress;
  31.         public uint CGClickSpellAddr;
  32.         public Hook(int processID)
  33.         {
  34.             this.BlackMagic.OpenProcessAndThread(processID);
  35.             this.CurrentProcess = processID;
  36.             this.BaseAddress = (int)this.BlackMagic.MainModule.BaseAddress;
  37.             this.BlackMagic.SetDebugPrivileges = true;
  38.             this.codeCavePtr = this.BlackMagic.AllocateMemory(4);
  39.             this.BlackMagic.WriteUInt(this.codeCavePtr, 0);
  40.             this.detourPtr = this.BlackMagic.AllocateMemory(0x256);
  41.         }
  42.  
  43.         public bool IsApplied { get; private set; }
  44.  
  45.         //Public methods.
  46.         public bool ApplyDetour()
  47.         {
  48.             try
  49.             {
  50.                 this.BlackMagic.SuspendThread();
  51.                 this.Apply();
  52.                 this.BlackMagic.ResumeThread();
  53.                 return true;
  54.             }
  55.  
  56.             catch
  57.             {
  58.                 return false;
  59.             }
  60.         }
  61.  
  62.         public void RestoreDetour()
  63.         {
  64.             try
  65.             {
  66.                 this.Restore();
  67.             }
  68.             catch
  69.             {
  70.  
  71.             }
  72.         }
  73.  
  74.         public string Lua_GetReturnValue(string Command)
  75.         {
  76.             return Lua_GetReturnValue(Command, String.Empty);
  77.         }
  78.  
  79.         public string Lua_GetReturnValue(string Command, string Argument)
  80.         {
  81.             return Lua_GetReturnValue(Command, Argument, 0xff);
  82.         }
  83.  
  84.         public string Lua_GetReturnValue(string Command, string Argument, int ReturnLength)
  85.         {
  86.  
  87.             if (Argument.Length == 0)
  88.             {
  89.                 Argument = "nil";
  90.             }
  91.             byte[] bytes = Encoding.UTF8.GetBytes(Command);
  92.             byte[] buffer2 = Encoding.UTF8.GetBytes(Argument);
  93.             uint num = this.BlackMagic.AllocateMemory(bytes.Length + 1);
  94.             uint num2 = this.BlackMagic.AllocateMemory(buffer2.Length + 1);
  95.             uint num3 = this.BlackMagic.AllocateMemory(4);
  96.             this.BlackMagic.WriteBytes(num, bytes);
  97.             this.BlackMagic.WriteBytes(num2, buffer2);
  98.  
  99.             string[] aSM = new string[] {
  100.                 "mov eax, " + num,
  101.                 "push 0",
  102.                 "push eax",
  103.                 "push eax",
  104.                 "mov eax, " + ((uint) ((BaseAddress) + FrameScript_ExecuteBuffer)),
  105.                 "call eax",
  106.                 "add esp, 0xC",
  107.                 "call " + ((uint) ((BaseAddress) + ClntObjMgrGetActivePlayerObjAddress)),
  108.                 "test eax, eax", "je @out", "mov ecx, eax", "push -1", "mov edx, " + num2,
  109.                 "push edx", "call " + ((uint) ((BaseAddress) + FrameScript_GetLocalizedText)),
  110.                 "mov [" + num3 + "], eax",
  111.                 "@out:", "retn"
  112.              };
  113.             this.InjectAndExecute(aSM, "Lua_GetReturnValue");
  114.             uint dwAddress = this.BlackMagic.ReadUInt(num3);
  115.             this.BlackMagic.FreeMemory(num);
  116.             this.BlackMagic.FreeMemory(num2);
  117.             this.BlackMagic.FreeMemory(num3);
  118.             if (dwAddress != 0)
  119.             {
  120.                 return ReadUTF8String(dwAddress, 0x7d0);
  121.             }
  122.             return string.Empty;
  123.         }
  124.  
  125.         //End public methods
  126.  
  127.  
  128.         private void Apply()
  129.         {
  130.             if (IsApplied)
  131.             {
  132.                 Restore();
  133.             }
  134.  
  135.             this.dwAddress = (uint)BaseAddress + DetourAddress;
  136.  
  137.             string[] aSM = new string[] { "pushfd", "pushad", "mov eax, [" + this.codeCavePtr + "]", "cmp eax, 0", "je @out", "call eax", "mov eax, " + this.codeCavePtr, "xor edx, edx", "mov [eax], edx", "@out:", "popad", "popfd", "jmp " + ((this.dwAddress + ((uint)this.overwrittenBytes.Length))).ToString() };
  138.             aSM = RandomizeASM(aSM);
  139.             this.BlackMagic.WriteBytes(this.detourPtr, this.overwrittenBytes);
  140.             this.Inject(aSM, this.detourPtr + ((uint)this.overwrittenBytes.Length));
  141.             string[] strArray2 = new string[] { "jmp " + this.detourPtr.ToString() };
  142.             this.Inject(strArray2, this.dwAddress);
  143.             this.IsApplied = true;
  144.         }
  145.  
  146.         private void Restore()
  147.         {
  148.             if (this.IsApplied)
  149.             {
  150.                 this.BlackMagic.WriteBytes(this.dwAddress, this.overwrittenBytes);
  151.                 this.IsApplied = false;
  152.             }
  153.         }
  154.  
  155.         private void Inject(string[] ASM, uint Address)
  156.         {
  157.             ManagedFasm fasm = new ManagedFasm(this.BlackMagic.ProcessHandle);
  158.             try
  159.             {
  160.                 fasm.SetMemorySize(0x4096);
  161.                 foreach (string str in ASM)
  162.                 {
  163.                     fasm.AddLine(str);
  164.                 }
  165.  
  166.                 fasm.Inject(Address);
  167.             }
  168.             catch
  169.             {
  170.  
  171.             }
  172.             finally
  173.             {
  174.                 fasm.Dispose();
  175.             }
  176.         }
  177.  
  178.         private bool InjectAndExecute(string[] ASM, string Details)
  179.         {
  180.             bool flag;
  181.             Hook executor;
  182.             Monitor.Enter(executor = this);
  183.             try
  184.             {
  185.                 ASM = RandomizeASM(ASM);
  186.                 uint address = this.BlackMagic.AllocateMemory(0x4096);
  187.                 this.Inject(ASM, address);
  188.                 this.BlackMagic.WriteUInt(this.codeCavePtr, address);
  189.                 int tickCount = Environment.TickCount;
  190.                 while (this.BlackMagic.ReadInt(this.codeCavePtr) != 0)
  191.                 {
  192.                     if ((tickCount + 0xbb8) < Environment.TickCount)
  193.                     {
  194.                         return false;
  195.                     }
  196.                     Thread.Sleep(10);
  197.                 }
  198.                 this.BlackMagic.FreeMemory(address);
  199.                 flag = true;
  200.             }
  201.             catch
  202.             {
  203.                 flag = false;
  204.             }
  205.             finally
  206.             {
  207.                 Monitor.Exit(executor);
  208.             }
  209.             return flag;
  210.         }
  211.  
  212.         #region ASM Code
  213.         private static string[] RandomAsmCode = new string[] {
  214.             "mov eax, eax", "mov ecx, ecx", "mov ebp, ebp", "mov edx, edx", "mov ebx, ebx", "mov esp, esp", "mov esi, esi", "mov edi, edi", "nop", "push ebp|pop ebp", "push eax|pop eax", "push ecx|pop ecx", "push edx|pop edx", "push ebx|pop ebx", "push esp|pop esp", "push edi|pop edi",
  215.             "xchg eax, eax", "xchg ebp, ebp", "xchg ecx, ecx", "xchg edx, edx", "xchg ebx, ebx", "xchg esp, esp", "xchg edi, edi", "xchg eax, ebp|xchg ebp, eax", "xchg ecx, ebp|xchg ebp, ecx", "xchg eax, edx|xchg edx, eax", "xchg eax, ebx|xchg ebx, eax", "xchg eax, edi|xchg edi, eax", "xchg edi, edx|xchg edx, edi", "xchg ecx, ebx|xchg ebx, ecx", "xchg ebp, edi|xchg edi, ebp"
  216.          };
  217.  
  218.         internal static string[] RandomizeASM(string[] ASM)
  219.         {
  220.             Random random = new Random();
  221.             List<string> list = new List<string>();
  222.             foreach (string str in ASM)
  223.             {
  224.                 for (int i = 0; i < random.Next(1, 5); i++)
  225.                 {
  226.                     string item = RandomAsmCode[random.Next(0, RandomAsmCode.Length - 1)];
  227.                     if (item.Contains("|"))
  228.                     {
  229.                         foreach (string str3 in item.Split(new char[] { '|' }))
  230.                         {
  231.                             if (str3.Length > 0)
  232.                             {
  233.                                 list.Add(str3);
  234.                             }
  235.                         }
  236.                     }
  237.                     else
  238.                     {
  239.                         list.Add(item);
  240.                     }
  241.                 }
  242.                 list.Add(str);
  243.             }
  244.             return list.ToArray();
  245.         }
  246.         #endregion
  247.  
  248.         private string ReadUTF8String(uint dwAddress, int Size)
  249.         {
  250.             byte[] buffer = this.BlackMagic.ReadBytes(dwAddress, Size);
  251.             if (buffer == null)
  252.             {
  253.                 return string.Empty;
  254.             }
  255.             return StringFromBytes(buffer);
  256.         }
  257.  
  258.  
  259.  
  260.         private string StringFromBytes(byte[] myBuffer)
  261.         {
  262.             System.Text.Encoding encoding = System.Text.Encoding.UTF8;
  263.             string myString = encoding.GetString(myBuffer, 0, myBuffer.Length);
  264.  
  265.             if (myString.IndexOf("\0") != -1)
  266.             {
  267.                 myString = myString.Remove(myString.IndexOf("\0"), myString.Length - myString.IndexOf("\0"));
  268.             }
  269.             return myString;
  270.         }
  271.  
  272.     }
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement