C# Lua Wrapper (No ASM, Requires injected CLR) menu

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 15 of 30
  1. #1
    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)

    C# Lua Wrapper (No ASM, Requires injected CLR)

    This is some code from my private bot Onyx. It currently injects the CLR into the target process, and does everything within the main thread (via an EndScene hook, credits to kynox for that code).

    Keep in mind; EVERYTHING IN THIS CLASS MUST BE CALLED FROM WOW'S MAIN THREAD OR IT WILL FAIL! Got that? If something seems to 'not work' properly, then it's your own damned fault for not calling it from the main thread. No, I won't explain how to create an EndScene hook, you can go look it up. And no, I won't explain how to inject the CLR in WoW. You can look that up as well.

    On to the nitty gritty!

    Some of the stuff in this class are just quick wrappers. I do use WriteProcessMemory instead of Marshal.WriteX due to a bug that won't allow us to actually write to the targeted memory address. It happens randomly, but WPM works 100%.

    I do NOT provide the code to patch the InvalidPtrCheck, or any code to get around it entirely. That's up to you. (You didn't seriously think I'd just 'hand' you this code, did you?)

    The offsets are up to date. And if you do things properly, it works 100% without a single issue.

    The code:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using Onyx.WoW.Native;
    
    namespace Onyx.WoW
    {
        internal enum Luas
        {
            Lua_DoString = 0x0049AAB0,
            Lua_Register = 0x004998E0,
            Lua_GetTop = 0x0091A8B0,
            Lua_ToString = 0x0091ADC0,
            Lua_InvalidPtrCheck = 0x0046ED80,
        }
    
        public static class Lua
        {
            #region Delegates
    
            public delegate int ConsoleCommandCallback();
    
            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate uint LuaRegisterCommand(string szName, IntPtr pFunc);
    
            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate int RegisteredLuaCommandHandler(IntPtr pLuaState);
    
            #endregion
    
            private static readonly RegisteredLuaCommandHandler CommandParser = OnyxInputHandler;
            private static readonly LuaDoString DoStringHandler;
            private static readonly LuaGetTop GetTopHandler;
            private static readonly List<string> LuaValues = new List<string>();
            public static readonly LuaRegisterCommand RegisterCommandHandler;
            private static readonly LuaToString ToStringHandler;
    
            static Lua()
            {
                Process.EnterDebugMode();
                Win32.MemoryOpen();
                RegisterCommandHandler = Utilities.RegisterDelegate<LuaRegisterCommand>((uint) Luas.Lua_Register);
                DoStringHandler = Utilities.RegisterDelegate<LuaDoString>((uint) Luas.Lua_DoString);
                GetTopHandler = Utilities.RegisterDelegate<LuaGetTop>((uint) Luas.Lua_GetTop);
                ToStringHandler = Utilities.RegisterDelegate<LuaToString>((uint) Luas.Lua_ToString);
                RegisterCommand("OnyxInput", CommandParser);
            }
    
            private static int OnyxInputHandler(IntPtr pLuaState)
            {
                LuaValues.Clear();
                int num = GetTop(pLuaState);
                for (int i = 0; i < num; i++)
                {
                    string tmp = ToString(pLuaState, i);
                    LuaValues.Add(tmp);
                }
                return 0;
            }
    
            public static void DoString(string lua)
            {
                DoStringHandler(lua, "Onyx.lua", 0);
            }
    
            public static string[] GetReturnValues(string lua)
            {
                DoString(string.Format("OnyxInput({0})", lua));
                return LuaValues.ToArray();
            }
    
            public static T GetReturnVal<T>(string lua, uint retVal)
            {
                DoString(string.Format("OnyxInput({0})", lua));
                object tmp;
                if (typeof(T) == typeof(bool))
                {
                    tmp = LuaValues[(int) retVal] == "1";
                }
                else
                {
                    tmp = (T) Convert.ChangeType(LuaValues[(int) retVal], typeof(T));
                }
                return (T) tmp;
            }
    
            public static void RegisterCommand(string commandName, RegisteredLuaCommandHandler handler)
            {
                RegisterCommandHandler(commandName, WriteLuaCallback(Marshal.GetFunctionPointerForDelegate(handler)));
                return;
            }
    
            private static IntPtr WriteLuaCallback(IntPtr callbackPtr)
            {
                // You need to either patch the InvalidPtrCheck, or do something else to avoid the EndOfText scan
                // and check. Sorry, no code here.
                return callbackPtr;
            }
    
            private static int GetTop(IntPtr pLuaState)
            {
                return GetTopHandler(pLuaState);
            }
    
            private static string ToString(IntPtr pLuaState, int index)
            {
                return ToStringHandler(pLuaState, index + 1, 0);
            }
    
            #region Nested type: LuaDoString
    
            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            private delegate void LuaDoString(string lua, string fileName, uint pState);
    
            #endregion
    
            #region Nested type: LuaGetTop
    
            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            private delegate int LuaGetTop(IntPtr pLuaState);
    
            #endregion
    
            #region Nested type: LuaToString
    
            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            private delegate string LuaToString(IntPtr pLuaState, int idx, int length);
    
            #endregion
        }
    }
    That's it. Yes, GetReturnVal<int>("GetTime()", 0); will return whatever GetTime() usually would return.

    The RegisterDelegate code (overloads removed for brevity)

    Code:
            public static T RegisterDelegate<T>(IntPtr address) where T : class
            {
                return Marshal.GetDelegateForFunctionPointer(address, typeof(T)) as T;
            }
    Yes, I'm lazy.

    Credits;

    kynox - Creating the AWESOME CLR loader and EndScene hook we use in Onyx, and helping debug some things (even if you did *Something* which I shall not mention via TeamViewer).
    jjaa - Helping debug stupid stuff, and the awesome code contribs.
    Cypher - ******.
    Other people I forgot to mention: Tough shit.

    P.S; JuJu, you are specifically prohibited from using any of this code. Period. If I find this stuff in a commercial bot, you shall be ruined!

    P.P.S: Yes, it IS injection. So if you're scared about it, DON'T USE IT!


    P.P.P.S: No, this wasn't 'stolen' from the other thread. Ask anybody who has access to the Elite Memory Editing section. (Yes, we have one.)

    C# Lua Wrapper (No ASM, Requires injected CLR)
  2. #2
    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)
    Oh noez. News of the Elite Memory Editing section is out. Now I'm gonna get another 50 PMs per day.

    Meh, not like it matters anyway. I ignore 99% of my PMs.

    Lol @ pwning JuJu. **** you @ calilng me a ******. ******.

    P.S. Your code is teh ghey.

  3. #3
    KuRIoS's Avatar Admin
    Authenticator enabled
    Reputation
    2984
    Join Date
    Apr 2006
    Posts
    9,811
    Thanks G/R
    353/298
    Trade Feedback
    9 (100%)
    Mentioned
    5 Post(s)
    Tagged
    1 Thread(s)
    No it is not stolen and no you will not get access to that section, we pick you, not the other way around

  4. #4
    Robske's Avatar Contributor
    Reputation
    305
    Join Date
    May 2007
    Posts
    1,062
    Thanks G/R
    3/4
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's a huge concentration of awesome in this thread.

    Needs more "nil" tho
    Last edited by Robske; 06-16-2009 at 04:16 PM.
    "Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - Martin Golding
    "I cried a little earlier when I had to poop" - Sku

  5. #5
    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)
    Fixt

    Code:
            public static T GetReturnVal<T>(string lua, uint retVal)
            {
                DoString(string.Format("OnyxInput({0})", lua));
                object tmp;
    
                if (LuaValues[(int)retVal] == "nil")
                    return default(T);
    
                if (typeof(T) == typeof(bool))
                {
                    tmp = LuaValues[(int) retVal] == "1" || LuaValues[(int)retVal].ToLower() == "true";
                }
                else
                {
                    tmp = (T) Convert.ChangeType(LuaValues[(int) retVal], typeof(T));
                }
                return (T) tmp;
            }

  6. #6
    tanis2000's Avatar Active Member
    Reputation
    39
    Join Date
    Feb 2009
    Posts
    123
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Damn you Apoc.. if you posted this a week ago you'd saved me a lot of time
    Just kidding, good one! Thx for sharing!

  7. #7
    Maeco's Avatar Member
    Reputation
    2
    Join Date
    May 2009
    Posts
    14
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't know how grateful I am for this. It will surely help me a great deal in my learning process and finishing my ANN-driven bot.

    Thanks a million guys!

  8. #8
    barthen's Avatar Contributor Authenticator enabled
    Reputation
    94
    Join Date
    Apr 2007
    Posts
    112
    Thanks G/R
    4/4
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for sharing!

    A few pointers for the lost souls:

    - You have several examples on hooking EndScene at gamedeception (bobbysing's WoWX being one of them)

    - As for injecting a native dll that will start the .NET runtime you can learn a lot here: How To Inject a Managed .NET Assembly (DLL) Into Another Process - Coding the Wheel

    - It may be worth looking at EasyHook if you need to make managed detours

    Hope it helps

  9. #9
    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)
    With easyhook's managed hooking, you have to write your routines in an external DLL to actually mod the function, don't you? Or can you use Marshal and such classes to modify the params, etc. in C#?

  10. #10
    tanis2000's Avatar Active Member
    Reputation
    39
    Join Date
    Feb 2009
    Posts
    123
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by lanman92 View Post
    With easyhook's managed hooking, you have to write your routines in an external DLL to actually mod the function, don't you? Or can you use Marshal and such classes to modify the params, etc. in C#?
    Check out the source code of BabBot. We use EasyHook to inject the LUA stuff.
    SourceForge.net Repository - [babbot] Index of /
    Last edited by tanis2000; 06-17-2009 at 11:25 AM.

  11. #11
    tanis2000's Avatar Active Member
    Reputation
    39
    Join Date
    Feb 2009
    Posts
    123
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    btw according to LUA's manual, your code should not work Shynd

    Code:
    int luaL_dostring (lua_State *L, const char *str);
    const char *lua_tostring (lua_State *L, int index); // same as lua_tolstring with no length passed
    const char *lua_tolstring (lua_State *L, int index, size_t *len);
    whereas your code is

    Code:
    private delegate void LuaDoString(string lua, string fileName, uint pState);
    private delegate string LuaToString(IntPtr pLuaState, int idx, int length);
    It's like you are calling DoString with one more parameter and the state being passed in the wrong position and ToString actually calling toLstring instead (but your offset is the one of tostring apparently).

    I wonder how is it possible for that code to actually work :-P

  12. #12
    jjaa's Avatar Contributor
    Reputation
    245
    Join Date
    Dec 2006
    Posts
    562
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by tanis2000 View Post
    btw according to LUA's manual, your code should not work Shynd

    Code:
    int luaL_dostring (lua_State *L, const char *str);
    const char *lua_tostring (lua_State *L, int index); // same as lua_tolstring with no length passed
    const char *lua_tolstring (lua_State *L, int index, size_t *len);
    whereas your code is

    Code:
    private delegate void LuaDoString(string lua, string fileName, uint pState);
    private delegate string LuaToString(IntPtr pLuaState, int idx, int length);
    It's like you are calling DoString with one more parameter and the state being passed in the wrong position and ToString actually calling toLstring instead (but your offset is the one of tostring apparently).

    I wonder how is it possible for that code to actually work :-P

    Lua_DoString is not the actual Lua_DoString function. It should actually be called Lua_RunScript or something, because its from API RunScript - WoWWiki - Your guide to the World of Warcraft
    EDIT: btw i should add that the class works perfectly.
    Last edited by jjaa; 06-22-2009 at 08:47 AM.

  13. #13
    tanis2000's Avatar Active Member
    Reputation
    39
    Join Date
    Feb 2009
    Posts
    123
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by jjaa View Post

    Lua_DoString is not the actual Lua_DoString function. It should actually be called Lua_RunScript or something, because its from API RunScript - WoWWiki - Your guide to the World of Warcraft
    Yeah that whole DoString stuff is actually misdirecting.

  14. #14
    Shynd's Avatar Contributor
    Reputation
    97
    Join Date
    May 2008
    Posts
    393
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by tanis2000 View Post
    btw according to LUA's manual, your code should not work Shynd
    Have I posted in this thread without realizing it?

  15. #15
    tanis2000's Avatar Active Member
    Reputation
    39
    Join Date
    Feb 2009
    Posts
    123
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    LOL! Sorry Shynd I meant Apoc but for some reason I wrote your name.. d'oh! /facepalm

Page 1 of 2 12 LastLast

Similar Threads

  1. Limit FPS with injected clr app
    By Corthezz in forum WoW Memory Editing
    Replies: 13
    Last Post: 11-09-2015, 01:29 PM
  2. Replies: 1
    Last Post: 01-19-2012, 03:14 AM
  3. Dostring Lua Formatting - multi-line , ";" required?
    By mongoosed in forum WoW Memory Editing
    Replies: 4
    Last Post: 12-14-2010, 05:10 PM
  4. Unlock Protected LUA with a Patch? (Without Injecting code?)
    By Zeroi9 in forum WoW Memory Editing
    Replies: 15
    Last Post: 03-30-2009, 05:58 PM
  5. Injecting ASM problems
    By lanman92 in forum WoW Memory Editing
    Replies: 33
    Last Post: 03-16-2009, 06:46 AM
All times are GMT -5. The time now is 07:17 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