@profullstack/threatcrush 0.13.74 → 0.13.76

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.
@@ -0,0 +1,754 @@
1
+ "use strict";
2
+
3
+ // ../../packages/av/src/node/locations.ts
4
+ var import_node_fs = require("fs");
5
+ var import_node_os = require("os");
6
+ var import_node_path = require("path");
7
+
8
+ // ../../packages/av/src/node/walk.ts
9
+ var import_promises = require("fs/promises");
10
+ var import_node_path2 = require("path");
11
+
12
+ // ../../packages/av/src/node/scan-file.ts
13
+ var import_node_crypto = require("crypto");
14
+ var import_node_fs2 = require("fs");
15
+
16
+ // ../../packages/av/src/magic.ts
17
+ var starts = (b, sig, at = 0) => b.length >= at + sig.length && sig.every((v, i) => b[at + i] === v);
18
+ function sniffKind(b) {
19
+ if (starts(b, [77, 90])) return "pe";
20
+ if (starts(b, [127, 69, 76, 70])) return "elf";
21
+ if (starts(b, [254, 237, 250, 206]) || starts(b, [254, 237, 250, 207]) || starts(b, [206, 250, 237, 254]) || starts(b, [207, 250, 237, 254])) return "macho";
22
+ if (starts(b, [202, 254, 186, 190]) && b.length >= 8 && b[4] === 0 && b[5] === 0 && b[6] === 0 && b[7] > 0 && b[7] < 20) return "macho";
23
+ if (starts(b, [80, 75, 3, 4]) || starts(b, [80, 75, 5, 6])) return "zip";
24
+ if (starts(b, [208, 207, 17, 224, 161, 177, 26, 225])) return "ole";
25
+ if (starts(b, [37, 80, 68, 70, 45])) return "pdf";
26
+ if (starts(b, [76, 0, 0, 0, 1, 20, 2, 0])) return "lnk";
27
+ if (starts(b, [123, 92, 114, 116, 102])) return "rtf";
28
+ if (starts(b, [100, 101, 120, 10])) return "dex";
29
+ if (starts(b, [35, 33])) return "script";
30
+ if (starts(b, [255, 216, 255]) || starts(b, [137, 80, 78, 71]) || starts(b, [71, 73, 70, 56]) || starts(b, [82, 73, 70, 70]) && starts(b, [87, 69, 66, 80], 8) || starts(b, [66, 77])) return "image";
31
+ if (starts(b, [102, 116, 121, 112], 4) || starts(b, [73, 68, 51]) || starts(b, [26, 69, 223, 163]) || starts(b, [79, 103, 103, 83]) || starts(b, [102, 76, 97, 67])) return "media";
32
+ if (starts(b, [31, 139]) || starts(b, [82, 97, 114, 33]) || starts(b, [55, 122, 188, 175]) || starts(b, [253, 55, 122, 88, 90]) || starts(b, [40, 181, 47, 253])) return "archive";
33
+ if (looksLikeText(b)) return "text";
34
+ return "unknown";
35
+ }
36
+ function looksLikeText(b) {
37
+ const n = Math.min(b.length, 4096);
38
+ if (n === 0) return true;
39
+ let odd = 0;
40
+ for (let i = 0; i < n; i++) {
41
+ const c = b[i];
42
+ if (c === 0) return false;
43
+ if (c < 9 || c > 13 && c < 32) odd++;
44
+ }
45
+ return odd / n < 0.05;
46
+ }
47
+ var EXECUTABLE_KINDS = /* @__PURE__ */ new Set(["pe", "elf", "macho"]);
48
+ var DOCUMENT_EXTENSIONS = /* @__PURE__ */ new Set([
49
+ "pdf",
50
+ "doc",
51
+ "docx",
52
+ "xls",
53
+ "xlsx",
54
+ "ppt",
55
+ "pptx",
56
+ "odt",
57
+ "ods",
58
+ "txt",
59
+ "rtf",
60
+ "csv",
61
+ "jpg",
62
+ "jpeg",
63
+ "png",
64
+ "gif",
65
+ "bmp",
66
+ "webp",
67
+ "heic",
68
+ "svg",
69
+ "mp3",
70
+ "mp4",
71
+ "mov",
72
+ "avi",
73
+ "mkv",
74
+ "wav",
75
+ "m4a"
76
+ ]);
77
+ function extOf(name) {
78
+ const base = name.split(/[\\/]/).pop() ?? name;
79
+ const dot = base.lastIndexOf(".");
80
+ return dot > 0 ? base.slice(dot + 1).toLowerCase() : "";
81
+ }
82
+ function baseName(name) {
83
+ return name.split(/[\\/]/).pop() ?? name;
84
+ }
85
+
86
+ // ../../packages/av/src/rules.ts
87
+ var EICAR = "X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*";
88
+ var SCRIPT_EXTS = /* @__PURE__ */ new Set(["ps1", "psm1", "bat", "cmd", "vbs", "vbe", "js", "jse", "wsf", "hta", "sh", "bash", "zsh", "py", "pl", "rb", "php", "command", "applescript", "scpt"]);
89
+ var WIN_SCRIPT_EXTS = /* @__PURE__ */ new Set(["js", "jse", "vbs", "vbe", "wsf", "hta"]);
90
+ var WEB_EXTS = /* @__PURE__ */ new Set(["html", "htm", "shtml", "xhtml", "svg", "hta"]);
91
+ var SERVER_SCRIPT_EXTS = /* @__PURE__ */ new Set(["php", "phtml", "php3", "php4", "php5", "php7", "phar", "asp", "aspx", "ashx", "jsp", "jspx"]);
92
+ var isScript = (c) => c.kind === "script" || SCRIPT_EXTS.has(c.ext);
93
+ var POWERSHELL_HOST_EXTS = /* @__PURE__ */ new Set(["ps1", "psm1", "bat", "cmd", "vbs", "vbe", "wsf", "hta"]);
94
+ var isWindowsScriptContext = (c) => POWERSHELL_HOST_EXTS.has(c.ext) || c.kind === "lnk" || ["js", "jse"].includes(c.ext) && /ActiveXObject|WScript\.|GetObject\(\s*["']winmgmts/i.test(c.text);
95
+ var NON_RUNNABLE_SOURCE_EXTS = /* @__PURE__ */ new Set(["ts", "tsx", "mts", "cts", "md", "markdown", "rst", "go", "rs", "c", "h", "cc", "cpp", "hpp", "java", "kt", "cs", "swift", "scala"]);
96
+ var NOTE_EXTS = /* @__PURE__ */ new Set(["txt", "html", "htm", "hta", "rtf", ""]);
97
+ var allText = (c) => c.wide ? `${c.text}
98
+ ${c.wide}` : c.text;
99
+ var RANSOM_COMMANDS = [
100
+ /vssadmin(?:\.exe)?["']?\s+delete\s+shadows/i,
101
+ /wmic(?:\.exe)?["']?\s+shadowcopy\s+delete/i,
102
+ /wbadmin(?:\.exe)?["']?\s+delete\s+(?:catalog|backup|systemstatebackup)/i,
103
+ /bcdedit(?:\.exe)?["']?\s+\/set\s+\{default\}\s+(?:recoveryenabled\s+no|bootstatuspolicy\s+ignoreallfailures)/i,
104
+ /Get-WmiObject\s+Win32_Shadowcopy\s*\|\s*ForEach-Object\s*\{\s*\$_\.Delete\(\)/i
105
+ ];
106
+ var RULES = [
107
+ {
108
+ id: "CrushAV.Test.EICAR",
109
+ name: "EICAR test file",
110
+ severity: "low",
111
+ impact: "malicious",
112
+ description: "The industry-standard harmless antivirus test file. It cannot do anything; it only proves the scanner works.",
113
+ test: (c) => c.size <= 128 && c.text.trimStart().startsWith(EICAR)
114
+ },
115
+ {
116
+ id: "CrushAV.Disguise.ExecutableAsDocument",
117
+ name: "Program disguised as a document",
118
+ severity: "high",
119
+ impact: "malicious",
120
+ description: "This file is a program, but its name says it is a document, picture or video. Real documents never do this; it is how malware tricks people into running it. Do not open it.",
121
+ test: (c) => EXECUTABLE_KINDS.has(c.kind) && DOCUMENT_EXTENSIONS.has(c.ext)
122
+ },
123
+ {
124
+ id: "CrushAV.Disguise.DoubleExtension",
125
+ name: "Fake file extension",
126
+ severity: "high",
127
+ impact: "suspicious",
128
+ description: 'The name hides a runnable extension behind a harmless-looking one (like "invoice.pdf.exe"). Windows hides the last part by default, so this looks like a document but runs as a program.',
129
+ test: (c) => /\.(?:pdf|docx?|xlsx?|pptx?|jpe?g|png|gif|txt|rtf|mp3|mp4|mov|zip)[\s_.]*\.(?:exe|scr|com|pif|bat|cmd|js|jse|vbs|vbe|wsf|hta|lnk|ps1|cpl)$/i.test(baseName(c.name)) || /\s{6,}\.(?:exe|scr|com|pif|bat|cmd|js|vbs|hta|lnk)$/i.test(baseName(c.name))
130
+ },
131
+ {
132
+ id: "CrushAV.Disguise.RightToLeftOverride",
133
+ name: "Reversed file name trick",
134
+ severity: "high",
135
+ impact: "malicious",
136
+ description: 'The file name contains an invisible character that reverses the text after it, so "photo\u202Egpj.exe" displays as "photoexe.jpg". Legitimate files have no reason to do this.',
137
+ test: (c) => baseName(c.name).includes("\u202E")
138
+ },
139
+ {
140
+ id: "CrushAV.PowerShell.EncodedCommand",
141
+ name: "Hidden PowerShell command",
142
+ severity: "high",
143
+ impact: "suspicious",
144
+ description: "Runs PowerShell with a long base64-encoded command, which hides what it does. Malware droppers use this constantly; legitimate software rarely needs to.",
145
+ test: (c) => (isWindowsScriptContext(c) || c.kind === "pe") && /powershell(?:\.exe)?["']?[^\n]{0,120}?\s-(?:e|ec|en|enc|enco|encod|encodedcommand)\s+["']?[A-Za-z0-9+/]{80,}={0,2}/i.test(allText(c))
146
+ },
147
+ {
148
+ id: "CrushAV.PowerShell.DownloadExecute",
149
+ name: "Downloads and runs code",
150
+ severity: "medium",
151
+ impact: "suspicious",
152
+ description: "Downloads code from the internet and runs it straight away. Some installers do this; if you did not expect this script, delete it.",
153
+ test: (c) => {
154
+ if (!isWindowsScriptContext(c)) return false;
155
+ const t = allText(c);
156
+ const download = /(?:DownloadString|DownloadData|DownloadFile|Invoke-WebRequest|\biwr\b|Invoke-RestMethod|\birm\b|Net\.WebClient|Start-BitsTransfer)/i.test(t);
157
+ const execute = /(?:\bIEX\b|Invoke-Expression|\|\s*iex\b|Start-Process|\.Invoke\(\))/i.test(t);
158
+ if (!download || !execute) return false;
159
+ const stealthy = /-w(?:indowstyle)?\s+h(?:idden)?\b/i.test(t) && /-ex(?:ecutionpolicy)?\s+bypass/i.test(t);
160
+ return stealthy ? { impact: "malicious", severity: "high", description: "Downloads code and runs it in a hidden window with security checks bypassed. This is how most Windows malware gets installed." } : true;
161
+ }
162
+ },
163
+ {
164
+ id: "CrushAV.Script.WindowsDropper",
165
+ name: "Script that downloads and runs programs",
166
+ severity: "high",
167
+ impact: "malicious",
168
+ description: "A Windows script (JS/VBS/HTA) that fetches something from the internet and launches it. Email attachments like this are malware droppers; nothing legitimate is delivered this way.",
169
+ test: (c) => {
170
+ if (!WIN_SCRIPT_EXTS.has(c.ext)) return false;
171
+ const t = c.text;
172
+ return /WScript\.Shell|Shell\.Application/i.test(t) && /\.(?:Run|Exec|ShellExecute)\s*\(/i.test(t) && /(?:MSXML2\.(?:Server)?XMLHTTP|WinHttp\.WinHttpRequest|ADODB\.Stream|Microsoft\.XMLHTTP)/i.test(t);
173
+ }
174
+ },
175
+ {
176
+ id: "CrushAV.Shortcut.LolbinLauncher",
177
+ name: "Booby-trapped shortcut",
178
+ severity: "high",
179
+ impact: "malicious",
180
+ description: "A Windows shortcut (.lnk) that launches PowerShell, mshta or a similar tool to fetch something from the internet. Shortcuts like this arrive in zip and ISO attachments to install malware.",
181
+ test: (c) => c.kind === "lnk" && /(?:powershell|mshta|wscript|cscript|rundll32|regsvr32|certutil|bitsadmin|cmd(?:\.exe)?\s*\/c)/i.test(allText(c)) && /(?:https?:\/\/|\\\\[\w.-]+@SSL|-enc(?:odedcommand)?\s)/i.test(allText(c))
182
+ },
183
+ {
184
+ id: "CrushAV.Office.MacroInDocx",
185
+ name: "Macros hidden in a regular Office file",
186
+ severity: "high",
187
+ impact: "suspicious",
188
+ description: "This document carries macro code even though its type (.docx/.xlsx/.pptx) is not supposed to have any. That mismatch is a common way to sneak malicious macros past filters.",
189
+ test: (c) => c.kind === "zip" && ["docx", "xlsx", "pptx"].includes(c.ext) && c.text.includes("vbaProject.bin")
190
+ },
191
+ {
192
+ id: "CrushAV.Office.AutoMacro",
193
+ name: "Office macro that runs on open and starts programs",
194
+ severity: "high",
195
+ impact: "malicious",
196
+ description: 'An Office document whose macro runs automatically when opened and starts other programs or downloads files. Never click "Enable content" on this one.',
197
+ test: (c) => {
198
+ if (c.kind !== "ole") return false;
199
+ const t = allText(c);
200
+ if (!/_VBA_PROJECT|VBA\b/i.test(t)) return false;
201
+ const auto = /(?:Auto_?Open|Document_Open|Workbook_Open|AutoExec|Auto_Close|Document_Close)/i.test(t);
202
+ const act = /(?:\bShell\b|WScript\.Shell|CreateObject|URLDownloadToFile|powershell|MSXML2)/i.test(t);
203
+ if (auto && act) return true;
204
+ if (auto) return { impact: "suspicious", severity: "medium", name: "Office macro that runs on open", description: "This document has a macro that runs automatically when opened. Only enable macros for documents you were expecting from someone you trust." };
205
+ return false;
206
+ }
207
+ },
208
+ {
209
+ id: "CrushAV.Office.MacroEnabled",
210
+ name: "Document contains macros",
211
+ severity: "info",
212
+ impact: "info",
213
+ description: "This is a macro-enabled Office document. That is normal for some spreadsheets, but only enable macros if you trust the sender.",
214
+ test: (c) => c.kind === "zip" && ["docm", "xlsm", "pptm", "dotm", "xltm"].includes(c.ext) && c.text.includes("vbaProject.bin")
215
+ },
216
+ {
217
+ id: "CrushAV.PDF.Launch",
218
+ name: "PDF that tries to start a program",
219
+ severity: "high",
220
+ impact: "suspicious",
221
+ description: "This PDF contains a /Launch action, which asks your PDF reader to run a program. Normal documents do not need that.",
222
+ test: (c) => {
223
+ if (c.kind !== "pdf" || !/\/Launch\b/.test(c.text)) return false;
224
+ return /(?:cmd\.exe|powershell|mshta|\.exe\b|\.bat\b|\.vbs\b)/i.test(c.text) ? { impact: "malicious", description: "This PDF tries to start a Windows program (cmd, PowerShell or an .exe) when opened. That is an attack; do not open it." } : true;
225
+ }
226
+ },
227
+ {
228
+ id: "CrushAV.PDF.AutoJavaScript",
229
+ name: "PDF runs JavaScript on open",
230
+ severity: "info",
231
+ impact: "info",
232
+ description: "This PDF runs JavaScript when opened. Fillable forms do this legitimately, so it is shown for information only.",
233
+ test: (c) => c.kind === "pdf" && /\/(?:OpenAction|AA)\b/.test(c.text) && /\/(?:JavaScript|JS)\b/.test(c.text)
234
+ },
235
+ {
236
+ id: "CrushAV.Shell.ReverseShell",
237
+ name: "Remote-control backdoor (reverse shell)",
238
+ severity: "critical",
239
+ impact: "malicious",
240
+ description: "This script connects out to another computer and hands it a command line on this one. That gives a stranger full control of the machine.",
241
+ test: (c) => {
242
+ if (!isScript(c)) return false;
243
+ const t = c.text;
244
+ return /bash\s+-i\s*>&\s*\/dev\/tcp\//.test(t) || /\/dev\/tcp\/\d{1,3}(?:\.\d{1,3}){3}\/\d{2,5}\s*0>&1/.test(t) || /\bnc(?:at)?\s+(?:-\w+\s+)*-e\s+\/bin\/(?:ba)?sh\b/.test(t) || /socket\.socket\([\s\S]{0,300}?(?:os\.dup2|pty\.spawn)\([\s\S]{0,300}?\/bin\/(?:ba)?sh/.test(t) || /New-Object\s+System\.Net\.Sockets\.TCPClient\([\s\S]{0,600}?(?:\bIEX\b|Invoke-Expression)/i.test(t);
245
+ }
246
+ },
247
+ {
248
+ id: "CrushAV.Web.Webshell",
249
+ name: "Website backdoor (webshell)",
250
+ severity: "critical",
251
+ impact: "malicious",
252
+ description: "Server code that runs whatever a visitor sends it. Attackers plant these on hacked websites to keep control. Remove it and change your hosting passwords.",
253
+ test: (c) => {
254
+ if (!SERVER_SCRIPT_EXTS.has(c.ext)) return false;
255
+ const t = c.text;
256
+ return /\b(?:eval|assert|system|passthru|shell_exec|exec|popen|proc_open|create_function)\s*\(\s*@?(?:(?:base64_decode|gzinflate|str_rot13|gzuncompress|stripslashes)\s{0,4}\(\s{0,4}@?){0,4}\$_(?:POST|GET|REQUEST|COOKIE)\b/i.test(t) || /\beval\s*\(\s*gz(?:inflate|uncompress)\s*\(\s*base64_decode\s*\(/i.test(t) || /Runtime\.getRuntime\(\)\.exec\(\s*request\.getParameter/.test(t) || /Request\.(?:Form|QueryString|Item)\[[^\]]+\][\s\S]{0,120}?(?:Process\.Start|eval\s*\()/i.test(t);
257
+ }
258
+ },
259
+ {
260
+ id: "CrushAV.PUA.CoinMiner",
261
+ name: "Cryptocurrency miner",
262
+ severity: "high",
263
+ impact: "suspicious",
264
+ description: "Software that uses your computer to mine cryptocurrency for someone. If you did not install a miner yourself, it is stealing your electricity and slowing you down.",
265
+ test: (c) => {
266
+ if (NON_RUNNABLE_SOURCE_EXTS.has(c.ext)) return false;
267
+ const t = c.text;
268
+ return /stratum2?\+(?:tcp|ssl|tls):\/\//i.test(t) && /(?:xmrig|donate-level|cryptonight|randomx|nicehash|cpu-max-threads-hint)/i.test(t);
269
+ }
270
+ },
271
+ {
272
+ id: "CrushAV.Ransomware.ShadowCopyWipe",
273
+ name: "Deletes backups (ransomware behaviour)",
274
+ severity: "critical",
275
+ impact: "malicious",
276
+ description: "This program deletes Windows backup copies and disables recovery, which is what ransomware does right before encrypting files. Disconnect from the network and do not run it.",
277
+ test: (c) => {
278
+ if (!(EXECUTABLE_KINDS.has(c.kind) || isWindowsScriptContext(c))) return false;
279
+ const t = allText(c);
280
+ const hits = RANSOM_COMMANDS.filter((r) => r.test(t)).length;
281
+ if (hits >= 2) return true;
282
+ if (hits === 1) return { impact: "suspicious", severity: "high", description: "This program deletes Windows backup copies. Ransomware does this before encrypting files; a few admin tools do it on purpose. If you do not know what this is, do not run it." };
283
+ return false;
284
+ }
285
+ },
286
+ {
287
+ id: "CrushAV.Ransomware.Note",
288
+ name: "Ransom note",
289
+ severity: "critical",
290
+ impact: "malicious",
291
+ description: "This looks like a ransom note, which ransomware leaves behind after encrypting files. Disconnect this computer from the network, do not pay, and check nomoreransom.org for a free decryptor.",
292
+ test: (c) => c.size <= 64 * 1024 && c.kind === "text" && NOTE_EXTS.has(c.ext) && /(?:all\s+)?your\s+(?:important\s+|personal\s+)?(?:files|documents|data)\s+(?:have\s+been|are|were)\s+(?:encrypted|locked)/i.test(c.text) && /(?:bitcoin|\bBTC\b|monero|\.onion\b|tor\s+browser|decrypt(?:or|ion)\s+(?:key|tool|software|program))/i.test(c.text)
293
+ },
294
+ {
295
+ id: "CrushAV.HackTool.CredentialDumper",
296
+ name: "Password-stealing tool",
297
+ severity: "high",
298
+ impact: "malicious",
299
+ description: "A tool that pulls saved passwords out of Windows memory (Mimikatz or a copy of it). Attackers use it to spread through networks.",
300
+ test: (c) => (c.kind === "pe" || c.ext === "ps1") && /sekurlsa::(?:logonpasswords|wdigest|msv)|lsadump::(?:sam|dcsync|secrets)/i.test(allText(c))
301
+ },
302
+ {
303
+ id: "CrushAV.macOS.PasswordPhishStealer",
304
+ name: "Mac password-stealing script",
305
+ severity: "critical",
306
+ impact: "malicious",
307
+ description: "Shows a fake macOS password prompt and then reaches for your keychain, browser data or wallets. This is how Mac stealers like AMOS work. Do not enter your password; delete it.",
308
+ test: (c) => {
309
+ if (!(isScript(c) || c.kind === "macho")) return false;
310
+ const t = c.text;
311
+ return /display\s+dialog[\s\S]{0,400}?with\s+hidden\s+answer/i.test(t) && /(?:Library\/Keychains|login\.keychain|Cookies|Login Data|wallet|find-generic-password|dscl\s+\.\s+-?authonly|curl\s)/i.test(t);
312
+ }
313
+ },
314
+ {
315
+ id: "CrushAV.Web.ClickFix",
316
+ name: 'Fake "verify you are human" trick (ClickFix)',
317
+ severity: "critical",
318
+ impact: "malicious",
319
+ description: 'A web page that secretly copies a command to your clipboard and tells you to paste it into Run or Terminal to "verify" yourself. Pasting it installs malware. Never paste commands a website gives you.',
320
+ test: (c) => {
321
+ if (!(WEB_EXTS.has(c.ext) || c.ext === "js")) return false;
322
+ const t = c.text;
323
+ return /(?:navigator\.clipboard\.writeText|document\.execCommand\(\s*["']copy)/.test(t) && /(?:powershell|mshta|cmd(?:\.exe)?\s*\/c|curl\s[^\n]{0,200}\|\s*(?:ba|z)?sh|osascript)/i.test(t) && /(?:Win(?:dows)?\s*(?:key\s*)?\+\s*R|Windows\s+key|Ctrl\s*\+\s*V|Cmd\s*\+\s*Space|verify\s+you\s+are\s+(?:a\s+)?human|I(?:'|&#39;)?m\s+not\s+a\s+robot)/i.test(t);
324
+ }
325
+ },
326
+ {
327
+ id: "CrushAV.Web.HtmlSmuggling",
328
+ name: "Web page that builds a hidden download",
329
+ severity: "high",
330
+ impact: "suspicious",
331
+ description: "This HTML or SVG file carries a large hidden file inside itself and saves it to your computer when opened. Attackers use this to sneak malware past email filters.",
332
+ test: (c) => {
333
+ if (!WEB_EXTS.has(c.ext)) return false;
334
+ const t = c.text;
335
+ return /new\s+Blob\s*\(/.test(t) && /(?:\.download\s*=|msSaveOrOpenBlob|msSaveBlob)/.test(t) && /(?:atob\s*\(|base64)/.test(t) && /[A-Za-z0-9+/]{20000}/.test(t);
336
+ }
337
+ },
338
+ {
339
+ id: "CrushAV.Web.SvgRedirect",
340
+ name: "Image file that runs code",
341
+ severity: "medium",
342
+ impact: "suspicious",
343
+ description: 'This SVG "image" contains a script that redirects you or decodes hidden content. Phishing emails use SVG attachments like this to send you to fake login pages.',
344
+ test: (c) => c.ext === "svg" && /<script\b/i.test(c.text) && /(?:location(?:\.href)?\s*=|location\.replace\(|window\.open\(|atob\s*\()/.test(c.text)
345
+ }
346
+ ];
347
+ function buildContext(name, bytes, kind, size) {
348
+ let text = "";
349
+ const NodeBuffer = globalThis.Buffer;
350
+ if (NodeBuffer) {
351
+ text = NodeBuffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString("latin1");
352
+ } else {
353
+ for (let i = 0; i < bytes.length; i += 32768) {
354
+ text += String.fromCharCode.apply(null, bytes.subarray(i, i + 32768));
355
+ }
356
+ }
357
+ let wide = "";
358
+ if (kind === "pe" || kind === "lnk" || kind === "ole") {
359
+ const parts = [];
360
+ let run = "";
361
+ for (let i = 0; i + 1 < bytes.length; i += 2) {
362
+ const lo = bytes[i];
363
+ if (bytes[i + 1] === 0 && lo >= 32 && lo < 127) run += String.fromCharCode(lo);
364
+ else {
365
+ if (run.length >= 4) parts.push(run);
366
+ run = "";
367
+ }
368
+ }
369
+ if (run.length >= 4) parts.push(run);
370
+ wide = parts.join("\n");
371
+ }
372
+ return { name, ext: extOf(name), kind, size, bytes, text, wide };
373
+ }
374
+ function runRules(ctx, rules = RULES) {
375
+ const out = [];
376
+ for (const r of rules) {
377
+ let hit;
378
+ try {
379
+ hit = r.test(ctx);
380
+ } catch {
381
+ continue;
382
+ }
383
+ if (!hit) continue;
384
+ const base = { engine: "heuristic", id: r.id, name: r.name, severity: r.severity, impact: r.impact, description: r.description };
385
+ out.push(hit === true ? base : { ...base, ...hit });
386
+ }
387
+ return out;
388
+ }
389
+
390
+ // ../../packages/av/src/sha256.ts
391
+ var K = new Uint32Array([
392
+ 1116352408,
393
+ 1899447441,
394
+ 3049323471,
395
+ 3921009573,
396
+ 961987163,
397
+ 1508970993,
398
+ 2453635748,
399
+ 2870763221,
400
+ 3624381080,
401
+ 310598401,
402
+ 607225278,
403
+ 1426881987,
404
+ 1925078388,
405
+ 2162078206,
406
+ 2614888103,
407
+ 3248222580,
408
+ 3835390401,
409
+ 4022224774,
410
+ 264347078,
411
+ 604807628,
412
+ 770255983,
413
+ 1249150122,
414
+ 1555081692,
415
+ 1996064986,
416
+ 2554220882,
417
+ 2821834349,
418
+ 2952996808,
419
+ 3210313671,
420
+ 3336571891,
421
+ 3584528711,
422
+ 113926993,
423
+ 338241895,
424
+ 666307205,
425
+ 773529912,
426
+ 1294757372,
427
+ 1396182291,
428
+ 1695183700,
429
+ 1986661051,
430
+ 2177026350,
431
+ 2456956037,
432
+ 2730485921,
433
+ 2820302411,
434
+ 3259730800,
435
+ 3345764771,
436
+ 3516065817,
437
+ 3600352804,
438
+ 4094571909,
439
+ 275423344,
440
+ 430227734,
441
+ 506948616,
442
+ 659060556,
443
+ 883997877,
444
+ 958139571,
445
+ 1322822218,
446
+ 1537002063,
447
+ 1747873779,
448
+ 1955562222,
449
+ 2024104815,
450
+ 2227730452,
451
+ 2361852424,
452
+ 2428436474,
453
+ 2756734187,
454
+ 3204031479,
455
+ 3329325298
456
+ ]);
457
+ var Sha256 = class {
458
+ h = new Uint32Array([
459
+ 1779033703,
460
+ 3144134277,
461
+ 1013904242,
462
+ 2773480762,
463
+ 1359893119,
464
+ 2600822924,
465
+ 528734635,
466
+ 1541459225
467
+ ]);
468
+ block = new Uint8Array(64);
469
+ blockLen = 0;
470
+ total = 0;
471
+ w = new Uint32Array(64);
472
+ update(data) {
473
+ let i = 0;
474
+ this.total += data.length;
475
+ if (this.blockLen > 0) {
476
+ const take = Math.min(64 - this.blockLen, data.length);
477
+ this.block.set(data.subarray(0, take), this.blockLen);
478
+ this.blockLen += take;
479
+ i = take;
480
+ if (this.blockLen === 64) {
481
+ this.compress(this.block, 0);
482
+ this.blockLen = 0;
483
+ }
484
+ }
485
+ for (; i + 64 <= data.length; i += 64) this.compress(data, i);
486
+ if (i < data.length) {
487
+ this.block.set(data.subarray(i), 0);
488
+ this.blockLen = data.length - i;
489
+ }
490
+ return this;
491
+ }
492
+ hex() {
493
+ const bitLen = this.total * 8;
494
+ const pad = new Uint8Array((this.blockLen < 56 ? 56 : 120) - this.blockLen + 8);
495
+ pad[0] = 128;
496
+ const hi = Math.floor(bitLen / 4294967296);
497
+ const lo = bitLen >>> 0;
498
+ const n = pad.length;
499
+ pad[n - 8] = hi >>> 24;
500
+ pad[n - 7] = hi >>> 16;
501
+ pad[n - 6] = hi >>> 8;
502
+ pad[n - 5] = hi;
503
+ pad[n - 4] = lo >>> 24;
504
+ pad[n - 3] = lo >>> 16;
505
+ pad[n - 2] = lo >>> 8;
506
+ pad[n - 1] = lo;
507
+ this.total -= pad.length;
508
+ this.update(pad);
509
+ let out = "";
510
+ for (const v of this.h) out += v.toString(16).padStart(8, "0");
511
+ return out;
512
+ }
513
+ compress(d, o) {
514
+ const w = this.w;
515
+ for (let t = 0; t < 16; t++) {
516
+ const j = o + t * 4;
517
+ w[t] = d[j] << 24 | d[j + 1] << 16 | d[j + 2] << 8 | d[j + 3];
518
+ }
519
+ for (let t = 16; t < 64; t++) {
520
+ const a2 = w[t - 15], b2 = w[t - 2];
521
+ const s0 = (a2 >>> 7 | a2 << 25) ^ (a2 >>> 18 | a2 << 14) ^ a2 >>> 3;
522
+ const s1 = (b2 >>> 17 | b2 << 15) ^ (b2 >>> 19 | b2 << 13) ^ b2 >>> 10;
523
+ w[t] = w[t - 16] + s0 + w[t - 7] + s1 | 0;
524
+ }
525
+ let [a, b, c, dd, e, f, g, h] = this.h;
526
+ for (let t = 0; t < 64; t++) {
527
+ const S1 = (e >>> 6 | e << 26) ^ (e >>> 11 | e << 21) ^ (e >>> 25 | e << 7);
528
+ const ch = e & f ^ ~e & g;
529
+ const t1 = h + S1 + ch + K[t] + w[t] | 0;
530
+ const S0 = (a >>> 2 | a << 30) ^ (a >>> 13 | a << 19) ^ (a >>> 22 | a << 10);
531
+ const maj = a & b ^ a & c ^ b & c;
532
+ const t2 = S0 + maj | 0;
533
+ h = g;
534
+ g = f;
535
+ f = e;
536
+ e = dd + t1 | 0;
537
+ dd = c;
538
+ c = b;
539
+ b = a;
540
+ a = t1 + t2 | 0;
541
+ }
542
+ const H = this.h;
543
+ H[0] += a;
544
+ H[1] += b;
545
+ H[2] += c;
546
+ H[3] += dd;
547
+ H[4] += e;
548
+ H[5] += f;
549
+ H[6] += g;
550
+ H[7] += h;
551
+ }
552
+ };
553
+ async function sha256Hex(bytes) {
554
+ const subtle = globalThis.crypto?.subtle;
555
+ if (subtle) {
556
+ try {
557
+ const buf = await subtle.digest("SHA-256", bytes);
558
+ return Array.from(new Uint8Array(buf), (b) => b.toString(16).padStart(2, "0")).join("");
559
+ } catch {
560
+ }
561
+ }
562
+ return new Sha256().update(bytes).hex();
563
+ }
564
+ var SHA256_RE = /^[0-9a-f]{64}$/;
565
+
566
+ // ../../packages/av/src/engine.ts
567
+ var DEFAULT_HEURISTIC_BYTES = 16 * 1024 * 1024;
568
+ var SignatureSet = class {
569
+ map = /* @__PURE__ */ new Map();
570
+ constructor(entries = BUILTIN_SIGNATURES) {
571
+ for (const [h, n] of entries) this.add(h, n);
572
+ }
573
+ add(sha256, name) {
574
+ const h = sha256.trim().toLowerCase();
575
+ if (SHA256_RE.test(h)) this.map.set(h, name || "Malware");
576
+ }
577
+ /** Load `sha256 [name]` lines; `#` starts a comment. Returns how many were added. */
578
+ load(text) {
579
+ let n = 0;
580
+ for (const raw of text.split(/\r?\n/)) {
581
+ const line = raw.replace(/#.*/, "").trim();
582
+ if (!line) continue;
583
+ const [h, ...rest] = line.split(/[\s,;]+/);
584
+ if (SHA256_RE.test(h.toLowerCase())) {
585
+ this.add(h, rest.join(" "));
586
+ n++;
587
+ }
588
+ }
589
+ return n;
590
+ }
591
+ get(sha256) {
592
+ return this.map.get(sha256.toLowerCase());
593
+ }
594
+ /** Every `[sha256, name]` pair, so a set can be rebuilt in a worker thread. */
595
+ entries() {
596
+ return [...this.map];
597
+ }
598
+ get size() {
599
+ return this.map.size;
600
+ }
601
+ };
602
+ var BUILTIN_SIGNATURES = [
603
+ ["275a021bbfb6489e54d471899f7db9d1663fc695ec2fe2a2c4538aabf651fd0f", "EICAR-Test-File"]
604
+ ];
605
+ var IMPACT_RANK = { info: 0, suspicious: 1, malicious: 2 };
606
+ function verdictOf(detections) {
607
+ let worst = 0;
608
+ for (const d of detections) worst = Math.max(worst, IMPACT_RANK[d.impact]);
609
+ return worst === 2 ? "malicious" : worst === 1 ? "suspicious" : "clean";
610
+ }
611
+ async function scanBytes(input, opts = {}) {
612
+ const size = input.size ?? input.bytes.length;
613
+ const complete = input.bytes.length >= size;
614
+ const sha256 = input.sha256 ?? (complete ? await sha256Hex(input.bytes) : null);
615
+ const kind = sniffKind(input.bytes);
616
+ const detections = [];
617
+ const sigs = opts.signatures ?? DEFAULT_SIGNATURES;
618
+ const known = sha256 ? sigs.get(sha256) : void 0;
619
+ if (known) {
620
+ detections.push({
621
+ engine: "signature",
622
+ id: `CrushAV.Signature.${known.replace(/[^\w.-]+/g, "_")}`,
623
+ name: known,
624
+ severity: known.startsWith("EICAR") ? "low" : "critical",
625
+ impact: "malicious",
626
+ description: known.startsWith("EICAR") ? "The harmless standard antivirus test file. Seeing this means the scanner works." : `This exact file is known malware (${known}). Quarantine or delete it.`
627
+ });
628
+ }
629
+ const ctx = buildContext(input.name, input.bytes, kind, size);
630
+ for (const d of runRules(ctx, opts.rules ?? RULES)) {
631
+ if (known && d.id === "CrushAV.Test.EICAR") continue;
632
+ detections.push(d);
633
+ }
634
+ return {
635
+ path: input.name,
636
+ size,
637
+ sha256,
638
+ kind,
639
+ verdict: verdictOf(detections),
640
+ detections,
641
+ note: complete ? void 0 : `heuristics read the first ${Math.round(input.bytes.length / 1048576)} MB`
642
+ };
643
+ }
644
+ var DEFAULT_SIGNATURES = new SignatureSet();
645
+
646
+ // ../../packages/av/src/node/scan-file.ts
647
+ var CHUNK = 1 << 20;
648
+ var OPEN_FLAGS = import_node_fs2.constants.O_RDONLY | (import_node_fs2.constants.O_NOFOLLOW ?? 0) | (import_node_fs2.constants.O_NONBLOCK ?? 0);
649
+ var head = Buffer.allocUnsafe(64 * 1024);
650
+ var chunk = null;
651
+ async function scanFileSync(path, opts) {
652
+ let fd;
653
+ try {
654
+ fd = (0, import_node_fs2.openSync)(path, OPEN_FLAGS);
655
+ } catch (e) {
656
+ const code = e.code ?? "unreadable";
657
+ if (code === "ENOENT" || code === "ELOOP") return { kind: "none" };
658
+ return { kind: "result", result: errorResult(path, 0, code) };
659
+ }
660
+ let size = 0;
661
+ try {
662
+ const st = (0, import_node_fs2.fstatSync)(fd);
663
+ if (!st.isFile() || st.size === 0) return { kind: "none" };
664
+ size = st.size;
665
+ if (size > opts.maxFile) return { kind: "skip", path, reason: `larger than ${Math.round(opts.maxFile / 1048576)} MB` };
666
+ const want = Math.min(size, opts.cap);
667
+ if (head.length < want) head = Buffer.allocUnsafe(Math.max(want, head.length * 2 > opts.cap ? want : head.length * 2));
668
+ const hash = (0, import_node_crypto.createHash)("sha256");
669
+ let filled = 0;
670
+ while (filled < want) {
671
+ const n = (0, import_node_fs2.readSync)(fd, head, filled, want - filled, null);
672
+ if (n === 0) break;
673
+ filled += n;
674
+ }
675
+ hash.update(head.subarray(0, filled));
676
+ let total = filled;
677
+ if (filled === want && size > want) {
678
+ chunk ??= Buffer.allocUnsafe(CHUNK);
679
+ for (; ; ) {
680
+ const n = (0, import_node_fs2.readSync)(fd, chunk, 0, CHUNK, null);
681
+ if (n === 0) break;
682
+ hash.update(chunk.subarray(0, n));
683
+ total += n;
684
+ }
685
+ }
686
+ const result = await scanBytes(
687
+ { name: path, bytes: head.subarray(0, filled), size: total, sha256: hash.digest("hex") },
688
+ { signatures: opts.signatures }
689
+ );
690
+ return { kind: "result", result };
691
+ } catch (e) {
692
+ const code = e.code ?? e.message;
693
+ return { kind: "result", result: errorResult(path, size, code) };
694
+ } finally {
695
+ try {
696
+ (0, import_node_fs2.closeSync)(fd);
697
+ } catch {
698
+ }
699
+ }
700
+ }
701
+ function errorResult(path, size, note) {
702
+ return { path, size, sha256: null, kind: "unknown", verdict: "error", detections: [], note };
703
+ }
704
+
705
+ // ../../packages/av/src/node/scan-worker.ts
706
+ var import_node_worker_threads = require("worker_threads");
707
+ function runScanWorker() {
708
+ const port = import_node_worker_threads.parentPort;
709
+ if (!port) throw new Error("runScanWorker must run inside a worker thread");
710
+ const init = import_node_worker_threads.workerData;
711
+ const opts = {
712
+ cap: init.cap,
713
+ maxFile: init.maxFile,
714
+ signatures: init.signatures ? new SignatureSet(init.signatures) : void 0
715
+ };
716
+ let chain = Promise.resolve();
717
+ port.on("message", (msg) => {
718
+ chain = chain.then(async () => {
719
+ const out = [];
720
+ for (const f of msg.files) out.push(await scanFileSync(f, opts));
721
+ port.postMessage({ id: msg.id, out });
722
+ });
723
+ });
724
+ }
725
+
726
+ // ../../packages/av/src/node/clamav.ts
727
+ var import_node_child_process = require("child_process");
728
+ var import_node_fs3 = require("fs");
729
+ var import_promises2 = require("fs/promises");
730
+ var import_node_os2 = require("os");
731
+ var import_node_path3 = require("path");
732
+
733
+ // ../../packages/av/src/node/quarantine.ts
734
+ var import_node_crypto2 = require("crypto");
735
+ var import_node_fs4 = require("fs");
736
+ var import_promises3 = require("fs/promises");
737
+ var import_node_os3 = require("os");
738
+ var import_node_path4 = require("path");
739
+
740
+ // ../../packages/av/src/node/scan.ts
741
+ var import_node_crypto3 = require("crypto");
742
+ var import_node_fs5 = require("fs");
743
+ var import_promises4 = require("fs/promises");
744
+ var import_node_os4 = require("os");
745
+ var import_node_worker_threads2 = require("worker_threads");
746
+
747
+ // ../../packages/av/src/node/watch.ts
748
+ var import_node_fs6 = require("fs");
749
+ var import_promises5 = require("fs/promises");
750
+ var import_node_path5 = require("path");
751
+
752
+ // src/av-worker.ts
753
+ runScanWorker();
754
+ //# sourceMappingURL=av-worker.js.map