forked from arpitguptagithub/hackbellBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserSafety.py
112 lines (86 loc) · 4.06 KB
/
UserSafety.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
# 1. USER SAFETY: It will be checking if you have accessed any malicious site over the past 24 hour, by scanning through your web browser information locally. This process is taking place locally and once list is created it passes to another class. We use two things first our list of inbuilt dataset of safe and unsafe sites, next is scanning the unidentified sites and flagging them. Now for the scanning part we will be checking multiple factors and will be using somewhat binary classification of ML, i.e. we will be checking if the site follows a "quick change algorithm" for dynamic changing sites, suspicious regex, and other things similar to it.
import os
import sqlite3
import random
from datetime import datetime, timedelta
from colorama import Style, Fore
print()
time_interval = int(input(" Time interval for history capture(int-> in hrs): "))
# Adding safe and unsafe sites
safeSites = []
unsafeSites = []
# Dynamic changing sites and suspicious regex
quickChangeAlgorithm = [0.5 , 0.7]
def evaluate_site(url):
if ".onion" in url:
unsafeSites.append(url)
print(f"{Style.BRIGHT}{Fore.RED}Unsafe site{Style.RESET_ALL}: {url}")
else:
# Add additional logic to evaluate the site and determine if it is safe or unsafe
# You can use quickChangeAlgorithm or suspiciousRegex lists for more evaluation criteria
# For now, let's assume it's unsafe if not in safeSites
reward = simulate_reward(url)
if quickChangeAlgorithm and len(quickChangeAlgorithm) >= 2:
if reward > quickChangeAlgorithm[0]:
safeSites.append(url)
print(f"{Style.BRIGHT}{Fore.GREEN}Safe site{Fore.RESET}{Style.RESET_ALL}: {url}")
elif reward < quickChangeAlgorithm[1]:
unsafeSites.append(url)
print(f"{Style.BRIGHT}{Fore.RED}Unsafe site{Style.RESET_ALL}: {url}")
else:
# Continue monitoring or take default action
print(f"Continue monitoring: {url}")
else:
# Handle the case where quickChangeAlgorithm is not properly defined
print("Quickest Change Algorithm not properly defined")
def simulate_reward(url):
# Replace this
keyword_weightage = {
"important": 0.9,
"security": 0.85,
"entertainment": 0.7,
"youtube": 0.9,
"google": 0.9,
"github": 0.9,
"shopping": 0.8,
"education": 0.75
}
for keyword, weightage in keyword_weightage.items():
if keyword in url:
# If the keyword is present in the URL, return the associated weightage as reward
return random.uniform(weightage, 1.0)
# If none of the keywords are present, return a default low to medium reward
return random.uniform(0.0, 0.7)
# Code to scan through the web browser information
data_path = os.path.expanduser("~/.mozilla/firefox/")
# Iterate through file
for subdirectory in os.listdir(data_path) :
subdirectory_path = os.path.join(data_path, subdirectory)
if os.path.isdir(subdirectory_path) and 'default-' in subdirectory and subdirectory.index('default-') > 0 :
data_path = subdirectory_path
break
history_db = os.path.join(data_path, 'places.sqlite')
c = sqlite3.connect(history_db)
cursor = c.cursor()
# Calculate the timestamp for 24 hours ago
twenty_four_hours_ago = datetime.utcnow() - timedelta(hours=time_interval)
timestamp_24_hours_ago = int(twenty_four_hours_ago.timestamp() * 1e6)
select_statement = "SELECT url, visit_count FROM moz_places WHERE last_visit_date >= ?;"
cursor.execute(select_statement, (timestamp_24_hours_ago,))
results = cursor.fetchall()
# Check if the site is safe or unsafe
for url, count in results:
if url in safeSites:
continue
elif url in unsafeSites:
print(f"{Style.BRIGHT}{Fore.RED}Unsafe site{Style.RESET_ALL}: {url}")
else:
evaluate_site(url)
# Close the cursor and connection
cursor.close()
c.close()
# # code to delete the list and history accessed
# safeSites.clear()
# unsafeSites.clear()
# os.remove(history_db)
# # End of snippet