当前位置: 首页 > 后端技术 > Python

LyScript插件实现自定义反汇编

时间:2023-03-25 21:55:10 Python

LyScript插件默认提供了一个get_disasm_code()方法直接获取指定行数的反汇编代码,但是如果需要自定义获取或者需要封装一个反汇编方法yourself,youcan可以通过以下两种方式获得。插件地址:https://github.com/lyshark/Ly...第一步直接获取指定EIP位置的反汇编代码。这段代码可以这样写。fromLyScript32importMyDebugif__name__=="__main__":dbg=MyDebug()conn=dbg.connect()#获取当前EIP地址eip=dbg.get_register("eip")print("eip={}".format(hex(eip)))#向下反汇编字节数count=eip+15whileTrue:#每次获取dissasm命令dissasm=dbg.get_disasm_one_code(eip)print("0x{:08x}|{}".format(eip,dissasm))#判断是否满足退出条件ifeip>=count:breakelse:#获取这段反汇编代码的长度dis_size=dbg.assemble_code_size(dissasm)eip=eip+dis_sizedbg.close()passoutputTheeffect如下。第二步,获取当前EIP机器码,获取当前EIP指针所在机器码,可以灵活使用反汇编代码组合实现。fromLyScript32importMyDebug#获取机器码defGetHexCode(dbg,address):ref_bytes=[]#先获取反汇编指令,再获取指令长度asm_len=dbg.assemble_code_size(dbg.get_disasm_one_code(address))#循环获取range(0,asm_len)中索引的每个A机器码:ref_bytes.append(dbg.read_memory_byte(address))address=address+1returnref_bytesif__name__=="__main__":dbg=MyDebug()conn=dbg.connect()#获取当前EIP地址eip=dbg.get_register("eip")print("eip={}".format(hex(eip)))#获取机器码ref=GetHexCode(dbg,eip)foriinrange(0,len(ref)):print("0x{:02x}".format(ref[i]),end="")dbg.close()pass的输出如下:如果将以上两种方法组合在一起,则可以在x64dbg反汇编窗口中得到三个主要参数区的内容。fromLyScript32importMyDebug#获取机器码defGetHexCode(dbg,address):ref_bytes=[]#先获取反汇编指令,再获取指令长度asm_len=dbg.assemble_code_size(dbg.get_disasm_one_code(address))#循环获取range(0,asm_len)中索引的每个A机器码:ref_bytes.append(dbg.read_memory_byte(address))address=address+1returnref_bytesif__name__=="__main__":dbg=MyDebug()conn=dbg.connect()#获取当前EIP地址eip=dbg.get_register("eip")print("eip={}".format(hex(eip)))#向下反汇编字节数count=eip+20whileTrue:#everyGetadissasminstructiondissasm=dbg.get_disasm_one_code(eip)print("0x{:08x}|{:50}|".format(eip,dissasm),end="")#获取机器码ref=GetHexCode(dbg,eip)foriinrange(0,len(ref)):print("0x{:02x}".format(ref[i]),end="")print()#判断是否是如果eip>=count:break满足退出条件else:#获取本次反汇编代码的长度dis_size=dbg.assemble_code_size(dissasm)eip=eip+dis_sizedbg.close()pass获取效果图如下: