LuaPytecode/luac.py

268 lines
8.3 KiB
Python
Raw Normal View History

2019-08-14 20:19:03 +00:00
import struct
import array
lua_opcode_types = [
"ABC", "ABx", "ABC", "ABC",
"ABC", "ABx", "ABC", "ABx",
"ABC", "ABC", "ABC", "ABC",
"ABC", "ABC", "ABC", "ABC",
"ABC", "ABC", "ABC", "ABC",
"ABC", "ABC", "AsBx", "ABC",
"ABC", "ABC", "ABC", "ABC",
"ABC", "ABC", "ABC", "AsBx",
"AsBx", "ABC", "ABC", "ABC",
"ABx", "ABC"
]
lua_opcode_names = [
"MOVE", "LOADK", "LOADBOOL", "LOADNIL",
"GETUPVAL", "GETGLOBAL", "GETTABLE", "SETGLOBAL",
"SETUPVAL", "SETTABLE", "NEWTABLE", "SELF",
"ADD", "SUB", "MUL", "DIV",
"MOD", "POW", "UNM", "NOT",
"LEN", "CONCAT", "JMP", "EQ",
"LT", "LE", "TEST", "TESTSET",
"CALL", "TAILCALL", "RETURN", "FORLOOP",
"FORPREP", "TFORLOOP", "SETLIST", "CLOSE",
"CLOSURE", "VARARG"
]
# at [p]osition to k
2019-08-14 20:19:03 +00:00
def get_bits(num, p, k):
# convert number into binary first
binary = bin(num)
# remove first two characters
binary = binary[2:]
# fill in missing bits
for i in range(32 - len(binary)):
binary = '0' + binary
end = len(binary) - p
start = len(binary) - k
2019-08-14 20:19:03 +00:00
# extract k bit sub-string
kBitSubStr = binary[start : end]
# convert extracted sub-string into decimal again
2019-08-15 20:04:35 +00:00
return (int(kBitSubStr,2))
2019-08-14 20:47:18 +00:00
class LuaUndump:
2019-08-14 20:19:03 +00:00
def __init__(self):
self.chunks = []
2019-08-15 17:02:55 +00:00
self.chunk = {}
2019-08-14 20:19:03 +00:00
self.index = 0
2019-08-15 17:02:55 +00:00
@staticmethod
def dis_chunk(chunk):
print("==== [[" + str(chunk['NAME']) + "]] ====\n")
for z in chunk['PROTOTYPES']:
print("** decoding proto\n")
LuaUndump.dis_chunk(chunk['PROTOTYPES'][z])
2019-08-15 17:02:55 +00:00
print("\n==== [[" + str(chunk['NAME']) + "'s constants]] ====\n")
for z in chunk['CONSTANTS']:
i = chunk['CONSTANTS'][z]
print(str(z) + ": " + str(i['DATA']))
print("\n==== [[" + str(chunk['NAME']) + "'s dissassembly]] ====\n")
for z in chunk['INSTRUCTIONS']:
i = chunk['INSTRUCTIONS'][z]
if (i['TYPE'] == "ABC"):
print(lua_opcode_names[i['OPCODE']], i['A'], i['B'], i['C'])
elif (i['TYPE'] == "ABx"):
if (i['OPCODE'] == 1 or i['OPCODE'] == 5):
print(lua_opcode_names[i['OPCODE']], i['A'], -i['Bx']-1, chunk['CONSTANTS'][i['Bx']]['DATA'])
else:
print(lua_opcode_names[i['OPCODE']], i['A'], -i['Bx']-1)
elif (i['TYPE'] == "AsBx"):
print("AsBx", lua_opcode_names[i['OPCODE']], i['A'], i['sBx'])
def loadBlock(self, sz):
temp = bytearray(self.bytecode[self.index:self.index+sz])
self.index = self.index + sz
return temp
2019-08-14 20:19:03 +00:00
def get_byte(self):
return self.loadBlock(1)[0]
2019-08-14 20:19:03 +00:00
def get_int32(self):
if (self.big_endian):
return int.from_bytes(self.loadBlock(4), byteorder='big', signed=False)
2019-08-14 20:19:03 +00:00
else:
return int.from_bytes(self.loadBlock(4), byteorder='little', signed=False)
2019-08-14 20:19:03 +00:00
def get_int(self):
if (self.big_endian):
return int.from_bytes(self.loadBlock(self.int_size), byteorder='big', signed=False)
2019-08-14 20:19:03 +00:00
else:
return int.from_bytes(self.loadBlock(self.int_size), byteorder='little', signed=False)
2019-08-14 20:19:03 +00:00
def get_size_t(self):
if (self.big_endian):
return int.from_bytes(self.loadBlock(self.size_t), byteorder='big', signed=False)
2019-08-14 20:19:03 +00:00
else:
return int.from_bytes(self.loadBlock(self.size_t), byteorder='little', signed=False)
2019-08-14 20:19:03 +00:00
2019-08-14 20:47:18 +00:00
def get_double(self):
if self.big_endian:
return struct.unpack('>d', self.loadBlock(8))[0]
2019-08-14 20:47:18 +00:00
else:
return struct.unpack('<d', self.loadBlock(8))[0]
2019-08-14 20:19:03 +00:00
def get_string(self, size):
if (size == None):
size = self.get_size_t()
if (size == 0):
return None
return "".join(chr(x) for x in self.loadBlock(size))
2019-08-14 20:19:03 +00:00
def decode_chunk(self):
chunk = {
'INSTRUCTIONS': {},
'CONSTANTS': {},
'PROTOTYPES': {}
}
chunk['NAME'] = self.get_string(None)
chunk['FIRST_LINE'] = self.get_int()
chunk['LAST_LINE'] = self.get_int()
chunk['UPVALUES'] = self.get_byte()
chunk['ARGUMENTS'] = self.get_byte()
chunk['VARG'] = self.get_byte()
chunk['STACK'] = self.get_byte()
if (not chunk['NAME'] == None):
chunk['NAME'] = chunk['NAME'][1:-1]
# parse instructions
print("** DECODING INSTRUCTIONS")
num = self.get_int()
for i in range(num):
instruction = {
# opcode = opcode number;
# type = [ABC, ABx, AsBx]
# A, B, C, Bx, or sBx depending on type
2019-08-15 17:02:55 +00:00
}
2019-08-14 20:19:03 +00:00
2019-08-15 17:02:55 +00:00
data = self.get_int32()
opcode = get_bits(data, 0, 6)
2019-08-15 17:02:55 +00:00
tp = lua_opcode_types[opcode]
2019-08-14 20:19:03 +00:00
instruction['OPCODE'] = opcode
instruction['TYPE'] = tp
instruction['A'] = get_bits(data, 7, 14)
if instruction['TYPE'] == "ABC":
instruction['B'] = get_bits(data, 24, 31)
2019-08-15 17:02:55 +00:00
instruction['C'] = get_bits(data, 15, 23)
2019-08-14 20:19:03 +00:00
elif instruction['TYPE'] == "ABx":
instruction['Bx'] = get_bits(data, 15, 31)
2019-08-14 20:19:03 +00:00
elif instruction['TYPE'] == "AsBx":
instruction['sBx'] = get_bits(data, 15, 31) - 131071
2019-08-14 20:19:03 +00:00
chunk['INSTRUCTIONS'][i] = instruction
print(lua_opcode_names[opcode], instruction)
# get constants
print("** DECODING CONSTANTS")
2019-08-15 17:02:55 +00:00
num = self.get_int()
2019-08-14 20:19:03 +00:00
for i in range(num):
constant = {
# type = constant type;
# data = constant data;
2019-08-15 17:02:55 +00:00
}
2019-08-14 20:19:03 +00:00
constant['TYPE'] = self.get_byte()
if constant['TYPE'] == 1:
2019-08-15 17:02:55 +00:00
constant['DATA'] = (self.get_byte() != 0)
2019-08-14 20:19:03 +00:00
elif constant['TYPE'] == 3:
2019-08-15 17:02:55 +00:00
constant['DATA'] = self.get_double()
2019-08-14 20:19:03 +00:00
elif constant['TYPE'] == 4:
2019-08-15 17:02:55 +00:00
constant['DATA'] = self.get_string(None)[:-1]
2019-08-14 20:19:03 +00:00
print(constant)
chunk['CONSTANTS'][i] = constant
# parse protos
print("** DECODING PROTOS")
2019-08-15 17:02:55 +00:00
num = self.get_int()
2019-08-14 20:19:03 +00:00
for i in range(num):
chunk['PROTOTYPES'][i] = self.decode_chunk()
# debug stuff
print("** DECODING DEBUG SYMBOLS")
# line numbers
2019-08-15 17:02:55 +00:00
num = self.get_int()
2019-08-14 20:19:03 +00:00
for i in range(num):
self.get_int32()
# locals
2019-08-15 17:02:55 +00:00
num = self.get_int()
2019-08-14 20:19:03 +00:00
for i in range(num):
2019-08-15 17:02:55 +00:00
print(self.get_string(None)[:-1]) # local name
2019-08-14 20:19:03 +00:00
self.get_int32() # local start PC
self.get_int32() # local end PC
# upvalues
2019-08-15 17:02:55 +00:00
num = self.get_int()
2019-08-14 20:19:03 +00:00
for i in range(num):
self.get_string(None) # upvalue name
2019-08-15 17:02:55 +00:00
self.chunks.append(chunk)
2019-08-14 20:19:03 +00:00
return chunk
def decode_rawbytecode(self, rawbytecode):
# bytecode sanity checks
if not rawbytecode[0:4] == b'\x1bLua':
print("Lua Bytecode expected!")
exit(0)
bytecode = array.array('b', rawbytecode)
return self.decode_bytecode(bytecode)
def decode_bytecode(self, bytecode):
self.bytecode = bytecode
# alligns index lol
self.index = 4
self.vm_version = self.get_byte()
2019-08-15 17:02:55 +00:00
self.bytecode_format = self.get_byte()
2019-08-14 20:19:03 +00:00
self.big_endian = (self.get_byte() == 0)
self.int_size = self.get_byte()
self.size_t = self.get_byte()
2019-08-15 17:02:55 +00:00
self.instr_size = self.get_byte() # gets size of instructions
self.l_number_size = self.get_byte() # size of lua_Number
self.integral_flag = self.get_byte()
2019-08-14 20:19:03 +00:00
print("Lua VM version: ", hex(self.vm_version))
print("Big Endian: ", self.big_endian)
print("int_size: ", self.int_size)
print("size_t: ", self.size_t)
#print(self.bytecode)
2019-08-15 17:02:55 +00:00
self.chunk = self.decode_chunk()
return self.chunk
2019-08-14 20:19:03 +00:00
def loadFile(self, luaCFile):
with open(luaCFile, 'rb') as luac_file:
2019-08-14 20:19:03 +00:00
bytecode = luac_file.read()
return self.decode_rawbytecode(bytecode)
2019-08-15 17:02:55 +00:00
def print_dissassembly(self):
LuaUndump.dis_chunk(self.chunk)
2019-08-15 17:02:55 +00:00