File size: 5,246 Bytes
5f02c05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import random
import re
from collections import Counter

import pytest

from hades.data.synth.authlog import (
    LABELS,
    SYSTEM_USERS,
    generate_dataset,
    generate_unlabelled,
    generate_window,
    make_vocab,
)

FAILED = re.compile(r"Failed password for (invalid user )?(\S+) from (\S+) port")
ACCEPTED = re.compile(r"Accepted \S+ for (\S+) from (\S+) port")
INVALID = re.compile(r"Invalid user (\S+) from (\S+)")


def facts(lines):
    fails = [(m.group(2), m.group(3), bool(m.group(1))) for l in lines if (m := FAILED.search(l))]
    accepted = [(m.group(1), m.group(2)) for l in lines if (m := ACCEPTED.search(l))]
    invalid = [m.group(1) for l in lines if (m := INVALID.search(l))]
    sudo_denied = [l for l in lines if "NOT in sudoers" in l or "incorrect password attempts" in l]
    return fails, accepted, invalid, sudo_denied


def windows(label, n=60, split="train"):
    rng = random.Random(label)
    vocab = make_vocab(split)
    return [generate_window(rng, label, vocab) for _ in range(n)]


def test_labels():
    assert LABELS == ("normal", "failed_login", "brute_force", "password_spray", "invalid_user_scan", "sudo_denied", "compromise")


@pytest.mark.parametrize("label", LABELS)
def test_window_length(label):
    for w in windows(label):
        assert 5 <= len(w) <= 20


def test_normal():
    for w in windows("normal"):
        fails, _, invalid, denied = facts(w)
        assert not fails and not invalid and not denied


def test_failed_login():
    for w in windows("failed_login"):
        fails, accepted, invalid, denied = facts(w)
        assert 1 <= len(fails) <= 2 and len({ip for _, ip, _ in fails}) == 1
        assert not invalid and not denied


def test_brute_force():
    for w in windows("brute_force"):
        fails, accepted, _, _ = facts(w)
        ip = Counter(ip for _, ip, _ in fails).most_common(1)[0][0]
        from_ip = [u for u, i, _ in fails if i == ip]
        assert len(from_ip) >= 6 and len(set(from_ip)) <= 3
        assert ip not in {i for _, i in accepted}


def test_password_spray():
    for w in windows("password_spray"):
        fails, _, _, _ = facts(w)
        by_ip = {}
        for u, ip, inv in fails:
            if not inv:
                by_ip.setdefault(ip, set()).add(u)
        assert max(len(s) for s in by_ip.values()) >= 5


def test_invalid_user_scan():
    for w in windows("invalid_user_scan"):
        fails, _, invalid, _ = facts(w)
        assert len(invalid) >= 4
        assert all(inv for _, _, inv in fails)  # only invalid-user failures


def test_sudo_denied():
    for w in windows("sudo_denied"):
        fails, _, invalid, denied = facts(w)
        assert denied and not fails and not invalid


def test_compromise():
    for w in windows("compromise"):
        fails, accepted, _, _ = facts(w)
        ip = Counter(ip for _, ip, _ in fails).most_common(1)[0][0]
        assert sum(1 for _, i, _ in fails if i == ip) >= 6
        acc_idx = [k for k, l in enumerate(w) if (m := ACCEPTED.search(l)) and m.group(2) == ip]
        last_fail = max(k for k, l in enumerate(w) if (m := FAILED.search(l)) and m.group(3) == ip)
        assert acc_idx and acc_idx[0] > last_fail


def test_deterministic():
    assert generate_dataset(14, seed=5) == generate_dataset(14, seed=5)
    assert generate_dataset(14, seed=5) != generate_dataset(14, seed=6)


def test_balanced():
    counts = Counter(w.label for w in generate_dataset(70, seed=1))
    assert set(counts.values()) == {10}


def test_heldout_vocab_disjoint():
    train, held = make_vocab("train"), make_vocab("heldout")
    assert not set(train.usernames) & set(held.usernames)
    assert not set(train.invalid_usernames) & set(held.invalid_usernames)
    assert not set(train.hostnames) & set(held.hostnames)
    assert not set(SYSTEM_USERS) & (set(train.usernames) | set(held.usernames))
    train_names = set(train.usernames) | set(train.invalid_usernames) | set(train.hostnames)
    for w in generate_dataset(70, seed=2, split="heldout"):
        words = set(re.findall(r"[A-Za-z0-9_.-]+", w.text))
        assert not words & train_names


def test_iso_space_only_heldout():
    iso_space = re.compile(r"^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d ", re.M)
    assert not any(iso_space.search(w.text) for w in generate_dataset(140, seed=3))
    assert any(iso_space.search(w.text) for w in generate_dataset(140, seed=3, split="heldout"))


def test_token_cap():
    for w in generate_dataset(70, seed=4, count_tokens=len, max_tokens=900):
        assert len(w.text) <= 900


def test_test_seeds_reserved():
    with pytest.raises(ValueError):
        generate_dataset(7, seed=9001)
    with pytest.raises(ValueError):
        generate_dataset(7, seed=5, purpose="test")
    assert len(generate_dataset(7, seed=9001, purpose="test")) == 7


def test_unlabelled():
    docs = list(generate_unlabelled(100_000, 0))
    text = "\n".join(docs)
    assert len(text.encode()) >= 100_000
    assert all(50 <= len(d.splitlines()) <= 500 for d in docs)
    for label in LABELS:
        assert label not in text


def test_impossible_token_cap_raises_instead_of_looping():
    with pytest.raises(ValueError, match="max_tokens"):
        generate_dataset(7, seed=1, count_tokens=len, max_tokens=50)