|
|
@@ -5,11 +5,26 @@ from datetime import datetime
|
|
|
class Sample:
|
|
|
EPOCH = datetime(1970, 1, 1)
|
|
|
TIME_FMT = '%Y-%m-%d %H:%M:%S.%f'
|
|
|
-
|
|
|
+
|
|
|
+ # Boundaries measured in packets/second
|
|
|
+ def set_activity_thresholds(lower_bound: float, upper_bound: float,
|
|
|
+ lookback: float):
|
|
|
+ assert(lower_bound < upper_bound)
|
|
|
+ assert(0 < lower_bound)
|
|
|
+ assert(0 < lookback)
|
|
|
+ Sample.high_act_threshold = upper_bound
|
|
|
+ Sample.low_act_threshold = lower_bound
|
|
|
+ Sample.lookback = lookback
|
|
|
+
|
|
|
+ def packet_time(p):
|
|
|
+ return (datetime.strptime(p.time, Sample.TIME_FMT) - Sample.EPOCH) \
|
|
|
+ .total_seconds()
|
|
|
+
|
|
|
def __init__(self, keylog: typing.TextIO, pcap: typing.BinaryIO):
|
|
|
self.extract_tag(keylog)
|
|
|
f = pyshark.FileCapture(pcap.strip(), only_summaries=True)
|
|
|
f.load_packets()
|
|
|
+ self.extract_activity_stats(f)
|
|
|
self.extract_packet_stats(f)
|
|
|
|
|
|
def extract_tag(self, keylog: typing.TextIO):
|
|
|
@@ -18,11 +33,35 @@ class Sample:
|
|
|
self.is_guided = os.path.basename(dir_guided) == "y"
|
|
|
dir_user = os.path.dirname(dir_guided)
|
|
|
self.user = os.path.basename(dir_user)
|
|
|
+
|
|
|
+ def extract_activity_stats(self, keylog: typing.TextIO):
|
|
|
+ high_activity = []
|
|
|
+ mid_activity = []
|
|
|
+ low_activity = []
|
|
|
+ q = []
|
|
|
+ for p in keylog:
|
|
|
+ ptime = Sample.packet_time(p)
|
|
|
+ q.append(p)
|
|
|
+ if len(q) < self.low_act_threshold:
|
|
|
+ low_activity.append(q)
|
|
|
+ elif len(q) > self.high_act_threshold:
|
|
|
+ high_activity.append(q)
|
|
|
+ else:
|
|
|
+ mid_activity.append(q)
|
|
|
+ while Sample.packet_time(q[0]) + self.lookback < ptime:
|
|
|
+ q = q[1:]
|
|
|
+ self.activities = {
|
|
|
+ "high": Sample.count_activity_stats(high_activity),
|
|
|
+ "mid": Sample.count_activity_stats(mid_activity),
|
|
|
+ "low": Sample.count_activity_stats(low_activity)
|
|
|
+ }
|
|
|
+
|
|
|
+ def count_activity_stats(arr):
|
|
|
+ return {
|
|
|
+ "total packets": len(arr)
|
|
|
+ }
|
|
|
|
|
|
def extract_packet_stats(self, pcap):
|
|
|
- start = (datetime.strptime(pcap[0].time, self.TIME_FMT) - self.EPOCH)\
|
|
|
- .total_seconds()
|
|
|
- end = (datetime.strptime(pcap[-1].time, self.TIME_FMT) - self.EPOCH)\
|
|
|
- .total_seconds()
|
|
|
+ start = Sample.packet_time(pcap[0])
|
|
|
+ end = Sample.packet_time(pcap[-1])
|
|
|
self.average_iat = self.average_iat = (end - start) / len(pcap)
|
|
|
-
|