dsh-plugin-explorer 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +48 -15
- package/client.js +397 -21
- package/index.js +64 -9
- package/lib/gate/LICENSE +21 -0
- package/lib/gate/osv.js +116 -0
- package/lib/gate/rules.js +628 -0
- package/lib/gate/scan.js +609 -0
- package/lib/gate/targz.js +146 -0
- package/lib/gate-scan.js +144 -0
- package/lib/installed.js +86 -0
- package/lib/npm.js +109 -0
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +3 -3
- package/scripts/verify.mjs +64 -6
|
@@ -0,0 +1,628 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-plugin-gate — signature rules for the installation safety gate.
|
|
3
|
+
*
|
|
4
|
+
* Pure module: no DSH, Cordis or Node imports, fully unit-testable in
|
|
5
|
+
* isolation. Rules are regex signatures over file text. Each rule reports a
|
|
6
|
+
* line-level hit; count-threshold rules only fire when a line accumulates at
|
|
7
|
+
* least minCount matches (obfuscation heuristics).
|
|
8
|
+
*
|
|
9
|
+
* Severity model:
|
|
10
|
+
* high — patterns that are almost always malicious in a plugin package
|
|
11
|
+
* (exec on install, encoded PowerShell, eval/Function, webhooks,
|
|
12
|
+
* cloud-metadata probes, private keys, credential reads).
|
|
13
|
+
* medium — risky-but-common patterns that need context (fetch calls, socket
|
|
14
|
+
* APIs, writes to home/system-ish paths, chmod 777, base64 blobs).
|
|
15
|
+
* low — informational (any http URL, any env access, IPv4 literals).
|
|
16
|
+
*
|
|
17
|
+
* The final verdict is computed by lib/scan.js aggregate(): any high => BLOCK,
|
|
18
|
+
* any medium => WARN, otherwise PASS. Rules only read text; the gate never
|
|
19
|
+
* executes scanned content.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
export const CATEGORY = {
|
|
23
|
+
SCRIPTS: "scripts",
|
|
24
|
+
OBFUSCATION: "obfuscation",
|
|
25
|
+
PERMISSIONS: "permissions",
|
|
26
|
+
NETWORK: "network",
|
|
27
|
+
SECRETS: "secrets",
|
|
28
|
+
BINARY: "binary",
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const SEVERITY_WEIGHT = { high: 3, medium: 2, low: 1 };
|
|
32
|
+
|
|
33
|
+
/** @type {Rule[]} */
|
|
34
|
+
export const RULES = [
|
|
35
|
+
{
|
|
36
|
+
id: "exec_sync",
|
|
37
|
+
category: CATEGORY.SCRIPTS,
|
|
38
|
+
severity: "high",
|
|
39
|
+
name: "Synchronous process execution",
|
|
40
|
+
re: /\b(execSync|execFileSync|spawnSync|fork)\s*\(/g,
|
|
41
|
+
hint: "Synchronous child-process execution runs inside the host at plugin runtime.",
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
id: "exec_api",
|
|
45
|
+
category: CATEGORY.SCRIPTS,
|
|
46
|
+
severity: "high",
|
|
47
|
+
name: "Process execution",
|
|
48
|
+
re: /\b(exec|execFile)\s*\(/g,
|
|
49
|
+
hint: "child_process.exec/execFile spawns OS commands; inspect the command string.",
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
id: "spawn_call",
|
|
53
|
+
category: CATEGORY.SCRIPTS,
|
|
54
|
+
severity: "medium",
|
|
55
|
+
name: "Process spawn",
|
|
56
|
+
re: /\bspawn\s*\(/g,
|
|
57
|
+
hint: "child_process.spawn is common in legit tools; verify arguments are not shell-interpolated.",
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
id: "shell_true",
|
|
61
|
+
category: CATEGORY.SCRIPTS,
|
|
62
|
+
severity: "high",
|
|
63
|
+
name: "Shell mode enabled",
|
|
64
|
+
re: /\bshell\s*:\s*true\b/g,
|
|
65
|
+
hint: "spawn/exec with shell:true runs strings through a shell; command injection risk.",
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
id: "download_cmd",
|
|
69
|
+
category: CATEGORY.SCRIPTS,
|
|
70
|
+
severity: "high",
|
|
71
|
+
name: "Download-and-run primitive",
|
|
72
|
+
re: /\b(curl|wget|iwr|Invoke-WebRequest|DownloadString|bitsadmin|certutil|scrobj|mshta|regsvr32|rundll32)\b/g,
|
|
73
|
+
hint: "Common download/execute utilities; combined with install scripts this is a classic payload drop.",
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
id: "pipe_to_shell",
|
|
77
|
+
category: CATEGORY.SCRIPTS,
|
|
78
|
+
severity: "high",
|
|
79
|
+
name: "Pipe to shell",
|
|
80
|
+
re: /\|\s*(ba|z)?sh\b|\|\s*powershell\b|\|\s*(cmd|pwsh)\b/g,
|
|
81
|
+
hint: "curl|sh style remote-code execution is a textbook malware pattern.",
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
id: "ps_encoded",
|
|
85
|
+
category: CATEGORY.SCRIPTS,
|
|
86
|
+
severity: "high",
|
|
87
|
+
name: "Encoded PowerShell",
|
|
88
|
+
re: /powershell[^\r\n]{0,120}-enc(odedcommand)?\b/ig,
|
|
89
|
+
hint: "Encoded PowerShell commands hide the real payload from inspection.",
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
id: "ps_iex",
|
|
93
|
+
category: CATEGORY.SCRIPTS,
|
|
94
|
+
severity: "high",
|
|
95
|
+
name: "PowerShell invoke-expression",
|
|
96
|
+
re: /\b(Invoke-Expression|\bIEX\b|iex\s*\()/ig,
|
|
97
|
+
hint: "IEX evaluates remote or constructed strings.",
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
id: "cmd_launch",
|
|
101
|
+
category: CATEGORY.SCRIPTS,
|
|
102
|
+
severity: "medium",
|
|
103
|
+
name: "Windows script host / cmd launch",
|
|
104
|
+
re: /\b(cmd(\.exe)?\s*\/c|start\s+\/B|wscript|cscript)\b/ig,
|
|
105
|
+
hint: "Launching cmd/WSH from a plugin is unusual.",
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
id: "dynamic_require",
|
|
109
|
+
category: CATEGORY.SCRIPTS,
|
|
110
|
+
severity: "medium",
|
|
111
|
+
name: "Dynamic require/import",
|
|
112
|
+
re: /\brequire\s*\(\s*[\x60"'][^\x60"']*["'\x60]\s*\+|\bimport\s*\(\s*[^\x60"'\s)]/g,
|
|
113
|
+
hint: "Non-literal module loading can smuggle in payloads from computed paths.",
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
id: "env_in_cmd",
|
|
117
|
+
category: CATEGORY.SCRIPTS,
|
|
118
|
+
severity: "medium",
|
|
119
|
+
name: "Environment in command string",
|
|
120
|
+
re: /\b(execSync|exec|spawn|spawnSync)\s*\([^)\n]{0,200}process\.env/g,
|
|
121
|
+
hint: "Environment variables flowing into command strings can be exfiltrated or abused.",
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
id: "rm_recursive",
|
|
125
|
+
category: CATEGORY.SCRIPTS,
|
|
126
|
+
severity: "high",
|
|
127
|
+
name: "Recursive delete",
|
|
128
|
+
re: /\brm\s+-rf\b|\brmSync\s*\(/g,
|
|
129
|
+
hint: "Destructive deletion; verify the target path before trusting it.",
|
|
130
|
+
},
|
|
131
|
+
|
|
132
|
+
{
|
|
133
|
+
id: "eval_call",
|
|
134
|
+
category: CATEGORY.OBFUSCATION,
|
|
135
|
+
severity: "high",
|
|
136
|
+
name: "eval()",
|
|
137
|
+
re: /\beval\s*\(/g,
|
|
138
|
+
hint: "eval of constructed strings is the classic obfuscation/payload entry.",
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
id: "new_function",
|
|
142
|
+
category: CATEGORY.OBFUSCATION,
|
|
143
|
+
severity: "high",
|
|
144
|
+
name: "new Function()",
|
|
145
|
+
re: /\bnew\s+Function\s*\(/g,
|
|
146
|
+
hint: "Function constructor compiles strings at runtime.",
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
id: "function_ctor",
|
|
150
|
+
category: CATEGORY.OBFUSCATION,
|
|
151
|
+
severity: "high",
|
|
152
|
+
name: "Function constructor",
|
|
153
|
+
re: /\bFunction\s*\(\s*["'\x60]/g,
|
|
154
|
+
hint: "Function('...') is string-compiled code.",
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
id: "vm_run",
|
|
158
|
+
category: CATEGORY.OBFUSCATION,
|
|
159
|
+
severity: "high",
|
|
160
|
+
name: "vm sandbox execution",
|
|
161
|
+
re: /\bvm\.runIn(NewContext|ThisContext|Context)\s*\(/g,
|
|
162
|
+
hint: "vm.runIn* executes strings; often used to hide payloads from static analysis.",
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
id: "fromcharcode",
|
|
166
|
+
category: CATEGORY.OBFUSCATION,
|
|
167
|
+
severity: "medium",
|
|
168
|
+
name: "String.fromCharCode",
|
|
169
|
+
re: /\bString\.fromCharCode\s*\(/g,
|
|
170
|
+
hint: "Common in packed payloads that decode strings at runtime.",
|
|
171
|
+
},
|
|
172
|
+
{
|
|
173
|
+
id: "atob_call",
|
|
174
|
+
category: CATEGORY.OBFUSCATION,
|
|
175
|
+
severity: "medium",
|
|
176
|
+
name: "base64 decode",
|
|
177
|
+
re: /\batob\s*\(/g,
|
|
178
|
+
hint: "Decoding base64 at runtime; check what is being decoded.",
|
|
179
|
+
},
|
|
180
|
+
{
|
|
181
|
+
id: "hex_escapes",
|
|
182
|
+
category: CATEGORY.OBFUSCATION,
|
|
183
|
+
severity: "high",
|
|
184
|
+
name: "Hex-escape flood",
|
|
185
|
+
re: /\\x[0-9a-fA-F]{2}/g,
|
|
186
|
+
minCount: 6,
|
|
187
|
+
hint: "Lines packed with \\xNN escapes are typically encoded payloads.",
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
id: "uni_escapes",
|
|
191
|
+
category: CATEGORY.OBFUSCATION,
|
|
192
|
+
severity: "medium",
|
|
193
|
+
name: "Unicode-escape flood",
|
|
194
|
+
re: /\\u[0-9a-fA-F]{4}/g,
|
|
195
|
+
minCount: 10,
|
|
196
|
+
hint: "Heavy \\uXXXX encoding hides string content from review.",
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
id: "base64_blob",
|
|
200
|
+
category: CATEGORY.OBFUSCATION,
|
|
201
|
+
severity: "medium",
|
|
202
|
+
name: "Large base64 blob",
|
|
203
|
+
re: /[A-Za-z0-9+/]{240,}={0,2}/g,
|
|
204
|
+
hint: "A 240+ char base64 literal may embed a payload; decode and review.",
|
|
205
|
+
},
|
|
206
|
+
{
|
|
207
|
+
id: "char_array",
|
|
208
|
+
category: CATEGORY.OBFUSCATION,
|
|
209
|
+
severity: "medium",
|
|
210
|
+
name: "Character-array packing",
|
|
211
|
+
re: /\b(var|let|const)\s+\w+\s*=\s*\[[^;\n]{80,}\]\s*\.(join|map|reverse|fromCharCode)\s*\(/g,
|
|
212
|
+
hint: "Array-of-chars packing is a common obfuscation primitive.",
|
|
213
|
+
},
|
|
214
|
+
|
|
215
|
+
{
|
|
216
|
+
id: "env_secret_key",
|
|
217
|
+
category: CATEGORY.PERMISSIONS,
|
|
218
|
+
severity: "high",
|
|
219
|
+
name: "Credential environment read",
|
|
220
|
+
re: /\bprocess\.env\.(OPENAI|ANTHROPIC|DEEPSEEK|GEMINI|GOOGLE|OPENROUTER|GITHUB|GITLAB|NPM|AWS|AZURE|STRIPE|SLACK|TELEGRAM|DISCORD|HUGGING|REPLICATE|TOGETHER|FAL|MISTRAL|COHERE|GROQ)[_A-Z0-9]*\b/gi,
|
|
221
|
+
hint: "Reading known API-credential environment variables; ensure they are not sent anywhere.",
|
|
222
|
+
},
|
|
223
|
+
{
|
|
224
|
+
id: "env_any",
|
|
225
|
+
category: CATEGORY.PERMISSIONS,
|
|
226
|
+
severity: "low",
|
|
227
|
+
name: "Environment access",
|
|
228
|
+
re: /\bprocess\.env\b/g,
|
|
229
|
+
hint: "Any environment access; inspect which keys are read.",
|
|
230
|
+
},
|
|
231
|
+
{
|
|
232
|
+
id: "read_secrets_paths",
|
|
233
|
+
category: CATEGORY.PERMISSIONS,
|
|
234
|
+
severity: "medium",
|
|
235
|
+
name: "Credential-file path",
|
|
236
|
+
re: /(\.ssh|id_rsa|id_ed25519|\.aws[\\/]|credentials\.json|\.npmrc|_authToken|netrc)/g,
|
|
237
|
+
hint: "Reading ssh keys, aws credentials, npm tokens or netrc is a credential-theft pattern.",
|
|
238
|
+
},
|
|
239
|
+
{
|
|
240
|
+
id: "homedir",
|
|
241
|
+
category: CATEGORY.PERMISSIONS,
|
|
242
|
+
severity: "medium",
|
|
243
|
+
name: "Home/profile path access",
|
|
244
|
+
re: /\b(os\.homedir|USERPROFILE|APPDATA|LOCALAPPDATA|HOME)\b/g,
|
|
245
|
+
hint: "Accessing user-profile locations; plugins usually only need the session workspace.",
|
|
246
|
+
},
|
|
247
|
+
{
|
|
248
|
+
id: "write_system",
|
|
249
|
+
category: CATEGORY.PERMISSIONS,
|
|
250
|
+
severity: "high",
|
|
251
|
+
name: "Write to system paths",
|
|
252
|
+
re: /(writeFile(?:Sync)?|appendFile(?:Sync)?|createWriteStream|mkdir(?:Sync)?|unlink(?:Sync)?|rm(?:Sync)?|rename(?:Sync)?|copyFile(?:Sync)?)\s*\([^)\n]{0,160}(etc[\\/]|usr[\\/]|Windows[\\/]|System32|Program\s*Files|boot[\\/]|dev[\\/]|proc[\\/])/gi,
|
|
253
|
+
hint: "Writes outside the package/workspace into system directories are very suspicious.",
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
id: "write_home",
|
|
257
|
+
category: CATEGORY.PERMISSIONS,
|
|
258
|
+
severity: "medium",
|
|
259
|
+
name: "Write to home paths",
|
|
260
|
+
re: /(writeFile(?:Sync)?|appendFile(?:Sync)?|createWriteStream|mkdir(?:Sync)?)\s*\([^)\n]{0,160}(~[\\/]|\$HOME|os\.homedir|os\.userInfo|%APPDATA%|%USERPROFILE%|%LOCALAPPDATA%)/gi,
|
|
261
|
+
hint: "Persisting to home/profile dirs at install time is a persistence pattern.",
|
|
262
|
+
},
|
|
263
|
+
{
|
|
264
|
+
id: "dotfile_write",
|
|
265
|
+
category: CATEGORY.PERMISSIONS,
|
|
266
|
+
severity: "medium",
|
|
267
|
+
name: "Shell/config dotfile write",
|
|
268
|
+
re: /(writeFile(?:Sync)?|appendFile(?:Sync)?|createWriteStream)[^\n]{0,120}(\.bashrc|\.zshrc|\.profile|\.npmrc|autoexec)/gi,
|
|
269
|
+
hint: "Rewriting shell rc or npmrc grants persistence or token theft.",
|
|
270
|
+
},
|
|
271
|
+
{
|
|
272
|
+
id: "chmod_suid",
|
|
273
|
+
category: CATEGORY.PERMISSIONS,
|
|
274
|
+
severity: "medium",
|
|
275
|
+
name: "Permissive chmod",
|
|
276
|
+
re: /chmod[^\n]*(777|4755|666|a\+[rwx])/g,
|
|
277
|
+
hint: "World-writable or setuid permissions are dangerous.",
|
|
278
|
+
},
|
|
279
|
+
{
|
|
280
|
+
id: "sandbox_escalation",
|
|
281
|
+
category: CATEGORY.PERMISSIONS,
|
|
282
|
+
severity: "medium",
|
|
283
|
+
name: "Sandbox escalation request",
|
|
284
|
+
re: /\b(danger-full-access|sandbox_permissions|workspace-write)\b/g,
|
|
285
|
+
hint: "Code that requests wider sandbox permissions; review the justification strings.",
|
|
286
|
+
},
|
|
287
|
+
{
|
|
288
|
+
id: "path_traversal",
|
|
289
|
+
category: CATEGORY.PERMISSIONS,
|
|
290
|
+
severity: "medium",
|
|
291
|
+
name: "Path traversal",
|
|
292
|
+
re: /["'\x60][^"'\x60]*(\.\.\/|\.\.\\){2,}/g,
|
|
293
|
+
hint: "Multiple ../ segments escape the package root.",
|
|
294
|
+
},
|
|
295
|
+
|
|
296
|
+
{
|
|
297
|
+
id: "http_url",
|
|
298
|
+
category: CATEGORY.NETWORK,
|
|
299
|
+
severity: "low",
|
|
300
|
+
name: "HTTP(S) URL",
|
|
301
|
+
re: /\bhttps?:\/\//g,
|
|
302
|
+
hint: "External URL literal; hosts are collected and checked against the allowlist.",
|
|
303
|
+
},
|
|
304
|
+
{
|
|
305
|
+
id: "ws_url",
|
|
306
|
+
category: CATEGORY.NETWORK,
|
|
307
|
+
severity: "medium",
|
|
308
|
+
name: "WebSocket URL",
|
|
309
|
+
re: /\bwss?:\/\//g,
|
|
310
|
+
hint: "WebSocket endpoints can stream data out in real time.",
|
|
311
|
+
},
|
|
312
|
+
{
|
|
313
|
+
id: "fetch_call",
|
|
314
|
+
category: CATEGORY.NETWORK,
|
|
315
|
+
severity: "medium",
|
|
316
|
+
name: "fetch() call",
|
|
317
|
+
re: /\bfetch\s*\(/g,
|
|
318
|
+
hint: "Outbound HTTP from a plugin; check destinations, especially combined with file reads.",
|
|
319
|
+
},
|
|
320
|
+
{
|
|
321
|
+
id: "http_client",
|
|
322
|
+
category: CATEGORY.NETWORK,
|
|
323
|
+
severity: "medium",
|
|
324
|
+
name: "HTTP client library",
|
|
325
|
+
re: /\b(axios|node-fetch|got|superagent|undici|request)\s*[.(]/g,
|
|
326
|
+
hint: "HTTP client usage; verify every destination.",
|
|
327
|
+
},
|
|
328
|
+
{
|
|
329
|
+
id: "socket_api",
|
|
330
|
+
category: CATEGORY.NETWORK,
|
|
331
|
+
severity: "medium",
|
|
332
|
+
name: "Raw socket / websocket API",
|
|
333
|
+
re: /\b(net\.(connect|createConnection|createServer)|dgram|tls\.connect|http2\.|WebSocket|io\s*\(|socket\.io)\b/g,
|
|
334
|
+
hint: "Raw TCP/UDP/TLS or websocket listeners can be C2 (command-and-control) channels.",
|
|
335
|
+
},
|
|
336
|
+
{
|
|
337
|
+
id: "dns_call",
|
|
338
|
+
category: CATEGORY.NETWORK,
|
|
339
|
+
severity: "medium",
|
|
340
|
+
name: "DNS lookup",
|
|
341
|
+
re: /\b(dns\.(lookup|resolve|promises)|nslookup\s)/g,
|
|
342
|
+
hint: "Programmatic DNS queries are used for covert channel/exfil staging.",
|
|
343
|
+
},
|
|
344
|
+
{
|
|
345
|
+
id: "cloud_metadata",
|
|
346
|
+
category: CATEGORY.NETWORK,
|
|
347
|
+
severity: "high",
|
|
348
|
+
name: "Cloud metadata endpoint",
|
|
349
|
+
re: /\b(169\.254\.169\.254|metadata\.google\.internal|metadata\.compute\.internal|100\.100\.100\.200)\b/g,
|
|
350
|
+
hint: "Cloud metadata endpoints can leak IAM credentials; never legitimate in a plugin.",
|
|
351
|
+
},
|
|
352
|
+
{
|
|
353
|
+
id: "webhook",
|
|
354
|
+
category: CATEGORY.NETWORK,
|
|
355
|
+
severity: "high",
|
|
356
|
+
name: "Messaging webhook",
|
|
357
|
+
re: /\b(discord(app)?\.com\/api\/webhooks|api\.telegram\.org\/bot|hooks\.slack\.com\/services)/g,
|
|
358
|
+
hint: "Hardcoded Discord/Telegram/Slack webhooks are classic data-exfiltration sinks.",
|
|
359
|
+
},
|
|
360
|
+
{
|
|
361
|
+
id: "onion",
|
|
362
|
+
category: CATEGORY.NETWORK,
|
|
363
|
+
severity: "high",
|
|
364
|
+
name: "Tor hidden service",
|
|
365
|
+
re: /\.onion\b/g,
|
|
366
|
+
hint: ".onion destinations are anonymized exfiltration endpoints.",
|
|
367
|
+
},
|
|
368
|
+
{
|
|
369
|
+
id: "suspicious_tld",
|
|
370
|
+
category: CATEGORY.NETWORK,
|
|
371
|
+
severity: "low",
|
|
372
|
+
name: "Suspicious TLD",
|
|
373
|
+
re: /\.(top|xyz|tk|ml|ga|cf|gq|pw|work|click|stream|icu|vip)\b/ig,
|
|
374
|
+
hint: "Cheap-registration TLDs are over-represented in malware C2.",
|
|
375
|
+
},
|
|
376
|
+
{
|
|
377
|
+
id: "ipv4",
|
|
378
|
+
category: CATEGORY.NETWORK,
|
|
379
|
+
severity: "low",
|
|
380
|
+
name: "IPv4 literal",
|
|
381
|
+
re: /\b(?:(?:25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|1?\d?\d)\b/g,
|
|
382
|
+
hint: "Hardcoded IPs bypass DNS and can point straight at a C2 server.",
|
|
383
|
+
},
|
|
384
|
+
|
|
385
|
+
{
|
|
386
|
+
id: "sk_key",
|
|
387
|
+
category: CATEGORY.SECRETS,
|
|
388
|
+
severity: "high",
|
|
389
|
+
name: "sk- API key",
|
|
390
|
+
re: /\bsk-[A-Za-z0-9]{16,}/g,
|
|
391
|
+
hint: "Hardcoded OpenAI-style secret key in the package.",
|
|
392
|
+
},
|
|
393
|
+
{
|
|
394
|
+
id: "gh_token",
|
|
395
|
+
category: CATEGORY.SECRETS,
|
|
396
|
+
severity: "high",
|
|
397
|
+
name: "GitHub token",
|
|
398
|
+
re: /\bghp_[A-Za-z0-9]{20,}/g,
|
|
399
|
+
hint: "Hardcoded GitHub personal access token.",
|
|
400
|
+
},
|
|
401
|
+
{
|
|
402
|
+
id: "aws_key",
|
|
403
|
+
category: CATEGORY.SECRETS,
|
|
404
|
+
severity: "high",
|
|
405
|
+
name: "AWS access key",
|
|
406
|
+
re: /\bAKIA[0-9A-Z]{16}\b/g,
|
|
407
|
+
hint: "Hardcoded AWS access key id.",
|
|
408
|
+
},
|
|
409
|
+
{
|
|
410
|
+
id: "priv_key",
|
|
411
|
+
category: CATEGORY.SECRETS,
|
|
412
|
+
severity: "high",
|
|
413
|
+
name: "Private key block",
|
|
414
|
+
re: /-----BEGIN [A-Z ]*PRIVATE KEY-----/g,
|
|
415
|
+
hint: "Embedded private key material.",
|
|
416
|
+
},
|
|
417
|
+
{
|
|
418
|
+
id: "bearer",
|
|
419
|
+
category: CATEGORY.SECRETS,
|
|
420
|
+
severity: "medium",
|
|
421
|
+
name: "Bearer token",
|
|
422
|
+
re: /\bBearer\s+[A-Za-z0-9._-]{20,}/g,
|
|
423
|
+
hint: "Hardcoded bearer token.",
|
|
424
|
+
},
|
|
425
|
+
{
|
|
426
|
+
id: "pwd_assign",
|
|
427
|
+
category: CATEGORY.SECRETS,
|
|
428
|
+
severity: "medium",
|
|
429
|
+
name: "Inline password/secret",
|
|
430
|
+
re: /(password|passwd|api[_-]?key|secret)\s*[:=]\s*["'\x60][^"'\x60]{8,}["'\x60]/gi,
|
|
431
|
+
hint: "Inline credential literal; verify it is a placeholder, not a live secret.",
|
|
432
|
+
},
|
|
433
|
+
{
|
|
434
|
+
id: "reverse_shell",
|
|
435
|
+
category: CATEGORY.NETWORK,
|
|
436
|
+
severity: "high",
|
|
437
|
+
name: "Reverse shell primitive",
|
|
438
|
+
re: /(bash|sh|zsh)\s+-i[^;\n]*\/dev\/tcp\/|(\bnc\b|\bncat\b|\bsocat\b)\s+[^;]*(exec|-e\s+\/bin\/|-e\s+cmd)/gi,
|
|
439
|
+
hint: "Reverse shell pattern - the plugin would open an outbound interactive shell.",
|
|
440
|
+
},
|
|
441
|
+
{
|
|
442
|
+
id: "symlink_escape",
|
|
443
|
+
category: CATEGORY.PERMISSIONS,
|
|
444
|
+
severity: "high",
|
|
445
|
+
name: "Symlink outside workspace",
|
|
446
|
+
re: /(fs\.symlink(Sync)?\s*\(\s*["'"][^"']+["'"]\s*,\s*["'"][^"']+["'"]|\bln\s+-s\s+[^|&;\n]*\/)/g,
|
|
447
|
+
hint: "Creates a symlink - if it points outside the workspace, recursive deletes on the link can wipe real data.",
|
|
448
|
+
},
|
|
449
|
+
{
|
|
450
|
+
id: "rc_persistence",
|
|
451
|
+
category: CATEGORY.PERMISSIONS,
|
|
452
|
+
severity: "high",
|
|
453
|
+
name: "Shell rc persistence",
|
|
454
|
+
re: /(\.bashrc|\.zshrc|\.profile|\.bash_profile|\.zprofile)(\s*[<>]|["']\s*\+?=|\bappend\b|\bwriteFile\b)|([<>]{1,2}\s*~?\/?\.(bashrc|zshrc|profile|bash_profile|zprofile))/gi,
|
|
455
|
+
hint: "Writing to shell rc files persists code across sessions.",
|
|
456
|
+
},
|
|
457
|
+
{
|
|
458
|
+
id: "cron_persistence",
|
|
459
|
+
category: CATEGORY.PERMISSIONS,
|
|
460
|
+
severity: "high",
|
|
461
|
+
name: "Scheduled-task persistence",
|
|
462
|
+
re: /(\bcrontab\b|\/etc\/cron|\bschtasks\b|\bsystemctl\s+enable|\/etc\/systemd\/system|\/etc\/rc\.local)/gi,
|
|
463
|
+
hint: "Installing scheduled tasks / system services persists execution.",
|
|
464
|
+
},
|
|
465
|
+
{
|
|
466
|
+
id: "exfil_post",
|
|
467
|
+
category: CATEGORY.NETWORK,
|
|
468
|
+
severity: "high",
|
|
469
|
+
name: "Credential/file exfiltration POST",
|
|
470
|
+
re: /(\.(post|put|send|fetch)|(?<![A-Za-z0-9_])fetch)\s*\((?=[^)]*https?:\/\/)(?=[^)]*(process\.env|readFile|readFileSync|base64|btoa))/gi,
|
|
471
|
+
hint: "Sends environment/file-derived data to an external host - the classic steal shape.",
|
|
472
|
+
},
|
|
473
|
+
{
|
|
474
|
+
id: "encoded_pipe_sh",
|
|
475
|
+
category: CATEGORY.OBFUSCATION,
|
|
476
|
+
severity: "high",
|
|
477
|
+
name: "Encoded command piped to shell",
|
|
478
|
+
re: /(base64\s+-(d|decode)|echo\s+["'"][A-Za-z0-9+\/=]{20,}["'"])[^|&;\n]*\|[^|&;\n]*(sh|bash|powershell|cmd)/gi,
|
|
479
|
+
hint: "Base64-decoded payload executed by a shell.",
|
|
480
|
+
},
|
|
481
|
+
{
|
|
482
|
+
id: "node_eval",
|
|
483
|
+
category: CATEGORY.SCRIPTS,
|
|
484
|
+
severity: "medium",
|
|
485
|
+
name: "Node inline eval",
|
|
486
|
+
re: /\bnode\s+(-[ec]|--eval|--print)\s+["'"][^"']{8,}["'"]/g,
|
|
487
|
+
hint: "Node evaluating inline code - verify the code string.",
|
|
488
|
+
},
|
|
489
|
+
{
|
|
490
|
+
id: "wget_exec",
|
|
491
|
+
category: CATEGORY.SCRIPTS,
|
|
492
|
+
severity: "high",
|
|
493
|
+
name: "wget download-and-execute",
|
|
494
|
+
re: /\bwget\b[^|&;\n]*-(O-|-qO-|q-)\s+[^\s]+[^|&;\n]*\|[^|&;\n]*(sh|bash|zsh)/gi,
|
|
495
|
+
hint: "wget piping a download straight into a shell.",
|
|
496
|
+
},
|
|
497
|
+
{
|
|
498
|
+
id: "ps_download_exec",
|
|
499
|
+
category: CATEGORY.SCRIPTS,
|
|
500
|
+
severity: "high",
|
|
501
|
+
name: "PowerShell download-and-execute",
|
|
502
|
+
re: /(IEX\s*\(\s*\(?\s*New-Object\s+Net\.WebClient|(iwr|Invoke-WebRequest)[^|&;\n]*\|[^|&;\n]*iex\b)/gi,
|
|
503
|
+
hint: "PowerShell downloading and executing remote content.",
|
|
504
|
+
},
|
|
505
|
+
{
|
|
506
|
+
id: "ip_obfuscation",
|
|
507
|
+
category: CATEGORY.NETWORK,
|
|
508
|
+
severity: "low",
|
|
509
|
+
name: "Obfuscated IP literal",
|
|
510
|
+
re: /\b0x[0-9a-fA-F]{6,8}\b|\b\d{9,12}\b/g,
|
|
511
|
+
hint: "Hex/alternate-notation IP literals are often used to evade allowlists.",
|
|
512
|
+
},
|
|
513
|
+
{
|
|
514
|
+
id: "startup_registry",
|
|
515
|
+
category: CATEGORY.PERMISSIONS,
|
|
516
|
+
severity: "high",
|
|
517
|
+
name: "Startup/registry persistence",
|
|
518
|
+
re: /(HKCU\\[^"'\s]*\\Run|HKLM\\[^"'\s]*\\Run|Startup\\|reg\s+add[^|&;\n]*(Run|CurrentVersion))/gi,
|
|
519
|
+
hint: "Registers the plugin to start on boot (Windows).",
|
|
520
|
+
},
|
|
521
|
+
];
|
|
522
|
+
|
|
523
|
+
/** @type {Map<string, Rule>} */
|
|
524
|
+
const RULE_BY_ID = new Map(RULES.map((r) => [r.id, r]));
|
|
525
|
+
|
|
526
|
+
/** Get a rule by id. */
|
|
527
|
+
export function ruleById(id) {
|
|
528
|
+
return RULE_BY_ID.get(id);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* Apply every rule to one file's text, producing line-level hits.
|
|
533
|
+
* Only rules with a global flag are used; each match maps to its line.
|
|
534
|
+
* @param {string} fileName - basename/relative path for the report.
|
|
535
|
+
* @param {string} content - file text.
|
|
536
|
+
* @returns {Array<{rule:string, category:string, severity:string, file:string, line:number, evidence:string, hint:string}>}
|
|
537
|
+
*/
|
|
538
|
+
export function scanText(fileName, content) {
|
|
539
|
+
const hits = [];
|
|
540
|
+
const text = String(content);
|
|
541
|
+
for (const rule of RULES) {
|
|
542
|
+
const re = new RegExp(rule.re.source, rule.re.flags.includes("g") ? rule.re.flags : rule.re.flags + "g");
|
|
543
|
+
let match;
|
|
544
|
+
const perLine = new Map();
|
|
545
|
+
while ((match = re.exec(text)) !== null) {
|
|
546
|
+
const line = lineOf(text, match.index);
|
|
547
|
+
const key = line.number;
|
|
548
|
+
const bucket = perLine.get(key) || { count: 0, index: line.start };
|
|
549
|
+
bucket.count += 1;
|
|
550
|
+
if (bucket.index > line.start) bucket.index = line.start;
|
|
551
|
+
perLine.set(key, bucket);
|
|
552
|
+
}
|
|
553
|
+
for (const [lineNo, bucket] of perLine) {
|
|
554
|
+
if (rule.minCount !== undefined && bucket.count < rule.minCount) continue;
|
|
555
|
+
const lineText = lineTextAt(text, bucket.index).trim();
|
|
556
|
+
hits.push({
|
|
557
|
+
rule: rule.id,
|
|
558
|
+
category: rule.category,
|
|
559
|
+
severity: rule.severity,
|
|
560
|
+
file: fileName,
|
|
561
|
+
line: lineNo,
|
|
562
|
+
count: bucket.count,
|
|
563
|
+
evidence: (lineText || String(match)).slice(0, 200),
|
|
564
|
+
hint: rule.hint,
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
return hits;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
/** Line number + start index of the line containing byte offset index. */
|
|
572
|
+
function lineOf(text, index) {
|
|
573
|
+
let line = 1;
|
|
574
|
+
let start = 0;
|
|
575
|
+
for (let i = 0; i < index && i < text.length; i++) {
|
|
576
|
+
if (text.charCodeAt(i) === 10) {
|
|
577
|
+
line += 1;
|
|
578
|
+
start = i + 1;
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
return { number: line, start };
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
/** Extract the full line text at byte offset index. */
|
|
585
|
+
function lineTextAt(text, index) {
|
|
586
|
+
const start = text.lastIndexOf("\n", index - 1) + 1;
|
|
587
|
+
let end = text.indexOf("\n", index);
|
|
588
|
+
if (end === -1) end = text.length;
|
|
589
|
+
return text.slice(start, end);
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
/**
|
|
593
|
+
* Collect unique external hosts from a text blob.
|
|
594
|
+
* @param {string} content - text to scan for URLs.
|
|
595
|
+
* @returns {string[]} unique hostnames in appearance order.
|
|
596
|
+
*/
|
|
597
|
+
export function extractHosts(content) {
|
|
598
|
+
const hosts = [];
|
|
599
|
+
const seen = new Set();
|
|
600
|
+
const urlRe = /\bhttps?:\/\/[^\s"'\x60<>()]+/g;
|
|
601
|
+
let m;
|
|
602
|
+
while ((m = urlRe.exec(content)) !== null) {
|
|
603
|
+
let host;
|
|
604
|
+
try {
|
|
605
|
+
host = new URL(m[0]).hostname;
|
|
606
|
+
} catch {
|
|
607
|
+
continue;
|
|
608
|
+
}
|
|
609
|
+
const key = host.toLowerCase();
|
|
610
|
+
if (seen.has(key)) continue;
|
|
611
|
+
seen.add(key);
|
|
612
|
+
hosts.push(host);
|
|
613
|
+
}
|
|
614
|
+
return hosts;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
/**
|
|
618
|
+
* Heuristic: does the same file read local files AND send data out?
|
|
619
|
+
* @param {string} content - file text.
|
|
620
|
+
* @returns {boolean}
|
|
621
|
+
*/
|
|
622
|
+
export function hasReadAndSend(content) {
|
|
623
|
+
const reads = /\b(readFile|readFileSync|readText|readBytes|createReadStream|Get-Content)/g;
|
|
624
|
+
const sends = /\b(fetch\s*\(|http\.(get|request)|https\.(get|request)|axios|WebSocket|net\.connect)/g;
|
|
625
|
+
reads.lastIndex = 0;
|
|
626
|
+
sends.lastIndex = 0;
|
|
627
|
+
return reads.test(content) && sends.test(content);
|
|
628
|
+
}
|