-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_parse.py
201 lines (178 loc) · 6.89 KB
/
log_parse.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
# from xml.etree import ElementTree as ET
# import urllib.parse
# import os
# import base64
# import csv
# log_path = "./good_requests.log"
# output_csv_log='http_log.csv'
# class_flag= "1"
# class LogParse:
# def __init__(self):
# pass
# def parse_log(self, log_path):
# '''
# This function accepts burp log file path
# and returns a dict of requests and responses
# result = {'GET /page.php...':'200 OK HTTP / 1.1....','':'',.....}
# '''
# result = {}
# if not os.path.exists(log_path):
# print("[+] Error!!!", log_path, "doesn't exist..")
# exit()
# try:
# tree = ET.parse(log_path)
# except ET.ParseError:
# print(
# '[+] Oops..! Please make sure binary data is not present in the log, like raw image dump, flash (.swf files) dump, etc.')
# exit()
# root = tree.getroot()
# for reqs in root.findall('item'):
# raw_req = reqs.find('request').text
# if raw_req is not None:
# raw_req = urllib.parse.unquote(raw_req).decode('utf8')
# raw_resp = reqs.find('response').text
# result[raw_req] = raw_resp
# return result
# def parseRawHTTPReq(self, rawreq):
# try:
# raw = rawreq.decode('utf8')
# except Exception as e:
# raw = rawreq
# global headers, method, body, path
# headers = {}
# sp = raw.split('\r\n\r\n', 1)
# if len(sp) > 1:
# head = sp[0]
# body = sp[1]
# else:
# head = sp[0]
# body = ""
# lines = head.split('\r\n')
# request_line = lines[0]
# method, path, _ = request_line.split(' ', 2)
# for line in lines[1:]:
# if ': ' in line:
# key, value = line.split(': ', 1)
# headers[key] = value
# return headers, method, body, path
# #jaii ganesha
# badwords=['sleep', 'drop', 'uid', 'select', 'waitfor', 'delay', 'system', 'union', 'order by', 'group by']
# def ExtractFeatures(method, path_enc, body_enc, headers):
# badwords_count=0
# path = urllib.parse.unquote_plus(path_enc)
# body = urllib.parse.unquote(body_enc)
# single_q = path.count("'") + body.count("'")
# double_q = path.count("\"") + body.count("\"")
# dashes = path.count("--") + body.count("--")
# braces = path.count("(") + body.count("(")
# spaces = path.count(" ") + body.count(" ")
# for word in badwords:
# badwords += path.count(word) + body.count(word)
# for header in headers:
# badwords_count += headers[header].count(word) + headers[header].count(word)
# return [method, path_enc.encode('utf-8').strip(), body_enc.encode('utf-8').strip(), single_q, double_q, dashes,
# braces, spaces, badwords_count,class_flag]
# # Open the log file
# with open(output_csv_log, "w", newline='') as f:
# c = csv.writer(f)
# c.writerow(["method","path", "body","single_q", "double_q", "dashes", "braces","spaces", "badwords", "class"])
# f.close();
# lp= LogParse();
# result = lp.parse_log(log_path)
# f=open(output_csv_log, "w")
# c=csv.writer(f);
# for raw_req in result:
# data = []
# try:
# raaw = base64.b64decode(raw_req.encode('utf-8'))
# headers, method, body, path = lp.parseRawHTTPReq(raaw)
# result=ExtractFeatures(method,path,body, headers )
# c.writerow(result)
# except Exception as e:
# print(f"Error processing request: {e}")
from xml.etree import ElementTree as ET
import urllib.parse
import os
import base64
import csv
log_path = "./good_requests.log"
output_csv_log = 'good.csv'
class_flag = "1"
class LogParse:
def __init__(self):
pass
def parse_log(self, log_path):
'''
This function accepts burp log file path
and returns a dict of requests and responses
result = {'GET /page.php...':'200 OK HTTP / 1.1....','':'',.....}
'''
result = {}
if not os.path.exists(log_path):
print("[+] Error!!!", log_path, "doesn't exist..")
exit()
try:
tree = ET.parse(log_path)
except ET.ParseError:
print('[+] Oops..! Please make sure binary data is not present in the log, like raw image dump, flash (.swf files) dump, etc.')
exit()
root = tree.getroot()
for reqs in root.findall('item'):
raw_req = reqs.find('request').text
if raw_req is not None:
raw_req = urllib.parse.unquote(raw_req)
raw_resp = reqs.find('response').text
result[raw_req] = raw_resp
return result
def parseRawHTTPReq(self, rawreq):
try:
raw = rawreq.decode('utf8')
except Exception as e:
raw = rawreq
global headers, method, body, path
headers = {}
sp = raw.split('\r\n\r\n', 1)
if len(sp) > 1:
head = sp[0]
body = sp[1]
else:
head = sp[0]
body = ""
lines = head.split('\r\n')
request_line = lines[0]
method, path, _ = request_line.split(' ', 2)
for line in lines[1:]:
if ': ' in line:
key, value = line.split(': ', 1)
headers[key] = value
return headers, method, body, path
badwords = ['sleep', 'drop', 'uid', 'select', 'waitfor', 'delay', 'system', 'union', 'order by', 'group by']
def ExtractFeatures(method, path_enc, body_enc, headers):
badwords_count = 0
path = urllib.parse.unquote_plus(path_enc)
body = urllib.parse.unquote(body_enc)
single_q = path.count("'") + body.count("'")
double_q = path.count('"') + body.count('"')
dashes = path.count("--") + body.count("--")
braces = path.count("(") + body.count("(")
spaces = path.count(" ") + body.count(" ")
for word in badwords:
badwords_count += path.count(word) + body.count(word)
for header in headers:
badwords_count += headers[header].count(word) + headers[header].count(word)
return [method, path_enc.encode('utf-8').strip(), body_enc.encode('utf-8').strip(), single_q, double_q, dashes,
braces, spaces, badwords_count, class_flag]
# Open the log file
with open(output_csv_log, "w", newline='') as f:
c = csv.writer(f)
c.writerow(["method", "path", "body", "single_q", "double_q", "dashes", "braces", "spaces", "badwords", "class"])
lp = LogParse()
result = lp.parse_log(log_path)
for raw_req in result:
try:
raaw = base64.b64decode(raw_req.encode('utf-8'))
headers, method, body, path = lp.parseRawHTTPReq(raaw)
row = ExtractFeatures(method, path, body, headers)
c.writerow(row)
except Exception as e:
print(f"Error processing request: {e}")