LyScript 插件通過配合記憶體讀寫,可實作對特定位置的ShellCode代碼的匯出,或者將一段存盤在文本中的ShellCode代碼插入到程式堆中,此功能可用于快速將自己撰寫的ShellCode注入到目標行程中,以用于后續測驗作業,
LyScript專案地址:https://github.com/lyshark/LyScript
將本地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()
寫出后的檔案如下:

著作權宣告:本博客文章與代碼均為學習時整理的筆記,文章 [均為原創] 作品,轉載請 [添加出處] ,您添加出處是我創作的動力!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/500799.html
標籤:訊息安全
