@panguard-ai/threat-cloud 0.1.1 → 0.2.1

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 (62) hide show
  1. package/dist/backup.d.ts +40 -0
  2. package/dist/backup.d.ts.map +1 -0
  3. package/dist/backup.js +123 -0
  4. package/dist/backup.js.map +1 -0
  5. package/dist/cli.js +24 -64
  6. package/dist/cli.js.map +1 -1
  7. package/dist/database.d.ts +58 -36
  8. package/dist/database.d.ts.map +1 -1
  9. package/dist/database.js +279 -328
  10. package/dist/database.js.map +1 -1
  11. package/dist/index.d.ts +4 -10
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +2 -9
  14. package/dist/index.js.map +1 -1
  15. package/dist/llm-reviewer.d.ts +47 -0
  16. package/dist/llm-reviewer.d.ts.map +1 -0
  17. package/dist/llm-reviewer.js +205 -0
  18. package/dist/llm-reviewer.js.map +1 -0
  19. package/dist/server.d.ts +43 -64
  20. package/dist/server.d.ts.map +1 -1
  21. package/dist/server.js +316 -647
  22. package/dist/server.js.map +1 -1
  23. package/dist/types.d.ts +36 -301
  24. package/dist/types.d.ts.map +1 -1
  25. package/package.json +20 -18
  26. package/LICENSE +0 -21
  27. package/dist/audit-logger.d.ts +0 -46
  28. package/dist/audit-logger.d.ts.map +0 -1
  29. package/dist/audit-logger.js +0 -105
  30. package/dist/audit-logger.js.map +0 -1
  31. package/dist/correlation-engine.d.ts +0 -41
  32. package/dist/correlation-engine.d.ts.map +0 -1
  33. package/dist/correlation-engine.js +0 -313
  34. package/dist/correlation-engine.js.map +0 -1
  35. package/dist/feed-distributor.d.ts +0 -36
  36. package/dist/feed-distributor.d.ts.map +0 -1
  37. package/dist/feed-distributor.js +0 -125
  38. package/dist/feed-distributor.js.map +0 -1
  39. package/dist/ioc-store.d.ts +0 -83
  40. package/dist/ioc-store.d.ts.map +0 -1
  41. package/dist/ioc-store.js +0 -278
  42. package/dist/ioc-store.js.map +0 -1
  43. package/dist/query-handlers.d.ts +0 -40
  44. package/dist/query-handlers.d.ts.map +0 -1
  45. package/dist/query-handlers.js +0 -211
  46. package/dist/query-handlers.js.map +0 -1
  47. package/dist/reputation-engine.d.ts +0 -44
  48. package/dist/reputation-engine.d.ts.map +0 -1
  49. package/dist/reputation-engine.js +0 -169
  50. package/dist/reputation-engine.js.map +0 -1
  51. package/dist/rule-generator.d.ts +0 -47
  52. package/dist/rule-generator.d.ts.map +0 -1
  53. package/dist/rule-generator.js +0 -238
  54. package/dist/rule-generator.js.map +0 -1
  55. package/dist/scheduler.d.ts +0 -52
  56. package/dist/scheduler.d.ts.map +0 -1
  57. package/dist/scheduler.js +0 -143
  58. package/dist/scheduler.js.map +0 -1
  59. package/dist/sighting-store.d.ts +0 -61
  60. package/dist/sighting-store.d.ts.map +0 -1
  61. package/dist/sighting-store.js +0 -191
  62. package/dist/sighting-store.js.map +0 -1
package/dist/database.js CHANGED
@@ -2,11 +2,10 @@
2
2
  * SQLite database layer for Threat Cloud
3
3
  * 威脅雲 SQLite 資料庫層
4
4
  *
5
- * Stores anonymized threat data, enriched events, IoCs, campaigns, and rules.
5
+ * Stores anonymized threat data and community rules using better-sqlite3.
6
6
  *
7
7
  * @module @panguard-ai/threat-cloud/database
8
8
  */
9
- import { createHash } from 'node:crypto';
10
9
  import Database from 'better-sqlite3';
