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

LyScript实现绕过反调试保护

时间:2023-03-26 15:43:22 Python

LyScript插件中内置的方法可以实现各种反调试和屏蔽特定的API函数。这类函数在对付病毒等恶意程序时非常有效,例如,当程序调用特定的API函数时,我们可以对其进行拦截,从而保护系统在调试过程中不被破坏。插件地址:https://github.com/lyshark/Ly...绕过反调试机制:最常用的反调试机制是使用IsDebuggerPresent标志检查PEB+2处的内容,如果它为1,表示正在Debugging,我们运行脚本直接设置为0,绕过反调试机制。即+2在进程环境块中的位置,这里是一个字节标志,反调试机制是,程序调用IsDebuggerPresent检查这里的标志,如果为1,说明程序正在调试,如果为0,说明没有在调试Debug,只需要在运行前设置为0即可绕过反调试。fromLyScript32importMyDebugif__name__=="__main__":#初始化dbg=MyDebug()dbg.connect()#通过PEB找到debugflagpeb=dbg.get_peb_address(dbg.get_process_id())print("Debugflagaddress:0x{:x}".format(peb+2))flag=dbg.read_memory_byte(peb+2)print("Debugflag:{}".format(flag))#将调试标志设置为0以通过反调试nop_debug=dbg.write_memory_byte(peb+2,0)print("Anti-debugbypassstatus:{}".format(nop_debug))dbg.close()将程序加载到调试器中,运行上面的脚本,然后Run程序,你会发现反调试被绕过了。其次,我们还可以在函数开头动态写subeax、eax、ret指令,这样当程序要调用特定函数时,直接返回退出,从而达到屏蔽函数的目的执行。fromLyScript32importMyDebug#获取需要的机器码defset_assemble_opcde(dbg,address):#获取第一行的长度opcode_size=dbg.assemble_code_size("subeax,eax")#编写汇编指令dbg.assemble_at(address,"subeax,eax")dbg.assemble_at(address+opcode_size,"ret")if__name__=="__main__":#初始化dbg=MyDebug()dbg.connect()#获取函数的内存地址process32first=dbg.get_module_from_function("kernel32","Process32FirstW")process32next=dbg.get_module_from_function("kernel32","Process32NextW")messagebox=dbg.get_module_from_function("user32.dll","MessageBoxA")messageboxw=dbg.get_module_from_function("ll"MessageBoxW")print(hex(messagebox),"",hex(messageboxw))#替换函数位置为subeax,eaxretset_assemble_opcde(dbg,messagebox)set_assemble_opcde(dbg,messageboxw)dbg.close()如上,我们是在弹窗的位置写上return命令,然后运行程序,你会发现不会出现弹窗,也就屏蔽了这个功能。同理,绕过进程枚举仍然可以通过这种方式实现。绕过进程枚举:病毒会枚举所有正在运行的进程,以确认是否有调试器在运行。我们可以在特定函数的开头写SUBEAX,EAXRET指令会使其无法调用枚举函数从而失败。fromLyScript32importMyDebug#获取需要的机器码defset_assemble_opcde(dbg,address):#获取第一行的长度opcode_size=dbg.assemble_code_size("subeax,eax")#编写汇编指令dbg.assemble_at(address,"subeax,eax")dbg.assemble_at(address+opcode_size,"ret")if__name__=="__main__":#初始化dbg=MyDebug()dbg.connect()#获取函数的内存地址process32first=dbg.get_module_from_function("kernel32","Process32FirstW")process32next=dbg.get_module_from_function("kernel32","Process32NextW")打印("process32first=0x{:x}|process32next=0x{:x}".format(process32first,process32next))#替换函数的位置是subeax,eaxretset_assemble_opcde(dbg,process32first)set_assemble_opcde(dbg,process32next)dbg.close()