forked from kpdyer/website-fingerprinting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Datastore.py
160 lines (136 loc) · 5.84 KB
/
Datastore.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# 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 MySQLdb
import math
import config
import pcapparser
from Webpage import Webpage
from Trace import Trace
from Packet import Packet
import memcache
mc = memcache.Client(['127.0.0.1:11211'], debug=0)
ENABLE_CACHE = False
import cPickle
class Datastore:
conn = None
@staticmethod
def getWebpagesLL( webpageIds, traceIndexStart, traceIndexEnd ):
webpages = []
for webpageId in webpageIds:
webpage = Webpage(webpageId)
for traceIndex in range(traceIndexStart, traceIndexEnd):
trace = Datastore.getTraceLL( webpageId, traceIndex )
webpage.addTrace(trace)
webpages.append(webpage)
return webpages
@staticmethod
def getTraceLL( webpageId, traceIndex ):
key = '.'.join(['Webpage',
'LL',
str(webpageId),
str(traceIndex)])
trace = mc.get(key)
if ENABLE_CACHE and trace:
trace = cPickle.loads(trace)
else:
dateTime = config.DATA_SET[traceIndex]
trace = pcapparser.readfile(dateTime['month'],
dateTime['day'],
dateTime['hour'],
webpageId)
mc.set(key,cPickle.dumps(trace,protocol=cPickle.HIGHEST_PROTOCOL))
return trace
@staticmethod
def getWebpagesHerrmann( webpageIds, traceIndexStart, traceIndexEnd ):
webpages = []
for webpageId in webpageIds:
webpage = Webpage(webpageId)
for traceIndex in range(traceIndexStart, traceIndexEnd):
trace = Datastore.getTraceHerrmann( webpageId, traceIndex )
webpage.addTrace(trace)
webpages.append(webpage)
return webpages
@staticmethod
def getTraceHerrmann( webpageId, traceIndex ):
if config.DATA_SOURCE == 1:
datasourceId = 4
elif config.DATA_SOURCE == 2:
datasourceId = 5
key = '.'.join(['Webpage',
'H',
str(datasourceId),
str(webpageId),
str(traceIndex)])
trace = mc.get(key)
if ENABLE_CACHE and trace:
trace = cPickle.loads(trace)
else:
connection = MySQLdb.connect(host=config.MYSQL_HOST,
user=config.MYSQL_USER,
passwd=config.MYSQL_PASSWD,
db=config.MYSQL_DB )
cursor = connection.cursor()
command = """SELECT packets.trace_id,
packets.size,
ROUND(packets.abstime*1000)
FROM (SELECT id
FROM traces
WHERE site_id = (SELECT id
FROM sites
WHERE dataset_id = """+str(datasourceId)+"""
ORDER BY id
LIMIT """+str(webpageId)+""",1)
ORDER BY id
LIMIT """+str(traceIndex)+""",1) traces,
packets
WHERE traces.id = packets.trace_id
ORDER BY packets.trace_id, packets.abstime"""
cursor.execute( command )
data = cursor.fetchall()
trace = Trace(webpageId)
for item in data:
trace.setId(int(item[0]))
direction = Packet.UP
if int(item[1])>0:
direction = Packet.DOWN
time = item[2]
length = int(math.fabs(item[1]))
trace.addPacket( Packet( direction, time, length ) )
connection.close()
mc.set(key,cPickle.dumps(trace,protocol=cPickle.HIGHEST_PROTOCOL))
return trace
@classmethod
def get_trace(cls, trace_id=None, site_id=None, dataset=2, limit=1, multi=False):
if cls.conn is None:
cls.conn = MySQLdb.connect(host=config.MYSQL_HOST,
user=config.MYSQL_USER,
passwd=config.MYSQL_PASSWD,
db=config.MYSQL_DB)
cur = cls.conn.cursor()
if trace_id is None:
cur.execute('SELECT id FROM traces where site_id=%s ORDER BY RAND() LIMIT {}'.format(limit), [site_id])
if limit == 1:
trace_ids = [cur.fetchone()[0]]
else:
trace_ids = [r[0] for r in cur.fetchall()]
else:
trace_ids = [trace_id]
# print('SEL-TRACE', trace_ids)
traces = []
for trace_id in trace_ids:
cur.execute('SELECT size, ROUND(abstime*1000) FROM packets WHERE trace_id=%s ORDER BY abstime',
[trace_id])
data = cur.fetchall()
trace = Trace(trace_id, webpage=site_id)
for item in data:
direction = Packet.UP
if int(item[0]) > 0:
direction = Packet.DOWN
time = item[1]
length = int(math.fabs(item[0]))
trace.addPacket(Packet(direction, time, length))
traces.append(trace)
if limit == 1 and not multi:
return traces[0] if traces else None
return traces