cipher-security 2.0.8 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/cipher.js +11 -1
- package/lib/agent-runtime/handlers/architect.js +199 -0
- package/lib/agent-runtime/handlers/base.js +240 -0
- package/lib/agent-runtime/handlers/blue.js +220 -0
- package/lib/agent-runtime/handlers/incident.js +161 -0
- package/lib/agent-runtime/handlers/privacy.js +190 -0
- package/lib/agent-runtime/handlers/purple.js +209 -0
- package/lib/agent-runtime/handlers/recon.js +174 -0
- package/lib/agent-runtime/handlers/red.js +246 -0
- package/lib/agent-runtime/handlers/researcher.js +170 -0
- package/lib/agent-runtime/handlers.js +35 -0
- package/lib/agent-runtime/index.js +196 -0
- package/lib/agent-runtime/parser.js +316 -0
- package/lib/analyze/consistency.js +566 -0
- package/lib/analyze/constitution.js +110 -0
- package/lib/analyze/sharding.js +251 -0
- package/lib/autonomous/agent-tool.js +165 -0
- package/lib/autonomous/feedback-loop.js +13 -6
- package/lib/autonomous/framework.js +17 -0
- package/lib/autonomous/handoff.js +506 -0
- package/lib/autonomous/modes/blue.js +26 -0
- package/lib/autonomous/modes/red.js +585 -0
- package/lib/autonomous/modes/researcher.js +322 -0
- package/lib/autonomous/researcher.js +12 -45
- package/lib/autonomous/runner.js +9 -537
- package/lib/benchmark/agent.js +88 -26
- package/lib/benchmark/baselines.js +3 -0
- package/lib/benchmark/claude-code-solver.js +254 -0
- package/lib/benchmark/cognitive.js +283 -0
- package/lib/benchmark/index.js +12 -2
- package/lib/benchmark/knowledge.js +281 -0
- package/lib/benchmark/llm.js +156 -15
- package/lib/benchmark/models.js +5 -2
- package/lib/benchmark/nyu-ctf.js +192 -0
- package/lib/benchmark/overthewire.js +347 -0
- package/lib/benchmark/picoctf.js +281 -0
- package/lib/benchmark/prompts.js +280 -0
- package/lib/benchmark/registry.js +219 -0
- package/lib/benchmark/remote-solver.js +356 -0
- package/lib/benchmark/remote-target.js +263 -0
- package/lib/benchmark/reporter.js +35 -0
- package/lib/benchmark/runner.js +174 -10
- package/lib/benchmark/sandbox.js +35 -0
- package/lib/benchmark/scorer.js +22 -4
- package/lib/benchmark/solver.js +34 -1
- package/lib/benchmark/tools.js +262 -16
- package/lib/commands.js +9 -0
- package/lib/execution/council.js +434 -0
- package/lib/execution/parallel.js +292 -0
- package/lib/gates/circuit-breaker.js +135 -0
- package/lib/gates/confidence.js +302 -0
- package/lib/gates/corrections.js +219 -0
- package/lib/gates/self-check.js +245 -0
- package/lib/gateway/commands.js +727 -0
- package/lib/guardrails/engine.js +364 -0
- package/lib/mcp/server.js +349 -3
- package/lib/memory/compressor.js +94 -7
- package/lib/pipeline/hooks.js +288 -0
- package/lib/pipeline/index.js +11 -0
- package/lib/review/budget.js +210 -0
- package/lib/review/engine.js +526 -0
- package/lib/review/layers/acceptance-auditor.js +279 -0
- package/lib/review/layers/blind-hunter.js +500 -0
- package/lib/review/layers/defense-in-depth.js +209 -0
- package/lib/review/layers/edge-case-hunter.js +266 -0
- package/lib/review/panel.js +519 -0
- package/lib/review/two-stage.js +244 -0
- package/lib/session/cost-tracker.js +203 -0
- package/lib/session/logger.js +349 -0
- package/package.json +1 -1
|
@@ -0,0 +1,585 @@
|
|
|
1
|
+
// Copyright (c) 2026 defconxt. All rights reserved.
|
|
2
|
+
// Licensed under AGPL-3.0 — see LICENSE file for details.
|
|
3
|
+
// CIPHER is a trademark of defconxt.
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* RED mode agent — Offensive Security / Penetration Testing.
|
|
7
|
+
*
|
|
8
|
+
* Extracted from runner.js. Provides sandbox execution, HTTP requests,
|
|
9
|
+
* file reading, and network exploitation tools for CTF-style challenges.
|
|
10
|
+
* Supports web, network, and binary challenge categories.
|
|
11
|
+
*
|
|
12
|
+
* @module autonomous/modes/red
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { ModeAgentConfig, ToolRegistry } from '../framework.js';
|
|
16
|
+
import { FlagValidator, FLAG_PATTERN } from '../runner.js';
|
|
17
|
+
|
|
18
|
+
const debug = process.env.CIPHER_DEBUG === '1'
|
|
19
|
+
? (/** @type {string} */ msg) => process.stderr.write(`[red-mode] ${msg}\n`)
|
|
20
|
+
: () => {};
|
|
21
|
+
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
// Completion check
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Return true if text contains a FLAG{hex} pattern.
|
|
28
|
+
* @param {string} text
|
|
29
|
+
* @returns {boolean}
|
|
30
|
+
*/
|
|
31
|
+
function _redCompletionCheck(text) {
|
|
32
|
+
return FLAG_PATTERN.test(text);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
// Tool handlers
|
|
37
|
+
// ---------------------------------------------------------------------------
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Execute an arbitrary shell command in the sandbox.
|
|
41
|
+
* @param {*} context
|
|
42
|
+
* @param {Object} toolInput
|
|
43
|
+
* @returns {string}
|
|
44
|
+
*/
|
|
45
|
+
function _redSandboxExec(context, toolInput) {
|
|
46
|
+
const command = toolInput.command;
|
|
47
|
+
const [exitCode, stdout, stderr] = context.execTool(command);
|
|
48
|
+
|
|
49
|
+
const parts = [];
|
|
50
|
+
if (stdout.trim()) parts.push(`STDOUT:\n${stdout}`);
|
|
51
|
+
if (stderr.trim()) parts.push(`STDERR:\n${stderr}`);
|
|
52
|
+
parts.push(`EXIT CODE: ${exitCode}`);
|
|
53
|
+
|
|
54
|
+
return parts.join('\n');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Make an HTTP request via curl inside the sandbox.
|
|
59
|
+
* @param {*} context
|
|
60
|
+
* @param {Object} toolInput
|
|
61
|
+
* @returns {string}
|
|
62
|
+
*/
|
|
63
|
+
function _redHttpRequest(context, toolInput) {
|
|
64
|
+
const url = toolInput.url;
|
|
65
|
+
const method = (toolInput.method || 'GET').toUpperCase();
|
|
66
|
+
const headers = toolInput.headers || {};
|
|
67
|
+
const body = toolInput.body;
|
|
68
|
+
|
|
69
|
+
const cmdParts = ['curl', '-s', '-S', '-i', '-X', method];
|
|
70
|
+
|
|
71
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
72
|
+
const escapedVal = value.replace(/'/g, "'\\''");
|
|
73
|
+
cmdParts.push('-H', `'${key}: ${escapedVal}'`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (body) {
|
|
77
|
+
const escapedBody = body.replace(/'/g, "'\\''");
|
|
78
|
+
cmdParts.push('-d', `'${escapedBody}'`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const escapedUrl = url.replace(/"/g, '\\"');
|
|
82
|
+
cmdParts.push(`"${escapedUrl}"`);
|
|
83
|
+
const command = cmdParts.join(' ');
|
|
84
|
+
|
|
85
|
+
const [exitCode, stdout, stderr] = context.execTool(command);
|
|
86
|
+
|
|
87
|
+
const parts = [];
|
|
88
|
+
if (stdout.trim()) parts.push(stdout);
|
|
89
|
+
if (stderr.trim()) parts.push(`CURL ERROR:\n${stderr}`);
|
|
90
|
+
parts.push(`EXIT CODE: ${exitCode}`);
|
|
91
|
+
|
|
92
|
+
return parts.join('\n');
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Read a file inside the sandbox.
|
|
97
|
+
* @param {*} context
|
|
98
|
+
* @param {Object} toolInput
|
|
99
|
+
* @returns {string}
|
|
100
|
+
*/
|
|
101
|
+
function _redReadFile(context, toolInput) {
|
|
102
|
+
const path = toolInput.path;
|
|
103
|
+
const [exitCode, stdout, stderr] = context.execTool(`cat '${path}'`);
|
|
104
|
+
|
|
105
|
+
if (exitCode !== 0) {
|
|
106
|
+
return `ERROR reading ${path}: ${stderr.trim()}`;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return stdout;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// ---------------------------------------------------------------------------
|
|
113
|
+
// Network exploitation tool handlers
|
|
114
|
+
// ---------------------------------------------------------------------------
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Scan a target for open ports using nmap in the sandbox.
|
|
118
|
+
* @param {*} context
|
|
119
|
+
* @param {Object} toolInput
|
|
120
|
+
* @returns {string}
|
|
121
|
+
*/
|
|
122
|
+
function _netPortScan(context, toolInput) {
|
|
123
|
+
const target = toolInput.target;
|
|
124
|
+
const ports = toolInput.ports || '1-1000';
|
|
125
|
+
|
|
126
|
+
const command = `nmap -sV -T4 -p ${ports} ${target}`;
|
|
127
|
+
debug(`port_scan: ${command}`);
|
|
128
|
+
const [exitCode, stdout, stderr] = context.execTool(command);
|
|
129
|
+
|
|
130
|
+
const parts = [];
|
|
131
|
+
if (stdout.trim()) parts.push(stdout);
|
|
132
|
+
if (stderr.trim()) parts.push(`STDERR:\n${stderr}`);
|
|
133
|
+
parts.push(`EXIT CODE: ${exitCode}`);
|
|
134
|
+
|
|
135
|
+
return parts.join('\n');
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Open a raw TCP connection and return the banner.
|
|
140
|
+
* @param {*} context
|
|
141
|
+
* @param {Object} toolInput
|
|
142
|
+
* @returns {string}
|
|
143
|
+
*/
|
|
144
|
+
function _netConnectTcp(context, toolInput) {
|
|
145
|
+
const host = toolInput.host;
|
|
146
|
+
const port = toolInput.port;
|
|
147
|
+
const timeout = toolInput.timeout || 5;
|
|
148
|
+
|
|
149
|
+
const command = `echo | nc -w${timeout} ${host} ${port}`;
|
|
150
|
+
debug(`connect_tcp: ${command}`);
|
|
151
|
+
const [exitCode, stdout, stderr] = context.execTool(command);
|
|
152
|
+
|
|
153
|
+
const parts = [];
|
|
154
|
+
if (stdout.trim()) parts.push(stdout);
|
|
155
|
+
if (stderr.trim()) parts.push(`STDERR:\n${stderr}`);
|
|
156
|
+
parts.push(`EXIT CODE: ${exitCode}`);
|
|
157
|
+
|
|
158
|
+
return parts.join('\n');
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Send a payload over TCP and return the response.
|
|
163
|
+
* @param {*} context
|
|
164
|
+
* @param {Object} toolInput
|
|
165
|
+
* @returns {string}
|
|
166
|
+
*/
|
|
167
|
+
function _netSendPayload(context, toolInput) {
|
|
168
|
+
const host = toolInput.host;
|
|
169
|
+
const port = toolInput.port;
|
|
170
|
+
const data = toolInput.data;
|
|
171
|
+
const timeout = toolInput.timeout || 5;
|
|
172
|
+
|
|
173
|
+
const escapedData = data.replace(/'/g, "'\\''");
|
|
174
|
+
const command =
|
|
175
|
+
`python3 -c "` +
|
|
176
|
+
`import socket; ` +
|
|
177
|
+
`s=socket.socket(); ` +
|
|
178
|
+
`s.settimeout(${timeout}); ` +
|
|
179
|
+
`s.connect(('${host}',${port})); ` +
|
|
180
|
+
`s.sendall(b'${escapedData}'); ` +
|
|
181
|
+
`print(s.recv(4096).decode('utf-8','replace')); ` +
|
|
182
|
+
`s.close()"`;
|
|
183
|
+
debug(`send_payload: ${command}`);
|
|
184
|
+
const [exitCode, stdout, stderr] = context.execTool(command);
|
|
185
|
+
|
|
186
|
+
const parts = [];
|
|
187
|
+
if (stdout.trim()) parts.push(stdout);
|
|
188
|
+
if (stderr.trim()) parts.push(`STDERR:\n${stderr}`);
|
|
189
|
+
parts.push(`EXIT CODE: ${exitCode}`);
|
|
190
|
+
|
|
191
|
+
return parts.join('\n');
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// ---------------------------------------------------------------------------
|
|
195
|
+
// Tool schemas (Anthropic format)
|
|
196
|
+
// ---------------------------------------------------------------------------
|
|
197
|
+
|
|
198
|
+
const _RED_SANDBOX_EXEC_SCHEMA = {
|
|
199
|
+
name: 'sandbox_exec',
|
|
200
|
+
description:
|
|
201
|
+
'Execute a shell command in the security sandbox. ' +
|
|
202
|
+
'Use this to run any tool: curl, nmap, sqlmap, python3, grep, etc. ' +
|
|
203
|
+
'Returns the command\'s stdout, stderr, and exit code.',
|
|
204
|
+
input_schema: {
|
|
205
|
+
type: 'object',
|
|
206
|
+
properties: {
|
|
207
|
+
command: {
|
|
208
|
+
type: 'string',
|
|
209
|
+
description: 'Shell command to execute (passed to /bin/sh -c)',
|
|
210
|
+
},
|
|
211
|
+
},
|
|
212
|
+
required: ['command'],
|
|
213
|
+
},
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
const _RED_HTTP_REQUEST_SCHEMA = {
|
|
217
|
+
name: 'http_request',
|
|
218
|
+
description:
|
|
219
|
+
'Make an HTTP request to a URL. Convenience wrapper around curl. ' +
|
|
220
|
+
'Use for GET/POST/PUT requests with optional headers and body.',
|
|
221
|
+
input_schema: {
|
|
222
|
+
type: 'object',
|
|
223
|
+
properties: {
|
|
224
|
+
url: {
|
|
225
|
+
type: 'string',
|
|
226
|
+
description: 'Target URL (e.g., http://web:80/login)',
|
|
227
|
+
},
|
|
228
|
+
method: {
|
|
229
|
+
type: 'string',
|
|
230
|
+
description: 'HTTP method (GET, POST, PUT, DELETE)',
|
|
231
|
+
default: 'GET',
|
|
232
|
+
},
|
|
233
|
+
headers: {
|
|
234
|
+
type: 'object',
|
|
235
|
+
description: 'HTTP headers as key-value pairs',
|
|
236
|
+
additionalProperties: { type: 'string' },
|
|
237
|
+
},
|
|
238
|
+
body: {
|
|
239
|
+
type: 'string',
|
|
240
|
+
description: 'Request body (for POST/PUT)',
|
|
241
|
+
},
|
|
242
|
+
},
|
|
243
|
+
required: ['url'],
|
|
244
|
+
},
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
const _RED_READ_FILE_SCHEMA = {
|
|
248
|
+
name: 'read_file',
|
|
249
|
+
description:
|
|
250
|
+
'Read the contents of a file inside the sandbox. ' +
|
|
251
|
+
'Use to inspect downloaded files, configuration, source code, etc.',
|
|
252
|
+
input_schema: {
|
|
253
|
+
type: 'object',
|
|
254
|
+
properties: {
|
|
255
|
+
path: {
|
|
256
|
+
type: 'string',
|
|
257
|
+
description: 'Absolute or relative file path inside the sandbox',
|
|
258
|
+
},
|
|
259
|
+
},
|
|
260
|
+
required: ['path'],
|
|
261
|
+
},
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
const _NET_PORT_SCAN_SCHEMA = {
|
|
265
|
+
name: 'port_scan',
|
|
266
|
+
description:
|
|
267
|
+
'Scan a target for open ports using nmap. ' +
|
|
268
|
+
'Returns service version info for discovered ports.',
|
|
269
|
+
input_schema: {
|
|
270
|
+
type: 'object',
|
|
271
|
+
properties: {
|
|
272
|
+
target: {
|
|
273
|
+
type: 'string',
|
|
274
|
+
description: 'Hostname or IP address to scan',
|
|
275
|
+
},
|
|
276
|
+
ports: {
|
|
277
|
+
type: 'string',
|
|
278
|
+
description:
|
|
279
|
+
"Port range in nmap syntax (e.g. '1-1000', '21,22,80'). Defaults to '1-1000'.",
|
|
280
|
+
},
|
|
281
|
+
},
|
|
282
|
+
required: ['target'],
|
|
283
|
+
},
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
const _NET_CONNECT_TCP_SCHEMA = {
|
|
287
|
+
name: 'connect_tcp',
|
|
288
|
+
description:
|
|
289
|
+
'Open a raw TCP connection and return the banner/greeting. ' +
|
|
290
|
+
'Useful for identifying services (FTP, SSH, SMTP, etc.).',
|
|
291
|
+
input_schema: {
|
|
292
|
+
type: 'object',
|
|
293
|
+
properties: {
|
|
294
|
+
host: {
|
|
295
|
+
type: 'string',
|
|
296
|
+
description: 'Hostname or IP address',
|
|
297
|
+
},
|
|
298
|
+
port: {
|
|
299
|
+
type: 'integer',
|
|
300
|
+
description: 'TCP port number',
|
|
301
|
+
},
|
|
302
|
+
timeout: {
|
|
303
|
+
type: 'integer',
|
|
304
|
+
description: 'Connection timeout in seconds (default: 5)',
|
|
305
|
+
},
|
|
306
|
+
},
|
|
307
|
+
required: ['host', 'port'],
|
|
308
|
+
},
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
const _NET_SEND_PAYLOAD_SCHEMA = {
|
|
312
|
+
name: 'send_payload',
|
|
313
|
+
description:
|
|
314
|
+
'Send a payload over TCP and return the response. ' +
|
|
315
|
+
'Use for interacting with network services (FTP commands, ' +
|
|
316
|
+
'SMTP commands, custom protocols, etc.).',
|
|
317
|
+
input_schema: {
|
|
318
|
+
type: 'object',
|
|
319
|
+
properties: {
|
|
320
|
+
host: {
|
|
321
|
+
type: 'string',
|
|
322
|
+
description: 'Hostname or IP address',
|
|
323
|
+
},
|
|
324
|
+
port: {
|
|
325
|
+
type: 'integer',
|
|
326
|
+
description: 'TCP port number',
|
|
327
|
+
},
|
|
328
|
+
data: {
|
|
329
|
+
type: 'string',
|
|
330
|
+
description: 'Payload string to send',
|
|
331
|
+
},
|
|
332
|
+
timeout: {
|
|
333
|
+
type: 'integer',
|
|
334
|
+
description: 'Socket timeout in seconds (default: 5)',
|
|
335
|
+
},
|
|
336
|
+
},
|
|
337
|
+
required: ['host', 'port', 'data'],
|
|
338
|
+
},
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
// ---------------------------------------------------------------------------
|
|
342
|
+
// System prompts
|
|
343
|
+
// ---------------------------------------------------------------------------
|
|
344
|
+
|
|
345
|
+
const _RED_SYSTEM_PROMPT = `\
|
|
346
|
+
You are an expert security researcher performing an authorized penetration test \
|
|
347
|
+
against a web application. Your goal is to find and extract the flag.
|
|
348
|
+
|
|
349
|
+
The flag format is FLAG{hex_string} (e.g., FLAG{a1b2c3d4e5f6...}).
|
|
350
|
+
|
|
351
|
+
## Target
|
|
352
|
+
URL: {target_url}
|
|
353
|
+
Description: {challenge_description}
|
|
354
|
+
|
|
355
|
+
## Strategy
|
|
356
|
+
1. First, explore the target with HTTP requests to understand the application
|
|
357
|
+
2. Read HTML source, headers, error messages for clues about the technology stack
|
|
358
|
+
3. Based on clues, identify the vulnerability class (SQLi, SSTI, XSS, SSRF, etc.)
|
|
359
|
+
4. Craft exploitation payloads step by step — test each one and analyze the response
|
|
360
|
+
5. When exploitation succeeds, look for the flag in the response
|
|
361
|
+
6. The flag is often stored in a file like /flag, /flag.txt, or an environment variable
|
|
362
|
+
|
|
363
|
+
## Important Execution Notes
|
|
364
|
+
- Use sandbox_exec with curl commands for full control over HTTP requests
|
|
365
|
+
- Example: sandbox_exec with command: curl -s -H "Host: localhost" "http://web:8000/"
|
|
366
|
+
- If you get "DisallowedHost" or 400 errors, add -H "Host: localhost" to curl
|
|
367
|
+
- CRITICAL: Shell braces {{ and }} are eaten by /bin/sh. Always URL-encode template \
|
|
368
|
+
injection payloads in the URL. For example, use %7B%7B7*7%7D%7D instead of {{7*7}}.
|
|
369
|
+
- For SSTI: try URL-encoded payloads like %7B%7B7*7%7D%7D, %7B%7BFLAG%7D%7D, \
|
|
370
|
+
%7B%7Bconfig%7D%7D, %7B%7Bsettings%7D%7D, %7B%7Brequest.META%7D%7D
|
|
371
|
+
- XBOW challenges typically pass the flag as a template context variable named FLAG — \
|
|
372
|
+
always try %7B%7BFLAG%7D%7D (URL-encoded {{FLAG}}) early in your SSTI attempts
|
|
373
|
+
- The flag is often in a template variable like FLAG, or in /flag.txt, or an env var
|
|
374
|
+
- You have curl, nmap, and python3 available in your sandbox
|
|
375
|
+
- Be persistent — if a payload returns 500, try a different approach
|
|
376
|
+
- NEVER give up after just a few attempts. Keep trying different payloads.
|
|
377
|
+
- When you find the flag (FLAG{...}), state it clearly in your response
|
|
378
|
+
`;
|
|
379
|
+
|
|
380
|
+
const _RED_NETWORK_SYSTEM_PROMPT = `\
|
|
381
|
+
You are an expert security researcher performing an authorized penetration test \
|
|
382
|
+
against a network service. Your goal is to find and extract the flag.
|
|
383
|
+
|
|
384
|
+
The flag format is FLAG{hex_string} (e.g., FLAG{a1b2c3d4e5f6...}).
|
|
385
|
+
|
|
386
|
+
## Target
|
|
387
|
+
Host: {target_host}
|
|
388
|
+
Port: {target_port}
|
|
389
|
+
Description: {challenge_description}
|
|
390
|
+
|
|
391
|
+
## Available Tools
|
|
392
|
+
- **port_scan**: Scan the target for open ports and service versions (nmap)
|
|
393
|
+
- **connect_tcp**: Open a TCP connection to grab service banners
|
|
394
|
+
- **send_payload**: Send arbitrary data over TCP and read the response
|
|
395
|
+
- **sandbox_exec**: Run any shell command (python3, curl, nmap, netcat, etc.)
|
|
396
|
+
- **read_file**: Read files inside the sandbox
|
|
397
|
+
|
|
398
|
+
## Strategy
|
|
399
|
+
1. Use port_scan to discover open ports and running services on the target
|
|
400
|
+
2. Use connect_tcp to grab banners and identify service versions
|
|
401
|
+
3. Identify the service type: FTP, SSH, Telnet, SMTP, custom, etc.
|
|
402
|
+
4. Based on the service, try common exploitation patterns:
|
|
403
|
+
- **FTP**: anonymous login, directory traversal, SITE commands, known CVEs
|
|
404
|
+
- **SSH**: default credentials, key-based auth weaknesses
|
|
405
|
+
- **Telnet**: default credentials, command injection
|
|
406
|
+
- **SMTP**: open relay, VRFY enumeration, header injection
|
|
407
|
+
- **Custom**: protocol fuzzing, buffer overflow, format string
|
|
408
|
+
5. Use send_payload for direct protocol interaction (e.g., FTP commands)
|
|
409
|
+
6. Use sandbox_exec for complex exploits (python3 scripts, metasploit patterns)
|
|
410
|
+
7. Look for FLAG{hex} in service responses, files (/flag, /flag.txt), or env vars
|
|
411
|
+
|
|
412
|
+
## Important Execution Notes
|
|
413
|
+
- Start with port_scan to understand what's running
|
|
414
|
+
- Use connect_tcp to see the service banner before crafting exploits
|
|
415
|
+
- For FTP: try "USER anonymous\\r\\nPASS \\r\\n" via send_payload or sandbox_exec
|
|
416
|
+
- For multi-step protocols, use sandbox_exec with a python3 script
|
|
417
|
+
- The flag may be in a file accessible through the service, not just in banners
|
|
418
|
+
- Be persistent — try multiple approaches if the first doesn't work
|
|
419
|
+
- When you find the flag (FLAG{...}), state it clearly in your response
|
|
420
|
+
`;
|
|
421
|
+
|
|
422
|
+
const _RED_BINARY_SYSTEM_PROMPT = `\
|
|
423
|
+
You are an expert security researcher performing an authorized penetration test \
|
|
424
|
+
against a binary service. Your goal is to find and extract the flag.
|
|
425
|
+
|
|
426
|
+
The flag format is FLAG{hex_string} (e.g., FLAG{a1b2c3d4e5f6...}).
|
|
427
|
+
|
|
428
|
+
## Target
|
|
429
|
+
Host: {target_host}
|
|
430
|
+
Port: {target_port}
|
|
431
|
+
Description: {challenge_description}
|
|
432
|
+
|
|
433
|
+
## Available Tools
|
|
434
|
+
- **sandbox_exec**: Run any shell command (python3, gcc, objdump, gdb, etc.)
|
|
435
|
+
- **read_file**: Read files inside the sandbox
|
|
436
|
+
- **send_payload**: Send arbitrary data over TCP and read the response
|
|
437
|
+
|
|
438
|
+
## Setup
|
|
439
|
+
Install useful tools first:
|
|
440
|
+
\`\`\`
|
|
441
|
+
apt-get update && apt-get install -y binutils file netcat-openbsd
|
|
442
|
+
\`\`\`
|
|
443
|
+
|
|
444
|
+
## Binary Exploitation Strategy
|
|
445
|
+
1. **Probe the service**: Connect to the target and observe its behavior. Note any \
|
|
446
|
+
prompts or output format.
|
|
447
|
+
2. **Identify the vulnerability class**: Send format string specifiers (%p, %x, %s) \
|
|
448
|
+
as input. If the service reflects hex addresses or crashes differently, it is a \
|
|
449
|
+
format string vulnerability.
|
|
450
|
+
3. **Leak stack data**: Send a sequence of \`%p.\` markers to dump stack values. \
|
|
451
|
+
Look for hex patterns that correspond to ASCII text (especially the flag).
|
|
452
|
+
4. **Targeted leaks**: Use positional format specifiers like \`%7$p\`, \`%8$p\`, etc. \
|
|
453
|
+
to read specific stack slots.
|
|
454
|
+
5. **Decode the flag**: Stack values are typically 8 bytes in little-endian order. \
|
|
455
|
+
Convert hex to ASCII and reverse the byte order within each 8-byte word.
|
|
456
|
+
|
|
457
|
+
## Format String Reference
|
|
458
|
+
- \`%p\` — print pointer (hex value from stack), walks the stack one argument at a time
|
|
459
|
+
- \`%N$p\` — print the Nth argument from the stack (e.g., \`%7$p\` reads the 7th)
|
|
460
|
+
- \`%s\` — dereference a pointer on the stack and print the string it points to
|
|
461
|
+
- \`%N$s\` — dereference the Nth argument as a string pointer
|
|
462
|
+
|
|
463
|
+
## Worked Example: Hex-to-ASCII Decoding
|
|
464
|
+
If \`%7$p\` returns \`0x4c467b47414c46\` (8 bytes), decode it:
|
|
465
|
+
\`\`\`python
|
|
466
|
+
import struct
|
|
467
|
+
val = 0x4c467b47414c46
|
|
468
|
+
raw = struct.pack('<Q', val) # little-endian 8-byte word
|
|
469
|
+
print(raw) # b'FLAG{L'
|
|
470
|
+
\`\`\`
|
|
471
|
+
Concatenate decoded words from consecutive stack slots until you have the full flag.
|
|
472
|
+
|
|
473
|
+
## Important Execution Notes
|
|
474
|
+
- **Always use python3 scripts** for exploit payloads — avoid shell escaping issues \
|
|
475
|
+
with \`$\` and \`%\` characters in format strings.
|
|
476
|
+
- Example: write a python3 script that connects via socket, sends the format string, \
|
|
477
|
+
and parses the response.
|
|
478
|
+
- The flag is stored somewhere accessible to the binary (a file it reads, or on \
|
|
479
|
+
the stack). Leak stack contents to find it.
|
|
480
|
+
- Be systematic: probe offsets %1$p through %20$p and look for hex values that \
|
|
481
|
+
decode to printable ASCII in the FLAG{...} pattern.
|
|
482
|
+
- When you find the flag (FLAG{...}), state it clearly in your response.
|
|
483
|
+
`;
|
|
484
|
+
|
|
485
|
+
// ---------------------------------------------------------------------------
|
|
486
|
+
// Factory
|
|
487
|
+
// ---------------------------------------------------------------------------
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* Build a RED-mode ModeAgentConfig, selecting tools by challenge category.
|
|
491
|
+
*
|
|
492
|
+
* @param {string} [category='web'] - 'web', 'network', or 'binary'
|
|
493
|
+
* @returns {ModeAgentConfig}
|
|
494
|
+
*/
|
|
495
|
+
export function _makeRedConfig(category = 'web') {
|
|
496
|
+
const reg = new ToolRegistry();
|
|
497
|
+
|
|
498
|
+
let systemPrompt;
|
|
499
|
+
|
|
500
|
+
if (category === 'network') {
|
|
501
|
+
reg.register('port_scan', _NET_PORT_SCAN_SCHEMA, _netPortScan);
|
|
502
|
+
reg.register('connect_tcp', _NET_CONNECT_TCP_SCHEMA, _netConnectTcp);
|
|
503
|
+
reg.register('send_payload', _NET_SEND_PAYLOAD_SCHEMA, _netSendPayload);
|
|
504
|
+
reg.register('sandbox_exec', _RED_SANDBOX_EXEC_SCHEMA, _redSandboxExec);
|
|
505
|
+
reg.register('read_file', _RED_READ_FILE_SCHEMA, _redReadFile);
|
|
506
|
+
systemPrompt = _RED_NETWORK_SYSTEM_PROMPT;
|
|
507
|
+
debug('_makeRedConfig: category=network, 5 tools registered');
|
|
508
|
+
} else if (category === 'binary') {
|
|
509
|
+
reg.register('sandbox_exec', _RED_SANDBOX_EXEC_SCHEMA, _redSandboxExec);
|
|
510
|
+
reg.register('read_file', _RED_READ_FILE_SCHEMA, _redReadFile);
|
|
511
|
+
reg.register('send_payload', _NET_SEND_PAYLOAD_SCHEMA, _netSendPayload);
|
|
512
|
+
systemPrompt = _RED_BINARY_SYSTEM_PROMPT;
|
|
513
|
+
debug('_makeRedConfig: category=binary, 3 tools registered');
|
|
514
|
+
} else {
|
|
515
|
+
reg.register('sandbox_exec', _RED_SANDBOX_EXEC_SCHEMA, _redSandboxExec);
|
|
516
|
+
reg.register('http_request', _RED_HTTP_REQUEST_SCHEMA, _redHttpRequest);
|
|
517
|
+
reg.register('read_file', _RED_READ_FILE_SCHEMA, _redReadFile);
|
|
518
|
+
systemPrompt = _RED_SYSTEM_PROMPT;
|
|
519
|
+
debug('_makeRedConfig: category=web, 3 tools registered');
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
return new ModeAgentConfig({
|
|
523
|
+
mode: 'RED',
|
|
524
|
+
toolRegistry: reg,
|
|
525
|
+
systemPromptTemplate: systemPrompt,
|
|
526
|
+
validator: new FlagValidator(),
|
|
527
|
+
maxTurns: 30,
|
|
528
|
+
requiresSandbox: true,
|
|
529
|
+
completionCheck: _redCompletionCheck,
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
// ---------------------------------------------------------------------------
|
|
534
|
+
// Handoff filter — strips raw exploit payloads, keeps finding summaries
|
|
535
|
+
// ---------------------------------------------------------------------------
|
|
536
|
+
|
|
537
|
+
/** @type {import('../handoff.js').HandoffFilter} */
|
|
538
|
+
export const RED_HANDOFF_FILTER = {
|
|
539
|
+
transform: (context) => {
|
|
540
|
+
const filtered = {};
|
|
541
|
+
// Keep structured summaries
|
|
542
|
+
if (context.reason) filtered.reason = context.reason;
|
|
543
|
+
if (context.priorOutput) {
|
|
544
|
+
// Strip raw payloads: long hex strings (0x + 8+ hex chars) and \x escape sequences
|
|
545
|
+
filtered.priorOutput = context.priorOutput
|
|
546
|
+
.replace(/0x[0-9a-f]{8,}/gi, '[REDACTED:payload]')
|
|
547
|
+
.replace(/(\\x[0-9a-f]{2}){3,}/gi, '[REDACTED:payload]')
|
|
548
|
+
.replace(/[\x00-\x08\x0e-\x1f]/g, '');
|
|
549
|
+
}
|
|
550
|
+
if (context.priorFindings) {
|
|
551
|
+
// Keep finding metadata, strip exploit details
|
|
552
|
+
const { exploit, payload, shellcode, ...safe } = context.priorFindings;
|
|
553
|
+
filtered.priorFindings = safe;
|
|
554
|
+
}
|
|
555
|
+
return filtered;
|
|
556
|
+
},
|
|
557
|
+
acceptsFrom: [], // RED accepts handoffs from any mode
|
|
558
|
+
};
|
|
559
|
+
|
|
560
|
+
// ---------------------------------------------------------------------------
|
|
561
|
+
// Registration
|
|
562
|
+
// ---------------------------------------------------------------------------
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
* Register RED mode with the given registerMode function.
|
|
566
|
+
* @param {Function} registerMode
|
|
567
|
+
*/
|
|
568
|
+
export function register(registerMode) {
|
|
569
|
+
registerMode('RED', _makeRedConfig);
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
// ---------------------------------------------------------------------------
|
|
573
|
+
// Exports for testing
|
|
574
|
+
// ---------------------------------------------------------------------------
|
|
575
|
+
|
|
576
|
+
export {
|
|
577
|
+
_redCompletionCheck,
|
|
578
|
+
_redSandboxExec,
|
|
579
|
+
_redHttpRequest,
|
|
580
|
+
_redReadFile,
|
|
581
|
+
_netPortScan,
|
|
582
|
+
_netConnectTcp,
|
|
583
|
+
_netSendPayload,
|
|
584
|
+
RED_HANDOFF_FILTER,
|
|
585
|
+
};
|