pompelmi 0.35.5 → 1.0.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 (133) hide show
  1. package/.claude/settings.local.json +40 -0
  2. package/LICENSE +12 -18
  3. package/README.md +159 -183
  4. package/eslint.config.mjs +8 -0
  5. package/package.json +26 -251
  6. package/src/ClamAVDatabaseUpdater.js +48 -0
  7. package/src/ClamAVInstaller.js +49 -0
  8. package/src/ClamAVScanner.js +31 -0
  9. package/src/InstallerCommand.js +11 -0
  10. package/src/config.js +22 -0
  11. package/src/constants.js +3 -0
  12. package/src/favicon.ico +0 -0
  13. package/src/grapefruit.png +0 -0
  14. package/src/index.js +5 -0
  15. package/CHANGELOG.md +0 -71
  16. package/dist/pompelmi.audit.cjs +0 -128
  17. package/dist/pompelmi.audit.cjs.map +0 -1
  18. package/dist/pompelmi.audit.esm.js +0 -107
  19. package/dist/pompelmi.audit.esm.js.map +0 -1
  20. package/dist/pompelmi.browser.cjs +0 -1549
  21. package/dist/pompelmi.browser.cjs.map +0 -1
  22. package/dist/pompelmi.browser.esm.js +0 -1523
  23. package/dist/pompelmi.browser.esm.js.map +0 -1
  24. package/dist/pompelmi.cjs +0 -2591
  25. package/dist/pompelmi.cjs.map +0 -1
  26. package/dist/pompelmi.esm.js +0 -2525
  27. package/dist/pompelmi.esm.js.map +0 -1
  28. package/dist/pompelmi.hooks.cjs +0 -75
  29. package/dist/pompelmi.hooks.cjs.map +0 -1
  30. package/dist/pompelmi.hooks.esm.js +0 -72
  31. package/dist/pompelmi.hooks.esm.js.map +0 -1
  32. package/dist/pompelmi.policy-packs.cjs +0 -240
  33. package/dist/pompelmi.policy-packs.cjs.map +0 -1
  34. package/dist/pompelmi.policy-packs.esm.js +0 -232
  35. package/dist/pompelmi.policy-packs.esm.js.map +0 -1
  36. package/dist/pompelmi.quarantine.cjs +0 -317
  37. package/dist/pompelmi.quarantine.cjs.map +0 -1
  38. package/dist/pompelmi.quarantine.esm.js +0 -293
  39. package/dist/pompelmi.quarantine.esm.js.map +0 -1
  40. package/dist/pompelmi.react.cjs +0 -1580
  41. package/dist/pompelmi.react.cjs.map +0 -1
  42. package/dist/pompelmi.react.esm.js +0 -1553
  43. package/dist/pompelmi.react.esm.js.map +0 -1
  44. package/dist/types/audit.d.ts +0 -84
  45. package/dist/types/browser-index.d.ts +0 -29
  46. package/dist/types/config.d.ts +0 -143
  47. package/dist/types/engines/dynamic-taint.d.ts +0 -102
  48. package/dist/types/engines/hybrid-orchestrator.d.ts +0 -65
  49. package/dist/types/engines/hybrid-taint-integration.d.ts +0 -129
  50. package/dist/types/engines/taint-policies.d.ts +0 -84
  51. package/dist/types/hipaa-compliance.d.ts +0 -110
  52. package/dist/types/hooks.d.ts +0 -89
  53. package/dist/types/index.d.ts +0 -29
  54. package/dist/types/magic.d.ts +0 -7
  55. package/dist/types/node/scanDir.d.ts +0 -30
  56. package/dist/types/policy-packs.d.ts +0 -98
  57. package/dist/types/policy.d.ts +0 -12
  58. package/dist/types/presets.d.ts +0 -72
  59. package/dist/types/quarantine/index.d.ts +0 -18
  60. package/dist/types/quarantine/storage.d.ts +0 -77
  61. package/dist/types/quarantine/types.d.ts +0 -78
  62. package/dist/types/quarantine/workflow.d.ts +0 -97
  63. package/dist/types/react-index.d.ts +0 -13
  64. package/dist/types/risk.d.ts +0 -18
  65. package/dist/types/scan/remote.d.ts +0 -12
  66. package/dist/types/scan.d.ts +0 -17
  67. package/dist/types/scanners/common-heuristics.d.ts +0 -14
  68. package/dist/types/scanners/zip-bomb-guard.d.ts +0 -9
  69. package/dist/types/scanners/zipTraversalGuard.d.ts +0 -19
  70. package/dist/types/src/audit.d.ts +0 -84
  71. package/dist/types/src/browser-index.d.ts +0 -29
  72. package/dist/types/src/config.d.ts +0 -143
  73. package/dist/types/src/engines/dynamic-taint.d.ts +0 -102
  74. package/dist/types/src/engines/hybrid-orchestrator.d.ts +0 -65
  75. package/dist/types/src/engines/hybrid-taint-integration.d.ts +0 -129
  76. package/dist/types/src/engines/taint-policies.d.ts +0 -84
  77. package/dist/types/src/hipaa-compliance.d.ts +0 -110
  78. package/dist/types/src/hooks.d.ts +0 -89
  79. package/dist/types/src/index.d.ts +0 -29
  80. package/dist/types/src/magic.d.ts +0 -7
  81. package/dist/types/src/node/scanDir.d.ts +0 -30
  82. package/dist/types/src/policy-packs.d.ts +0 -98
  83. package/dist/types/src/policy.d.ts +0 -12
  84. package/dist/types/src/presets.d.ts +0 -72
  85. package/dist/types/src/quarantine/index.d.ts +0 -18
  86. package/dist/types/src/quarantine/storage.d.ts +0 -77
  87. package/dist/types/src/quarantine/types.d.ts +0 -78
  88. package/dist/types/src/quarantine/workflow.d.ts +0 -97
  89. package/dist/types/src/react-index.d.ts +0 -13
  90. package/dist/types/src/risk.d.ts +0 -18
  91. package/dist/types/src/scan/remote.d.ts +0 -12
  92. package/dist/types/src/scan.d.ts +0 -17
  93. package/dist/types/src/scanners/common-heuristics.d.ts +0 -14
  94. package/dist/types/src/scanners/zip-bomb-guard.d.ts +0 -11
  95. package/dist/types/src/scanners/zipTraversalGuard.d.ts +0 -19
  96. package/dist/types/src/stream.d.ts +0 -10
  97. package/dist/types/src/types/decompilation.d.ts +0 -96
  98. package/dist/types/src/types/taint-tracking.d.ts +0 -495
  99. package/dist/types/src/types.d.ts +0 -48
  100. package/dist/types/src/useFileScanner.d.ts +0 -15
  101. package/dist/types/src/utils/advanced-detection.d.ts +0 -21
  102. package/dist/types/src/utils/batch-scanner.d.ts +0 -62
  103. package/dist/types/src/utils/cache-manager.d.ts +0 -95
  104. package/dist/types/src/utils/export.d.ts +0 -51
  105. package/dist/types/src/utils/performance-metrics.d.ts +0 -68
  106. package/dist/types/src/utils/threat-intelligence.d.ts +0 -96
  107. package/dist/types/src/validate.d.ts +0 -7
  108. package/dist/types/src/verdict.d.ts +0 -2
  109. package/dist/types/src/yara/browser.d.ts +0 -7
  110. package/dist/types/src/yara/index.d.ts +0 -17
  111. package/dist/types/src/yara/node.d.ts +0 -2
  112. package/dist/types/src/yara/remote.d.ts +0 -10
  113. package/dist/types/src/yara-bridge.d.ts +0 -3
  114. package/dist/types/src/zip.d.ts +0 -13
  115. package/dist/types/stream.d.ts +0 -10
  116. package/dist/types/types/decompilation.d.ts +0 -96
  117. package/dist/types/types/taint-tracking.d.ts +0 -495
  118. package/dist/types/types.d.ts +0 -48
  119. package/dist/types/useFileScanner.d.ts +0 -15
  120. package/dist/types/utils/advanced-detection.d.ts +0 -21
  121. package/dist/types/utils/batch-scanner.d.ts +0 -62
  122. package/dist/types/utils/cache-manager.d.ts +0 -95
  123. package/dist/types/utils/export.d.ts +0 -51
  124. package/dist/types/utils/performance-metrics.d.ts +0 -68
  125. package/dist/types/utils/threat-intelligence.d.ts +0 -96
  126. package/dist/types/validate.d.ts +0 -7
  127. package/dist/types/verdict.d.ts +0 -2
  128. package/dist/types/yara/browser.d.ts +0 -7
  129. package/dist/types/yara/index.d.ts +0 -17
  130. package/dist/types/yara/node.d.ts +0 -2
  131. package/dist/types/yara/remote.d.ts +0 -10
  132. package/dist/types/yara-bridge.d.ts +0 -3
  133. package/dist/types/zip.d.ts +0 -13
