In the last post, we saw how to properly hook and use AngelScript from HPL Engine to manipulate a game. If you haven’t read it yet, I really recommend you to do it, it was an interesting case, and there isn’t much information about hooking AngelScript on the Internet.
This time we go for more, we are going to see how to hook Lua inside a well-known game (Vermintide 2) to practically do anything we want, boost our experience, spawn items on your inventory or on the floor, heal yourself, send chat messages, kill everything? Why not?
After an introduction to Lua, we are going to analyze how we can hook Lua inside any game and then we are going to explain each step to write your own LuaHook. In the end, we will be executing our own Lua code inside the game context without any limitation.
As we have seen with AngelScript, when Lua is used in games, it has the main goal to add client site features to the game. It is an extremely powerful, flexible and easy to integrate scripting language, which allows game developers to add functionalities to game client in a much more readily way. Lua is distributed in packages and can be built in almost all the platforms that have a stander C compiler. It runs in multiple operating systems such as Windows, Unix, Android, iOS, etc. I let you here an interesting post about Lua and games here.
It is highly possible that you find something called LuaJIT inside games, this is an independent implementation of Lua using a Just-in-time compiler which runs even with more speed than the normal Lua. This is actually what we are going to see today, a variation of Lua, which is being used by Vermintide 2 to add content and features to the game.
What we are going to do with LuaJIT, can be done with the normal version of Lua too. Especially because you can use common methods that Lua and LuaJIT have in common to create the hook we are going to see now.
How to hook Lua?
In order to hook Lua we need to introduce the concept of LuaState. LuaState is the universe where all the code will run. Games can run multiple States and it will depend on us to identify the State we need to hook. Usually, each call to a Lua method requires a reference to a valid State structure, if we are able to obtain this reference, we can invoke those methods by ourselves.
Of course, this process may change depending on the Lua version the game is using, and also if it is using vanilla Lua or LuaJIT. Identify if they use LuaJIT is quite easy, I usually check the methods defined inside luaXX.dll to see if there is any function like luaJIT_XXXX:
Actually, you will have a method called luaJIT_version_X_X_X, which will display the current version in use. I truly recommend you to validate the version before trying to hook the script engine since you may need some slight modifications to work well depending on the version.
Obtaining a valid reference to a LuaState
There are multiple functions that we can hook to obtain the reference we need. Usually, a pointer to a LuaState will be the first argument on each function. We can confirm it by looking at one example: lua_gettop()
As the documentation says: “Returns the index of the top element in the stack. Because indices start at 1, this result is equal to the number of elements in the stack (and so 0 means an empty stack)”. And the most important aspect is that this call is usually called multiple times and in a very constant way, which makes it perfect to be hooked. We can be sure that this method will be called in the future and our hook will be invoked.
Let’s start coding:
What do we have here? On the first line, we have the definition of a void pointer called lua_State, by doing this we are declaring our pointer to a LuaState structure. Then, we are creating two variables to store the reference we need. Why two? I will explain it later.
And finally, we will define the existing function lua_gettop as gettop and we will create our own version called _gettop. If you paid enough attention, you may be wondering where all this came from:
(*(DWORD *)(state + 24) - *(DWORD *)(state + 16)) >> 3.
Simple, instead of looking for the original address of lua_gettop and defining a new function on that address to call it before returning, I decided to implement it by my self. Actually, what I did was open the lua51.dll that comes with the game with IDA PRO and then I used the decompiler to obtain an equivalent code in C++ of lua_gettop. This option, of course, is not the best one, but I wanted to show you that sometimes we can implement everything by ourselves without calling the original function. In this case, the function is quite simple and we can easily do it.
Last but not least, we will store the argument on our lua_State pointer.
Defining our Lua functions with typedef
Once we have our reference to LuaState, we won’t be able to call any Lua function without defining first the required functions we want to call. Again there are multiple functions and ways to execute code inside Lua. Some functions and macro examples are, but not limited to:
- luaL_dostring: macro for luaL_loadstring and lua_pcall
- luaL_dofile: macro for luaL_loadfile and lua_pcall
- luaL_loadfilex and lua_pcall
Those functions are all built-in with the library and may vary from one version to another.
I decided to go for the last option:
Original addresses and Detour
By looking at lua51.dll, we can obtain all the required offsets by subtracting the DLL base address to the address of the function in run time as we have seen before. As an example, the offset address of luaJIT_version_2_1_0_beta3 will be
7FF990590000 : 0x34130
Then, we detour lua_gettop:
Cloning the LuaState
Sometimes we may encounter problems when trying to use the same luaState the game is using at the same time. If that’s the case Lua provides us with a solution: lua_newthread. This will create a new thread and return a pointer to a lua_State that represents this new thread. The new state returned by this function shares with the original state all global objects (such as tables) but has an independent execution stack.
Perfect, we can clone the luaState and use it to execute code in the game but with an independent execution stack. That’s why we had defined two pointers to luaState, in case we needed to do this.
Writing your own Lua Script
We have been talking about executing Lua code in the game context, but how are we going to write our own scripts? As same as happened with AngelScript in the previous post, those scripts are usually compiled inside the game files. We are not going to cover how to extract these files in this post but I will provide some examples of code that we can use to manipulate the game.
Boosting your character experience:
These examples were crafted by observing and analyzing the existing Lua scripts inside the game. After a couple of minutes, you will be able to write anything you want by just modifying existing scripts.
Execute our code!
The final step would be to properly call our script and check if it works properly. Remember that we had defined previously all the required function and that we found the real address of each of them. Now we will just call those function and invoke them using the correct parameters:
Once injected the DLL, each time the player press F9, the cheat will look for the main.lua file inside the game executable folder and run it. If you are not sure where the game is looking for the file you can either hardcode a path or use ProcessMonitor to check all the system files calls:
Lua is a strong and powerful scripting engine and it is used by hundreds of games. Targeting a scripting engine like this will be definitely a good entry point to any game using it. Squirrel is another scripting engine in our TODO list, and I may write a post about it on a near future 🙂
I will be uploading the code to GitHub in coming days.