-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathred_script.py
More file actions
288 lines (222 loc) · 10 KB
/
Copy pathred_script.py
File metadata and controls
288 lines (222 loc) · 10 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import os
dirs = [
'red_agent', 'red_agent/config',
'red_agent/core', 'red_agent/utils'
]
for d in dirs:
os.makedirs(d, exist_ok=True)
# ── requirements.txt ─────────────────────────────────────────────────────────
with open('red_agent/requirements.txt', 'w') as f:
f.write(
"cryptography>=41.0.0\n"
"psutil>=5.9.0\n"
)
# ── config/__init__.py ────────────────────────────────────────────────────────
open('red_agent/config/__init__.py', 'w').close()
# ── config/settings.py ───────────────────────────────────────────────────────
with open('red_agent/config/settings.py', 'w') as f:
f.write('''\
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Optional
@dataclass
class AgentConfig:
# Entropy
entropy_minimum_bits: int = 128
# Stochastic timing (milliseconds)
timing_min_delay_ms: float = 50.0
timing_max_delay_ms: float = 2000.0
timing_distribution: str = "lognormal" # lognormal | uniform | exponential
# Output gate
gate_max_reevals: int = 3
gate_safety_margin_ratio: float = 0.10
gate_safety_margin_min_ms: float = 100.0
# Recovery signal
recovery_signal_ttl_seconds: int = 300
recovery_probe_threshold: int = 5
# Expired-unit escalation
expired_unit_probe_window_seconds: int = 60
expired_unit_probe_threshold: int = 3
# Storage paths
audit_store_path: str = "red_agent_audit.jsonl"
artifact_store_path: str = "red_agent_artifacts.json"
# Pre-shared key for recovery signals (hex-encoded, 32 bytes)
recovery_psk_hex: Optional[str] = None
# Deception layer (OPTIONAL)
deception_layer_active: bool = False
@property
def recovery_psk(self) -> Optional[bytes]:
if self.recovery_psk_hex:
return bytes.fromhex(self.recovery_psk_hex)
return None
''')
# ── utils/__init__.py ─────────────────────────────────────────────────────────
open('red_agent/utils/__init__.py', 'w').close()
# ── utils/entropy.py ──────────────────────────────────────────────────────────
with open('red_agent/utils/entropy.py', 'w') as f:
f.write('''\
"""
Entropy scoring and seed generation (Section 4.2 / Section 5 STEP 01).
Minimum threshold: 128 bits.
Adversary-controllable sources contribute 0 bits.
"""
from __future__ import annotations
import os, time, struct, hashlib
from typing import Tuple
try:
import psutil
_PSUTIL_AVAILABLE = True
except ImportError:
_PSUTIL_AVAILABLE = False
class EntropyInsufficientError(RuntimeError):
"""Raised when scored entropy total falls below 128-bit minimum."""
# ── source collectors ─────────────────────────────────────────────────────────
def _os_entropy() -> Tuple[bytes, int]:
raw = os.urandom(32) # 256 bits from OS CSPRNG
return raw, 128 # credited at threshold minimum
def _wall_clock_jitter() -> Tuple[bytes, int]:
samples: list[int] = []
for _ in range(8):
t1 = time.perf_counter_ns()
time.sleep(0)
t2 = time.perf_counter_ns()
samples.append(t2 - t1)
raw = struct.pack(">8Q", *samples)
return raw, 16 # credited at 16 bits
def _memory_pressure() -> Tuple[bytes, int]:
if not _PSUTIL_AVAILABLE:
return b"\\x00" * 16, 0
try:
vm = psutil.virtual_memory()
swap = psutil.swap_memory()
raw = struct.pack(">QQd", vm.available, vm.used, float(swap.percent))
return raw, 8
except Exception:
return b"\\x00" * 20, 0
# ── public API ────────────────────────────────────────────────────────────────
def generate_seed(task_queue_depth: int = 0) -> bytes:
"""
Aggregate ambient entropy; hash-condition the result.
Raises EntropyInsufficientError if total < 128 bits.
Returns 32 bytes suitable for use as a noise seed.
"""
sources: list[Tuple[bytes, int]] = []
raw, bits = _os_entropy()
sources.append((raw, bits))
raw, bits = _wall_clock_jitter()
sources.append((raw, bits))
raw, bits = _memory_pressure()
sources.append((raw, bits))
if task_queue_depth > 0:
tq_raw = struct.pack(">I", task_queue_depth)
tq_bits = min(task_queue_depth.bit_length(), 8)
sources.append((tq_raw, tq_bits))
total_bits = sum(b for _, b in sources)
if total_bits < 128:
raise EntropyInsufficientError(
f"Scored entropy {total_bits} bits — minimum 128 required; halting."
)
combined = b"".join(raw for raw, _ in sources)
seed = hashlib.blake2b(combined, digest_size=32).digest()
return seed
''')
# ── utils/crypto.py ───────────────────────────────────────────────────────────
with open('red_agent/utils/crypto.py', 'w') as f:
f.write('''\
"""
Cryptographic primitives:
- SHA-256 for hash-chain entries (Section 4.3.2)
- HMAC-SHA256 for recovery signal verification (Section 4.6)
- secure_zero: best-effort process-local memory zeroing
"""
from __future__ import annotations
import ctypes, hashlib, hmac, os, sys
from typing import Optional
# ── SHA-256 ───────────────────────────────────────────────────────────────────
def sha256(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
# ── HMAC-SHA256 ───────────────────────────────────────────────────────────────
def compute_hmac(key: bytes, message: bytes) -> bytes:
return hmac.new(key, message, hashlib.sha256).digest()
def verify_hmac(key: bytes, message: bytes, tag: bytes) -> bool:
expected = compute_hmac(key, message)
return hmac.compare_digest(expected, tag)
# ── secure_zero ───────────────────────────────────────────────────────────────
def secure_zero(buf: bytearray) -> bool:
"""
Overwrite *buf* with zeros using ctypes memset, which the compiler
cannot optimise away. Returns True on success, False on failure.
Simulates explicit_bzero / SecureZeroMemory semantics.
"""
if not isinstance(buf, bytearray):
return False
try:
addr = (ctypes.c_char * len(buf)).from_buffer(buf)
ctypes.memset(addr, 0, len(buf))
return True
except Exception:
try:
for i in range(len(buf)):
buf[i] = 0
return True
except Exception:
return False
''')
# ── utils/serialization.py ────────────────────────────────────────────────────
with open('red_agent/utils/serialization.py', 'w') as f:
f.write('''\
"""
Serialization Boundary Protocol (Section 3.2).
- Randomize field ordering within each object.
- Stochastic padding on string fields (stripped by receiver).
- Variation driven by session entropy seed — not deterministic.
"""
from __future__ import annotations
import json, random, secrets
from typing import Any
_PAD_CHAR = "\\x00" # stripped by receiver on deserialise
def _make_rng(seed: bytes) -> random.Random:
rng = random.Random()
rng.seed(seed)
return rng
def _pad_string(value: str, rng: random.Random, max_pad: int = 32) -> str:
n = rng.randint(0, max_pad)
return value + (_PAD_CHAR * n)
def _shuffle_dict(d: dict, rng: random.Random) -> dict:
keys = list(d.keys())
rng.shuffle(keys)
return {k: d[k] for k in keys}
def transform(payload: dict, seed: bytes) -> bytes:
"""
Apply serialization boundary transformation driven by *seed*.
Returns wire-format bytes.
"""
rng = _make_rng(seed)
def _transform_value(v: Any) -> Any:
if isinstance(v, str):
return _pad_string(v, rng)
if isinstance(v, dict):
return _shuffle_dict({k: _transform_value(vv) for k, vv in v.items()}, rng)
if isinstance(v, list):
return [_transform_value(i) for i in v]
return v
transformed = _transform_value(payload)
if isinstance(transformed, dict):
transformed = _shuffle_dict(transformed, rng)
return json.dumps(transformed, separators=(",", ":")).encode()
def detransform(data: bytes) -> dict:
"""
Strip padding and return canonical dict (field order is restored by receiver).
"""
raw = json.loads(data.decode())
def _strip(v: Any) -> Any:
if isinstance(v, str):
return v.rstrip(_PAD_CHAR)
if isinstance(v, dict):
return {k: _strip(vv) for k, vv in v.items()}
if isinstance(v, list):
return [_strip(i) for i in v]
return v
return _strip(raw)
''')
print("Batch 1 (config + utils) written successfully.")