利用LyScript實現(xiàn)應(yīng)用層鉤子掃描器
Capstone 是一個輕量級的多平臺、多架構(gòu)的反匯編框架,該模塊支持目前所有通用操作系統(tǒng),反匯編架構(gòu)幾乎全部支持,本篇文章將運(yùn)用LyScript插件結(jié)合Capstone反匯編引擎實現(xiàn)一個鉤子掃描器。
要實現(xiàn)應(yīng)用層鉤子掃描,我們需要得到程序內(nèi)存文件的機(jī)器碼以及磁盤中的機(jī)器碼,并通過capstone這個第三方反匯編引擎,對兩者進(jìn)行反匯編,最后逐條對比匯編指令,實現(xiàn)進(jìn)程鉤子掃描的效果。
LyScript項目地址:https://github.com/lyshark/LyScript
通過LyScript插件讀取出內(nèi)存中的機(jī)器碼,然后交給第三方反匯編庫執(zhí)行,并將結(jié)果輸出成字典格式。
#coding: utf-8
import binascii,os,sys
import pefile
from capstone import *
from LyScript32 import MyDebug
# 得到內(nèi)存反匯編代碼
def get_memory_disassembly(address,offset,len):
# 反匯編列表
dasm_memory_dict = []
# 內(nèi)存列表
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)))
# 得到內(nèi)存反匯編代碼
dasm_memory_list = get_memory_disassembly(pe_base,0,pe_size)
print(dasm_memory_list)
dbg.close()
效果如下:

我們將文件反匯編也寫一下,然后讓其對比,這樣就可以實現(xiàn)掃描內(nèi)存與文件中的匯編指令是否一致。
#coding: utf-8
import binascii,os,sys
import pefile
from capstone import *
from LyScript32 import MyDebug
# 得到內(nèi)存反匯編代碼
def get_memory_disassembly(address,offset,len):
# 反匯編列表
dasm_memory_dict = []
# 內(nèi)存列表
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
# 反匯編文件中的機(jī)器碼
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)))
# 得到內(nèi)存反匯編代碼
dasm_memory_list = get_memory_disassembly(pe_base,0,pe_size)
dasm_file_list = get_file_disassembly("d://win32project1.exe")
# 循環(huán)對比內(nèi)存與文件中的機(jī)器碼
for index in range(0,len(dasm_file_list)):
if dasm_memory_list[index] != dasm_file_list[index]:
print("地址: {:8} --> 內(nèi)存反匯編: {:32} --> 磁盤反匯編: {:32}".
format(dasm_memory_list[index].get("address"),dasm_memory_list[index].get("opcode"),dasm_file_list[index].get("opcode")))
dbg.close()
此處如果一致,則說明沒有鉤子,如果不一致則輸出,這里的輸出結(jié)果不一定準(zhǔn)確,此處只是拋磚引玉。

到此這篇關(guān)于利用LyScript實現(xiàn)應(yīng)用層鉤子掃描器的文章就介紹到這了,更多相關(guān)LyScript應(yīng)用層鉤子掃描器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python圖像處理庫crop()函數(shù)?thumbnail方法使用詳解
這篇文章主要為大家介紹了Python圖像處理庫crop()函數(shù)?thumbnail方法使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
python圖片剪裁代碼(圖片按四個點坐標(biāo)剪裁)
這篇文章主要介紹了python圖片剪裁代碼(圖片按四個點坐標(biāo)剪裁),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
PyTorch如何創(chuàng)建自己的數(shù)據(jù)集
這篇文章主要介紹了PyTorch如何創(chuàng)建自己的數(shù)據(jù)集,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
使用pyinstaller打包.exe文件的詳細(xì)教程
PyInstaller是一個跨平臺的Python應(yīng)用打包工具,能夠把 Python 腳本及其所在的 Python 解釋器打包成可執(zhí)行文件,下面這篇文章主要給大家介紹了關(guān)于使用pyinstaller打包.exe文件的相關(guān)資料,需要的朋友可以參考下2022-04-04
Python+騰訊云服務(wù)器實現(xiàn)每日自動健康打卡
本文主要介紹了通過Python+騰訊云服務(wù)器實現(xiàn)每日自動健康打卡,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12
python對Excel按條件進(jìn)行內(nèi)容補(bǔ)充(推薦)
這篇文章主要介紹了python對Excel按條件進(jìn)行內(nèi)容補(bǔ)充的相關(guān)知識,非常不錯,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11

