-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
201 lines (153 loc) · 6.49 KB
/
Copy pathdatabase.py
File metadata and controls
201 lines (153 loc) · 6.49 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
"""
SQLite database layer for LinkGuard.
Stored at %%APPDATA%%/LinkGuard/linkguard.db
"""
import sqlite3
import os
import logging
from datetime import datetime
from pathlib import Path
APP_DIR = Path(os.environ.get("APPDATA", os.path.expanduser("~"))) / "LinkGuard"
APP_DIR.mkdir(parents=True, exist_ok=True)
DB_PATH = APP_DIR / "linkguard.db"
# Migrate data from old PhishUrl directory if needed
_old_dir = Path(os.environ.get("APPDATA", os.path.expanduser("~"))) / "PhishUrl"
if not DB_PATH.exists() and (_old_dir / "phishurl.db").exists():
import shutil
shutil.copy2(_old_dir / "phishurl.db", DB_PATH)
SCHEMA = """
CREATE TABLE IF NOT EXISTS whitelist (
domain TEXT PRIMARY KEY,
added_at TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS blacklist (
domain TEXT PRIMARY KEY,
added_at TEXT NOT NULL,
reason TEXT
);
CREATE TABLE IF NOT EXISTS history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
url TEXT NOT NULL,
domain TEXT NOT NULL,
risk_score INTEGER NOT NULL,
risk_level TEXT NOT NULL,
reasons TEXT,
action TEXT,
checked_at TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS settings (
key TEXT PRIMARY KEY,
value TEXT NOT NULL
);
"""
DEFAULTS = {
"clipboard_monitoring": "true",
"protocol_handler": "false",
"startup": "true",
"risk_threshold": "suspicious", # clean | suspicious | phishing
"google_sbrowsing_key": "",
"virustotal_key": "",
"notify_clean": "false",
"real_browser_cmd": "", # stored when protocol handler is enabled
}
def _connect() -> sqlite3.Connection:
conn = sqlite3.connect(str(DB_PATH))
conn.row_factory = sqlite3.Row
conn.execute("PRAGMA journal_mode=WAL")
return conn
def init_db():
with _connect() as conn:
conn.executescript(SCHEMA)
for k, v in DEFAULTS.items():
conn.execute(
"INSERT OR IGNORE INTO settings (key, value) VALUES (?, ?)", (k, v)
)
# ── Settings ──────────────────────────────────────────────────────────────────
def get_setting(key: str) -> str:
with _connect() as conn:
row = conn.execute("SELECT value FROM settings WHERE key=?", (key,)).fetchone()
return row["value"] if row else DEFAULTS.get(key, "")
def set_setting(key: str, value: str):
with _connect() as conn:
conn.execute(
"INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)", (key, value)
)
def get_all_settings() -> dict:
with _connect() as conn:
rows = conn.execute("SELECT key, value FROM settings").fetchall()
return {r["key"]: r["value"] for r in rows}
# ── Whitelist ─────────────────────────────────────────────────────────────────
def is_whitelisted(domain: str) -> bool:
domain = _normalize_domain(domain)
with _connect() as conn:
row = conn.execute(
"SELECT 1 FROM whitelist WHERE domain=?", (domain,)
).fetchone()
return row is not None
def add_whitelist(domain: str):
domain = _normalize_domain(domain)
with _connect() as conn:
conn.execute(
"INSERT OR REPLACE INTO whitelist (domain, added_at) VALUES (?, ?)",
(domain, _now()),
)
def remove_whitelist(domain: str):
domain = _normalize_domain(domain)
with _connect() as conn:
conn.execute("DELETE FROM whitelist WHERE domain=?", (domain,))
def get_whitelist() -> list[dict]:
with _connect() as conn:
rows = conn.execute(
"SELECT domain, added_at FROM whitelist ORDER BY domain"
).fetchall()
return [dict(r) for r in rows]
# ── Blacklist ─────────────────────────────────────────────────────────────────
def is_blacklisted(domain: str) -> bool:
domain = _normalize_domain(domain)
with _connect() as conn:
row = conn.execute(
"SELECT 1 FROM blacklist WHERE domain=?", (domain,)
).fetchone()
return row is not None
def add_blacklist(domain: str, reason: str = ""):
domain = _normalize_domain(domain)
with _connect() as conn:
conn.execute(
"INSERT OR REPLACE INTO blacklist (domain, added_at, reason) VALUES (?, ?, ?)",
(domain, _now(), reason),
)
def remove_blacklist(domain: str):
domain = _normalize_domain(domain)
with _connect() as conn:
conn.execute("DELETE FROM blacklist WHERE domain=?", (domain,))
def get_blacklist() -> list[dict]:
with _connect() as conn:
rows = conn.execute(
"SELECT domain, added_at, reason FROM blacklist ORDER BY domain"
).fetchall()
return [dict(r) for r in rows]
# ── History ───────────────────────────────────────────────────────────────────
def add_history(url: str, domain: str, risk_score: int, risk_level: str,
reasons: list[str], action: str):
with _connect() as conn:
conn.execute(
"""INSERT INTO history (url, domain, risk_score, risk_level, reasons, action, checked_at)
VALUES (?, ?, ?, ?, ?, ?, ?)""",
(url, domain, risk_score, risk_level, "; ".join(reasons), action, _now()),
)
def get_history(limit: int = 200) -> list[dict]:
with _connect() as conn:
rows = conn.execute(
"""SELECT id, url, domain, risk_score, risk_level, reasons, action, checked_at
FROM history ORDER BY checked_at DESC LIMIT ?""",
(limit,),
).fetchall()
return [dict(r) for r in rows]
def clear_history():
with _connect() as conn:
conn.execute("DELETE FROM history")
# ── Helpers ───────────────────────────────────────────────────────────────────
def _normalize_domain(domain: str) -> str:
return domain.strip().lower().lstrip("www.")
def _now() -> str:
return datetime.now().isoformat(sep=" ", timespec="seconds")