-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrun.py
executable file
·393 lines (315 loc) · 13.3 KB
/
run.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
from flask import Flask,render_template,Blueprint,request,make_response,jsonify
from copy import deepcopy
from fingerprint.attributes_manager import *
from fingerprint.tags_manager import *
from fingerprint.acceptable_manager import *
from flask_babel import Babel
from flask_pymongo import PyMongo
from datetime import datetime, timedelta
from functools import wraps
import env_config as config
import json
import hashlib
import sys
###### App
app = Flask(__name__)
app.debug = config.debug
attributes = Blueprint('site', __name__, static_url_path='', static_folder='fingerprint/attributes',url_prefix='/fp')
app.register_blueprint(attributes)
files,variables = get_files_and_variables()
variablesWithHTTP = [["User-Agent"],["Accept"],["Accept-Language"],["Accept-Encoding"],["Connection"]]
variablesWithHTTP.extend(variables)
definitions = get_definitions()
tagChecker = TagChecker()
tags = tagChecker.getTagList()
acceptableChecker = AcceptableChecker()
unspecifiedValue = "-"
@app.route('/')
def home():
return render_template('home.html')
@app.route('/fp')
def fp():
return render_template('fp.html', files=files, variables=variables, headers=request.headers)
@app.route('/fpNoJS')
def fpNoJS():
# If cookie is not present, we store
# the headers into the database
if "fpcentral" not in request.cookies:
headers = {}
# Transformation from array of tuples to dictionary
for key, value in request.headers:
headers[key] = value
db.storeFP(headers, False)
#Get total number of fingerprints
nbTotal = db.getNumberLifetimeFP()
#Get percentages of all HTTP headers
headersPer = []
for header in request.headers:
if header[0] != "Cookie":
headersPer.append(header+(db.getNumberFP({'name':header[0],"value":header[1]})*100/nbTotal,))
resp = make_response(render_template('fpNoJS.html', headers=headersPer, nbFP = nbTotal))
#We store a cookie if not present
if "fpcentral" not in request.cookies:
resp.set_cookie('fpcentral', 'true', expires=datetime.now() + timedelta(days=config.cookiesDays))
return resp
@app.route('/tor')
def tor():
return render_template('tor.html')
@app.route('/globalStats')
def globalStats():
return render_template('globalStats.html',
totalFP=db.getNumberLifetimeFP(),
epochFP=db.getNumberLastDaysFP(90),
dailyFP=db.getNumberDailyFP(),
lang=db.getValues({"name":"Accept-Language"})
)
@app.route('/customStats')
def customStats():
return render_template('customStats.html',
tags=tags,
listOfVariables=variablesWithHTTP,
)
@app.route('/faq')
def faq():
return render_template('faq.html',definitions=definitions)
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/store', methods=['POST'])
def store():
return json.dumps(db.storeFP(request.data,True))
###### Babel
babel = Babel(app)
@babel.localeselector
def get_locale():
return request.accept_languages.best_match(config.LANGUAGES.keys())
###### DB
class Db(object):
def __init__(self):
#Initialize connection to the Mongo database
app.config['MONGO_HOST'] = config.db_host
app.config['MONGO_PORT'] = config.db_port
app.config['MONGO_DBNAME'] = config.db_dbname
app.config['MONGO_USERNAME'] = config.db_username
app.config['MONGO_PASSWORD'] = config.db_password
app.config['MONGO_CONNECT'] = False #For multiprocessing
self.mongo = PyMongo(app, config_prefix='MONGO')
#Get the list of hashed variables
self.hashedVariables = get_hashed_variables()
######Utility functions
# Hashing method using SHA-256
@staticmethod
def hashValue(value):
return hashlib.sha256(value.encode('ascii', 'ignore')).hexdigest()
# Method to get the date from the date string
@staticmethod
def getStartDate(date):
return datetime.strptime(date, '%Y-%m-%d')
# Method to add one day to the end date so that
# it takes the data collected during the day
@staticmethod
def getEndDate(date):
return datetime.strptime(date, '%Y-%m-%d') + timedelta(days=1)
######Storage
def storeFP(self,fingerprint,decode):
if decode :
parsedFP = json.loads(fingerprint.decode('utf-8'))
else :
parsedFP = fingerprint
#Adding date
parsedFP["date"] = datetime.utcnow()
#Adding tags
parsedFP["tags"] = tagChecker.checkFingerprint(parsedFP)
#Store the complete fingerprint in the main collection
insertedID = self.mongo.db.fp.insert_one(parsedFP).inserted_id
#Compute hashes for hashed variables and
#store them in a secondary collection
hashes = { "_id": insertedID}
for key in parsedFP:
if key in self.hashedVariables:
hashes[key] = self.hashValue(parsedFP[key])
if len(hashes)>1:
self.mongo.db.hash.insert_one(hashes)
if len(parsedFP["tags"]) == 0:
return {'tags': "No tags"}
else:
return {'tags': parsedFP["tags"]}
######Number of fingerprints
#Returns the number of lifetime fingerprints
def getNumberLifetimeFP(self):
return self.mongo.db.fp.count()
#Returns the number of fingerprints collected in the last X days
def getNumberLastDaysFP(self, days):
startID = datetime.today() - timedelta(days=days)
return self.mongo.db.fp.find({"date": {"$gte": startID}}).count()
# Get the number of daily stored fingerprints
def getNumberDailyFP(self):
return list(self.mongo.db.fp.aggregate([{"$group": {"_id": {"year": {"$year": "$date"},
"month": {"$month": "$date"},
"day": {"$dayOfMonth": "$date"}
},
"count": {"$sum": 1}}}]))
#Returns the number of fingerprints
# start/end -> With or without a specific time period
# name/value -> With or without an attribute and its value
# tags -> With or without a list of specific tags
#includeNoJS -> With or without fingerprints without JS
def getNumberFP(self,jsonData):
query = {}
date = {}
if "start" in jsonData:
date["$gte"] = jsonData["start"] if type(jsonData["start"]) is datetime else self.getStartDate(jsonData["start"])
if "end" in jsonData:
date["$lt"] = jsonData["end"] if type(jsonData["end"]) is datetime else self.getEndDate(jsonData["end"])
if len(date) > 0:
query["date"] = date
if "tags" in jsonData and jsonData["tags"] not in ["all","No tags"]:
if "tagComb" in jsonData:
query["tags"] = {"$"+jsonData["tagComb"]: jsonData["tags"]}
else:
#We put "in" by default if it is not specified
query["tags"] = {"$in": jsonData["tags"]}
if "includeNoJS" in jsonData and jsonData["includeNoJS"] == "false":
query["platform"] = {"$exists" : True}
if "value" not in jsonData:
return self.mongo.db.fp.find(query).count()
else:
if jsonData["name"] in self.hashedVariables:
query[jsonData["name"]] = self.hashValue(json.loads(jsonData["value"]))
return self.mongo.db.hash.find(query).count()
else:
query[jsonData["name"]] = json.loads(jsonData["value"])
return self.mongo.db.fp.find(query).count()
######Values
#Return specific values
# start/end -> With or without a specific time period
# tags -> With or without a list of specific tags
#includeNoJS -> With or without fingerprints without JS
# name -> List of attributes or a single attribute
# limit -> With or without a limit on the nb of values
def getValues(self, jsonData):
query = []
#Generation of the match query
match = []
date = {}
if "start" in jsonData:
date["$gte"] = jsonData["start"] if type(jsonData["start"]) is datetime else self.getStartDate(jsonData["start"])
if "end" in jsonData:
date["$lt"] = jsonData["end"] if type(jsonData["end"]) is datetime else self.getEndDate(jsonData["end"])
if len(date)>0:
match.append({"date": date})
if "tags" in jsonData and jsonData["tags"] not in ["all","No tags"]:
if "tagComb" in jsonData:
match.append({"tags": {"$"+jsonData["tagComb"]: jsonData["tags"]}})
else:
#We put "in" by default if it is not specified
match.append({"tags": {"$in": jsonData["tags"]}})
if "includeNoJS" in jsonData and jsonData["includeNoJS"] == "false":
match.append({"timezone": {"$exists": True}})
if len(match)>0:
query.append({"$match": {"$and": match}})
#Generation of the project query
attList = jsonData["name"]
if type(attList) is list:
att = {}
project = {}
for attribute in attList:
att[attribute] = "$"+attribute
project[attribute] = {"$ifNull": ["$"+attribute,unspecifiedValue]}
query.append({"$project": project})
else:
#attList is a single value
att = "$"+attList
query.append({"$project": {attList: {"$ifNull": ["$" + attList, unspecifiedValue]}}})
#Generation of the group query
query.append({"$group": {"_id": att, "count": {"$sum": 1}}})
#Generation of the sort query
query.append({"$sort": {"count": -1}})
#Generation of the limit query
if "limit" in jsonData:
query.append({"$limit": jsonData["limit"]})
return list(self.mongo.db.fp.aggregate(query))
######Database maintenance
#Update the tags of all fingerprints -> run it if a tag's logic
# has been updated or if a new tag has been added
def updateTags(self):
with app.app_context():
cursor = self.mongo.db.fp.find(modifiers={"$snapshot": True})
for doc in cursor:
self.mongo.db.fp.update_one({"_id": doc["_id"]}, {"$set": {"tags": tagChecker.checkFingerprint(doc)}})
db = Db()
###### API
def jsonResponse(func):
@wraps(func)
def wrapper(*args, **kwargs):
return jsonify(func(*args, **kwargs))
return wrapper
def getCount(jsonData,prefix):
result = {}
startDays = datetime.today() - timedelta(days=90)
# Adding count with tags
result[prefix+"Per"] = db.getNumberFP(jsonData)
# Adding count with tags and a 90-day limit
jsonData["start"] = startDays
result[prefix+"PerLimit"] = db.getNumberFP(jsonData)
# Adding count with a 90-day limit
del (jsonData["tags"])
result[prefix+"PerTotalLimit"] = db.getNumberFP(jsonData)
# Adding count with no tags (all database)
del (jsonData["start"])
result[prefix+"PerTotal"] = db.getNumberFP(jsonData)
return result
@app.route('/stats/number', methods=['POST'])
@jsonResponse
def getNumberFP():
return getCount(request.get_json(force=True),"number")
@app.route('/stats', methods=['POST'])
@jsonResponse
def getIndividualStats():
#Get the statistics for the value sent in POST
jsonData = request.get_json(force=True)
if "name" in jsonData and "value" in jsonData:
#Generating variables
result = getCount(deepcopy(jsonData),"")
#If one of the tag is a Tor tag, we check for an acceptable value
if "Tor 6.X" in jsonData["tags"] or "Tor Browser 7.0" in jsonData["tags"]:
result["acceptable"] = acceptableChecker.checkValue(jsonData["tags"],jsonData["name"], json.loads(jsonData["value"]))
#If value is not acceptable, we get the most popular value
#and check if any help is available for this attribute
if result["acceptable"] == "No":
#Get the most popular value
jsonData["limit"] = 1
result["popular"] = db.getValues(jsonData)
#Check for help
l = acceptableChecker.hasHelp(jsonData["name"])
if l != "":
result["link"] = l
return result
else:
return ""
@app.route('/customStats', methods=['POST'])
@jsonResponse
def getCustomStats():
jsonData = request.get_json(force=True)
if "start" in jsonData and "name" in jsonData:
nbFP = db.getNumberFP(jsonData)
#We limit the returned values
if 'p' in [el[0] for el in request.args] and request.args['p'] == config.statsPassword:
jsonData["limit"] = 200
else:
jsonData["limit"] = 10
data = db.getValues(jsonData)
# We send data for the customStats page
return {
"totalFP": nbFP,
"data": data
}
else:
return ""
if __name__ == '__main__':
if len(sys.argv)>1 and sys.argv[1] == "updateTags":
#Update the list of tags of all fingerprints
db.updateTags()
else:
#Launch application
app.run()