-
Notifications
You must be signed in to change notification settings - Fork 77
/
http.py
265 lines (237 loc) · 10.3 KB
/
http.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
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
#! /usr/bin/env python
# -*- coding: UTF-8 -*-
# Author : Steeve Barbeau, Luca Invernizzi
# This program is published under a GPLv2 license
import re
from scapy.packet import Packet, bind_layers
from scapy.fields import StrField
from scapy.layers.inet import TCP
def _canonicalize_header(name):
''' Takes a header key (i.e., "Host" in "Host: www.google.com",
and returns a canonical representation of it '''
return name.strip().lower()
def _parse_headers(s):
headers = s.split("\r\n")
headers_found = {}
for header_line in headers:
try:
key, value = header_line.split(':', 1)
except:
continue
headers_found[_canonicalize_header(key)] = header_line.strip()
return headers_found
def _parse_headers_and_body(s):
''' Takes a HTTP packet, and returns a tuple containing:
- the first line (e.g., "GET ...")
- the headers in a dictionary
- the body '''
try:
crlfcrlf = b"\x0d\x0a\x0d\x0a"
crlfcrlfIndex = s.find(crlfcrlf)
headers = s[:crlfcrlfIndex + len(crlfcrlf)].decode("utf-8")
body = s[crlfcrlfIndex + len(crlfcrlf):]
except:
headers = s
body = ''
first_line, headers = headers.split("\r\n", 1)
return first_line.strip(), _parse_headers(headers), body
def _dissect_headers(obj, s):
''' Takes a HTTP packet as the string s, and populates the scapy layer obj
(either HTTPResponse or HTTPRequest). Returns the first line of the
HTTP packet, and the body
'''
first_line, headers, body = _parse_headers_and_body(s)
obj.setfieldval('Headers', '\r\n'.join(list(headers.values())))
for f in obj.fields_desc:
canonical_name = _canonicalize_header(f.name)
try:
header_line = headers[canonical_name]
except:
continue
key, value = header_line.split(':', 1)
obj.setfieldval(f.name, value.strip())
del headers[canonical_name]
if headers:
# Kept for compatibility
obj.setfieldval(
'Additional-Headers', '\r\n'.join(list(headers.values())) + '\r\n')
return first_line, body
def _get_field_value(obj, name):
''' Returns the value of a packet field.'''
val = obj.getfieldval(name)
if name != 'Headers':
return val
# Headers requires special handling, as we give a parsed representation of it.
headers = _parse_headers(val)
val = []
for header_name in headers:
try:
header_value = obj.getfieldval(header_name.capitalize())
# If we provide a parsed representation for this header
headers[header_name] = header_value
val.append('%s: %s' % (header_name.capitalize(), header_value))
except AttributeError as e:
# If we don't provide a parsed representation
val.append(headers[header_name])
return '\r\n'.join(val)
def _self_build(obj, field_pos_list=None):
''' Takes an HTTPRequest or HTTPResponse object, and creates its internal
scapy representation as a string. That is, generates the HTTP
packet as a string '''
p = b""
newline = b'\x0d\x0a' # '\r\n'
# Walk all the fields, in order
for f in obj.fields_desc:
if f.name not in ['Method', 'Path', 'Status-Line', 'Http-Version',
'Headers']:
# Additional fields added for user-friendliness should be ignored
continue
# Get the field value
val = _get_field_value(obj, f.name)
# Fields used in the first line have a space as a separator, whereas
# headers are terminated by a new line
if f.name in ['Method', 'Path', 'Status-Line']:
separator = b' '
else:
separator = newline
# Add the field into the packet
p = f.addfield(obj, p, val + separator)
# The packet might be empty, and in that case it should stay empty.
if p:
# Add an additional line after the last header
p = f.addfield(obj, p, '\r\n')
return p
class HTTPRequest(Packet):
name = "HTTP Request"
http_methods = "^(OPTIONS|GET|HEAD|POST|PUT|DELETE|TRACE|CONNECT)"
fields_desc = [StrField("Method", None, fmt="H"),
StrField("Path", None, fmt="H"),
StrField("Http-Version", None, fmt="H"),
StrField("Host", None, fmt="H"),
StrField("User-Agent", None, fmt="H"),
StrField("Accept", None, fmt="H"),
StrField("Accept-Language", None, fmt="H"),
StrField("Accept-Encoding", None, fmt="H"),
StrField("Accept-Charset", None, fmt="H"),
StrField("Referer", None, fmt="H"),
StrField("Authorization", None, fmt="H"),
StrField("Expect", None, fmt="H"),
StrField("From", None, fmt="H"),
StrField("If-Match", None, fmt="H"),
StrField("If-Modified-Since", None, fmt="H"),
StrField("If-None-Match", None, fmt="H"),
StrField("If-Range", None, fmt="H"),
StrField("If-Unmodified-Since", None, fmt="H"),
StrField("Max-Forwards", None, fmt="H"),
StrField("Proxy-Authorization", None, fmt="H"),
StrField("Range", None, fmt="H"),
StrField("TE", None, fmt="H"),
StrField("Cache-Control", None, fmt="H"),
StrField("Connection", None, fmt="H"),
StrField("Date", None, fmt="H"),
StrField("Pragma", None, fmt="H"),
StrField("Trailer", None, fmt="H"),
StrField("Transfer-Encoding", None, fmt="H"),
StrField("Upgrade", None, fmt="H"),
StrField("Via", None, fmt="H"),
StrField("Warning", None, fmt="H"),
StrField("Keep-Alive", None, fmt="H"),
StrField("Allow", None, fmt="H"),
StrField("Content-Encoding", None, fmt="H"),
StrField("Content-Language", None, fmt="H"),
StrField("Content-Length", None, fmt="H"),
StrField("Content-Location", None, fmt="H"),
StrField("Content-MD5", None, fmt="H"),
StrField("Content-Range", None, fmt="H"),
StrField("Content-Type", None, fmt="H"),
StrField("Expires", None, fmt="H"),
StrField("Last-Modified", None, fmt="H"),
StrField("Cookie", None, fmt="H"),
StrField("Headers", None, fmt="H"),
# Deprecated
StrField("Additional-Headers", None, fmt="H")]
def do_dissect(self, s):
''' From the HTTP packet string, populate the scapy object '''
first_line, body = _dissect_headers(self, s)
Method, Path, HTTPVersion = re.split("\s+", first_line)
self.setfieldval('Method', Method)
self.setfieldval('Path', Path)
self.setfieldval('Http-Version', HTTPVersion)
return body
def self_build(self, field_pos_list=None):
''' Generate the HTTP packet string (the oppposite of do_dissect) '''
return _self_build(self, field_pos_list)
class HTTPResponse(Packet):
name = "HTTP Response"
fields_desc = [StrField("Status-Line", None, fmt="H"),
StrField("Accept-Ranges", None, fmt="H"),
StrField("Age", None, fmt="H"),
StrField("E-Tag", None, fmt="H"),
StrField("Location", None, fmt="H"),
StrField("Proxy-Authenticate", None, fmt="H"),
StrField("Retry-After", None, fmt="H"),
StrField("Server", None, fmt="H"),
StrField("Vary", None, fmt="H"),
StrField("WWW-Authenticate", None, fmt="H"),
StrField("Cache-Control", None, fmt="H"),
StrField("Connection", None, fmt="H"),
StrField("Date", None, fmt="H"),
StrField("Pragma", None, fmt="H"),
StrField("Trailer", None, fmt="H"),
StrField("Transfer-Encoding", None, fmt="H"),
StrField("Upgrade", None, fmt="H"),
StrField("Via", None, fmt="H"),
StrField("Warning", None, fmt="H"),
StrField("Keep-Alive", None, fmt="H"),
StrField("Allow", None, fmt="H"),
StrField("Content-Encoding", None, fmt="H"),
StrField("Content-Language", None, fmt="H"),
StrField("Content-Length", None, fmt="H"),
StrField("Content-Location", None, fmt="H"),
StrField("Content-MD5", None, fmt="H"),
StrField("Content-Range", None, fmt="H"),
StrField("Content-Type", None, fmt="H"),
StrField("Expires", None, fmt="H"),
StrField("Last-Modified", None, fmt="H"),
StrField("Headers", None, fmt="H"),
# Deprecated
StrField("Additional-Headers", None, fmt="H")]
def do_dissect(self, s):
''' From the HTTP packet string, populate the scapy object '''
first_line, body = _dissect_headers(self, s)
self.setfieldval('Status-Line', first_line)
return body
def self_build(self, field_pos_list=None):
''' From the HTTP packet string, populate the scapy object '''
return _self_build(self, field_pos_list)
class HTTP(Packet):
name = "HTTP"
def do_dissect(self, s):
return s
def guess_payload_class(self, payload):
''' Decides if the payload is an HTTP Request or Response, or
something else '''
try:
prog = re.compile(
r"^(?:OPTIONS|GET|HEAD|POST|PUT|DELETE|TRACE|CONNECT) "
r"(?:.+?) "
r"HTTP/\d\.\d$"
)
crlfIndex = payload.index("\r\n".encode())
req = payload[:crlfIndex].decode("utf-8")
result = prog.match(req)
if result:
return HTTPRequest
else:
prog = re.compile(r"^HTTP/\d\.\d \d\d\d .*$")
result = prog.match(req)
if result:
return HTTPResponse
except:
pass
return Packet.guess_payload_class(self, payload)
bind_layers(TCP, HTTP, dport=80)
bind_layers(TCP, HTTP, sport=80)
#For Proxy
bind_layers(TCP, HTTP, sport=8080)
bind_layers(TCP, HTTP, dport=8080)