forked from kpdyer/website-fingerprinting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trace.py
132 lines (102 loc) · 4.23 KB
/
Trace.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# This is a Python framework to compliment "Peek-a-Boo, I Still See You: Why Efficient Traffic Analysis Countermeasures Fail".
# Copyright (C) 2012 Kevin P. Dyer (kpdyer.com)
# See LICENSE for more details.
import math
import config
from Packet import Packet
class Trace:
def __init__(self,id):
self.__packetArray = []
self.__id = id
self.__histogramUp = {}
self.__histogramDown = {}
self.__packetsUp = 0
self.__packetsDown = 0
self.__filePath = None
self.__year = 0
self.__month = 0
self.__day = 0
self.__hour = 0
def getId(self): return self.__id
def getPacketCount( self, direction = None ):
return len(self.getPackets(direction))
def getPackets( self, direction = None ):
retArray = []
for packet in self.__packetArray:
if direction == None or packet.getDirection() == direction:
retArray.append( packet )
return retArray
def addPacket( self, packet ):
# Completely ignore ACK packets
if config.IGNORE_ACK and packet.getLength() == Packet.HEADER_LENGTH:
return self.__packetArray
key = str(packet.getDirection())+'-'+str(packet.getLength())
if packet.getDirection()==Packet.UP:
self.__packetsUp += 1
if not self.__histogramUp.get( key ):
self.__histogramUp[key] = 0
self.__histogramUp[key] += 1
elif packet.getDirection()==Packet.DOWN:
self.__packetsDown += 1
if not self.__histogramDown.get( key ):
self.__histogramDown[key] = 0
self.__histogramDown[key] += 1
return self.__packetArray.append( packet )
def getBandwidth( self, direction = None ):
totalBandwidth = 0
for packet in self.getPackets():
if (direction == None or direction == packet.getDirection()) and packet.getLength() != Packet.HEADER_LENGTH:
totalBandwidth += packet.getLength()
return totalBandwidth
def getTime( self, direction = None ):
timeCursor = 0
for packet in self.getPackets():
if direction == None or direction == packet.getDirection():
timeCursor = packet.getTime()
return timeCursor
def getHistogram( self, direction = None, normalize = False ):
if direction == Packet.UP:
histogram = dict(self.__histogramUp)
totalPackets = self.__packetsUp
elif direction == Packet.DOWN:
histogram = dict(self.__histogramDown)
totalPackets = self.__packetsDown
else:
histogram = dict(self.__histogramUp)
for key in self.__histogramDown:
histogram[key] = self.__histogramDown[key]
totalPackets = self.__packetsDown + self.__packetsUp
if normalize==True:
for key in histogram:
histogram[key] = (histogram[key] * 1.0) / totalPackets
return histogram
def calcL1Distance( self, targetDistribution, filterDirection=None ):
localDistribution = self.getHistogram( filterDirection, True )
keys = localDistribution.keys()
for key in targetDistribution:
if key not in keys:
keys.append( key )
distance = 0
for key in keys:
l = localDistribution.get(key)
r = targetDistribution.get(key)
if l == None and r == None: continue
if l == None: l = 0
if r == None: r = 0
distance += math.fabs( l - r )
return distance
def getMostSkewedDimension( self, targetDistribution ):
localDistribution = self.getHistogram( None, True )
keys = targetDistribution.keys()
worstKey = None
worstKeyDistance = 0
for key in keys:
l = localDistribution.get(key)
r = targetDistribution.get(key)
if l == None: l = 0
if r == None: r = 0
if worstKey==None or (r - l) > worstKeyDistance:
worstKeyDistance = r - l
worstKey = key
bits = worstKey.split('-')
return [int(bits[0]),int(bits[1])]