Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions app/src/pages/attack-ranges/[id].astro
Original file line number Diff line number Diff line change
Expand Up @@ -843,17 +843,42 @@ if (id) {
<p>${result.message}</p>
`;

// 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, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;');
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 += '<div class="execution-output">';
outputHtml += '<h5>Execution Output:</h5>';
for (const [host, outputs] of Object.entries(result.execution_output)) {
if (summary) {
outputHtml += `<p><strong>Status:</strong> ${escapeHtml(executionOutput.status)} (${escapeHtml(summary.succeeded)}/${escapeHtml(summary.total)} succeeded, ${escapeHtml(summary.failed)} failed)</p>`;
}
for (const [host, hostPayload] of Object.entries(byHost)) {
const hostResults = Array.isArray(hostPayload?.results) ? hostPayload.results : [];
outputHtml += `<div class="host-output">`;
outputHtml += `<strong>Host: ${host}</strong>`;
for (const output of outputs) {
outputHtml += `<strong>Host: ${escapeHtml(host)}</strong>`;
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 += `<div class="technique-output">`;
outputHtml += `<strong>Technique: ${output.technique}</strong>`;
outputHtml += `<pre class="output-lines">${output.output.join('\n')}</pre>`;
outputHtml += `<strong>Technique: ${escapeHtml(label)} — ${escapeHtml(state)}</strong>`;
outputHtml += `<pre class="output-lines">${escapeHtml(lines.join('\n'))}</pre>`;
outputHtml += `</div>`;
}
outputHtml += `</div>`;
Expand Down
40 changes: 35 additions & 5 deletions app/src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
};
};
}

Expand Down
75 changes: 75 additions & 0 deletions app/tests/e2e/simulate.spec.ts
Original file line number Diff line number Diff line change
@@ -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)', '<script>not-html</script>'],
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)', '<script>not-html</script>'],
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('<script>not-html</script>');
});