Skip to content

Commit

Permalink
Merge branch 'master' of github.com:invernizzi/scapy-http
Browse files Browse the repository at this point in the history
Conflicts:
	scapy_http/http.py
  • Loading branch information
invernizzi committed Jan 24, 2016
2 parents b39b1ea + 01df483 commit dd025ef
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Scapy-http
==========

Support for parsing HTTP in Scapy (http://www.secdev.org/projects/scapy/).
Support for parsing HTTP in [Scapy](http://www.secdev.org/projects/scapy/). Compatible with [Scapy3k](https://github.com/phaethon/scapy).


Installation
Expand Down
2 changes: 1 addition & 1 deletion example.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@

packets = scapy.rdpcap('example_network_traffic.pcap')
for p in packets:
print '=' * 78
print('=' * 78)
p.show()
18 changes: 12 additions & 6 deletions scapy_http/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ def _parse_headers(s):
- the headers in a dictionary
- the body '''
try:
headers, body = s.split("\r\n\r\n", 1)
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 = ''
Expand Down Expand Up @@ -64,7 +67,8 @@ def _self_build(obj, field_pos_list=None):
''' Takse an HTTPRequest or HTTPResponse object, and creates its internal
scapy representation as a string. That is, generates the HTTP
packet as a string '''
p = ""
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',
Expand All @@ -76,12 +80,13 @@ def _self_build(obj, field_pos_list=None):
# 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 = ' '
separator = b' '
else:
separator = '\r\n'
separator = newline
# Add the field into the packet
p = f.addfield(obj, p, val + separator)
if p: # The packet might be empty, and in that case it should stay empty.
# 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
Expand Down Expand Up @@ -216,7 +221,8 @@ def guess_payload_class(self, payload):
r"(?:.+?) "
r"HTTP/\d\.\d$"
)
req = payload[:payload.index("\r\n")]
crlfIndex = payload.index("\r\n".encode())
req = payload[:crlfIndex].decode("utf-8")
result = prog.match(req)
if result:
return HTTPRequest
Expand Down

0 comments on commit dd025ef

Please sign in to comment.