Advertisement
StefanBashkir

Partial Lua Binary Disassembler

Mar 24th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.97 KB | None | 0 0
  1. local function TestFunction(a,b,c)
  2.     local A = workspace:FindFirstChild("Nonexistent");
  3.     return A;
  4. end
  5.  
  6. local PrecompiledChunk = ("").dump(TestFunction);
  7.  
  8. -- Pre-compiled chunk reading
  9. -- Lua binary chunk reading is simple for those who understand basic computer theory and bit marking.
  10. -- The format of Lua 5.1's binary structure can be found:
  11. --      http://luaforge.net/docman/83/98/ANoFrillsIntroToLua51VMInstructions.pdf
  12. local Lexer = 1;
  13.  
  14. function ReadNextByte()
  15.     local Byte = PrecompiledChunk:sub(Lexer,Lexer):byte();
  16.     Lexer = Lexer + 1
  17.     return Byte;
  18. end
  19.  
  20. function ReadNextByte2()
  21.     local B1 = ReadNextByte();
  22.     local B2 = ReadNextByte();
  23.     return (B2 * 256) + B1
  24. end
  25.  
  26. function ReadNextByte4()
  27.     local B1 = ReadNextByte();
  28.     local B2 = ReadNextByte();
  29.     local B3 = ReadNextByte();
  30.     local B4 = ReadNextByte();
  31.     return (B4 * 16777216 ) + (B3 * 65536 ) + (B2 * 256) + B1
  32. end
  33.  
  34. function ReadNextByte8()
  35.     local B1 = ReadNextByte();
  36.     local B2 = ReadNextByte();
  37.     local B3 = ReadNextByte();
  38.     local B4 = ReadNextByte();
  39.     local B5 = ReadNextByte();
  40.     return (B5 * 4294967296 ) + (B4 * 16777216 ) + (B3 * 65536 ) + (B2 * 256) + B1
  41. end
  42.  
  43. function ReadString(LengthOfString)
  44.     local String = PrecompiledChunk:sub(Lexer, Lexer + (LengthOfString - 1));
  45.     Lexer = Lexer + LengthOfString
  46. end
  47.  
  48. -- Lua Header
  49. -- The Lua header is always 12 bytes, no matter what.
  50. local Identification = ReadString(4);
  51. local VersionNumber = ReadNextByte();
  52. local FormatVersion = ReadNextByte();
  53. local EndianFlag = ReadNextByte();
  54. local SizeOfInt = ReadNextByte();
  55. local Size_T = ReadNextByte();
  56. local SizeOfInstruction = ReadNextByte() -- Lua Assembly OpCode sizes
  57. local SizeOfLuaNumber = ReadNextByte();
  58. local IntFlag = ReadNextByte();
  59. print(SizeOfInt, Size_T, SizeOfInstruction); -- Should print three 4's. This means it is correct.
  60. -- That totals to 12 bytes. Success
  61.  
  62. -- The next step is to get the source name. The size of the string is defined by Size_T. It is defaulted to 4.
  63. -- Obtain the string size like this:
  64. local SizeOfSourceName = (Size_T == 4) and ReadNextByte4() or ReadNextByte8();
  65. print(SizeOfSourceName);
  66.  
  67. -- With the size of the string, loop through each position by 1 byte
  68. local SourceName = "";
  69. for i = 1, SizeOfSourceName do
  70.     SourceName = SourceName .. ("").char(ReadNextByte());
  71. end
  72. print(SourceName); -- Should print the source name of this script (about the same as script:GetFullName());
  73.  
  74. -- The next two function proto's are the line definitions of the ORIGINAL dumped function.
  75. -- It is optional debug data. The size of the intenger in the binary dump are define by SizeOfInt (default 4).
  76. local LineDefined = (SizeOfInt == 4) and ReadNextByte4() or ReadNextByte8();
  77. local LineEnded = (SizeOfInt == 4) and ReadNextByte4() or ReadNextByte8();
  78. print(LineDefined, LineEnded);
  79.  
  80. -- The following is the number of upvalues the function had, and number of parameters:
  81. local NumOfUpvalues = ReadNextByte();
  82. local NumOfParameters = ReadNextByte();
  83. print(NumOfUpvalues, NumOfParameters);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement