Skip to content

Commit 221c6fb

Browse files
gpotter2guedou
authored andcommitted
Minor HTTP improvements
1 parent dae5581 commit 221c6fb

3 files changed

Lines changed: 36 additions & 12 deletions

File tree

scapy/layers/http.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
3232
You can turn auto-decompression/auto-compression off with:
3333
34-
>>> conf.contribs["http"]["auto_compression"] = True
34+
>>> conf.contribs["http"]["auto_compression"] = False
35+
36+
(Defaults to True)
3537
"""
3638

3739
# This file is a modified version of the former scapy_http plugin.
@@ -59,11 +61,9 @@
5961

6062
try:
6163
import brotli
62-
is_brotli_available = True
64+
_is_brotli_available = True
6365
except ImportError:
64-
is_brotli_available = False
65-
log_loading.info("Can't import brotli. Won't be able to decompress "
66-
"data streams compressed with brotli.")
66+
_is_brotli_available = False
6767

6868
if "http" not in conf.contribs:
6969
conf.contribs["http"] = {}
@@ -310,8 +310,14 @@ def post_dissect(self, s):
310310
elif "compress" in encodings:
311311
import lzw
312312
s = lzw.decompress(s)
313-
elif "br" in encodings and is_brotli_available:
314-
s = brotli.decompress(s)
313+
elif "br" in encodings:
314+
if _is_brotli_available:
315+
s = brotli.decompress(s)
316+
else:
317+
log_loading.info(
318+
"Can't import brotli. brotli decompression "
319+
"will be ignored !"
320+
)
315321
except Exception:
316322
# Cannot decompress - probably incomplete data
317323
pass
@@ -330,8 +336,14 @@ def post_build(self, pkt, pay):
330336
elif "compress" in encodings:
331337
import lzw
332338
pay = lzw.compress(pay)
333-
elif "br" in encodings and is_brotli_available:
334-
pay = brotli.compress(pay)
339+
elif "br" in encodings:
340+
if _is_brotli_available:
341+
pay = brotli.compress(pay)
342+
else:
343+
log_loading.info(
344+
"Can't import brotli. brotli compression will "
345+
"be ignored !"
346+
)
335347
return pkt + pay
336348

337349
def self_build(self, field_pos_list=None):
@@ -569,7 +581,7 @@ def tcp_reassemble(cls, data, metadata):
569581
chunked = ("chunked" in encodings)
570582
is_response = isinstance(http_packet.payload, HTTPResponse)
571583
if chunked:
572-
detect_end = lambda dat: dat.endswith(b"\r\n\r\n")
584+
detect_end = lambda dat: dat.endswith(b"0\r\n\r\n")
573585
# HTTP Requests that do not have any content,
574586
# end with a double CRLF
575587
elif isinstance(http_packet.payload, HTTPRequest):

scapy/sessions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def _process_packet(self, pkt):
240240
pay = pkt[TCP].payload
241241
if isinstance(pay, (NoPayload, conf.padding_layer)):
242242
return pkt
243-
new_data = raw(pay)
243+
new_data = pay.original
244244
# Match packets by a uniqute TCP identifier
245245
seq = pkt[TCP].seq
246246
ident = pkt.sprintf(self.fmt)
@@ -256,7 +256,7 @@ def _process_packet(self, pkt):
256256
pay_class = metadata["pay_class"]
257257
# Get a relative sequence number for a storage purpose
258258
relative_seq = metadata.get("relative_seq", None)
259-
if not relative_seq:
259+
if relative_seq is None:
260260
relative_seq = metadata["relative_seq"] = seq - 1
261261
seq = seq - relative_seq
262262
# Add the data to the buffer

test/regression.uts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7750,6 +7750,18 @@ for i in range(3, 10):
77507750
assert HTTP not in pkts[i]
77517751
assert H2Frame in pkts[i]
77527752

7753+
= Test chunked with gzip
7754+
7755+
conf.contribs["http"]["auto_compression"] = False
7756+
z = b'\x1f\x8b\x08\x00S\\-_\x02\xff\xb3\xc9(\xc9\xcd\xb1\xcb\xcd)\xb0\xd1\x07\xb3\x00\xe6\xedpt\x10\x00\x00\x00'
7757+
a = IP(dst="1.1.1.1", src="2.2.2.2")/TCP(seq=1)/HTTP()/HTTPResponse(Content_Encoding="gzip", Transfer_Encoding="chunked")/(b"5\r\n" + z[:5] + b"\r\n")
7758+
b = IP(dst="1.1.1.1", src="2.2.2.2")/TCP(seq=len(a[TCP].payload)+1)/HTTP()/(hex(len(z[5:])).encode()[2:] + b"\r\n" + z[5:] + b"\r\n0\r\n\r\n")
7759+
xa, xb = IP(raw(a)), IP(raw(b))
7760+
conf.contribs["http"]["auto_compression"] = True
7761+
7762+
c = sniff(offline=[xa, xb], session=TCPSession)[0]
7763+
assert gzip_decompress(z) == c.load
7764+
77537765
############
77547766
############
77557767
+ LLMNR protocol

0 commit comments

Comments
 (0)