11
10
  /**
12
11
  * Threat Cloud database backed by SQLite
@@ -18,27 +17,9 @@ export class ThreatCloudDB {
18
17
  this.db = new Database(dbPath);
19
18
  this.db.pragma('journal_mode = WAL');
20
19
  this.db.pragma('foreign_keys = ON');
21
- this.db.pragma('busy_timeout = 15000');
22
- this.db.pragma('synchronous = NORMAL');
23
- this.db.pragma('cache_size = -64000');
24
- this.db.pragma('temp_store = MEMORY');
25
- this.db.pragma('wal_autocheckpoint = 1000');
26
- this.db.pragma('journal_size_limit = 104857600');
27
20
  this.initialize();
28
- this.runMigrations();
29
21
  }
30
- /** Create a backup of the database / 建立資料庫備份 */
31
- backup(destPath) {
32
- this.db.backup(destPath);
33
- }
34
- /** Expose underlying db for sub-modules (IoCStore, etc.) / 暴露底層 DB 給子模組 */
35
- getDB() {
36
- return this.db;
37
- }
38
- // -------------------------------------------------------------------------
39
- // Schema initialization / 資料表初始化
40
- // -------------------------------------------------------------------------
41
- /** Create original tables if they don't exist / 建立原始資料表 */
22
+ /** Create tables if they don't exist / 建立資料表 */
42
23
  initialize() {
43
24
  this.db.exec(`
44
25
  CREATE TABLE IF NOT EXISTS threats (
@@ -67,199 +48,72 @@ export class ThreatCloudDB {
67
48
  CREATE INDEX IF NOT EXISTS idx_threats_mitre ON threats(mitre_technique);
68
49
  CREATE INDEX IF NOT EXISTS idx_rules_published ON rules(published_at);
69
50
 
70
- CREATE TABLE IF NOT EXISTS schema_migrations (
71
- version INTEGER PRIMARY KEY,
72
- name TEXT NOT NULL,
73
- applied_at TEXT NOT NULL DEFAULT (datetime('now'))
51
+ CREATE TABLE IF NOT EXISTS atr_proposals (
52
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
53
+ pattern_hash TEXT NOT NULL,
54
+ rule_content TEXT NOT NULL,
55
+ llm_provider TEXT NOT NULL,
56
+ llm_model TEXT NOT NULL,
57
+ self_review_verdict TEXT NOT NULL,
58
+ client_id TEXT,
59
+ status TEXT NOT NULL DEFAULT 'pending',
60
+ confirmations INTEGER NOT NULL DEFAULT 0,
61
+ llm_review_verdict TEXT,
62
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
63
+ updated_at TEXT NOT NULL DEFAULT (datetime('now'))
64
+ );
65
+
66
+ CREATE TABLE IF NOT EXISTS atr_feedback (
67
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
68
+ rule_id TEXT NOT NULL,
69
+ is_true_positive INTEGER NOT NULL,
70
+ client_id TEXT,
71
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
72
+ );
73
+
74
+ CREATE TABLE IF NOT EXISTS skill_threats (
75
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
76
+ skill_hash TEXT NOT NULL,
77
+ skill_name TEXT NOT NULL,
78
+ risk_score INTEGER NOT NULL,
79
+ risk_level TEXT NOT NULL,
80
+ finding_summaries TEXT,
81
+ client_id TEXT,
82
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
83
+ );
84
+
85
+ CREATE TABLE IF NOT EXISTS ioc_entries (
86
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
87
+ type TEXT NOT NULL,
88
+ value TEXT NOT NULL UNIQUE,
89
+ reputation INTEGER NOT NULL DEFAULT 50,
90
+ source TEXT,
91
+ first_seen TEXT DEFAULT (datetime('now')),
92
+ last_seen TEXT DEFAULT (datetime('now')),
93
+ sighting_count INTEGER DEFAULT 1
94
+ );
95
+
96
+ CREATE TABLE IF NOT EXISTS skill_whitelist (
97
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
98
+ skill_name TEXT NOT NULL,
99
+ normalized_name TEXT NOT NULL UNIQUE,
100
+ fingerprint_hash TEXT,
101
+ confirmations INTEGER NOT NULL DEFAULT 1,
102
+ status TEXT NOT NULL DEFAULT 'pending',
103
+ first_reported TEXT DEFAULT (datetime('now')),
104
+ last_reported TEXT DEFAULT (datetime('now'))
74
105
  );
106
+
107
+ CREATE INDEX IF NOT EXISTS idx_atr_proposals_status ON atr_proposals(status);
108
+ CREATE INDEX IF NOT EXISTS idx_atr_proposals_pattern ON atr_proposals(pattern_hash);
109
+ CREATE INDEX IF NOT EXISTS idx_skill_threats_hash ON skill_threats(skill_hash);
110
+ CREATE INDEX IF NOT EXISTS idx_atr_feedback_rule ON atr_feedback(rule_id);
111
+ CREATE INDEX IF NOT EXISTS idx_ioc_entries_type ON ioc_entries(type);
112
+ CREATE INDEX IF NOT EXISTS idx_ioc_entries_reputation ON ioc_entries(reputation);
113
+ CREATE INDEX IF NOT EXISTS idx_skill_whitelist_status ON skill_whitelist(status);
114
+ CREATE INDEX IF NOT EXISTS idx_skill_whitelist_name ON skill_whitelist(normalized_name);
75
115
  `);
76
116
  }
