@panguard-ai/atr 0.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.
Files changed (129) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +299 -0
  3. package/dist/cli.d.ts +12 -0
  4. package/dist/cli.d.ts.map +1 -0
  5. package/dist/cli.js +720 -0
  6. package/dist/cli.js.map +1 -0
  7. package/dist/coverage-analyzer.d.ts +43 -0
  8. package/dist/coverage-analyzer.d.ts.map +1 -0
  9. package/dist/coverage-analyzer.js +329 -0
  10. package/dist/coverage-analyzer.js.map +1 -0
  11. package/dist/engine.d.ts +127 -0
  12. package/dist/engine.d.ts.map +1 -0
  13. package/dist/engine.js +636 -0
  14. package/dist/engine.js.map +1 -0
  15. package/dist/index.d.ts +26 -0
  16. package/dist/index.d.ts.map +1 -0
  17. package/dist/index.js +18 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/loader.d.ts +21 -0
  20. package/dist/loader.d.ts.map +1 -0
  21. package/dist/loader.js +124 -0
  22. package/dist/loader.js.map +1 -0
  23. package/dist/mcp-server.d.ts +13 -0
  24. package/dist/mcp-server.d.ts.map +1 -0
  25. package/dist/mcp-server.js +220 -0
  26. package/dist/mcp-server.js.map +1 -0
  27. package/dist/mcp-tools/coverage-gaps.d.ts +13 -0
  28. package/dist/mcp-tools/coverage-gaps.d.ts.map +1 -0
  29. package/dist/mcp-tools/coverage-gaps.js +55 -0
  30. package/dist/mcp-tools/coverage-gaps.js.map +1 -0
  31. package/dist/mcp-tools/list-rules.d.ts +17 -0
  32. package/dist/mcp-tools/list-rules.d.ts.map +1 -0
  33. package/dist/mcp-tools/list-rules.js +45 -0
  34. package/dist/mcp-tools/list-rules.js.map +1 -0
  35. package/dist/mcp-tools/scan.d.ts +18 -0
  36. package/dist/mcp-tools/scan.d.ts.map +1 -0
  37. package/dist/mcp-tools/scan.js +75 -0
  38. package/dist/mcp-tools/scan.js.map +1 -0
  39. package/dist/mcp-tools/submit-proposal.d.ts +12 -0
  40. package/dist/mcp-tools/submit-proposal.d.ts.map +1 -0
  41. package/dist/mcp-tools/submit-proposal.js +95 -0
  42. package/dist/mcp-tools/submit-proposal.js.map +1 -0
  43. package/dist/mcp-tools/threat-summary.d.ts +12 -0
  44. package/dist/mcp-tools/threat-summary.d.ts.map +1 -0
  45. package/dist/mcp-tools/threat-summary.js +74 -0
  46. package/dist/mcp-tools/threat-summary.js.map +1 -0
  47. package/dist/mcp-tools/validate.d.ts +15 -0
  48. package/dist/mcp-tools/validate.d.ts.map +1 -0
  49. package/dist/mcp-tools/validate.js +45 -0
  50. package/dist/mcp-tools/validate.js.map +1 -0
  51. package/dist/modules/index.d.ts +144 -0
  52. package/dist/modules/index.d.ts.map +1 -0
  53. package/dist/modules/index.js +82 -0
  54. package/dist/modules/index.js.map +1 -0
  55. package/dist/modules/semantic.d.ts +105 -0
  56. package/dist/modules/semantic.d.ts.map +1 -0
  57. package/dist/modules/semantic.js +283 -0
  58. package/dist/modules/semantic.js.map +1 -0
  59. package/dist/modules/session.d.ts +70 -0
  60. package/dist/modules/session.d.ts.map +1 -0
  61. package/dist/modules/session.js +128 -0
  62. package/dist/modules/session.js.map +1 -0
  63. package/dist/rule-scaffolder.d.ts +39 -0
  64. package/dist/rule-scaffolder.d.ts.map +1 -0
  65. package/dist/rule-scaffolder.js +173 -0
  66. package/dist/rule-scaffolder.js.map +1 -0
  67. package/dist/session-tracker.d.ts +56 -0
  68. package/dist/session-tracker.d.ts.map +1 -0
  69. package/dist/session-tracker.js +175 -0
  70. package/dist/session-tracker.js.map +1 -0
  71. package/dist/skill-fingerprint.d.ts +96 -0
  72. package/dist/skill-fingerprint.d.ts.map +1 -0
  73. package/dist/skill-fingerprint.js +337 -0
  74. package/dist/skill-fingerprint.js.map +1 -0
  75. package/dist/types.d.ts +129 -0
  76. package/dist/types.d.ts.map +1 -0
  77. package/dist/types.js +6 -0
  78. package/dist/types.js.map +1 -0
  79. package/package.json +75 -0
  80. package/rules/agent-manipulation/ATR-2026-030-cross-agent-attack.yaml +175 -0
  81. package/rules/agent-manipulation/ATR-2026-032-goal-hijacking.yaml +135 -0
  82. package/rules/agent-manipulation/ATR-2026-074-cross-agent-privilege-escalation.yaml +115 -0
  83. package/rules/agent-manipulation/ATR-2026-076-inter-agent-message-spoofing.yaml +165 -0
  84. package/rules/agent-manipulation/ATR-2026-077-human-trust-exploitation.yaml +144 -0
  85. package/rules/context-exfiltration/ATR-2026-020-system-prompt-leak.yaml +175 -0
  86. package/rules/context-exfiltration/ATR-2026-021-api-key-exposure.yaml +176 -0
  87. package/rules/context-exfiltration/ATR-2026-075-agent-memory-manipulation.yaml +115 -0
  88. package/rules/data-poisoning/ATR-2026-070-data-poisoning.yaml +160 -0
  89. package/rules/excessive-autonomy/ATR-2026-050-runaway-agent-loop.yaml +134 -0
  90. package/rules/excessive-autonomy/ATR-2026-051-resource-exhaustion.yaml +137 -0
  91. package/rules/excessive-autonomy/ATR-2026-052-cascading-failure.yaml +153 -0
  92. package/rules/model-security/ATR-2026-072-model-behavior-extraction.yaml +115 -0
  93. package/rules/model-security/ATR-2026-073-malicious-finetuning-data.yaml +108 -0
  94. package/rules/privilege-escalation/ATR-2026-040-privilege-escalation.yaml +175 -0
  95. package/rules/privilege-escalation/ATR-2026-041-scope-creep.yaml +124 -0
  96. package/rules/prompt-injection/ATR-2026-001-direct-prompt-injection.yaml +265 -0
  97. package/rules/prompt-injection/ATR-2026-002-indirect-prompt-injection.yaml +214 -0
  98. package/rules/prompt-injection/ATR-2026-003-jailbreak-attempt.yaml +250 -0
  99. package/rules/prompt-injection/ATR-2026-004-system-prompt-override.yaml +204 -0
  100. package/rules/prompt-injection/ATR-2026-005-multi-turn-injection.yaml +181 -0
  101. package/rules/prompt-injection/ATR-PRED-2026-001.yaml +61 -0
  102. package/rules/prompt-injection/ATR-PRED-2026-002.yaml +58 -0
  103. package/rules/prompt-injection/ATR-PRED-2026-003.yaml +61 -0
  104. package/rules/prompt-injection/ATR-PRED-2026-005.yaml +55 -0
  105. package/rules/prompt-injection/ATR-PRED-2026-006.yaml +51 -0
  106. package/rules/prompt-injection/ATR-PRED-2026-007.yaml +57 -0
  107. package/rules/prompt-injection/ATR-PRED-2026-008.yaml +57 -0
  108. package/rules/prompt-injection/ATR-PRED-2026-009.yaml +51 -0
  109. package/rules/prompt-injection/ATR-PRED-2026-010.yaml +57 -0
  110. package/rules/prompt-injection/ATR-PRED-2026-011.yaml +53 -0
  111. package/rules/prompt-injection/ATR-PRED-2026-012.yaml +57 -0
  112. package/rules/prompt-injection/ATR-PRED-2026-023.yaml +56 -0
  113. package/rules/prompt-injection/ATR-PRED-2026-025.yaml +68 -0
  114. package/rules/prompt-injection/ATR-PRED-2026-026.yaml +66 -0
  115. package/rules/prompt-injection/ATR-PRED-2026-027.yaml +62 -0
  116. package/rules/skill-compromise/ATR-2026-060-skill-impersonation.yaml +153 -0
  117. package/rules/skill-compromise/ATR-2026-061-description-behavior-mismatch.yaml +98 -0
  118. package/rules/skill-compromise/ATR-2026-062-hidden-capability.yaml +96 -0
  119. package/rules/skill-compromise/ATR-2026-063-skill-chain-attack.yaml +96 -0
  120. package/rules/skill-compromise/ATR-2026-064-over-permissioned-skill.yaml +115 -0
  121. package/rules/skill-compromise/ATR-2026-065-skill-update-attack.yaml +93 -0
  122. package/rules/skill-compromise/ATR-2026-066-parameter-injection.yaml +106 -0
  123. package/rules/tool-poisoning/ATR-2026-010-mcp-malicious-response.yaml +237 -0
  124. package/rules/tool-poisoning/ATR-2026-011-tool-output-injection.yaml +185 -0
  125. package/rules/tool-poisoning/ATR-2026-012-unauthorized-tool-call.yaml +190 -0
  126. package/rules/tool-poisoning/ATR-2026-013-tool-ssrf.yaml +208 -0
  127. package/rules/tool-poisoning/ATR-PRED-2026-004.yaml +54 -0
  128. package/rules/tool-poisoning/ATR-PRED-2026-024.yaml +68 -0
  129. package/spec/atr-schema.yaml +375 -0
