-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfortify_parser.py
More file actions
327 lines (248 loc) · 10.7 KB
/
fortify_parser.py
File metadata and controls
327 lines (248 loc) · 10.7 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
import xml.etree.ElementTree as ET
import xlsxwriter
import sys
try:
# Taking input
xml_file_name = sys.argv[1]
# Checking for xml extension
if xml_file_name.split(".")[-1] != "xml":
print("Please provide an xml file only")
exit()
# Parsing the xml file
tree = ET.parse(xml_file_name)
root = tree.getroot()
# Handling absence of files in command lines
except IndexError as e:
print("Usage: python fortify_parser.py <xml file name>")
exit()
# Handling improper structured xml file
except ET.ParseError as e:
print("Could not parse the given XML file, please check the format")
exit()
# Variable Declaration
report = {}
counter = 1
compiled_info = {'critical': [],
'high': [],
'medium': [],
'low': []}
report_root = root.findall(".//ReportSection[3]/SubSection/IssueListing/Chart//GroupingSection")
def main():
# Creating a new Excel file
workbook = xlsxwriter.Workbook('Fortify Report.xlsx')
# Worksheets declaration
worksheet = workbook.add_worksheet('Static Scan Results')
# Initialising text formatting dictionary
text_format = set_text_format(workbook)
# Setting the header fields
set_headers(worksheet, text_format['header'])
for parent in report_root:
report["security_risk"] = parent.find("groupTitle").text
child = parent.findall('Issue')
for grand_child in child:
# Fetching header objects
severity = grand_child.find("Folder")
description = grand_child.find("Abstract")
source_file_name = grand_child.find("Source/FileName")
source_file_path = grand_child.find("Source/FilePath")
source_line_number = grand_child.find("Source/LineStart")
sink_file_name = grand_child.find("Primary/FileName")
sink_file_path = grand_child.find("Primary/FilePath")
sink_line_number = grand_child.find("Primary/LineStart")
comments = grand_child.findall("Comment")
# Preparing the dictionary of report
if severity is not None:
report["severity"] = severity.text
else:
report["severity"] = ''
if description is not None:
report["description"] = description.text
else:
report["description"] = ''
if source_file_name is not None:
report["source_file_name"] = source_file_name.text
else:
report["source_file_name"] = ''
if source_file_path is not None:
report["source_file_path"] = source_file_path.text
else:
report["source_file_path"] = ''
if source_line_number is not None:
report["source_line_number"] = source_line_number.text
else:
report["source_line_number"] = ''
if sink_file_name is not None:
report["sink_file_name"] = sink_file_name.text
else:
report["sink_file_name"] = ''
if sink_file_path is not None:
report["sink_file_path"] = sink_file_path.text
else:
report["sink_file_path"] = ''
if sink_line_number is not None:
report["sink_line_number"] = sink_line_number.text
else:
report["sink_line_number"] = ''
# Extracting comments
actual_comment = ''
if comments is not None:
for sub_comment in comments:
comment = sub_comment.find('Comment')
actual_comment += comment.text + "\n"
report['comments'] = actual_comment[:-1] # For removing the extra new line character
# Compile the results
compile_report(report)
# Populate the values in excel file
print_report(worksheet, text_format['severity'], text_format['normal'])
# Setting the zoom factor of the excel sheet
worksheet.set_zoom(70)
# Closing the workbook
workbook.close()
# Function to compile report into their respective categories
def compile_report(report):
if bool(report['severity']):
if report['severity'] == 'Critical':
compiled_info['critical'].append((report['security_risk'], report['severity'], report['description'],
report['source_file_name'], report['source_file_path'],
report['source_line_number'], report['sink_file_name'],
report['sink_file_path'], report['sink_line_number'], report['comments']))
if report['severity'] == 'High':
compiled_info['high'].append((report['security_risk'], report['severity'], report['description'],
report['source_file_name'], report['source_file_path'],
report['source_line_number'], report['sink_file_name'], report['sink_file_path'],
report['sink_line_number'], report['comments']))
if report['severity'] == 'Medium':
compiled_info['medium'].append((report['security_risk'], report['severity'], report['description'],
report['source_file_name'], report['source_file_path'],
report['source_line_number'], report['sink_file_name'], report['sink_file_path'],
report['sink_line_number'], report['comments']))
if report['severity'] == 'Low':
compiled_info['low'].append((report['security_risk'], report['severity'], report['description'],
report['source_file_name'], report['source_file_path'], report['source_line_number'],
report['sink_file_name'], report['sink_file_path'], report['sink_line_number'],
report['comments']))
# Function to set header fields
def set_headers(worksheet, header_format):
# Setting the column width
worksheet.set_column('A:A', 10)
worksheet.set_column('B:B', 40)
worksheet.set_column('C:C', 10)
worksheet.set_column('D:D', 50)
worksheet.set_column('E:E', 20)
worksheet.set_column('F:F', 40)
worksheet.set_column('G:G', 11)
worksheet.set_column('H:H', 20)
worksheet.set_column('I:I', 40)
worksheet.set_column('J:J', 11)
worksheet.set_column('K:K', 20)
# Populating the header fields
# Serial Number
worksheet.write("A1", "S.No.", header_format)
# Security Risk
worksheet.write("B1", "Security Risk", header_format)
# Severity
worksheet.write("C1", "Severity", header_format)
# Description
worksheet.write("D1", "Description", header_format)
# Source File Name
worksheet.write("E1", "Source File Name", header_format)
# Source File Path
worksheet.write("F1", "Source File Path", header_format)
# Source Line number
worksheet.write("G1", "Line Number", header_format)
# Sink File Name
worksheet.write("H1", "Sink File Name", header_format)
# Sink File Path
worksheet.write("I1", "Sink File Path", header_format)
# Sink Line Number
worksheet.write("J1", "Line Number", header_format)
# Remarks
worksheet.write("K1", "Remarks", header_format)
# Function to set text formatting
def set_text_format(workbook):
# Setting the header text formatting
header_format = workbook.add_format({
'bold': True,
'border': True,
'align': 'center',
'valign': 'vcenter',
'bg_color': '#1E88E5', # Blue
'font_color': '#FFFFFF'}) # White
# Setting the text formatting
normal_text_format = workbook.add_format({
'align': 'left',
'valign': 'vcenter',
'border': True,
'text_wrap': True})
# Setting formatting for severity levels
# Critical
critical_format = workbook.add_format({
'align': 'center',
'valign': 'vcenter',
'border': True,
'font_color': '#FFFFFF', # White
'bg_color': '#FF0000'}) # Red
# High
high_format = workbook.add_format({
'align': 'center',
'valign': 'vcenter',
'border': True,
'font_color': '#FFFFFF', # White
'bg_color': '#FF6E00'}) # Orange
# Medium
medium_format = workbook.add_format({
'align': 'center',
'valign': 'vcenter',
'border': True,
'font_color': '#FFFFFF', # White
'bg_color': '#FFBB00'}) # Yellow
# Low
low_format = workbook.add_format({
'align': 'center',
'valign': 'vcenter',
'border': True,
'font_color': '#FFFFFF', # White
'bg_color': '#27AE60'}) # Green
text_format = {'header': header_format,
'normal': normal_text_format,
'severity': {
'Critical': critical_format,
'High': high_format,
'Medium': medium_format,
'Low': low_format}
}
return text_format
# Function to sort the report by severity
def print_report(worksheet, severity_format, text_format):
# Variable declaration
global counter
# Writing information according to severity of the bug
for sequence in ("critical", "high", "medium", "low"):
for i in compiled_info[sequence]:
row = counter + 1
# Setting Serial number
worksheet.write(f'A{row}', counter, text_format)
# Setting security risk
worksheet.write(f'B{row}', i[0], text_format)
# Setting severity
worksheet.write(f'C{row}', i[1], severity_format[i[1]])
# Setting Description
worksheet.write(f'D{row}', i[2], text_format)
# Setting Source File Name
worksheet.write(f'E{row}', i[3], text_format)
# Setting Source File Path
worksheet.write(f'F{row}', i[4], text_format)
# Setting Source Line Number
worksheet.write(f'G{row}', i[5], text_format)
# Setting Sink File Name
worksheet.write(f'H{row}', i[6], text_format)
# Setting Sink File Path
worksheet.write(f'I{row}', i[7], text_format)
# Setting Sink Line Number
worksheet.write(f'J{row}', i[8], text_format)
# Setting Comments
worksheet.write(f'K{row}', i[9], text_format)
counter += 1
if __name__ == "__main__":
main()
print("Report created successfully")