-
Notifications
You must be signed in to change notification settings - Fork 0
/
sys_hooker.py
283 lines (249 loc) · 7.64 KB
/
sys_hooker.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import argparse
import json
import frida
import sys
import random
import threading
import time
import os
global_var = """
var sys_data = $$sys_data$$;
var funcs_map = {};
var register_args = ["rcx", "rdx", "r8", "r9"];
"""
common = """
function lib_includes(libname, liblist) {
for (var i = 0; i < liblist.length; i++) {
if (libname.toLowerCase() == liblist[i].toLowerCase()) {
return true
}
}
return false
}
function func_includes(funcname, funclist) {
for (var i = 0; i < funclist.length; i++) {
if (funcname.toLowerCase() == funclist[i].toLowerCase()) {
return true
}
}
return false
}
function mutate_n(n, ratio){
return n + Math.floor(Math.random() * ratio) - ratio/2;
}
function mutate_buf(ptr, size, arg_i) {
let buf = Memory.readByteArray(ptr, size);
let buf_view = new Uint8Array(buf);
// deep copy buf
let new_buf = new ArrayBuffer(size);
let new_buf_view = new Uint8Array(new_buf);
for (let i = 0; i < size; i++) {
if (Math.random() < 0.7) {
new_buf_view[i] = buf_view[i];
}else{
new_buf_view[i] = mutate_n(buf_view[i], 10)
}
}
Memory.writeByteArray(ptr, new_buf_view);
console.log("Mutating buffer arg " + arg_i);
console.log("From:");
console.log(buf);
console.log("To:");
console.log(new_buf);
}
function mutate_args(context, args, f_args_infos) {
let syscall_args = ["rcx", "rdx", "r8", "r9"];
for (let i = 0; i < args.length; i++) {
if (Math.random() < 0.02) {
try{
Memory.readPointer(args[i])
mutate_buf(Memory.readPointer(args[i]), 0x100, i);
}catch(e){
continue;
}
}
else if (i < syscall_args.length && Math.random() < 0.01 ) {
let reg_name = syscall_args[i];
// convert to int
let reg_val = parseInt(args[i]);
let new_val = mutate_n(reg_val, 10000);
context[reg_name] = new_val;
let hex_val = "0x" + new_val.toString(16);
console.log("Mutating " + reg_name + " from " + args[i] + " to " + hex_val);
}
}
}
"""
process_module = """
setTimeout(function() {
Process.enumerateModules({
onMatch: function(module) {
//console.log("Found " + module.name + " at " + module.base);
if (lib_includes(module.name, Object.keys(sys_data))) {
let exports = module.enumerateExports();
let module_name = module.name.toLowerCase();
for (let f_name in sys_data[module_name]) {
// call random function
let infos = sys_data[module_name][f_name];
let ea = infos["addr"]
ea = module.base.add(ptr(ea));
funcs_map[ea] = [f_name, module.name]
try {
//console.log("Hooking " + f_name + " at " + ea);
Interceptor.attach(ea, {
onEnter: function(args) {
let f_name = funcs_map[this.context.pc][0];
let module_name = funcs_map[this.context.pc][1];
console.log(f_name);
let rcx = this.context.rcx;
let rdx = this.context.rdx;
let r8 = this.context.r8;
let r9 = this.context.r9;
let syscall_args = [rcx, rdx, r8, r9];
let arg_index = 0;
let stack_arg = Memory.readPointer(this.context.rsp.add(arg_index * Process.pointerSize));
while (stack_arg != 0 && arg_index < 4) {
stack_arg = Memory.readPointer(this.context.rsp.add(arg_index * Process.pointerSize));
syscall_args.push(stack_arg);
arg_index++;
}
let f_args_infos = sys_data[module_name][f_name]["args"];
for (let i = 0; i < syscall_args.length; i++) {
let arg_info = "";
if (i < f_args_infos.length) {
arg_info = f_args_infos[i];
}
console.log("Arg " + i + ": " + " " + arg_info + " " + syscall_args[i]);
}
mutate_args(this.context, syscall_args, f_args_infos);
}
});
} catch (e) {
console.log("Error: " + e);
}
}
}
},
onComplete: function() {
}
});
}, 500);
"""
def clean(exe):
# powershell command to kill all notepad processes
os.system("taskkill /f /im {}".format(exe))
def match(args):
valid_matches = [
["in", "buf"],
["in", "*"],
["PVOID"],
["_in_reads_"],
["_out_writes_bytes_"],
]
for match in valid_matches:
for arg in args:
arg = arg.lower()
if all([x in arg for x in match]):
return True
return False
def match_func_name(f_name):
invalid_matches = [
"free",
"alloc",
"malloc",
"realloc",
"copy",
"move",
"duplicate",
"dup",
"token",
"hook",
"call"
]
for match in invalid_matches:
if match in f_name.lower():
return False
return True
def filter(sys_data):
filtered_sys_data = {}
for lib in sys_data:
filtered_sys_data[lib] = {}
for f_name in sys_data[lib]:
if match_func_name(f_name):
filtered_sys_data[lib][f_name] = sys_data[lib][f_name]
return filtered_sys_data
def replace_all(text, dic_vars):
for i, j in dic_vars.items():
text = text.replace(i, j)
return text
def build_script(sys_data):
dict_vars = {
"$$sys_data$$": json.dumps(sys_data),
}
script = replace_all(global_var, dict_vars)
script += common
script += process_module
with open("debug_script.js", "w") as f:
f.write(script)
return script
def random_pick(funcs_map):
DESIRED_FUNCS = 100
r_funcs_map = {}
for lib in funcs_map:
p = (1 / len(funcs_map[lib])) * DESIRED_FUNCS
r_funcs_map[lib] = {}
for f_name, f_infos in funcs_map[lib].items():
if random.random() < p:
r_funcs_map[lib][f_name] = f_infos
return r_funcs_map
def async_launch(script, path):
# spawn process and pause it
try:
pid = frida.spawn(path)
session = frida.attach(pid)
script_s = session.create_script(script)
script_s.load()
# wait for script to finish to hook
time.sleep(1)
print("Resuming {} with pid {}".format(path, pid))
frida.resume(pid)
except Exception as e:
pass
def fuzzer_thread(sys_data, process_to_fuzz):
MAX_THREADS = 1
INTERVAL = 2
exe = process_to_fuzz["name"]
path = process_to_fuzz["path"]
try:
while True:
for i in range(MAX_THREADS):
# pick 100 random functions for each library
r_funcs_map = random_pick(filter(sys_data))
script = build_script(r_funcs_map)
async_launch(script, path)
# wait
time.sleep(INTERVAL)
# detach and kill processes
clean(exe)
except KeyboardInterrupt:
clean(exe)
sys.exit(0)
parser = argparse.ArgumentParser(description="Function definitions")
parser.add_argument("config", metavar="config", type=str, help="Json file containing function definitions")
parser.add_argument(
"sys_data",
metavar="sys_data",
type=str,
help="Json file containing function definitions",
)
parser.add_argument(
"p_index",
metavar="p_index",
type=int,
help="Process index to fuzz",
)
args = parser.parse_args()
sys_data = json.load(open(args.sys_data, "r"))
programs = json.load(open(args.config, "r"))["programs"]
process_to_fuzz = programs[args.p_index]
fuzzer_thread(sys_data, process_to_fuzz)