package/dist/cli.js ADDED
@@ -0,0 +1,720 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * ATR CLI - Command-line interface for Agent Threat Rules
4
+ *
5
+ * Usage:
6
+ * npx agent-threat-rules scan <events.json> Scan events against all rules
7
+ * npx agent-threat-rules validate <rule.yaml> Validate a rule file
8
+ * npx agent-threat-rules test <rule.yaml> Run a rule's test cases
9
+ * npx agent-threat-rules stats Show rule collection stats
10
+ */
11
+ import { readFileSync, readdirSync, existsSync, statSync } from 'node:fs';
12
+ import { resolve, dirname, join } from 'node:path';
13
+ import { fileURLToPath } from 'node:url';
14
+ import { ATREngine } from './engine.js';
15
+ import { loadRuleFile, loadRulesFromDirectory, validateRule } from './loader.js';
16
+ const __filename = fileURLToPath(import.meta.url);
17
+ const __dirname = dirname(__filename);
18
+ const RULES_DIR = resolve(__dirname, '..', 'rules');
19
+ const SEVERITY_COLORS = {
20
+ critical: '\x1b[91m', // bright red
21
+ high: '\x1b[31m', // red
22
+ medium: '\x1b[33m', // yellow
23
+ low: '\x1b[36m', // cyan
24
+ informational: '\x1b[37m', // white
25
+ };
26
+ const RESET = '\x1b[0m';
27
+ const GREEN = '\x1b[32m';
28
+ const RED = '\x1b[31m';
29
+ const DIM = '\x1b[2m';
30
+ const BOLD = '\x1b[1m';
31
+ function printUsage() {
32
+ console.log(`
33
+ ${BOLD}ATR - Agent Threat Rules${RESET}
34
+ Open detection rules for AI agent security threats.
35
+
36
+ ${BOLD}Usage:${RESET}
37
+ atr scan <events.json> [--rules <dir>] Scan events against ATR rules
38
+ atr validate <rule.yaml|dir> Validate rule file(s)
39
+ atr test <rule.yaml|dir> Run embedded test cases
40
+ atr stats [--rules <dir>] Show rule collection statistics
41
+ atr mcp Start MCP server (stdio transport)
42
+ atr scaffold Interactive rule scaffolding
43
+ atr submit <rule.yaml> [--endpoint <url>] Submit rule to Threat Cloud
44
+
45
+ ${BOLD}Options:${RESET}
46
+ --rules <dir> Custom rules directory (default: bundled rules)
47
+ --json Output results as JSON
48
+ --severity <s> Minimum severity to report (critical|high|medium|low|informational)
49
+ --endpoint <url> Threat Cloud endpoint (default: https://cloud.panguard.ai)
50
+ --help Show this help message
51
+
52
+ ${BOLD}Examples:${RESET}
53
+ ${DIM}# Scan agent events for threats${RESET}
54
+ atr scan events.json
55
+
56
+ ${DIM}# Validate a custom rule${RESET}
57
+ atr validate my-rules/custom-rule.yaml
58
+
59
+ ${DIM}# Test all rules against their embedded test cases${RESET}
60
+ atr test rules/
61
+
62
+ ${DIM}# Show stats for bundled rules${RESET}
63
+ atr stats
64
+
65
+ ${DIM}# Start MCP server for AI agent integration${RESET}
66
+ atr mcp
67
+
68
+ ${DIM}# Interactively scaffold a new rule${RESET}
69
+ atr scaffold
70
+
71
+ ${DIM}# Submit a rule to Threat Cloud for community review${RESET}
72
+ atr submit my-rules/new-rule.yaml --endpoint https://cloud.panguard.ai
73
+ `);
74
+ }
75
+ function parseArgs(argv) {
76
+ const args = argv.slice(2);
77
+ const command = args[0] ?? 'help';
78
+ const options = {};
79
+ let target = '';
80
+ for (let i = 1; i < args.length; i++) {
81
+ if (args[i].startsWith('--')) {
82
+ const key = args[i].slice(2);
83
+ if (key === 'json' || key === 'help') {
84
+ options[key] = 'true';
85
+ }
86
+ else {
87
+ options[key] = args[++i] ?? '';
88
+ }
89
+ }
90
+ else if (!target) {
91
+ target = args[i];
92
+ }
93
+ }
94
+ return { command, target, options };
95
+ }
96
+ // --- SCAN command ---
97
+ async function cmdScan(target, options) {
98
+ if (!target) {
99
+ console.error(`${RED}Error: Missing events file. Usage: atr scan <events.json>${RESET}`);
100
+ process.exit(1);
101
+ }
102
+ const eventsPath = resolve(target);
103
+ if (!existsSync(eventsPath)) {
104
+ console.error(`${RED}Error: File not found: ${eventsPath}${RESET}`);
105
+ process.exit(1);
106
+ }
107
+ const rulesDir = options['rules'] ? resolve(options['rules']) : RULES_DIR;
108
+ const minSeverity = options['severity'] ?? 'informational';
109
+ const jsonOutput = options['json'] === 'true';
110
+ const raw = readFileSync(eventsPath, 'utf-8');
111
+ let events;
112
+ try {
113
+ const parsed = JSON.parse(raw);
114
+ events = Array.isArray(parsed) ? parsed : [parsed];
115
+ }
116
+ catch {
117
+ console.error(`${RED}Error: Invalid JSON in ${eventsPath}${RESET}`);
118
+ process.exit(1);
119
+ }
120
+ const engine = new ATREngine({ rulesDir });
121
+ await engine.loadRules();
122
+ const severityOrder = ['informational', 'low', 'medium', 'high', 'critical'];
123
+ const minIdx = severityOrder.indexOf(minSeverity);
124
+ const allMatches = [];
125
+ let totalThreats = 0;
126
+ for (const event of events) {
127
+ const matches = engine.evaluate(event)
128
+ .filter(m => severityOrder.indexOf(m.rule.severity) >= minIdx);
129
+ if (matches.length > 0) {
130
+ allMatches.push({ event, matches });
131
+ totalThreats += matches.length;
132
+ }
133
+ }
134
+ if (jsonOutput) {
135
+ console.log(JSON.stringify({
136
+ eventsScanned: events.length,
137
+ threatsDetected: totalThreats,
138
+ rulesLoaded: engine.getRuleCount(),
139
+ results: allMatches.map(({ event, matches }) => ({
140
+ event: { type: event.type, timestamp: event.timestamp, contentPreview: event.content.slice(0, 100) },
141
+ matches: matches.map(m => ({
142
+ ruleId: m.rule.id,
143
+ title: m.rule.title,
144
+ severity: m.rule.severity,
145
+ confidence: m.confidence,
146
+ matchedConditions: m.matchedConditions,
147
+ })),
148
+ })),
149
+ }, null, 2));
150
+ return;
151
+ }
152
+ console.log(`\n${BOLD}ATR Scan Results${RESET}`);
153
+ console.log(`${DIM}${'─'.repeat(60)}${RESET}`);
154
+ console.log(` Events scanned: ${events.length}`);
155
+ console.log(` Rules loaded: ${engine.getRuleCount()}`);
156
+ console.log(` Threats found: ${totalThreats > 0 ? RED + totalThreats + RESET : GREEN + '0' + RESET}`);
157
+ console.log(`${DIM}${'─'.repeat(60)}${RESET}\n`);
158
+ if (totalThreats === 0) {
159
+ console.log(`${GREEN}No threats detected.${RESET}\n`);
160
+ return;
161
+ }
162
+ for (const { event, matches } of allMatches) {
163
+ const preview = event.content.slice(0, 80).replace(/\n/g, ' ');
164
+ console.log(` ${DIM}Event: [${event.type}] "${preview}..."${RESET}`);
165
+ for (const m of matches) {
166
+ const color = SEVERITY_COLORS[m.rule.severity] ?? '';
167
+ console.log(` ${color}${m.rule.severity.toUpperCase().padEnd(13)}${RESET} ${m.rule.id} - ${m.rule.title}`);
168
+ console.log(` ${DIM}Confidence: ${(m.confidence * 100).toFixed(0)}% | Conditions: ${m.matchedConditions.join(', ')}${RESET}`);
169
+ }
170
+ console.log('');
171
+ }
172
+ }
173
+ // --- VALIDATE command ---
174
+ function cmdValidate(target, options) {
175
+ if (!target) {
176
+ console.error(`${RED}Error: Missing rule file/directory. Usage: atr validate <rule.yaml|dir>${RESET}`);
177
+ process.exit(1);
178
+ }
179
+ const targetPath = resolve(target);
180
+ if (!existsSync(targetPath)) {
181
+ console.error(`${RED}Error: Not found: ${targetPath}${RESET}`);
182
+ process.exit(1);
183
+ }
184
+ const jsonOutput = options['json'] === 'true';
185
+ const files = [];
186
+ if (statSync(targetPath).isDirectory()) {
187
+ const rules = loadRulesFromDirectory(targetPath);
188
+ // Re-validate each file individually for error reporting
189
+ collectYamlFiles(targetPath, files);
190
+ }
191
+ else {
192
+ files.push(targetPath);
193
+ }
194
+ let passed = 0;
195
+ let failed = 0;
196
+ const results = [];
197
+ for (const file of files) {
198
+ try {
199
+ const rule = loadRuleFile(file);
200
+ const result = validateRule(rule);
201
+ results.push({ file, valid: result.valid, errors: result.errors });
202
+ if (result.valid) {
203
+ passed++;
204
+ }
205
+ else {
206
+ failed++;
207
+ }
208
+ }
209
+ catch (e) {
210
+ const msg = e instanceof Error ? e.message : String(e);
211
+ results.push({ file, valid: false, errors: [msg] });
212
+ failed++;
213
+ }
214
+ }
215
+ if (jsonOutput) {
216
+ console.log(JSON.stringify({ total: files.length, passed, failed, results }, null, 2));
217
+ return;
218
+ }
219
+ console.log(`\n${BOLD}ATR Rule Validation${RESET}`);
220
+ console.log(`${DIM}${'─'.repeat(60)}${RESET}`);
221
+ for (const r of results) {
222
+ const icon = r.valid ? `${GREEN}PASS${RESET}` : `${RED}FAIL${RESET}`;
223
+ const shortPath = r.file.replace(process.cwd() + '/', '');
224
+ console.log(` ${icon} ${shortPath}`);
225
+ if (!r.valid) {
226
+ for (const err of r.errors) {
227
+ console.log(` ${RED}${err}${RESET}`);
228
+ }
229
+ }
230
+ }
231
+ console.log(`${DIM}${'─'.repeat(60)}${RESET}`);
232
+ console.log(` ${GREEN}${passed} passed${RESET} ${failed > 0 ? RED + failed + ' failed' + RESET : ''}\n`);
233
+ if (failed > 0)
234
+ process.exit(1);
235
+ }
236
+ function collectYamlFiles(dir, out) {
237
+ const entries = readdirSync(dir);
238
+ for (const entry of entries) {
239
+ const full = join(dir, entry);
240
+ if (statSync(full).isDirectory()) {
241
+ collectYamlFiles(full, out);
242
+ }
243
+ else if (full.endsWith('.yaml') || full.endsWith('.yml')) {
244
+ out.push(full);
245
+ }
246
+ }
247
+ }
248
+ // --- TEST command ---
249
+ async function cmdTest(target, options) {
250
+ if (!target) {
251
+ // Default: test bundled rules
252
+ target = RULES_DIR;
253
+ }
254
+ const targetPath = resolve(target);
255
+ if (!existsSync(targetPath)) {
256
+ console.error(`${RED}Error: Not found: ${targetPath}${RESET}`);
257
+ process.exit(1);
258
+ }
259
+ const jsonOutput = options['json'] === 'true';
260
+ const rules = [];
261
+ if (statSync(targetPath).isDirectory()) {
262
+ rules.push(...loadRulesFromDirectory(targetPath));
263
+ }
264
+ else {
265
+ rules.push(loadRuleFile(targetPath));
266
+ }
267
+ let totalTests = 0;
268
+ let passed = 0;
269
+ let failed = 0;
270
+ const failures = [];
271
+ // Map extended agent_source types to basic event-compatible source types
272
+ // so the engine's source type filter doesn't skip rules during testing.
273
+ // The engine only recognizes: llm_io, tool_call, mcp_exchange, agent_behavior, multi_agent_comm
274
+ const EXTENDED_SOURCE_TO_BASE = {
275
+ context_window: 'llm_io',
276
+ memory_access: 'llm_io',
277
+ skill_lifecycle: 'tool_call',
278
+ skill_permission: 'tool_call',
279
+ skill_chain: 'tool_call',
280
+ };
281
+ for (const rule of rules) {
282
+ if (!rule.test_cases)
283
+ continue;
284
+ // For testing, normalize extended source types so the engine doesn't filter them out
285
+ const originalSourceType = rule.agent_source?.type;
286
+ const baseSourceType = EXTENDED_SOURCE_TO_BASE[originalSourceType ?? ''];
287
+ const testRule = baseSourceType
288
+ ? { ...rule, agent_source: { ...rule.agent_source, type: baseSourceType } }
289
+ : rule;
290
+ const engine = new ATREngine({ rules: [testRule] });
291
+ await engine.loadRules();
292
+ const tp = rule.test_cases.true_positives ?? [];
293
+ const tn = rule.test_cases.true_negatives ?? [];
294
+ for (const tc of tp) {
295
+ totalTests++;
296
+ const event = buildEventFromTestCase(tc, rule);
297
+ const matches = engine.evaluate(event);
298
+ const triggered = matches.some(m => m.rule.id === rule.id);
299
+ if (triggered) {
300
+ passed++;
301
+ }
302
+ else {
303
+ failed++;
304
+ failures.push({
305
+ ruleId: rule.id,
306
+ testType: 'true_positive',
307
+ input: String(tc.input ?? tc.tool_response ?? tc.agent_output ?? '').slice(0, 100),
308
+ expected: 'triggered',
309
+ got: 'not_triggered',
310
+ });
311
+ }
312
+ }
313
+ for (const tc of tn) {
314
+ totalTests++;
315
+ const event = buildEventFromTestCase(tc, rule);
316
+ const matches = engine.evaluate(event);
317
+ const triggered = matches.some(m => m.rule.id === rule.id);
318
+ if (!triggered) {
319
+ passed++;
320
+ }
321
+ else {
322
+ failed++;
323
+ failures.push({
324
+ ruleId: rule.id,
325
+ testType: 'true_negative',
326
+ input: String(tc.input ?? tc.tool_response ?? tc.agent_output ?? '').slice(0, 100),
327
+ expected: 'not_triggered',
328
+ got: 'triggered',
329
+ });
330
+ }
331
+ }
332
+ }
333
+ if (jsonOutput) {
334
+ console.log(JSON.stringify({ totalRules: rules.length, totalTests, passed, failed, failures }, null, 2));
335
+ return;
336
+ }
337
+ console.log(`\n${BOLD}ATR Test Runner${RESET}`);
338
+ console.log(`${DIM}${'─'.repeat(60)}${RESET}`);
339
+ console.log(` Rules tested: ${rules.length}`);
340
+ console.log(` Test cases: ${totalTests}`);
341
+ console.log(` ${GREEN}Passed:${RESET} ${passed}`);
342
+ if (failed > 0) {
343
+ console.log(` ${RED}Failed:${RESET} ${failed}`);
344
+ }
345
+ console.log(`${DIM}${'─'.repeat(60)}${RESET}`);
346
+ if (failures.length > 0) {
347
+ console.log(`\n${RED}Failures:${RESET}\n`);
348
+ for (const f of failures) {
349
+ console.log(` ${RED}FAIL${RESET} ${f.ruleId} [${f.testType}]`);
350
+ console.log(` ${DIM}Input: "${f.input}..."${RESET}`);
351
+ console.log(` Expected: ${f.expected}, Got: ${f.got}\n`);
352
+ }
353
+ process.exit(1);
354
+ }
355
+ else {
356
+ console.log(`\n${GREEN}All tests passed.${RESET}\n`);
357
+ }
358
+ }
359
+ function buildEventFromTestCase(tc, rule) {
360
+ // Stringify any object values
361
+ const str = (v) => {
362
+ if (v === undefined || v === null)
363
+ return '';
364
+ if (typeof v === 'string')
365
+ return v;
366
+ return JSON.stringify(v);
367
+ };
368
+ // Extract fields, handling both flat and object-style test cases.
369
+ // Object-style: input: { tool_name: "...", tool_args: "...", response: "..." }
370
+ // Flat-style: input: "...", tool_response: "...", tool_name: "..."
371
+ const rawInput = tc['input'];
372
+ let input = '';
373
+ let toolName = str(tc['tool_name']);
374
+ let toolArgs = str(tc['tool_args']);
375
+ let toolResponse = str(tc['tool_response']);
376
+ const agentOutput = str(tc['agent_output']);
377
+ if (rawInput !== null && rawInput !== undefined && typeof rawInput === 'object' && !Array.isArray(rawInput)) {
378
+ const inputObj = rawInput;
379
+ if (inputObj['tool_name'] && !toolName)
380
+ toolName = str(inputObj['tool_name']);
381
+ if (inputObj['tool_args'] && !toolArgs)
382
+ toolArgs = str(inputObj['tool_args']);
383
+ // Handle 'response' as alias for 'tool_response' (used in ATR-065 etc.)
384
+ if (inputObj['response'] && !toolResponse)
385
+ toolResponse = str(inputObj['response']);
386
+ if (inputObj['tool_response'] && !toolResponse)
387
+ toolResponse = str(inputObj['tool_response']);
388
+ // For object inputs, use tool_args as the primary string input.
389
+ // If no tool_args, only use JSON-stringified input as fallback when there's
390
+ // no other meaningful field (tool_response/response) extracted.
391
+ if (toolArgs) {
392
+ input = toolArgs;
393
+ }
394
+ else if (!toolResponse) {
395
+ input = str(rawInput);
396
+ }
397
+ }
398
+ else {
399
+ input = str(rawInput);
400
+ }
401
+ // Infer event type from rule's agent_source and test case structure
402
+ const sourceType = rule.agent_source?.type ?? 'llm_io';
403
+ const SOURCE_TO_EVENT = {
404
+ llm_io: 'llm_input',
405
+ tool_call: 'tool_call',
406
+ mcp_exchange: 'tool_response',
407
+ agent_behavior: 'agent_behavior',
408
+ multi_agent_comm: 'multi_agent_message',
409
+ context_window: 'llm_input',
410
+ memory_access: 'llm_input',
411
+ skill_lifecycle: 'tool_call',
412
+ skill_permission: 'tool_call',
413
+ skill_chain: 'tool_call',
414
+ };
415
+ let type = SOURCE_TO_EVENT[sourceType] ?? 'llm_input';
416
+ // If rule expects tool_call but test case has only plain text input
417
+ // (no tool_name, tool_args, or tool_response), use llm_input event type
418
+ // since the content is natural language, not a tool invocation.
419
+ // This prevents the engine's tool_name fallback from treating text as a tool name.
420
+ if (type === 'tool_call' && !toolName && !toolArgs && !toolResponse) {
421
+ type = 'llm_input';
422
+ }
423
+ // Determine the primary content based on what the rule conditions check
424
+ // Problem 2 & 3: For rules that check tool_response, the tool_response
425
+ // should be the primary content when the event type resolves tool_response from content
426
+ let content;
427
+ if (toolResponse && type === 'tool_response') {
428
+ // If event type is tool_response, engine resolves field:tool_response from event.content
429
+ content = toolResponse;
430
+ }
431
+ else {
432
+ content = input || toolResponse || agentOutput || '';
433
+ }
434
+ const fields = {};
435
+ // Populate ALL known field aliases so the engine can resolve any field name
436
+ if (input)
437
+ fields['user_input'] = input;
438
+ if (toolResponse)
439
+ fields['tool_response'] = toolResponse;
440
+ if (agentOutput)
441
+ fields['agent_output'] = agentOutput;
442
+ // Always set tool_name (even empty) to prevent engine fallback
443
+ // from using event.content as tool_name for tool_call events
444
+ fields['tool_name'] = toolName;
445
+ if (toolArgs)
446
+ fields['tool_args'] = toolArgs;
447
+ // For content-based rules, set content and agent_message fields
448
+ fields['content'] = content;
449
+ fields['agent_message'] = content;
450
+ return {
451
+ type,
452
+ timestamp: new Date().toISOString(),
453
+ content,
454
+ fields,
455
+ sessionId: 'test-session',
456
+ agentId: 'test-agent',
457
+ };
458
+ }
459
+ // --- STATS command ---
460
+ function cmdStats(options) {
461
+ const rulesDir = options['rules'] ? resolve(options['rules']) : RULES_DIR;
462
+ const jsonOutput = options['json'] === 'true';
463
+ const rules = loadRulesFromDirectory(rulesDir);
464
+ const byCategory = {};
465
+ const bySeverity = {};
466
+ const byMaturity = {};
467
+ const byTier = {};
468
+ let totalTP = 0;
469
+ let totalTN = 0;
470
+ let totalEvasion = 0;
471
+ let withCVE = 0;
472
+ for (const rule of rules) {
473
+ const cat = rule.tags?.category ?? 'unknown';
474
+ byCategory[cat] = (byCategory[cat] ?? 0) + 1;
475
+ bySeverity[rule.severity] = (bySeverity[rule.severity] ?? 0) + 1;
476
+ const maturity = rule['maturity'] ?? 'experimental';
477
+ byMaturity[maturity] = (byMaturity[maturity] ?? 0) + 1;
478
+ const tier = rule['detection_tier'] ?? 'pattern';
479
+ byTier[tier] = (byTier[tier] ?? 0) + 1;
480
+ if (rule.test_cases) {
481
+ totalTP += rule.test_cases.true_positives?.length ?? 0;
482
+ totalTN += rule.test_cases.true_negatives?.length ?? 0;
483
+ }
484
+ const evasion = rule['evasion_tests'];
485
+ if (Array.isArray(evasion))
486
+ totalEvasion += evasion.length;
487
+ if (rule.references?.cve && rule.references.cve.length > 0)
488
+ withCVE++;
489
+ }
490
+ if (jsonOutput) {
491
+ console.log(JSON.stringify({
492
+ totalRules: rules.length,
493
+ byCategory, bySeverity, byMaturity, byTier,
494
+ testCases: { truePositives: totalTP, trueNegatives: totalTN, evasionTests: totalEvasion },
495
+ rulesWithCVE: withCVE,
496
+ }, null, 2));
497
+ return;
498
+ }
499
+ console.log(`\n${BOLD}ATR Rule Collection Statistics${RESET}`);
500
+ console.log(`${DIM}${'─'.repeat(60)}${RESET}`);
501
+ console.log(` Total rules: ${rules.length}`);
502
+ console.log(` True positives: ${totalTP}`);
503
+ console.log(` True negatives: ${totalTN}`);
504
+ console.log(` Evasion tests: ${totalEvasion}`);
505
+ console.log(` Rules with CVE: ${withCVE}`);
506
+ console.log(`\n ${BOLD}By Category:${RESET}`);
507
+ for (const [cat, count] of Object.entries(byCategory).sort((a, b) => b[1] - a[1])) {
508
+ console.log(` ${cat.padEnd(24)} ${count}`);
509
+ }
510
+ console.log(`\n ${BOLD}By Severity:${RESET}`);
511
+ for (const sev of ['critical', 'high', 'medium', 'low', 'informational']) {
512
+ if (bySeverity[sev]) {
513
+ const color = SEVERITY_COLORS[sev] ?? '';
514
+ console.log(` ${color}${sev.padEnd(24)}${RESET} ${bySeverity[sev]}`);
515
+ }
516
+ }
517
+ console.log(`\n ${BOLD}By Maturity:${RESET}`);
518
+ for (const [mat, count] of Object.entries(byMaturity).sort((a, b) => b[1] - a[1])) {
519
+ console.log(` ${mat.padEnd(24)} ${count}`);
520
+ }
521
+ console.log(`\n ${BOLD}By Detection Tier:${RESET}`);
522
+ for (const [tier, count] of Object.entries(byTier).sort((a, b) => b[1] - a[1])) {
523
+ console.log(` ${tier.padEnd(24)} ${count}`);
524
+ }
525
+ console.log('');
526
+ }
527
+ // --- MCP command ---
528
+ async function cmdMcp() {
529
+ const { startMCPServer } = await import('./mcp-server.js');
530
+ await startMCPServer();
531
+ }
532
+ // --- SCAFFOLD command ---
533
+ async function cmdScaffold() {
534
+ const { createInterface } = await import('node:readline');
535
+ const { RuleScaffolder } = await import('./rule-scaffolder.js');
536
+ const rl = createInterface({ input: process.stdin, output: process.stdout });
537
+ const ask = (question) => new Promise((resolve) => rl.question(question, resolve));
538
+ console.log(`\n${BOLD}ATR Rule Scaffolder${RESET}`);
539
+ console.log(`${DIM}Generate a draft ATR detection rule interactively.${RESET}\n`);
540
+ const title = await ask('Rule title: ');
541
+ if (!title.trim()) {
542
+ console.error(`${RED}Error: Title is required.${RESET}`);
543
+ rl.close();
544
+ process.exit(1);
545
+ }
546
+ const categories = [
547
+ 'prompt-injection', 'tool-poisoning', 'context-exfiltration',
548
+ 'agent-manipulation', 'privilege-escalation', 'excessive-autonomy',
549
+ 'data-poisoning', 'model-abuse', 'skill-compromise',
550
+ ];
551
+ console.log(`\nCategories: ${categories.join(', ')}`);
552
+ const category = await ask('Category: ');
553
+ if (!categories.includes(category.trim())) {
554
+ console.error(`${RED}Error: Invalid category.${RESET}`);
555
+ rl.close();
556
+ process.exit(1);
557
+ }
558
+ const attackDescription = await ask('Attack description: ');
559
+ if (!attackDescription.trim()) {
560
+ console.error(`${RED}Error: Description is required.${RESET}`);
561
+ rl.close();
562
+ process.exit(1);
563
+ }
564
+ console.log('\nEnter example payloads (one per line, empty line to finish):');
565
+ const payloads = [];
566
+ while (true) {
567
+ const payload = await ask(` Payload ${payloads.length + 1}: `);
568
+ if (!payload.trim())
569
+ break;
570
+ payloads.push(payload.trim());
571
+ }
572
+ if (payloads.length === 0) {
573
+ console.error(`${RED}Error: At least one example payload is required.${RESET}`);
574
+ rl.close();
575
+ process.exit(1);
576
+ }
577
+ const severities = ['critical', 'high', 'medium', 'low', 'informational'];
578
+ const severity = await ask(`Severity [${severities.join('/')}] (default: medium): `);
579
+ const finalSeverity = severity.trim() && severities.includes(severity.trim())
580
+ ? severity.trim()
581
+ : 'medium';
582
+ rl.close();
583
+ const scaffolder = new RuleScaffolder();
584
+ const result = scaffolder.scaffold({
585
+ title: title.trim(),
586
+ category: category.trim(),
587
+ attackDescription: attackDescription.trim(),
588
+ examplePayloads: payloads,
589
+ severity: finalSeverity,
590
+ });
591
+ console.log(`\n${GREEN}Generated rule ${result.id}:${RESET}\n`);
592
+ console.log(`${DIM}${'─'.repeat(60)}${RESET}`);
593
+ console.log(result.yaml);
594
+ console.log(`${DIM}${'─'.repeat(60)}${RESET}`);
595
+ if (result.warnings.length > 0) {
596
+ console.log(`\n${BOLD}Warnings:${RESET}`);
597
+ for (const w of result.warnings) {
598
+ console.log(` - ${w}`);
599
+ }
600
+ }
601
+ console.log(`\n${DIM}Copy this YAML to a .yaml file in rules/${category.trim()}/ and validate with: atr validate <file>${RESET}\n`);
602
+ }
603
+ // --- SUBMIT command ---
604
+ async function cmdSubmit(target, options) {
605
+ if (!target) {
606
+ console.error(`${RED}Error: Missing rule file. Usage: atr submit <rule.yaml>${RESET}`);
607
+ process.exit(1);
608
+ }
609
+ const targetPath = resolve(target);
610
+ if (!existsSync(targetPath)) {
611
+ console.error(`${RED}Error: File not found: ${targetPath}${RESET}`);
612
+ process.exit(1);
613
+ }
614
+ // Load and validate the rule first
615
+ let rule;
616
+ try {
617
+ rule = loadRuleFile(targetPath);
618
+ const validation = validateRule(rule);
619
+ if (!validation.valid) {
620
+ console.error(`${RED}Rule validation failed:${RESET}`);
621
+ for (const err of validation.errors) {
622
+ console.error(` ${RED}${err}${RESET}`);
623
+ }
624
+ console.error(`\n${DIM}Fix validation errors before submitting.${RESET}`);
625
+ process.exit(1);
626
+ }
627
+ }
628
+ catch (e) {
629
+ console.error(`${RED}Error loading rule: ${e instanceof Error ? e.message : String(e)}${RESET}`);
630
+ process.exit(1);
631
+ }
632
+ const endpoint = options['endpoint'] ?? process.env['THREAT_CLOUD_ENDPOINT'] ?? 'https://cloud.panguard.ai';
633
+ const apiKey = process.env['THREAT_CLOUD_API_KEY'];
634
+ // Read raw YAML content for submission
635
+ const ruleContent = readFileSync(targetPath, 'utf-8');
636
+ // Generate pattern hash from rule ID
637
+ const { createHash } = await import('node:crypto');
638
+ const patternHash = createHash('sha256').update(rule.id).digest('hex').slice(0, 16);
639
+ console.log(`\n${BOLD}ATR Rule Submission${RESET}`);
640
+ console.log(`${DIM}${'─'.repeat(60)}${RESET}`);
641
+ console.log(` Rule: ${rule.id} - ${rule.title}`);
642
+ console.log(` Severity: ${SEVERITY_COLORS[rule.severity] ?? ''}${rule.severity}${RESET}`);
643
+ console.log(` Category: ${rule.tags?.category ?? 'unknown'}`);
644
+ console.log(` Endpoint: ${endpoint}`);
645
+ console.log(`${DIM}${'─'.repeat(60)}${RESET}`);
646
+ try {
647
+ const headers = { 'Content-Type': 'application/json' };
648
+ if (apiKey) {
649
+ headers['Authorization'] = `Bearer ${apiKey}`;
650
+ }
651
+ const response = await fetch(`${endpoint}/api/atr-proposals`, {
652
+ method: 'POST',
653
+ headers,
654
+ body: JSON.stringify({
655
+ patternHash,
656
+ ruleContent,
657
+ llmProvider: 'human',
658
+ llmModel: 'manual-submission',
659
+ selfReviewVerdict: JSON.stringify({ verdict: 'approved', source: 'author' }),
660
+ }),
661
+ signal: AbortSignal.timeout(10_000),
662
+ });
663
+ const result = await response.json();
664
+ if (result.ok) {
665
+ console.log(`\n${GREEN}Submitted successfully.${RESET}`);
666
+ console.log(`${DIM}Your rule will go through community consensus voting.${RESET}`);
667
+ console.log(`${DIM}Check status: atr submit --status ${patternHash}${RESET}\n`);
668
+ }
669
+ else {
670
+ console.error(`\n${RED}Submission failed: ${result.error ?? 'Unknown error'}${RESET}\n`);
671
+ process.exit(1);
672
+ }
673
+ }
674
+ catch (err) {
675
+ const msg = err instanceof Error ? err.message : String(err);
676
+ console.error(`\n${RED}Failed to connect to Threat Cloud: ${msg}${RESET}`);
677
+ console.error(`${DIM}Check your endpoint URL and network connection.${RESET}\n`);
678
+ process.exit(1);
679
+ }
680
+ }
681
+ // --- Main ---
682
+ async function main() {
683
+ const { command, target, options } = parseArgs(process.argv);
684
+ if (command === 'help' || options['help']) {
685
+ printUsage();
686
+ return;
687
+ }
688
+ switch (command) {
689
+ case 'scan':
690
+ await cmdScan(target, options);
691
+ break;
692
+ case 'validate':
693
+ cmdValidate(target, options);
694
+ break;
695
+ case 'test':
696
+ await cmdTest(target, options);
697
+ break;
698
+ case 'stats':
699
+ cmdStats(options);
700
+ break;
701
+ case 'mcp':
702
+ await cmdMcp();
703
+ break;
704
+ case 'scaffold':
705
+ await cmdScaffold();
706
+ break;
707
+ case 'submit':
708
+ await cmdSubmit(target, options);
709
+ break;
710
+ default:
711
+ console.error(`${RED}Unknown command: ${command}${RESET}`);
712
+ printUsage();
713
+ process.exit(1);
714
+ }
715
+ }
716
+ main().catch(err => {
717
+ console.error(`${RED}Error: ${err instanceof Error ? err.message : String(err)}${RESET}`);
718
+ process.exit(1);
719
+ });
720
+ //# sourceMappingURL=cli.js.map