-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
392 lines (330 loc) · 14.9 KB
/
app.py
File metadata and controls
392 lines (330 loc) · 14.9 KB
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
from asyncio import sleep
import asyncio
from flask import Flask, Response, request, render_template
from pynput import mouse
from pynput.mouse import Controller
from threading import Thread
import matplotlib.pyplot as plt
import matplotlib
from pymongo import MongoClient
from dotenv import dotenv_values
from flask_pymongo import PyMongo
import joblib
import sys
import os
from utils import blackListUA, processUA
from utils import readFromDB
from utils.prepareInput import prepare_keystroke_input, prepare_mouse_analysis_input
import pandas as pd
import numpy as np
import torch
from PIL import Image
from torchvision import models
from torchvision import transforms
import torch.nn as nn
utils_path = '/home/dishamodi0910/DEV/true-identify/utils'
sys.path.append(os.path.dirname(utils_path))
config = dotenv_values(".env")
keystroke_model = joblib.load("model/best_keystroke_model.joblib");
def findOutNumberOfSpacesInString(text):
spaceCount = 0;
for i in range(len(text)):
if(text[i] == ' '):
spaceCount = spaceCount + 1
return spaceCount
#DEAL WITH MOUSE RELATED DATA
X = []
Y = []
def generate_separate_lists(mouse_readings):
list1, list2 = zip(*mouse_readings)
global X,Y
#print(len(list1))
#print(len(list2))
X = list(list1)
Y = list(list2)
def generate_graph(mouse_readings):
generate_separate_lists(mouse_readings)
#print(len(X))
#print(len(Y))
matplotlib.use('Agg')
plt.plot(X, Y, marker='o', linestyle='-', color='b')
plt.xlabel('X-Axis (X)')
plt.ylabel('Y-Axis (Y)')
plt.title('Plot of Coordinates')
plt.savefig('fig.png')
#DEAL WITH kEYSTROKE RELATED DATA
typingSpeed = 0
avgKeyStrokeLatency = 0
avgDiGraphDuration = 0
avgHoldTime = 0
avgInterReleaseLatency = 0
maxKeyStrokeLatency = 0
maxDiGraphDuration = 0
maxHoldTime = 0
maxInterReleaseLatency = 0
minKeyStrokeLatency = 0
minDiGraphDuration = 0
minHoldTime = 0
minInterReleaseLatency = 0
generate_keywise_times = []
def calculate_keystroke(events):
global generate_keywise_times
print("Generate keywise times : ", generate_keywise_times)
for i in range(len(events)):
event = events[i]
if(event['type'] == 'Keydown'):
for j in range(i+1, len(events)):
if events[j]['key'] == event['key'] and events[j]['type'] == "Keyup" and events[j]['time'] > event['time']:
key = event['key']
key_press_time = event['time']
key_release_time = events[j]['time']
generate_keywise_times.append({"Key" : key, "keyPresssTime" : key_press_time, "keyReleaseTime" : key_release_time})
break
print(f"The data generated : {generate_keywise_times}")
holdTimeAll = []
keyStrokeLatencyAll = []
digraphDurationAll = []
interReleaseLatencyAll = []
def generate_dynamics():
global generate_keywise_times
for i in range(len(generate_keywise_times)):
characterWiseTime = generate_keywise_times[i]
holdtime = characterWiseTime['keyReleaseTime'] - characterWiseTime['keyPresssTime']
holdTimeAll.append(holdtime);
if(i > 0):
previous_char = generate_keywise_times[i-1]
keystrokelatency = characterWiseTime['keyPresssTime'] - previous_char['keyPresssTime']
keyStrokeLatencyAll.append(keystrokelatency)
digraphduration = characterWiseTime['keyPresssTime'] - previous_char['keyReleaseTime']
digraphDurationAll.append(digraphduration)
intereleaselatency = characterWiseTime['keyReleaseTime'] - previous_char['keyReleaseTime']
interReleaseLatencyAll.append(intereleaselatency)
global holdTime, avgHoldTime, minHoldTime, maxHoldTime
global keyStrokeLatency, avgKeyStrokeLatency, minKeyStrokeLatency, maxKeyStrokeLatency
global digraphDuration, avgDiGraphDuration, minDiGraphDuration, maxDiGraphDuration
global interReleaseLatency, avgInterReleaseLatency, minInterReleaseLatency, maxInterReleaseLatency
if(holdTimeAll):
avgHoldTime = sum(holdTimeAll)/len(holdTimeAll)
minHoldTime = min(holdTimeAll)
maxHoldTime = max(holdTimeAll)
if(keyStrokeLatencyAll):
avgKeyStrokeLatency = sum(keyStrokeLatencyAll)/len(keyStrokeLatencyAll)
minKeyStrokeLatency = min(keyStrokeLatencyAll)
maxKeyStrokeLatency = max(keyStrokeLatencyAll)
if(digraphDurationAll):
avgDiGraphDuration = sum(digraphDurationAll)/len(digraphDurationAll)
minDiGraphDuration = min(digraphDurationAll)
maxDiGraphDuration = max(digraphDurationAll)
if(interReleaseLatencyAll):
avgInterReleaseLatency = sum(interReleaseLatencyAll)/len(interReleaseLatencyAll)
minInterReleaseLatency = min(interReleaseLatencyAll)
maxInterReleaseLatency = max(interReleaseLatencyAll)
def calculateTypingSpeed(username, password):
global generate_keywise_times
global typingSpeed
totalCharsTyped = 0
#Typing Speed will be total time taken/Total characters written
# spacesUsername = findOutNumberOfSpacesInString(username)
# spacesPassword = findOutNumberOfSpacesInString(password)
# if(spacesUsername > 0):
# wordsInUserName = len(username.split(' '))
# totalWordsTyped = totalWordsTyped + wordsInUserName + spacesUsername
# else:
# totalWordsTyped = totalWordsTyped + 1
# if(spacesPassword > 0):
# wordsInPassword = len(password.split(' '))
# totalWordsTyped = totalWordsTyped + wordsInPassword + spacesPassword
# else:
# totalWordsTyped = totalWordsTyped + 1
totalCharsTyped = len(username) + len(password);
totalTimeTaken = (generate_keywise_times[len(generate_keywise_times) - 1]['keyReleaseTime'] - generate_keywise_times[0]['keyPresssTime'])*1.0;
typingSpeed = (totalTimeTaken)/totalCharsTyped;
return typingSpeed
stop_tracking = False
mouse_readings = []
def track_behaviour(mouse):
global stop_tracking, mouse_readings
while not stop_tracking:
mouse_readings.append(mouse.position)
def create_app(test_config=None):
app = Flask(__name__)
app.config["MONGO_URI"] = config.get("MONGO_URI")
print("Mongo URI is : ", config.get("MONGO_URI"))
mongo = PyMongo(app)
@app.route('/hello')
def hello():
return render_template('hello.html')
@app.route('/login', methods=['GET'])
def process_request():
print("Method type : ",request.method);
print("Referrer String : ",request.referrer);
print("Headers info" ,dict(request.headers.items()));
print("User Agent String",request.headers.get('User-Agent'))
print("Remote Address",request.remote_addr);
userAgentString = str(request.headers.get('User-Agent'))
print(userAgentString)
userAgentProcessed = processUA.processUserAgentString(userAgentString)
dbCollectionValues = readFromDB.readFromDB()
UAIdentifier = userAgentProcessed.get("parsedLegacyToken")
maliciousReferrerList = dbCollectionValues.get("badReferrer")
isMaliciousReferrer = request.referrer in maliciousReferrerList
maliciousIPList = dbCollectionValues.get("badIP")
isMaliciousIP = request.remote_addr in maliciousIPList
maliciousUAList = dbCollectionValues.get("badUA")
isMaliciousUA = UAIdentifier in maliciousUAList
print("Is Malicious IP : ", isMaliciousIP)
print("Is Malicious Referrer : ", isMaliciousReferrer)
print("Is Malicious UA : ", isMaliciousUA)
requestedPath = request.path
print("Requested Path is : ", requestedPath)
if(requestedPath.endswith("robots.txt")):
blackListUA.blackListUA(UAIdentifier)
mouse = Controller()
tracking_thread = Thread(target=track_behaviour, args=(mouse,))
tracking_thread.daemon = True
tracking_thread.start()
return render_template('login.html');
@app.route('/home', methods=['POST'])
def form_submitted():
stop_tracking = True
data = request.get_json()
username = data['username'];
password = data['password'];
typingData = data['typingData'];
keypressData = data['keypressData'];
backSpaceCount = data['backSpaceCount'];
hiddenFieldUsed = data['hiddenFieldUsed'];
# innerHeight = data['innerHeight']
# outerHeight = data['outerHeight']
# innerWidth = data['innerWidth']
# outerWidth = data['outerWidth']
print(f'MouseReadingsList Size: {len(mouse_readings)}');
generate_graph(mouse_readings)
print(f'Username : {username}');
print(f'Password : {password}');
#print(f'typingData : {typingData}');
print(f'keypressData : {keypressData}');
generate_keywise_times = calculate_keystroke(keypressData);
generate_dynamics()
print(f"Hold time : {holdTimeAll}")
print(f"Average Hold time : {avgHoldTime}")
print(f"Max Hold time : {maxHoldTime}")
print(f"Min Hold time : {minHoldTime}")
print(f"KeyStroke Latencies : {keyStrokeLatencyAll}")
print(f"Average Keystroke Latency: {avgKeyStrokeLatency} ms")
print(f"Max Keystroke Latency: {maxKeyStrokeLatency} ms")
print(f"Min Keystroke Latency: {minKeyStrokeLatency} ms")
print(f"Digraph Duration{digraphDurationAll}")
print(f"Average Digraph Duration: {avgDiGraphDuration} ms")
print(f"Max Digraph Duration: {maxDiGraphDuration} ms")
print(f"Min Digraph Duration: {minDiGraphDuration} ms")
print(f"InterRelease Latency : {interReleaseLatencyAll}")
print(f"Average Inter-Release Latency: {avgInterReleaseLatency} ms")
print(f"Max Inter-Release Latency: {maxInterReleaseLatency} ms")
print(f"Min Inter-Release Latency: {minInterReleaseLatency} ms")
print(f'backSpaceCount : {backSpaceCount}');
print(f'hiddenFieldUsed : {hiddenFieldUsed}');
calculateTypingSpeed(username, password)
print(f"Typing Speed is : {typingSpeed}");
# print(f"OuterWidth : {outerWidth}")
# print(f"InnerWidth : {innerWidth}")
# print(f"OuterHeight : {outerHeight}")
# print(f"InnerHeight : {innerHeight}")
# keystroke_data = {
# "username": username,
# "password": password,
# "keypress_data": keypressData,
# "Typing Speed" : typingSpeed,
# "No of Backspaces" : backSpaceCount,
# "typing_metrics": {
# "hold_time": {
# "all": holdTimeAll,
# "average": avgHoldTime,
# "min": minHoldTime,
# "max": maxHoldTime,
# },
# "keystroke_latency": {
# "all": keyStrokeLatencyAll,
# "average": avgKeyStrokeLatency,
# "min": minKeyStrokeLatency,
# "max": maxKeyStrokeLatency,
# },
# "digraph_duration": {
# "all": digraphDurationAll,
# "average": avgDiGraphDuration,
# "min": minDiGraphDuration,
# "max": maxDiGraphDuration,
# },
# "inter_release_latency": {
# "all": interReleaseLatencyAll,
# "average": avgInterReleaseLatency,
# "min": minInterReleaseLatency,
# "max": maxInterReleaseLatency,
# },
# },
# "hidden_field_used": hiddenFieldUsed,
# "inner_dimensions": {"width": innerWidth, "height": innerHeight},
# "outer_dimensions": {"width": outerWidth, "height": outerHeight},
# }
keystroke_data = np.array([typingSpeed, backSpaceCount, avgHoldTime, maxHoldTime, minHoldTime, avgKeyStrokeLatency, maxKeyStrokeLatency, minKeyStrokeLatency, avgDiGraphDuration, maxDiGraphDuration, minDiGraphDuration, avgInterReleaseLatency, maxInterReleaseLatency, minInterReleaseLatency])
# dimensions_honeypot_info = {
# "innerWidth" : innerWidth,
# "innerHeight" : innerHeight,
# "outerWidth" : outerWidth,
# "outerHeight" : outerHeight,
# "hidden_field_used" : hiddenFieldUsed,
# "output_label" : 0
# }
# try:
# mongo.db.keystrokedata.insert_one(keystroke_data)
# mongo.db.keystrokes_bot.insert_one(keystrokes_data_small)
# mongo.db.dimensions_bot.insert_one(dimensions_honeypot_info)
# print("Keystroke data saved to MongoDB.")
# except Exception as e:
# print(f"Error inserting into MongoDB: {e}")
# return "Error saving to db"
request_response = -1
mouse_analysis_response = -1
keystroke_analysis_response = -1
input_keystrokes_formatted = prepare_keystroke_input(keystroke_data)
print("Input formatted strokes : ", input_keystrokes_formatted)
input_keystrokes_formatted = input_keystrokes_formatted.reshape(1,-1);
print("Input keystroke reshaped : ", input_keystrokes_formatted);
predicted_keystroke_result = keystroke_model.predict(input_keystrokes_formatted)
# print("Predicted Keystroke Result : ", predicted_keystroke_result)
mouse_prediction_result = -1
img_path = "fig.png"
image = Image.open(img_path)
image = image.convert("RGB")
model = models.efficientnet_b0(weights=models.EfficientNet_B0_Weights.DEFAULT)
num_features = model.classifier[1].in_features
model.classifier[1] = nn.Linear(num_features, 2)
model.load_state_dict(torch.load('model\mouse_trajectory_model.pth', map_location=torch.device('cpu')))
model.eval()
input_image = prepare_mouse_analysis_input(image)
with torch.no_grad():
output = model(input_image)
_, predicted = torch.max(output, 1)
predicted = predicted.numpy()
mouse_prediction_result = predicted[0]
# print("Mouse Prediction Result : ", mouse_prediction_result)
result = {
'mouse_prediction_result' : "Bot" if mouse_prediction_result == 1 else "Human",
'keystroke_prediction_result' : "Bot" if predicted_keystroke_result == 0 else "Human"
}
if (hiddenFieldUsed || isMaliciousIP || isMaliciousReferrer || isMaliciousUA)
{
request_response = 0
mouse_analysis_response = 0
keystroke_analysis_response = 0
}
else
{
request_response = 0.5
mouse_analysis_response = 1 - mouse_prediction_result
keystroke_analysis_response = keystroke_analysis_response
}
final_conclusion = ( request_response + mouse_analysis_response + keystroke_analysis_response ) / 3.0;
print(result)
return render_template('result.html', result=result)
return app