-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfn.py
executable file
·466 lines (385 loc) · 13.3 KB
/
fn.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
#!/usr/bin/env python3
import regex
import sys
import os
import datetime
def skip_file(fname):
import cq
global ns
if ns:
return False
for skip in cq.SKIP_DIRS:
if skip.search(fname):
return True
for skip in cq.SKIP_EXTS:
if skip.search(fname):
return True
return False
def get_global_checks(re_checks_todo):
import cq # noqa
global_checks = []
for c in cq.GLOBAL_CHECKS:
check_name = c[0]
proc = c[1]
arg = c[2]
if re_checks_todo.search(check_name):
global_checks += [(check_name, proc, arg)]
return global_checks
def get_global_post_checks(re_checks_todo):
import cq # noqa
global_checks = []
for c in cq.GLOBAL_POST_CHECKS:
check_name = c[0]
proc = c[1]
arg = c[2]
if re_checks_todo.search(check_name):
global_checks += [(check_name, proc, arg)]
return global_checks
def get_file_checks(fname, re_checks_todo):
import cq
# FILENAME_CHECKS, 0 is regex, 1 is outfile
file_checks = []
for c in cq.FILE_CHECKS:
check_name = c[0]
regex_text = c[1]
proc = c[2]
arg = c[3]
if regex_text.search(fname):
if re_checks_todo.search(check_name):
file_checks += [(check_name, regex_text, proc, arg)]
return file_checks
def get_line_regex_checks(fname, re_checks_todo):
import cq
global sc, sa # noqa
# compile regexes
line_checks = []
for c in cq.LINE_REGEX_CHECKS:
check_name = c[0]
check_re = c[1]
files = c[2] if len(c) > 2 else None
exclusions = c[3] if len(c) > 3 else None
fmt = c[4] if len(c) > 4 else None
add = True
if sc:
files = cq.ALL_CODE_FILES
if sa:
add = True
elif files:
add = False
for f in files:
if fname.lower().endswith(f):
add = True
break
if add:
if re_checks_todo.search(check_name):
line_checks += [(check_name, check_re, files, exclusions, fmt)]
for ext in cq.C_FILES:
if fname.lower().endswith(ext):
for fn in cq.BannedFunctions:
re = regex.compile(re_checks_todo)
if re.search('banned_' + fn):
line_checks += [('banned_' + fn, regex.compile(r'\W' + fn + r'\s*\(.{0,99}$'), None, None)]
return line_checks
def get_line_custom_checks(fname, re_checks_todo):
import cq
global sc, sa # noqa
# compile regexes
line_checks = []
for c in cq.LINE_CUSTOM_CHECKS:
check_name = c[0]
check_fn = c[1]
files = c[2] if len(c) > 2 else None
exclusions = c[3] if len(c) > 3 else None
fmt = c[4] if len(c) > 4 else None
add = True
if sc:
files = cq.ALL_CODE_FILES
if sa:
add = True
elif files:
add = False
for f in files:
if fname.lower().endswith(f):
add = True
break
if add:
if re_checks_todo.search(check_name):
line_checks += [(check_name, check_fn, files, exclusions, fmt)]
return line_checks
def do_file_check(check, fname):
out_fname = outdir + '/' + check[0] + '.txt'
# regex_text = check[1]
proc = check[2]
arg = check[3]
proc(fname, out_fname, arg)
def do_line_regex_check(check, fname, line, line_num):
try:
outfile = check[0]
re = check[1]
# files = check[2] if len(check) > 2 else None
exclusions = check[3] if len(check) > 3 else None
fmt = check[4] if len(check) > 4 else '{fname}:{line_num}:{g0}'
if fmt is None: fmt = '{fname}:{line_num}:{g0}' # noqa
result = re.search(line)
if result:
skip = False
if exclusions:
for exclude in exclusions:
res_ex = exclude.search(line)
if res_ex:
skip = True
break
if not skip:
# write output in format.
# string.format?
g0 = result.group(0)
groups = result.groups()
g1 = groups[0] if len(groups) > 0 else ''
g2 = groups[1] if len(groups) > 1 else ''
g3 = groups[2] if len(groups) > 2 else ''
msg = fmt.format(fname=fname, line_num=line_num, g0=g0, g1=g1, g2=g2, g3=g3)
write_result(outfile, msg)
except Exception as e:
print("Exception: " + str(e))
print("")
def do_line_custom_check(check, fname, line, line_num):
try:
outfile = check[0]
fn = check[1]
base_fmt = '{fname}:{line_num}:{g0}'
# files = check[2] if len(check) > 2 else None
# exclusions = check[3] if len(check) > 3 else None
fmt = check[4] if len(check) > 4 else base_fmt
if fmt is None: fmt = base_fmt # noqa
results = fn(line)
for result, score in results: # noqa
score_str = '%02d' % score
g0 = result
msg = fmt.format(fname=fname, line_num=line_num, score_str=score_str, g0=g0)
write_result('crit_' + outfile, msg) # noqa
except Exception as e:
print("Exception: " + str(e))
print("")
def global_run_tool(out_fname, arg):
cmd = arg.format(out_fname=out_fname)
os.system(cmd)
def do_global_check(check):
check_name = check[0]
out_fname = outdir + '/' + check_name + '.txt'
proc = check[1]
arg = check[2]
if print_progress:
print('Running Tool: ' + check_name)
proc(out_fname, arg)
def sort_file(filename):
try:
in_file = open(filename, 'r')
lines = sorted(set(in_file.readlines()))
in_file.close()
in_file = open(filename, 'w')
in_file.writelines(lines)
in_file.close()
except: # noqa
return False
def sort_df_sources_and_sinks(out_fname, arg): # noqa
import cq
for source_file in cq.DF_FILES:
source_file = outdir + '/' + source_file + '_sources.txt'
sort_file(source_file)
source_file = outdir + '/' + source_file + '_sinks.txt'
sort_file(source_file)
def df_report_if_issue(df_file, src_result, sink_result):
# True if strings match to the first colon ':'
lhs = src_result.split(':')
rhs = sink_result.split(':')
if len(lhs) < 2 or len(rhs) < 2:
return False
if lhs[0] != rhs[0]:
return False
i_l = int(lhs[1])
i_r = int(rhs[1])
diff = abs(i_l - i_r)
if diff > 1000:
return False
msg = lhs[0] + ':' + lhs[1] + ':SCORE:' + f'{diff:05}'
write_result(df_file + '_issues.txt', msg)
return True
def gen_df_bugs(out_fname, arg): # noqa
import cq # noqa
for df_file in cq.DF_FILES:
source_file = outdir + '/' + df_file + '_sources.txt'
sink_file = outdir + '/' + df_file + '_sinks.txt'
# for each source, advance sink to 'first after'; numeric (not alpha) comparison for line number.
# compare two lines. Same file is line_number diff.
# Output issues with prefix of 'score', which is this diff.
try:
in_file = open(source_file, 'r')
src_lines = in_file.readlines()
in_file.close()
in_file = open(sink_file, 'r')
sink_lines = in_file.readlines()
in_file.close()
except: # noqa
continue
i_src = 0
i_sink = 0
while (i_src < len(src_lines)) and (i_sink < len(sink_lines)):
src = src_lines[i_src]
sink = sink_lines[i_sink]
while (sink < src) and (i_sink < len(sink_lines)):
sink = sink_lines[i_sink]
i_sink = i_sink + 1
df_report_if_issue(df_file, src, sink)
i_src = i_src + 1
def basic_get_time(out_fname, arg):
now = datetime.datetime.now()
start = now.strftime("%Y-%m-%d %H:%M:%S")
write_result_to_path(out_fname, start + arg)
def basic_path(out_fname, arg):
write_result_to_path(out_fname, os.path.abspath(".") + arg)
def file_run_tool(fname, out_fname, arg):
cmd = arg.format(fname=fname, out_fname=out_fname)
os.system(cmd)
def file_scan_exe(fname, out_fname, arg): # noqa
write_result_to_path(out_fname, fname)
def file_exists(fname, out_fname, arg): # noqa
write_result_to_path(out_fname, fname)
def file_scan_shell(fname, out_fname, arg): # noqa
write_result_to_path(out_fname, 'Scan shell: ' + fname)
cmdline = 'shellcheck "{fname}" >> "{out_fname}"'
cmd = cmdline.format(fname=fname, out_fname=out_fname)
os.system(cmd)
def write_result(fname, result_msg):
with open(outdir + '/' + fname + '.txt', 'a') as outfile:
if result_msg.endswith('\n'):
outfile.write(result_msg)
else:
outfile.write(result_msg + '\n')
def write_result_to_path(fullpath, result_msg):
with open(fullpath, 'a') as outfile:
if result_msg.endswith('\n'):
outfile.write(result_msg)
else:
outfile.write(result_msg + '\n')
def is_binary(line):
if line.find(b'\x00'):
return True
if line.find(b'\xff'):
return True
if line.find(b'\xfe'):
return True
return False
def do_checks(re_checks_todo):
if outdir == '':
return
if a:
mode = 'rb'
else:
mode = 'r'
global_checks = get_global_checks(re_checks_todo)
for check in global_checks:
do_global_check(check)
for root, subdirs, files in os.walk(os.path.abspath(".")):
for fn in files:
fname = str(root) + "/" + str(fn)
# exclude some files/paths based on verbosity options
if skip_file(fname):
continue
if vvv or print_progress:
print('Scanning ' + fname)
# do checks based on filename
file_checks = get_file_checks(fname, re_checks_todo)
for check in file_checks:
do_file_check(check, fname)
# do checks based on the text of lines in a file
regex_checks = get_line_regex_checks(fname, re_checks_todo)
custom_checks = get_line_custom_checks(fname, re_checks_todo)
if regex_checks or custom_checks:
try:
with open(fname, mode) as f:
line_num = 1
for line in f:
# do all line checks
if a:
line = str(line)
for check in regex_checks:
do_line_regex_check(check, fname, line, line_num)
for check in custom_checks:
do_line_custom_check(check, fname, line, line_num)
line_num += 1
except: # noqa
continue
checks = get_global_post_checks(re_checks_todo)
for check in checks:
do_global_check(check)
def syntax():
print(
'''cq.py : Universal SAST Tool [ By Chris Anley ]
Syntax:
cq.py [options] <output directory>
-a : check all files, including binaries (i.e. files containing invalid utf-8 chars)
-c <checks regex> : only run checks matching the regex, e.g. 'php'
-p : print progress
-v : quite verbose
-vv : annoyingly verbose
-vvv : pointlessly verbose
-ns : no skip : don't skip files/directories that are irrelevant, like test, /vendor/, /node_modules/, .zip etc
-sa : scan all files, not just recommended / code files
-sc : scan all code files for all bugs, i.e. not just python bugs in python files
''')
a = False
v = False
vv = False
vvv = False
ns = False
sa = False
sc = False
print_progress = False
re_checks = '.*'
outdir = '/tmp/' # Note - this is always set to something else, but - just in case it isn't, put output in /tmp.
def do_main():
global a, v, vv, vvv, ns, sa, sc, outdir, print_progress, re_checks
argc = len(sys.argv)
argv = sys.argv
if argc <= 1:
return syntax()
for i in range(1, argc):
if argv[i] == '-a':
a = True
ns = True # no skip directories
sa = True # apply all checks to all files
continue
if argv[i] == '-v':
v = True
continue
if argv[i] == '-vv':
v = True
vv = True
continue
if argv[i] == '-vvv':
v = True
vv = True
vvv = True
continue
if argv[i] == '-ns': # no skip directories / files
ns = True
continue
if argv[i] == '-sa': # apply all checks to all files
sa = True
continue
if argv[i] == '-sc': # apply all checks to all code files
sc = True
continue
if argv[i] == '-p':
print_progress = True
if i == argc - 1:
outdir = argv[i]
if argv[i] == '-c':
re_checks = argv[i + 1]
i += 1
try:
os.makedirs(outdir)
except: # noqa
print("Outdir exists")
print("Starting")
do_checks(regex.compile(re_checks))