|
|
@@ -14,8 +14,10 @@ class Sample:
|
|
|
def make_samples(keylog: typing.TextIO,
|
|
|
pcap: typing.BinaryIO,
|
|
|
sample_size: int):
|
|
|
+ import pyshark, os
|
|
|
f = pyshark.FileCapture(pcap.strip(),
|
|
|
display_filter=Sample.FILTER,
|
|
|
+ tshark_path="/usr/sbin/tshark", # TODO: os.environ['PATH']
|
|
|
only_summaries=True)
|
|
|
f.load_packets()
|
|
|
# Start from sample_size to skip incomplete samples
|
|
|
@@ -45,11 +47,13 @@ class Sample:
|
|
|
Sample.BASE_PACKET_SIZE
|
|
|
|
|
|
def __packet_time(p):
|
|
|
- return (datetime.strptime(p.time, Sample.TIME_FMT) - Sample.EPOCH) \
|
|
|
- .total_seconds()
|
|
|
+ if p.time.isnumeric():
|
|
|
+ return int(p.time)
|
|
|
+ else:
|
|
|
+ return (datetime.strptime(p.time, Sample.TIME_FMT) - Sample.EPOCH) \
|
|
|
+ .total_seconds()
|
|
|
|
|
|
def __init__(self, packets, keylog: typing.TextIO):
|
|
|
- import pyshark
|
|
|
self.__general = {}
|
|
|
self.__extract_tag(keylog)
|
|
|
self.__extract_activity_stats(packets)
|
|
|
@@ -74,6 +78,7 @@ class Sample:
|
|
|
i = 0
|
|
|
for p in packets:
|
|
|
ptime = Sample.__packet_time(p)
|
|
|
+ p.length = int(p.length)
|
|
|
p.index = i
|
|
|
i += 1
|
|
|
if q:
|
|
|
@@ -150,6 +155,25 @@ class Sample:
|
|
|
self["large_pastes"] = sum(p.length > Sample.large_paste_threshold
|
|
|
for p in pcaps)
|
|
|
self["small_pastes"] = self["total_pastes"] - self["large_pastes"]
|
|
|
+ if self["total_pastes"] == 0:
|
|
|
+ self["avg_paste_size"] = 0
|
|
|
+ else:
|
|
|
+ self["avg_paste_size"] = sum(p.length for p in pcaps
|
|
|
+ if p.length > Sample.small_paste_threshold) \
|
|
|
+ / self["total_pastes"] - Sample.BASE_PACKET_SIZE
|
|
|
+ if self["large_pastes"] == 0:
|
|
|
+ self["avg_large_paste_size"] = 0
|
|
|
+ else:
|
|
|
+ self["avg_large_paste_size"] = sum(p.length for p in pcaps
|
|
|
+ if p.length > Sample.large_paste_threshold) \
|
|
|
+ / self["large_pastes"] - Sample.BASE_PACKET_SIZE
|
|
|
+ if self["small_pastes"] == 0:
|
|
|
+ self["avg_small_paste_size"] = 0
|
|
|
+ else:
|
|
|
+ self["avg_small_paste_size"] = sum(p.length for p in pcaps
|
|
|
+ if p.length < Sample.large_paste_threshold and
|
|
|
+ p.length > Sample.small_paste_threshold) \
|
|
|
+ / self["small_pastes"] - Sample.BASE_PACKET_SIZE
|
|
|
|
|
|
def __is_valid_prefix(pre):
|
|
|
return pre in ["high", "mid", "low"]
|