Download scripts/validate.py from DaveyD347/sanitized-incident-logs: direct link, hf CLI and curl.
- Browser
- Download file 3.95 kB
-
https://huggingface.co/datasets/DaveyD347/sanitized-incident-logs/resolve/main/scripts/validate.py
- Command line
-
hf download hf://datasets/DaveyD347/sanitized-incident-logs/scripts/validate.py
-
curl -L -o validate.py https://huggingface.co/datasets/DaveyD347/sanitized-incident-logs/resolve/main/scripts/validate.py
3.95 kB
| #!/usr/bin/env python3 | |
| """Validate the generated dataset: schema, label consistency, split leakage, and PII/identifier safety.""" | |
| import ipaddress | |
| import re | |
| import sys | |
| import pandas as pd | |
| DATA = sys.argv[1] if len(sys.argv) > 1 else "data" | |
| ALLOWED_NETS = [ipaddress.ip_network(n) for n in | |
| ("192.0.2.0/24", "198.51.100.0/24", "203.0.113.0/24", "10.0.0.0/8", "0.0.0.0/32")] | |
| ALLOWED_DOMAIN_SUFFIX = (".example.com", ".example.net", ".example.org", ".test", "example.com", | |
| "example.net", "example.org", "amazonaws.com") | |
| IP_RE = re.compile(r"(?<![\d.])(\d{1,3}(?:\.\d{1,3}){3})(?![\d.])") | |
| EMAIL_RE = re.compile(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}") | |
| AKIA_RE = re.compile(r"\b(AKIA|ASIA)[A-Z0-9]{16}\b") | |
| URL_HOST_RE = re.compile(r"https?://([A-Za-z0-9.-]+)") | |
| ACCOUNT_RE = re.compile(r"arn:aws:iam::(\d{12})") | |
| fails = [] | |
| def check(cond, msg): | |
| print(("PASS " if cond else "FAIL ") + msg) | |
| if not cond: | |
| fails.append(msg) | |
| ev = pd.concat([pd.read_parquet(f"{DATA}/events/{s}.parquet").assign(_split=s) for s in ("train", "validation", "test")]) | |
| se = pd.concat([pd.read_parquet(f"{DATA}/sessions/{s}.parquet").assign(_split=s) for s in ("train", "validation", "test")]) | |
| # --- schema / integrity | |
| check(ev.event_id.is_unique, "event_id unique") | |
| check(se.session_id.is_unique, "session_id unique") | |
| check(set(ev.session_id) == set(se.session_id), "every event belongs to a session and vice versa") | |
| check(ev.groupby("session_id")._split.nunique().max() == 1, "no session spans splits (no leakage)") | |
| check(ev.raw_log.notna().all() and (ev.raw_log.str.len() > 0).all(), "raw_log non-empty") | |
| n_ev = ev.groupby("session_id").size() | |
| check((se.set_index("session_id").num_events == n_ev.reindex(se.session_id).values).all(), "num_events matches events table") | |
| # --- label consistency | |
| m = ev.groupby("session_id").is_malicious.any() | |
| lab = se.set_index("session_id").label | |
| check(((lab == "malicious") == m.reindex(lab.index)).all(), "session label == any(event.is_malicious)") | |
| check(ev[ev.is_malicious].mitre_technique_id.notna().all(), "all malicious events carry a MITRE technique") | |
| check(ev[~ev.is_malicious].mitre_technique_id.isna().all(), "no benign event carries a MITRE technique") | |
| check(((se.label == "benign") == (se.severity == "none")).all(), "severity 'none' iff benign") | |
| # --- PII / identifier safety over every raw log + summary | |
| text = pd.concat([ev.raw_log, se.summary]).astype(str) | |
| bad_ips, bad_domains = set(), set() | |
| for t in text: | |
| for ip in IP_RE.findall(t): | |
| try: | |
| a = ipaddress.ip_address(ip) | |
| except ValueError: | |
| continue | |
| if not any(a in n for n in ALLOWED_NETS): | |
| bad_ips.add(ip) | |
| for h in URL_HOST_RE.findall(t): | |
| if not (ipaddress.ip_address(h) if re.fullmatch(r"[\d.]+", h) else None) and not h.endswith(ALLOWED_DOMAIN_SUFFIX): | |
| bad_domains.add(h) | |
| check(not bad_ips, f"all IPv4 addresses in RFC 5737 / RFC 1918 ranges (bad: {sorted(bad_ips)[:5]})") | |
| check(not bad_domains, f"all URL hosts use reserved domains (bad: {sorted(bad_domains)[:5]})") | |
| qnames = ev[ev.source_type == "dns_query"].raw_log.str.extract(r"query: (\S+) IN")[0] | |
| bad_q = qnames[~qnames.str.endswith(ALLOWED_DOMAIN_SUFFIX)] | |
| check(bad_q.empty, f"all DNS qnames use reserved domains (bad: {bad_q.head(3).tolist()})") | |
| emails = {e for t in text for e in EMAIL_RE.findall(t)} | |
| check(not emails, f"no email addresses (found: {list(emails)[:5]})") | |
| keys = {k.group(0) for t in text for k in AKIA_RE.finditer(t)} | |
| check(all(k.endswith("EXAMPLE") for k in keys), f"all AWS access keys are EXAMPLE placeholders ({len(keys)} keys)") | |
| accts = {a for t in ev.raw_log for a in ACCOUNT_RE.findall(t)} | |
| check(all(a.startswith("0000") for a in accts), f"all AWS account IDs use reserved 0000 prefix ({len(accts)} ids)") | |
| print(f"\nevents={len(ev)} sessions={len(se)} malicious_event_rate={ev.is_malicious.mean():.3f}") | |
| sys.exit(1 if fails else 0) | |