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

LyScript内存扫描和查壳实现

时间:2023-03-25 21:45:05 Python

LyScript提供了多种内存特征扫描函数,每种扫描函数的使用方式不同。在使用扫描功能时,首先要了解它们之间的区别,如下。介绍各个内存扫描函数的灵活使用方法。最后会实现一个简单版的内存shell检查脚本,可以快速定位到目标程序中添加的shell。插件地址:https://github.com/lyshark/Ly...先来了解一下第一个函数scan_memory_all()的特点,这个函数用来扫描EIP中EIP指向的位置的整个内存段当前进程。feature,如果找到,则返回一个列表,如果没有找到,则返回False,这个函数和scan_memory_one()函数的原理是一致的,唯一不同的是all返回表单中所有匹配的行ofalist,andoneonlyreturnsmatchingrows的第一条记录,这两个函数都支持模糊匹配。如果你加载一个程序,默认停留在系统空域,调用该函数得到的特征记录只能是系统空域中特定dll中的特征集。例如扫描ntdll.dll模块中特征字段为558bec83e4的所有记录,代码如下。fromLyScript32importMyDebugif__name__=="__main__":dbg=MyDebug()conn=dbg.connect()ref_one=dbg.scan_memory_one("558bec83e4")print("扫描一行:{}".format(hex(ref_one)))ref_all=dbg.scan_memory_all("558bec83e4")forindexinrange(0,len(ref_all)):print("Record:{}address:{}".format(index,hex(ref_all[index])))dbg.close()运行效果如下:有时候我们需要指定扫描某个模块,比如扫描进程中的msvcr120.dll模块,特征值里面。这时候需要获取模块的入口地址,然后切换EIP。此时调用scan_memory_all()完成查找。当然最好先备份原来的EIP所在位置,这样扫描完就可以直接切换回来了。fromLyScript32importMyDebugif__name__=="__main__":dbg=MyDebug()conn=dbg.connect()#获取所有模块local_module_base=dbg.get_all_module()forindexinlocal_module_base:#找到需要的模块ifindex.get("name")=="msvcr120.dll":entry=index.get("entry")print("Scanentry:{}".format(hex(entry)))#跳过dbg.set_register("eip",entry)#开始搜索特征scan_ref=dbg.scan_memory_all("5dc20c00558bec")forxinscan_ref:print("Scanto:{}".format(hex(x)))dbg.close()输出结果如下:当然,为了让扫描更高效,新版插件增加了scan_memory_any()函数,可以在不切换到模块入口的情况下扫描特定模块中的特征,但是这个函数只能返回找到的第一条记录,并且需要传入扫描起始位置和扫描长度,不过获取这些参数并不难。fromLyScript32importMyDebugif__name__=="__main__":dbg=MyDebug()conn=dbg.connect()#获取进程模块local_module=dbg.get_all_module()[0]#获取模块参数module_base=local_module.get("base")module_size=local_module.get("size")print("基址:{}长度:{}结束地址:{}".format(hex(module_base),hex(module_size),hex(module_base+module_size)))#扫描内存ref=dbg.scan_memory_any(module_base,module_size,"515ca8f84c3433")ifref!=False:print("Memoryfound:{}".format(hex(ref)))dbg.close()扫描结果如下:如果上面的内存扫描方式都能看懂,那么查壳功能就变得很简单了。市面上的查壳软件PEID基本都是采用特征码定位的方式,所以我们要实现特征码扫描的方式可以查壳,检测编译器的特征。下面的代码可以实现shell校验功能。fromLyScript32importMyDebug#Shellcheckingfunctiondefscan(dbg,string):#获取进程模块local_module=dbg.get_all_module()[0]#获取模块参数module_base=local_module.get("base")module_size=local_module.get("size")#print("baseaddress:{}length:{}endaddress:{}".format(hex(module_base),hex(module_size),hex(module_base+module_size)))#扫描内存ref=dbg.scan_memory_any(module_base,module_size,string)ifref!=False:returnTruereturnFalseif__name__=="__main__":dbg=MyDebug()conn=dbg.connect()#存储签名符号=[{"key":"MicrosoftVisualC++2013","value":"e8????????e9????558bec"},{"key":"UPX3.96w","value":"60be??????8dbe0090ffff57"}]forindexinsigns:check=scan(dbg,index.get("value"))ifcheck==True:print(&qu哦;编译特性:{}".format(index.get("key")))dbg.close()分别测试后输出结果如下:upx加壳软件的输出是vs2013编译特性的输出