File size: 3,058 Bytes
a56b6dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Copy the browser model into the Space and pick the sample streams it offers.

Usage: python src/export_space.py --data ../incident-logs-dataset/data --challenge data/challenge.parquet
"""
import argparse
import json
import shutil

import pandas as pd

from common import load_events

# (scenario, button label, source set)
PICKS = [
    ("ssh_bruteforce_persistence", "SSH brute force → cron persistence", "test"),
    ("web_sqli_webshell", "SQL injection → web shell", "test"),
    ("cloud_key_compromise", "Stolen AWS key", "test"),
    ("dns_tunneling_exfil", "DNS tunneling", "test"),
    ("credential_stuffing", "Credential stuffing", "test"),
    ("benign_ssh_admin", "Admins (typo'd passwords)", "test"),
    ("benign_cloud_ops", "Cloud ops (key rotation)", "test"),
    ("ch_ssh_spray", "Slow SSH spray + miner (unseen)", "challenge"),
    ("ch_cloud_key", "Stolen key, normal CLI (unseen)", "challenge"),
    ("ch_dns_tunnel", "Hex DNS tunnel (unseen)", "challenge"),
    ("ch_benign_web", "Health checks + typos (unseen)", "challenge"),
]
MAX_LINES = 70


def pick(df, scenario, need_hard_negative=False):
    g = df[df.scenario == scenario]
    sizes = g.groupby("session_id").size()
    ok = sizes[(sizes >= 15) & (sizes <= MAX_LINES)]
    cands = list(ok.index) or list(sizes.sort_values().index[:1])
    for sid in cands:
        s = g[g.session_id == sid]
        if scenario == "benign_ssh_admin" and not s.raw_log.str.contains("Failed password").any():
            continue
        if scenario == "benign_cloud_ops" and not s.raw_log.str.contains("CreateAccessKey").any():
            continue
        if scenario == "cloud_key_compromise" and not s.raw_log.str.contains("StopLogging|DeleteTrail").any():
            continue
        if scenario == "web_sqli_webshell" and not s.raw_log.str.contains("/dev/tcp").any():
            continue
        return s.sort_values("seq").head(MAX_LINES)
    return g[g.session_id == cands[0]].sort_values("seq").head(MAX_LINES)


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--data", default=None)
    ap.add_argument("--challenge", default="data/challenge.parquet")
    ap.add_argument("--model", default="outputs/model/model.json")
    ap.add_argument("--space", default="space")
    args = ap.parse_args()
    shutil.copy(args.model, f"{args.space}/model.json")
    sets = {"test": load_events(args.data, "test"), "challenge": pd.read_parquet(args.challenge)}
    out = []
    for scen, label, src in PICKS:
        s = pick(sets[src], scen)
        out.append({"name": label, "set": src, "scenario": scen,
                    "malicious": bool(s.is_malicious.any()),
                    "lines": s.raw_log.tolist(), "truth": s.is_malicious.astype(int).tolist(),
                    "technique": s.mitre_technique_id.fillna("").tolist()})
    with open(f"{args.space}/samples.json", "w") as fh:
        json.dump(out, fh, separators=(",", ":"))
    print(f"wrote {len(out)} samples:", [(o["name"], len(o["lines"])) for o in out])


if __name__ == "__main__":
    main()