Skip to content

Commit 2548263

Browse files
author
Roman Hergenreder
committed
pcap file extract fix
1 parent c50aa4c commit 2548263

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
__all__ = [
66
"util", "fileserver", "xss_handler", "rev_shell",
77
"xp_cmdshell", "dnsserver", "sqli", "smtpserver",
8-
"upload_file"
8+
"upload_file", "pcap_file_extract"
99
]
1010

1111
inc_dir = os.path.dirname(os.path.realpath(__file__))

pcap-file-extract.py pcap_file_extract.py

+24-13
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,26 @@
66
from abc import ABC, abstractmethod
77
from scapy.all import *
88
from hackingscripts import util
9+
from collections import OrderedDict
910

1011

1112
class HttpPacket(ABC):
12-
def __init__(self, version):
13+
def __init__(self, sock_src, version):
1314
self.version = version
1415
self.headers = util.CaseInsensitiveDict()
1516
self.payload = None
17+
self.socket = sock_src
1618

1719
@staticmethod
18-
def parse(data):
20+
def parse(sock_src, data):
1921
index = data.index(b"\r\n")
2022
first_line = data[0:index+2].decode()
2123
matches_req = re.match(HttpRequest.PATTERN.decode(), first_line)
2224
matches_res = re.match(HttpResponse.PATTERN.decode(), first_line)
2325
if matches_req:
24-
http_packet = HttpRequest(*matches_req.groups())
26+
http_packet = HttpRequest(sock_src, *matches_req.groups())
2527
elif matches_res:
26-
http_packet = HttpResponse(*matches_res.groups())
28+
http_packet = HttpResponse(sock_src, *matches_res.groups())
2729
else:
2830
return None
2931

@@ -43,8 +45,8 @@ def get_file_path(self):
4345
class HttpRequest(HttpPacket):
4446
PATTERN = b"([A-Z]+) ([^ ]+) HTTP/([0-9.]+)\r\n"
4547

46-
def __init__(self, method, uri, version):
47-
super().__init__(version)
48+
def __init__(self, socket, method, uri, version):
49+
super().__init__(socket, version)
4850
self.method = method
4951
self.uri = uri
5052

@@ -58,8 +60,8 @@ def get_file_path(self):
5860
class HttpResponse(HttpPacket):
5961
PATTERN = b"HTTP/([0-9.]+) ([0-9]+) (.*)\r\n"
6062

61-
def __init__(self, version, status_code, status_text):
62-
super().__init__(version)
63+
def __init__(self, socket, version, status_code, status_text):
64+
super().__init__(socket, version)
6365
self.status_code = int(status_code)
6466
self.status_text = status_text
6567
self.response_to = None
@@ -150,7 +152,7 @@ def __repr__(self):
150152

151153
class TcpConnections:
152154
def __init__(self):
153-
self.connections = {}
155+
self.connections = OrderedDict()
154156

155157
def __contains__(self, item: TcpConnection):
156158
return str(item) in self.connections
@@ -189,7 +191,6 @@ def __init__(self, pcap_path, output_dir="extracted_files/", filters=None):
189191
self._packets = None
190192

191193
def _open_file(self):
192-
# self._packets = pcapkit.extract(fin=self.pcap_path, store=False, nofile=True)
193194
self._packets = rdpcap(self.pcap_path)
194195

195196
def extract_all(self):
@@ -204,6 +205,15 @@ def extract_all(self):
204205

205206
print(f"[+] Extracted: {file_path} {util.human_readable_size(len(packet.payload))} Bytes")
206207

208+
def __iter__(self):
209+
self._open_file()
210+
http_packets = self._parse_http_packets()
211+
self.iter_filtered_packets = self._apply_filters(http_packets)
212+
return iter(self.iter_filtered_packets)
213+
214+
def __next__(self):
215+
return next(self.iter_filtered_packets)
216+
207217
def _apply_filters(self, packets):
208218
filtered_packets = packets
209219
for f in self.filters:
@@ -238,15 +248,16 @@ def get_http_packet(self, packet_iterator, sock_src, initial_packet):
238248
buff = (next_packet[TCP].seq, raw(next_packet[TCP].payload))
239249
# potential TCP ZeroWindowProbe
240250
continue
241-
251+
252+
# TODO: instead of assertions, we should make sure, the seq. is ascending
242253
assert next_packet[TCP].seq > prev_seq
243254
assert next_packet[IP].frag == 0
244255
http_buffer += raw(next_packet[TCP].payload)
245256
prev_seq = next_packet[TCP].seq
246257
else:
247258
break
248259

249-
return HttpPacket.parse(http_buffer)
260+
return HttpPacket.parse(sock_src, http_buffer)
250261

251262
def _parse_http_packets(self):
252263

@@ -298,7 +309,7 @@ def _parse_http_packets(self):
298309
parser.add_argument("-o", "--output-dir", help="Path to destination directory", default="extracted_files/",
299310
dest="output_dir")
300311
parser.add_argument("-l", "--list", help="List available files only", default=False, action="store_true")
301-
parser.add_argument("-e", "--extract", help="Extract files (default)", default=True, action="store_true")
312+
parser.add_argument("-e", "--extract", help="Extract files (default)", default=False, action="store_true")
302313
parser.add_argument("-ec", "--exclude-codes", help="Exclude http status codes, default: 101,304,403,404",
303314
default="101,304,403,404", dest="exclude_codes")
304315
parser.add_argument("-ic", "--include-codes", help="Limit http status codes", type=str,

0 commit comments

Comments
 (0)