|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * bb-edr (simulation) |
| 5 | + * |
| 6 | + * Generates an example `.hush/audit.jsonl` log by simulating a handful of tool actions |
| 7 | + * (file reads/writes, egress, and commands), gating each one with hushclaw policy checks. |
| 8 | + * |
| 9 | + * This does NOT run OpenClaw. It demonstrates the data you can feed into an agentic EDR loop. |
| 10 | + */ |
| 11 | + |
| 12 | +const fs = require('fs'); |
| 13 | +const path = require('path'); |
| 14 | + |
| 15 | +const { importOpenclawSdk } = require('./tools/openclaw'); |
| 16 | + |
| 17 | +function loadScenario() { |
| 18 | + const raw = fs.readFileSync('./scenario.json', 'utf8'); |
| 19 | + const parsed = JSON.parse(raw); |
| 20 | + |
| 21 | + if (!parsed || typeof parsed !== 'object') { |
| 22 | + throw new Error('Invalid scenario.json'); |
| 23 | + } |
| 24 | + if (!Array.isArray(parsed.actions)) { |
| 25 | + throw new Error('scenario.json must contain an "actions" array'); |
| 26 | + } |
| 27 | + |
| 28 | + return parsed; |
| 29 | +} |
| 30 | + |
| 31 | +async function main() { |
| 32 | + const { checkPolicy, AuditStore } = await importOpenclawSdk(); |
| 33 | + |
| 34 | + const config = { |
| 35 | + policy: './policy.yaml', |
| 36 | + mode: 'deterministic', |
| 37 | + logLevel: 'error', |
| 38 | + }; |
| 39 | + |
| 40 | + const scenario = loadScenario(); |
| 41 | + const runId = typeof scenario.runId === 'string' ? scenario.runId : 'bb-edr-demo'; |
| 42 | + |
| 43 | + const store = new AuditStore('.hush/audit.jsonl'); |
| 44 | + store.clear(); |
| 45 | + |
| 46 | + console.log('bb-edr (Simulated Agent Activity)'); |
| 47 | + console.log('================================\n'); |
| 48 | + |
| 49 | + for (const item of scenario.actions) { |
| 50 | + const action = item.action; |
| 51 | + const resource = item.resource; |
| 52 | + const note = typeof item.note === 'string' ? item.note : ''; |
| 53 | + |
| 54 | + if (typeof action !== 'string' || typeof resource !== 'string') { |
| 55 | + console.log(`[skip] invalid action entry: ${JSON.stringify(item)}`); |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + const decision = await checkPolicy(config, action, resource); |
| 60 | + |
| 61 | + const status = decision.denied ? 'DENY' : decision.warn ? 'WARN' : 'ALLOW'; |
| 62 | + const guard = decision.guard ? ` (${decision.guard})` : ''; |
| 63 | + const reason = decision.reason ? ` - ${decision.reason}` : ''; |
| 64 | + const noteSuffix = note ? ` # ${note}` : ''; |
| 65 | + |
| 66 | + console.log(`[hushclaw] ${status}: ${action} ${JSON.stringify(resource)}${guard}${reason}${noteSuffix}`); |
| 67 | + |
| 68 | + store.append({ |
| 69 | + type: action, |
| 70 | + resource, |
| 71 | + decision: decision.denied ? 'denied' : 'allowed', |
| 72 | + guard: decision.guard, |
| 73 | + reason: decision.reason, |
| 74 | + runId, |
| 75 | + }); |
| 76 | + |
| 77 | + // Optional side effect for the demo: create the "quarantine" file if policy allows it. |
| 78 | + if (action === 'file_write' && !decision.denied) { |
| 79 | + const absolute = path.resolve(resource); |
| 80 | + fs.mkdirSync(path.dirname(absolute), { recursive: true }); |
| 81 | + fs.writeFileSync(absolute, `bb-edr quarantine artifact (${new Date().toISOString()})\n`); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + console.log('\nWrote audit log: .hush/audit.jsonl'); |
| 86 | + console.log('Next: npm run triage'); |
| 87 | +} |
| 88 | + |
| 89 | +main().catch((err) => { |
| 90 | + console.error(err); |
| 91 | + process.exit(1); |
| 92 | +}); |
0 commit comments