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