@pwddd/skills-scanner 1.0.3
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 +453 -0
- package/index.ts +373 -0
- package/openclaw.plugin.json +60 -0
- package/package.json +49 -0
- package/skills/skills-scanner/SKILL.md +585 -0
- package/skills/skills-scanner/__pycache__/scan.cpython-314.pyc +0 -0
- package/skills/skills-scanner/scan.py +440 -0
- package/src/commands.ts +302 -0
- package/src/config.ts +170 -0
- package/src/cron.ts +174 -0
- package/src/deps.ts +73 -0
- package/src/prompt-guidance.ts +25 -0
- package/src/report.ts +100 -0
- package/src/scanner.ts +50 -0
- package/src/state.ts +71 -0
- package/src/types.ts +48 -0
- package/src/watcher.ts +125 -0
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
# /// script
|
|
2
|
+
# dependencies = [
|
|
3
|
+
# "requests>=2.31.0",
|
|
4
|
+
# ]
|
|
5
|
+
# ///
|
|
6
|
+
"""
|
|
7
|
+
OpenClaw Skills 安全扫描器 (HTTP 客户端)
|
|
8
|
+
通过 HTTP API 调用远程 skill-scanner-api 服务
|
|
9
|
+
|
|
10
|
+
注意:此脚本必须使用 venv 中的 Python 运行
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
import sys
|
|
14
|
+
import os
|
|
15
|
+
import json
|
|
16
|
+
import argparse
|
|
17
|
+
import tempfile
|
|
18
|
+
import zipfile
|
|
19
|
+
import time
|
|
20
|
+
from pathlib import Path
|
|
21
|
+
from typing import Optional, Dict, Any, List
|
|
22
|
+
|
|
23
|
+
# 依赖检查
|
|
24
|
+
try:
|
|
25
|
+
import requests
|
|
26
|
+
except ImportError as e:
|
|
27
|
+
print("❌ requests 未安装。")
|
|
28
|
+
print(f" 导入错误: {e}")
|
|
29
|
+
print(" 请运行: uv pip install requests")
|
|
30
|
+
sys.exit(1)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
# 配置
|
|
34
|
+
DEFAULT_API_URL = "http://localhost:8000"
|
|
35
|
+
REQUEST_TIMEOUT = 180 # 3 分钟
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
# 颜色输出
|
|
39
|
+
USE_COLOR = sys.stdout.isatty()
|
|
40
|
+
|
|
41
|
+
def c(text, code):
|
|
42
|
+
return f"\033[{code}m{text}\033[0m" if USE_COLOR else text
|
|
43
|
+
|
|
44
|
+
RED = lambda t: c(t, "31")
|
|
45
|
+
YELLOW = lambda t: c(t, "33")
|
|
46
|
+
GREEN = lambda t: c(t, "32")
|
|
47
|
+
CYAN = lambda t: c(t, "36")
|
|
48
|
+
BOLD = lambda t: c(t, "1")
|
|
49
|
+
DIM = lambda t: c(t, "2")
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
# HTTP 客户端
|
|
53
|
+
class SkillScannerClient:
|
|
54
|
+
"""skill-scanner HTTP API 客户端"""
|
|
55
|
+
|
|
56
|
+
def __init__(self, base_url: str = DEFAULT_API_URL):
|
|
57
|
+
self.base_url = base_url.rstrip('/')
|
|
58
|
+
self.session = requests.Session()
|
|
59
|
+
|
|
60
|
+
def health_check(self) -> Dict[str, Any]:
|
|
61
|
+
"""健康检查,返回详细信息"""
|
|
62
|
+
try:
|
|
63
|
+
response = self.session.get(f"{self.base_url}/health", timeout=5)
|
|
64
|
+
response.raise_for_status()
|
|
65
|
+
return {
|
|
66
|
+
'status': 'healthy',
|
|
67
|
+
'data': response.json()
|
|
68
|
+
}
|
|
69
|
+
except requests.exceptions.ConnectionError:
|
|
70
|
+
return {
|
|
71
|
+
'status': 'unreachable',
|
|
72
|
+
'error': f'无法连接到 {self.base_url}'
|
|
73
|
+
}
|
|
74
|
+
except requests.exceptions.Timeout:
|
|
75
|
+
return {
|
|
76
|
+
'status': 'timeout',
|
|
77
|
+
'error': '请求超时'
|
|
78
|
+
}
|
|
79
|
+
except Exception as e:
|
|
80
|
+
return {
|
|
81
|
+
'status': 'error',
|
|
82
|
+
'error': str(e)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
def scan_upload(
|
|
86
|
+
self,
|
|
87
|
+
skill_path: str,
|
|
88
|
+
policy: str = "balanced",
|
|
89
|
+
use_llm: bool = False,
|
|
90
|
+
use_behavioral: bool = False
|
|
91
|
+
) -> Dict[str, Any]:
|
|
92
|
+
"""上传 ZIP 文件扫描(单个 Skill)
|
|
93
|
+
|
|
94
|
+
API: POST /scan-upload
|
|
95
|
+
- 上传 ZIP 文件
|
|
96
|
+
- 服务器解压并查找 SKILL.md
|
|
97
|
+
- 返回扫描结果
|
|
98
|
+
"""
|
|
99
|
+
with tempfile.NamedTemporaryFile(suffix='.zip', delete=False) as tmp_zip:
|
|
100
|
+
zip_path = tmp_zip.name
|
|
101
|
+
|
|
102
|
+
try:
|
|
103
|
+
self._create_zip(skill_path, zip_path)
|
|
104
|
+
|
|
105
|
+
with open(zip_path, 'rb') as f:
|
|
106
|
+
files = {'file': (os.path.basename(skill_path) + '.zip', f, 'application/zip')}
|
|
107
|
+
data = {
|
|
108
|
+
'policy': policy,
|
|
109
|
+
'use_llm': str(use_llm).lower(),
|
|
110
|
+
'use_behavioral': str(use_behavioral).lower()
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
response = self.session.post(
|
|
114
|
+
f"{self.base_url}/scan-upload",
|
|
115
|
+
files=files,
|
|
116
|
+
data=data,
|
|
117
|
+
timeout=REQUEST_TIMEOUT
|
|
118
|
+
)
|
|
119
|
+
response.raise_for_status()
|
|
120
|
+
return response.json()
|
|
121
|
+
finally:
|
|
122
|
+
if os.path.exists(zip_path):
|
|
123
|
+
os.unlink(zip_path)
|
|
124
|
+
|
|
125
|
+
def scan_batch_upload(
|
|
126
|
+
self,
|
|
127
|
+
skill_paths: List[str],
|
|
128
|
+
policy: str = "balanced",
|
|
129
|
+
use_llm: bool = False,
|
|
130
|
+
use_behavioral: bool = False
|
|
131
|
+
) -> List[Dict[str, Any]]:
|
|
132
|
+
"""批量上传多个 Skill(客户端循环)"""
|
|
133
|
+
results = []
|
|
134
|
+
|
|
135
|
+
for i, skill_path in enumerate(skill_paths, 1):
|
|
136
|
+
print(f"[{i}/{len(skill_paths)}] 正在扫描: {skill_path}")
|
|
137
|
+
|
|
138
|
+
try:
|
|
139
|
+
result = self.scan_upload(
|
|
140
|
+
skill_path,
|
|
141
|
+
policy=policy,
|
|
142
|
+
use_llm=use_llm,
|
|
143
|
+
use_behavioral=use_behavioral
|
|
144
|
+
)
|
|
145
|
+
results.append({
|
|
146
|
+
'path': skill_path,
|
|
147
|
+
'success': True,
|
|
148
|
+
'result': result
|
|
149
|
+
})
|
|
150
|
+
status = "✓" if result.get('is_safe', False) else "✗"
|
|
151
|
+
print(f" {status} {result.get('skill_name', 'Unknown')}: {result.get('findings_count', 0)} 个发现")
|
|
152
|
+
except Exception as e:
|
|
153
|
+
results.append({
|
|
154
|
+
'path': skill_path,
|
|
155
|
+
'success': False,
|
|
156
|
+
'error': str(e)
|
|
157
|
+
})
|
|
158
|
+
print(f" ✗ 失败: {e}")
|
|
159
|
+
|
|
160
|
+
return results
|
|
161
|
+
|
|
162
|
+
def scan_clawhub(
|
|
163
|
+
self,
|
|
164
|
+
clawhub_url: str,
|
|
165
|
+
policy: str = "balanced",
|
|
166
|
+
use_llm: bool = False,
|
|
167
|
+
use_behavioral: bool = False
|
|
168
|
+
) -> Dict[str, Any]:
|
|
169
|
+
"""扫描 ClawHub 上的 Skill
|
|
170
|
+
|
|
171
|
+
API: POST /scan-clawhub
|
|
172
|
+
- 提供 ClawHub URL
|
|
173
|
+
- 服务器自动下载并扫描
|
|
174
|
+
- 返回扫描结果
|
|
175
|
+
|
|
176
|
+
Args:
|
|
177
|
+
clawhub_url: ClawHub 项目 URL (例如: https://clawhub.ai/username/project)
|
|
178
|
+
policy: 扫描策略
|
|
179
|
+
use_llm: 是否启用 LLM 分析
|
|
180
|
+
use_behavioral: 是否启用行为分析
|
|
181
|
+
"""
|
|
182
|
+
data = {
|
|
183
|
+
'clawhub_url': clawhub_url,
|
|
184
|
+
'policy': policy,
|
|
185
|
+
'use_llm': use_llm,
|
|
186
|
+
'use_behavioral': use_behavioral,
|
|
187
|
+
'llm_provider': 'anthropic',
|
|
188
|
+
'use_virustotal': False,
|
|
189
|
+
'use_aidefense': False,
|
|
190
|
+
'use_trigger': False,
|
|
191
|
+
'enable_meta': False
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
response = self.session.post(
|
|
195
|
+
f"{self.base_url}/scan-clawhub",
|
|
196
|
+
json=data,
|
|
197
|
+
timeout=REQUEST_TIMEOUT
|
|
198
|
+
)
|
|
199
|
+
response.raise_for_status()
|
|
200
|
+
return response.json()
|
|
201
|
+
|
|
202
|
+
@staticmethod
|
|
203
|
+
def _create_zip(source_dir: str, zip_path: str):
|
|
204
|
+
"""创建 ZIP 文件"""
|
|
205
|
+
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
|
206
|
+
source_path = Path(source_dir)
|
|
207
|
+
for file_path in source_path.rglob('*'):
|
|
208
|
+
if file_path.is_file():
|
|
209
|
+
arcname = file_path.relative_to(source_path.parent)
|
|
210
|
+
zipf.write(file_path, arcname)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
# 格式化输出
|
|
214
|
+
def format_scan_result(result: Dict[str, Any], detailed: bool = False) -> str:
|
|
215
|
+
"""格式化扫描结果"""
|
|
216
|
+
lines = []
|
|
217
|
+
|
|
218
|
+
skill_name = result.get('skill_name', 'Unknown')
|
|
219
|
+
is_safe = result.get('is_safe', False)
|
|
220
|
+
max_severity = result.get('max_severity', 'NONE')
|
|
221
|
+
findings_count = result.get('findings_count', 0)
|
|
222
|
+
|
|
223
|
+
status_icon = GREEN("✓") if is_safe else RED("✗")
|
|
224
|
+
lines.append(f"{status_icon} {BOLD(skill_name)}")
|
|
225
|
+
lines.append(f" 严重性: {_severity_color(max_severity)}")
|
|
226
|
+
lines.append(f" 发现数: {findings_count}")
|
|
227
|
+
|
|
228
|
+
if detailed and findings_count > 0:
|
|
229
|
+
findings = result.get('findings', [])
|
|
230
|
+
lines.append("")
|
|
231
|
+
lines.append(BOLD("发现详情:"))
|
|
232
|
+
for i, finding in enumerate(findings[:10], 1):
|
|
233
|
+
severity = finding.get('severity', 'UNKNOWN')
|
|
234
|
+
category = finding.get('category', 'Unknown')
|
|
235
|
+
description = finding.get('description', 'No description')
|
|
236
|
+
lines.append(f" {i}. [{_severity_color(severity)}] {category}")
|
|
237
|
+
lines.append(f" {description}")
|
|
238
|
+
|
|
239
|
+
if len(findings) > 10:
|
|
240
|
+
lines.append(f" ... 还有 {len(findings) - 10} 条发现")
|
|
241
|
+
|
|
242
|
+
return "\n".join(lines)
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def format_batch_result(result: Dict[str, Any]) -> str:
|
|
246
|
+
"""格式化批量扫描结果"""
|
|
247
|
+
lines = []
|
|
248
|
+
|
|
249
|
+
total = result.get('total_skills_scanned', 0)
|
|
250
|
+
safe = result.get('safe_skills', 0)
|
|
251
|
+
unsafe = result.get('unsafe_skills', 0)
|
|
252
|
+
|
|
253
|
+
lines.append(BOLD("批量扫描结果"))
|
|
254
|
+
lines.append(f" 总计: {total} 个 Skills")
|
|
255
|
+
lines.append(f" 安全: {GREEN(str(safe))}")
|
|
256
|
+
lines.append(f" 问题: {RED(str(unsafe))}")
|
|
257
|
+
|
|
258
|
+
if unsafe > 0:
|
|
259
|
+
skills = result.get('skills', [])
|
|
260
|
+
unsafe_skills = [s for s in skills if not s.get('is_safe', True)]
|
|
261
|
+
lines.append("")
|
|
262
|
+
lines.append(BOLD("问题 Skills:"))
|
|
263
|
+
for skill in unsafe_skills[:10]:
|
|
264
|
+
name = skill.get('skill_name', 'Unknown')
|
|
265
|
+
severity = skill.get('max_severity', 'UNKNOWN')
|
|
266
|
+
count = skill.get('findings_count', 0)
|
|
267
|
+
lines.append(f" • {name} [{_severity_color(severity)}] - {count} 条发现")
|
|
268
|
+
|
|
269
|
+
return "\n".join(lines)
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
def _severity_color(severity: str) -> str:
|
|
273
|
+
"""严重性着色"""
|
|
274
|
+
severity_upper = severity.upper()
|
|
275
|
+
if severity_upper in ('CRITICAL', 'HIGH'):
|
|
276
|
+
return RED(severity_upper)
|
|
277
|
+
elif severity_upper == 'MEDIUM':
|
|
278
|
+
return YELLOW(severity_upper)
|
|
279
|
+
elif severity_upper == 'LOW':
|
|
280
|
+
return CYAN(severity_upper)
|
|
281
|
+
else:
|
|
282
|
+
return DIM(severity_upper)
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
# 命令行接口
|
|
286
|
+
def main():
|
|
287
|
+
parser = argparse.ArgumentParser(
|
|
288
|
+
description="OpenClaw Skills 安全扫描器 (HTTP 客户端)",
|
|
289
|
+
formatter_class=argparse.RawDescriptionHelpFormatter
|
|
290
|
+
)
|
|
291
|
+
|
|
292
|
+
parser.add_argument(
|
|
293
|
+
'--api-url',
|
|
294
|
+
default=DEFAULT_API_URL,
|
|
295
|
+
help=f'API 服务地址 (默认: {DEFAULT_API_URL})'
|
|
296
|
+
)
|
|
297
|
+
|
|
298
|
+
subparsers = parser.add_subparsers(dest='command', help='命令')
|
|
299
|
+
|
|
300
|
+
# scan 命令
|
|
301
|
+
scan_parser = subparsers.add_parser('scan', help='扫描单个 Skill(上传 ZIP)')
|
|
302
|
+
scan_parser.add_argument('path', help='Skill 目录路径')
|
|
303
|
+
scan_parser.add_argument('--detailed', action='store_true', help='显示详细发现')
|
|
304
|
+
scan_parser.add_argument('--behavioral', action='store_true', help='启用行为分析')
|
|
305
|
+
scan_parser.add_argument('--llm', action='store_true', help='启用 LLM 分析')
|
|
306
|
+
scan_parser.add_argument('--policy', default='balanced', choices=['strict', 'balanced', 'permissive'], help='扫描策略')
|
|
307
|
+
scan_parser.add_argument('--json', metavar='FILE', help='输出 JSON 到文件')
|
|
308
|
+
|
|
309
|
+
# batch 命令(客户端批量上传)
|
|
310
|
+
batch_parser = subparsers.add_parser('batch', help='批量扫描多个 Skills(客户端循环)')
|
|
311
|
+
batch_parser.add_argument('paths', nargs='+', help='多个 Skill 目录路径')
|
|
312
|
+
batch_parser.add_argument('--behavioral', action='store_true', help='启用行为分析')
|
|
313
|
+
batch_parser.add_argument('--llm', action='store_true', help='启用 LLM 分析')
|
|
314
|
+
batch_parser.add_argument('--policy', default='balanced', choices=['strict', 'balanced', 'permissive'], help='扫描策略')
|
|
315
|
+
batch_parser.add_argument('--json', metavar='FILE', help='输出 JSON 到文件')
|
|
316
|
+
|
|
317
|
+
# clawhub 命令
|
|
318
|
+
clawhub_parser = subparsers.add_parser('clawhub', help='扫描 ClawHub 上的 Skill')
|
|
319
|
+
clawhub_parser.add_argument('url', help='ClawHub 项目 URL (例如: https://clawhub.ai/username/project)')
|
|
320
|
+
clawhub_parser.add_argument('--detailed', action='store_true', help='显示详细发现')
|
|
321
|
+
clawhub_parser.add_argument('--behavioral', action='store_true', help='启用行为分析')
|
|
322
|
+
clawhub_parser.add_argument('--llm', action='store_true', help='启用 LLM 分析')
|
|
323
|
+
clawhub_parser.add_argument('--policy', default='balanced', choices=['strict', 'balanced', 'permissive'], help='扫描策略')
|
|
324
|
+
clawhub_parser.add_argument('--json', metavar='FILE', help='输出 JSON 到文件')
|
|
325
|
+
|
|
326
|
+
# health 命令
|
|
327
|
+
subparsers.add_parser('health', help='健康检查')
|
|
328
|
+
|
|
329
|
+
args = parser.parse_args()
|
|
330
|
+
|
|
331
|
+
if not args.command:
|
|
332
|
+
parser.print_help()
|
|
333
|
+
sys.exit(1)
|
|
334
|
+
|
|
335
|
+
client = SkillScannerClient(args.api_url)
|
|
336
|
+
|
|
337
|
+
try:
|
|
338
|
+
if args.command == 'health':
|
|
339
|
+
health_result = client.health_check()
|
|
340
|
+
|
|
341
|
+
if health_result['status'] == 'healthy':
|
|
342
|
+
print(GREEN("✓") + " API 服务正常")
|
|
343
|
+
|
|
344
|
+
data = health_result.get('data', {})
|
|
345
|
+
if data:
|
|
346
|
+
print(f" 版本: {data.get('version', 'Unknown')}")
|
|
347
|
+
analyzers = data.get('analyzers_available', [])
|
|
348
|
+
if analyzers:
|
|
349
|
+
print(f" 可用分析器: {', '.join(analyzers)}")
|
|
350
|
+
print(json.dumps(data))
|
|
351
|
+
|
|
352
|
+
sys.exit(0)
|
|
353
|
+
else:
|
|
354
|
+
print(RED("✗") + f" API 服务不可用: {args.api_url}")
|
|
355
|
+
error = health_result.get('error', '未知错误')
|
|
356
|
+
print(f" 错误: {error}")
|
|
357
|
+
sys.exit(1)
|
|
358
|
+
|
|
359
|
+
elif args.command == 'scan':
|
|
360
|
+
print(f"正在扫描: {args.path}")
|
|
361
|
+
result = client.scan_upload(
|
|
362
|
+
args.path,
|
|
363
|
+
policy=args.policy,
|
|
364
|
+
use_llm=args.llm,
|
|
365
|
+
use_behavioral=args.behavioral
|
|
366
|
+
)
|
|
367
|
+
|
|
368
|
+
if args.json:
|
|
369
|
+
with open(args.json, 'w') as f:
|
|
370
|
+
json.dump(result, f, indent=2)
|
|
371
|
+
print(f"结果已保存到: {args.json}")
|
|
372
|
+
else:
|
|
373
|
+
print(format_scan_result(result, args.detailed))
|
|
374
|
+
|
|
375
|
+
sys.exit(0 if result.get('is_safe', False) else 1)
|
|
376
|
+
|
|
377
|
+
elif args.command == 'batch':
|
|
378
|
+
print(f"正在批量扫描 {len(args.paths)} 个 Skills...")
|
|
379
|
+
results = client.scan_batch_upload(
|
|
380
|
+
args.paths,
|
|
381
|
+
policy=args.policy,
|
|
382
|
+
use_llm=args.llm,
|
|
383
|
+
use_behavioral=args.behavioral
|
|
384
|
+
)
|
|
385
|
+
|
|
386
|
+
total = len(results)
|
|
387
|
+
success = sum(1 for r in results if r['success'])
|
|
388
|
+
failed = total - success
|
|
389
|
+
|
|
390
|
+
if args.json:
|
|
391
|
+
with open(args.json, 'w') as f:
|
|
392
|
+
json.dump(results, f, indent=2)
|
|
393
|
+
print(f"\n结果已保存到: {args.json}")
|
|
394
|
+
|
|
395
|
+
print(f"\n批量扫描完成: {success}/{total} 成功, {failed} 失败")
|
|
396
|
+
sys.exit(0 if failed == 0 else 1)
|
|
397
|
+
|
|
398
|
+
elif args.command == 'clawhub':
|
|
399
|
+
print(f"正在扫描 ClawHub Skill: {args.url}")
|
|
400
|
+
result = client.scan_clawhub(
|
|
401
|
+
args.url,
|
|
402
|
+
policy=args.policy,
|
|
403
|
+
use_llm=args.llm,
|
|
404
|
+
use_behavioral=args.behavioral
|
|
405
|
+
)
|
|
406
|
+
|
|
407
|
+
if args.json:
|
|
408
|
+
with open(args.json, 'w') as f:
|
|
409
|
+
json.dump(result, f, indent=2)
|
|
410
|
+
print(f"结果已保存到: {args.json}")
|
|
411
|
+
else:
|
|
412
|
+
print(format_scan_result(result, args.detailed))
|
|
413
|
+
|
|
414
|
+
sys.exit(0 if result.get('is_safe', False) else 1)
|
|
415
|
+
|
|
416
|
+
except requests.exceptions.ConnectionError:
|
|
417
|
+
print(RED("✗") + f" 无法连接到 API 服务: {args.api_url}")
|
|
418
|
+
print("请确保 skill-scanner-api 服务正在运行")
|
|
419
|
+
sys.exit(1)
|
|
420
|
+
except requests.exceptions.Timeout:
|
|
421
|
+
print(RED("✗") + " 请求超时")
|
|
422
|
+
sys.exit(1)
|
|
423
|
+
except requests.exceptions.HTTPError as e:
|
|
424
|
+
print(RED("✗") + f" HTTP 错误: {e}")
|
|
425
|
+
if e.response is not None:
|
|
426
|
+
try:
|
|
427
|
+
error_detail = e.response.json()
|
|
428
|
+
print(f"详情: {error_detail}")
|
|
429
|
+
except:
|
|
430
|
+
print(f"响应: {e.response.text}")
|
|
431
|
+
sys.exit(1)
|
|
432
|
+
except Exception as e:
|
|
433
|
+
print(RED("✗") + f" 错误: {e}")
|
|
434
|
+
import traceback
|
|
435
|
+
traceback.print_exc()
|
|
436
|
+
sys.exit(1)
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
if __name__ == '__main__':
|
|
440
|
+
main()
|