77
- /** Run idempotent schema migrations / 執行冪等 schema 遷移 */
78
- runMigrations() {
79
- const applied = new Set(this.db.prepare('SELECT version FROM schema_migrations').all().map((r) => r.version));
80
- const migrations = [
81
- {
82
- version: 1,
83
- name: 'create_iocs_table',
84
- sql: `
85
- CREATE TABLE IF NOT EXISTS iocs (
86
- id INTEGER PRIMARY KEY AUTOINCREMENT,
87
- type TEXT NOT NULL CHECK(type IN ('ip','domain','url','hash_md5','hash_sha1','hash_sha256')),
88
- value TEXT NOT NULL,
89
- normalized_value TEXT NOT NULL,
90
- threat_type TEXT NOT NULL,
91
- source TEXT NOT NULL,
92
- confidence INTEGER NOT NULL DEFAULT 50 CHECK(confidence BETWEEN 0 AND 100),
93
- reputation_score INTEGER NOT NULL DEFAULT 50 CHECK(reputation_score BETWEEN 0 AND 100),
94
- first_seen TEXT NOT NULL,
95
- last_seen TEXT NOT NULL,
96
- sightings INTEGER NOT NULL DEFAULT 1,
97
- status TEXT NOT NULL DEFAULT 'active' CHECK(status IN ('active','expired','revoked','under_review')),
98
- tags TEXT NOT NULL DEFAULT '[]',
99
- metadata TEXT NOT NULL DEFAULT '{}',
100
- created_at TEXT NOT NULL DEFAULT (datetime('now')),
101
- updated_at TEXT NOT NULL DEFAULT (datetime('now')),
102
- UNIQUE(type, normalized_value)
103
- );
104
- CREATE INDEX IF NOT EXISTS idx_iocs_type ON iocs(type);
105
- CREATE INDEX IF NOT EXISTS idx_iocs_normalized ON iocs(normalized_value);
106
- CREATE INDEX IF NOT EXISTS idx_iocs_reputation ON iocs(reputation_score DESC);
107
- CREATE INDEX IF NOT EXISTS idx_iocs_last_seen ON iocs(last_seen);
108
- CREATE INDEX IF NOT EXISTS idx_iocs_status ON iocs(status);
109
- CREATE INDEX IF NOT EXISTS idx_iocs_source ON iocs(source);
110
- `,
111
- },
112
- {
113
- version: 2,
114
- name: 'create_enriched_threats_table',
115
- sql: `
116
- CREATE TABLE IF NOT EXISTS enriched_threats (
117
- id INTEGER PRIMARY KEY AUTOINCREMENT,
118
- source_type TEXT NOT NULL CHECK(source_type IN ('guard','trap','external_feed')),
119
- attack_source_ip TEXT NOT NULL,
120
- attack_type TEXT NOT NULL,
121
- mitre_techniques TEXT NOT NULL DEFAULT '[]',
122
- sigma_rule_matched TEXT NOT NULL DEFAULT '',
123
- timestamp TEXT NOT NULL,
124
- industry TEXT,
125
- region TEXT NOT NULL DEFAULT 'unknown',
126
- confidence INTEGER NOT NULL DEFAULT 50,
127
- severity TEXT NOT NULL DEFAULT 'medium',
128
- service_type TEXT,
129
- skill_level TEXT,
130
- intent TEXT,
131
- tools TEXT,
132
- event_hash TEXT NOT NULL UNIQUE,
133
- received_at TEXT NOT NULL DEFAULT (datetime('now')),
134
- campaign_id TEXT
135
- );
136
- CREATE INDEX IF NOT EXISTS idx_enriched_timestamp ON enriched_threats(timestamp);
137
- CREATE INDEX IF NOT EXISTS idx_enriched_attack_type ON enriched_threats(attack_type);
138
- CREATE INDEX IF NOT EXISTS idx_enriched_ip ON enriched_threats(attack_source_ip);
139
- CREATE INDEX IF NOT EXISTS idx_enriched_campaign ON enriched_threats(campaign_id);
140
- CREATE INDEX IF NOT EXISTS idx_enriched_source_type ON enriched_threats(source_type);
141
- CREATE INDEX IF NOT EXISTS idx_enriched_region ON enriched_threats(region);
142
- CREATE INDEX IF NOT EXISTS idx_enriched_severity ON enriched_threats(severity);
143
- CREATE INDEX IF NOT EXISTS idx_enriched_received ON enriched_threats(received_at);
144
- `,
145
- },
146
- {
147
- version: 3,
148
- name: 'create_trap_credentials_table',
149
- sql: `
150
- CREATE TABLE IF NOT EXISTS trap_credentials (
151
- id INTEGER PRIMARY KEY AUTOINCREMENT,
152
- enriched_threat_id INTEGER NOT NULL REFERENCES enriched_threats(id) ON DELETE CASCADE,
153
- username TEXT NOT NULL,
154
- attempt_count INTEGER NOT NULL DEFAULT 1,
155
- created_at TEXT NOT NULL DEFAULT (datetime('now'))
156
- );
157
- CREATE INDEX IF NOT EXISTS idx_trap_creds_threat ON trap_credentials(enriched_threat_id);
158
- CREATE INDEX IF NOT EXISTS idx_trap_creds_username ON trap_credentials(username);
159
- `,
160
- },
161
- {
162
- version: 4,
163
- name: 'create_generated_patterns_table',
164
- sql: `
165
- CREATE TABLE IF NOT EXISTS generated_patterns (
166
- pattern_hash TEXT PRIMARY KEY,
167
- attack_type TEXT NOT NULL,
168
- mitre_techniques TEXT NOT NULL,
169
- rule_id TEXT NOT NULL REFERENCES rules(rule_id) ON DELETE CASCADE,
170
- occurrences INTEGER NOT NULL,
171
- distinct_ips INTEGER NOT NULL,
172
- first_seen TEXT NOT NULL,
173
- last_seen TEXT NOT NULL,
174
- created_at TEXT NOT NULL DEFAULT (datetime('now')),
175
- updated_at TEXT NOT NULL DEFAULT (datetime('now'))
176
- );
177
- CREATE INDEX IF NOT EXISTS idx_gen_patterns_attack ON generated_patterns(attack_type);
178
- CREATE INDEX IF NOT EXISTS idx_gen_patterns_rule ON generated_patterns(rule_id);
179
- `,
180
- },
181
- {
182
- version: 5,
183
- name: 'create_daily_aggregates_table',
184
- sql: `
185
- CREATE TABLE IF NOT EXISTS daily_aggregates (
186
- id INTEGER PRIMARY KEY AUTOINCREMENT,
187
- date TEXT NOT NULL,
188
- attack_type TEXT NOT NULL,
189
- region TEXT NOT NULL,
190
- source_type TEXT NOT NULL,
191
- event_count INTEGER NOT NULL,
192
- unique_ips INTEGER NOT NULL,
193
- avg_confidence REAL NOT NULL,
194
- severity_distribution TEXT NOT NULL DEFAULT '{}',
195
- created_at TEXT NOT NULL DEFAULT (datetime('now')),
196
- UNIQUE(date, attack_type, region, source_type)
197
- );
198
- CREATE INDEX IF NOT EXISTS idx_daily_agg_date ON daily_aggregates(date);
199
- CREATE INDEX IF NOT EXISTS idx_daily_agg_type ON daily_aggregates(attack_type);
200
- `,
201
- },
202
- {
203
- version: 6,
204
- name: 'create_sightings_table',
205
- sql: `
206
- CREATE TABLE IF NOT EXISTS sightings (
207
- id INTEGER PRIMARY KEY AUTOINCREMENT,
208
- ioc_id INTEGER NOT NULL REFERENCES iocs(id) ON DELETE CASCADE,
209
- type TEXT NOT NULL CHECK(type IN ('positive','negative','false_positive')),
210
- source TEXT NOT NULL,
211
- confidence INTEGER NOT NULL DEFAULT 50 CHECK(confidence BETWEEN 0 AND 100),
212
- details TEXT NOT NULL DEFAULT '',
213
- actor_hash TEXT NOT NULL DEFAULT '',
214
- created_at TEXT NOT NULL DEFAULT (datetime('now'))
215
- );
216
- CREATE INDEX IF NOT EXISTS idx_sightings_ioc ON sightings(ioc_id);
217
- CREATE INDEX IF NOT EXISTS idx_sightings_type ON sightings(type);
218
- CREATE INDEX IF NOT EXISTS idx_sightings_created ON sightings(created_at);
219
- `,
220
- },
221
- {
222
- version: 7,
223
- name: 'create_audit_log_table',
224
- sql: `
225
- CREATE TABLE IF NOT EXISTS audit_log (
226
- id INTEGER PRIMARY KEY AUTOINCREMENT,
227
- action TEXT NOT NULL,
228
- entity_type TEXT NOT NULL,
229
- entity_id TEXT NOT NULL,
230
- actor_hash TEXT NOT NULL DEFAULT '',
231
- ip_address TEXT NOT NULL DEFAULT '',
232
- details TEXT NOT NULL DEFAULT '{}',
233
- created_at TEXT NOT NULL DEFAULT (datetime('now'))
234
- );
235
- CREATE INDEX IF NOT EXISTS idx_audit_action ON audit_log(action);
236
- CREATE INDEX IF NOT EXISTS idx_audit_entity ON audit_log(entity_type, entity_id);
237
- CREATE INDEX IF NOT EXISTS idx_audit_created ON audit_log(created_at);
238
- CREATE INDEX IF NOT EXISTS idx_audit_actor ON audit_log(actor_hash);
239
- `,
240
- },
241
- {
242
- version: 8,
243
- name: 'add_source_reliability_to_iocs',
244
- sql: `
245
- ALTER TABLE iocs ADD COLUMN source_reliability TEXT NOT NULL DEFAULT 'F'
246
- CHECK(source_reliability IN ('A','B','C','D','E','F'));
247
- `,
248
- },
249
- ];
250
- const insertMigration = this.db.prepare('INSERT INTO schema_migrations (version, name) VALUES (?, ?)');
251
- for (const m of migrations) {
252
- if (!applied.has(m.version)) {
253
- this.db.transaction(() => {
254
- this.db.exec(m.sql);
255
- insertMigration.run(m.version, m.name);
256
- })();
257
- }
258
- }
259
- }
260
- // -------------------------------------------------------------------------
261
- // Legacy threat operations (backward compatible) / 原始威脅操作
262
- // -------------------------------------------------------------------------
263
117
  /** Insert anonymized threat data / 插入匿名化威脅數據 */
