有些漏洞利用代碼需要在某個保護模式被關閉的情況下才可以利用成功,在此之前需要得到程式開啟了何種保護方式,驗證其實有很多方法,其原理是讀入PE檔案頭部結構,找到OPTIONAL_HEADER.DllCharacteristics結構,通過與不同的運算元與運算得到,LyScript插件完全可以實作這個驗證功能,實作起來也是很簡單的,
LyScript專案地址:https://github.com/lyshark/LyScript
驗證PE保護方式: 驗證自身保護方式無需要遍歷加載過的模塊,讀入DllCharacteristics并依次與運算元與運算得到主程式的保護方式,
from LyScript32 import MyDebug
import pefile
if __name__ == "__main__":
# 初始化
dbg = MyDebug()
dbg.connect()
# 根據text節得到程式首地址
base = dbg.get_base_from_address(dbg.get_local_base())
byte_array = bytearray()
for index in range(0,4096):
read_byte = dbg.read_memory_byte(base + index)
byte_array.append(read_byte)
oPE = pefile.PE(data = https://www.cnblogs.com/LyShark/archive/2022/08/03/byte_array)
# 隨機基址 => hex(pe.OPTIONAL_HEADER.DllCharacteristics) & 0x40 == 0x40
if ((oPE.OPTIONAL_HEADER.DllCharacteristics & 64) == 64):
print("基址隨機化: True")
else:
print("基址隨機化: False")
# 資料不可執行 DEP => hex(pe.OPTIONAL_HEADER.DllCharacteristics) & 0x100 == 0x100
if ((oPE.OPTIONAL_HEADER.DllCharacteristics & 256) == 256):
print("DEP保護狀態: True")
else:
print("DEP保護狀態: False")
# 強制完整性=> hex(pe.OPTIONAL_HEADER.DllCharacteristics) & 0x80 == 0x80
if ((oPE.OPTIONAL_HEADER.DllCharacteristics & 128) == 128):
print("強制完整性: True")
else:
print("強制完整性: False")
if ((oPE.OPTIONAL_HEADER.DllCharacteristics & 1024) == 1024):
print("SEH例外保護: True")
else:
print("SEH例外保護: False")
dbg.close()
程式運行后即可輸出,當前主程式內啟用了何種保護方式:

如果需要驗證當前程式加載的所有模塊,則可以通過dbg.get_all_module()遍歷加載過的模塊,并依次讀入DllCharacteristics與運算元進行與運算得到保護方式,
from LyScript32 import MyDebug
import pefile
if __name__ == "__main__":
# 初始化
dbg = MyDebug()
dbg.connect()
# 得到所有加載過的模塊
module_list = dbg.get_all_module()
print("-" * 100)
print("模塊名 \t\t\t 基址隨機化 \t\t DEP保護 \t\t 強制完整性 \t\t SEH例外保護 \t\t")
print("-" * 100)
for module_index in module_list:
print("{:15}\t\t".format(module_index.get("name")),end="")
# 依次讀入程式所載入的模塊
byte_array = bytearray()
for index in range(0, 4096):
read_byte = dbg.read_memory_byte(module_index.get("base") + index)
byte_array.append(read_byte)
oPE = pefile.PE(data=https://www.cnblogs.com/LyShark/archive/2022/08/03/byte_array)
# 隨機基址 => hex(pe.OPTIONAL_HEADER.DllCharacteristics) & 0x40 == 0x40
if ((oPE.OPTIONAL_HEADER.DllCharacteristics & 64) == 64):
print("True\t\t\t",end="")
else:
print("False\t\t\t",end="")
# 資料不可執行 DEP => hex(pe.OPTIONAL_HEADER.DllCharacteristics) & 0x100 == 0x100
if ((oPE.OPTIONAL_HEADER.DllCharacteristics & 256) == 256):
print("True\t\t\t",end="")
else:
print("False\t\t\t",end="")
# 強制完整性=> hex(pe.OPTIONAL_HEADER.DllCharacteristics) & 0x80 == 0x80
if ((oPE.OPTIONAL_HEADER.DllCharacteristics & 128) == 128):
print("True\t\t\t",end="")
else:
print("False\t\t\t",end="")
if ((oPE.OPTIONAL_HEADER.DllCharacteristics & 1024) == 1024):
print("True\t\t\t",end="")
else:
print("False\t\t\t",end="")
print()
dbg.close()
驗證得到的保護放入串列如下:

得到了程式開啟的保護方式以后,就可以對癥下藥,提前判斷出漏洞攻擊后是否可以反彈了,
文章出處:https://www.cnblogs.com/LyShark/p/16547097.html著作權宣告:本博客文章與代碼均為學習時整理的筆記,文章 [均為原創] 作品,轉載請 [添加出處] ,您添加出處是我創作的動力!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/500805.html
標籤:其他
