Patching lua registerfunction callback menu

Shout-Out

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    skiiippp's Avatar Corporal
    Reputation
    1
    Join Date
    Mar 2010
    Posts
    16
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Patching lua registerfunction callback

    I have my dll injected and hooked to endscene. Now I am trying to register my own lua function so I can retrieve data back from lua calls using a callback.

    I've checked awesomeapoc's thread/code on the topic and I wrote a patch for the callback pointer that's send to FrameScript_RegisterFunction("myfunctionname", callbackfunctionpointer).
    However, the result so far is a bit disappointing; I can call functions like SendChatMessage after patching but everything wrapped in the injected functionname is never executed.

    Code:
    //calling this (once) from endscene:
    Lua.RegisterCallbackPointer();
    Lua.DoString("SendChatMessage(\"yarrr\", \"WHISPER\", \"common\", \"myname\");");
    Lua.GetReturnValues("SendChatMessage(\"functiontest\", \"WHISPER\", \"common\", \"myname\");");
    String[] res = Lua.GetReturnValues("test = GetRealZoneText();");
    foreach (string s in res) Log("Lua.GetReturnValues: " + s);
      
    
    //bypassing IsFunctionInRange
    private static IntPtr codecave_ptr = Marshal.AllocHGlobal(8);
    private static IntPtr patchCallback(IntPtr callbackPtr)
    {
      var buf = new byte[4];
      var buf2 = new byte[1];
      //jump
      uint p = (uint)callbackPtr - (uint)codecave_ptr - 5;
      //bytes that make the codecave_ptr jump to our callback function
      //inspired by Ellesar1; http://www.mmowned.com/forums/wow-memory-editing/263874-lua_register.html
      buf2[0] = 0xE9;
      buf[3] = (byte)((p & 0xFF000000) >> 24);
      buf[2] = (byte)((p & 0xFF0000) >> 16);
      buf[1] = (byte)((p & 0xFF00) >> 8);
      buf[0] = (byte)((p & 0xFF));
    
      //write patch to our codecave_ptr using kernel32.dll's WriteProcessMemory
      IntPtr ProcessHandle = Shizzle.Memory.OpenProcess(Shizzle.Memory.ProcessAccessFlags.All, false, (UInt32)Process.GetCurrentProcess().Id);
      IntPtr bytesout;
      IntPtr bytesout2;
      bool ReturnVal = Shizzle.Memory.WriteProcessMemory(ProcessHandle, codecave_ptr, buf2, (UIntPtr)buf2.Length, out bytesout);
      bool ReturnVal2 = Shizzle.Memory.WriteProcessMemory(ProcessHandle, new IntPtr((uint)codecave_ptr + 1), buf, (UIntPtr)buf.Length, out bytesout2);
      Shizzle.Main.Log(string.Format("WriteLuaCallback() - bytesout:{0} result1:{1} || bytesout2:{2} result2:{3}", bytesout.ToInt32(), ReturnVal, bytesout2.ToInt32(), ReturnVal2));
      Shizzle.Memory.CloseHandle(ProcessHandle);
    
      return codecave_ptr;
    }
    
    
    public static void RegisterCallbackPointer()
    {
      //RegisterCommandHandler is a delegate to FrameScript_RegisterFunction @ 0x007F1340
      //CommandParser is a delegate to my callback function
      RegisterCommandHandler("OnyxInput", patchCallback(Marshal.GetFunctionPointerForDelegate(CommandParser)));
      return;
    }
    public static void DoString(string lua)
    {
      //DoStringHandler is a delegate to FrameScript_Execute @ 0x007F25C0
      DoStringHandler(lua, "plugin.lua", 0);
    }
    public static string[] GetReturnValues(string lua)
    {
      DoString(string.Format("OnyxInput({0})", lua));
      return LuaValues.ToArray();
    }
    the result; I do get the "yarrr" whisper but not the latter "functiontest" whisper that I'm expecting as well. And consequently, my callback is never called.

    So I'm thinking I went wrong with the patching of the callback pointer... am I?

    Patching lua registerfunction callback
  2. #2
    lanman92's Avatar Active Member
    Reputation
    50
    Join Date
    Mar 2007
    Posts
    1,033
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If your patching was wrong, you would crash WoW(invalid function pointer). Try making a simpler test first, before you go crazy. Register a function, try calling it. Make sure dostring is working. You will have to pop variables off of the lua stack to get return values in your callback function. The functions are in the 3.3.2 dump.

  3. #3
    skiiippp's Avatar Corporal
    Reputation
    1
    Join Date
    Mar 2010
    Posts
    16
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by lanman92 View Post
    If your patching was wrong, you would crash WoW(invalid function pointer). Try making a simpler test first, before you go crazy. Register a function, try calling it. Make sure dostring is working.
    The stuff in the opening post is quite a simple test. All I do is register the patch, test DoString(it does, as stated at the bottom of the post) and then test again with a call to the registered function (fails, as stated).

    Originally Posted by lanman92 View Post
    You will have to pop variables off of the lua stack to get return values in your callback function. The functions are in the 3.3.2 dump.
    I didn't know I had to call anything extra after DoString(string.Format("OnyxInput({0})", lua)) to get the callback working. What function would that be? And does that explain why SendChatMessage doesn't do anything when it's wrapped in our registered function?

    edit: the thing that puzzles me most is that nothing seems to happen when I use DoString with some lua code wrapped in "OnyxInput({0})"... it's as if the registration of the function fails. Can anyone verify that FrameScript_RegisterFunction is the right function for the job?
    Last edited by skiiippp; 03-07-2010 at 01:55 PM.

  4. #4
    Apoc's Avatar Angry Penguin
    Reputation
    1388
    Join Date
    Jan 2008
    Posts
    2,750
    Thanks G/R
    0/13
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you weren't copy/pasting code, you'd understand what the issue is.

  5. #5
    lanman92's Avatar Active Member
    Reputation
    50
    Join Date
    Mar 2007
    Posts
    1,033
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    int lua_callback(lua_state* L)
    {
    int myvar = lua_toint(L);
    Logger << myvar << std::endl;
    return 0;
    }
    ...
    PatchFunction();
    RegisterFunction("test_call", lua_callback);
    DoString("test_call(UnitReaction(\"player\", \"target\"));", "test_call(UnitReaction(\"player\", \"target\"));", 0);
    This should be it. I haven't really screwed with this though, as it's easy to detect... That might be the wrong type of return value for UnitReaction, but I hope this clarifies things. Ensure that you are in the main thread as well.
    Last edited by lanman92; 03-07-2010 at 02:02 PM.

  6. #6
    skiiippp's Avatar Corporal
    Reputation
    1
    Join Date
    Mar 2010
    Posts
    16
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by lanman92 View Post
    Code:
    int lua_callback(lua_state* L)
    {
    int myvar = lua_toint(L);
    Logger << myvar << std::endl;
    return 0;
    }
    ...
    PatchFunction();
    RegisterFunction("test_call", lua_callback);
    DoString("test_call(UnitReaction(\"player\", \"target\"));", "test_call(UnitReaction(\"player\", \"target\"));", 0);
    This should be it. I haven't really screwed with this though, as it's easy to detect... That might be the wrong type of return value for UnitReaction, but I hope this clarifies things. Ensure that you are in the main thread as well.
    I'm calling from EndScene, so I am in the main thread. The problem seems to be my call to the register function. Even if I don't patch the pointer I send to my register function, or I send a zero pointer, nothing happends; dostring still works for regular commands even though it should be broken after registering a function and not patching it. right?

    edit: when I pop a macro in wow that calls the function we registered, I do get the invalidptr thing. So the registration seems to be ok... so apparently my patch is not working and my dosend is not picking up my registered command/function.
    Last edited by skiiippp; 03-07-2010 at 02:29 PM.

  7. #7
    miceiken's Avatar Contributor Authenticator enabled
    Reputation
    209
    Join Date
    Dec 2007
    Posts
    401
    Thanks G/R
    7/9
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am also stuck with this WriteCallback function from Apoc's LUA Wrapper
    Code:
    private static IntPtr WriteLuaCallback(IntPtr CallbackPtr)
    {
        uint InvalidPtr = (uint)Luas.Lua_InvalidPtrCheck; // From Apoc's thread: 0x0046ED80
        int BytesWritten;
        Log.Output("WriteLuaCallback() - Starting ...");
        bool ReturnVal;
        uint p = (uint)CallbackPtr - InvalidPtr - 5;
        var buf = new byte[4];
        var buf2 = new byte[1];
        buf2[0] = 0xE9;
        buf[3] = (byte)((p & 0xFF000000) >> 24);
        buf[2] = (byte)((p & 0xFF0000) >> 16);
        buf[1] = (byte)((p & 0xFF00) >> 8);
        buf[0] = (byte)((p & 0xFF));
    
        IntPtr hProcess = Kernel32.OpenProcess(Kernel32.ProcessAccessFlags.All, false, (uint)Memory.ProcessId); // Memory is the instance of BlackMagic that I use
        Log.Output("WriteLuaCallback() - hProcess = {0:X}", (uint)hProcess);
        ReturnVal = Kernel32.WriteProcessMemory(hProcess, (IntPtr)InvalidPtr, buf2, 1, out BytesWritten);
        if (!ReturnVal) { Log.Output(LogType.Error, "WriteLuaCallback() - Error during first WriteProcessMemory"); }
        Log.Output("WriteLuaCallback() - Written {0:d} bytes", BytesWritten);
    
        ReturnVal = Kernel32.WriteProcessMemory(hProcess, (IntPtr)((uint)InvalidPtr + 1), buf, 4, out BytesWritten);
        if (!ReturnVal) { Log.Output(LogType.Error, "WriteLuaCallback() - Error during second WriteProcessMemory"); }
        Log.Output("WriteLuaCallback() - Written {0:d} bytes", BytesWritten);
        Log.Output("WriteLuaCallback() - Success");
    
        return CallbackPtr;
    }
    from my log I get
    Code:
     22:00:05  WriteLuaCallback() - Starting ...
     22:00:05  WriteLuaCallback() - hProcess = 490
     22:00:05  WriteLuaCallback() - Written 1 bytes
     22:00:05  WriteLuaCallback() - Written 4 bytes
     22:00:05  WriteLuaCallback() - Success
    Yet I crash with an AccessViolationException:
    Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
    On this line:
    RegisterCommandHandler(commandName, WriteLuaCallback(Marshal.GetFunctionPointerForDelegate(handler)));

    EDIT: Just realized I am using old offsets, lol
    I have updated everything except InvalidPtrCheck, does anyone have it?
    Code:
    internal enum Luas
    {
        Lua_DoString = 0x007F25C0,
        Lua_Register = 0x007F1340,
        Lua_GetTop = 0x00826D80,
        Lua_ToString = 0x00827290,
        Lua_InvalidPtrCheck = 0x0046ED80, // not updated
    }
    Last edited by miceiken; 03-07-2010 at 04:37 PM.

  8. #8
    lanman92's Avatar Active Member
    Reputation
    50
    Join Date
    Mar 2007
    Posts
    1,033
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hint: the part that makes it crash and burn is at 0x8448D4.

  9. #9
    skiiippp's Avatar Corporal
    Reputation
    1
    Join Date
    Mar 2010
    Posts
    16
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    heh, thanks for the hint ^^
    I was just looking in the "[Collection] WoW Binaries (Release & PTR) " topic and fired up IDA for an old version of which I found the invalidfunctionptr in an old topic.
    I fetched the same function in the current release, and it's the same as you're hinting at.

    There's still a bug in my patching code though, applying the code from my opening post to 0x008448A0 will get the callback working but also makes wow crash after;
    Exception: 0xC0000005 (ACCESS_VIOLATION) at 001B:008272A4
    The instruction at "0x008272A4" referenced memory at "0x00000013".
    The memory could not be "read".

    I'm gonna take another good look at my patch code, it doesn't do what I think it does. Also, I'm gonna see if I can find some database file to make my Hex Rays look a bit less alien, or at least a script to import the function and var names from the dump topic.


    edit; First of all, thanks again Ianman92! I looked at the code again and realised I had to patch it at the part where it's gonna throw me the invalidpointer misery, not the start of the function.
    Also, my test call "test = GetRealZoneText();" was kinda flawed. Need to ditch the ";" and "test=" assignment, just calling GetReturnValues("GetRealZoneText()") works like a charm now. +Rep for pointing me in the right direction


    edit2: The lua stuff is really versatile but I was just wondering, how safe is the usage of "protected" stuff?
    For example, CastSpellByName. I can fire that lua function from my code but the blizz UI blocks it.
    So are there lua calls I should avoid? I'm checking out if and how I can pass lua functions and macros to the FrameScript_Execute/DoSend atm.
    If there are no security issues involved, I would prefer to make extensive usage of lua calls.
    Last edited by skiiippp; 03-07-2010 at 10:42 PM.

  10. #10
    lanman92's Avatar Active Member
    Reputation
    50
    Join Date
    Mar 2007
    Posts
    1,033
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can call engine functions just as easily. They're faster, not that it really matters. And it removes the additional layer of crap that lua forces you to have.

  11. #11
    Apoc's Avatar Angry Penguin
    Reputation
    1388
    Join Date
    Jan 2008
    Posts
    2,750
    Thanks G/R
    0/13
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by lanman92 View Post
    You can call engine functions just as easily. They're faster, not that it really matters. And it removes the additional layer of crap that lua forces you to have.
    And using Lua allows you to remove the hundreds of layers of crap you'd need to re-implement if you were to not use it.

    Plus, you'd have a ton more stuff to update each patch! (As opposed to roughly 4 addresses for full Lua support)

  12. #12
    lanman92's Avatar Active Member
    Reputation
    50
    Join Date
    Mar 2007
    Posts
    1,033
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright, you got me

  13. #13
    SinnerG's Avatar Member
    Reputation
    6
    Join Date
    Aug 2006
    Posts
    78
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One thing I don't get : LuaNinja got detected, what prevents Warden from detecting any of this?

  14. #14
    Cypher's Avatar Kynox's Sister's Pimp
    Reputation
    1358
    Join Date
    Apr 2006
    Posts
    5,368
    Thanks G/R
    0/6
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by SinnerG View Post
    One thing I don't get : LuaNinja got detected, what prevents Warden from detecting any of this?
    Short answer: Nothing.

  15. #15
    Apoc's Avatar Angry Penguin
    Reputation
    1388
    Join Date
    Jan 2008
    Posts
    2,750
    Thanks G/R
    0/13
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Cypher View Post
    Short answer: Nothing.
    Lies. I have cake. I'm fully protected.

Page 1 of 2 12 LastLast

Similar Threads

  1. [Patch Release] Lua Commands
    By Link_S in forum WoW EMU General Releases
    Replies: 16
    Last Post: 02-12-2009, 04:17 AM
  2. LUA restriction patch
    By schlumpf in forum WoW Memory Editing
    Replies: 9
    Last Post: 01-21-2009, 08:25 AM
  3. How can i apply LUA++ patch to my Arc Emu ?
    By Wheeze201 in forum WoW EMU Questions & Requests
    Replies: 10
    Last Post: 08-22-2008, 08:55 PM
  4. LUA Portal and Patch 2.4.3
    By edcbabe in forum World of Warcraft Emulator Servers
    Replies: 4
    Last Post: 07-29-2008, 09:33 PM
  5. Replies: 22
    Last Post: 05-29-2008, 03:52 PM
All times are GMT -5. The time now is 06:40 AM. 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