264
118
  insertThreat(data) {
265
119
  const stmt = this.db.prepare(`
@@ -268,106 +122,6 @@ export class ThreatCloudDB {
268
122
  `);
269
123
  stmt.run(data.attackSourceIP, data.attackType, data.mitreTechnique, data.sigmaRuleMatched, data.timestamp, data.industry ?? null, data.region);
270
124
  }
271
- // -------------------------------------------------------------------------
272
- // Enriched threat operations / 豐富化威脅操作
273
- // -------------------------------------------------------------------------
274
- /**
275
- * Insert enriched threat event (deduplicates by event_hash).
276
- * Returns the row id if inserted, null if duplicate.
277
- * 插入豐富化威脅事件(以 event_hash 去重)
278
- */
279
- insertEnrichedThreat(event) {
280
- const stmt = this.db.prepare(`
281
- INSERT OR IGNORE INTO enriched_threats
282
- (source_type, attack_source_ip, attack_type, mitre_techniques, sigma_rule_matched,
283
- timestamp, industry, region, confidence, severity, service_type, skill_level,
284
- intent, tools, event_hash)
285
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
286
- `);
287
- const result = stmt.run(event.sourceType, event.attackSourceIP, event.attackType, JSON.stringify(event.mitreTechniques), event.sigmaRuleMatched, event.timestamp, event.industry ?? null, event.region, event.confidence, event.severity, event.serviceType ?? null, event.skillLevel ?? null, event.intent ?? null, event.tools ? JSON.stringify(event.tools) : null, event.eventHash);
288
- return result.changes > 0 ? Number(result.lastInsertRowid) : null;
289
- }
290
- /**
291
- * Insert trap credential records.
292
- * Usernames are hashed (SHA-256, truncated to 16 hex chars) before storage
293
- * to avoid storing PII from attacker-attempted credentials.
294
- * 插入 Trap 憑證記錄(使用者名稱先雜湊化以避免 PII 洩漏)
295
- */
296
- insertTrapCredentials(enrichedThreatId, credentials) {
297
- const stmt = this.db.prepare('INSERT INTO trap_credentials (enriched_threat_id, username, attempt_count) VALUES (?, ?, ?)');
298
- const insertAll = this.db.transaction((creds) => {
299
- for (const c of creds) {
300
- const hashedUsername = createHash('sha256').update(c.username).digest('hex').slice(0, 16);
301
- stmt.run(enrichedThreatId, hashedUsername, c.count);
302
- }
303
- });
304
- insertAll(credentials);
305
- }
306
- /** Get enriched threats count by source type / 依來源類型取得豐富化威脅數量 */
307
- getEnrichedThreatCountBySource() {
308
- const rows = this.db
309
- .prepare('SELECT source_type, COUNT(*) as count FROM enriched_threats GROUP BY source_type')
310
- .all();
311
- const result = {};
312
- for (const r of rows) {
313
- result[r.source_type] = r.count;
314
- }
315
- return result;
316
- }
317
- /** Count related threats for an IP / 計算某 IP 的相關威脅數量 */
318
- countRelatedThreats(ip) {
319
- return this.db
320
- .prepare('SELECT COUNT(*) as count FROM enriched_threats WHERE attack_source_ip = ?')
321
- .get(ip).count;
322
- }
323
- // -------------------------------------------------------------------------
324
- // Conversion helpers / 轉換輔助函式
325
- // -------------------------------------------------------------------------
326
- /** Convert AnonymizedThreatData to EnrichedThreatEvent / 轉換 Guard 資料 */
327
- static guardToEnriched(data) {
328
- const hashInput = `${data.attackSourceIP}|${data.attackType}|${data.mitreTechnique}|${data.timestamp}`;
329
- const eventHash = createHash('sha256').update(hashInput).digest('hex');
330
- return {
331
- sourceType: 'guard',
332
- attackSourceIP: data.attackSourceIP,
333
- attackType: data.attackType,
334
- mitreTechniques: [data.mitreTechnique],
335
- sigmaRuleMatched: data.sigmaRuleMatched,
336
- timestamp: data.timestamp,
337
- industry: data.industry,
338
- region: data.region,
339
- confidence: 50,
340
- severity: 'medium',
341
- eventHash,
342
- receivedAt: new Date().toISOString(),
343
- };
344
- }
345
- /** Convert TrapIntelligencePayload to EnrichedThreatEvent / 轉換 Trap 資料 */
346
- static trapToEnriched(data) {
347
- const techniques = data.mitreTechniques ?? [];
348
- const hashInput = `${data.sourceIP}|${data.attackType}|${techniques.join(',')}|${data.timestamp}`;
349
- const eventHash = createHash('sha256').update(hashInput).digest('hex');
350
- return {
351
- sourceType: 'trap',
352
- attackSourceIP: data.sourceIP,
353
- attackType: data.attackType,
354
- mitreTechniques: techniques,
355
- sigmaRuleMatched: '',
356
- timestamp: data.timestamp,
357
- region: data.region ?? 'unknown',
358
- confidence: 60,
359
- severity: data.skillLevel === 'apt' || data.skillLevel === 'advanced' ? 'high' : 'medium',
360
- serviceType: data.serviceType,
361
- skillLevel: data.skillLevel,
362
- intent: data.intent,
363
- tools: data.tools,
364
- eventHash,
365
- receivedAt: new Date().toISOString(),
366
- };
367
- }
368
- // -------------------------------------------------------------------------
369
- // Rules / 規則
370
- // -------------------------------------------------------------------------
371
125
  /** Insert or update a community rule / 插入或更新社群規則 */
