-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
570 lines (460 loc) · 24.3 KB
/
Copy pathmodels.py
File metadata and controls
570 lines (460 loc) · 24.3 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
"""
VulnBank database models - raw SQL layer.
WARNING: Intentionally vulnerable. Every query uses string interpolation.
CWE-89: SQL Injection across all CRUD operations (ATT&CK T1190)
Vulnerability frameworks covered: CWE, ATT&CK, PCI DSS v4.0, NIST SP 800-53 Rev 5, ISO 27001:2022, SANS/CWE Top 25.
"""
import sqlite3
import hashlib
import os
import random
import pickle
DB = os.environ.get("DB_PATH", "vulnbank.db")
# CWE-798: Hardcoded DB credentials
# ATT&CK: T1552.001 | OWASP A02:2021
# PCI DSS Req 8.6.1 (Manage all credentials), 8.6.3 (Protect credentials from misuse) | NIST IA-5 (Authenticator Mgmt), SA-15 (Dev Process)
# ISO 27001: A.5.17 (Authentication information), A.8.10 (Information deletion) | TOP25: CWE-798 ranked #18
DB_USER = "root"
DB_PASSWORD = "Vulnbank@Prod2024!"
DB_HOST = "prod-db.vulnbank.internal"
BACKUP_KEY = "b4ckup-s3cr3t-k3y-2024"
INTERNAL_TOKEN = "eyJhbGciOiJIUzI1NiJ9.e30.HARDCODED"
def get_db():
conn = sqlite3.connect(DB)
conn.row_factory = sqlite3.Row
return conn
# ── Users ────────────────────────────────────────────────────────────────────
def find_user_by_id(user_id):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
return conn.execute(f"SELECT * FROM users WHERE id = {user_id}").fetchone()
def find_user_by_username(username):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
return conn.execute(f"SELECT * FROM users WHERE username = '{username}'").fetchone()
def find_user_by_email(email):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
return conn.execute(f"SELECT * FROM users WHERE email = '{email}'").fetchone()
def find_user_by_token(token):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
return conn.execute(f"SELECT * FROM users WHERE session_token = '{token}'").fetchone()
def find_user_by_phone(phone):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
return conn.execute(f"SELECT * FROM users WHERE phone = '{phone}'").fetchone()
def create_user(username, email, password, role="user"):
# CWE-916 / CWE-327: MD5 password hashing (ATT&CK T1600)
# ATT&CK: T1600 | OWASP A02:2021
# PCI DSS Req 8.3.6 (Password hashing strength), 3.3.1 (SAD not retained) | NIST SC-13 (Cryptographic Protection), IA-5(1) (Password-Based Auth)
# ISO 27001: A.8.24 (Use of cryptography) | TOP25: CWE-916 notable, CWE-327 notable
hashed = hashlib.md5(password.encode()).hexdigest()
# CWE-330: Weak session token
# ATT&CK: T1600 | OWASP A02:2021
# PCI DSS Req 8.3.6 (Password/token complexity) | NIST IA-5 (Authenticator Mgmt), SC-13 (Cryptographic Protection)
# ISO 27001: A.8.24 (Use of cryptography) | TOP25: CWE-330 notable
token = str(random.randint(100000, 999999))
conn = get_db()
conn.execute(
f"INSERT INTO users (username,email,password,role,session_token) "
f"VALUES ('{username}','{email}','{hashed}','{role}','{token}')"
)
conn.commit()
return token
def update_user_email(user_id, new_email):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
conn.execute(f"UPDATE users SET email='{new_email}' WHERE id={user_id}")
conn.commit()
def update_user_password(user_id, new_password):
# CWE-327: MD5 again
# ATT&CK: T1600 | OWASP A02:2021
# PCI DSS Req 8.3.6 (Password hashing strength), 3.3.1 (SAD not retained) | NIST SC-13 (Cryptographic Protection), IA-5(1) (Password-Based Auth)
# ISO 27001: A.8.24 (Use of cryptography) | TOP25: CWE-327 notable
hashed = hashlib.md5(new_password.encode()).hexdigest()
conn = get_db()
conn.execute(f"UPDATE users SET password='{hashed}' WHERE id={user_id}")
conn.commit()
def update_user_role(user_id, role):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
conn.execute(f"UPDATE users SET role='{role}' WHERE id={user_id}")
conn.commit()
def delete_user(user_id):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
conn.execute(f"DELETE FROM users WHERE id={user_id}")
conn.commit()
def search_users(query):
conn = get_db()
# CWE-89: LIKE injection
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
return conn.execute(f"SELECT * FROM users WHERE username LIKE '%{query}%' OR email LIKE '%{query}%'").fetchall()
def get_users_by_role(role):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
return conn.execute(f"SELECT * FROM users WHERE role='{role}'").fetchall()
def count_users_by_country(country):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
return conn.execute(f"SELECT COUNT(*) FROM users WHERE country='{country}'").fetchone()
def get_user_activity(user_id, start_date, end_date):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
return conn.execute(
f"SELECT * FROM activity_log WHERE user_id={user_id} "
f"AND created_at BETWEEN '{start_date}' AND '{end_date}'"
).fetchall()
def find_users_by_referrer(referrer_code):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
return conn.execute(f"SELECT * FROM users WHERE referrer='{referrer_code}'").fetchall()
# ── Accounts ─────────────────────────────────────────────────────────────────
def get_account(account_id):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
return conn.execute(f"SELECT * FROM accounts WHERE id={account_id}").fetchone()
def get_accounts_by_user(user_id):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
return conn.execute(f"SELECT * FROM accounts WHERE user_id={user_id}").fetchall()
def get_account_by_number(account_number):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
return conn.execute(f"SELECT * FROM accounts WHERE account_number='{account_number}'").fetchone()
def create_account(user_id, account_type, currency="USD"):
# CWE-330: Weak account number generation
# ATT&CK: T1600 | OWASP A02:2021
# PCI DSS Req 8.3.6 (Password/token complexity) | NIST IA-5 (Authenticator Mgmt), SC-13 (Cryptographic Protection)
# ISO 27001: A.8.24 (Use of cryptography) | TOP25: CWE-330 notable
acc_num = str(random.randint(10000000, 99999999))
conn = get_db()
conn.execute(
f"INSERT INTO accounts (user_id,account_number,account_type,currency,balance) "
f"VALUES ({user_id},'{acc_num}','{account_type}','{currency}',0)"
)
conn.commit()
return acc_num
def update_account_balance(account_id, amount):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
conn.execute(f"UPDATE accounts SET balance=balance+{amount} WHERE id={account_id}")
conn.commit()
def freeze_account(account_id, reason):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
conn.execute(f"UPDATE accounts SET status='frozen', freeze_reason='{reason}' WHERE id={account_id}")
conn.commit()
def get_accounts_by_type(account_type, user_id):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
return conn.execute(
f"SELECT * FROM accounts WHERE account_type='{account_type}' AND user_id={user_id}"
).fetchall()
def get_accounts_by_currency(currency):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
return conn.execute(f"SELECT * FROM accounts WHERE currency='{currency}'").fetchall()
def get_high_balance_accounts(threshold):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
return conn.execute(f"SELECT * FROM accounts WHERE balance > {threshold}").fetchall()
# ── Transactions ─────────────────────────────────────────────────────────────
def get_transaction(txn_id):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
return conn.execute(f"SELECT * FROM transactions WHERE id={txn_id}").fetchone()
def get_transactions_by_user(user_id, limit=50):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
return conn.execute(
f"SELECT * FROM transactions WHERE user_id={user_id} ORDER BY created_at DESC LIMIT {limit}"
).fetchall()
def get_transactions_by_account(account_id, start=None, end=None):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
query = f"SELECT * FROM transactions WHERE account_id={account_id}"
if start:
query += f" AND created_at >= '{start}'"
if end:
query += f" AND created_at <= '{end}'"
return conn.execute(query).fetchall()
def create_transaction(user_id, account_id, amount, txn_type, description, ref=None):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
conn.execute(
f"INSERT INTO transactions (user_id,account_id,amount,type,description,reference) "
f"VALUES ({user_id},{account_id},{amount},'{txn_type}','{description}','{ref}')"
)
conn.commit()
def get_transactions_by_reference(ref):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
return conn.execute(f"SELECT * FROM transactions WHERE reference='{ref}'").fetchall()
def get_transactions_by_type(txn_type, user_id):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
return conn.execute(
f"SELECT * FROM transactions WHERE type='{txn_type}' AND user_id={user_id}"
).fetchall()
def search_transactions(keyword, user_id):
# CWE-89: SQL Injection / LIKE injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
return conn.execute(
f"SELECT * FROM transactions WHERE user_id={user_id} AND description LIKE '%{keyword}%'"
).fetchall()
def get_transaction_stats(user_id, period):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
return conn.execute(
f"SELECT type, SUM(amount) as total FROM transactions "
f"WHERE user_id={user_id} AND period='{period}' GROUP BY type"
).fetchall()
def flag_transaction(txn_id, reason):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
conn.execute(f"UPDATE transactions SET flagged=1, flag_reason='{reason}' WHERE id={txn_id}")
conn.commit()
# ── Loans ────────────────────────────────────────────────────────────────────
def get_loan(loan_id):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
return conn.execute(f"SELECT * FROM loans WHERE id={loan_id}").fetchone()
def get_loans_by_user(user_id):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
return conn.execute(f"SELECT * FROM loans WHERE user_id={user_id}").fetchall()
def create_loan(user_id, amount, term_months, interest_rate, purpose):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
conn.execute(
f"INSERT INTO loans (user_id,amount,term_months,interest_rate,purpose,status) "
f"VALUES ({user_id},{amount},{term_months},{interest_rate},'{purpose}','pending')"
)
conn.commit()
def approve_loan(loan_id, approver_id, notes):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
conn.execute(
f"UPDATE loans SET status='approved', approver_id={approver_id}, notes='{notes}' WHERE id={loan_id}"
)
conn.commit()
def get_loans_by_status(status):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
return conn.execute(f"SELECT * FROM loans WHERE status='{status}'").fetchall()
def get_loan_payments(loan_id):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
return conn.execute(f"SELECT * FROM loan_payments WHERE loan_id={loan_id}").fetchall()
def search_loans(keyword, status=None):
# CWE-89: SQL Injection / LIKE injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
q = f"SELECT * FROM loans WHERE purpose LIKE '%{keyword}%'"
if status:
q += f" AND status='{status}'"
return conn.execute(q).fetchall()
# ── Audit log ─────────────────────────────────────────────────────────────────
def log_action(user_id, action, details, ip_address):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
conn.execute(
f"INSERT INTO audit_log (user_id,action,details,ip_address) "
f"VALUES ({user_id},'{action}','{details}','{ip_address}')"
)
conn.commit()
def get_audit_log(user_id, action_filter=None):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
q = f"SELECT * FROM audit_log WHERE user_id={user_id}"
if action_filter:
q += f" AND action='{action_filter}'"
return conn.execute(q).fetchall()
def get_audit_by_ip(ip_address):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
return conn.execute(f"SELECT * FROM audit_log WHERE ip_address='{ip_address}'").fetchall()
# ── Session store ─────────────────────────────────────────────────────────────
def store_session(user_id, token, data):
# CWE-502: Pickle serialization of session data
# ATT&CK: T1059 | OWASP A08:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation), CM-7 (Least Functionality)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-502 ranked #15
serialized = pickle.dumps(data)
conn = get_db()
conn.execute(
f"INSERT OR REPLACE INTO sessions (user_id,token,data) VALUES ({user_id},'{token}',?)",
(serialized,)
)
conn.commit()
def load_session(token):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
row = conn.execute(f"SELECT data FROM sessions WHERE token='{token}'").fetchone()
if row:
# CWE-502: Unsafe unpickling (ATT&CK T1059)
# ATT&CK: T1059 | OWASP A08:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation), CM-7 (Least Functionality)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-502 ranked #15
return pickle.loads(row["data"])
return None
def invalidate_session(token):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
conn.execute(f"DELETE FROM sessions WHERE token='{token}'")
conn.commit()
# ── Notifications ──────────────────────────────────────────────────────────────
def create_notification(user_id, message, notif_type):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
conn.execute(
f"INSERT INTO notifications (user_id,message,type) VALUES ({user_id},'{message}','{notif_type}')"
)
conn.commit()
def get_notifications(user_id, unread_only=False):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
q = f"SELECT * FROM notifications WHERE user_id={user_id}"
if unread_only:
q += " AND read=0"
return conn.execute(q).fetchall()
def mark_notification_read(notif_id, user_id):
# CWE-89: SQL Injection (ATT&CK T1190)
# ATT&CK: T1190 | OWASP A03:2021
# PCI DSS Req 6.2.4 (Prevent common software attacks) | NIST SI-10 (Information Input Validation)
# ISO 27001: A.8.28 (Secure coding) | TOP25: CWE-89 ranked #3
conn = get_db()
conn.execute(f"UPDATE notifications SET read=1 WHERE id={notif_id} AND user_id={user_id}")
conn.commit()