File size: 8,608 Bytes
ccd6313 | 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | """Multi-stage attack orchestrator following Cyber Kill Chain model.
Each attacker has a scenario (one of 5 patterns) and progresses through
phases 0→3. Adaptation is non-trivial:
- Detected attackers may switch to stealth mode (mimic benign profiles)
- Undetected attackers escalate normally
- Fully blocked attackers are terminated
- Attackers that reach exfiltration (phase 3) are marked as succeeded
"""
from __future__ import annotations
from dataclasses import dataclass
from typing import Dict, List, Set
import numpy as np
# Updated import path
from server.utils.data_loader import TrafficGenerator
SCENARIOS = [
"port_scan_exploit_c2",
"credential_stuffing_lateral",
"supply_chain_compromise",
"low_and_slow_apt",
"ddos_amplification",
]
# How many sessions each scenario generates per phase
SESSION_COUNTS: Dict[str, List[int]] = {
"port_scan_exploit_c2": [4, 2, 1, 2],
"credential_stuffing_lateral": [3, 3, 2, 2],
"supply_chain_compromise": [1, 1, 1, 2],
"low_and_slow_apt": [1, 1, 1, 1],
"ddos_amplification": [6, 10, 15, 20],
}
# Probability that an attacker escalates per tick (if not detected)
ESCALATION_PROB: Dict[str, float] = {
"port_scan_exploit_c2": 0.30,
"credential_stuffing_lateral": 0.25,
"supply_chain_compromise": 0.15,
"low_and_slow_apt": 0.10,
"ddos_amplification": 0.40,
}
@dataclass
class AttackerState:
attacker_id: str
scenario: str
phase: int = 0
times_detected: int = 0
stealth_mode: bool = False
alive: bool = True
succeeded: bool = False
ticks_alive: int = 0
sessions_blocked: int = 0
sessions_generated: int = 0
class ThreatEngine:
"""Manages the lifecycle of active attackers and generates attack sessions."""
def __init__(self, seed: int = 0) -> None:
self.rng = np.random.default_rng(seed)
self._attacker_counter = 0
self._active_attackers: Dict[str, AttackerState] = {}
self._dead_attackers: List[AttackerState] = []
self._threat_intel: Dict = {
"known_bad_ports": [21, 22, 23, 25, 445, 3389, 5900],
"known_bad_ja3_ranges": [(200, 255), (230, 255)],
"active_campaigns": [],
"recent_detections": 0,
}
def reset(self) -> None:
self._attacker_counter = 0
self._active_attackers = {}
self._dead_attackers = []
self._threat_intel["active_campaigns"] = []
self._threat_intel["recent_detections"] = 0
def maybe_spawn_attacker(self, threat_probability: float) -> None:
"""Probabilistically spawn a new attacker."""
if self.rng.random() > threat_probability:
return
self._attacker_counter += 1
scenario = SCENARIOS[int(self.rng.integers(0, len(SCENARIOS)))]
attacker_id = f"a-{self._attacker_counter:04d}"
state = AttackerState(attacker_id=attacker_id, scenario=scenario)
self._active_attackers[attacker_id] = state
# Update threat intel
campaigns = set(self._threat_intel["active_campaigns"])
campaigns.add(scenario)
self._threat_intel["active_campaigns"] = sorted(campaigns)
def generate_attack_sessions(
self, tick: int, generator: TrafficGenerator,
blocked_attackers: Set[str],
escalation_rate_mod: float = 1.0,
stealth_multiplier: float = 1.0,
) -> List[Dict]:
"""Generate attack sessions for all active attackers, handling adaptation.
Args:
escalation_rate_mod: Multiplier on base escalation probability.
Values > 1.0 make attackers advance through kill chain faster.
stealth_multiplier: Controls how hard stealth attacks are to detect.
Values > 1.0 blend malicious features toward benign distributions.
"""
sessions: List[Dict] = []
for attacker in list(self._active_attackers.values()):
if not attacker.alive:
continue
attacker.ticks_alive += 1
# --- Handle detection / blocking ---
if attacker.attacker_id in blocked_attackers:
attacker.times_detected += 1
attacker.sessions_blocked += 1
self._threat_intel["recent_detections"] += 1
if attacker.times_detected >= 3:
# Fully blocked — attacker gives up
attacker.alive = False
self._dead_attackers.append(attacker)
continue
elif attacker.times_detected >= 2:
# Switch to stealth mode — generate fewer, more benign-looking sessions
attacker.stealth_mode = True
else:
# First detection — try to advance past detected phase
attacker.phase = min(attacker.phase + 1, 3)
# --- Natural phase escalation (modified by escalation_rate_mod) ---
else:
base_prob = ESCALATION_PROB.get(attacker.scenario, 0.2)
effective_prob = min(0.95, base_prob * escalation_rate_mod)
if self.rng.random() < effective_prob:
attacker.phase = min(attacker.phase + 1, 3)
# --- Check for success (exfiltration complete) ---
if attacker.phase == 3 and attacker.ticks_alive > 8:
if self.rng.random() < 0.15:
attacker.succeeded = True
attacker.alive = False
self._dead_attackers.append(attacker)
continue
# --- Generate sessions based on current state ---
counts = SESSION_COUNTS.get(attacker.scenario, [2, 2, 2, 2])
count = counts[min(attacker.phase, 3)]
if attacker.stealth_mode:
# In stealth mode: reduce count, use profiles that look more benign
count = max(1, count // 2)
generated = generator.generate_malicious_sessions(
tick=tick,
count=count,
attack_phase=attacker.phase,
scenario=attacker.scenario,
attacker_id=attacker.attacker_id,
)
# --- Apply stealth blending to stealth-mode sessions ---
if attacker.stealth_mode and stealth_multiplier > 1.0:
blend_strength = min(0.6, 0.2 * (stealth_multiplier - 1.0))
for s in generated:
s["metadata"]["is_stealth"] = True
feats = s["features"]
# Blend suspicious features toward benign ranges
feats["session_history_score"] = float(min(1.0,
feats["session_history_score"] + blend_strength * 0.5
))
feats["connection_reuse"] = float(min(1.0,
feats["connection_reuse"] + blend_strength * 0.4
))
feats["entropy_score"] = float(max(0.0,
feats["entropy_score"] - blend_strength * 0.3
))
feats["geo_distance"] = float(max(0.0,
feats["geo_distance"] * (1.0 - blend_strength * 0.4)
))
else:
for s in generated:
s["metadata"]["is_stealth"] = False
attacker.sessions_generated += len(generated)
sessions.extend(generated)
return sessions
def intelligence_feed(self) -> Dict:
"""Return threat intelligence available to the agent."""
active_scenarios = set()
for a in self._active_attackers.values():
if a.alive:
active_scenarios.add(a.scenario)
self._threat_intel["active_campaigns"] = sorted(active_scenarios)
return dict(self._threat_intel)
def attacker_outcomes(self) -> Dict[str, str]:
"""Return status of all known attackers (for info/debugging)."""
outcomes: Dict[str, str] = {}
for a in self._active_attackers.values():
if a.alive:
outcomes[a.attacker_id] = "active"
elif a.succeeded:
outcomes[a.attacker_id] = "succeeded"
else:
outcomes[a.attacker_id] = "stopped"
for a in self._dead_attackers:
if a.attacker_id not in outcomes:
outcomes[a.attacker_id] = "succeeded" if a.succeeded else "stopped"
return outcomes |