neuralos 3.2.13 → 3.2.15
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/gybackend.cjs +218 -48
- package/package.json +5 -42
- package/plugins/mitmproxy-bridge/index.d.mts +20 -0
- package/plugins/mitmproxy-bridge/index.mjs +256 -0
- package/plugins/mitmproxy-bridge/plugin.json +17 -0
- package/plugins/netexec-bridge/index.d.mts +28 -0
- package/plugins/netexec-bridge/index.mjs +325 -0
- package/plugins/netexec-bridge/plugin.json +20 -0
- package/plugins/promptfoo-redteam/index.d.mts +15 -0
- package/plugins/promptfoo-redteam/index.mjs +277 -0
- package/plugins/promptfoo-redteam/plugin.json +16 -0
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mitmproxy-bridge — mitmproxy traffic capture for RTerm.
|
|
3
|
+
*
|
|
4
|
+
* Two use modes:
|
|
5
|
+
* 1. Agent self-inspection — capture what the LLM actually sends over the
|
|
6
|
+
* wire (verify no secrets leak in prompts, inspect provider requests).
|
|
7
|
+
* 2. Authorized interception — capture traffic on hosts you administer,
|
|
8
|
+
* following the APerf deploy pattern (run on the RTerm host as a sidecar).
|
|
9
|
+
*
|
|
10
|
+
* Pure + injectable: process spawning and file reads are injected; command
|
|
11
|
+
* building, flow parsing, and secret detection are pure and fully testable.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
// --- Pure: build the mitmdump command line ---
|
|
15
|
+
|
|
16
|
+
export function buildMitmCommand(opts) {
|
|
17
|
+
const {
|
|
18
|
+
mode = 'regular', // regular | reverse
|
|
19
|
+
listenPort = 8080,
|
|
20
|
+
upstreamTarget, // for reverse mode: the upstream host:port
|
|
21
|
+
flowsFile, // where to write the flow file
|
|
22
|
+
filterExpr, // optional mitmproxy filter expression
|
|
23
|
+
extraArgs = [],
|
|
24
|
+
} = opts || {}
|
|
25
|
+
|
|
26
|
+
if (!flowsFile) throw new Error('buildMitmCommand needs flowsFile')
|
|
27
|
+
const args = ['mitmdump']
|
|
28
|
+
|
|
29
|
+
if (mode === 'reverse') {
|
|
30
|
+
if (!upstreamTarget) throw new Error('reverse mode needs upstreamTarget')
|
|
31
|
+
args.push('--mode', `reverse:${upstreamTarget}`)
|
|
32
|
+
} else {
|
|
33
|
+
args.push('--mode', 'regular')
|
|
34
|
+
}
|
|
35
|
+
args.push('--listen-port', String(listenPort))
|
|
36
|
+
args.push('-w', flowsFile)
|
|
37
|
+
if (filterExpr) args.push('--set', `flow_detail=0`, '--set', `intercept=${filterExpr}`)
|
|
38
|
+
for (const a of extraArgs) args.push(String(a))
|
|
39
|
+
return { cmd: 'mitmdump', args }
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// --- Pure: parse a mitmproxy flows file (JSON lines from mitmdump --flow-detail) ---
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Parse mitmproxy flow records (the JSON array/dump format) into a compact
|
|
46
|
+
* summary: per-host request counts, methods, status codes, and content types.
|
|
47
|
+
*/
|
|
48
|
+
export function parseFlows(rawFlows) {
|
|
49
|
+
const summary = {
|
|
50
|
+
total: 0,
|
|
51
|
+
byHost: {},
|
|
52
|
+
byStatus: {},
|
|
53
|
+
requests: [],
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let flows = rawFlows
|
|
57
|
+
if (typeof rawFlows === 'string') {
|
|
58
|
+
try { flows = JSON.parse(rawFlows) } catch { return { ...summary, error: 'unparseable flows' } }
|
|
59
|
+
}
|
|
60
|
+
if (!Array.isArray(flows)) return { ...summary, error: 'flows is not an array' }
|
|
61
|
+
|
|
62
|
+
for (const f of flows) {
|
|
63
|
+
const host = String(f?.request?.host ?? f?.host ?? 'unknown')
|
|
64
|
+
const method = String(f?.request?.method ?? f?.method ?? '?')
|
|
65
|
+
const status = f?.response?.status_code ?? f?.status_code
|
|
66
|
+
const path = String(f?.request?.path ?? f?.path ?? '')
|
|
67
|
+
const contentType = String(f?.response?.headers?.['content-type'] ?? '')
|
|
68
|
+
|
|
69
|
+
summary.total += 1
|
|
70
|
+
const byHost = summary.byHost[host] || (summary.byHost[host] = { count: 0, methods: {} })
|
|
71
|
+
byHost.count += 1
|
|
72
|
+
byHost.methods[method] = (byHost.methods[method] || 0) + 1
|
|
73
|
+
|
|
74
|
+
const statusKey = status !== undefined && status !== null ? String(status) : 'no-response'
|
|
75
|
+
summary.byStatus[statusKey] = (summary.byStatus[statusKey] || 0) + 1
|
|
76
|
+
|
|
77
|
+
if (summary.requests.length < 100) {
|
|
78
|
+
summary.requests.push({ host, method, path: path.slice(0, 120), status: statusKey, contentType: contentType.slice(0, 60) })
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return summary
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// --- Pure: detect secret-looking content in captured request bodies ---
|
|
86
|
+
|
|
87
|
+
const SECRET_PATTERNS = [
|
|
88
|
+
{ re: /\bsk-[A-Za-z0-9]{20,}/g, name: 'openai-style-key' },
|
|
89
|
+
{ re: /\bghp_[A-Za-z0-9]{30,}/g, name: 'github-token' },
|
|
90
|
+
{ re: /\bAKIA[A-Z0-9]{16}\b/g, name: 'aws-access-key' },
|
|
91
|
+
{ re: /\bxox[baprs]-[A-Za-z0-9-]{10,}/g, name: 'slack-token' },
|
|
92
|
+
// JWT: header/payload/signature. Real JWT headers are commonly exactly 20
|
|
93
|
+
// chars total (17 after the "eyJ" prefix) — {17,} catches those; {20,} was
|
|
94
|
+
// an FN that missed short-header JWTs.
|
|
95
|
+
{ re: /\beyJ[A-Za-z0-9_-]{17,}\.eyJ[A-Za-z0-9_-]{20,}\.[A-Za-z0-9_-]{10,}\b/g, name: 'jwt' },
|
|
96
|
+
{ re: /\b(?:password|passwd|pwd|secret|token|api[_-]?key)\s*[:=]\s*['"]?[^\s'"]{8,}/gi, name: 'credential-assignment' },
|
|
97
|
+
]
|
|
98
|
+
|
|
99
|
+
export function detectSecrets(text) {
|
|
100
|
+
const findings = []
|
|
101
|
+
if (!text || typeof text !== 'string') return findings
|
|
102
|
+
for (const { re, name } of SECRET_PATTERNS) {
|
|
103
|
+
const matches = text.match(re)
|
|
104
|
+
if (matches) {
|
|
105
|
+
findings.push({
|
|
106
|
+
kind: name,
|
|
107
|
+
count: matches.length,
|
|
108
|
+
// redact: show only a prefix so the secret itself never lands in output
|
|
109
|
+
preview: matches[0].slice(0, 8) + '…',
|
|
110
|
+
})
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return findings
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// --- Pure: which hosts are allowed to be intercepted ---
|
|
117
|
+
|
|
118
|
+
export function isHostAllowed(host, allowlist) {
|
|
119
|
+
if (!Array.isArray(allowlist) || allowlist.length === 0) return false
|
|
120
|
+
const h = String(host || '').toLowerCase()
|
|
121
|
+
return allowlist.some((a) => {
|
|
122
|
+
const pat = String(a || '').toLowerCase()
|
|
123
|
+
if (pat === h) return true
|
|
124
|
+
// allow "*.example.com" style suffix matching
|
|
125
|
+
if (pat.startsWith('*.')) return h.endsWith(pat.slice(1))
|
|
126
|
+
return false
|
|
127
|
+
})
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// --- Plugin entry ---
|
|
131
|
+
|
|
132
|
+
export function register(ctx) {
|
|
133
|
+
const { registerTool, registerPanel, log, spawnProcess } = ctx
|
|
134
|
+
|
|
135
|
+
let mitmChild = null
|
|
136
|
+
let lastSummary = null
|
|
137
|
+
|
|
138
|
+
registerTool({
|
|
139
|
+
name: 'mitm_start',
|
|
140
|
+
description: 'Start a mitmproxy capture. Mode "regular" listens as an HTTP proxy on listenPort; mode "reverse" forwards to upstreamTarget. Flows are written to a file for later analysis. Only start captures for hosts you are authorized to intercept.',
|
|
141
|
+
params: {
|
|
142
|
+
mode: { type: 'string', description: '"regular" (proxy) or "reverse" (forward to upstreamTarget)' },
|
|
143
|
+
listenPort: { type: 'number', description: 'Port to listen on (default 8080)' },
|
|
144
|
+
upstreamTarget: { type: 'string', description: 'For reverse mode: host:port to forward to' },
|
|
145
|
+
allowlist: { type: 'array', description: 'Hosts/patterns authorized for interception, e.g. ["api.example.com", "*.internal"]' },
|
|
146
|
+
},
|
|
147
|
+
handler: async (params) => {
|
|
148
|
+
if (typeof spawnProcess !== 'function') {
|
|
149
|
+
return { error: 'mitmproxy-bridge requires process spawning, which is not available in this RTerm build.' }
|
|
150
|
+
}
|
|
151
|
+
if (mitmChild) {
|
|
152
|
+
return { error: 'A mitmproxy capture is already running. Stop it with mitm_stop first.' }
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const mode = params?.mode === 'reverse' ? 'reverse' : 'regular'
|
|
156
|
+
if (mode === 'reverse' && !params?.upstreamTarget) {
|
|
157
|
+
return { error: 'reverse mode needs upstreamTarget (host:port).' }
|
|
158
|
+
}
|
|
159
|
+
// Governance: an allowlist must be provided — no unbounded interception.
|
|
160
|
+
const allowlist = Array.isArray(params?.allowlist) ? params.allowlist : []
|
|
161
|
+
if (mode === 'reverse' && allowlist.length === 0) {
|
|
162
|
+
return { error: 'reverse mode requires an allowlist of authorized hosts. Unbounded interception is not permitted.' }
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const fs = await import('node:fs')
|
|
166
|
+
const os = await import('node:os')
|
|
167
|
+
const path = await import('node:path')
|
|
168
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'mitm-rterm-'))
|
|
169
|
+
const flowsFile = path.join(dir, 'flows.mitm')
|
|
170
|
+
|
|
171
|
+
let plan
|
|
172
|
+
try {
|
|
173
|
+
plan = buildMitmCommand({
|
|
174
|
+
mode,
|
|
175
|
+
listenPort: params?.listenPort,
|
|
176
|
+
upstreamTarget: params?.upstreamTarget,
|
|
177
|
+
flowsFile,
|
|
178
|
+
})
|
|
179
|
+
} catch (e) {
|
|
180
|
+
return { error: e?.message ?? String(e) }
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
log(`[mitmproxy] starting ${mode} capture on :${params?.listenPort ?? 8080} → ${flowsFile}`)
|
|
184
|
+
try {
|
|
185
|
+
mitmChild = spawnProcess(plan.cmd, plan.args, { stdio: 'pipe', detached: false })
|
|
186
|
+
} catch (e) {
|
|
187
|
+
return { error: `failed to spawn mitmdump: ${e?.message ?? e}. Is mitmproxy installed (pip install mitmproxy / brew install mitmproxy)?` }
|
|
188
|
+
}
|
|
189
|
+
if (!mitmChild) return { error: 'spawnProcess returned no child process' }
|
|
190
|
+
|
|
191
|
+
return {
|
|
192
|
+
started: true,
|
|
193
|
+
mode,
|
|
194
|
+
listenPort: params?.listenPort ?? 8080,
|
|
195
|
+
flowsFile,
|
|
196
|
+
note: mode === 'regular'
|
|
197
|
+
? 'Configure clients to use this host as HTTP proxy. Stop with mitm_stop.'
|
|
198
|
+
: `Point clients at :${params?.listenPort ?? 8080}; traffic forwards to ${params?.upstreamTarget}. Stop with mitm_stop.`,
|
|
199
|
+
}
|
|
200
|
+
},
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
registerTool({
|
|
204
|
+
name: 'mitm_stop',
|
|
205
|
+
description: 'Stop the running mitmproxy capture and summarize the captured flows.',
|
|
206
|
+
params: {},
|
|
207
|
+
handler: async () => {
|
|
208
|
+
if (!mitmChild) return { error: 'No mitmproxy capture is running.' }
|
|
209
|
+
try { mitmChild.kill?.() } catch { /* ignore */ }
|
|
210
|
+
mitmChild = null
|
|
211
|
+
return { stopped: true }
|
|
212
|
+
},
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
registerTool({
|
|
216
|
+
name: 'mitm_flows',
|
|
217
|
+
description: 'Summarize captured flows from a mitmproxy flow dump: per-host counts, methods, status codes, and secret detection on request bodies. Use mitmdump -nr <flowsFile> --flow-detail 3 to export, or pass pre-parsed flows.',
|
|
218
|
+
params: {
|
|
219
|
+
flows: { type: 'array', description: 'Pre-parsed flow records [{request:{host,method,path}, response:{status_code}}]' },
|
|
220
|
+
},
|
|
221
|
+
handler: async (params) => {
|
|
222
|
+
const flows = Array.isArray(params?.flows) ? params.flows : []
|
|
223
|
+
if (flows.length === 0) {
|
|
224
|
+
return { error: 'No flows given. Export flows from the capture first (mitmdump -nr <file>) and pass the parsed records.' }
|
|
225
|
+
}
|
|
226
|
+
const summary = parseFlows(flows)
|
|
227
|
+
|
|
228
|
+
// Secret scan across request paths + any body text present in the records.
|
|
229
|
+
const textBlob = flows
|
|
230
|
+
.map((f) => `${f?.request?.path ?? ''} ${typeof f?.request?.body === 'string' ? f.request.body : ''}`)
|
|
231
|
+
.join('\n')
|
|
232
|
+
.slice(0, 200_000)
|
|
233
|
+
const secrets = detectSecrets(textBlob)
|
|
234
|
+
|
|
235
|
+
lastSummary = { summary, secrets }
|
|
236
|
+
log(`[mitmproxy] summarized ${summary.total} flows across ${Object.keys(summary.byHost).length} host(s); ${secrets.length} secret pattern(s) found`)
|
|
237
|
+
return { summary, secrets }
|
|
238
|
+
},
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
registerPanel({
|
|
242
|
+
name: 'mitmproxy-flows',
|
|
243
|
+
title: 'Traffic Capture',
|
|
244
|
+
render: async () => {
|
|
245
|
+
if (!lastSummary) return '<div class="panel-section"><h3>Traffic Capture</h3><p>No flows analyzed yet. Use mitm_start / mitm_flows.</p></div>'
|
|
246
|
+
const hostRows = Object.entries(lastSummary.summary.byHost)
|
|
247
|
+
.slice(0, 10)
|
|
248
|
+
.map(([h, s]) => `<tr><td>${h}</td><td>${s.count}</td></tr>`)
|
|
249
|
+
.join('')
|
|
250
|
+
return `<div class="panel-section"><h3>Traffic Capture — ${lastSummary.summary.total} flows</h3>
|
|
251
|
+
<table><tr><th>host</th><th>requests</th></tr>${hostRows}</table>
|
|
252
|
+
${lastSummary.secrets.length ? `<p class="warn">⚠ ${lastSummary.secrets.length} secret pattern(s) detected</p>` : '<p>No secrets detected</p>'}
|
|
253
|
+
</div>`
|
|
254
|
+
},
|
|
255
|
+
})
|
|
256
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mitmproxy-bridge",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "mitmproxy traffic capture for RTerm — agent self-inspection (what the LLM sends over the wire) and authorized interception on administered hosts. Flow summaries + secret-pattern detection on captured bodies.",
|
|
5
|
+
"entry": "index.mjs",
|
|
6
|
+
"tools": [
|
|
7
|
+
"mitm_start",
|
|
8
|
+
"mitm_stop",
|
|
9
|
+
"mitm_flows"
|
|
10
|
+
],
|
|
11
|
+
"panels": [
|
|
12
|
+
"mitmproxy-flows"
|
|
13
|
+
],
|
|
14
|
+
"permissions": [
|
|
15
|
+
"spawnProcess"
|
|
16
|
+
]
|
|
17
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// netexec-bridge plugin type declarations
|
|
2
|
+
export function register(ctx: any): void
|
|
3
|
+
export function buildNetexecCommand(opts: {
|
|
4
|
+
protocol: string
|
|
5
|
+
targets: string
|
|
6
|
+
action: string
|
|
7
|
+
extraArgs?: string[]
|
|
8
|
+
username?: string
|
|
9
|
+
passwordRef?: string
|
|
10
|
+
domain?: string
|
|
11
|
+
timeoutSec?: number
|
|
12
|
+
}): { cmd: string; args: string[] }
|
|
13
|
+
export function validateTargets(targets: string | null | undefined, allowlist: string[] | null | undefined): { ok: boolean; reason?: string; targets?: string[] }
|
|
14
|
+
export function parseNetexecOutput(raw: string | null | undefined): {
|
|
15
|
+
hosts: Array<{ ip: string; hostname: string; status: string; detail?: string }>
|
|
16
|
+
authSuccess: Array<{ protocol: string; ip: string; port: number; hostname: string; detail: string; pwned: boolean }>
|
|
17
|
+
authFailed: Array<{ protocol: string; ip: string; port: number; hostname: string; detail: string }>
|
|
18
|
+
errors: string[]
|
|
19
|
+
raw: string
|
|
20
|
+
}
|
|
21
|
+
export function buildSprayPlan(opts: {
|
|
22
|
+
targets: string
|
|
23
|
+
usernames: string[]
|
|
24
|
+
attemptsPerUser?: number
|
|
25
|
+
delayMs?: number
|
|
26
|
+
jitterMs?: number
|
|
27
|
+
}): { targets: string; steps: Array<{ username: string; attempt: number; waitBeforeMs: number }>; totalAttempts: number; note: string }
|
|
28
|
+
export default any
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* netexec-bridge — NetExec external attack-simulation for RTerm.
|
|
3
|
+
*
|
|
4
|
+
* Complements rmagent (which watches FROM the boxes) by testing FROM the
|
|
5
|
+
* outside: credential validation, SMB/LDAP enumeration, spray simulation.
|
|
6
|
+
* The purple-team loop: NetExec stages an external attack, rmagent's attest
|
|
7
|
+
* must catch the resulting 4625s — the drill then scores both halves.
|
|
8
|
+
*
|
|
9
|
+
* GOVERNANCE (non-negotiable):
|
|
10
|
+
* - Target allowlist required on every call. No unbounded scanning.
|
|
11
|
+
* - Targets must be hosts the operator administers (same rule as rmagent).
|
|
12
|
+
* - Credential sprays are rate-limited and jittered by default.
|
|
13
|
+
* - The plugin never stores credentials; they resolve from the vault/env.
|
|
14
|
+
*
|
|
15
|
+
* Pure + injectable: process spawning injected; command building and output
|
|
16
|
+
* parsing are pure and fully testable.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
// --- Pure: build the netexec command line ---
|
|
20
|
+
|
|
21
|
+
export function buildNetexecCommand(opts) {
|
|
22
|
+
const {
|
|
23
|
+
protocol, // smb | ldap | winrm | mssql | ssh | ftp | rdp | wmi
|
|
24
|
+
targets, // string: CIDR, range, or comma-separated hosts
|
|
25
|
+
action, // netexec subcommand: users, groups, shares, --sam, etc.
|
|
26
|
+
extraArgs = [],
|
|
27
|
+
username,
|
|
28
|
+
passwordRef, // vault key — never the password itself
|
|
29
|
+
domain,
|
|
30
|
+
timeoutSec = 120,
|
|
31
|
+
} = opts || {}
|
|
32
|
+
|
|
33
|
+
if (!protocol) throw new Error('buildNetexecCommand needs a protocol (smb, ldap, winrm, ...)')
|
|
34
|
+
if (!targets) throw new Error('buildNetexecCommand needs targets')
|
|
35
|
+
if (!action) throw new Error('buildNetexecCommand needs an action')
|
|
36
|
+
|
|
37
|
+
const args = ['netexec', protocol, String(targets)]
|
|
38
|
+
if (username) args.push('-u', String(username))
|
|
39
|
+
// password resolves at runtime from the vault — we pass an env var name
|
|
40
|
+
if (passwordRef) args.push('-p', `env:${passwordRef}`)
|
|
41
|
+
if (domain) args.push('-d', String(domain))
|
|
42
|
+
args.push('--timeout', String(timeoutSec))
|
|
43
|
+
const actionStr = String(action)
|
|
44
|
+
if (actionStr.startsWith('--')) args.push(actionStr)
|
|
45
|
+
else args.push(actionStr)
|
|
46
|
+
for (const a of extraArgs) args.push(String(a))
|
|
47
|
+
|
|
48
|
+
return { cmd: 'netexec', args }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// --- Pure: validate targets against the authorized allowlist ---
|
|
52
|
+
|
|
53
|
+
export function validateTargets(targets, allowlist) {
|
|
54
|
+
if (!targets || typeof targets !== 'string') {
|
|
55
|
+
return { ok: false, reason: 'targets must be a string (CIDR, range, or comma-separated hosts)' }
|
|
56
|
+
}
|
|
57
|
+
if (!Array.isArray(allowlist) || allowlist.length === 0) {
|
|
58
|
+
return { ok: false, reason: 'an authorized target allowlist is required — unbounded scanning is not permitted' }
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const requested = targets.split(',').map((t) => t.trim()).filter(Boolean)
|
|
62
|
+
const allowedSet = new Set(allowlist.map((a) => String(a).trim()))
|
|
63
|
+
const denied = []
|
|
64
|
+
|
|
65
|
+
for (const t of requested) {
|
|
66
|
+
// exact host match
|
|
67
|
+
if (allowedSet.has(t)) continue
|
|
68
|
+
// CIDR membership: allowlist entry is a CIDR and target is a bare IP
|
|
69
|
+
const inCidr = allowlist.some((a) => {
|
|
70
|
+
const cidr = String(a).trim()
|
|
71
|
+
if (!cidr.includes('/')) return false
|
|
72
|
+
return ipInCidr(t, cidr)
|
|
73
|
+
})
|
|
74
|
+
if (inCidr) continue
|
|
75
|
+
denied.push(t)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (denied.length > 0) {
|
|
79
|
+
return { ok: false, reason: `target(s) not in the authorized allowlist: ${denied.join(', ')}` }
|
|
80
|
+
}
|
|
81
|
+
return { ok: true, targets: requested }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function ipInCidr(ip, cidr) {
|
|
85
|
+
const [base, bitsStr] = cidr.split('/')
|
|
86
|
+
const bits = Number(bitsStr)
|
|
87
|
+
if (!Number.isInteger(bits) || bits < 0 || bits > 32) return false
|
|
88
|
+
const toInt = (s) => {
|
|
89
|
+
const parts = s.split('.').map(Number)
|
|
90
|
+
if (parts.length !== 4 || parts.some((p) => !Number.isInteger(p) || p < 0 || p > 255)) return null
|
|
91
|
+
return ((parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3]) >>> 0
|
|
92
|
+
}
|
|
93
|
+
const ipInt = toInt(ip)
|
|
94
|
+
const baseInt = toInt(base)
|
|
95
|
+
if (ipInt === null || baseInt === null) return false
|
|
96
|
+
if (bits === 0) return true
|
|
97
|
+
const mask = (0xFFFFFFFF << (32 - bits)) >>> 0
|
|
98
|
+
return (ipInt & mask) === (baseInt & mask)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// --- Pure: parse netexec output ---
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* netexec prints lines like:
|
|
105
|
+
* SMB 192.168.1.10 445 HOSTNAME [+] hostname\user (Pwn3d!)
|
|
106
|
+
* SMB 192.168.1.11 445 HOSTNAME2 [-] hostname\user:BADPW
|
|
107
|
+
* Parse into hosts + auth results.
|
|
108
|
+
*/
|
|
109
|
+
export function parseNetexecOutput(raw) {
|
|
110
|
+
const result = {
|
|
111
|
+
hosts: [],
|
|
112
|
+
authSuccess: [],
|
|
113
|
+
authFailed: [],
|
|
114
|
+
errors: [],
|
|
115
|
+
raw: typeof raw === 'string' ? raw.slice(0, 5000) : '',
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (typeof raw !== 'string') return result
|
|
119
|
+
const lines = raw.split('\n')
|
|
120
|
+
|
|
121
|
+
for (const line of lines) {
|
|
122
|
+
const trimmed = line.trim()
|
|
123
|
+
if (!trimmed) continue
|
|
124
|
+
|
|
125
|
+
// Host status line: PROTOCOL ip port hostname [status]
|
|
126
|
+
const m = trimmed.match(/^(\w+)\s+(\S+)\s+(\d+)\s+(\S+)\s+(.*)$/)
|
|
127
|
+
if (!m) {
|
|
128
|
+
if (/error|failed to|traceback/i.test(trimmed)) result.errors.push(trimmed.slice(0, 200))
|
|
129
|
+
continue
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const [, proto, ip, port, hostname, rest] = m
|
|
133
|
+
const entry = { protocol: proto, ip, port: Number(port), hostname, detail: rest.slice(0, 200) }
|
|
134
|
+
|
|
135
|
+
if (/\[\+\]/.test(rest)) {
|
|
136
|
+
result.authSuccess.push({ ...entry, pwned: /\(Pwn3d!?\)/.test(rest) })
|
|
137
|
+
result.hosts.push({ ip, hostname, status: 'auth-ok' })
|
|
138
|
+
} else if (/\[-\]/.test(rest)) {
|
|
139
|
+
result.authFailed.push(entry)
|
|
140
|
+
result.hosts.push({ ip, hostname, status: 'auth-failed' })
|
|
141
|
+
} else if (/\[\*\]/.test(rest)) {
|
|
142
|
+
result.hosts.push({ ip, hostname, status: 'info', detail: rest.slice(0, 120) })
|
|
143
|
+
} else {
|
|
144
|
+
result.hosts.push({ ip, hostname, status: 'unknown' })
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return result
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// --- Pure: build a spray plan (rate-limited, jittered) ---
|
|
152
|
+
|
|
153
|
+
export function buildSprayPlan(opts) {
|
|
154
|
+
const {
|
|
155
|
+
targets,
|
|
156
|
+
usernames, // array of usernames to try
|
|
157
|
+
attemptsPerUser = 1,
|
|
158
|
+
delayMs = 5000, // between attempts — slow by default
|
|
159
|
+
jitterMs = 2000,
|
|
160
|
+
} = opts || {}
|
|
161
|
+
|
|
162
|
+
if (!Array.isArray(usernames) || usernames.length === 0) {
|
|
163
|
+
throw new Error('buildSprayPlan needs usernames')
|
|
164
|
+
}
|
|
165
|
+
if (!targets) throw new Error('buildSprayPlan needs targets')
|
|
166
|
+
|
|
167
|
+
const plan = []
|
|
168
|
+
for (const user of usernames) {
|
|
169
|
+
for (let i = 0; i < Math.max(1, attemptsPerUser); i++) {
|
|
170
|
+
const jitter = Math.floor(Math.random() * Math.max(0, jitterMs))
|
|
171
|
+
plan.push({
|
|
172
|
+
username: String(user),
|
|
173
|
+
attempt: i + 1,
|
|
174
|
+
waitBeforeMs: delayMs + jitter,
|
|
175
|
+
})
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return {
|
|
179
|
+
targets: String(targets),
|
|
180
|
+
steps: plan,
|
|
181
|
+
totalAttempts: plan.length,
|
|
182
|
+
note: `Spray plan: ${plan.length} attempt(s) across ${usernames.length} user(s), ≥${delayMs}ms between attempts (jitter ${jitterMs}ms). This is a SLOW spray by design — verify rmagent attest catches the 4625s.`,
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// --- Plugin entry ---
|
|
187
|
+
|
|
188
|
+
export function register(ctx) {
|
|
189
|
+
const { registerTool, registerTrigger, registerPanel, log, spawnProcess } = ctx
|
|
190
|
+
|
|
191
|
+
let lastResult = null
|
|
192
|
+
|
|
193
|
+
const runNetexec = async (plan) => {
|
|
194
|
+
if (typeof spawnProcess !== 'function') {
|
|
195
|
+
return { error: 'netexec-bridge requires process spawning, which is not available in this RTerm build.' }
|
|
196
|
+
}
|
|
197
|
+
return await new Promise((resolve) => {
|
|
198
|
+
let stdout = ''
|
|
199
|
+
let stderr = ''
|
|
200
|
+
let child
|
|
201
|
+
try {
|
|
202
|
+
child = spawnProcess(plan.cmd, plan.args, { stdio: 'pipe' })
|
|
203
|
+
} catch (e) {
|
|
204
|
+
resolve({ error: `failed to spawn netexec: ${e?.message ?? e}. Install with: pip install netexec` })
|
|
205
|
+
return
|
|
206
|
+
}
|
|
207
|
+
if (!child || typeof child.on !== 'function') {
|
|
208
|
+
resolve({ error: 'spawnProcess returned no child process' })
|
|
209
|
+
return
|
|
210
|
+
}
|
|
211
|
+
let settled = false
|
|
212
|
+
const settle = (v) => { if (!settled) { settled = true; resolve(v) } }
|
|
213
|
+
child.stdout?.on?.('data', (d) => { stdout += String(d) })
|
|
214
|
+
child.stderr?.on?.('data', (d) => { stderr += String(d) })
|
|
215
|
+
child.on('error', (e) => settle({ error: `netexec spawn error: ${e?.message ?? e}` }))
|
|
216
|
+
child.on('close', (code) => settle({ code, stdout, stderr }))
|
|
217
|
+
const t = setTimeout(() => {
|
|
218
|
+
try { child.kill?.() } catch { /* ignore */ }
|
|
219
|
+
settle({ error: 'netexec timed out', stdout, stderr })
|
|
220
|
+
}, 300_000)
|
|
221
|
+
child.on('close', () => clearTimeout(t))
|
|
222
|
+
})
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
registerTool({
|
|
226
|
+
name: 'netexec_check',
|
|
227
|
+
description: 'Run a NetExec check against authorized targets (credential validation, enumeration). Targets MUST be in the authorized allowlist — the same hosts you administer. Results parse into per-host auth outcomes; successful auths are findings.',
|
|
228
|
+
params: {
|
|
229
|
+
protocol: { type: 'string', description: 'smb | ldap | winrm | mssql | ssh | ftp | rdp | wmi' },
|
|
230
|
+
targets: { type: 'string', description: 'Target hosts (comma-separated IPs or a CIDR). Must be within the authorized allowlist.' },
|
|
231
|
+
action: { type: 'string', description: 'NetExec action: users, groups, shares, --sam, --users, etc.' },
|
|
232
|
+
allowlist: { type: 'array', description: 'Authorized target hosts/CIDRs. REQUIRED — no unbounded scanning.' },
|
|
233
|
+
username: { type: 'string', description: 'Username to test' },
|
|
234
|
+
passwordRef: { type: 'string', description: 'Vault key holding the password (never the password itself)' },
|
|
235
|
+
domain: { type: 'string', description: 'Optional domain' },
|
|
236
|
+
},
|
|
237
|
+
handler: async (params) => {
|
|
238
|
+
const { protocol, targets, action } = params || {}
|
|
239
|
+
const allowlist = Array.isArray(params?.allowlist) ? params.allowlist : []
|
|
240
|
+
|
|
241
|
+
// Governance gate FIRST.
|
|
242
|
+
const check = validateTargets(targets, allowlist)
|
|
243
|
+
if (!check.ok) {
|
|
244
|
+
return { error: `Authorization failed: ${check.reason}. Only test hosts you administer.` }
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
let plan
|
|
248
|
+
try {
|
|
249
|
+
plan = buildNetexecCommand({
|
|
250
|
+
protocol, targets, action,
|
|
251
|
+
username: params?.username,
|
|
252
|
+
passwordRef: params?.passwordRef,
|
|
253
|
+
domain: params?.domain,
|
|
254
|
+
})
|
|
255
|
+
} catch (e) {
|
|
256
|
+
return { error: e?.message ?? String(e) }
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
log(`[netexec] ${protocol} ${action} against ${check.targets.length} authorized target(s)`)
|
|
260
|
+
const run = await runNetexec(plan)
|
|
261
|
+
if (run?.error) return { error: run.error, stderr: run.stderr?.slice(0, 400) }
|
|
262
|
+
|
|
263
|
+
const parsed = parseNetexecOutput(run.stdout)
|
|
264
|
+
lastResult = parsed
|
|
265
|
+
|
|
266
|
+
log(`[netexec] ${parsed.hosts.length} host(s), ${parsed.authSuccess.length} auth success, ${parsed.authFailed.length} auth failed`)
|
|
267
|
+
return {
|
|
268
|
+
hosts: parsed.hosts,
|
|
269
|
+
authSuccess: parsed.authSuccess,
|
|
270
|
+
authFailed: parsed.authFailed,
|
|
271
|
+
errors: parsed.errors,
|
|
272
|
+
note: parsed.authSuccess.length > 0
|
|
273
|
+
? '⚠ Successful authentication found — treat as a finding and verify rmagent attest saw the corresponding logons.'
|
|
274
|
+
: 'No successful authentications.',
|
|
275
|
+
}
|
|
276
|
+
},
|
|
277
|
+
})
|
|
278
|
+
|
|
279
|
+
registerTool({
|
|
280
|
+
name: 'netexec_spray_plan',
|
|
281
|
+
description: 'Build a rate-limited, jittered credential-spray plan (does NOT execute it). Returns the step schedule to review before running via netexec_check. Slow by design so rmagent attest can catch the 4625 pattern.',
|
|
282
|
+
params: {
|
|
283
|
+
targets: { type: 'string', description: 'Target hosts (must be authorized)' },
|
|
284
|
+
usernames: { type: 'array', description: 'Usernames to try' },
|
|
285
|
+
attemptsPerUser: { type: 'number', description: 'Attempts per username (default 1)' },
|
|
286
|
+
},
|
|
287
|
+
handler: async (params) => {
|
|
288
|
+
try {
|
|
289
|
+
const plan = buildSprayPlan({
|
|
290
|
+
targets: params?.targets,
|
|
291
|
+
usernames: params?.usernames,
|
|
292
|
+
attemptsPerUser: params?.attemptsPerUser,
|
|
293
|
+
})
|
|
294
|
+
return plan
|
|
295
|
+
} catch (e) {
|
|
296
|
+
return { error: e?.message ?? String(e) }
|
|
297
|
+
}
|
|
298
|
+
},
|
|
299
|
+
})
|
|
300
|
+
|
|
301
|
+
registerTrigger({
|
|
302
|
+
name: 'netexec_auth_success',
|
|
303
|
+
description: 'Fires when a NetExec check finds a successful authentication on an authorized target. Use for incident + rmagent correlation playbooks.',
|
|
304
|
+
match: (event) => {
|
|
305
|
+
if (event?.source !== 'netexec') return false
|
|
306
|
+
return Array.isArray(event?.authSuccess) && event.authSuccess.length > 0
|
|
307
|
+
},
|
|
308
|
+
action: 'run-playbook',
|
|
309
|
+
})
|
|
310
|
+
|
|
311
|
+
registerPanel({
|
|
312
|
+
name: 'netexec-results',
|
|
313
|
+
title: 'NetExec',
|
|
314
|
+
render: async () => {
|
|
315
|
+
if (!lastResult) return '<div class="panel-section"><h3>NetExec</h3><p>No check run yet. Use netexec_check.</p></div>'
|
|
316
|
+
const rows = lastResult.hosts.slice(0, 15)
|
|
317
|
+
.map((h) => `<tr><td>${h.ip}</td><td>${h.hostname}</td><td>${h.status}</td></tr>`)
|
|
318
|
+
.join('')
|
|
319
|
+
return `<div class="panel-section"><h3>NetExec — ${lastResult.hosts.length} host(s)</h3>
|
|
320
|
+
<table><tr><th>ip</th><th>hostname</th><th>status</th></tr>${rows}</table>
|
|
321
|
+
${lastResult.authSuccess.length ? `<p class="warn">⚠ ${lastResult.authSuccess.length} successful auth(s)</p>` : ''}
|
|
322
|
+
</div>`
|
|
323
|
+
},
|
|
324
|
+
})
|
|
325
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "netexec-bridge",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "NetExec external attack-simulation for RTerm — credential validation, SMB/LDAP enumeration, and rate-limited spray simulation against authorized targets. Complements rmagent (inside view) with the outside view; closes the purple-team loop.",
|
|
5
|
+
"entry": "index.mjs",
|
|
6
|
+
"tools": [
|
|
7
|
+
"netexec_check",
|
|
8
|
+
"netexec_spray_plan"
|
|
9
|
+
],
|
|
10
|
+
"triggers": [
|
|
11
|
+
"netexec_auth_success"
|
|
12
|
+
],
|
|
13
|
+
"panels": [
|
|
14
|
+
"netexec-results"
|
|
15
|
+
],
|
|
16
|
+
"permissions": [
|
|
17
|
+
"spawnProcess",
|
|
18
|
+
"readLedger:incidents"
|
|
19
|
+
]
|
|
20
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// promptfoo-redteam plugin type declarations
|
|
2
|
+
export function register(ctx: any): void
|
|
3
|
+
export function buildPromptfooConfig(opts: {
|
|
4
|
+
providers: Array<{ name: string; model: string; baseUrl?: string; apiKeyRef?: string }>
|
|
5
|
+
tests: Array<{ vars: { prompt: string }; assert?: any[] }>
|
|
6
|
+
description?: string
|
|
7
|
+
}): { description: string; prompts: string[]; providers: any[]; tests: any[] }
|
|
8
|
+
export function builtinRedteamTests(): Array<{ description: string; vars: { prompt: string }; assert: any[] }>
|
|
9
|
+
export function parsePromptfooResults(raw: unknown): {
|
|
10
|
+
summary: { total: number; passed: number; failed: number; errors: number; byProvider: Record<string, { total: number; passed: number; failed: number }> }
|
|
11
|
+
findings: Array<{ severity: string; category: string; provider: string; test: string; score: number; message: string }>
|
|
12
|
+
error?: string
|
|
13
|
+
}
|
|
14
|
+
export function redteamVerdict(summary: { total: number; passed?: number; failed?: number; errors?: number } | null | undefined): string
|
|
15
|
+
export default any
|