package/dist/pompelmi.cjs DELETED
@@ -1,2591 +0,0 @@
1
- 'use strict';
2
-
3
- var crypto = require('crypto');
4
- var os = require('os');
5
- var path = require('path');
6
- var zlib = require('zlib');
7
-
8
- function _interopNamespaceDefault(e) {
9
- var n = Object.create(null);
10
- if (e) {
11
- Object.keys(e).forEach(function (k) {
12
- if (k !== 'default') {
13
- var d = Object.getOwnPropertyDescriptor(e, k);
14
- Object.defineProperty(n, k, d.get ? d : {
15
- enumerable: true,
16
- get: function () { return e[k]; }
17
- });
18
- }
19
- });
20
- }
21
- n.default = e;
22
- return Object.freeze(n);
23
- }
24
-
25
- var crypto__namespace = /*#__PURE__*/_interopNamespaceDefault(crypto);
26
- var os__namespace = /*#__PURE__*/_interopNamespaceDefault(os);
27
- var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
28
-
29
- /**
30
- * Advanced configuration system for pompelmi
31
- * @module config
32
- */
33
- /**
34
- * Default configuration
35
- */
36
- const DEFAULT_CONFIG = {
37
- defaultPreset: "zip-basic",
38
- performance: {
39
- enableCache: false,
40
- enablePerformanceTracking: false,
41
- enableParallel: true,
42
- maxConcurrency: 5,
43
- cacheOptions: {
44
- maxSize: 1000,
45
- ttl: 3600000, // 1 hour
46
- enableLRU: true,
47
- enableStats: false,
48
- },
49
- },
50
- security: {
51
- maxFileSize: 100 * 1024 * 1024, // 100MB
52
- enableThreatIntel: false,
53
- scanTimeout: 30000, // 30 seconds
54
- strictMode: false,
55
- },
56
- advanced: {
57
- enablePolyglotDetection: true,
58
- enableObfuscationDetection: true,
59
- enableNestedArchiveAnalysis: true,
60
- maxArchiveDepth: 5,
61
- },
62
- logging: {
63
- verbose: false,
64
- level: "info",
65
- enableStats: false,
66
- },
67
- };
68
- /**
69
- * Configuration presets for common use cases
70
- */
71
- const CONFIG_PRESETS = {
72
- /** Fast scanning with minimal features */
73
- fast: {
74
- defaultPreset: "basic",
75
- performance: {
76
- enableCache: true,
77
- enablePerformanceTracking: false,
78
- maxConcurrency: 10,
79
- },
80
- advanced: {
81
- enablePolyglotDetection: false,
82
- enableObfuscationDetection: false,
83
- enableNestedArchiveAnalysis: false,
84
- },
85
- },
86
- /** Balanced scanning (recommended) */
87
- balanced: DEFAULT_CONFIG,
88
- /** Thorough scanning with all features */
89
- thorough: {
90
- defaultPreset: "advanced",
91
- performance: {
92
- enableCache: true,
93
- enablePerformanceTracking: true,
94
- maxConcurrency: 3,
95
- },
96
- security: {
97
- maxFileSize: 500 * 1024 * 1024, // 500MB
98
- enableThreatIntel: true,
99
- scanTimeout: 60000, // 60 seconds
100
- strictMode: true,
101
- },
102
- advanced: {
103
- enablePolyglotDetection: true,
104
- enableObfuscationDetection: true,
105
- enableNestedArchiveAnalysis: true,
106
- maxArchiveDepth: 10,
107
- },
108
- logging: {
109
- verbose: true,
110
- level: "debug",
111
- enableStats: true,
112
- },
113
- },
114
- /** Production-ready configuration */
115
- production: {
116
- defaultPreset: "advanced",
117
- performance: {
118
- enableCache: true,
119
- enablePerformanceTracking: true,
120
- maxConcurrency: 5,
121
- cacheOptions: {
122
- maxSize: 5000,
123
- ttl: 7200000, // 2 hours
124
- enableLRU: true,
125
- enableStats: true,
126
- },
127
- },
128
- security: {
129
- maxFileSize: 200 * 1024 * 1024, // 200MB
130
- enableThreatIntel: true,
131
- scanTimeout: 45000,
132
- strictMode: false,
133
- },
134
- advanced: {
135
- enablePolyglotDetection: true,
136
- enableObfuscationDetection: true,
137
- enableNestedArchiveAnalysis: true,
138
- maxArchiveDepth: 7,
139
- },
140
- logging: {
141
- verbose: false,
142
- level: "warn",
143
- enableStats: true,
144
- },
145
- },
146
- /** Development configuration */
147
- development: {
148
- defaultPreset: "basic",
149
- performance: {
150
- enableCache: false,
151
- enablePerformanceTracking: true,
152
- maxConcurrency: 3,
153
- },
154
- security: {
155
- maxFileSize: 50 * 1024 * 1024, // 50MB
156
- scanTimeout: 15000,
157
- strictMode: false,
158
- },
159
- logging: {
160
- verbose: true,
161
- level: "debug",
162
- enableStats: true,
163
- },
164
- },
165
- };
166
- /**
167
- * Configuration manager
168
- */
169
- class ConfigManager {
170
- constructor(initialConfig) {
171
- this.config = this.mergeConfig(DEFAULT_CONFIG, initialConfig || {});
172
- }
173
- /**
174
- * Get current configuration
175
- */
176
- getConfig() {
177
- return { ...this.config };
178
- }
179
- /**
180
- * Update configuration
181
- */
182
- updateConfig(updates) {
183
- this.config = this.mergeConfig(this.config, updates);
184
- }
185
- /**
186
- * Load a preset configuration
187
- */
188
- loadPreset(preset) {
189
- const presetConfig = CONFIG_PRESETS[preset];
190
- this.config = this.mergeConfig(DEFAULT_CONFIG, presetConfig);
191
- }
192
- /**
193
- * Reset to default configuration
194
- */
195
- reset() {
196
- this.config = { ...DEFAULT_CONFIG };
197
- }
198
- /**
199
- * Get a specific configuration value
200
- */
201
- get(key) {
202
- return this.config[key];
203
- }
204
- /**
205
- * Set a specific configuration value
206
- */
207
- set(key, value) {
208
- this.config[key] = value;
209
- }
210
- /**
211
- * Validate configuration
212
- */
213
- validate() {
214
- const errors = [];
215
- // Validate performance settings
216
- if (this.config.performance?.maxConcurrency !== undefined) {
217
- if (this.config.performance.maxConcurrency < 1) {
218
- errors.push("maxConcurrency must be at least 1");
219
- }
220
- if (this.config.performance.maxConcurrency > 50) {
221
- errors.push("maxConcurrency should not exceed 50");
222
- }
223
- }
224
- // Validate security settings
225
- if (this.config.security?.maxFileSize !== undefined) {
226
- if (this.config.security.maxFileSize < 1024) {
227
- errors.push("maxFileSize must be at least 1KB");
228
- }
229
- }
230
- if (this.config.security?.scanTimeout !== undefined) {
231
- if (this.config.security.scanTimeout < 1000) {
232
- errors.push("scanTimeout must be at least 1000ms");
233
- }
234
- }
235
- // Validate advanced settings
236
- if (this.config.advanced?.maxArchiveDepth !== undefined) {
237
- if (this.config.advanced.maxArchiveDepth < 1) {
238
- errors.push("maxArchiveDepth must be at least 1");
239
- }
240
- if (this.config.advanced.maxArchiveDepth > 20) {
241
- errors.push("maxArchiveDepth should not exceed 20");
242
- }
243
- }
244
- return {
245
- valid: errors.length === 0,
246
- errors,
247
- };
248
- }
249
- /**
250
- * Deep merge configuration objects
251
- */
252
- mergeConfig(base, updates) {
253
- return {
254
- ...base,
255
- ...updates,
256
- performance: {
257
- ...base.performance,
258
- ...updates.performance,
259
- cacheOptions: {
260
- ...base.performance?.cacheOptions,
261
- ...updates.performance?.cacheOptions,
262
- },
263
- },
264
- security: {
265
- ...base.security,
266
- ...updates.security,
267
- },
268
- advanced: {
269
- ...base.advanced,
270
- ...updates.advanced,
271
- },
272
- logging: {
273
- ...base.logging,
274
- ...updates.logging,
275
- },
276
- callbacks: {
277
- ...base.callbacks,
278
- ...updates.callbacks,
279
- },
280
- presetOptions: {
281
- ...base.presetOptions,
282
- ...updates.presetOptions,
283
- },
284
- };
285
- }
286
- /**
287
- * Export configuration as JSON
288
- */
289
- toJSON() {
290
- return JSON.stringify(this.config, null, 2);
291
- }
292
- /**
293
- * Load configuration from JSON
294
- */
295
- fromJSON(json) {
296
- try {
297
- const parsed = JSON.parse(json);
298
- this.config = this.mergeConfig(DEFAULT_CONFIG, parsed);
299
- }
300
- catch (error) {
301
- throw new Error(`Failed to parse configuration JSON: ${error}`);
302
- }
303
- }
304
- }
305
- /**
306
- * Create a new configuration manager
307
- */
308
- function createConfig(config) {
309
- return new ConfigManager(config);
310
- }
311
- /**
312
- * Get a preset configuration
313
- */
314
- function getPresetConfig(preset) {
315
- return { ...DEFAULT_CONFIG, ...CONFIG_PRESETS[preset] };
316
- }
317
-
318
- /**
319
- * HIPAA Compliance Module for Pompelmi
320
- *
321
- * This module provides comprehensive HIPAA compliance features for healthcare environments
322
- * where Pompelmi is used to analyze potentially compromised systems containing PHI.
323
- *
324
- * Key protections:
325
- * - Data sanitization and redaction
326
- * - Secure temporary file handling
327
- * - Audit logging
328
- * - Memory protection
329
- * - Error message sanitization
330
- */
331
- class HipaaComplianceManager {
332
- constructor(config) {
333
- this.auditEvents = [];
334
- this.config = {
335
- sanitizeErrors: true,
336
- sanitizeFilenames: true,
337
- encryptTempFiles: true,
338
- memoryProtection: true,
339
- requireSecureTransport: true,
340
- ...config,
341
- enabled: config.enabled !== undefined ? config.enabled : true,
342
- };
343
- this.sessionId = this.generateSessionId();
344
- }
345
- /**
346
- * Sanitize filename to prevent PHI leakage in logs
347
- */
348
- sanitizeFilename(filename) {
349
- if (!this.config.enabled || !this.config.sanitizeFilenames || !filename) {
350
- return filename || "unknown";
351
- }
352
- // Remove potentially sensitive path information
353
- const basename = path__namespace.basename(filename);
354
- // Hash the filename to create a consistent but non-revealing identifier
355
- const hash = crypto__namespace.createHash("sha256").update(basename).digest("hex").substring(0, 8);
356
- // Preserve file extension for analysis purposes
357
- const ext = path__namespace.extname(basename);
358
- return `file_${hash}${ext}`;
359
- }
360
- /**
361
- * Sanitize error messages to prevent PHI exposure
362
- */
363
- sanitizeError(error) {
364
- if (!this.config.enabled || !this.config.sanitizeErrors) {
365
- return typeof error === "string" ? error : error.message;
366
- }
367
- const message = typeof error === "string" ? error : error.message;
368
- // Remove common patterns that might contain PHI
369
- const sanitized = message
370
- // Remove file paths
371
- .replace(/[A-Za-z]:\\\\[^\\s]+/g, "[REDACTED_PATH]")
372
- .replace(/\/[^\\s]+/g, "[REDACTED_PATH]")
373
- // Remove potential patient identifiers (numbers that could be MRNs, SSNs)
374
- .replace(/\\b\\d{3}-?\\d{2}-?\\d{4}\\b/g, "[REDACTED_ID]")
375
- .replace(/\\b\\d{6,}\\b/g, "[REDACTED_ID]")
376
- // Remove email addresses
377
- .replace(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}/g, "[REDACTED_EMAIL]")
378
- // Remove potential names (capitalize words in error messages)
379
- .replace(/\\b[A-Z][a-z]+\\s+[A-Z][a-z]+\\b/g, "[REDACTED_NAME]")
380
- // Remove IP addresses
381
- .replace(/\\b(?:\\d{1,3}\\.){3}\\d{1,3}\\b/g, "[REDACTED_IP]");
382
- return sanitized;
383
- }
384
- /**
385
- * Create secure temporary file path with encryption if enabled
386
- */
387
- createSecureTempPath(prefix = "pompelmi") {
388
- if (!this.config.enabled) {
389
- return path__namespace.join(os__namespace.tmpdir(), `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2)}`);
390
- }
391
- // Use cryptographically secure random names
392
- const randomId = crypto__namespace.randomBytes(16).toString("hex");
393
- const timestamp = Date.now();
394
- // Create path in secure temp directory
395
- const secureTempDir = this.getSecureTempDir();
396
- const tempPath = path__namespace.join(secureTempDir, `${prefix}-${timestamp}-${randomId}`);
397
- this.auditLog("temp_file_created", {
398
- action: "create_temp_file",
399
- success: true,
400
- metadata: { path: this.sanitizeFilename(tempPath) },
401
- });
402
- return tempPath;
403
- }
404
- /**
405
- * Get or create secure temporary directory with restricted permissions
406
- */
407
- getSecureTempDir() {
408
- const secureTempPath = path__namespace.join(os__namespace.tmpdir(), "pompelmi-secure");
409
- try {
410
- const fs = require("fs");
411
- if (!fs.existsSync(secureTempPath)) {
412
- fs.mkdirSync(secureTempPath, { mode: 0o700 }); // Owner read/write/execute only
413
- }
414
- }
415
- catch (error) {
416
- // Fallback to system temp
417
- return os__namespace.tmpdir();
418
- }
419
- return secureTempPath;
420
- }
421
- /**
422
- * Secure file cleanup with multiple overwrite passes
423
- */
424
- async secureFileCleanup(filePath) {
425
- if (!this.config.enabled) {
426
- try {
427
- const fs = await import('fs/promises');
428
- await fs.unlink(filePath);
429
- }
430
- catch {
431
- // Ignore cleanup errors
432
- }
433
- return;
434
- }
435
- try {
436
- const fs = await import('fs/promises');
437
- const stats = await fs.stat(filePath);
438
- if (this.config.memoryProtection) {
439
- // Overwrite file with random data multiple times (DoD 5220.22-M standard)
440
- const fileSize = stats.size;
441
- const buffer = crypto__namespace.randomBytes(Math.min(fileSize, 64 * 1024)); // 64KB chunks
442
- for (let pass = 0; pass < 3; pass++) {
443
- const handle = await fs.open(filePath, "r+");
444
- try {
445
- for (let offset = 0; offset < fileSize; offset += buffer.length) {
446
- const chunk = offset + buffer.length > fileSize ? buffer.subarray(0, fileSize - offset) : buffer;
447
- await handle.write(chunk, 0, chunk.length, offset);
448
- }
449
- await handle.sync();
450
- }
451
- finally {
452
- await handle.close();
453
- }
454
- }
455
- }
456
- // Final deletion
457
- await fs.unlink(filePath);
458
- this.auditLog("temp_file_deleted", {
459
- action: "secure_delete",
460
- success: true,
461
- metadata: {
462
- path: this.sanitizeFilename(filePath),
463
- overwritePasses: this.config.memoryProtection ? 3 : 0,
464
- },
465
- });
466
- }
467
- catch (error) {
468
- this.auditLog("temp_file_deleted", {
469
- action: "secure_delete",
470
- success: false,
471
- sanitizedError: this.sanitizeError(error),
472
- metadata: { path: this.sanitizeFilename(filePath) },
473
- });
474
- }
475
- }
476
- /**
477
- * Calculate secure file hash for audit purposes
478
- */
479
- calculateFileHash(data) {
480
- return crypto__namespace.createHash("sha256").update(data).digest("hex");
481
- }
482
- /**
483
- * Log audit event
484
- */
485
- auditLog(eventType, details) {
486
- if (!this.config.enabled)
487
- return;
488
- const event = {
489
- timestamp: new Date().toISOString(),
490
- eventType,
491
- sessionId: this.sessionId,
492
- details: {
493
- action: details.action || "unknown",
494
- success: details.success ?? true,
495
- ...details,
496
- },
497
- };
498
- this.auditEvents.push(event);
499
- // Write to audit log file if configured
500
- if (this.config.auditLogPath) {
501
- this.writeAuditLog(event).catch(() => {
502
- // Silent failure to prevent error loops
503
- });
504
- }
505
- }
506
- /**
507
- * Write audit event to file
508
- */
509
- async writeAuditLog(event) {
510
- if (!this.config.auditLogPath)
511
- return;
512
- try {
513
- const fs = await import('fs/promises');
514
- const logLine = JSON.stringify(event) + "\\n";
515
- await fs.appendFile(this.config.auditLogPath, logLine, { flag: "a" });
516
- }
517
- catch {
518
- // Silent failure
519
- }
520
- }
521
- /**
522
- * Generate cryptographically secure session ID
523
- */
524
- generateSessionId() {
525
- return crypto__namespace.randomBytes(16).toString("hex");
526
- }
527
- /**
528
- * Get current audit events for this session
529
- */
530
- getAuditEvents() {
531
- return [...this.auditEvents];
532
- }
533
- /**
534
- * Clear sensitive data from memory
535
- */
536
- clearSensitiveData() {
537
- if (!this.config.enabled || !this.config.memoryProtection)
538
- return;
539
- // Clear audit events
540
- this.auditEvents.length = 0;
541
- // Force garbage collection if available
542
- if (global.gc) {
543
- global.gc();
544
- }
545
- }
546
- /**
547
- * Validate transport security
548
- */
549
- validateTransportSecurity(url) {
550
- if (!this.config.enabled || !this.config.requireSecureTransport) {
551
- return true;
552
- }
553
- if (!url)
554
- return true;
555
- try {
556
- const urlObj = new URL(url);
557
- const isSecure = urlObj.protocol === "https:" ||
558
- urlObj.hostname === "localhost" ||
559
- urlObj.hostname === "127.0.0.1";
560
- if (!isSecure) {
561
- this.auditLog("security_violation", {
562
- action: "insecure_transport",
563
- success: false,
564
- metadata: { protocol: urlObj.protocol, hostname: urlObj.hostname },
565
- });
566
- }
567
- return isSecure;
568
- }
569
- catch {
570
- return false;
571
- }
572
- }
573
- }
574
- // Global HIPAA compliance instance
575
- let hipaaManager = null;
576
- /**
577
- * Initialize HIPAA compliance
578
- */
579
- function initializeHipaaCompliance(config) {
580
- hipaaManager = new HipaaComplianceManager(config);
581
- return hipaaManager;
582
- }
583
- /**
584
- * Get current HIPAA compliance manager
585
- */
586
- function getHipaaManager() {
587
- return hipaaManager;
588
- }
589
- /**
590
- * HIPAA-compliant error wrapper
591
- */
592
- function createHipaaError(error, context) {
593
- const manager = getHipaaManager();
594
- if (!manager) {
595
- return typeof error === "string" ? new Error(error) : error;
596
- }
597
- const sanitizedMessage = manager.sanitizeError(error);
598
- const hipaaError = new Error(sanitizedMessage);
599
- manager.auditLog("error_occurred", {
600
- action: context || "error",
601
- success: false,
602
- sanitizedError: sanitizedMessage,
603
- });
604
- return hipaaError;
605
- }
606
- /**
607
- * HIPAA-compliant temporary file utilities
608
- */
609
- const HipaaTemp = {
610
- createPath: (prefix) => {
611
- const manager = getHipaaManager();
612
- return manager
613
- ? manager.createSecureTempPath(prefix)
614
- : path__namespace.join(os__namespace.tmpdir(), `${prefix || "pompelmi"}-${Date.now()}`);
615
- },
616
- cleanup: async (filePath) => {
617
- const manager = getHipaaManager();
618
- if (manager) {
619
- await manager.secureFileCleanup(filePath);
620
- }
621
- else {
622
- try {
623
- const fs = await import('fs/promises');
624
- await fs.unlink(filePath);
625
- }
626
- catch {
627
- // Ignore errors
628
- }
629
- }
630
- },
631
- };
632
-
633
- const MB$1 = 1024 * 1024;
634
- const DEFAULT_POLICY = {
635
- includeExtensions: ["zip", "png", "jpg", "jpeg", "pdf"],
636
- allowedMimeTypes: ["application/zip", "image/png", "image/jpeg", "application/pdf", "text/plain"],
637
- maxFileSizeBytes: 20 * MB$1,
638
- timeoutMs: 5000,
639
- concurrency: 4,
640
- failClosed: true,
641
- };
642
- function definePolicy(input = {}) {
643
- const p = { ...DEFAULT_POLICY, ...input };
644
- if (!Array.isArray(p.includeExtensions))
645
- throw new TypeError("includeExtensions must be string[]");
646
- if (!Array.isArray(p.allowedMimeTypes))
647
- throw new TypeError("allowedMimeTypes must be string[]");
648
- if (!(Number.isFinite(p.maxFileSizeBytes) && p.maxFileSizeBytes > 0))
649
- throw new TypeError("maxFileSizeBytes must be > 0");
650
- if (!(Number.isFinite(p.timeoutMs) && p.timeoutMs > 0))
651
- throw new TypeError("timeoutMs must be > 0");
652
- if (!(Number.isInteger(p.concurrency) && p.concurrency > 0))
653
- throw new TypeError("concurrency must be > 0");
654
- return p;
655
- }
656
-
657
- /**
658
- * Policy packs for Pompelmi.
659
- *
660
- * Pre-configured, named policies for common upload scenarios. Each pack
661
- * defines the file type allowlist, size limits, and timeout appropriate for
662
- * its use case.
663
- *
664
- * All packs are built on `definePolicy` and are fully overridable:
665
- *
666
- * ```ts
667
- * import { POLICY_PACKS } from 'pompelmi/policy-packs';
668
- *
669
- * // Use a pack as-is:
670
- * const policy = POLICY_PACKS['images-only'];
671
- *
672
- * // Or override individual fields:
673
- * import { definePolicy } from 'pompelmi';
674
- * const custom = definePolicy({ ...POLICY_PACKS['documents-only'], maxFileSizeBytes: 5 * 1024 * 1024 });
675
- * ```
676
- *
677
- * These packs are *deterministic* and *descriptor-based* — they do not
678
- * depend on any external threat intelligence feed.
679
- *
680
- * @module policy-packs
681
- */
682
- const KB = 1024;
683
- const MB = 1024 * KB;
684
- // ── Policy packs ──────────────────────────────────────────────────────────────
685
- /**
686
- * Documents-only policy.
687
- *
688
- * Appropriate for: document management APIs, PDF/Office file upload endpoints,
689
- * data import pipelines.
690
- *
691
- * Allowed: PDF, Word (.docx/.doc), Excel (.xlsx/.xls), PowerPoint (.pptx/.ppt),
692
- * CSV, plain text, JSON, YAML, ODT/ODS/ODP (OpenDocument).
693
- * Max size: 25 MB.
694
- */
695
- const DOCUMENTS_ONLY = definePolicy({
696
- includeExtensions: [
697
- "pdf",
698
- "doc",
699
- "docx",
700
- "xls",
701
- "xlsx",
702
- "ppt",
703
- "pptx",
704
- "odt",
705
- "ods",
706
- "odp",
707
- "csv",
708
- "txt",
709
- "json",
710
- "yaml",
711
- "yml",
712
- "md",
713
- ],
714
- allowedMimeTypes: [
715
- "application/pdf",
716
- "application/msword",
717
- "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
718
- "application/vnd.ms-excel",
719
- "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
720
- "application/vnd.ms-powerpoint",
721
- "application/vnd.openxmlformats-officedocument.presentationml.presentation",
722
- "application/vnd.oasis.opendocument.text",
723
- "application/vnd.oasis.opendocument.spreadsheet",
724
- "application/vnd.oasis.opendocument.presentation",
725
- "text/csv",
726
- "text/plain",
727
- "application/json",
728
- "text/yaml",
729
- "text/markdown",
730
- ],
731
- maxFileSizeBytes: 25 * MB,
732
- timeoutMs: 10000,
733
- concurrency: 4,
734
- failClosed: true,
735
- });
736
- /**
737
- * Images-only policy.
738
- *
739
- * Appropriate for: avatar uploads, product image APIs, content platforms with
740
- * user-generated imagery.
741
- *
742
- * Allowed: JPEG, PNG, GIF, WebP, AVIF, TIFF, BMP, ICO.
743
- * Max size: 10 MB.
744
- * Note: SVG is intentionally excluded — inline SVGs can contain scripts.
745
- */
746
- const IMAGES_ONLY = definePolicy({
747
- includeExtensions: ["jpg", "jpeg", "png", "gif", "webp", "avif", "tiff", "tif", "bmp", "ico"],
748
- allowedMimeTypes: [
749
- "image/jpeg",
750
- "image/png",
751
- "image/gif",
752
- "image/webp",
753
- "image/avif",
754
- "image/tiff",
755
- "image/bmp",
756
- "image/x-icon",
757
- "image/vnd.microsoft.icon",
758
- ],
759
- maxFileSizeBytes: 10 * MB,
760
- timeoutMs: 5000,
761
- concurrency: 8,
762
- failClosed: true,
763
- });
764
- /**
765
- * Strict public-upload policy.
766
- *
767
- * Appropriate for: anonymous or low-trust upload endpoints, public APIs,
768
- * any surface exposed to untrusted users.
769
- *
770
- * Aggressive size limit (5 MB), short timeout, fail-closed, narrow MIME
771
- * allowlist. Only allows plain images and PDF.
772
- */
773
- const STRICT_PUBLIC_UPLOAD = definePolicy({
774
- includeExtensions: ["jpg", "jpeg", "png", "webp", "pdf"],
775
- allowedMimeTypes: ["image/jpeg", "image/png", "image/webp", "application/pdf"],
776
- maxFileSizeBytes: 5 * MB,
777
- timeoutMs: 4000,
778
- concurrency: 2,
779
- failClosed: true,
780
- });
781
- /**
782
- * Conservative default policy.
783
- *
784
- * A hardened version of the built-in `DEFAULT_POLICY` suitable for
785
- * production without further customisation. Stricter size limit and
786
- * shorter timeout than the permissive default.
787
- */
788
- const CONSERVATIVE_DEFAULT = definePolicy({
789
- includeExtensions: ["zip", "png", "jpg", "jpeg", "pdf", "txt", "csv", "docx", "xlsx"],
790
- allowedMimeTypes: [
791
- "application/zip",
792
- "image/png",
793
- "image/jpeg",
794
- "application/pdf",
795
- "text/plain",
796
- "text/csv",
797
- "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
798
- "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
799
- ],
800
- maxFileSizeBytes: 10 * MB,
801
- timeoutMs: 8000,
802
- concurrency: 4,
803
- failClosed: true,
804
- });
805
- /**
806
- * Archives policy.
807
- *
808
- * Appropriate for: endpoints that accept ZIP, tar, or compressed archives.
809
- * Combines a generous size allowance with a longer timeout for deep inspection.
810
- *
811
- * NOTE: Pair this policy with `createZipBombGuard()` to defend against
812
- * decompression-bomb attacks:
813
- *
814
- * ```ts
815
- * import { composeScanners, createZipBombGuard, CommonHeuristicsScanner } from 'pompelmi';
816
- * const scanner = composeScanners(
817
- * [['zipGuard', createZipBombGuard()], ['heuristics', CommonHeuristicsScanner]]
818
- * );
819
- * ```
820
- */
821
- const ARCHIVES = definePolicy({
822
- includeExtensions: ["zip", "tar", "gz", "tgz", "bz2", "xz", "7z", "rar"],
823
- allowedMimeTypes: [
824
- "application/zip",
825
- "application/x-tar",
826
- "application/gzip",
827
- "application/x-bzip2",
828
- "application/x-xz",
829
- "application/x-7z-compressed",
830
- "application/x-rar-compressed",
831
- ],
832
- maxFileSizeBytes: 100 * MB,
833
- timeoutMs: 30000,
834
- concurrency: 2,
835
- failClosed: true,
836
- });
837
- /**
838
- * Named map of all built-in policy packs.
839
- *
840
- * ```ts
841
- * import { POLICY_PACKS } from 'pompelmi/policy-packs';
842
- * const policy = POLICY_PACKS['strict-public-upload'];
843
- * ```
844
- */
845
- const POLICY_PACKS = {
846
- "documents-only": DOCUMENTS_ONLY,
847
- "images-only": IMAGES_ONLY,
848
- "strict-public-upload": STRICT_PUBLIC_UPLOAD,
849
- "conservative-default": CONSERVATIVE_DEFAULT,
850
- archives: ARCHIVES,
851
- };
852
- /**
853
- * Look up a policy pack by name.
854
- * Throws if the name is not recognised.
855
- */
856
- function getPolicyPack(name) {
857
- const policy = POLICY_PACKS[name];
858
- if (!policy)
859
- throw new Error(`Unknown policy pack: '${name}'. Valid names: ${Object.keys(POLICY_PACKS).join(", ")}`);
860
- return policy;
861
- }
862
-
863
- function hasAsciiToken(buf, token) {
864
- // Use latin1 so we can safely search binary
865
- return buf.indexOf(token, 0, "latin1") !== -1;
866
- }
867
- function startsWith(buf, bytes) {
868
- if (buf.length < bytes.length)
869
- return false;
870
- for (let i = 0; i < bytes.length; i++)
871
- if (buf[i] !== bytes[i])
872
- return false;
873
- return true;
874
- }
875
- function isPDF(buf) {
876
- // %PDF-
877
- return startsWith(buf, [0x25, 0x50, 0x44, 0x46, 0x2d]);
878
- }
879
- function isOleCfb(buf) {
880
- // D0 CF 11 E0 A1 B1 1A E1
881
- const sig = [0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1];
882
- return startsWith(buf, sig);
883
- }
884
- function isZipLike$1(buf) {
885
- // PK\x03\x04
886
- return startsWith(buf, [0x50, 0x4b, 0x03, 0x04]);
887
- }
888
- function isPeExecutable(buf) {
889
- // "MZ"
890
- return startsWith(buf, [0x4d, 0x5a]);
891
- }
892
- /** OOXML macro hint via filename token in ZIP container */
893
- function hasOoxmlMacros(buf) {
894
- if (!isZipLike$1(buf))
895
- return false;
896
- return hasAsciiToken(buf, "vbaProject.bin");
897
- }
898
- /** PDF risky features (/JavaScript, /OpenAction, /AA, /Launch) */
899
- function pdfRiskTokens(buf) {
900
- const tokens = ["/JavaScript", "/OpenAction", "/AA", "/Launch"];
901
- return tokens.filter((t) => hasAsciiToken(buf, t));
902
- }
903
- const CommonHeuristicsScanner = {
904
- async scan(input) {
905
- const buf = Buffer.from(input);
906
- const matches = [];
907
- // Office macros (OLE / OOXML)
908
- if (isOleCfb(buf)) {
909
- matches.push({ rule: "office_ole_container", severity: "suspicious" });
910
- }
911
- if (hasOoxmlMacros(buf)) {
912
- matches.push({ rule: "office_ooxml_macros", severity: "suspicious" });
913
- }
914
- // PDF risky tokens
915
- if (isPDF(buf)) {
916
- const toks = pdfRiskTokens(buf);
917
- if (toks.length) {
918
- matches.push({
919
- rule: "pdf_risky_actions",
920
- severity: "suspicious",
921
- meta: { tokens: toks },
922
- });
923
- }
924
- }
925
- // Executable header
926
- if (isPeExecutable(buf)) {
927
- matches.push({ rule: "pe_executable_signature", severity: "suspicious" });
928
- }
929
- // EICAR test file
930
- const EICAR_NEEDLE = "X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!";
931
- if (hasAsciiToken(buf, EICAR_NEEDLE)) {
932
- matches.push({
933
- rule: "eicar_test_file",
934
- severity: "high",
935
- meta: { note: "EICAR standard antivirus test file detected" },
936
- });
937
- }
938
- return matches;
939
- },
940
- };
941
-
942
- function toScanFn(s) {
943
- return (typeof s === "function" ? s : s.scan);
944
- }
945
- /** Map a Match's severity field to a Verdict for stopOn comparison. */
946
- function matchToVerdict(m) {
947
- const s = m.severity;
948
- if (s === "critical" || s === "high" || s === "malicious")
949
- return "malicious";
950
- if (s === "medium" || s === "low" || s === "suspicious" || s === "info")
951
- return "suspicious";
952
- return "clean";
953
- }
954
- /** Highest verdict across all matches in the list. */
955
- function highestSeverity(matches) {
956
- if (matches.length === 0)
957
- return null;
958
- if (matches.some((m) => matchToVerdict(m) === "malicious"))
959
- return "malicious";
960
- if (matches.some((m) => matchToVerdict(m) === "suspicious"))
961
- return "suspicious";
962
- return "clean";
963
- }
964
- const SEVERITY_RANK = { malicious: 2, suspicious: 1, clean: 0 };
965
- function shouldStop(matches, stopOn) {
966
- if (!stopOn)
967
- return false;
968
- const highest = highestSeverity(matches);
969
- if (!highest)
970
- return false;
971
- return SEVERITY_RANK[highest] >= SEVERITY_RANK[stopOn];
972
- }
973
- async function runWithTimeout(fn, timeoutMs) {
974
- if (!timeoutMs)
975
- return fn();
976
- return new Promise((resolve, reject) => {
977
- const timer = setTimeout(() => reject(new Error("scanner timeout")), timeoutMs);
978
- fn().then((v) => {
979
- clearTimeout(timer);
980
- resolve(v);
981
- }, (e) => {
982
- clearTimeout(timer);
983
- reject(e);
984
- });
985
- });
986
- }
987
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
988
- function composeScanners(...args) {
989
- const first = args[0];
990
- const rest = args.slice(1);
991
- // ── Named-scanner array form ──────────────────────────────────────────────
992
- if (Array.isArray(first) &&
993
- (first.length === 0 || (Array.isArray(first[0]) && typeof first[0][0] === "string"))) {
994
- const entries = first;
995
- const opts = rest.length > 0 &&
996
- !Array.isArray(rest[0]) &&
997
- typeof rest[0] !== "function" &&
998
- !(typeof rest[0] === "object" && rest[0] !== null && "scan" in rest[0])
999
- ? rest[0]
1000
- : {};
1001
- return async (input, ctx) => {
1002
- const all = [];
1003
- if (opts.parallel) {
1004
- // Parallel execution — collect all results then return
1005
- const results = await Promise.allSettled(entries.map(([_name, scanner]) => runWithTimeout(() => toScanFn(scanner)(input, ctx), opts.timeoutMsPerScanner)));
1006
- for (let i = 0; i < results.length; i++) {
1007
- const result = results[i];
1008
- if (result.status === "fulfilled" && Array.isArray(result.value)) {
1009
- const matches = opts.tagSourceName
1010
- ? result.value.map((m) => ({
1011
- ...m,
1012
- meta: { ...m.meta, _sourceName: entries[i][0] },
1013
- }))
1014
- : result.value;
1015
- all.push(...matches);
1016
- }
1017
- }
1018
- }
1019
- else {
1020
- // Sequential execution with optional stopOn short-circuit
1021
- for (const [name, scanner] of entries) {
1022
- try {
1023
- const out = await runWithTimeout(() => toScanFn(scanner)(input, ctx), opts.timeoutMsPerScanner);
1024
- if (Array.isArray(out)) {
1025
- const matches = opts.tagSourceName
1026
- ? out.map((m) => ({ ...m, meta: { ...m.meta, _sourceName: name } }))
1027
- : out;
1028
- all.push(...matches);
1029
- if (shouldStop(all, opts.stopOn))
1030
- break;
1031
- }
1032
- }
1033
- catch {
1034
- // individual scanner failure is non-fatal
1035
- }
1036
- }
1037
- }
1038
- return all;
1039
- };
1040
- }
1041
- // ── Variadic form (backward-compatible) ───────────────────────────────────
1042
- const scanners = [first, ...rest].filter(Boolean);
1043
- return async (input, ctx) => {
1044
- const all = [];
1045
- for (const s of scanners) {
1046
- try {
1047
- const out = await toScanFn(s)(input, ctx);
1048
- if (Array.isArray(out))
1049
- all.push(...out);
1050
- }
1051
- catch {
1052
- // ignore individual scanner failures
1053
- }
1054
- }
1055
- return all;
1056
- };
1057
- }
1058
- function createPresetScanner(preset, opts = {}) {
1059
- const baseScanners = [CommonHeuristicsScanner];
1060
- const dynamicScannerPromises = [];
1061
- // Add decompilation scanners based on preset
1062
- if (preset === "decompilation-basic" ||
1063
- preset === "decompilation-deep" ||
1064
- preset === "malware-analysis" ||
1065
- opts.enableDecompilation) {
1066
- const depth = preset === "decompilation-deep" || preset === "malware-analysis"
1067
- ? "deep"
1068
- : preset === "decompilation-basic"
1069
- ? "basic"
1070
- : opts.decompilationDepth || "basic";
1071
- let importModule;
1072
- try {
1073
- // Dynamic import to avoid bundling issues - using Function to bypass TypeScript type checking
1074
- importModule = new Function("specifier", "return import(specifier)");
1075
- }
1076
- catch {
1077
- importModule = undefined;
1078
- }
1079
- if (importModule &&
1080
- (!opts.decompilationEngine ||
1081
- opts.decompilationEngine === "binaryninja-hlil" ||
1082
- opts.decompilationEngine === "both")) {
1083
- dynamicScannerPromises.push(importModule("@pompelmi/engine-binaryninja")
1084
- .then((mod) => mod.createBinaryNinjaScanner({
1085
- timeout: opts.decompilationTimeout || opts.timeout || 30000,
1086
- depth,
1087
- pythonPath: opts.pythonPath,
1088
- binaryNinjaPath: opts.binaryNinjaPath,
1089
- }))
1090
- .catch(() => null));
1091
- }
1092
- if (importModule &&
1093
- (!opts.decompilationEngine ||
1094
- opts.decompilationEngine === "ghidra-pcode" ||
1095
- opts.decompilationEngine === "both")) {
1096
- dynamicScannerPromises.push(importModule("@pompelmi/engine-ghidra")
1097
- .then((mod) => mod.createGhidraScanner({
1098
- timeout: opts.decompilationTimeout || opts.timeout || 30000,
1099
- depth,
1100
- ghidraPath: opts.ghidraPath,
1101
- analyzeHeadless: opts.analyzeHeadless,
1102
- }))
1103
- .catch(() => null));
1104
- }
1105
- }
1106
- let composedScannerPromise;
1107
- const getComposedScanner = async () => {
1108
- composedScannerPromise ?? (composedScannerPromise = Promise.all(dynamicScannerPromises).then((dynamicScanners) => composeScanners(...baseScanners, ...dynamicScanners.filter((scanner) => scanner !== null))));
1109
- return composedScannerPromise;
1110
- };
1111
- return async (input, ctx) => {
1112
- const scanner = await getComposedScanner();
1113
- return scanner(input, ctx);
1114
- };
1115
- }
1116
-
1117
- /**
1118
- * Advanced threat detection utilities
1119
- * @module utils/advanced-detection
1120
- */
1121
- /**
1122
- * Enhanced polyglot file detection
1123
- * Detects files that can be interpreted as multiple formats
1124
- */
1125
- function detectPolyglot(bytes) {
1126
- const matches = [];
1127
- // Check for PDF/ZIP polyglot
1128
- if (isPDFZipPolyglot(bytes)) {
1129
- matches.push({
1130
- rule: "polyglot_pdf_zip",
1131
- severity: "high",
1132
- meta: { description: "File can be interpreted as both PDF and ZIP" },
1133
- });
1134
- }
1135
- // Check for image/script polyglot
1136
- if (isImageScriptPolyglot(bytes)) {
1137
- matches.push({
1138
- rule: "polyglot_image_script",
1139
- severity: "high",
1140
- meta: { description: "Image file contains executable script content" },
1141
- });
1142
- }
1143
- // Check for GIFAR (GIF/JAR polyglot)
1144
- if (isGIFAR(bytes)) {
1145
- matches.push({
1146
- rule: "polyglot_gifar",
1147
- severity: "critical",
1148
- meta: { description: "GIF file contains Java archive" },
1149
- });
1150
- }
1151
- return matches;
1152
- }
1153
- /**
1154
- * Detect obfuscated JavaScript/VBScript
1155
- */
1156
- function detectObfuscatedScripts(bytes) {
1157
- const matches = [];
1158
- const text = new TextDecoder("utf-8", { fatal: false }).decode(bytes.slice(0, Math.min(64 * 1024, bytes.length)));
1159
- // Check for common obfuscation patterns
1160
- const obfuscationPatterns = [
1161
- /eval\s*\(\s*unescape\s*\(/gi,
1162
- /eval\s*\(\s*atob\s*\(/gi,
1163
- /String\.fromCharCode\s*\(\s*\d+(?:\s*,\s*\d+){10,}/gi,
1164
- /[a-z0-9]{100,}/gi, // Long encoded strings
1165
- /\\x[0-9a-f]{2}/gi, // Hex escapes
1166
- ];
1167
- for (const pattern of obfuscationPatterns) {
1168
- if (pattern.test(text)) {
1169
- matches.push({
1170
- rule: "obfuscated_script",
1171
- severity: "medium",
1172
- meta: {
1173
- description: "Detected obfuscated script content",
1174
- pattern: pattern.source,
1175
- },
1176
- });
1177
- break;
1178
- }
1179
- }
1180
- return matches;
1181
- }
1182
- /**
1183
- * Enhanced nested archive detection with depth limits
1184
- */
1185
- function analyzeNestedArchives(bytes, maxDepth = 10) {
1186
- let depth = 0;
1187
- let currentBytes = bytes;
1188
- while (depth < maxDepth) {
1189
- if (isArchive(currentBytes)) {
1190
- depth++;
1191
- {
1192
- break;
1193
- }
1194
- }
1195
- else {
1196
- break;
1197
- }
1198
- }
1199
- return {
1200
- depth,
1201
- hasExcessiveNesting: depth >= 5,
1202
- };
1203
- }
1204
- // Helper functions
1205
- function isPDFZipPolyglot(bytes) {
1206
- if (bytes.length < 8)
1207
- return false;
1208
- // Check for PDF signature
1209
- const hasPDF = bytes[0] === 0x25 && bytes[1] === 0x50 && bytes[2] === 0x44 && bytes[3] === 0x46;
1210
- // Check for ZIP signature anywhere in the file
1211
- let hasZIP = false;
1212
- for (let i = 0; i < Math.min(bytes.length - 4, 1024); i++) {
1213
- if (bytes[i] === 0x50 &&
1214
- bytes[i + 1] === 0x4b &&
1215
- bytes[i + 2] === 0x03 &&
1216
- bytes[i + 3] === 0x04) {
1217
- hasZIP = true;
1218
- break;
1219
- }
1220
- }
1221
- return hasPDF && hasZIP;
1222
- }
1223
- function isImageScriptPolyglot(bytes) {
1224
- if (bytes.length < 100)
1225
- return false;
1226
- // Check for image signatures
1227
- const isImage = (bytes[0] === 0xff && bytes[1] === 0xd8) || // JPEG
1228
- (bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4e && bytes[3] === 0x47) || // PNG
1229
- (bytes[0] === 0x47 && bytes[1] === 0x49 && bytes[2] === 0x46); // GIF
1230
- if (!isImage)
1231
- return false;
1232
- // Check for script content
1233
- const text = new TextDecoder("utf-8", { fatal: false }).decode(bytes);
1234
- return /<script|javascript:|eval\(|function\s*\(/i.test(text);
1235
- }
1236
- function isGIFAR(bytes) {
1237
- if (bytes.length < 100)
1238
- return false;
1239
- // Check for GIF signature
1240
- const isGIF = bytes[0] === 0x47 && bytes[1] === 0x49 && bytes[2] === 0x46;
1241
- // Check for ZIP/JAR signature
1242
- let hasZIP = false;
1243
- for (let i = 0; i < Math.min(bytes.length - 4, 1024); i++) {
1244
- if (bytes[i] === 0x50 &&
1245
- bytes[i + 1] === 0x4b &&
1246
- bytes[i + 2] === 0x03 &&
1247
- bytes[i + 3] === 0x04) {
1248
- hasZIP = true;
1249
- break;
1250
- }
1251
- }
1252
- return isGIF && hasZIP;
1253
- }
1254
- function isArchive(bytes) {
1255
- if (bytes.length < 4)
1256
- return false;
1257
- return (
1258
- // ZIP
1259
- (bytes[0] === 0x50 && bytes[1] === 0x4b && bytes[2] === 0x03 && bytes[3] === 0x04) ||
1260
- // RAR
1261
- (bytes[0] === 0x52 && bytes[1] === 0x61 && bytes[2] === 0x72 && bytes[3] === 0x21) ||
1262
- // 7z
1263
- (bytes[0] === 0x37 && bytes[1] === 0x7a && bytes[2] === 0xbc && bytes[3] === 0xaf) ||
1264
- // tar.gz
1265
- (bytes[0] === 0x1f && bytes[1] === 0x8b));
1266
- }
1267
-
1268
- /**
1269
- * Cache management system for scan results
1270
- * @module utils/cache-manager
1271
- */
1272
- /**
1273
- * LRU cache for scan results with TTL support
1274
- */
1275
- class ScanCacheManager {
1276
- constructor(options = {}) {
1277
- this.cache = new Map();
1278
- // Statistics
1279
- this.stats = {
1280
- hits: 0,
1281
- misses: 0,
1282
- evictions: 0,
1283
- };
1284
- this.maxSize = options.maxSize ?? 1000;
1285
- this.ttl = options.ttl ?? 3600000; // 1 hour default
1286
- this.enableLRU = options.enableLRU ?? true;
1287
- this.enableStats = options.enableStats ?? false;
1288
- }
1289
- /**
1290
- * Generate cache key from file content
1291
- */
1292
- generateKey(content, preset) {
1293
- const hash = crypto.createHash("sha256")
1294
- .update(content)
1295
- .update(preset || "default")
1296
- .digest("hex");
1297
- return hash;
1298
- }
1299
- /**
1300
- * Check if cache entry is still valid
1301
- */
1302
- isValid(entry) {
1303
- return Date.now() - entry.timestamp < this.ttl;
1304
- }
1305
- /**
1306
- * Evict oldest or least-used entry when cache is full
1307
- */
1308
- evict() {
1309
- if (this.cache.size === 0)
1310
- return;
1311
- let targetKey = null;
1312
- let oldestTime = Infinity;
1313
- let lowestAccess = Infinity;
1314
- for (const [key, entry] of this.cache.entries()) {
1315
- if (this.enableLRU) {
1316
- // LRU: evict least recently used
1317
- if (entry.timestamp < oldestTime) {
1318
- oldestTime = entry.timestamp;
1319
- targetKey = key;
1320
- }
1321
- }
1322
- else {
1323
- // LFU: evict least frequently used
1324
- if (entry.accessCount < lowestAccess) {
1325
- lowestAccess = entry.accessCount;
1326
- targetKey = key;
1327
- }
1328
- }
1329
- }
1330
- if (targetKey) {
1331
- this.cache.delete(targetKey);
1332
- if (this.enableStats)
1333
- this.stats.evictions++;
1334
- }
1335
- }
1336
- /**
1337
- * Store scan result in cache
1338
- */
1339
- set(content, report, preset) {
1340
- const key = this.generateKey(content, preset);
1341
- // Evict if necessary
1342
- if (this.cache.size >= this.maxSize) {
1343
- this.evict();
1344
- }
1345
- this.cache.set(key, {
1346
- report,
1347
- timestamp: Date.now(),
1348
- accessCount: 0,
1349
- });
1350
- }
1351
- /**
1352
- * Retrieve scan result from cache
1353
- */
1354
- get(content, preset) {
1355
- const key = this.generateKey(content, preset);
1356
- const entry = this.cache.get(key);
1357
- if (!entry) {
1358
- if (this.enableStats)
1359
- this.stats.misses++;
1360
- return null;
1361
- }
1362
- if (!this.isValid(entry)) {
1363
- this.cache.delete(key);
1364
- if (this.enableStats)
1365
- this.stats.misses++;
1366
- return null;
1367
- }
1368
- // Update access tracking
1369
- entry.accessCount++;
1370
- entry.timestamp = Date.now(); // Update for LRU
1371
- if (this.enableStats)
1372
- this.stats.hits++;
1373
- return entry.report;
1374
- }
1375
- /**
1376
- * Check if result exists in cache
1377
- */
1378
- has(content, preset) {
1379
- const key = this.generateKey(content, preset);
1380
- const entry = this.cache.get(key);
1381
- return entry !== undefined && this.isValid(entry);
1382
- }
1383
- /**
1384
- * Clear entire cache
1385
- */
1386
- clear() {
1387
- this.cache.clear();
1388
- if (this.enableStats) {
1389
- this.stats.hits = 0;
1390
- this.stats.misses = 0;
1391
- this.stats.evictions = 0;
1392
- }
1393
- }
1394
- /**
1395
- * Remove expired entries
1396
- */
1397
- prune() {
1398
- let removed = 0;
1399
- for (const [key, entry] of this.cache.entries()) {
1400
- if (!this.isValid(entry)) {
1401
- this.cache.delete(key);
1402
- removed++;
1403
- }
1404
- }
1405
- return removed;
1406
- }
1407
- /**
1408
- * Get cache statistics
1409
- */
1410
- getStats() {
1411
- const total = this.stats.hits + this.stats.misses;
1412
- const hitRate = total > 0 ? (this.stats.hits / total) * 100 : 0;
1413
- return {
1414
- hits: this.stats.hits,
1415
- misses: this.stats.misses,
1416
- size: this.cache.size,
1417
- hitRate,
1418
- evictions: this.stats.evictions,
1419
- };
1420
- }
1421
- /**
1422
- * Get current cache size
1423
- */
1424
- get size() {
1425
- return this.cache.size;
1426
- }
1427
- }
1428
- // Export singleton instance for convenience
1429
- let defaultCache = null;
1430
- /**
1431
- * Get or create the default cache instance
1432
- */
1433
- function getDefaultCache(options) {
1434
- if (!defaultCache) {
1435
- defaultCache = new ScanCacheManager(options);
1436
- }
1437
- return defaultCache;
1438
- }
1439
- /**
1440
- * Reset the default cache instance
1441
- */
1442
- function resetDefaultCache() {
1443
- defaultCache = null;
1444
- }
1445
-
1446
- /**
1447
- * Performance monitoring utilities for pompelmi scans
1448
- * @module utils/performance-metrics
1449
- */
1450
- /**
1451
- * Track performance metrics for a scan operation
1452
- */
1453
- class PerformanceTracker {
1454
- constructor() {
1455
- this.checkpoints = new Map();
1456
- this.startTime = Date.now();
1457
- }
1458
- /**
1459
- * Mark a checkpoint in the scan process
1460
- */
1461
- checkpoint(name) {
1462
- this.checkpoints.set(name, Date.now());
1463
- }
1464
- /**
1465
- * Get duration since start or since a specific checkpoint
1466
- */
1467
- getDuration(since) {
1468
- const now = Date.now();
1469
- if (since && this.checkpoints.has(since)) {
1470
- return now - (this.checkpoints.get(since) ?? now);
1471
- }
1472
- return now - this.startTime;
1473
- }
1474
- /**
1475
- * Generate final metrics report
1476
- */
1477
- getMetrics(bytesScanned) {
1478
- const totalDuration = this.getDuration();
1479
- const throughput = totalDuration > 0 ? (bytesScanned / totalDuration) * 1000 : 0;
1480
- return {
1481
- totalDurationMs: totalDuration,
1482
- heuristicsDurationMs: this.checkpoints.has("heuristics_end")
1483
- ? (this.checkpoints.get("heuristics_end") ?? 0) -
1484
- (this.checkpoints.get("heuristics_start") ?? 0)
1485
- : undefined,
1486
- yaraDurationMs: this.checkpoints.has("yara_end")
1487
- ? (this.checkpoints.get("yara_end") ?? 0) - (this.checkpoints.get("yara_start") ?? 0)
1488
- : undefined,
1489
- prepDurationMs: this.checkpoints.has("prep_end")
1490
- ? (this.checkpoints.get("prep_end") ?? 0) - this.startTime
1491
- : undefined,
1492
- throughputBps: throughput,
1493
- bytesScanned,
1494
- startedAt: this.startTime,
1495
- completedAt: Date.now(),
1496
- };
1497
- }
1498
- }
1499
- /**
1500
- * Aggregate statistics from multiple scan reports
1501
- */
1502
- function aggregateScanStats(reports) {
1503
- let cleanCount = 0;
1504
- let suspiciousCount = 0;
1505
- let maliciousCount = 0;
1506
- let totalDuration = 0;
1507
- let totalBytes = 0;
1508
- let validDurationCount = 0;
1509
- for (const report of reports) {
1510
- if (report.verdict === "clean")
1511
- cleanCount++;
1512
- else if (report.verdict === "suspicious")
1513
- suspiciousCount++;
1514
- else if (report.verdict === "malicious")
1515
- maliciousCount++;
1516
- if (report.durationMs !== undefined) {
1517
- totalDuration += report.durationMs;
1518
- validDurationCount++;
1519
- }
1520
- if (report.file?.size !== undefined) {
1521
- totalBytes += report.file.size;
1522
- }
1523
- }
1524
- const avgDuration = validDurationCount > 0 ? totalDuration / validDurationCount : 0;
1525
- const avgThroughput = totalDuration > 0 ? (totalBytes / totalDuration) * 1000 : 0;
1526
- return {
1527
- totalScans: reports.length,
1528
- cleanCount,
1529
- suspiciousCount,
1530
- maliciousCount,
1531
- avgDurationMs: avgDuration,
1532
- avgThroughputBps: avgThroughput,
1533
- totalBytesScanned: totalBytes,
1534
- };
1535
- }
1536
-
1537
- /** Mappa veloce estensione -> mime (basic) */
1538
- function guessMimeByExt(name) {
1539
- if (!name)
1540
- return;
1541
- const ext = name.toLowerCase().split(".").pop();
1542
- switch (ext) {
1543
- case "zip":
1544
- return "application/zip";
1545
- case "png":
1546
- return "image/png";
1547
- case "jpg":
1548
- case "jpeg":
1549
- return "image/jpeg";
1550
- case "pdf":
1551
- return "application/pdf";
1552
- case "txt":
1553
- return "text/plain";
1554
- default:
1555
- return;
1556
- }
1557
- }
1558
- /** Heuristica semplice per verdetto */
1559
- function computeVerdict(matches) {
1560
- if (!matches.length)
1561
- return "clean";
1562
- // se la regola contiene 'zip_' lo marchiamo "suspicious"
1563
- const anyHigh = matches.some((m) => (m.tags ?? []).includes("critical") || (m.tags ?? []).includes("high"));
1564
- return anyHigh ? "malicious" : "suspicious";
1565
- }
1566
- /** Converte i Match (heuristics) in YaraMatch-like per uniformare l'output */
1567
- function toYaraMatches(ms) {
1568
- return ms.map((m) => ({
1569
- rule: m.rule,
1570
- namespace: "heuristics",
1571
- tags: ["heuristics"].concat(m.severity ? [m.severity] : []),
1572
- meta: m.meta,
1573
- }));
1574
- }
1575
- /** Scan di bytes (browser/node) usando preset (default: zip-basic) */
1576
- async function scanBytes(input, opts = {}) {
1577
- // Check cache first if enabled
1578
- if (opts.enableCache || opts.config?.performance?.enableCache) {
1579
- const cache = getDefaultCache(opts.config?.performance?.cacheOptions);
1580
- const cached = cache.get(input, opts.preset);
1581
- if (cached) {
1582
- return cached;
1583
- }
1584
- }
1585
- const perfTracker = opts.enablePerformanceTracking || opts.config?.performance?.enablePerformanceTracking
1586
- ? new PerformanceTracker()
1587
- : null;
1588
- perfTracker?.checkpoint("prep_start");
1589
- const preset = opts.preset ?? opts.config?.defaultPreset ?? "zip-basic";
1590
- const ctx = {
1591
- ...opts.ctx,
1592
- mimeType: opts.ctx?.mimeType ?? guessMimeByExt(opts.ctx?.filename),
1593
- size: opts.ctx?.size ?? input.byteLength,
1594
- };
1595
- perfTracker?.checkpoint("prep_end");
1596
- perfTracker?.checkpoint("heuristics_start");
1597
- const scanFn = createPresetScanner(preset);
1598
- const matchesH = await (typeof scanFn === "function"
1599
- ? scanFn
1600
- : scanFn.scan)(input, ctx);
1601
- const allMatches = [...matchesH];
1602
- perfTracker?.checkpoint("heuristics_end");
1603
- // Advanced detection (enabled by default, can be overridden by config)
1604
- const advancedEnabled = opts.enableAdvancedDetection ?? opts.config?.advanced?.enablePolyglotDetection ?? true;
1605
- if (advancedEnabled) {
1606
- perfTracker?.checkpoint("advanced_start");
1607
- // Detect polyglot files
1608
- if (opts.config?.advanced?.enablePolyglotDetection !== false) {
1609
- const polyglotMatches = detectPolyglot(input);
1610
- allMatches.push(...polyglotMatches);
1611
- }
1612
- // Detect obfuscated scripts
1613
- if (opts.config?.advanced?.enableObfuscationDetection !== false) {
1614
- const obfuscatedMatches = detectObfuscatedScripts(input);
1615
- allMatches.push(...obfuscatedMatches);
1616
- }
1617
- // Check for excessive nesting in archives
1618
- if (opts.config?.advanced?.enableNestedArchiveAnalysis !== false) {
1619
- const nestingAnalysis = analyzeNestedArchives(input);
1620
- const maxDepth = opts.config?.advanced?.maxArchiveDepth ?? 5;
1621
- if (nestingAnalysis.hasExcessiveNesting || nestingAnalysis.depth > maxDepth) {
1622
- allMatches.push({
1623
- rule: "excessive_archive_nesting",
1624
- severity: "high",
1625
- meta: {
1626
- description: "Excessive archive nesting detected",
1627
- depth: nestingAnalysis.depth,
1628
- maxAllowed: maxDepth,
1629
- },
1630
- });
1631
- }
1632
- }
1633
- perfTracker?.checkpoint("advanced_end");
1634
- }
1635
- const matches = toYaraMatches(allMatches);
1636
- const verdict = computeVerdict(matches);
1637
- perfTracker ? perfTracker.getDuration() : Date.now();
1638
- const durationMs = perfTracker ? perfTracker.getDuration() : 0;
1639
- const report = {
1640
- ok: verdict === "clean",
1641
- verdict,
1642
- matches,
1643
- reasons: matches.map((m) => m.rule),
1644
- file: { name: ctx.filename, mimeType: ctx.mimeType, size: ctx.size },
1645
- durationMs,
1646
- engine: "heuristics",
1647
- truncated: false,
1648
- timedOut: false,
1649
- };
1650
- // Add performance metrics if tracking enabled
1651
- if (perfTracker &&
1652
- (opts.enablePerformanceTracking || opts.config?.performance?.enablePerformanceTracking)) {
1653
- report.performanceMetrics = perfTracker.getMetrics(input.byteLength);
1654
- }
1655
- // Cache result if enabled
1656
- if (opts.enableCache || opts.config?.performance?.enableCache) {
1657
- const cache = getDefaultCache(opts.config?.performance?.cacheOptions);
1658
- cache.set(input, report, opts.preset);
1659
- }
1660
- // Invoke callbacks if configured
1661
- opts.config?.callbacks?.onScanComplete?.(report);
1662
- return report;
1663
- }
1664
- /** Scan di un file su disco (Node). Import dinamico per non vincolare il bundle browser. */
1665
- async function scanFile(filePath, opts = {}) {
1666
- const [{ readFile, stat }, path] = await Promise.all([import('fs/promises'), import('path')]);
1667
- const [buf, st] = await Promise.all([readFile(filePath), stat(filePath)]);
1668
- const ctx = {
1669
- filename: path.basename(filePath),
1670
- mimeType: guessMimeByExt(filePath),
1671
- size: st.size,
1672
- };
1673
- return scanBytes(new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength), { ...opts, ctx });
1674
- }
1675
- /** Scan multipli File (browser) usando scanBytes + preset di default */
1676
- async function scanFiles(files, opts = {}) {
1677
- const list = Array.from(files);
1678
- const out = [];
1679
- for (const f of list) {
1680
- const buf = new Uint8Array(await f.arrayBuffer());
1681
- const rep = await scanBytes(buf, {
1682
- ...opts,
1683
- ctx: { filename: f.name, mimeType: f.type || guessMimeByExt(f.name), size: f.size },
1684
- });
1685
- out.push(rep);
1686
- }
1687
- return out;
1688
- }
1689
-
1690
- async function createRemoteEngine(opts) {
1691
- const { endpoint, headers = {}, rulesField = "rules", fileField = "file", mode = "multipart", rulesAsBase64 = false, } = opts;
1692
- const engine = {
1693
- async compile(rulesSource) {
1694
- return {
1695
- async scan(data) {
1696
- const fetchFn = globalThis.fetch;
1697
- if (!fetchFn)
1698
- throw new Error("[remote-yara] fetch non disponibile in questo ambiente");
1699
- let res;
1700
- if (mode === "multipart") {
1701
- const FormDataCtor = globalThis.FormData;
1702
- const BlobCtor = globalThis.Blob;
1703
- if (!FormDataCtor || !BlobCtor) {
1704
- throw new Error("[remote-yara] FormData/Blob non disponibili (usa json-base64 oppure esegui in browser)");
1705
- }
1706
- const form = new FormDataCtor();
1707
- form.set(rulesField, new BlobCtor([rulesSource], { type: "text/plain" }), "rules.yar");
1708
- form.set(fileField, new BlobCtor([data], { type: "application/octet-stream" }), "sample.bin");
1709
- res = await fetchFn(endpoint, { method: "POST", body: form, headers });
1710
- }
1711
- else {
1712
- const b64 = base64FromBytes(data);
1713
- const payload = { [fileField]: b64 };
1714
- if (rulesAsBase64) {
1715
- payload["rulesB64"] = base64FromString(rulesSource);
1716
- }
1717
- else {
1718
- payload[rulesField] = rulesSource;
1719
- }
1720
- res = await fetchFn(endpoint, {
1721
- method: "POST",
1722
- headers: { "Content-Type": "application/json", ...headers },
1723
- body: JSON.stringify(payload),
1724
- });
1725
- }
1726
- if (!res.ok) {
1727
- throw new Error(`[remote-yara] HTTP ${res.status} ${res.statusText}`);
1728
- }
1729
- const json = await res.json().catch(() => null);
1730
- const arr = Array.isArray(json) ? json : (json?.matches ?? []);
1731
- return (arr ?? []).map((m) => ({
1732
- rule: m.rule ?? m.ruleIdentifier ?? "unknown",
1733
- tags: m.tags ?? [],
1734
- }));
1735
- },
1736
- };
1737
- },
1738
- };
1739
- return engine;
1740
- }
1741
- // Helpers
1742
- function base64FromBytes(bytes) {
1743
- // usa btoa se disponibile (browser); altrimenti fallback manuale
1744
- const btoaFn = globalThis.btoa;
1745
- let bin = "";
1746
- for (let i = 0; i < bytes.byteLength; i++)
1747
- bin += String.fromCharCode(bytes[i]);
1748
- return btoaFn ? btoaFn(bin) : Buffer.from(bin, "binary").toString("base64");
1749
- }
1750
- function base64FromString(s) {
1751
- const btoaFn = globalThis.btoa;
1752
- return btoaFn ? btoaFn(s) : Buffer.from(s, "utf8").toString("base64");
1753
- }
1754
-
1755
- // src/scan/remote.ts
1756
- /**
1757
- * Scansiona una lista di File nel browser usando il motore remoto via HTTP.
1758
- * Non richiede WASM né dipendenze native sul client.
1759
- */
1760
- async function scanFilesWithRemoteYara(files, rulesSource, remote) {
1761
- const engine = await createRemoteEngine(remote);
1762
- const compiled = await engine.compile(rulesSource);
1763
- const results = [];
1764
- for (const file of files) {
1765
- try {
1766
- const bytes = new Uint8Array(await file.arrayBuffer());
1767
- const matches = await compiled.scan(bytes);
1768
- results.push({ file, matches });
1769
- }
1770
- catch (err) {
1771
- console.warn("[remote-yara] scan error for", file.name, err);
1772
- results.push({ file, matches: [], error: String(err?.message ?? err) });
1773
- }
1774
- }
1775
- return results;
1776
- }
1777
-
1778
- const ARCHIVE_BOMB_DETECTED = "ARCHIVE_BOMB_DETECTED";
1779
- const SIG_LFH = 0x04034b50;
1780
- const SIG_CEN = 0x02014b50;
1781
- const DEFAULTS = {
1782
- maxEntries: 1000,
1783
- maxTotalUncompressedBytes: 500 * 1024 * 1024,
1784
- maxPerEntryUncompressedBytes: 100 * 1024 * 1024,
1785
- maxEntryNameLength: 255,
1786
- maxCompressionRatio: 100,
1787
- eocdSearchWindow: 70000,
1788
- };
1789
- function r16(buf, off) {
1790
- return buf.readUInt16LE(off);
1791
- }
1792
- function r32(buf, off) {
1793
- return buf.readUInt32LE(off);
1794
- }
1795
- function isZipLike(buf) {
1796
- return (buf.length >= 4 && buf[0] === 0x50 && buf[1] === 0x4b && buf[2] === 0x03 && buf[3] === 0x04);
1797
- }
1798
- function lastIndexOfEOCD(buf, window) {
1799
- const sig = Buffer.from([0x50, 0x4b, 0x05, 0x06]);
1800
- const start = Math.max(0, buf.length - window);
1801
- const idx = buf.lastIndexOf(sig, Math.min(buf.length - sig.length, buf.length - 1));
1802
- return idx >= start ? idx : -1;
1803
- }
1804
- function hasTraversal(name) {
1805
- return (name.includes("../") || name.includes("..\\") || name.startsWith("/") || /^[A-Za-z]:/.test(name));
1806
- }
1807
- function makeBombError() {
1808
- return Object.assign(new Error("Archive bomb detected: decompression limits exceeded"), {
1809
- code: ARCHIVE_BOMB_DETECTED,
1810
- });
1811
- }
1812
- /**
1813
- * Feeds `compressed` into a raw DEFLATE inflate stream and counts the actual
1814
- * output bytes. Resolves with bombed=true and aborts early if any limit fires:
1815
- * - decompressed bytes > maxPerEntry
1816
- * - totalSoFar + decompressed > maxTotal
1817
- * - decompressed / compressed > maxRatio (ratio measured on real bytes, not headers)
1818
- *
1819
- * Malformed DEFLATE is treated as safe (bombed=false, decompressed=0).
1820
- */
1821
- function streamInflate(compressed, maxPerEntry, maxTotal, alreadySeen, maxRatio) {
1822
- return new Promise((resolve) => {
1823
- const inf = zlib.createInflateRaw();
1824
- let out = 0;
1825
- const compBytes = compressed.length;
1826
- let done = false;
1827
- const finish = (bombed) => {
1828
- if (done)
1829
- return;
1830
- done = true;
1831
- inf.destroy();
1832
- resolve({ decompressed: out, bombed });
1833
- };
1834
- inf.on("data", (chunk) => {
1835
- out += chunk.length;
1836
- if (out > maxPerEntry ||
1837
- alreadySeen + out > maxTotal ||
1838
- (compBytes > 0 && out / compBytes > maxRatio)) {
1839
- finish(true);
1840
- }
1841
- });
1842
- inf.on("end", () => finish(false));
1843
- // Malformed DEFLATE stream → not a bomb, just corrupt
1844
- inf.on("error", () => finish(false));
1845
- inf.end(compressed);
1846
- });
1847
- }
1848
- function createZipBombGuard(opts = {}) {
1849
- const cfg = { ...DEFAULTS, ...opts };
1850
- return {
1851
- async scan(input) {
1852
- const buf = Buffer.from(input);
1853
- const matches = [];
1854
- if (!isZipLike(buf))
1855
- return matches;
1856
- // ── 1. Locate EOCD ──────────────────────────────────────────────────────
1857
- const eocdPos = lastIndexOfEOCD(buf, cfg.eocdSearchWindow);
1858
- if (eocdPos < 0 || eocdPos + 22 > buf.length) {
1859
- matches.push({ rule: "zip_eocd_not_found", severity: "medium" });
1860
- return matches;
1861
- }
1862
- const totalEntries = r16(buf, eocdPos + 10);
1863
- const cdSize = r32(buf, eocdPos + 12);
1864
- const cdOffset = r32(buf, eocdPos + 16);
1865
- if (cdOffset + cdSize > buf.length) {
1866
- matches.push({ rule: "zip_cd_out_of_bounds", severity: "medium" });
1867
- return matches;
1868
- }
1869
- const lfhIndex = [];
1870
- let ptr = cdOffset;
1871
- let seen = 0;
1872
- while (ptr + 46 <= cdOffset + cdSize && seen < totalEntries) {
1873
- if (r32(buf, ptr) !== SIG_CEN)
1874
- break;
1875
- const cdCompSize = r32(buf, ptr + 20);
1876
- const fnLen = r16(buf, ptr + 28);
1877
- const exLen = r16(buf, ptr + 30);
1878
- const cmLen = r16(buf, ptr + 32);
1879
- const lfhOffset = r32(buf, ptr + 42);
1880
- const nameEnd = ptr + 46 + fnLen;
1881
- if (nameEnd > buf.length)
1882
- break;
1883
- const name = buf.toString("utf8", ptr + 46, nameEnd);
1884
- seen++;
1885
- lfhIndex.push({ lfhOffset, cdCompSize });
1886
- if (name.length > cfg.maxEntryNameLength) {
1887
- matches.push({
1888
- rule: "zip_entry_name_too_long",
1889
- severity: "medium",
1890
- meta: { name, length: name.length },
1891
- });
1892
- }
1893
- if (hasTraversal(name)) {
1894
- matches.push({ rule: "zip_path_traversal_entry", severity: "medium", meta: { name } });
1895
- }
1896
- ptr = nameEnd + exLen + cmLen;
1897
- }
1898
- if (seen !== totalEntries) {
1899
- matches.push({
1900
- rule: "zip_cd_truncated",
1901
- severity: "medium",
1902
- meta: { seen, totalEntries },
1903
- });
1904
- }
1905
- if (seen > cfg.maxEntries) {
1906
- matches.push({
1907
- rule: "zip_too_many_entries",
1908
- severity: "medium",
1909
- meta: { seen, limit: cfg.maxEntries },
1910
- });
1911
- // Return early — decompressing thousands of entries would be a DoS vector
1912
- return matches;
1913
- }
1914
- // ── 3. True streaming decompression — archive bomb detection ────────────
1915
- // For every DEFLATE entry (method=8) we feed the raw compressed bytes into
1916
- // zlib.createInflateRaw() and count the bytes that come OUT. We abort the
1917
- // moment any limit fires; we NEVER trust the header-reported uncompressed
1918
- // size for the ratio decision.
1919
- //
1920
- // For STORED entries (method=0) compressed == uncompressed by spec, so the
1921
- // byte count is immediate.
1922
- let totalDecompressed = 0;
1923
- for (const { lfhOffset, cdCompSize } of lfhIndex) {
1924
- if (lfhOffset + 30 > buf.length)
1925
- continue;
1926
- if (r32(buf, lfhOffset) !== SIG_LFH)
1927
- continue;
1928
- const gpbf = r16(buf, lfhOffset + 6);
1929
- const method = r16(buf, lfhOffset + 8);
1930
- let lfhCompSz = r32(buf, lfhOffset + 18);
1931
- const fnLen = r16(buf, lfhOffset + 26);
1932
- const exLen = r16(buf, lfhOffset + 28);
1933
- const dataOff = lfhOffset + 30 + fnLen + exLen;
1934
- // If the data-descriptor flag is set (GPBF bit 3), the LFH sizes are 0.
1935
- // Fall back to the CD size purely for navigation — not for bomb detection.
1936
- if ((gpbf & 0x08) !== 0 && lfhCompSz === 0) {
1937
- lfhCompSz = cdCompSize;
1938
- }
1939
- if (dataOff + lfhCompSz > buf.length)
1940
- continue; // truncated entry — skip
1941
- if (method === 8 /* DEFLATE */) {
1942
- const compressed = buf.slice(dataOff, dataOff + lfhCompSz);
1943
- const { decompressed, bombed } = await streamInflate(compressed, cfg.maxPerEntryUncompressedBytes, cfg.maxTotalUncompressedBytes, totalDecompressed, cfg.maxCompressionRatio);
1944
- if (bombed)
1945
- throw makeBombError();
1946
- totalDecompressed += decompressed;
1947
- }
1948
- else if (method === 0 /* STORED */) {
1949
- // Compressed == uncompressed for stored entries
1950
- if (lfhCompSz > cfg.maxPerEntryUncompressedBytes)
1951
- throw makeBombError();
1952
- totalDecompressed += lfhCompSz;
1953
- if (totalDecompressed > cfg.maxTotalUncompressedBytes)
1954
- throw makeBombError();
1955
- }
1956
- // Other methods (bzip2=12, lzma=14, zstd=93, …) — skip; no built-in support
1957
- }
1958
- return matches;
1959
- },
1960
- };
1961
- }
1962
-
1963
- /** Decompilation-specific types for Pompelmi */
1964
- const SUSPICIOUS_PATTERNS = [
1965
- {
1966
- name: "syscall_direct",
1967
- description: "Direct system call without library wrapper",
1968
- severity: "medium",
1969
- pattern: /syscall|sysenter|int\s+0x80/i,
1970
- },
1971
- {
1972
- name: "process_injection",
1973
- description: "Process injection techniques",
1974
- severity: "high",
1975
- pattern: /CreateRemoteThread|WriteProcessMemory|VirtualAllocEx/i,
1976
- },
1977
- {
1978
- name: "anti_debug",
1979
- description: "Anti-debugging techniques",
1980
- severity: "medium",
1981
- pattern: /IsDebuggerPresent|CheckRemoteDebuggerPresent|OutputDebugString/i,
1982
- },
1983
- {
1984
- name: "obfuscation_xor",
1985
- description: "XOR-based obfuscation pattern",
1986
- severity: "medium",
1987
- pattern: /xor.*0x[0-9a-f]+.*xor/i,
1988
- },
1989
- {
1990
- name: "crypto_constants",
1991
- description: "Cryptographic constants",
1992
- severity: "low",
1993
- pattern: /0x67452301|0xefcdab89|0x98badcfe|0x10325476/i,
1994
- },
1995
- ];
1996
-
1997
- /**
1998
- * Batch scanning with concurrency control
1999
- * @module utils/batch-scanner
2000
- */
2001
- /**
2002
- * Batch file scanner with concurrency control and progress tracking
2003
- */
2004
- class BatchScanner {
2005
- constructor(options = {}) {
2006
- this.options = {
2007
- concurrency: 5,
2008
- continueOnError: true,
2009
- ...options,
2010
- };
2011
- }
2012
- /**
2013
- * Scan multiple files with controlled concurrency
2014
- */
2015
- async scanBatch(tasks) {
2016
- const startTime = Date.now();
2017
- const results = new Array(tasks.length);
2018
- const errors = [];
2019
- let successCount = 0;
2020
- let errorCount = 0;
2021
- let completedCount = 0;
2022
- const concurrency = this.options.concurrency ?? 5;
2023
- // Process tasks in chunks with controlled concurrency
2024
- const processingQueue = [];
2025
- let currentIndex = 0;
2026
- const processTask = async (index) => {
2027
- try {
2028
- const task = tasks[index];
2029
- const report = await scanBytes(task.content, {
2030
- ...this.options,
2031
- ctx: task.context,
2032
- });
2033
- results[index] = report;
2034
- successCount++;
2035
- completedCount++;
2036
- if (this.options.onProgress) {
2037
- this.options.onProgress(completedCount, tasks.length, report);
2038
- }
2039
- }
2040
- catch (error) {
2041
- errorCount++;
2042
- completedCount++;
2043
- const err = error instanceof Error ? error : new Error(String(error));
2044
- if (this.options.onError) {
2045
- this.options.onError(err, index);
2046
- }
2047
- errors.push({ index, error: err });
2048
- if (!this.options.continueOnError) {
2049
- throw err;
2050
- }
2051
- results[index] = null;
2052
- }
2053
- };
2054
- // Start initial batch of concurrent tasks
2055
- while (currentIndex < tasks.length) {
2056
- while (processingQueue.length < concurrency && currentIndex < tasks.length) {
2057
- const promise = processTask(currentIndex);
2058
- processingQueue.push(promise);
2059
- currentIndex++;
2060
- // Remove completed promises from queue
2061
- promise
2062
- .finally(() => {
2063
- const idx = processingQueue.indexOf(promise);
2064
- if (idx > -1)
2065
- processingQueue.splice(idx, 1);
2066
- })
2067
- .catch(() => {
2068
- // Rejections are handled by the main queue waits; swallow the cleanup chain.
2069
- });
2070
- }
2071
- // Wait for at least one task to complete before continuing
2072
- if (processingQueue.length >= concurrency) {
2073
- await Promise.race(processingQueue);
2074
- }
2075
- }
2076
- // Wait for all remaining tasks
2077
- await Promise.all(processingQueue);
2078
- const totalDurationMs = Date.now() - startTime;
2079
- return {
2080
- reports: results,
2081
- successCount,
2082
- errorCount,
2083
- totalDurationMs,
2084
- errors,
2085
- };
2086
- }
2087
- /**
2088
- * Scan files from File objects (browser environment)
2089
- */
2090
- async scanFiles(files) {
2091
- const tasks = await Promise.all(files.map(async (file) => ({
2092
- content: new Uint8Array(await file.arrayBuffer()),
2093
- context: {
2094
- filename: file.name,
2095
- mimeType: file.type,
2096
- size: file.size,
2097
- },
2098
- })));
2099
- return this.scanBatch(tasks);
2100
- }
2101
- /**
2102
- * Scan files from file paths (Node.js environment)
2103
- */
2104
- async scanFilePaths(filePaths) {
2105
- const fs = await import('fs/promises');
2106
- const path = await import('path');
2107
- const tasks = await Promise.all(filePaths.map(async (filePath) => {
2108
- const [content, stats] = await Promise.all([fs.readFile(filePath), fs.stat(filePath)]);
2109
- return {
2110
- content: new Uint8Array(content),
2111
- context: {
2112
- filename: path.basename(filePath),
2113
- size: stats.size,
2114
- },
2115
- };
2116
- }));
2117
- return this.scanBatch(tasks);
2118
- }
2119
- }
2120
- /**
2121
- * Quick helper for batch scanning with default options
2122
- */
2123
- async function batchScan(tasks, options) {
2124
- const scanner = new BatchScanner(options);
2125
- return scanner.scanBatch(tasks);
2126
- }
2127
-
2128
- /**
2129
- * Export utilities for scan results
2130
- * @module utils/export
2131
- */
2132
- /**
2133
- * Export scan results to various formats
2134
- */
2135
- class ScanResultExporter {
2136
- /**
2137
- * Export to JSON format
2138
- */
2139
- toJSON(reports, options = {}) {
2140
- const data = Array.isArray(reports) ? reports : [reports];
2141
- if (!options.includeDetails) {
2142
- // Simplified output
2143
- const simplified = data.map((r) => ({
2144
- verdict: r.verdict,
2145
- file: r.file?.name,
2146
- matches: r.matches.length,
2147
- durationMs: r.durationMs,
2148
- }));
2149
- return options.prettyPrint ? JSON.stringify(simplified, null, 2) : JSON.stringify(simplified);
2150
- }
2151
- return options.prettyPrint ? JSON.stringify(data, null, 2) : JSON.stringify(data);
2152
- }
2153
- /**
2154
- * Export to CSV format
2155
- */
2156
- toCSV(reports, options = {}) {
2157
- const data = Array.isArray(reports) ? reports : [reports];
2158
- const headers = [
2159
- "filename",
2160
- "verdict",
2161
- "matches_count",
2162
- "file_size",
2163
- "mime_type",
2164
- "duration_ms",
2165
- "engine",
2166
- ];
2167
- if (options.includeDetails) {
2168
- headers.push("reasons", "match_rules");
2169
- }
2170
- const rows = data.map((report) => {
2171
- const row = [
2172
- this.escapeCsv(report.file?.name || "unknown"),
2173
- report.verdict,
2174
- report.matches.length.toString(),
2175
- (report.file?.size || 0).toString(),
2176
- this.escapeCsv(report.file?.mimeType || "unknown"),
2177
- (report.durationMs || 0).toString(),
2178
- report.engine || "unknown",
2179
- ];
2180
- if (options.includeDetails) {
2181
- row.push(this.escapeCsv((report.reasons || []).join("; ")), this.escapeCsv(report.matches.map((m) => m.rule).join("; ")));
2182
- }
2183
- return row.join(",");
2184
- });
2185
- return [headers.join(","), ...rows].join("\n");
2186
- }
2187
- /**
2188
- * Export to Markdown format
2189
- */
2190
- toMarkdown(reports, options = {}) {
2191
- const data = Array.isArray(reports) ? reports : [reports];
2192
- let md = "# Scan Results\n\n";
2193
- md += `**Total Scans:** ${data.length}\n\n`;
2194
- const clean = data.filter((r) => r.verdict === "clean").length;
2195
- const suspicious = data.filter((r) => r.verdict === "suspicious").length;
2196
- const malicious = data.filter((r) => r.verdict === "malicious").length;
2197
- md += "## Summary\n\n";
2198
- md += `- ✅ Clean: ${clean}\n`;
2199
- md += `- ⚠️ Suspicious: ${suspicious}\n`;
2200
- md += `- ❌ Malicious: ${malicious}\n\n`;
2201
- md += "## Detailed Results\n\n";
2202
- for (const report of data) {
2203
- const icon = report.verdict === "clean" ? "✅" : report.verdict === "suspicious" ? "⚠️" : "❌";
2204
- md += `### ${icon} ${report.file?.name || "Unknown"}\n\n`;
2205
- md += `- **Verdict:** ${report.verdict}\n`;
2206
- md += `- **Size:** ${this.formatBytes(report.file?.size || 0)}\n`;
2207
- md += `- **MIME Type:** ${report.file?.mimeType || "unknown"}\n`;
2208
- md += `- **Duration:** ${report.durationMs || 0}ms\n`;
2209
- md += `- **Matches:** ${report.matches.length}\n`;
2210
- if (options.includeDetails && report.matches.length > 0) {
2211
- md += "\n**Match Details:**\n";
2212
- for (const match of report.matches) {
2213
- md += `- ${match.rule}`;
2214
- if (match.tags && match.tags.length > 0) {
2215
- md += ` (${match.tags.join(", ")})`;
2216
- }
2217
- md += "\n";
2218
- }
2219
- }
2220
- md += "\n";
2221
- }
2222
- return md;
2223
- }
2224
- /**
2225
- * Export to SARIF format (Static Analysis Results Interchange Format)
2226
- * Useful for CI/CD integration
2227
- */
2228
- toSARIF(reports, options = {}) {
2229
- const data = Array.isArray(reports) ? reports : [reports];
2230
- const results = data.flatMap((report) => {
2231
- if (report.verdict === "clean")
2232
- return [];
2233
- return report.matches.map((match) => ({
2234
- ruleId: match.rule,
2235
- level: report.verdict === "malicious" ? "error" : "warning",
2236
- message: {
2237
- text: `${match.rule} detected in ${report.file?.name || "unknown file"}`,
2238
- },
2239
- locations: [
2240
- {
2241
- physicalLocation: {
2242
- artifactLocation: {
2243
- uri: report.file?.name || "unknown",
2244
- },
2245
- },
2246
- },
2247
- ],
2248
- properties: {
2249
- tags: match.tags,
2250
- metadata: match.meta,
2251
- },
2252
- }));
2253
- });
2254
- const sarif = {
2255
- version: "2.1.0",
2256
- $schema: "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
2257
- runs: [
2258
- {
2259
- tool: {
2260
- driver: {
2261
- name: "Pompelmi",
2262
- version: "0.29.0",
2263
- informationUri: "https://pompelmi.github.io/pompelmi/",
2264
- },
2265
- },
2266
- results,
2267
- },
2268
- ],
2269
- };
2270
- return options.prettyPrint ? JSON.stringify(sarif, null, 2) : JSON.stringify(sarif);
2271
- }
2272
- /**
2273
- * Export to HTML format
2274
- */
2275
- toHTML(reports, options = {}) {
2276
- const data = Array.isArray(reports) ? reports : [reports];
2277
- const clean = data.filter((r) => r.verdict === "clean").length;
2278
- const suspicious = data.filter((r) => r.verdict === "suspicious").length;
2279
- const malicious = data.filter((r) => r.verdict === "malicious").length;
2280
- let html = `<!DOCTYPE html>
2281
- <html lang="en">
2282
- <head>
2283
- <meta charset="UTF-8">
2284
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
2285
- <title>Pompelmi Scan Results</title>
2286
- <style>
2287
- body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; max-width: 1200px; margin: 0 auto; padding: 20px; }
2288
- .summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px; margin: 20px 0; }
2289
- .card { padding: 20px; border-radius: 8px; text-align: center; }
2290
- .clean { background: #d4edda; color: #155724; }
2291
- .suspicious { background: #fff3cd; color: #856404; }
2292
- .malicious { background: #f8d7da; color: #721c24; }
2293
- .result { border: 1px solid #ddd; border-radius: 8px; padding: 15px; margin: 10px 0; }
2294
- .result h3 { margin-top: 0; }
2295
- .badge { display: inline-block; padding: 4px 8px; border-radius: 4px; font-size: 0.8em; margin: 2px; }
2296
- table { width: 100%; border-collapse: collapse; }
2297
- th, td { padding: 8px; text-align: left; border-bottom: 1px solid #ddd; }
2298
- </style>
2299
- </head>
2300
- <body>
2301
- <h1>🛡️ Pompelmi Scan Results</h1>
2302
- <div class="summary">
2303
- <div class="card clean"><h2>${clean}</h2><p>Clean Files</p></div>
2304
- <div class="card suspicious"><h2>${suspicious}</h2><p>Suspicious Files</p></div>
2305
- <div class="card malicious"><h2>${malicious}</h2><p>Malicious Files</p></div>
2306
- </div>
2307
- <h2>Detailed Results</h2>`;
2308
- for (const report of data) {
2309
- const statusClass = report.verdict;
2310
- html += `<div class="result ${statusClass}">`;
2311
- html += `<h3>${this.escapeHtml(report.file?.name || "Unknown")}</h3>`;
2312
- html += `<table>`;
2313
- html += `<tr><th>Verdict</th><td>${report.verdict.toUpperCase()}</td></tr>`;
2314
- html += `<tr><th>Size</th><td>${this.formatBytes(report.file?.size || 0)}</td></tr>`;
2315
- html += `<tr><th>MIME Type</th><td>${this.escapeHtml(report.file?.mimeType || "unknown")}</td></tr>`;
2316
- html += `<tr><th>Duration</th><td>${report.durationMs || 0}ms</td></tr>`;
2317
- html += `<tr><th>Matches</th><td>${report.matches.length}</td></tr>`;
2318
- html += `</table>`;
2319
- if (options.includeDetails && report.matches.length > 0) {
2320
- html += `<h4>Match Details:</h4><ul>`;
2321
- for (const match of report.matches) {
2322
- html += `<li><strong>${this.escapeHtml(match.rule)}</strong>`;
2323
- if (match.tags && match.tags.length > 0) {
2324
- html += ` ${match.tags.map((tag) => `<span class="badge">${this.escapeHtml(tag)}</span>`).join("")}`;
2325
- }
2326
- html += `</li>`;
2327
- }
2328
- html += `</ul>`;
2329
- }
2330
- html += `</div>`;
2331
- }
2332
- html += `</body></html>`;
2333
- return html;
2334
- }
2335
- /**
2336
- * Export to specified format
2337
- */
2338
- export(reports, format, options = {}) {
2339
- switch (format) {
2340
- case "json":
2341
- return this.toJSON(reports, options);
2342
- case "csv":
2343
- return this.toCSV(reports, options);
2344
- case "markdown":
2345
- return this.toMarkdown(reports, options);
2346
- case "html":
2347
- return this.toHTML(reports, options);
2348
- case "sarif":
2349
- return this.toSARIF(reports, options);
2350
- default:
2351
- throw new Error(`Unsupported export format: ${format}`);
2352
- }
2353
- }
2354
- escapeCsv(value) {
2355
- if (value.includes(",") || value.includes('"') || value.includes("\n")) {
2356
- return `"${value.replace(/"/g, '""')}"`;
2357
- }
2358
- return value;
2359
- }
2360
- escapeHtml(value) {
2361
- return value
2362
- .replace(/&/g, "&amp;")
2363
- .replace(/</g, "&lt;")
2364
- .replace(/>/g, "&gt;")
2365
- .replace(/"/g, "&quot;")
2366
- .replace(/'/g, "&#039;");
2367
- }
2368
- formatBytes(bytes) {
2369
- if (bytes === 0)
2370
- return "0 Bytes";
2371
- const k = 1024;
2372
- const sizes = ["Bytes", "KB", "MB", "GB"];
2373
- const i = Math.floor(Math.log(bytes) / Math.log(k));
2374
- return Math.round((bytes / k ** i) * 100) / 100 + " " + sizes[i];
2375
- }
2376
- }
2377
- /**
2378
- * Quick export helper
2379
- */
2380
- function exportScanResults(reports, format, options) {
2381
- const exporter = new ScanResultExporter();
2382
- return exporter.export(reports, format, options);
2383
- }
2384
-
2385
- /**
2386
- * Threat intelligence integration and enhanced detection
2387
- * @module utils/threat-intelligence
2388
- */
2389
- /**
2390
- * Built-in threat intelligence - known malware hashes
2391
- * In production, this would connect to real threat intel APIs
2392
- */
2393
- class LocalThreatIntelligence {
2394
- constructor() {
2395
- this.name = "Local Database";
2396
- this.knownThreats = new Map();
2397
- // Initialize with some example known threats (in production, load from database)
2398
- this.initializeKnownThreats();
2399
- }
2400
- initializeKnownThreats() {
2401
- // Example: EICAR test file hash
2402
- this.knownThreats.set("275a021bbfb6489e54d471899f7db9d1663fc695ec2fe2a2c4538aabf651fd0f", {
2403
- threatLevel: 100,
2404
- category: "test-malware",
2405
- source: "local",
2406
- metadata: { name: "EICAR Test File" },
2407
- });
2408
- }
2409
- async checkHash(hash) {
2410
- return this.knownThreats.get(hash.toLowerCase()) || null;
2411
- }
2412
- /**
2413
- * Add a known threat to the local database
2414
- */
2415
- addThreat(hash, info) {
2416
- this.knownThreats.set(hash.toLowerCase(), info);
2417
- }
2418
- /**
2419
- * Remove a threat from the local database
2420
- */
2421
- removeThreat(hash) {
2422
- return this.knownThreats.delete(hash.toLowerCase());
2423
- }
2424
- /**
2425
- * Get all known threats
2426
- */
2427
- getAllThreats() {
2428
- return new Map(this.knownThreats);
2429
- }
2430
- }
2431
- /**
2432
- * Threat intelligence aggregator
2433
- */
2434
- class ThreatIntelligenceAggregator {
2435
- constructor(sources) {
2436
- this.sources = [];
2437
- if (sources) {
2438
- this.sources = sources;
2439
- }
2440
- else {
2441
- // Default to local intelligence
2442
- this.sources = [new LocalThreatIntelligence()];
2443
- }
2444
- }
2445
- /**
2446
- * Add a threat intelligence source
2447
- */
2448
- addSource(source) {
2449
- this.sources.push(source);
2450
- }
2451
- /**
2452
- * Check file hash against all sources
2453
- */
2454
- async checkHash(hash) {
2455
- const results = await Promise.allSettled(this.sources.map((source) => source.checkHash(hash)));
2456
- const threats = [];
2457
- for (const result of results) {
2458
- if (result.status === "fulfilled" && result.value) {
2459
- threats.push(result.value);
2460
- }
2461
- }
2462
- return threats;
2463
- }
2464
- /**
2465
- * Enhance scan report with threat intelligence
2466
- */
2467
- async enhanceScanReport(content, report) {
2468
- // Calculate file hash
2469
- const hash = crypto.createHash("sha256").update(content).digest("hex");
2470
- // Check threat intelligence
2471
- const threatIntel = await this.checkHash(hash);
2472
- // Calculate risk score
2473
- const riskScore = this.calculateRiskScore(report, threatIntel);
2474
- return {
2475
- ...report,
2476
- fileHash: hash,
2477
- threatIntel: threatIntel.length > 0 ? threatIntel : undefined,
2478
- riskScore,
2479
- };
2480
- }
2481
- /**
2482
- * Calculate overall risk score based on scan results and threat intel
2483
- */
2484
- calculateRiskScore(report, threats) {
2485
- let score = 0;
2486
- // Base score from verdict
2487
- switch (report.verdict) {
2488
- case "malicious":
2489
- score += 70;
2490
- break;
2491
- case "suspicious":
2492
- score += 40;
2493
- break;
2494
- case "clean":
2495
- score += 0;
2496
- break;
2497
- }
2498
- // Add points for number of matches
2499
- score += Math.min(report.matches.length * 5, 20);
2500
- // Add points from threat intelligence
2501
- if (threats.length > 0) {
2502
- const maxThreat = Math.max(...threats.map((t) => t.threatLevel));
2503
- score = Math.max(score, maxThreat);
2504
- }
2505
- return Math.min(score, 100);
2506
- }
2507
- }
2508
- /**
2509
- * Create default threat intelligence aggregator
2510
- */
2511
- function createThreatIntelligence() {
2512
- return new ThreatIntelligenceAggregator();
2513
- }
2514
- /**
2515
- * Helper to get file hash
2516
- */
2517
- function getFileHash(content) {
2518
- return crypto.createHash("sha256").update(content).digest("hex");
2519
- }
2520
-
2521
- /**
2522
- * Validates a File by MIME type and size (max 5 MB).
2523
- */
2524
- function validateFile(file) {
2525
- const maxSize = 5 * 1024 * 1024;
2526
- const allowedTypes = ["text/plain", "application/json", "text/csv"];
2527
- if (!allowedTypes.includes(file.type)) {
2528
- return { valid: false, error: "Unsupported file type" };
2529
- }
2530
- if (file.size > maxSize) {
2531
- return { valid: false, error: "File too large (max 5 MB)" };
2532
- }
2533
- return { valid: true };
2534
- }
2535
-
2536
- function mapMatchesToVerdict(matches = []) {
2537
- if (!matches.length)
2538
- return "clean";
2539
- const malHints = ["trojan", "ransom", "worm", "spy", "rootkit", "keylog", "botnet"];
2540
- const tagSet = new Set(matches.flatMap((m) => (m.tags ?? []).map((t) => t.toLowerCase())));
2541
- const nameHit = (r) => malHints.some((h) => r.toLowerCase().includes(h));
2542
- const isMal = matches.some((m) => nameHit(m.rule)) || tagSet.has("malware") || tagSet.has("critical");
2543
- return isMal ? "malicious" : "suspicious";
2544
- }
2545
-
2546
- exports.ARCHIVES = ARCHIVES;
2547
- exports.BatchScanner = BatchScanner;
2548
- exports.CONFIG_PRESETS = CONFIG_PRESETS;
2549
- exports.CONSERVATIVE_DEFAULT = CONSERVATIVE_DEFAULT;
2550
- exports.CommonHeuristicsScanner = CommonHeuristicsScanner;
2551
- exports.ConfigManager = ConfigManager;
2552
- exports.DEFAULT_CONFIG = DEFAULT_CONFIG;
2553
- exports.DEFAULT_POLICY = DEFAULT_POLICY;
2554
- exports.DOCUMENTS_ONLY = DOCUMENTS_ONLY;
2555
- exports.HipaaTemp = HipaaTemp;
2556
- exports.IMAGES_ONLY = IMAGES_ONLY;
2557
- exports.LocalThreatIntelligence = LocalThreatIntelligence;
2558
- exports.POLICY_PACKS = POLICY_PACKS;
2559
- exports.PerformanceTracker = PerformanceTracker;
2560
- exports.STRICT_PUBLIC_UPLOAD = STRICT_PUBLIC_UPLOAD;
2561
- exports.SUSPICIOUS_PATTERNS = SUSPICIOUS_PATTERNS;
2562
- exports.ScanCacheManager = ScanCacheManager;
2563
- exports.ScanResultExporter = ScanResultExporter;
2564
- exports.ThreatIntelligenceAggregator = ThreatIntelligenceAggregator;
2565
- exports.aggregateScanStats = aggregateScanStats;
2566
- exports.analyzeNestedArchives = analyzeNestedArchives;
2567
- exports.batchScan = batchScan;
2568
- exports.composeScanners = composeScanners;
2569
- exports.createConfig = createConfig;
2570
- exports.createHipaaError = createHipaaError;
2571
- exports.createPresetScanner = createPresetScanner;
2572
- exports.createThreatIntelligence = createThreatIntelligence;
2573
- exports.createZipBombGuard = createZipBombGuard;
2574
- exports.definePolicy = definePolicy;
2575
- exports.detectObfuscatedScripts = detectObfuscatedScripts;
2576
- exports.detectPolyglot = detectPolyglot;
2577
- exports.exportScanResults = exportScanResults;
2578
- exports.getDefaultCache = getDefaultCache;
2579
- exports.getFileHash = getFileHash;
2580
- exports.getHipaaManager = getHipaaManager;
2581
- exports.getPolicyPack = getPolicyPack;
2582
- exports.getPresetConfig = getPresetConfig;
2583
- exports.initializeHipaaCompliance = initializeHipaaCompliance;
2584
- exports.mapMatchesToVerdict = mapMatchesToVerdict;
2585
- exports.resetDefaultCache = resetDefaultCache;
2586
- exports.scanBytes = scanBytes;
2587
- exports.scanFile = scanFile;
2588
- exports.scanFiles = scanFiles;
2589
- exports.scanFilesWithRemoteYara = scanFilesWithRemoteYara;
2590
- exports.validateFile = validateFile;
2591
- //# sourceMappingURL=pompelmi.cjs.map