[PoC] Execute code in the main thread without hook/detour using WndProc callback menu

User Tag List

Results 1 to 7 of 7
  1. #1
    NotJuJuBoSc's Avatar Corporal
    Reputation
    51
    Join Date
    Dec 2016
    Posts
    18
    Thanks G/R
    3/24
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [PoC] Execute code in the main thread without hook/detour using WndProc callback

    Hello,

    Here is a little proof of concept how to execute code in the main thread without any hook/detour/whatever, copy paste from my github :

    Code:
    This class allow you to run code in a remote process using SendMessage and WndProc override.
    
    It use MyMemory libary and only support x86 (tho x64 is barely the same).
    
    Feel free to copy paste.
    
    Here is how it work step by step :
    
    It generate a custom message number to handle future request.
    A codecave is written in the remote process as a WndProc callback.
    When we want to execute code, we call SendMessage from our application with our custom message.
    Then the remote process should call our callback.
    The callback detect our custom message.
    The callback then call the function passed in wParam.
    It then store result of the call (EAX) into lParam pointer.
    The program read the value stored in lParam pointer.
    Done, profit !
    GitHub - JuJuBoSc/RemoteWndProc: Example to execute code in a remote process using wndproc trick


    Only support x86 because I'm lazy and did that just to test the idea, thought it might be useful to some of you.

    [PoC] Execute code in the main thread without hook/detour using WndProc callback
  2. Thanks Corthezz, squiggy, tutrakan, WiNiFiX, aeo (5 members gave Thanks to NotJuJuBoSc for this useful post)
  3. #2
    Corthezz's Avatar Elite User Authenticator enabled
    Reputation
    386
    Join Date
    Nov 2011
    Posts
    325
    Thanks G/R
    183/98
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Jadd wrote a tutorial about this before: ntoskrnl | Hooking Threads Without Detours or Patches
    Non the less thanks for posting!
    Check my blog: https://zzuks.blogspot.com

  4. Thanks squiggy, tutrakan (2 members gave Thanks to Corthezz for this useful post)
  5. #3
    WiNiFiX's Avatar Banned
    Reputation
    242
    Join Date
    Jun 2008
    Posts
    447
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Glad you made a sample app, helped alot to understand Jadd's post, wish more devs realised learning from simple sample code is the best.

    @NotJuJuBoSc
    One question, I am not familar with your library for memory, how will I get the below to return a value from executed lua?

    PHP Code:
    static string GetLocalizedText(RemoteProcess processWndProcExecutor executorstring luaValue)
    {
        var 
    ClntObjMgrGetActivePlayerObj process.ModulesManager.MainModule.BaseAddress 0x8DD5A;
        var 
    FrameScript__GetLocalizedText process.ModulesManager.MainModule.BaseAddress 0x32A5C0;
        var 
    Lua_GetLocalizedText_Space Encoding.UTF8.GetBytes(luaValue);

        
    using (var RemoteBuffer process.MemoryManager.AllocateMemory((uint)luaValue.Length 1))
        {
            
    RemoteBuffer.WriteBytes(Lua_GetLocalizedText_Space);

            var 
    asm = new[]
            {
                
    "call " ClntObjMgrGetActivePlayerObj,
                
    "mov ecx, eax",
                
    "push -1",
                
    "mov edx, " Lua_GetLocalizedText_Space "",
                
    "push edx",
                
    "call " FrameScript__GetLocalizedText,
                
    "retn"
            
    };

            
    executor.Call(asm);                
            return ?
        }

    Last edited by WiNiFiX; 04-03-2017 at 07:45 AM.

  6. #4
    infotech1's Avatar Member
    Reputation
    3
    Join Date
    Jan 2007
    Posts
    43
    Thanks G/R
    0/2
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the example code
    Last edited by infotech1; 04-03-2017 at 09:22 AM.

  7. #5
    NotJuJuBoSc's Avatar Corporal
    Reputation
    51
    Join Date
    Dec 2016
    Posts
    18
    Thanks G/R
    3/24
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by WiNiFiX View Post
    Glad you made a sample app, helped alot to understand Jadd's post, wish more devs realised learning from simple sample code is the best.

    @NotJuJuBoSc
    One question, I am not familar with your library for memory, how will I get the below to return a value from executed lua?

    PHP Code:
    static string GetLocalizedText(RemoteProcess processWndProcExecutor executorstring luaValue)
    {
        var 
    ClntObjMgrGetActivePlayerObj process.ModulesManager.MainModule.BaseAddress 0x8DD5A;
        var 
    FrameScript__GetLocalizedText process.ModulesManager.MainModule.BaseAddress 0x32A5C0;
        var 
    Lua_GetLocalizedText_Space Encoding.UTF8.GetBytes(luaValue);

        
    using (var RemoteBuffer process.MemoryManager.AllocateMemory((uint)luaValue.Length 1))
        {
            
    RemoteBuffer.WriteBytes(Lua_GetLocalizedText_Space);

            var 
    asm = new[]
            {
                
    "call " ClntObjMgrGetActivePlayerObj,
                
    "mov ecx, eax",
                
    "push -1",
                
    "mov edx, " Lua_GetLocalizedText_Space "",
                
    "push edx",
                
    "call " FrameScript__GetLocalizedText,
                
    "retn"
            
    };

            
    executor.Call(asm);                
            return ?
        }

    PHP Code:
    IntPtr result executor.Call(asm); 
    simple as that

    also you could push directly instead to move it in edx, kinda unecesserary here
    Last edited by NotJuJuBoSc; 04-03-2017 at 10:30 AM.

  8. #6
    WiNiFiX's Avatar Banned
    Reputation
    242
    Join Date
    Jun 2008
    Posts
    447
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Figured it out, was silly mistake on my side
    PHP Code:
            static string GetLocalizedText(RemoteProcess processWndProcExecutor executorstring luaValue)
            {
                var 
    ClntObjMgrGetActivePlayerObj process.ModulesManager.MainModule.BaseAddress 0x8DD5A;
                var 
    FrameScript__GetLocalizedText process.ModulesManager.MainModule.BaseAddress 0x32A5C0;
                var 
    Lua_GetLocalizedText_Space Encoding.UTF8.GetBytes(luaValue);

                
    using (var RemoteBuffer process.MemoryManager.AllocateMemory((uint)luaValue.Length 1))
                {
                    
    RemoteBuffer.WriteBytes(Lua_GetLocalizedText_Space);

                    var 
    asm = new[]
                    {
                        
    "call " ClntObjMgrGetActivePlayerObj,
                        
    "mov ecx, eax",
                        
    "push -1",
                        
    "mov edx, " RemoteBuffer.Pointer "",
                        
    "push edx",
                        
    "call " FrameScript__GetLocalizedText,
                        
    "retn"
                    
    };

                    var 
    res executor.Call(asm);
                    return 
    process.MemoryManager.ReadString(res);
                }
            } 
    PHP Code:
    DoString(remoteProcessexecutor"zoneData = GetZoneText()");
    string result GetLocalizedText(remoteProcessexecutor"zoneData"); 

  9. #7
    zakkord's Avatar Member
    Reputation
    2
    Join Date
    Mar 2008
    Posts
    18
    Thanks G/R
    1/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So this is the same as just manually mapping a dll with WndProc handler but without proper inter-process communication, gotcha. (and it's still a hook cause you're messing with GWL_WNDPROC)
    Last edited by zakkord; 04-09-2017 at 09:21 PM.

Similar Threads

  1. Do LUA addons execute in Wow's main thread?
    By ggg898 in forum WoW Memory Editing
    Replies: 15
    Last Post: 01-12-2020, 01:32 PM
  2. [Internal] Executing from the main thread without detours
    By Jadd in forum Wildstar Memory Editing
    Replies: 5
    Last Post: 07-09-2014, 10:01 PM
  3. [Bot] Injection code into wow. Do you have to call functions from the main thread?
    By Miivers in forum World of Warcraft Bots and Programs
    Replies: 2
    Last Post: 01-13-2014, 02:56 PM
  4. Executing injected code on main thread
    By mozartmclaus in forum Diablo 3 Memory Editing
    Replies: 0
    Last Post: 05-23-2012, 03:04 PM
  5. Out of the main thread
    By Shamun in forum WoW Memory Editing
    Replies: 11
    Last Post: 12-20-2008, 06:36 AM
All times are GMT -5. The time now is 04:11 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search