-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathida_patch_applier.py
More file actions
66 lines (53 loc) · 2.08 KB
/
ida_patch_applier.py
File metadata and controls
66 lines (53 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
IDA Pro 补丁应用脚本
读取 angr 分析生成的补丁信息 JSON 文件,并在 IDA Pro 中应用这些补丁
用于修复 OLLVM 混淆的分支指令
"""
import ida_funcs
import idautils
def undefine_and_redefine_function(start, end):
"""
删除并重新定义函数范围
用于在应用补丁前清理函数定义
Args:
start: 函数起始地址
end: 函数结束地址(不包含)
"""
# 步骤1: 找到并删除范围[start, end)内的所有函数
for func_start in idautils.Functions(start, end):
if ida_funcs.del_func(func_start):
print(f"Deleted function at {hex(func_start)}")
else:
print(f"Failed to delete function at {hex(func_start)}")
# 步骤2: 添加新函数从start到end (end为exclusive)
if ida_funcs.add_func(start, end):
print(f"Successfully added new function from {hex(start)} to {hex(end)}")
else:
print(f"Failed to add new function from {hex(start)} to {hex(end)}")
# 示例调用: 替换为实际地址
undefine_and_redefine_function(0x307A38,0x30cdfc)
import json
import idc
import ida_name
import idaapi
from keystone import *
# 初始化 Keystone 汇编器(ARM64架构)
ks = keystone.Ks(KS_ARCH_ARM64, KS_MODE_LITTLE_ENDIAN)
# 读取补丁信息 JSON 文件
patch_info = json.loads(open(r"D:\Downloads\test\patch_info.json").read())
# 遍历所有补丁信息并应用
for item in patch_info:
print("patch " + hex(item["addr"]))
if item.get("sym"):
# 如果补丁信息包含符号名,通过符号名获取地址
target_ea = idc.get_name_ea_simple(item["sym"])
if target_ea == idc.BADADDR or target_ea == idaapi.BADADDR:
print("not find " + item["sym"])
continue
# 生成 bl 指令(函数调用)
asm_inst = "bl " + hex(target_ea)
code = ks.asm(asm_inst, item["addr"], True)[0]
idaapi.patch_bytes(item["addr"], code)
else:
# 如果补丁信息包含机器码,直接应用
idaapi.patch_bytes(item["addr"], bytes.fromhex(item["code"]))