237 lines
11 KiB
Python
237 lines
11 KiB
Python
|
|
"""Gateway access-log rules: classification, FP suppression, rate, caps.
|
||
|
|
|
||
|
|
Fixtures are real lines from the live stream (2026-11-27), including the four
|
||
|
|
Forgejo shapes that a substring rule gets wrong: `.git/info/refs`, a browse URL
|
||
|
|
ending in `.gitignore`, one ending in `.env.example` behind an RSS route, and a
|
||
|
|
`*-credentials.yaml` file view.
|
||
|
|
"""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import unittest
|
||
|
|
|
||
|
|
from netsec_scan import config, http_rules
|
||
|
|
|
||
|
|
|
||
|
|
def rec(path, code=404, host="siriusdevops.com", client="203.0.113.7",
|
||
|
|
ua="Mozilla/5.0", method="GET", peer="10.10.0.1", client_ip=None):
|
||
|
|
return {
|
||
|
|
"path": path, "response_code": code, "authority": host, "method": method,
|
||
|
|
"user_agent": ua, "peer": peer,
|
||
|
|
"client_ip": client if client_ip is None else client_ip,
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
class TestClassification(unittest.TestCase):
|
||
|
|
def test_secret_probe_from_calm_day(self):
|
||
|
|
# measured 2026-11-27: the 91.92.47.0/24 scanner, all 404
|
||
|
|
for path in ("/.env", "/backup/.env", "/api/app/.env", "/.aws/credentials",
|
||
|
|
"/bedrock-credentials.json", "/service-account.json",
|
||
|
|
"/.git/credentials", "/latest/meta-data/iam/security-credentials"):
|
||
|
|
cat, sev, rule = http_rules.classify(rec(path))
|
||
|
|
self.assertIn(cat, ("secret-file", "vcs-probe"), path)
|
||
|
|
self.assertEqual(sev, "MEDIUM", path)
|
||
|
|
|
||
|
|
def test_served_secret_is_high(self):
|
||
|
|
cat, sev, rule = http_rules.classify(rec("/.env", code=200))
|
||
|
|
self.assertEqual((cat, sev, rule), ("secret-file", "HIGH", "http-secret-probe"))
|
||
|
|
|
||
|
|
def test_traversal_in_path_is_high_only_when_served(self):
|
||
|
|
cat, sev, _ = http_rules.classify(rec("/../../etc/passwd", code=200))
|
||
|
|
self.assertEqual((cat, sev), ("traversal", "HIGH"))
|
||
|
|
cat, sev, _ = http_rules.classify(rec("/%2e%2e%2f%2e%2e%2fetc%2fpasswd", code=404))
|
||
|
|
self.assertEqual((cat, sev), ("traversal", "MEDIUM"))
|
||
|
|
|
||
|
|
def test_traversal_in_query_string_is_not_served_high(self):
|
||
|
|
"""`/?file=../../.env` on a static site answers 200 regardless."""
|
||
|
|
cat, sev, _ = http_rules.classify(rec("/?file=../../../etc/passwd", code=200))
|
||
|
|
self.assertEqual((cat, sev), ("traversal", "MEDIUM"))
|
||
|
|
|
||
|
|
def test_sqli(self):
|
||
|
|
for path in ("/index.php?q=union+select+1,2",
|
||
|
|
"/?id=1%27+or+1%3D1--",
|
||
|
|
"/item?id=1 union all select user,pass from users",
|
||
|
|
"/x.php?id=1;sleep(5)"):
|
||
|
|
cat, sev, rule = http_rules.classify(rec(path))
|
||
|
|
self.assertEqual(cat, "sqli", path)
|
||
|
|
self.assertEqual(rule, "http-sqli")
|
||
|
|
|
||
|
|
def test_nul_byte(self):
|
||
|
|
cat, sev, _ = http_rules.classify(rec("/.env%00.txt"))
|
||
|
|
self.assertEqual(cat, "nul-byte")
|
||
|
|
self.assertEqual(sev, "MEDIUM") # 400/404: attempt, not a read
|
||
|
|
|
||
|
|
def test_backup_extensions(self):
|
||
|
|
for path in ("/backup.zip", "/db.sql", "/site.tar.gz", "/config.php.bak",
|
||
|
|
"/credentials.old"):
|
||
|
|
cat, sev, rule = http_rules.classify(rec(path))
|
||
|
|
self.assertEqual(rule, "http-backup-probe", path)
|
||
|
|
self.assertEqual(sev, "LOW", path)
|
||
|
|
|
||
|
|
def test_scanner_user_agents(self):
|
||
|
|
for ua, sev in (("sqlmap/1.8#stable (https://sqlmap.org)", "HIGH"),
|
||
|
|
("Mozilla/5.00 (Nikto/2.5.0)", "HIGH"),
|
||
|
|
("zgrab/0.x", "LOW")):
|
||
|
|
cat, severity, rule = http_rules.classify(rec("/", code=200, ua=ua))
|
||
|
|
self.assertEqual(rule, "http-scanner-ua", ua)
|
||
|
|
self.assertEqual(severity, sev, ua)
|
||
|
|
self.assertTrue(cat.startswith("scanner-ua:"))
|
||
|
|
|
||
|
|
def test_ordinary_traffic_is_none(self):
|
||
|
|
for path, ua in (
|
||
|
|
("/", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/153.0"),
|
||
|
|
("/user/login", "Mozilla/5.0 (compatible; GPTBot/1.4; +https://openai.com/gptbot)"),
|
||
|
|
("/api/v1/repos/sirius/homek8s", "git/2.56.0"),
|
||
|
|
("/index.html", "connect-go/1.17.0 (go1.23.3)")):
|
||
|
|
self.assertIsNone(http_rules.classify(rec(path, code=200, ua=ua)), path)
|
||
|
|
|
||
|
|
|
||
|
|
class TestForgejoFalsePositives(unittest.TestCase):
|
||
|
|
"""The shapes that made a naive rule unusable on this stream."""
|
||
|
|
|
||
|
|
def assertNone(self, path, code=303):
|
||
|
|
self.assertIsNone(
|
||
|
|
http_rules.classify(rec(path, code=code, host="forgejo.siriusdevops.com",
|
||
|
|
client="137.118.156.124", ua="git/2.56.0")),
|
||
|
|
path)
|
||
|
|
|
||
|
|
def test_smart_http_git_endpoints(self):
|
||
|
|
for path in ("/sirius/homek8s.git/info/refs?service=git-upload-pack",
|
||
|
|
"/sirius/netsec-agent.git/info/refs?service=git-receive-pack",
|
||
|
|
"/sirius/homek8s.git/git-upload-pack",
|
||
|
|
"/sirius/homek8s.git/git-receive-pack"):
|
||
|
|
self.assertNone(path, code=200)
|
||
|
|
|
||
|
|
def test_browse_urls_carry_dangerous_filenames(self):
|
||
|
|
for path in (
|
||
|
|
"/sirius/onionwire/raw/commit/adcf52b8e48d9e0240896ef4289e540d40e98f8e/.gitignore",
|
||
|
|
"/mayx/blog/blame/commit/7785bc18c726c2757965874696fd0279cca57d2b/.gitlab-ci.yml",
|
||
|
|
"/sirius/gcloud-lab/commit/87632b22478417bdfd94d71e70fe752845adda4f/immediate-backup.yaml",
|
||
|
|
"/sirius/gcloud-lab/raw/commit/752d173a07eb698f661e8a8eb2f899a124a4cae5/apps/base/customer1/hermes-db/memory-db-credentials.yaml",
|
||
|
|
"/sirius/homek8s/blame/branch/wt/t_013fce30/.gitignore",
|
||
|
|
"/sirius/osint-dashboard/rss/branch/osint-dashboard/t_45362857-osint-hud-counts-shortcuts-terminator-zo/.env.example",
|
||
|
|
"/sirius/homek8s/compare/feat/anonymize-customer1...master?style=unified",
|
||
|
|
"/sirius/onionwire/-/archive/main/onionwire-main.tar.gz"):
|
||
|
|
self.assertNone(path)
|
||
|
|
|
||
|
|
def test_same_shapes_on_a_static_host_are_probes(self):
|
||
|
|
cat, sev, rule = http_rules.classify(rec("/.env", code=404))
|
||
|
|
self.assertEqual(rule, "http-secret-probe")
|
||
|
|
self.assertEqual(sev, "MEDIUM")
|
||
|
|
|
||
|
|
def test_git_endpoint_style_probe_on_another_host_is_vcs(self):
|
||
|
|
cat, _sev, rule = http_rules.classify(
|
||
|
|
rec("/.git/config", code=404, host="siriusdevops.com"))
|
||
|
|
self.assertEqual(cat, "vcs-probe")
|
||
|
|
self.assertEqual(rule, "http-vcs-probe")
|
||
|
|
|
||
|
|
|
||
|
|
class TestClientAttribution(unittest.TestCase):
|
||
|
|
def test_client_ip_wins(self):
|
||
|
|
self.assertEqual(http_rules.client_of(rec("/", client="198.51.100.9")),
|
||
|
|
"198.51.100.9")
|
||
|
|
|
||
|
|
def test_peer_is_the_fallback_for_internal_requests(self):
|
||
|
|
self.assertEqual(http_rules.client_of(rec("/", client_ip="", peer="10.10.0.3")),
|
||
|
|
"10.10.0.3")
|
||
|
|
|
||
|
|
def test_cloudflare_edge_without_client_ip_is_unattributable(self):
|
||
|
|
self.assertIsNone(http_rules.client_of(rec("/", client_ip="", peer="10.10.0.1")))
|
||
|
|
|
||
|
|
def test_x_forwarded_for_is_never_the_key(self):
|
||
|
|
r = rec("/", client_ip="", peer="10.10.0.3")
|
||
|
|
r["x_forwarded_for"] = "1.2.3.4,10.10.0.1"
|
||
|
|
self.assertEqual(http_rules.client_of(r), "10.10.0.3")
|
||
|
|
|
||
|
|
|
||
|
|
class TestAttackPathFindings(unittest.TestCase):
|
||
|
|
def test_one_finding_per_client_and_category_not_per_path(self):
|
||
|
|
records = [rec("/.env"), rec("/backup/.env"), rec("/api/.env.local"),
|
||
|
|
rec("/.git/config"), rec("/.env", client="198.51.100.9")]
|
||
|
|
findings, counts = http_rules.rule_attack_paths(records, "10m")
|
||
|
|
self.assertEqual(len(findings), 3) # 2 categories x 2 clients
|
||
|
|
secret = [f for f in findings if f.entity.startswith("secret-file")]
|
||
|
|
self.assertEqual(len(secret), 2)
|
||
|
|
self.assertTrue(any("(3x in 10m)" in f.detail for f in secret))
|
||
|
|
self.assertEqual(counts["http-secret-probe"], 4)
|
||
|
|
self.assertEqual(counts["http-vcs-probe"], 1)
|
||
|
|
|
||
|
|
def test_fingerprint_is_stable_and_path_independent(self):
|
||
|
|
a, _ = http_rules.rule_attack_paths([rec("/.env")], "10m")
|
||
|
|
b, _ = http_rules.rule_attack_paths(
|
||
|
|
[rec("/another/.env.production", code=200)], "10m")
|
||
|
|
self.assertEqual(a[0].fingerprint, b[0].fingerprint)
|
||
|
|
|
||
|
|
def test_unattributable_client_is_keyed_but_never_the_edge_ip(self):
|
||
|
|
records = [rec("/.env", client_ip="", peer="10.10.0.1")]
|
||
|
|
findings, _ = http_rules.rule_attack_paths(records, "10m")
|
||
|
|
self.assertIn("unknown", findings[0].entity)
|
||
|
|
self.assertNotIn("10.10.0.1", findings[0].entity)
|
||
|
|
|
||
|
|
def test_cap_collapses_a_flood_into_one_state_line(self):
|
||
|
|
records = [rec("/.env", client="198.51.100.%d" % i) for i in range(1, 21)]
|
||
|
|
findings, _ = http_rules.rule_attack_paths(records, "10m")
|
||
|
|
kept, overflow = http_rules.cap_findings(findings)
|
||
|
|
self.assertEqual(len(kept), config.HTTP_MAX_NEW_PER_TICK)
|
||
|
|
self.assertIsNotNone(overflow)
|
||
|
|
self.assertEqual(overflow.rule, "http-flood")
|
||
|
|
self.assertEqual(overflow.kind, "state")
|
||
|
|
self.assertIn("8 more", overflow.detail)
|
||
|
|
|
||
|
|
def test_under_the_cap_produces_no_overflow(self):
|
||
|
|
findings, _ = http_rules.rule_attack_paths([rec("/.env")], "10m")
|
||
|
|
kept, overflow = http_rules.cap_findings(findings)
|
||
|
|
self.assertEqual(kept, findings)
|
||
|
|
self.assertIsNone(overflow)
|
||
|
|
|
||
|
|
|
||
|
|
class TestAuthRate(unittest.TestCase):
|
||
|
|
def series(self, client, per_minute):
|
||
|
|
return [({"client_ip": client, "peer": "10.10.0.1"},
|
||
|
|
[(float(i * 60), float(v)) for i, v in enumerate(per_minute)])]
|
||
|
|
|
||
|
|
def test_ci_credential_retry_is_below_the_floor(self):
|
||
|
|
"""Measured: the git client retries 3/min, 25/hour - must stay silent."""
|
||
|
|
findings, n = http_rules.rule_auth_rate(self.series("137.118.156.124", [3, 3, 3, 3, 3]), "5m")
|
||
|
|
self.assertEqual(findings, [])
|
||
|
|
self.assertEqual(n, 1)
|
||
|
|
|
||
|
|
def test_single_minute_spike_below_total_is_silent(self):
|
||
|
|
# 19 in one minute, 19 total: neither the per-minute nor the total floor
|
||
|
|
findings, _ = http_rules.rule_auth_rate(self.series("198.51.100.5", [19, 0, 0, 0, 0]), "5m")
|
||
|
|
self.assertEqual(findings, [])
|
||
|
|
|
||
|
|
def test_sustained_brute_force_fires_once(self):
|
||
|
|
findings, _ = http_rules.rule_auth_rate(
|
||
|
|
self.series("198.51.100.5", [30, 2, 1, 0, 0]), "5m")
|
||
|
|
self.assertEqual(len(findings), 1)
|
||
|
|
self.assertEqual(findings[0].rule, "http-auth-brute")
|
||
|
|
self.assertEqual(findings[0].kind, "state")
|
||
|
|
self.assertIn("peak 30/min", findings[0].detail)
|
||
|
|
|
||
|
|
def test_heavy_brute_force_is_high(self):
|
||
|
|
findings, _ = http_rules.rule_auth_rate(
|
||
|
|
self.series("198.51.100.5", [60, 60, 60, 0, 0]), "5m")
|
||
|
|
self.assertEqual(findings[0].severity, "HIGH")
|
||
|
|
|
||
|
|
def test_internal_client_uses_peer(self):
|
||
|
|
matrix = [({"client_ip": "", "peer": "10.10.0.3"},
|
||
|
|
[(0.0, 40.0), (60.0, 5.0)])]
|
||
|
|
findings, _ = http_rules.rule_auth_rate(matrix, "5m")
|
||
|
|
self.assertEqual(len(findings), 1)
|
||
|
|
self.assertIn("10.10.0.3", findings[0].detail)
|
||
|
|
|
||
|
|
def test_edge_peer_is_not_a_client(self):
|
||
|
|
matrix = [({"client_ip": "", "peer": "10.10.0.1"},
|
||
|
|
[(0.0, 400.0), (60.0, 400.0)])]
|
||
|
|
self.assertEqual(http_rules.rule_auth_rate(matrix, "5m")[0], [])
|
||
|
|
|
||
|
|
|
||
|
|
class TestDecode(unittest.TestCase):
|
||
|
|
def test_double_encoding_is_decoded(self):
|
||
|
|
self.assertEqual(http_rules.decode_path("/%252f.env"), "//.env")
|
||
|
|
self.assertEqual(http_rules.decode_path("/%2f.env"), "//.env")
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
unittest.main()
|