[C#]Hook problem menu

Shout-Out

User Tag List

Results 1 to 5 of 5
  1. #1
    avizer's Avatar Member
    Reputation
    1
    Join Date
    Oct 2012
    Posts
    21
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [C#]Hook problem

    Please help me finish the assembly code. I use this hook:
    Code:
    public class Hook
        {
            // Addresse Inection code:
            uint injected_code = 0;
            uint addresseInjection = 0;
            public bool threadHooked = false;
            uint retnInjectionAsm = 0;
            bool InjectionUsed = false;
            public BlackMagic Memory = new BlackMagic();
            public uint _processId = 0;
            public Hook(uint processId)
            {
                _processId = processId;
                Hooking();
            }
    
            public void Hooking()
            {
                // Offset:
                uint DX_DEVICE = 0xC5DF88;
                uint DX_DEVICE_IDX = 0x397C;
                uint ENDSCENE_IDX = 0xA8;
    
                // Process Connect:
                if (!Memory.IsProcessOpen)
                {
                    Memory.OpenProcessAndThread((int)_processId);
                }
    
                if (Memory.IsProcessOpen)
                {
                    // Get address of EndScene
                    uint pDevice = Memory.ReadUInt(DX_DEVICE);
                    uint pEnd = Memory.ReadUInt(pDevice + DX_DEVICE_IDX);
                    uint pScene = Memory.ReadUInt(pEnd);
                    uint pEndScene = Memory.ReadUInt(pScene + ENDSCENE_IDX);
    
                    if (Memory.ReadByte(pEndScene) == 0xE9 && (injected_code == 0 || addresseInjection == 0)) // check if wow is already hooked and dispose Hook
                    {
                        DisposeHooking();
                    }
    
                    if (Memory.ReadByte(pEndScene) != 0xE9) // check if wow is already hooked
                    {
                        try
                        {
                            threadHooked = false;
                            // allocate memory to store injected code:
                            injected_code = Memory.AllocateMemory(2048);
                            // allocate memory the new injection code pointer:
                            addresseInjection = Memory.AllocateMemory(0x4);
                            Memory.WriteInt(addresseInjection, 0);
                            // allocate memory the pointer return value:
                            retnInjectionAsm = Memory.AllocateMemory(0x4);
                            Memory.WriteInt(retnInjectionAsm, 0);
    
                            // Generate the STUB to be injected
                            Memory.Asm.Clear(); // $Asm
    
                            // save regs
                            Memory.Asm.AddLine("pushad");
                            Memory.Asm.AddLine("pushfd");
    
                            // Test if you need launch injected code:
                            Memory.Asm.AddLine("mov eax, [" + addresseInjection + "]");
                            Memory.Asm.AddLine("test eax, eax");
                            Memory.Asm.AddLine("je @out");
    
                            // Launch Fonction:
                            Memory.Asm.AddLine("mov eax, [" + addresseInjection + "]");
                            Memory.Asm.AddLine("call eax");
    
                            // Copie pointer return value:
                            Memory.Asm.AddLine("mov [" + retnInjectionAsm + "], eax");
    
                            // Enter value 0 of addresse func inject
                            Memory.Asm.AddLine("mov edx, " + addresseInjection);
                            Memory.Asm.AddLine("mov ecx, 0");
                            Memory.Asm.AddLine("mov [edx], ecx");
    
                            // Close func
                            Memory.Asm.AddLine("@out:");
    
                            // load reg
                            Memory.Asm.AddLine("popfd");
                            Memory.Asm.AddLine("popad");
    
    
                            // injected code
                            uint sizeAsm = (uint)(Memory.Asm.Assemble().Length);
                            Memory.Asm.Inject(injected_code);
    
                            // Size asm jumpback
                            int sizeJumpBack = 5;
    
                            // copy and save original instructions
                            Memory.Asm.Clear();
                            Memory.Asm.AddLine("mov edi, edi");
                            Memory.Asm.AddLine("push ebp");
                            Memory.Asm.AddLine("mov ebp, esp");
                            Memory.Asm.Inject(injected_code + sizeAsm);
    
                            // create jump back stub
                            Memory.Asm.Clear();
                            Memory.Asm.AddLine("jmp " + (pEndScene + sizeJumpBack));
                            Memory.Asm.Inject(injected_code + sizeAsm + (uint)sizeJumpBack);
    
                            // create hook jump
                            Memory.Asm.Clear(); // $jmpto
                            Memory.Asm.AddLine("jmp " + (injected_code));
                            Memory.Asm.Inject(pEndScene);
                        }
                        catch { threadHooked = false; return; }
                    }
                    threadHooked = true;
                }
    
            }
    
            public void DisposeHooking()
            {
                try
                {
                    // Offset:
                    uint DX_DEVICE = 0xC5DF88;
                    uint DX_DEVICE_IDX = 0x397C;
                    uint ENDSCENE_IDX = 0xA8;
    
                    // Get address of EndScene:
                    uint pDevice = Memory.ReadUInt(DX_DEVICE);
                    uint pEnd = Memory.ReadUInt(pDevice + DX_DEVICE_IDX);
                    uint pScene = Memory.ReadUInt(pEnd);
                    uint pEndScene = Memory.ReadUInt(pScene + ENDSCENE_IDX);
    
                    if (Memory.ReadByte(pEndScene) == 0xE9) // check if wow is already hooked and dispose Hook
                    {
                        // Restore origine endscene:
                        Memory.Asm.Clear();
                        Memory.Asm.AddLine("mov edi, edi");
                        Memory.Asm.AddLine("push ebp");
                        Memory.Asm.AddLine("mov ebp, esp");
                        Memory.Asm.Inject(pEndScene);
                    }
    
                    // free memory:
                    Memory.FreeMemory(injected_code);
                    Memory.FreeMemory(addresseInjection);
                    Memory.FreeMemory(retnInjectionAsm);
    
                }
                catch { }
            }
    
            public byte[] InjectAndExecute(string[] asm, int returnLength = 0)
            {
                while (InjectionUsed)
                { Thread.Sleep(5); }
                InjectionUsed = true;
    
                // Hook Wow:
                Hooking();
    
                byte[] tempsByte = new byte[0];
    
                // reset return value pointer
                Memory.WriteInt(retnInjectionAsm, 0);
    
                if (Memory.IsProcessOpen && threadHooked)
                {
                    // Write the asm stuff
                    Memory.Asm.Clear();
                    foreach (string tempLineAsm in asm)
                    {
                        Memory.Asm.AddLine(tempLineAsm);
                    }
    
                    // Allocation Memory
                    uint injectionAsm_Codecave = Memory.AllocateMemory(Memory.Asm.Assemble().Length);
    
    
                    try
                    {
                        // Inject
                        Memory.Asm.Inject(injectionAsm_Codecave);
                        Memory.WriteInt(addresseInjection, (int)injectionAsm_Codecave);
                        while (Memory.ReadInt(addresseInjection) > 0) { Thread.Sleep(5); } // Wait to launch code
    
    
                        if (returnLength > 0)
                        {
                            tempsByte = Memory.ReadBytes(Memory.ReadUInt(retnInjectionAsm), returnLength);
                        }
                        else
                        {
                            byte Buf = new Byte();
                            List<byte> retnByte = new List<byte>();
                            uint dwAddress = Memory.ReadUInt(retnInjectionAsm);
                            Buf = Memory.ReadByte(dwAddress);
                            while (Buf != 0)
                            {
                                retnByte.Add(Buf);
                                dwAddress = dwAddress + 1;
                                Buf = Memory.ReadByte(dwAddress);
                            }
                            tempsByte = retnByte.ToArray();
                        }
                    }
                    catch { }
    
                    // Free memory allocated 
                    Memory.FreeMemory(injectionAsm_Codecave);
                }
                InjectionUsed = false;
                // return
                return tempsByte;
            }
    
        }
    Code:
    public static string GetLocalizedText(string Commandline)
            {
                // Command to send using LUA
                String Command = Commandline;
    
                // Allocate memory for command
                uint Lua_GetLocalizedText_Space = MyHook.Memory.AllocateMemory(Encoding.UTF8.GetBytes(Command).Length + 1);
    
                // offset:
                uint ClntObjMgrGetActivePlayerObj = 0x004038F0;
                uint FrameScript__GetLocalizedText = 0x007225E0;
    
                // Write command in the allocated memory
                MyHook.Memory.WriteBytes(Lua_GetLocalizedText_Space, Encoding.UTF8.GetBytes(Command));
    
                String[] asm = new String[] 
                {
                "call " + (uint)ClntObjMgrGetActivePlayerObj,
                "mov ecx, eax",
                "push -1",
                "mov edx, " + Lua_GetLocalizedText_Space + "",
                "push edx",
                "call " + (uint)FrameScript__GetLocalizedText,
                "retn",
                };
                // Inject the shit
                string sResult = Encoding.ASCII.GetString(MyHook.InjectAndExecute(asm));
    
                // Free memory allocated for command
                MyHook.Memory.FreeMemory(Lua_GetLocalizedText_Space);
    
                // Uninstall the hook
                return sResult;
            }
            public static void LuaDoString(string command)
            {
                // Allocate memory
                uint DoStringArg_Codecave = MyHook.Memory.AllocateMemory(Encoding.UTF8.GetBytes(command).Length + 1);
                // offset:
                uint FrameScript__Execute = 0x00819210;
    
    
                // Write value:
                MyHook.Memory.WriteBytes(DoStringArg_Codecave, Encoding.UTF8.GetBytes(command));
    
                // Write the asm stuff for Lua_DoString
                String[] asm = new String[] 
                {
                    "mov eax, " + DoStringArg_Codecave,
                    "push 0",
                    "push eax",
                    "push eax",
                    "mov eax, " + (uint)FrameScript__Execute, // Lua_DoString
                    "call eax",
                    "add esp, 0xC",
                    "retn",    
                };
    
                // Inject
                MyHook.InjectAndExecute(asm);
                // Free memory allocated 
                MyHook.Memory.FreeMemory(DoStringArg_Codecave);
            }
    But when the image does not change (eg loading) Vov off with an error. What and where I need to add to abolish the use of the hook, if the picture does not change?

    [C#]Hook problem
  2. #2
    Mr.Sergey's Avatar Contributor
    Reputation
    117
    Join Date
    Apr 2009
    Posts
    201
    Thanks G/R
    6/23
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you try hook Pandaria build you must add Memory.MainModule.BaseAddress to ClntObjMgrGetActivePlayerObj, FrameScript__GetLocalizedText, FrameScript__Execute, DX_DEVICE and use current build addressess.
    Last edited by Mr.Sergey; 08-13-2013 at 04:46 AM.

  3. #3
    Jadd's Avatar 🐸 Premium Seller
    Reputation
    1515
    Join Date
    May 2008
    Posts
    2,433
    Thanks G/R
    81/336
    Trade Feedback
    1 (100%)
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Mr.Sergey View Post
    If you try hook Pandaria build you must add Memory.MainModule.BaseAddress to ClntObjMgrGetActivePlayerObj, FrameScript__GetLocalizedText, FrameScript__Execute, DX_DEVICE and use current build addressess.
    Read the last part (below the code). I think he'd have many more problems if he was trying to work on MoP.

    Edit: You have some redundancy issues. It's not related to the issue at hand, but still probably look into it. As for the problem you're having; I would simply suggest you don't bother with this approach and try to find a better way to obtain access to main thread execution.
    Last edited by Jadd; 08-13-2013 at 09:47 AM.

  4. #4
    avizer's Avatar Member
    Reputation
    1
    Join Date
    Oct 2012
    Posts
    21
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the answers, I use a hook to 3.3.5. Help please, head broke already (

  5. #5
    Jadd's Avatar 🐸 Premium Seller
    Reputation
    1515
    Join Date
    May 2008
    Posts
    2,433
    Thanks G/R
    81/336
    Trade Feedback
    1 (100%)
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by avizer View Post
    Thanks for the answers, I use a hook to 3.3.5. Help please, head broke already (
    You could try hooking one of the main thread loop functions. Or hook a main thread function and install some events, then remove the hook. There's a function which has always existed in WoW named EventSetTimer. Look for it.

    I would suggest going in-process entirely, but that's up to you.

Similar Threads

  1. [C++] EndScene Hook Problems
    By ejt in forum WoW Memory Editing
    Replies: 2
    Last Post: 10-07-2013, 09:29 AM
  2. SendChatMessage Hook Problem
    By oldmanofmen in forum WoW Memory Editing
    Replies: 3
    Last Post: 09-13-2013, 06:19 PM
  3. WndProc (Mouse Input Hook) Problem
    By Amrok in forum WoW Memory Editing
    Replies: 10
    Last Post: 10-30-2011, 07:30 AM
  4. D3D Hook problem
    By Master674 in forum WoW Memory Editing
    Replies: 5
    Last Post: 09-20-2011, 12:04 PM
  5. Problem applying end scene hook
    By wag321 in forum WoW Memory Editing
    Replies: 6
    Last Post: 07-10-2011, 12:56 PM
All times are GMT -5. The time now is 02:46 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search