利用LyScript實現(xiàn)應用層鉤子掃描器
Capstone 是一個輕量級的多平臺、多架構的反匯編框架,該模塊支持目前所有通用操作系統(tǒng),反匯編架構幾乎全部支持,本篇文章將運用LyScript插件結合Capstone反匯編引擎實現(xiàn)一個鉤子掃描器。
要實現(xiàn)應用層鉤子掃描,我們需要得到程序內存文件的機器碼以及磁盤中的機器碼,并通過capstone這個第三方反匯編引擎,對兩者進行反匯編,最后逐條對比匯編指令,實現(xiàn)進程鉤子掃描的效果。
LyScript項目地址:https://github.com/lyshark/LyScript
通過LyScript插件讀取出內存中的機器碼,然后交給第三方反匯編庫執(zhí)行,并將結果輸出成字典格式。
#coding: utf-8
import binascii,os,sys
import pefile
from capstone import *
from LyScript32 import MyDebug
# 得到內存反匯編代碼
def get_memory_disassembly(address,offset,len):
# 反匯編列表
dasm_memory_dict = []
# 內存列表
ref_memory_list = bytearray()
# 讀取數(shù)據(jù)
for index in range(offset,len):
char = dbg.read_memory_byte(address + index)
ref_memory_list.append(char)
# 執(zhí)行反匯編
md = Cs(CS_ARCH_X86,CS_MODE_32)
for item in md.disasm(ref_memory_list,0x1):
addr = int(pe_base) + item.address
dasm_memory_dict.append({"address": str(addr), "opcode": item.mnemonic + " " + item.op_str})
return dasm_memory_dict
if __name__ == "__main__":
dbg = MyDebug()
dbg.connect()
pe_base = dbg.get_local_base()
pe_size = dbg.get_local_size()
print("模塊基地址: {}".format(hex(pe_base)))
print("模塊大小: {}".format(hex(pe_size)))
# 得到內存反匯編代碼
dasm_memory_list = get_memory_disassembly(pe_base,0,pe_size)
print(dasm_memory_list)
dbg.close()
效果如下:

我們將文件反匯編也寫一下,然后讓其對比,這樣就可以實現(xiàn)掃描內存與文件中的匯編指令是否一致。
#coding: utf-8
import binascii,os,sys
import pefile
from capstone import *
from LyScript32 import MyDebug
# 得到內存反匯編代碼
def get_memory_disassembly(address,offset,len):
# 反匯編列表
dasm_memory_dict = []
# 內存列表
ref_memory_list = bytearray()
# 讀取數(shù)據(jù)
for index in range(offset,len):
char = dbg.read_memory_byte(address + index)
ref_memory_list.append(char)
# 執(zhí)行反匯編
md = Cs(CS_ARCH_X86,CS_MODE_32)
for item in md.disasm(ref_memory_list,0x1):
addr = int(pe_base) + item.address
dic = {"address": str(addr), "opcode": item.mnemonic + " " + item.op_str}
dasm_memory_dict.append(dic)
return dasm_memory_dict
# 反匯編文件中的機器碼
def get_file_disassembly(path):
opcode_list = []
pe = pefile.PE(path)
ImageBase = pe.OPTIONAL_HEADER.ImageBase
for item in pe.sections:
if str(item.Name.decode('UTF-8').strip(b'\x00'.decode())) == ".text":
# print("虛擬地址: 0x%.8X 虛擬大小: 0x%.8X" %(item.VirtualAddress,item.Misc_VirtualSize))
VirtualAddress = item.VirtualAddress
VirtualSize = item.Misc_VirtualSize
ActualOffset = item.PointerToRawData
StartVA = ImageBase + VirtualAddress
StopVA = ImageBase + VirtualAddress + VirtualSize
with open(path,"rb") as fp:
fp.seek(ActualOffset)
HexCode = fp.read(VirtualSize)
md = Cs(CS_ARCH_X86, CS_MODE_32)
for item in md.disasm(HexCode, 0):
addr = hex(int(StartVA) + item.address)
dic = {"address": str(addr) , "opcode": item.mnemonic + " " + item.op_str}
# print("{}".format(dic))
opcode_list.append(dic)
return opcode_list
if __name__ == "__main__":
dbg = MyDebug()
dbg.connect()
pe_base = dbg.get_local_base()
pe_size = dbg.get_local_size()
print("模塊基地址: {}".format(hex(pe_base)))
print("模塊大小: {}".format(hex(pe_size)))
# 得到內存反匯編代碼
dasm_memory_list = get_memory_disassembly(pe_base,0,pe_size)
dasm_file_list = get_file_disassembly("d://win32project1.exe")
# 循環(huán)對比內存與文件中的機器碼
for index in range(0,len(dasm_file_list)):
if dasm_memory_list[index] != dasm_file_list[index]:
print("地址: {:8} --> 內存反匯編: {:32} --> 磁盤反匯編: {:32}".
format(dasm_memory_list[index].get("address"),dasm_memory_list[index].get("opcode"),dasm_file_list[index].get("opcode")))
dbg.close()
此處如果一致,則說明沒有鉤子,如果不一致則輸出,這里的輸出結果不一定準確,此處只是拋磚引玉。

到此這篇關于利用LyScript實現(xiàn)應用層鉤子掃描器的文章就介紹到這了,更多相關LyScript應用層鉤子掃描器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python 數(shù)據(jù)化運營之KMeans聚類分析總結
這篇文章主要介紹了Python 數(shù)據(jù)化運營KMeans聚類相關的一些總結,感興趣的話一起來閱讀下文吧2021-08-08
Python中str.format()和f-string的使用
本文主要介紹了Python中str.format()和f-string的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-02-02
使用Python爬蟲庫BeautifulSoup遍歷文檔樹并對標簽進行操作詳解
今天為大家介紹下Python爬蟲庫BeautifulSoup遍歷文檔樹并對標簽進行操作的詳細方法與函數(shù)2020-01-01

