关于python:LyScript-从文本中读写ShellCode

6次阅读

共计 1609 个字符,预计需要花费 5 分钟才能阅读完成。

LyScript 插件通过配合内存读写,可实现对特定地位的 ShellCode 代码的导出,或者将一段存储在文本中的 ShellCode 代码插入到程序堆中,此性能可用于疾速将本人编写的 ShellCode 注入到指标过程中,以用于后续测试工作。

  • 插件地址:https://github.com/lyshark/Ly…

将本地 ShellCode 注入到堆中: 第一种用法是将一个本地文本中的 ShellCode 代码导入到堆中。

首先筹备一个文本文件,将生成的 shellcode 放入文件内。

而后能够循环读取文本,并一一将 shellcode 注入到指标堆空间中。

from LyScript32 import MyDebug

# 将 shellcode 读入内存
def read_shellcode(path):
    shellcode_list = []
    with open(path,"r",encoding="utf-8") as fp:
        for index in fp.readlines():
            shellcode_line = index.replace('"',"").replace("","").replace("\n","").replace(";","")
            for code in shellcode_line.split("\\x"):
                if code != ""and code !="\\n":
                    shellcode_list.append("0x" + code)
    return shellcode_list

if __name__ == "__main__":
    dbg = MyDebug()
    dbg.connect()

    # 开拓堆空间
    address = dbg.create_alloc(1024)
    print("开拓堆空间: {}".format(hex(address)))
    if address == False:
        exit()

    # 设置内存可执行属性
    dbg.set_local_protect(address,32,1024)

    # 从文本中读取 shellcode
    shellcode = read_shellcode("d://shellcode.txt")

    # 循环写入到内存
    for code_byte in range(0,len(shellcode)):
        bytef = int(shellcode[code_byte],16)
        dbg.write_memory_byte(code_byte + address, bytef)

    # 设置 EIP 地位
    dbg.set_register("eip",address)
    
    input()
    dbg.delete_alloc(address)

    dbg.close()

执行后,堆空间内会主动填充。

如果把这个过程反过来,就是将特定地位的汇编代码保留到本地。

from LyScript32 import MyDebug

# 将特定内存保留到文本中
def write_shellcode(dbg,address,size,path):
    with open(path,"a+",encoding="utf-8") as fp:
        for index in range(0, size - 1):
            # 读取机器码
            read_code = dbg.read_memory_byte(address + index)

            if (index+1) % 16 == 0:
                print("\\x" + str(read_code))
                fp.write("\\x" + str(read_code) + "\n")
            else:
                print("\\x" + str(read_code),end="")
                fp.write("\\x" + str(read_code))

if __name__ == "__main__":
    dbg = MyDebug()
    dbg.connect()

    eip = dbg.get_register("eip")
    write_shellcode(dbg,eip,128,"d://lyshark.txt")
    dbg.close()

写出后的文件如下:

正文完
 0