#!/usr/bin/env python3 """ The experiment log files should be in the same directory as this script. The fingerprint files should be in ./devices. """ import glob import json import statistics from lib.utils import fuzzy_intersection high_risk = ("DNS", "open port") def ll_to_ht(ll): return {(x[0], x[1]) for x in ll} def get_jaccard(l, r): l, r = ll_to_ht(l), ll_to_ht(r) intersection = fuzzy_intersection(l, r) union = len(l) + len(r) - intersection return (intersection / union) * 100.0 def get_vanilla_jaccard(l, r): l, r = ll_to_ht(l), ll_to_ht(r) intersection = len(l.intersection(r)) union = len(l.union(r)) return (intersection / union) * 100.0 dts = {} for device_file in glob.glob("devices/*"): with open(device_file, "r") as device_type_file: uuid = device_file.replace("\\", "/").split("/")[-1] dts[uuid] = json.load(device_type_file) for fn in glob.glob("experiment-log*.txt"): with open(fn, "r") as f: parts = f.read().split("--- next run ---") id_line = parts[0].split("Recording for device type:")[1].splitlines()[0] rec_uuid = id_line.split("uuid=")[1].split(",")[0] rec_ip = id_line.split("at ")[1] parts = parts[1:] scores_no_high_risk, count, scores = [], [], [] next_best = () for p in parts: last_record = p.split("Local device at {} has characteristics".format(rec_ip))[-1].splitlines()[0].rsplit(",", 1)[0] last_record = json.loads(last_record) # get_jaccard = get_vanilla_jaccard scores.append(get_jaccard(last_record, dts[rec_uuid]["characteristics"])) scores_no_high_risk.append(get_jaccard([l for l in last_record if l[0] not in high_risk], [l for l in dts[rec_uuid]["characteristics"] if l[0] not in high_risk])) next_best = max([(get_jaccard(d["characteristics"], last_record), d["name"]) for d in dts.values() if d["name"] != dts[rec_uuid]["name"]], key=lambda x: x[0]) next_best_high_risk = max([(get_jaccard([l for l in d["characteristics"] if l[0] not in high_risk], [l for l in last_record if l[0] not in high_risk]), d["name"]) for d in dts.values() if d["name"] != dts[rec_uuid]["name"]], key=lambda x: x[0]) count = [] types = set([r[0] for r in last_record] + [r[0] for r in dts[rec_uuid]["characteristics"]]) for r in types: count.append((r, len([c for c in last_record if c[0] == r]))) print("Device {}:\tScores {}\t({} without high risk),\tmean {}\t({}),\tvariance {}\t({}),\tstdev {}\t({}),\tnext best: {}\tdist: {}\t({}),\tcount: {}".format( dts[rec_uuid]["name"], scores, scores_no_high_risk, statistics.mean(scores), statistics.mean(scores_no_high_risk), statistics.variance(scores), statistics.variance(scores_no_high_risk), statistics.stdev(scores), statistics.stdev(scores_no_high_risk), next_best, statistics.mean(scores)-next_best[0], next_best_high_risk, count))