日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達者為師

網(wǎng)站首頁 編程語言 正文

利用LyScript實現(xiàn)應(yīng)用層鉤子掃描器_python

作者:lyshark ? 更新時間: 2022-09-29 編程語言

Capstone 是一個輕量級的多平臺、多架構(gòu)的反匯編框架,該模塊支持目前所有通用操作系統(tǒng),反匯編架構(gòu)幾乎全部支持,本篇文章將運用LyScript插件結(jié)合Capstone反匯編引擎實現(xiàn)一個鉤子掃描器。

要實現(xiàn)應(yīng)用層鉤子掃描,我們需要得到程序內(nèi)存文件的機器碼以及磁盤中的機器碼,并通過capstone這個第三方反匯編引擎,對兩者進行反匯編,最后逐條對比匯編指令,實現(xiàn)進程鉤子掃描的效果。

LyScript項目地址:https://github.com/lyshark/LyScript

通過LyScript插件讀取出內(nèi)存中的機器碼,然后交給第三方反匯編庫執(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

# 反匯編文件中的機器碼
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)存與文件中的機器碼
    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)確,此處只是拋磚引玉。

原文鏈接:https://www.cnblogs.com/LyShark/p/16548656.html

欄目分類
最近更新