Tutorial How to mod Compiled Lua with Hex Comparing method

PMT

PMT Modder
Staff member
Modding-Team
In this thread, I will show you how to mod compiled lua.

-----------------------------------------------------------------------------------------------

Things Needed:
  1. Hex Editor (Any) - For bytes patching
  2. Unluac - Download Here - For decompiling our lua so we can compile it back and compare the hex
  3. LuacXX - XX = lua version, will be explained how to find the version later - To compile our decompiled lua
  4. Any text editor (Notepad++ preffered)
  5. Any java version installed (Newest preffered)
  6. And at least modding experience
-----------------------------------------------------------------------------------------------

Before we start, I just want to create an sample lua to make the tutorial easier.
This will be the code:
Code:
function getRandomScore()
        return math.random(0, 999999)--Return a random number from 0 to 999999
end
print(getRandomScore())
This code will print a random value that generated by getRandomScore function and print it, our purpose is to mod the function so it will always return 999999

-----------------------------------------------------------------------------------------------


How to determine the LUA version:

Please notice that the lua must be not on any protected condition (for example encrypted)Open up your hex editor and open the lua file (I'm using HxD for this test):
upload_2018-4-17_21-53-35.png

Take a look at the first 5 bytes. the 4th byte / byte on offset 0x4 is our version.So if the byte is 52, it means our lua version is lua5.2.

Simple right?

-----------------------------------------------------------------------------------------------

Modding steps:

Lua Decompiling
Open your command prompt and write this:
Code:
java -jar "<unluac path>" "<compiled lua path>">"<output path>"
Example:
Code:
java -jar "C:\unluac.jar" "C:\LuaTest.lua">"C:\LuaTest-dec.lua"
Voila! Example Result:
upload_2018-4-17_22-4-49.png
Function modification
Let's open our decompiled lua file with our text editor
Now, we know that L0_0 is getRandomScore function, we can see it by unluac has created a global variable "getRandomScore" and set it with L0_0, so it means that L0_0 is getRandomScore
As our purpose, we will make the function to always return 999999, so let's modif the L0_0 function from
Code:
 function L0_0()
        return math.random(0, 999999)
end
to
Code:
 function L0_0()
        return 999999
end
upload_2018-4-17_22-10-42.png

Save the file and you are done
Compiling back the modified lua
Open your command prompt and write this
Code:
"<luac path> -o <output lua path> <input lua path>"
Example:
Code:
"C:\uac52.exe -o LuaTest-dec.lua LuaTest.luac"
Y
ou are done!
Final Step, Hex Comparing and Patching Bytes
Take a look at file comparision below
upload_2018-4-17_22-18-10.png

You can see that blocked bytes from 1st file is shorter than the 2nd file, because the getRandomScore function on the 2nd file is already modified, so to patch the bytes. block our bytes on 2nd file from "01 02 0B" to "80 00 03" the copy it.
Block our bytes from 1st file from "01 02 07" to "80 00 02" and paste our byte, so the 1st file should looks like this:
upload_2018-4-17_22-21-28.png

To check if your 1st file is modified with correct bytes, just decompile it with unluac and if there is error, it means you patched wrong bytes or wrong start-end offset


That's it for compiled lua modding!!

-----------------------------------------------------------------------------------------------

FAQ:
Q: "Why don't we just use the new compiled lua?"
A: "In some case, yes you can use it. but not for well-coded-structured lua"

Q: "Will it work on any lua version?"
A: "Yes"

Q: "I got an error, what should I do?"
A: "Try patching the bytes from different start-offset to different end-offset"

-----------------------------------------------------------------------------------------------

If you have any questions, just feel free to reply on this thread =D

 
Last edited by a moderator:
Hi Yuuki im a big fan of you from ph scripting MLBB hacks. Can we have a talk about your vip cheat? Im willing to buy for personal use only. Im wishing you can notice me Thanks in advance
 
Tags Tags
lua
Back
Top Bottom