利用LyScript实现应用层钩子扫描器
作者:lyshark 时间:2023-01-10 16:28:39
Capstone 是一个轻量级的多平台、多架构的反汇编框架,该模块支持目前所有通用操作系统,反汇编架构几乎全部支持,本篇文章将运用LyScript插件结合Capstone反汇编引擎实现一个钩子扫描器。
要实现应用层钩子扫描,我们需要得到程序内存文件的机器码以及磁盘中的机器码,并通过capstone这个第三方反汇编引擎,对两者进行反汇编,最后逐条对比汇编指令,实现进程钩子扫描的效果。
LyScript项目地址:https://github.com/lyshark/LyScript
通过LyScript插件读取出内存中的机器码,然后交给第三方反汇编库执行,并将结果输出成字典格式。
#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()
# 读取数据
for index in range(offset,len):
char = dbg.read_memory_byte(address + index)
ref_memory_list.append(char)
# 执行反汇编
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()
效果如下:
我们将文件反汇编也写一下,然后让其对比,这样就可以实现扫描内存与文件中的汇编指令是否一致。
#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()
# 读取数据
for index in range(offset,len):
char = dbg.read_memory_byte(address + index)
ref_memory_list.append(char)
# 执行反汇编
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")
# 循环对比内存与文件中的机器码
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()
此处如果一致,则说明没有钩子,如果不一致则输出,这里的输出结果不一定准确,此处只是抛砖引玉。
来源:https://www.cnblogs.com/LyShark/p/16548656.html
标签:LyScript,应用层,钩子,扫描器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
基于Python采集爬取微信公众号历史数据
2023-04-11 15:16:13
![](https://img.aspxhome.com/file/2023/2/82762_0s.png)
JavaScript如何获取一个元素的样式信息
2023-08-28 12:16:17
Python标准库使用OrderedDict类的实例讲解
2022-07-17 22:27:31
浏览器右下角弹出提示窗口
2008-10-30 12:37:00
python实现两张图片拼接为一张图片并保存
2023-01-26 17:56:52
![](https://img.aspxhome.com/file/2023/4/107684_0s.png)
一个小技巧mysql命令行分页
2011-01-29 16:33:00
css样式表实现首写字母大写
2007-10-08 12:11:00
Django自定义YamlField实现过程解析
2021-01-14 16:43:20
F.conv2d pytorch卷积计算方式
2021-06-26 07:38:41
![](https://img.aspxhome.com/file/2023/4/100854_0s.jpg)
Python脚本如何在bilibili中查找弹幕发送者
2021-05-18 21:46:15
![](https://img.aspxhome.com/file/2023/8/123748_0s.jpg)
Python素数检测实例分析
2021-05-22 02:11:41
CSS的渲染效率:书写高效的CSS
2008-11-13 16:55:00
Langchain集成管理prompt功能详解
2022-12-13 22:56:31
30秒学会30个超实用Python代码片段【收藏版】
2021-08-04 17:13:32
go语言算法题解二叉树的拷贝、镜像和对称
2024-04-28 10:45:18
![](https://img.aspxhome.com/file/2023/0/134140_0s.jpg)
GO项目配置与使用的方法步骤
2024-05-22 17:45:57
![](https://img.aspxhome.com/file/2023/6/125096_0s.png)
关于python中readlines函数的参数hint的相关知识总结
2023-12-31 02:37:12
![](https://img.aspxhome.com/file/2023/5/63565_0s.png)
Linux下修改MySQL编码的方法
2024-01-27 02:36:15
![](https://img.aspxhome.com/file/2023/2/112892_0s.jpg)
通过Pandas读取大文件的实例
2023-12-25 21:18:31
基于JS实现html中placeholder属性提示文字效果示例
2023-09-07 22:50:58
![](https://img.aspxhome.com/file/2023/0/56130_0s.gif)