-
Notifications
You must be signed in to change notification settings - Fork 33
/
reqparser.py
178 lines (139 loc) · 5.6 KB
/
reqparser.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
from difflib import SequenceMatcher
from waflogger import loginfo
from waflogger import logdebug
from static.statics import parseKey
from static.statics import headerFile
class HTTPReq:
def __init__(self):
self.im = None
self.accept = None
self.accept_charset = None
self.accept_encoding = None
self.accept_language = None
self.accept_datetime = None
self.access_control_req_method = None
self.access_control_req_headers = None
self.authorization = None
self.cache_control = None
self.connection = None
self.content_length = None
self.content_type = None
self.cookie = None
self.date = None
self.expect = None
self.forwarded = None
self.reqfrom = None
self.host = None
self.if_match = None
self.if_modified_since = None
self.if_none_match = None
self.if_range = None
self.if_unmodified_since = None
self.max_forwards = None
self.method = None
self.origin = None
self.pragma = None
self.proxy_authorization = None
self.range = None
self.referer = None
self.te = None
self.uri = None
self.http = None
self.user_agent = None
self.upgrade = None
self.upgrade_insecure_requests = None
self.via = None
self.warning = None
self.dnt = None
self.x_requested_with = None
self.x_csrf_token = None
self.expiresself = None
self.body = None
self.test = None
self.result = None
# Request object
def generateobj():
global myreq
myreq = HTTPReq()
def getobj():
return myreq
def addheader(self, newheader, newvalue):
self.__dict__[newheader] = newvalue
def delheader(self, oldheader):
self.__dict__[oldheader.lower()] = None
self.__dict__[oldheader] = None
def changeheader(self, header, newvalue):
self.__dict__[header.lower()] = self.__dict__[header.lower()] + parseKey + newvalue
def getheaderList():
headerfile = open(headerFile, 'r')
headerList = []
for header in headerfile:
headerList.append(header.strip())
return headerList
def getdiffratio(header, objattr):
newobj = ""
if objattr == "reqfrom":
newobj = "FROM"
if newobj == "":
newobj = objattr
return objattr, SequenceMatcher(None, header.upper(), newobj.upper()).ratio()
def extbody(line, lastpart):
if myreq.body is None:
myreq.body = ""
if myreq.content_type is not None:
if "multipart" in myreq.content_type and lastpart != 0:
line += "\n"
myreq.body += line
def extmethods(line):
myreq.method = line.split(' ')[0].strip()
myreq.uri = line.split(' ')[1].strip()
myreq.http = line.split(' ')[2].strip()
def concobj(myreq, data, objattr):
if "-" in objattr:
newobj = ""
for leng in range(0, len(objattr.split('-'))):
if leng != int(len(objattr.split('-')) - 1):
newobj += objattr.split('-')[leng] + "_"
else:
newobj += objattr.split('-')[leng]
else:
newobj = objattr
setattr(myreq, newobj.lower(), data)
def parseheaders(usrreqlist):
loginfo('Header parsing module is running')
headers = getheaderList()
methodFlag = 0
payloadFlag = 0
for line in usrreqlist:
if payloadFlag != 0:
if line == usrreqlist[-1]:
extbody(line.strip(), 0)
else:
extbody(line.strip(), 1)
if line == "\n":
payloadFlag += 1
methodFlag += 1
if methodFlag == 1:
extmethods(line)
else:
for header in headers:
checkpart = line.split(' ')[0]
if ":" in checkpart:
checkpart = checkpart[:-1]
objattr, rate = getdiffratio(header, checkpart)
if objattr != myreq.content_type and objattr == "Content-Type" and myreq.content_type is not None:
rate = 0
if rate == 1:
logdebug('Header [' + objattr + '] is found in the request')
if len(line.split(":")) == 2:
data = line.split(":")[1].strip()
concobj(myreq, data, objattr)
else:
newline = ""
for leng in range(0, len(line.split(':'))):
if leng != 0:
if leng != len(line.split(':'))-1:
newline += line.split(":")[leng].strip() + ":"
else:
newline += line.split(":")[leng].strip()
concobj(myreq, newline, objattr)