|
78 | 78 | sys.path.append(CUCKOO_ROOT)
|
79 | 79 |
|
80 | 80 | TLS_HANDSHAKE = 22
|
| 81 | +PCAP_TYPE = None |
81 | 82 |
|
82 | 83 | Keyed = namedtuple("Keyed", ["key", "obj"])
|
83 | 84 | Packet = namedtuple("Packet", ["raw", "ts"])
|
@@ -736,7 +737,12 @@ def run(self):
|
736 | 737 | return self.results
|
737 | 738 |
|
738 | 739 | try:
|
739 |
| - pcap = dpkt.pcap.Reader(file) |
| 740 | + if PCAP_TYPE == "pcap": |
| 741 | + pcap = dpkt.pcap.Reader(file) |
| 742 | + elif PCAP_TYPE == "pcapng": |
| 743 | + pcap = dpkt.pcapng.Reader(file) |
| 744 | + else: |
| 745 | + return self.results |
740 | 746 | except dpkt.dpkt.NeedData:
|
741 | 747 | log.error('Unable to read PCAP file at path "%s"', self.filepath)
|
742 | 748 | return self.results
|
@@ -1087,6 +1093,8 @@ def _import_ja3_fprints(self):
|
1087 | 1093 | return ja3_fprints
|
1088 | 1094 |
|
1089 | 1095 | def run(self):
|
| 1096 | + global PCAP_TYPE |
| 1097 | + PCAP_TYPE = check_pcap_file_type(self.pcap_path) |
1090 | 1098 | self.key = "network"
|
1091 | 1099 | self.ja3_file = self.options.get("ja3_file", os.path.join(CUCKOO_ROOT, "data", "ja3", "ja3fingerprint.json"))
|
1092 | 1100 | if not IS_DPKT:
|
@@ -1225,14 +1233,20 @@ def __init__(self, path, linktype=1):
|
1225 | 1233 | def write(self, p=None):
|
1226 | 1234 | if not self.fileobj:
|
1227 | 1235 | self.fileobj = open(self.name, "wb")
|
1228 |
| - self.fd = dpkt.pcap.Writer(self.fileobj, linktype=self.linktype) |
| 1236 | + if PCAP_TYPE == "pcap": |
| 1237 | + self.fd = dpkt.pcap.Writer(self.fileobj, linktype=self.linktype) |
| 1238 | + elif PCAP_TYPE == "pcapng": |
| 1239 | + self.fd = dpkt.pcapng.Writer(self.fileobj, linktype=self.linktype) |
1229 | 1240 | if p:
|
1230 | 1241 | self.fd.writepkt(p.raw, p.ts)
|
1231 | 1242 |
|
1232 | 1243 | def __iter__(self):
|
1233 | 1244 | if not self.fileobj:
|
1234 | 1245 | self.fileobj = open(self.name, "rb")
|
1235 |
| - self.fd = dpkt.pcap.Reader(self.fileobj) |
| 1246 | + if PCAP_TYPE == "pcap": |
| 1247 | + self.fd = dpkt.pcap.Reader(self.fileobj) |
| 1248 | + elif PCAP_TYPE == "pcapng": |
| 1249 | + self.fd = dpkt.pcapng.Reader(self.fileobj) |
1236 | 1250 | self.fditer = iter(self.fd)
|
1237 | 1251 | self.linktype = self.fd.datalink()
|
1238 | 1252 | return self
|
@@ -1338,3 +1352,17 @@ def packets_for_stream(fobj, offset):
|
1338 | 1352 | fobj.seek(offset)
|
1339 | 1353 | for p in next_connection_packets(pcapiter, linktype=pcap.datalink()):
|
1340 | 1354 | yield p
|
| 1355 | + |
| 1356 | + |
| 1357 | +def check_pcap_file_type(filepath): |
| 1358 | + with open(filepath, "rb") as fd: |
| 1359 | + magic_number = fd.read(4) |
| 1360 | + fd.seek(0) |
| 1361 | + magic_number = int.from_bytes(magic_number, byteorder='little') |
| 1362 | + |
| 1363 | + if magic_number in (0xa1b2c3d4, 0xd4c3b2a1): |
| 1364 | + return "pcap" |
| 1365 | + elif magic_number == 0x0a0d0d0a: |
| 1366 | + return "pcapng" |
| 1367 | + else: |
| 1368 | + return |
0 commit comments