-
Notifications
You must be signed in to change notification settings - Fork 17
/
convert-burp-suite-http-proxy-history-to-csv.py
214 lines (174 loc) · 6.42 KB
/
convert-burp-suite-http-proxy-history-to-csv.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
"""
Python script that converts Burp Suite HTTP proxy history files to CSV or HTML files
"""
from __future__ import unicode_literals
from __future__ import print_function
import sys
import io
import argparse
import html
import base64
import xmltodict
from backports import csv
_g_csv_delimiter = ','
def main():
args = parse_arguments()
set_csv_delimiter(args.csv_delimiter)
format_handler = FORMATS[args.format](args.filename)
http_history = parse_http_history(args.filename)
convert_to_output_file(http_history, format_handler)
def parse_arguments():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('filename', help='Burp Suite HTTP proxy history file')
parser.add_argument('--format', default='html', choices=FORMATS.keys(),
help='output format, default: html')
parser.add_argument('--csv-delimiter', choices=(',', ';'),
help='CSV delimiter, default: ,')
return parser.parse_args()
def convert_to_output_file(http_history, format_handler):
with io.open(format_handler.filename, 'w', encoding='utf-8', newline='') as output_file:
format_handler.set_output_file(output_file)
format_handler.header_prefix()
format_handler.header_column('Time')
format_handler.header_column('URL')
format_handler.header_column('Hostname')
format_handler.header_column('IP address')
format_handler.header_column('Port')
format_handler.header_column('Protocol')
format_handler.header_column('Method')
format_handler.header_column('Path')
format_handler.header_column('Extension')
format_handler.header_column('Request')
format_handler.header_column('Status')
format_handler.header_column('Response length')
format_handler.header_column('MIME type')
format_handler.header_column('Response')
format_handler.header_column('Comment')
format_handler.header_suffix()
for line in http_history['items']['item']:
format_handler.row_prefix()
format_handler.row_column(line['time'])
format_handler.row_column(line['url'])
format_handler.row_column(line['host']['#text'])
format_handler.row_column(line['host']['@ip'])
format_handler.row_column(line['port'])
format_handler.row_column(line['protocol'])
format_handler.row_column(line['method'])
format_handler.row_column(line['path'])
format_handler.row_column(line['extension'])
if '#text' in line['request']:
format_handler.row_column(line['request']['#text'], encoded=True)
else:
format_handler.row_column("None")
format_handler.row_column(line['status'])
format_handler.row_column(line['responselength'])
format_handler.row_column(line['mimetype'])
if '#text' in line['response']:
format_handler.row_column(line['response']['#text'], encoded=True)
else:
format_handler.row_column("None")
format_handler.row_column(line['comment'])
format_handler.row_suffix()
format_handler.footer()
def parse_http_history(filename):
with open(filename, 'rb') as f:
return xmltodict.parse(f)
def base64decode(line):
decoded = base64.b64decode(line)
replace = 'backslashreplace' if sys.version_info[0] >= 3 else 'replace'
return decoded.decode('UTF-8', errors=replace)
def set_csv_delimiter(csv_delimiter):
if csv_delimiter:
global _g_csv_delimiter
_g_csv_delimiter = unicode(csv_delimiter)
class FormatHandlerBase(object):
def __init__(self, filename):
self.filename = filename + self.FILENAME_SUFFIX
class HtmlFormatHandler(FormatHandlerBase):
FILENAME_SUFFIX = '.html'
HEADER = '''<!DOCTYPE html>
<html>
<head>
<title>Burp Suite proxy history</title>
<style>
table {
border-collapse: collapse;
}
table, th, td {
border: 1px solid black;
font-family: Arial, sans-serif;
padding: 5px;
}
th {
text-align: left;
}
td {
vertical-align: top;
}
</style>
</head>
<body>
<table><thead><tr>
'''
FOOTER = '''</tbody></table>
</body></html>
'''
def set_output_file(self, output_file):
self.output_file = output_file
def header_prefix(self):
print(self.HEADER,
file=self.output_file)
def header_suffix(self):
print('</tr></thead><tbody>',
file=self.output_file)
def header_column(self, column_name):
print('<th>%s</th>' % column_name,
file=self.output_file)
def row_prefix(self):
print('<tr>', file=self.output_file)
def row_suffix(self):
print('</tr>', file=self.output_file)
def row_column(self, content, encoded=False):
template = '<td>%s</td>' if not encoded else '<td><pre>%s</pre></td>'
if encoded:
content = html.escape(base64decode(content))
print(template % content,
file=self.output_file)
def footer(self):
print(self.FOOTER,
file=self.output_file)
# note that total number of characters that an Excel cell can contain is 32,760
class CsvFormatHandler(FormatHandlerBase):
FILENAME_SUFFIX = '.csv'
def set_output_file(self, output_file):
self.writer = csv.writer(output_file, dialect='excel',
delimiter=_g_csv_delimiter)
self.header = []
def header_prefix(self):
pass
def header_suffix(self):
self.writer.writerow(self.header)
def header_column(self, column_name):
self.header.append(column_name)
def row_prefix(self):
self.row = []
def row_suffix(self):
self.writer.writerow(self.row)
def row_column(self, content, encoded=False):
if content and encoded:
content = base64decode(content)
# total number of characters that an Excel cell can contain is 32,760
if content and len(content) > 32760:
content = content[:32744] + '..[TRUNCATED!]'
self.row.append(content)
def footer(self):
pass
FORMATS = {
'html': HtmlFormatHandler,
'csv': CsvFormatHandler,
}
if __name__ == '__main__':
# In python 3, unicode is renamed to str
if sys.version_info[0] >= 3:
unicode = str
main()