forked from mtusi2026/mtusi2026lab7
Implement lab7 security and compromised worker recovery evidence
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
import base64
|
||||
import concurrent.futures
|
||||
import json
|
||||
import math
|
||||
import statistics
|
||||
import sys
|
||||
import time
|
||||
from collections import Counter
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
|
||||
from argon2 import PasswordHasher
|
||||
from client import Client, check
|
||||
from provision import compose
|
||||
from bootstrap import ROOT, write
|
||||
from security_lifecycle import wait, ready
|
||||
|
||||
|
||||
def sql(statement):
|
||||
return compose(
|
||||
"exec",
|
||||
"-T",
|
||||
"db",
|
||||
"psql",
|
||||
"-U",
|
||||
"operator",
|
||||
"-d",
|
||||
"messenger",
|
||||
"-At",
|
||||
"-v",
|
||||
"ON_ERROR_STOP=1",
|
||||
stdin=statement,
|
||||
).strip()
|
||||
|
||||
|
||||
def main():
|
||||
client = Client(base_url="https://127.0.0.1:8443", ca_file="secrets/public/ca.crt")
|
||||
wait(lambda: ready(client), 120)
|
||||
actor = client.user("passwordproof")
|
||||
basic = base64.b64encode(
|
||||
(actor["registration"]["username"] + ":" + actor["registration"]["password"]).encode()
|
||||
).decode()
|
||||
old = PasswordHasher(
|
||||
time_cost=2, memory_cost=16384, parallelism=1, hash_len=32, salt_len=16
|
||||
).hash(actor["registration"]["password"])
|
||||
sql("UPDATE users SET password_hash='" + old + "' WHERE id='" + actor["id"] + "';")
|
||||
latency = []
|
||||
replicas = set()
|
||||
for _ in range(12):
|
||||
start = time.monotonic()
|
||||
response = client.call(
|
||||
"POST",
|
||||
"/api/v1/auth/sessions",
|
||||
201,
|
||||
headers={"Authorization": "Basic " + basic},
|
||||
schema="Session",
|
||||
)
|
||||
latency.append(1000 * (time.monotonic() - start))
|
||||
replicas.add(response["headers"]["X-Instance-Id"])
|
||||
parameters = sql(
|
||||
"SELECT split_part(password_hash,'$',4) FROM users WHERE id='" + actor["id"] + "';"
|
||||
)
|
||||
check(parameters == "m=19456,t=2,p=1", "Successful login did not upgrade hash")
|
||||
sql(
|
||||
"UPDATE sessions SET expires_at=created_at+interval '1 microsecond' WHERE user_id='"
|
||||
+ actor["id"]
|
||||
+ "';"
|
||||
)
|
||||
client.call("GET", "/api/v1/users/me", 401, actor=actor)
|
||||
remaining = 60 - time.time() % 60
|
||||
if remaining < 10:
|
||||
time.sleep(remaining + 0.1)
|
||||
sql("DELETE FROM login_budget;")
|
||||
bad = base64.b64encode(
|
||||
(actor["registration"]["username"] + ":invalid-password").encode()
|
||||
).decode()
|
||||
|
||||
def request(_):
|
||||
local = Client(base_url="https://127.0.0.1:8443", ca_file="secrets/public/ca.crt")
|
||||
r = local.call(
|
||||
"POST", "/api/v1/auth/sessions", (401, 429), headers={"Authorization": "Basic " + bad}
|
||||
)
|
||||
if r["status"] == 429:
|
||||
check(int(r["headers"]["Retry-After"]) >= 1, "Retry-After missing")
|
||||
return r["status"], r["headers"]["X-Instance-Id"]
|
||||
|
||||
start = time.monotonic()
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=8) as pool:
|
||||
values = list(pool.map(request, range(72)))
|
||||
check({r for _, r in values} == replicas and len(replicas) == 2, "Both replicas not exercised")
|
||||
counts = Counter(code for code, _ in values)
|
||||
check(counts[429] >= 12 and counts[401] > 0, "Shared budget did not bound attempts")
|
||||
check(sql("SELECT max(attempts) FROM login_budget;") == "61", "Shared counter did not saturate")
|
||||
result = dict(
|
||||
status="PASS",
|
||||
argon2id=dict(
|
||||
memory_kib=19456,
|
||||
iterations=2,
|
||||
parallelism=1,
|
||||
salt_bytes=16,
|
||||
hash_bytes=32,
|
||||
rehash_on_success=True,
|
||||
),
|
||||
login_measurement=dict(
|
||||
samples=12,
|
||||
end_to_end_ms=dict(
|
||||
p50=round(statistics.median(latency), 3),
|
||||
p95=round(sorted(latency)[math.ceil(0.95 * len(latency)) - 1], 3),
|
||||
),
|
||||
api_cpu_limit=1,
|
||||
api_memory_mib=512,
|
||||
),
|
||||
dos_budget=dict(
|
||||
global_per_minute=60,
|
||||
per_replica_hash_slots=2,
|
||||
replicas=2,
|
||||
max_simultaneous_kdf_memory_mib=76,
|
||||
attempts=72,
|
||||
concurrency=8,
|
||||
duration_seconds=round(time.monotonic() - start, 3),
|
||||
statuses=dict(counts),
|
||||
shared_saturating_counter=61,
|
||||
retry_after=True,
|
||||
),
|
||||
expiry_rejected=True,
|
||||
limitations="fixed global minute window; at a window boundary up to 120 admissions; global protection may deny legitimate users during abuse; latency includes HTTP/TLS/DB",
|
||||
)
|
||||
write(ROOT / "evidence/security-auth.json", json.dumps(result, indent=2) + "\n")
|
||||
sql("DELETE FROM login_budget;")
|
||||
print(json.dumps(result, indent=2))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user