ToB企服应用市场:ToB评测及商务社交产业平台

标题: LyScript 从文本中读写ShellCode [打印本页]

作者: 篮之新喜    时间: 2022-9-6 09:12
标题: LyScript 从文本中读写ShellCode
LyScript 插件通过配合内存读写,可实现对特定位置的ShellCode代码的导出,或者将一段存储在文本中的ShellCode代码插入到程序堆中,此功能可用于快速将自己编写的ShellCode注入到目标进程中,以用于后续测试工作。
LyScript项目地址:https://github.com/lyshark/LyScript
将本地ShellCode注入到堆中: 第一种用法是将一个本地文本中的ShellCode代码导入到堆中。
首先准备一个文本文件,将生成的shellcode放入文件内。

然后可以循环读取文本,并逐个将shellcode注入到目标堆空间中。
  1. from LyScript32 import MyDebug
  2. # 将shellcode读入内存
  3. def read_shellcode(path):
  4.     shellcode_list = []
  5.     with open(path,"r",encoding="utf-8") as fp:
  6.         for index in fp.readlines():
  7.             shellcode_line = index.replace('"',"").replace(" ","").replace("\n","").replace(";","")
  8.             for code in shellcode_line.split("\\x"):
  9.                 if code != "" and code != "\\n":
  10.                     shellcode_list.append("0x" + code)
  11.     return shellcode_list
  12. if __name__ == "__main__":
  13.     dbg = MyDebug()
  14.     dbg.connect()
  15.     # 开辟堆空间
  16.     address = dbg.create_alloc(1024)
  17.     print("开辟堆空间: {}".format(hex(address)))
  18.     if address == False:
  19.         exit()
  20.     # 设置内存可执行属性
  21.     dbg.set_local_protect(address,32,1024)
  22.     # 从文本中读取shellcode
  23.     shellcode = read_shellcode("d://shellcode.txt")
  24.     # 循环写入到内存
  25.     for code_byte in range(0,len(shellcode)):
  26.         bytef = int(shellcode[code_byte],16)
  27.         dbg.write_memory_byte(code_byte + address, bytef)
  28.     # 设置EIP位置
  29.     dbg.set_register("eip",address)
  30.    
  31.     input()
  32.     dbg.delete_alloc(address)
  33.     dbg.close()
复制代码
执行后,堆空间内会自动填充。

如果把这个过程反过来,就是将特定位置的汇编代码保存到本地。
  1. from LyScript32 import MyDebug
  2. # 将特定内存保存到文本中
  3. def write_shellcode(dbg,address,size,path):
  4.     with open(path,"a+",encoding="utf-8") as fp:
  5.         for index in range(0, size - 1):
  6.             # 读取机器码
  7.             read_code = dbg.read_memory_byte(address + index)
  8.             if (index+1) % 16 == 0:
  9.                 print("\\x" + str(read_code))
  10.                 fp.write("\\x" + str(read_code) + "\n")
  11.             else:
  12.                 print("\\x" + str(read_code),end="")
  13.                 fp.write("\\x" + str(read_code))
  14. if __name__ == "__main__":
  15.     dbg = MyDebug()
  16.     dbg.connect()
  17.     eip = dbg.get_register("eip")
  18.     write_shellcode(dbg,eip,128,"d://lyshark.txt")
  19.     dbg.close()
复制代码
写出后的文件如下:


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4