File size: 1,792 Bytes
f4bf7d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Scripted attacker for defender Gen-1 GRPO training.

Provides randomised attack templates so the training dataset covers all 8
attack types, not just single_ip_flood.
"""
from __future__ import annotations

import logging
import random

logger = logging.getLogger(__name__)

try:
    from ..models import AttackerAction
except ImportError:
    from models import AttackerAction

TEMPLATES: list[str] = [
    "single_ip_flood",
    "ip_spray",
    "credential_stuffing",
    "payload_injection",
    "header_spoof",
    "slow_drip",
    "path_traversal",
    "mixed_legit_cover",
]

_DEFAULTS: dict[str, dict] = {
    "single_ip_flood":     {"count": 20, "target_path": "/login"},
    "ip_spray":            {"count": 15, "target_path": "/api/data"},
    "credential_stuffing": {"count": 10, "target_path": "/login"},
    "payload_injection":   {"count": 8,  "target_path": "/api/process"},
    "header_spoof":        {"count": 10, "target_path": "/login"},
    "slow_drip":           {"count": 5,  "target_path": "/"},
    "path_traversal":      {"count": 8,  "target_path": "/api/data"},
    "mixed_legit_cover":   {"count": 12, "target_path": "/"},
}


class ScriptedAttacker:
    """Randomly selects attack templates for defender training episodes.

    Uses a seeded RNG so the same seed always produces the same sequence.
    """

    def __init__(self, seed: int = 42) -> None:
        self._rng = random.Random(seed)

    def act(self) -> AttackerAction:
        """Return a random AttackerAction drawn uniformly from all 8 templates."""
        template = self._rng.choice(TEMPLATES)
        defaults = _DEFAULTS[template]
        return AttackerAction(
            template=template,
            count=defaults["count"],
            target_path=defaults["target_path"],
        )