diff --git a/app/src/pages/attack-ranges/[id].astro b/app/src/pages/attack-ranges/[id].astro index 80f070d5..66316500 100644 --- a/app/src/pages/attack-ranges/[id].astro +++ b/app/src/pages/attack-ranges/[id].astro @@ -843,17 +843,42 @@ if (id) {

${result.message}

`; - // Display execution output if available - if (result.execution_output) { + // Display execution output if available. + // Shape: { status, summary, results, by_host: { [host]: { status, summary, results } } } + const escapeHtml = (value: unknown) => + String(value ?? '') + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"') + .replace(/'/g, '''); + const executionOutput = result.execution_output; + if (executionOutput && typeof executionOutput === 'object') { + const summary = executionOutput.summary; + const byHost = + executionOutput.by_host && typeof executionOutput.by_host === 'object' + ? executionOutput.by_host + : { [result.target]: { results: executionOutput.results } }; outputHtml += '
'; outputHtml += '
Execution Output:
'; - for (const [host, outputs] of Object.entries(result.execution_output)) { + if (summary) { + outputHtml += `

Status: ${escapeHtml(executionOutput.status)} (${escapeHtml(summary.succeeded)}/${escapeHtml(summary.total)} succeeded, ${escapeHtml(summary.failed)} failed)

`; + } + for (const [host, hostPayload] of Object.entries(byHost)) { + const hostResults = Array.isArray(hostPayload?.results) ? hostPayload.results : []; outputHtml += `
`; - outputHtml += `Host: ${host}`; - for (const output of outputs) { + outputHtml += `Host: ${escapeHtml(host)}`; + for (const entry of hostResults) { + const label = entry.guid ? `${entry.technique}:${entry.guid}` : entry.technique; + const state = entry.success ? 'succeeded' : `failed (rc=${entry.return_code})`; + const lines = [ + ...(Array.isArray(entry.stdout_lines) ? entry.stdout_lines : []), + ...(Array.isArray(entry.stderr_lines) ? entry.stderr_lines : []), + ]; + if (!entry.success && entry.error) lines.push(entry.error); outputHtml += `
`; - outputHtml += `Technique: ${output.technique}`; - outputHtml += `
${output.output.join('\n')}
`; + outputHtml += `Technique: ${escapeHtml(label)} — ${escapeHtml(state)}`; + outputHtml += `
${escapeHtml(lines.join('\n'))}
`; outputHtml += `
`; } outputHtml += `
`; diff --git a/app/src/utils/api.ts b/app/src/utils/api.ts index f082ae97..4ecedfc6 100644 --- a/app/src/utils/api.ts +++ b/app/src/utils/api.ts @@ -204,11 +204,41 @@ export interface SimulateResponse { techniques: string[]; atomics: AtomicTestTarget[]; atomic_files?: AtomicFileTarget[]; - execution_output?: { - [host: string]: Array<{ - technique: string; - output: string[]; - }>; + execution_status?: string | null; + execution_summary?: AtomicExecutionSummary | null; + execution_output?: AtomicExecutionOutput | null; +} + +export interface AtomicExecutionSummary { + total: number; + succeeded: number; + failed: number; +} + +export interface AtomicExecutionResult { + technique: string; + guid: string; + success: boolean; + failed: boolean; + return_code: number; + stdout_lines: string[]; + stderr_lines: string[]; + stdout: string; + stderr: string; + error: string; +} + +/** Mirrors _extract_atomic_simulation_output() in attack_range/managers/ansible_manager.py */ +export interface AtomicExecutionOutput { + status: string; + summary: AtomicExecutionSummary; + results: AtomicExecutionResult[]; + by_host: { + [host: string]: { + status: string; + summary: AtomicExecutionSummary; + results: AtomicExecutionResult[]; + }; }; } diff --git a/app/tests/e2e/simulate.spec.ts b/app/tests/e2e/simulate.spec.ts new file mode 100644 index 00000000..cc26a624 --- /dev/null +++ b/app/tests/e2e/simulate.spec.ts @@ -0,0 +1,75 @@ +import { test, expect } from '@playwright/test'; + +// Response shape returned by POST /attack-range/simulate (see api/models.py SimulateResponse +// and _extract_atomic_simulation_output in attack_range/managers/ansible_manager.py). +const simulateResponse = { + status: 'success', + message: 'Successfully executed 1 technique(s) on Splunk', + attack_range_id: 'test-123', + target: 'Splunk', + techniques: ['T1197'], + atomics: [], + atomic_files: [], + execution_status: 'success', + execution_summary: { total: 1, succeeded: 1, failed: 0 }, + execution_output: { + results: [ + { + technique: 'T1197', + guid: '', + success: true, + failed: false, + return_code: 0, + stdout_lines: ['Executing test: T1197-1 Bitsadmin Download (cmd)', ''], + stderr_lines: [], + stdout: '', + stderr: '', + error: '', + }, + ], + status: 'success', + summary: { total: 1, succeeded: 1, failed: 0 }, + by_host: { + '10.0.2.10': { + status: 'success', + summary: { total: 1, succeeded: 1, failed: 0 }, + results: [ + { + technique: 'T1197', + guid: '', + success: true, + failed: false, + return_code: 0, + stdout_lines: ['Executing test: T1197-1 Bitsadmin Download (cmd)', ''], + stderr_lines: [], + stdout: '', + stderr: '', + error: '', + }, + ], + }, + }, + }, +}; + +test('simulation result renders structured execution output', async ({ page }) => { + await page.route('**/attack-range/simulate', (route) => + route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(simulateResponse) }), + ); + + await page.goto('/attack-ranges/test-123'); + await page.selectOption('#target-server', 'Splunk'); + await page.fill('#techniques', 'T1197'); + await page.click('#simulate-button'); + + const result = page.locator('#simulation-result'); + await expect(result).toBeVisible(); + await expect(result).toContainText('Simulation Completed Successfully'); + await expect(result).not.toContainText('Simulation Failed'); + await expect(result).toContainText('Host: 10.0.2.10'); + await expect(result).toContainText('Technique: T1197 — succeeded'); + await expect(result).toContainText('1/1 succeeded'); + await expect(result.locator('pre.output-lines')).toContainText('Bitsadmin Download'); + // Atomic output must be rendered as text, not injected as HTML + await expect(result.locator('pre.output-lines')).toContainText(''); +});