-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathburplog-parser.py
More file actions
264 lines (241 loc) · 9.99 KB
/
burplog-parser.py
File metadata and controls
264 lines (241 loc) · 9.99 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
#!/usr/bin/env python
import json
import cgi
import sys
def exportAsJSON(itemlist,filename):
newitemlist = []
for item in itemlist:
newitemlist.append({'time':item['time'],
'protocol':item['protocol'],#0
'hostname':item['hostname'],#1
'port':item['port'],#2
'hostip':item['hostip'],#3
'req_headers':item['request']['headers'],#4
'req_body':item['request']['body'],#5
'req_command':item['request']['command'],#6
'req_path':item['request']['path'],#7
'resp_headers':item['response']['headers'],#8
'resp_body':item['response']['body'],#9
'resp_status':item['response']['status'],#10
'resp_reason':item['response']['reason']})#11
outderdict = {'data':newitemlist}
f = open(filename, 'w')
f.write(json.dumps(outderdict,sort_keys=True,indent=4, separators=(',', ': ')))
f.close()
def exportAsArray(itemlist):
newitemlist = []
for item in itemlist:
newitemlist.append([item['time'],
item['protocol'],
item['hostname'],
item['hostip'],
item['port'],
item['request']['command'],
item['request']['path'],
item['response']['status'],
item['response']['reason'],
item['request']['headers'],
item['request']['body'],
item['response']['headers'],
item['response']['body']])
for rowid,row in enumerate(newitemlist):
for colid,col in enumerate(row):
if col == None:
newitemlist[rowid][colid]=''
return newitemlist
def exportAsHTML(itemlist,filename):
htmlhead = """<html>
<head>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.11/css/jquery.dataTables.min.css" />
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script src="https://cdn.datatables.net/1.10.11/js/jquery.dataTables.min.js"></script>
<script>
var dataSet ="""
htmlfoot = """;
function format ( d ) {
// `d` is the original data object for the row
reqheaders = "";
reqbody = "";
respheaders = "";
respbody="";
if (d[9] != null){
reqheaders = d[9]+'\\n\\n';
}
if (d[10] != null){
reqbody = d[10]+'\\n\\n';
}
if (d[11] != null){
respheaders = d[11]+'\\n\\n';
}
if (d[12] != null){
respbody = d[12]+'\\n\\n';
}
return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;text-align:top;width:500px">'+
'<tr>'+
// '<td>test</td>'+
'<td style="vertical-align:top;"><pre>'+reqheaders+reqbody+'</pre></td><td style="vertical-align:top;"><pre>'+respheaders+respbody+'</pre></td>'+
'</tr>'+
'</table>';
}
function filterColumn ( i ) {
$('#example').DataTable().column( i ).search($('#col_'+i+'_filter').val(),true,false).draw();
}
$(document).ready(function() {
var table = $('#example').DataTable( {
//"ajax": "burplog.json",
data: dataSet,
"columns": [
{ title:"time","className":'details-control'},
{ title:"protocol","className":'details-control'},
{ title:"hostname","className":'details-control'},
{ title:"hostip","className":'details-control'},
{ title:"port","className":'details-control'},
{ title:"req_command","className":'details-control'},
{ title:"req_path","className":'details-control'},
{ title:"resp_status","className":'details-control'},
{ title:"resp_reason","className":'details-control'},
{ title:"req_headers","className":'details-control',"visible":false},
{ title:"req_body","className":'details-control',"visible":false},
{ title:"resp_headers","className":'details-control',"visible":false},
{ title:"resp_body","className":'details-control',"visible":false},
]
} );
$('#example tbody').on('click', 'td.details-control', function () {
var tr = $(this).closest('tr');
var row = table.row( tr );
if ( row.child.isShown() ) {
// This row is already open - close it
row.child.hide();
tr.removeClass('shown');
}
else {
// Open this row
row.child( format(row.data()) ).show();
tr.addClass('shown');
}
} );
$('input.adv_filter').on( 'keyup click', function () {
filterColumn( $(this).attr('filter-id') );
} );
} );
</script>
</head>
<body>
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>time</th>
<th>protocol</th>
<th>hostname</th>
<th>hostip</th>
<th>port</th>
<th>req-command</th>
<th>req-path</th>
<th>resp-status</th>
<th>resp-reason</th>
<th>req-headers</th>
<th>req-body</th>
<th>resp-headers</th>
<th>resp-body</th>
</tr>
</thead>
<tfoot>
<tr>
<td><input type="text" class="adv_filter" filter-id="0" id="col_0_filter" placeholder="filter time"/></td>
<td><input type="text" class="adv_filter" filter-id="1" id="col_1_filter" placeholder="filter protocol"/></td>
<td><input type="text" class="adv_filter" filter-id="1" id="col_1_filter" placeholder="filter host"/></td>
<td><input type="text" class="adv_filter" filter-id="2" id="col_2_filter" placeholder="filter ip"/></td>
<td><input type="text" class="adv_filter" filter-id="3" id="col_3_filter" placeholder="filter port"/></td>
<td><input type="text" class="adv_filter" filter-id="4" id="col_4_filter" placeholder="filter command"/></td>
<td><input type="text" class="adv_filter" filter-id="5" id="col_5_filter" placeholder="filter path"/></td>
<td><input type="text" class="adv_filter" filter-id="6" id="col_6_filter" placeholder="filter status"/></td>
<td><input type="text" class="adv_filter" filter-id="7" id="col_7_filter" placeholder="filter reason"/></td>
</tr>
</tfoot>
</table>
</body>
<h3>Advanced Filters</h3>
<table id="filters">
<tr><td><input type="text" class="adv_filter" filter-id="9" id="col_9_filter" placeholder="filter request headers"/></td></tr>
<tr><td><input type="text" class="adv_filter" filter-id="10" id="col_10_filter" placeholder="filter request body"/></td></tr>
<tr><td><input type="text" class="adv_filter" filter-id="11" id="col_11_filter" placeholder="filter response headers"/></td></tr>
<tr><td><input type="text" class="adv_filter" filter-id="12" id="col_12_filter" placeholder="filter response body"/></td></tr>
</table>
</html>"""
outfile = open(filename, 'w')
outfile.write(htmlhead+str(exportAsArray(pairs))+htmlfoot)
outfile.close()
def parseRequest(req):
request = None
try:
splitreq = req.split('\n\n',1)
firstline = splitreq[0].split('\n')[0]
command = cgi.escape(firstline.split(' ')[0])
path =cgi.escape(firstline.split(' ')[1])
version = cgi.escape(firstline.split(' ')[2])
headers = cgi.escape(splitreq[0].strip())
body = None
if len(splitreq[1].strip())!=0: #is there a request body?
body = cgi.escape(splitreq[1].strip())
request = {'command':command,'path':path,'version':version,'headers':headers,'body':body}
except:
request = {'command':None,'path':None,'version':None,'headers':None,'body':None}
return request
def parseResponse(resp):
response = None
try:
splitresp = resp.split('\n\n',1)
firstline = splitresp[0].split('\n')[0]
version = cgi.escape(firstline.split(' ')[0])
status =cgi.escape(firstline.split(' ')[1])
reason = cgi.escape(firstline.split(' ')[2])
headers = cgi.escape(splitresp[0].strip())
body = None
if len(splitresp[1].strip())!=0: #is there a body?
body = cgi.escape(splitresp[1].strip())
response = {'version':version,'status':status,'reason':reason,'headers':headers,'body':body}
except:
response = {'version':None,'status':None,'reason':None,'headers':None,'body':None}
return response
if len(sys.argv) != 3:
print "Useage: "+sys.argv[0]+" <burplog> <htmlfile>"
exit()
logfile = open(sys.argv[1]).read()
if "======================================================" in logfile.split('\n')[0]: #hacky bit to move first line
lf2 = '\n'.join(logfile.split('\n')[1:])
logfile = lf2
pairsep = "======================================================\n\n\n\n======================================================\n"
pairs = []
for item in logfile.split(pairsep):
hostinfo = item.split('\n')[0].split(' ')
try:
time = cgi.escape(hostinfo[0])
except:
time = None
try:
protocol = cgi.escape(hostinfo[1].rsplit(':',1)[0].split('://')[0])
except:
protocol = None
try:
hostname = cgi.escape(hostinfo[1].rsplit(':',1)[0].split('://')[1])
except:
hostname = None
try:
port = cgi.escape(hostinfo[1].rsplit(':',1)[1])
except:
port = None
try:
hostiptmp = hostinfo[2]
hostip = cgi.escape(hostiptmp[1:-1])
except:
hostip = None
if "======================================================\nHTTP" in item:#is there a response?
#there is a response
req = item.split("======================================================\n")[1]
resp = item.split("======================================================\n")[2]
else:
#there isn't a response
req = item.split("======================================================\n")[1]
resp = None
pairs.append({'time':time,'protocol':protocol,'hostname':hostname,'port':port,'hostip':hostip,'request':parseRequest(req),'response':parseResponse(resp)})
exportAsHTML(pairs,sys.argv[2])