-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecurity_scanner.py
638 lines (608 loc) · 19 KB
/
security_scanner.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
from binaryninja import *
def conditions(bv, cond, param):
return eval(cond)
def no_guard(a, b, c, d, e):
return False
def is_parameter_dynamic(call_insn: HighLevelILInstruction, param_index: int, multiparameter) -> bool:
# TODO must do traceback here
results = []
if param_index == -1: # Bypass for functions where you dont care
return False
# Ensure the parameter index is within the bounds of the call's parameters
if param_index >= len(call_insn.params):
return False # Index out of bounds
if multiparameter:
params = call_insn.params[param_index:]
else:
params = [call_insn.params[param_index]]
for param in params:
if not param.operation in [HighLevelILOperation.HLIL_CONST, HighLevelILOperation.HLIL_CONST_PTR]:
results.append(True)
else:
results.append(False)
return any(results)
def compare_param(a,b):
if len(a.operands) != len(b.operands):
return False
if a.vars == b.vars:
for op_index in range(0,len(a.operands)):
if str(a.operands[op_index]) != str(b.operands[op_index]):
return False
return True
def has_func_guard(bv: BinaryView, call_insn: HighLevelILInstruction, param_index,func_list,multi_parameter):
# TODO multiparam here
if call_insn.operation != HighLevelILOperation.HLIL_CALL:
return False
# Check if the parameter index is within the range of the call's parameters
if param_index >= len(call_insn.params):
return False
# Get the parameter variable
param_var = call_insn.params[param_index]
# Find the strlen function by name
strlen_func = next((f for f in bv.get_functions_by_name('strlen')), None)
if not strlen_func:
return False
# Iterate over all references to the parameter variable
param_raw = call_insn.params[param_index]
for param_var in call_insn.params[param_index].vars:
for var_use in call_insn.function.get_var_uses(param_var):
parent = var_use.parent
while parent:
# Look for calls in the HLIL instructions
if parent.operation == HighLevelILOperation.HLIL_CALL:
# Check if the call is to strlen
# TODO change for simple string compare
func = bv.get_function_at(parent.dest.constant)
if parent.dest.operation == HighLevelILOperation.HLIL_CONST_PTR and func and func.name in func_list:
# Check if the parameter in question is used as an argument to strlen
for arg in parent.params:
if compare_param(param_raw,arg):
return True
parent = parent.parent
return False
def has_length_guard(bv: BinaryView, call_insn: HighLevelILInstruction, param_index,dummy=[],multi_parameter=False):
guard_param = call_insn.params[param_index]
if guard_param.operation in [HighLevelILOperation.HLIL_CONST, HighLevelILOperation.HLIL_CONST_PTR]:
return False
# Check for verification in preceding if conditions
cur_block = call_insn.il_basic_block
for block in bv.get_basic_blocks_at(cur_block.start):
for insn in block:
if insn.operation == HighLevelILOperation.HLIL_IF:
condition = insn.condition
# Simplistic check for a condition that compares the memcpy size
# This can be expanded with more sophisticated analysis
if guard_param in condition.vars_read:
return True
return False
def __is_op_in_insn(insn, op):
operands = insn.operands
while operands:
current_op = operands.pop(0)
if compare_param(current_op,op):
return True
operands.extend(current_op.operands)
return False
def is_ret_val_checked(bv: BinaryView, call_insn: HighLevelILInstruction):
# TODO must make example code
parent = call_insn.parent
while parent:
if parent.operation == HighLevelILOperation.HLIL_IF:
return True
if parent.operation in [HighLevelILOperation.HLIL_ASSIGN, HighLevelILOperation.HLIL_VAR_INIT, HighLevelILOperation.HLIL_ASSIGN_UNPACK]:
break
if parent.operation == HighLevelILOperation.HLIL_BLOCK:
return False
parent = parent.parent
# parent holds the assignment operation here
last_inst = list(bv.instructions)[call_insn.il_basic_block.end - 1]
if last_inst.operation == HighLevelILOperation.HLIL_IF and __is_op_in_insn(last_inst,parent):
return True
return False
case_list = {
"OS Command Injection": {**dict.fromkeys([
"system",
"_system",
"_popen",
"popen",
"wpopen",
"_wpopen",
"execl",
"execlp",
"execle",
"execv",
"execvp",
"execvpe",
"_execl",
"_execlp",
"_execle",
"_execv",
"_execvp",
"_execvpe"
],
{
"not_constant": {
0: {
"guard_check": no_guard,
"guard_params": [],
"multi_parameter": False
}
},
"conditions": "True",
"ret_val_check": False
}
)
},
"Buffer Overflow":{
**dict.fromkeys([
"memmove",
"_memmove",
"wmemmove",
"_wmemmove",
"memcpy",
"_memcpy",
"wmemcpy",
"_wmemcpy"
],
{
"not_constant": {
2: {
"guard_check": has_length_guard,
"guard_params": [],
"multi_parameter": False
}
},
"conditions": "True",
"ret_val_check": False
}
),
**dict.fromkeys([
"strncpy",
"_strncpy",
"lstrcpynA",
"lstrcpynW",
"strncat",
"_strncat"
],
{
"not_constant": {
1: {
"guard_check": has_func_guard,
"guard_params": ["strlen"],
"multi_parameter": False,
},
2: {
"guard_check": has_length_guard,
"guard_params": [],
"multi_parameter": False
}
},
"conditions": "True",
"ret_val_check": False
}
),
**dict.fromkeys([
"strcpy",
"_strcpy",
"wcscpy",
"_wcscpy",
"_mbscpy",
"mbscpy",
"lstrcpyA",
"lstrcpyW",
"strcat",
"_strcat",
"wcscat",
"_wcscat",
"_mbscat",
"mbscat",
"lstrcatA",
"lstrcatW"
],
{
"not_constant": {
1: {
"guard_check": has_func_guard,
"guard_params": ["strlen"],
"multi_parameter": False
}
},
"conditions": "True",
"ret_val_check": False
}
),
**dict.fromkeys([
"sprintf",
"_sprintf",
"siprintf",
"_siprintf"
],
{
"not_constant": {
2: {
"guard_check": has_func_guard,
"guard_params": ["strlen"],
"multi_parameter": True
}
},
"conditions": "param[1].operation == HighLevelILOperation.HLIL_CONST_PTR and '%s' in param[1].string[0]",
"ret_val_check": False
}
)
},
"Format String Issue": {
**dict.fromkeys([
"_printf",
"printf",
"printk",
"_printk",
"vprintf",
"_vprintf",
"scanf",
"_scanf",
"vscanf",
"_vscanf",
"NSLog",
"_NSLog",
"wprintf",
"_wprintf",
"vwprintf",
"_vwprintf",
"vwscanf",
"_vwscanf",
"vscanf_s",
"_vscanf_s",
"vwscanf_s",
"_vwscanf_s",
"_vscprintf",
"vscprintf",
"_vscprintf_l",
"vscprintf_l",
"_vscwprintf",
"vscwprintf",
"_vscwprintf_l",
"vscwprintf_l",
"_vscprintf_p",
"vscprintf_p",
"_vscprintf_p_l",
"vscprintf_p_l",
"_vscwprintf_p",
"vscwprintf_p",
"_vscwprintf_p_l",
"vscwprintf_p_l",
"_vcprintf",
"vcprintf",
"_vcprintf_l",
"vcprintf_l",
"_vcwprintf",
"vcwprintf",
"_vcwprintf_l",
"vcwprintf_l",
"_vcprintf_p",
"vcprintf_p",
"_vcprintf_p_l",
"vcprintf_p_l",
"_vcwprintf_p",
"vcwprintf_p",
"_vcwprintf_p_l",
"vcwprintf_p_l",
"_vcwprintf_s",
"vcwprintf_s",
"_vcwprintf_s_l",
"vcwprintf_s_l",
"_vprintf_l",
"vprintf_l",
"_vwprintf_l",
"vwprintf_l",
"_vprintf_p",
"vprintf_p",
"_vprintf_p_l",
"vprintf_p_l",
"_vwprintf_p",
"vwprintf_p",
"_vwprintf_p_l",
"vwprintf_p_l",
"vprintf_s",
"_vprintf_s",
"_vprintf_s_l",
"vprintf_s_l",
"vwprintf_s",
"_vwprintf_s",
"_vwprintf_s_l",
"vwprintf_s_l"
],
{
"not_constant": {
0: {
"guard_check": no_guard,
"guard_params": [],
"multi_parameter": False
}
},
"conditions": "True",
"ret_val_check": False
}
),
**dict.fromkeys([
"wsprintf",
"_wsprintf",
"wsprintfW",
"_wsprintfW",
"wsprintfW@IAT",
"_wsprintfW@IAT",
"wsprintfA",
"_wsprintfA",
"wsprintfA@IAT",
"_wsprintfA@IAT",
"fprintf",
"_fprintf",
"sprintf",
"_sprintf",
"vsprintf",
"_vsprintf",
"vfprintf",
"_vfprintf",
"sscanf",
"_sscanf",
"fscanf",
"_fscanf",
"vsscanf",
"_vsscanf",
"vfscanf",
"_vfscanf",
"fwprintf",
"_fwprintf",
"vfwprintf",
"_vfwprintf",
"_vfprintf_l",
"vfprintf_l",
"_vfwprintf_l",
"vfwprintf_l",
"_vfprintf_p",
"asprintf",
"_asprintf",
"vfprintf_p",
"_vfprintf_p_l",
"vfprintf_p_l",
"_vfwprintf_p",
"vfwprintf_p",
"_vfwprintf_p_l",
"vfwprintf_p_l",
"vfprintf_s",
"_vfprintf_s",
"_vfprintf_s_l",
"vfprintf_s_l",
"vfwprintf_s",
"_vfwprintf_s",
"_vfwprintf_s_l",
"vfwprintf_s_l",
"vasprintf",
"vasprintf",
"vfwscanf",
"_vfwscanf",
"vfscanf_s",
"_vfscanf_s",
"vfwscanf_s",
"_vfwscanf_s",
"_vsprintf_l",
"vsprintf_l",
"__vswprintf_l",
"vswprintf",
"_vswprintf",
"vsprintf_s",
"_vsprintf_s",
"vswprintf_s",
"_vswprintf_s",
"vswscanf",
"_vswscanf",
"vsscanf_s",
"_vsscanf_s",
"vswscanf_s",
"_vswscanf_s",
"__wprintf_chk",
"___vprintf_chk",
"__vwprintf_chk",
"___printf_chk",
"__printf_chk",
"__vprintf_chk",
"siprintf",
"_siprintf"
],
{
"not_constant": {
1: {
"guard_check": no_guard,
"guard_params": [],
"multi_parameter": False
}
},
"conditions": "True",
"ret_val_check": False
}
),
**dict.fromkeys([
"vsnprintf",
"_vsnprintf",
"snprintf",
"_snprintf",
"vswprintf",
"_vswprintf",
"swprintf",
"_swprintf",
"_vsnprintf_l",
"vsnprintf_l",
"_vsnwprintf",
"vsnwprintf",
"_vsnwprintf_l",
"vsnwprintf_l",
"_vsnprintf_s",
"vsnprintf_s",
"_vsnwprintf_s",
"vsnwprintf_s",
"_vswprintf_l",
"vswprintf_l",
"_vsprintf_p",
"vsprintf_p",
"_vsprintf_p_l",
"_sprintf_p_l",
"_vswprintf_p",
"vswprintf_p",
"_vswprintf_p_l",
"vswprintf_p_l",
"vsprintf_s",
"_vsprintf_s",
"_vsprintf_s_l",
"vsprintf_s_l",
"vswprintf_s",
"_vswprintf_s",
"_vswprintf_s_l",
"vswprintf_s_l",
"__asprintf_chk",
"__dprintf_chk",
"___fprintf_chk",
"__fprintf_chk",
"__fwprintf_chk",
"__obstack_vprintf_chk",
"__obstack_printf_chk",
"__vasprintf_chk",
"__vdprintf_chk",
"___vfprintf_chk",
"__vfwprintf_chk",
"__vfprintf_chk"
],
{
"not_constant": {
2: {
"guard_check": no_guard,
"guard_params": [],
"multi_parameter": False
}
},
"conditions": "True",
"ret_val_check": False
}
),
**dict.fromkeys([
"vsnprintf_s",
"_vsnprintf_s",
"_vsnprintf_s_l",
"vsnprintf_s_l",
"_vsnwprintf_s",
"vsnwprintf_s",
"_vsnwprintf_s_l",
"vsnwprintf_s_l",
"___vsprintf_chk",
"___sprintf_chk",
"__sprintf_chk",
"__vsprintf_chk"
],
{
"not_constant": {
3: {
"guard_check": no_guard,
"guard_params": [],
"multi_parameter": False
}
},
"conditions": "True",
"ret_val_check": False
}
),
**dict.fromkeys([
"___snprintf_chk",
"__snprintf_chk",
"__swprintf_chk",
"___vsnprintf_chk",
"__vswprintf_chk",
"__vsnprintf_chk"
],
{
"not_constant": {
4: {
"guard_check": no_guard,
"guard_params": [],
"multi_parameter": False
}
},
"conditions": "True",
"ret_val_check": False
}
),
},
"Unchecked Return Value of 'scanf'": {
**dict.fromkeys([
"_fscanf",
"_scanf",
"_sscanf",
"_vfscanf",
"_vfscanf_s",
"_vfwscanf",
"_vfwscanf_s",
"_vscanf",
"__isoc99_scanf",
"_vscanf_s",
"_vsscanf",
"_vsscanf_s",
"_vswscanf",
"_vswscanf_s",
"_vwscanf",
"_vwscanf_s",
"fscanf",
"scanf",
"sscanf",
"vfscanf",
"vfscanf_s",
"vfwscanf",
"vfwscanf_s",
"vscanf",
"vscanf_s",
"vsscanf",
"vsscanf_s",
"vswscanf",
"vswscanf_s",
"vwscanf",
"vwscanf_s"
],
{
"not_constant": {
},
"conditions": "param[1].operation == HighLevelILOperation.HLIL_CONST_PTR and '%s' in param[1].string[0]",
"ret_val_check": False
}
),
}
}
with open_index(bv, 'Potential Security Issues') as index:
f_count = 1
for func in bv.functions:
notify_progress(f_count, len(bv.functions), 'Finding potential security issues...')
f_count += 1
try:
hlil = func.hlil
except exceptions.ILException:
#log_warn(f"HLIL not available for function at {func.name}")
continue
for block in func.hlil:
for insn in block:
if insn.operation == HighLevelILOperation.HLIL_CALL:
for case in case_list:
if str(insn.dest) in case_list[case]:
details = case_list[case][str(insn.dest)]
mark = None
for n_c in details["not_constant"]:
if is_parameter_dynamic(insn,n_c,details["not_constant"][n_c]["multi_parameter"]) and conditions(bv,details["conditions"],insn.params):
# Not a constant, move on
if details["not_constant"][n_c]["guard_check"](bv,insn,n_c,details["not_constant"][n_c]["guard_params"],details["not_constant"][n_c]["multi_parameter"]):
mark = "Low"
else:
mark = "High"
elif mark:
if mark == "High":
mark = "Low"
else:
mark = "Info"
if details["ret_val_check"]:
# Do a check for a return value
is_ret_val_checked(bv,insn)
if mark:
index.add_entry(insn, {"Description": case ,"Priority": mark, "Found At": insn.function.source_function.name})