-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathSourceCodeSniffer.py
More file actions
344 lines (291 loc) · 12.3 KB
/
SourceCodeSniffer.py
File metadata and controls
344 lines (291 loc) · 12.3 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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# SourceCodeSniffer: Sniff out dangerous code segments
# Copyright (c) 2017, Austin Scott
#
# Contact information:
# Austin Scott
#
"""
Main application logic and automation functions
"""
__version__ = '0.1'
__lastupdated__ = 'April 11, 2017'
###
# Imports
###
import os
import sys
import time
import re
import ConfigParser
sys.path.insert(0, os.path.abspath('..'))
# from clint.textui import puts, progress, puts
BAR_TEMPLATE = '%s[%s%s] %i/%i - %s\r'
MILL_TEMPLATE = '%s %s %i/%i\r'
DOTS_CHAR = '.'
BAR_FILLED_CHAR = '#'
BAR_EMPTY_CHAR = ' '
MILL_CHARS = ['|', '/', '-', '\\']
# How long to wait before recalculating the ETA
ETA_INTERVAL = 1
# How many intervals (excluding the current one) to calculate the simple moving
# average
ETA_SMA_WINDOW = 9
STREAM = sys.stderr
class Bar(object):
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.done()
return False # we're not suppressing exceptions
def __init__(self, label='', width=32, hide=None, empty_char=BAR_EMPTY_CHAR,
filled_char=BAR_FILLED_CHAR, expected_size=None, every=1):
self.label = label
self.width = width
self.hide = hide
# Only show bar in terminals by default (better for piping, logging etc.)
if hide is None:
try:
self.hide = not STREAM.isatty()
except AttributeError: # output does not support isatty()
self.hide = True
self.empty_char = empty_char
self.filled_char = filled_char
self.expected_size = expected_size
self.every = every
self.start = time.time()
self.ittimes = []
self.eta = 0
self.etadelta = time.time()
self.etadisp = self.format_time(self.eta)
self.last_progress = 0
if (self.expected_size):
self.show(0)
def show(self, progress, count=None):
if count is not None:
self.expected_size = count
if self.expected_size is None:
raise Exception("expected_size not initialized")
self.last_progress = progress
if (time.time() - self.etadelta) > ETA_INTERVAL:
self.etadelta = time.time()
self.ittimes = \
self.ittimes[-ETA_SMA_WINDOW:] + \
[-(self.start - time.time()) / (progress + 1)]
self.eta = \
sum(self.ittimes) / float(len(self.ittimes)) * \
(self.expected_size - progress)
self.etadisp = self.format_time(self.eta)
x = int(self.width * progress / self.expected_size)
if not self.hide:
if ((progress % self.every) == 0 or # True every "every" updates
(progress == self.expected_size)): # And when we're done
STREAM.write(BAR_TEMPLATE % (
self.label, self.filled_char * x,
self.empty_char * (self.width - x), progress,
self.expected_size, self.etadisp))
STREAM.flush()
def done(self):
self.elapsed = time.time() - self.start
elapsed_disp = self.format_time(self.elapsed)
if not self.hide:
# Print completed bar with elapsed time
STREAM.write(BAR_TEMPLATE % (
self.label, self.filled_char * self.width,
self.empty_char * 0, self.last_progress,
self.expected_size, elapsed_disp))
STREAM.write('\n')
STREAM.flush()
def format_time(self, seconds):
return time.strftime('%H:%M:%S', time.gmtime(seconds))
class logger:
DEBUG = False;
VERBOSE = False;
@staticmethod
def debug(msg):
if logger.DEBUG == True:
print(msg)
@staticmethod
def verbose(msg):
if logger.VERBOSE == True:
print(msg)
class Colored:
@staticmethod
def redback(printString):
return "\033[0m\033[37m\033[41m" + printString
@staticmethod
def black(printString):
return '\033[0;30m' + printString
@staticmethod
def red(printString):
return '\033[0;31m' + printString
@staticmethod
def green(printString):
return '\033[0;32m' + printString
@staticmethod
def yellow(printString):
return '\033[0;33m' + printString
@staticmethod
def blue(printString):
return '\033[0;34m' + printString
@staticmethod
def magenta(printString):
return '\033[0;35m' + printString
@staticmethod
def cyan(printString):
return '\033[0;36m' + printString
@staticmethod
def white(printString):
return '\033[0;37m' + printString
@staticmethod
def grey(printString):
return '\033[0;38m' + printString
@staticmethod
def reset(printString):
# return printString
return '\033[0;39m' + printString
class tabled:
@staticmethod
def column(colText, colWidth):
if len(colText) < colWidth:
return colText + (" " * (colWidth - len(colText)))
class consoleOut:
@staticmethod
def echoOut(echoText):
os.system("echo " + echoText)
class SourceCodeSnifferMain:
def __init__(self, argv):
self.argv = argv
self._start_time = time.clock()
self._task_start_time = time.clock()
self._column_width = 60
self._compare_filename = "REPORT_Baseline_Compare_Results.txt"
self._config_files = ["Default.ini","ASP.ini", "CSharp.ini"]
self._ignore_files = (".html", ".js", "robots.txt")
self._path_to_scan = "."
self._report_filename = "REPORT.txt"
self._report_timer_filename = "REPORT_TIMES.txt"
self._remove_line_words = ['time', 'elapsed', 'Compare', 'BlkIo', 'Variable issues', 'Variable ConOut']
self._summaryReportIssuesByFile = []
self._summaryReportHighestRiskLevel = []
self._summaryReportTimer = []
self._summaryRiskTotal = 0
self._summaryCount = 0
# parse arguments
self.parse_args()
def get_version(self):
return "%s" % (__version__)
def add_to_summary_report(self, text):
self._summaryReportIssuesByFile.append(text)
def print_banner(self):
"""
Prints banner
"""
print(Colored.red(" Source Code Sniffer Version: " + __version__ + " Updated: " + __lastupdated__))
def usage(self):
print "\n- Command Line Usage\n\t``# %.65s [options]``\n" % sys.argv[0]
print "Options\n-------"
print "====================== =============================================================="
print "-c --configFiles specify the config files (default=" + str(self._config_files) + ")"
print " config files should be comma separated"
print "-p --pathToScan specify the path to scan (default=" + str(self._path_to_scan) + ")"
print " use the forward slash / for both *nix and windows paths"
print "-i --ignoreFiles specify files to not scan (default=" + str(self._ignore_files) + ")"
print " ignored files and file types should be comma separated "
print "-v --verbose verbose mode"
print "-d --debug show debug output"
print "-l --log output to log file"
print "====================== =============================================================="
print "Example:"
print " python SourceCodeSniffer.py -c ASP.ini,CSharp.ini,Default.ini,VBScript.ini -p c:/testpath/test/ -i .html,robots.txt"
def parse_args(self):
import getopt
try:
opts, args = getopt.getopt(self.argv, "fhvdnc:p:i:",
["help"])
except getopt.GetoptError, err:
print str(err)
self.usage()
return 32
for o, a in opts:
if o in ("-v", "--verbose"):
print "verbose"
logger.VERBOSE = True
elif o in ("-d", "--debug"):
print "debug"
logger.DEBUG = True
elif o in ("-c", "--configFiles"):
self._config_files = a.split(',')
elif o in ("-i", "--ignoreFiles"):
self._ignore_files = tuple(a.split(','))
elif o in ("-h", "--help"):
self.usage()
sys.exit(0)
return 0
elif o in ("-p", "--pathToScan"):
self._path_to_scan = a
else:
assert False, "unknown option"
def sourceCodeSniffFolder(self):
# Generate Validation Data Dumps
print(Colored.red("Sniffing for dangerous code..."))
for root, subdirs, files in os.walk(os.path.normpath(self._path_to_scan)):
logger().verbose('--\nroot = ' + root)
for subdir in subdirs:
logger().verbose('\t- subdirectory ' + subdir)
for filename in files:
file_path = os.path.join(root, filename)
logger().debug('\t- file %s (full path: %s)' % (filename, file_path))
if not file_path.lower().endswith(self._ignore_files):
self.sourceCodeSniffFile(file_path)
def sourceCodeSniffFile(self, file_path):
filename_has_been_shown = False
logger().verbose("\t\t- Sniffing a file: %s" % file_path)
for each_section in bar(self.config.sections()):
logger().verbose("\t\t\t- " + each_section.__str__())
pattern = re.compile(self.config.get(each_section, 'Regex'), re.IGNORECASE)
for i, line in enumerate(open(file_path)):
for match in re.finditer(pattern, line):
if filename_has_been_shown == False:
print file_path
filename_has_been_shown = True
logger().debug('\t-Found %s on line %s: %s' % (self.config.get(each_section, 'Message'), i + 1 , match.groups()))
logger().verbose(line)
self._summaryRiskTotal += self.config.get(each_section, 'RiskLevel')
self._summaryCount += 1
self._summaryReportIssuesByFile[file_path] += 1
if self._summaryReportHighestRiskLevel[file_path] < self.config.get(each_section, 'RiskLevel'):
self._summaryReportHighestRiskLevel[file_path] = self.config.get(each_section, 'RiskLevel')
##################################################################################
# Entry point for command-line execution
##################################################################################
def main(self):
self.print_banner()
print(Colored.red("Using configuration files: " + str(self._config_files)))
print(Colored.red("Recursively sniffing path for dangerous code: " + self._path_to_scan))
sys.stderr = open("errorlog.txt", 'w')
# load config
self.config = ConfigParser.ConfigParser()
self.config.read(self._config_files)
# remove previous report
if os.path.isfile(self._report_filename):
os.remove(self._report_filename)
self.sourceCodeSniffFolder()
sys.exit(0)
return 0
def bar(it, label='', width=32, hide=None, empty_char=BAR_EMPTY_CHAR, filled_char=BAR_FILLED_CHAR, expected_size=None,
every=1):
"""Progress iterator. Wrap your iterables with it."""
count = len(it) if expected_size is None else expected_size
with Bar(label=label, width=width, hide=hide, empty_char=BAR_EMPTY_CHAR,
filled_char=BAR_FILLED_CHAR, expected_size=count, every=every) \
as bar:
for i, item in enumerate(it):
yield item
bar.show(i + 1)
def main(argv=None):
sourceCodeSnifferMain = SourceCodeSnifferMain(argv if argv else sys.argv[1:])
return sourceCodeSnifferMain.main()
if __name__ == "__main__":
sys.exit(main())