372
126
  upsertRule(rule) {
373
127
  const stmt = this.db.prepare(`
@@ -391,51 +145,248 @@ export class ThreatCloudDB {
391
145
  `);
392
146
  return stmt.all(since);
393
147
  }
394
- /** Fetch all rules / 取得所有規則 */
395
- getAllRules() {
148
+ /** Fetch all rules with limit / 取得所有規則(含限制) */
149
+ getAllRules(limit = 5000) {
396
150
  const stmt = this.db.prepare(`
397
151
  SELECT rule_id as ruleId, rule_content as ruleContent, published_at as publishedAt, source
398
152
  FROM rules
399
153
  ORDER BY published_at DESC
154
+ LIMIT ?
155
+ `);
156
+ return stmt.all(limit);
157
+ }
158
+ /** Insert ATR rule proposal / 插入 ATR 規則提案 */
159
+ insertATRProposal(proposal) {
160
+ const stmt = this.db.prepare(`
161
+ INSERT INTO atr_proposals (pattern_hash, rule_content, llm_provider, llm_model, self_review_verdict, client_id)
162
+ VALUES (?, ?, ?, ?, ?, ?)
163
+ `);
164
+ stmt.run(proposal.patternHash, proposal.ruleContent, proposal.llmProvider, proposal.llmModel, proposal.selfReviewVerdict, proposal.clientId ?? null);
165
+ }
166
+ /** Get ATR proposals, optionally filtered by status / 取得 ATR 提案 */
167
+ getATRProposals(status) {
168
+ if (status) {
169
+ return this.db.prepare('SELECT * FROM atr_proposals WHERE status = ? ORDER BY created_at DESC').all(status);
170
+ }
171
+ return this.db.prepare('SELECT * FROM atr_proposals ORDER BY created_at DESC').all();
172
+ }
173
+ /** Increment confirmations for a proposal; auto-confirm at >= 3 / 增加提案確認數 */
174
+ confirmATRProposal(patternHash) {
175
+ this.db.prepare(`
176
+ UPDATE atr_proposals
177
+ SET confirmations = confirmations + 1,
178
+ status = CASE WHEN confirmations + 1 >= 3 THEN 'confirmed' ELSE status END,
179
+ updated_at = datetime('now')
180
+ WHERE pattern_hash = ?
181
+ `).run(patternHash);
182
+ }
183
+ /** Update LLM review verdict for a proposal / 更新 LLM 審查結果 */
184
+ updateATRProposalLLMReview(patternHash, verdict) {
185
+ this.db.prepare(`
186
+ UPDATE atr_proposals SET llm_review_verdict = ?, updated_at = datetime('now') WHERE pattern_hash = ?
187
+ `).run(verdict, patternHash);
188
+ }
189
+ /** Insert ATR feedback / 插入 ATR 回饋 */
190
+ insertATRFeedback(ruleId, isTruePositive, clientId) {
191
+ this.db.prepare(`
192
+ INSERT INTO atr_feedback (rule_id, is_true_positive, client_id) VALUES (?, ?, ?)
193
+ `).run(ruleId, isTruePositive ? 1 : 0, clientId ?? null);
194
+ }
195
+ /** Get feedback stats for a rule / 取得規則回饋統計 */
196
+ getATRFeedbackStats(ruleId) {
197
+ const tp = this.db.prepare('SELECT COUNT(*) as count FROM atr_feedback WHERE rule_id = ? AND is_true_positive = 1').get(ruleId).count;
198
+ const fp = this.db.prepare('SELECT COUNT(*) as count FROM atr_feedback WHERE rule_id = ? AND is_true_positive = 0').get(ruleId).count;
199
+ return { truePositives: tp, falsePositives: fp };
200
+ }
201
+ /** Insert skill threat submission / 插入技能威脅提交 */
202
+ insertSkillThreat(submission) {
203
+ const stmt = this.db.prepare(`
204
+ INSERT INTO skill_threats (skill_hash, skill_name, risk_score, risk_level, finding_summaries, client_id)
205
+ VALUES (?, ?, ?, ?, ?, ?)
400
206
  `);
401
- return stmt.all();
207
+ stmt.run(submission.skillHash, submission.skillName, submission.riskScore, submission.riskLevel, submission.findingSummaries ? JSON.stringify(submission.findingSummaries) : null, submission.clientId ?? null);
208
+ }
209
+ /** Get recent skill threats / 取得最近技能威脅 */
210
+ getSkillThreats(limit = 50) {
211
+ return this.db.prepare('SELECT * FROM skill_threats ORDER BY created_at DESC LIMIT ?').all(limit);
212
+ }
213
+ /** Get proposal statistics / 取得提案統計 */
214
+ getProposalStats() {
215
+ const pending = this.db.prepare("SELECT COUNT(*) as count FROM atr_proposals WHERE status = 'pending'").get().count;
216
+ const confirmed = this.db.prepare("SELECT COUNT(*) as count FROM atr_proposals WHERE status = 'confirmed'").get().count;
217
+ const rejected = this.db.prepare("SELECT COUNT(*) as count FROM atr_proposals WHERE status = 'rejected'").get().count;
218
+ const total = this.db.prepare('SELECT COUNT(*) as count FROM atr_proposals').get().count;
219
+ return { pending, confirmed, rejected, total };
402
220
  }
403
- // -------------------------------------------------------------------------
404
- // Statistics / 統計
405
- // -------------------------------------------------------------------------
406
221
  /** Get threat statistics / 取得威脅統計 */
407
222
  getStats() {
408
223
  const totalThreats = this.db.prepare('SELECT COUNT(*) as count FROM threats').get().count;
409
224
  const totalRules = this.db.prepare('SELECT COUNT(*) as count FROM rules').get().count;
410
- const last24h = this.db
411
- .prepare("SELECT COUNT(*) as count FROM threats WHERE received_at > datetime('now', '-1 day')")
412
- .get().count;
413
- const topAttackTypes = this.db
414
- .prepare(`
225
+ const last24h = this.db.prepare("SELECT COUNT(*) as count FROM threats WHERE received_at > datetime('now', '-1 day')").get().count;
226
+ const topAttackTypes = this.db.prepare(`
415
227
  SELECT attack_type as type, COUNT(*) as count
416
228
  FROM threats
417
229
  GROUP BY attack_type
418
230
  ORDER BY count DESC
419
231
  LIMIT 10
420
- `)
421
- .all();
422
- const topMitreTechniques = this.db
423
- .prepare(`
232
+ `).all();
233
+ const topMitreTechniques = this.db.prepare(`
424
234
  SELECT mitre_technique as technique, COUNT(*) as count
425
235
  FROM threats
426
236
  GROUP BY mitre_technique
427
237
  ORDER BY count DESC
428
238
  LIMIT 10
429
- `)
430
- .all();
239
+ `).all();
240
+ const proposalStats = this.getProposalStats();
241
+ const skillThreatsTotal = this.db.prepare('SELECT COUNT(*) as count FROM skill_threats').get().count;
431
242
  return {
432
243
  totalThreats,
433
244
  totalRules,
434
245
  topAttackTypes,
435
246
  topMitreTechniques,
436
247
  last24hThreats: last24h,
248
+ proposalStats,
249
+ skillThreatsTotal,
437
250
  };
438
251
  }
252
+ /** Get confirmed/promoted ATR rules, optionally filtered by date / 取得已確認 ATR 規則 */
253
+ getConfirmedATRRules(since) {
254
+ if (since) {
255
+ return this.db.prepare(`
256
+ SELECT pattern_hash as ruleId, rule_content as ruleContent, updated_at as publishedAt, 'atr-community' as source
257
+ FROM atr_proposals
258
+ WHERE (status = 'confirmed' OR status = 'promoted') AND updated_at > ?
259
+ ORDER BY updated_at ASC
260
+ `).all(since);
261
+ }
262
+ return this.db.prepare(`
263
+ SELECT pattern_hash as ruleId, rule_content as ruleContent, updated_at as publishedAt, 'atr-community' as source
264
+ FROM atr_proposals
265
+ WHERE status = 'confirmed' OR status = 'promoted'
266
+ ORDER BY updated_at ASC
267
+ `).all();
268
+ }
269
+ /** Get IP blocklist from IoC entries and aggregated threat data / 取得 IP 封鎖清單 */
270
+ getIPBlocklist(minReputation) {
271
+ // IoC entries with sufficient reputation
272
+ const iocIPs = this.db.prepare(`
273
+ SELECT value FROM ioc_entries
274
+ WHERE type = 'ip' AND reputation >= ?
275
+ ORDER BY reputation DESC
276
+ `).all(minReputation);
277
+ // Aggregate from threats table: distinct IPs with >= 3 occurrences
278
+ const threatIPs = this.db.prepare(`
279
+ SELECT attack_source_ip as value
280
+ FROM threats
281
+ GROUP BY attack_source_ip
282
+ HAVING COUNT(*) >= 3
283
+ `).all();
284
+ // Merge and deduplicate
285
+ const ipSet = new Set();
286
+ for (const row of iocIPs)
287
+ ipSet.add(row.value);
288
+ for (const row of threatIPs)
289
+ ipSet.add(row.value);
290
+ return Array.from(ipSet);
291
+ }
292
+ /** Get domain blocklist from IoC entries / 取得域名封鎖清單 */
293
+ getDomainBlocklist(minReputation) {
294
+ const iocDomains = this.db.prepare(`
295
+ SELECT value FROM ioc_entries
296
+ WHERE type = 'domain' AND reputation >= ?
297
+ ORDER BY reputation DESC
298
+ `).all(minReputation);
299
+ return iocDomains.map((row) => row.value);
300
+ }
301
+ /** Upsert an IoC entry / 插入或更新 IoC 條目 */
302
+ upsertIoC(type, value, reputation, source) {
303
+ this.db.prepare(`
304
+ INSERT INTO ioc_entries (type, value, reputation, source)
305
+ VALUES (?, ?, ?, ?)
306
+ ON CONFLICT(value) DO UPDATE SET
307
+ reputation = excluded.reputation,
308
+ source = excluded.source,
309
+ last_seen = datetime('now'),
310
+ sighting_count = sighting_count + 1
311
+ `).run(type, value, reputation, source);
312
+ }
313
+ /** Promote confirmed proposals with approved LLM review to rules / 推廣已確認提案為規則 */
314
+ promoteConfirmedProposals() {
315
+ const proposals = this.db.prepare(`
316
+ SELECT pattern_hash, rule_content, llm_review_verdict
317
+ FROM atr_proposals
318
+ WHERE status = 'confirmed' AND llm_review_verdict IS NOT NULL
319
+ `).all();
320
+ let promoted = 0;
321
+ for (const proposal of proposals) {
322
+ try {
323
+ const verdict = JSON.parse(proposal.llm_review_verdict);
324
+ if (verdict.approved !== true)
325
+ continue;
326
+ this.upsertRule({
327
+ ruleId: proposal.pattern_hash,
328
+ ruleContent: proposal.rule_content,
329
+ publishedAt: new Date().toISOString(),
330
+ source: 'atr-community',
331
+ });
332
+ this.db.prepare(`
333
+ UPDATE atr_proposals SET status = 'promoted', updated_at = datetime('now')
334
+ WHERE pattern_hash = ?
335
+ `).run(proposal.pattern_hash);
336
+ promoted++;
337
+ }
338
+ catch {
339
+ // Skip proposals with unparseable verdicts
340
+ }
341
+ }
342
+ return promoted;
343
+ }
344
+ /** Reject an ATR proposal / 拒絕 ATR 提案 */
345
+ rejectATRProposal(patternHash) {
346
+ this.db.prepare(`
347
+ UPDATE atr_proposals SET status = 'rejected', updated_at = datetime('now')
348
+ WHERE pattern_hash = ?
349
+ `).run(patternHash);
350
+ }
351
+ /** Get rules by source type, optionally filtered by date / 依來源取得規則 */
352
+ getRulesBySource(source, since) {
353
+ if (since) {
354
+ return this.db.prepare(`
355
+ SELECT rule_id as ruleId, rule_content as ruleContent, published_at as publishedAt, source
356
+ FROM rules
357
+ WHERE source = ? AND published_at > ?
358
+ ORDER BY published_at ASC
359
+ `).all(source, since);
360
+ }
361
+ return this.db.prepare(`
362
+ SELECT rule_id as ruleId, rule_content as ruleContent, published_at as publishedAt, source
363
+ FROM rules
364
+ WHERE source = ?
365
+ ORDER BY published_at ASC
366
+ `).all(source);
367
+ }
368
+ /** Report a safe skill (increment confirmations, auto-confirm at 3+) / 回報安全 skill */
369
+ reportSafeSkill(skillName, fingerprintHash) {
370
+ const normalized = skillName.toLowerCase().trim().replace(/\s+/g, '-');
371
+ this.db.prepare(`
372
+ INSERT INTO skill_whitelist (skill_name, normalized_name, fingerprint_hash)
373
+ VALUES (?, ?, ?)
374
+ ON CONFLICT(normalized_name) DO UPDATE SET
375
+ confirmations = confirmations + 1,
376
+ status = CASE WHEN confirmations + 1 >= 3 THEN 'confirmed' ELSE status END,
377
+ fingerprint_hash = COALESCE(excluded.fingerprint_hash, fingerprint_hash),
378
+ last_reported = datetime('now')
379
+ `).run(skillName, normalized, fingerprintHash ?? null);
380
+ }
381
+ /** Get confirmed community whitelist / 取得社群白名單 */
382
+ getSkillWhitelist() {
383
+ return this.db.prepare(`
384
+ SELECT skill_name as name, fingerprint_hash as hash, confirmations
385
+ FROM skill_whitelist
386
+ WHERE status = 'confirmed'
387
+ ORDER BY confirmations DESC
388
+ `).all();
389
+ }
439
390
  /** Close the database / 關閉資料庫 */
440
391
  close() {
441
392
  this.db.close();