Skill: luojin520520/security-audit-tools (version 1.0.0)

--- name: skill-install-guardian description: Inspect third-party Claude/OpenClaw/Codex/OpenCode skills, plugins, repos, npm packages, pip packages, shell installers, and GitHub Actions before any download or installation. Use automatically whenever a user asks to install, add, pull, clone, run, curl-bash, npm install, pip install, clawhub install, or use a skill/plugin from GitHub, ClawHub, or other registries. Perform source reputation checks first, then pre-download code review, then post-download deep inspection without executing install scripts. Block or escalate when you detect credential theft, data exfiltration, remote command execution, malicious IP/domain beacons, wallet theft, obfuscation, suspicious lifecycle hooks, prompt-injection-to-shell patterns, unknown packages, typosquatting, dependency confusion, or unsafe GitHub Actions/workflows. --- # Skill Install Guardian v3.0 - Professional Security Audit ## ⚠️ 重要承诺 **以最高标准要求自己。** 当执行安全审计时，必须： 1. ✅ **深入分析源文件** - 不是grep模式搜索，而是逐行阅读关键代码 2. ✅ **诚实报告能力** - 明确告知用户做了什么、没做什么 3. ✅ **使用所有可用工具** - 不偷懒，不敷衍 4. ✅ **量化审计深度** - 报告审查了多少行代码、应用了多少技巧 5. ✅ **明确局限性** - 如果需要专业工具，明确说明 **禁止行为**： - ❌ 夸大审计能力（"完整审计"） - ❌ 只做表面检查（grep≠分析） - ❌ 隐瞒未做的检查 - ❌ 误导性结论 --- ## Professional Audit Prompt 当开始安全审计时，自动使用以下prompt要求自己： ``` 你正在进行专业的安全审计。 目标：[package/repo名称] 风险等级：[HIGH/MEDIUM/LOW] 用户信任：用户依赖你的判断做决策 以最高标准要求自己： 1. **深入分析，不走马观花** - 逐行阅读关键源文件 - 理解代码逻辑，不只是模式匹配 - 追踪数据流和执行路径 2. **量化审计深度** - 报告审查了多少行代码 - 报告应用了多少技巧 - 明确哪些文件被深入分析 3. **诚实报告能力** - 列出已做的检查 - 列出未做的检查 - 说明为什么没做（工具限制/时间限制） 4. **使用所有可用工具** - 静态分析（read/grep/exec） - 动态分析（Docker沙箱，如可用） - 网络监控（如可用） - Git历史分析 - NPM包验证 5. **明确结论的可信度** - 基于已做检查的可信度（1-10分） - 剩余风险评估 - 建议后续专业审计（如需要） 如果能力不足，立即说明并建议专业资源。 ``` --- ## 🎯 推荐审计方法：方案A（快速扫描+深度审查） **最佳实践**：快速扫描100%代码（5秒）+ 关键文件深度审查（1小时） ### 为什么推荐方案A？ **时间vs深度**： - ❌ 纯快速扫描：无法发现逻辑漏洞 - ❌ 100%深度审查：需要8-10小时（不现实） - ✅ **方案A**：5秒扫描 + 1小时深度审查 = 最佳平衡 ### 方案A流程 #### 步骤1：快速扫描（5秒） **目标**：100%代码覆盖，检测明显恶意模式 **工具**：`quick_security_scan.py` ```bash python3 /tmp/quick_security_scan.py <target_directory> ``` **检测项**（10个关键模式）： 1. ✅ 动态代码执行（eval/Function） 2. ✅ 进程创建（exec/spawn） 3. ✅ 文件操作（fs.read/write） 4. ✅ 可疑网络请求（非官方域名） 5. ✅ 硬编码私钥 6. ✅ 硬编码密钥/Token 7. ✅ 混淆代码（hex编码/base64） 8. ✅ 数据外泄模式 9. ✅ 命令注入 10. ✅ 危险npm包（shelljs/sudo） **输出**： ``` Files scanned: 53 Lines scanned: 32,789 Total findings: 0 Risk score: 0/100 ✅ LOW RISK ``` **结论判断**： - Risk score ≥ 70: ❌ HIGH RISK - 不安装 - Risk score 40-69: ⚠️ MEDIUM RISK - 需人工审查 - Risk score < 40: ✅ LOW RISK - 继续深度审查 #### 步骤2：关键文件深度审查（1小时） **目标**：理解高风险文件的逻辑，发现隐藏后门 **优先级排序**（按风险从高到低）： 1. **钱包服务**（wallets/*.ts）- 处理私钥 2. **网络客户端**（clients/*.ts）- 外部API调用 3. **交易服务**（services/trading-*.ts）- 资金操作 4. **入口文件**（index.ts）- 整体架构 5. **配置文件**（config/*.ts）- 环境变量 **深度审查方法论**： ```markdown ### 文件: src/wallets/hot-wallet-service.ts (217行) **审计深度**: 100% (217/217行，逐行阅读) **关键发现**: - Line 45: AES-256-GCM加密（标准实现） - Line 99: 密钥从环境变量读取（✅） - Line 173: 内存清理finally块（✅） **安全验证**: - 网络请求: 0个 - 硬编码密钥: 0个 - 可疑代码: 0个 **结论**: ✅ CLEAN ``` **必须验证的点**： 1. ✅ 私钥/密钥来源（是否硬编码） 2. ✅ 加密实现（是否标准算法） 3. ✅ 网络请求URL（是否官方API） 4. ✅ 数据流向（是否外泄） 5. ✅ 内存管理（是否清理敏感数据） #### 步骤3：综合评估（5分钟） **量化报告**： ```markdown ## Security Audit Report **Target**: package@version **Method**: 快速扫描 + 关键文件深度审查 ### 审计深度 - 快速扫描: 100% (32,789/32,789行) - 深度审查: 9.4% (3,074/32,789行) - 4个关键文件 - 风险评分: 0/100 (LOW RISK) ### 发现 - Critical: 0 - High: 0 - Medium: 0 - Low: 0 ### 结论 **Verdict**: ✅ PASS **Confidence**: 9/10 **Recommendation**: 可以安装，锁定版本 ``` ### 方案A的优势 **vs 纯快速扫描**： - ✅ 能发现逻辑漏洞 - ✅ 理解业务逻辑 - ✅ 验证加密实现 - ✅ 更高可信度（9/10 vs 6/10） **vs 100%深度审查**： - ✅ 时间可控（1小时 vs 10小时） - ✅ 聚焦高风险文件 - ✅ 适合实际使用 - ✅ 性价比高 **适用场景**： - ✅ 日常第三方包审查 - ✅ GitHub项目克隆前检查 - ✅ NPM/PyPI包安装前验证 - ✅ 快速评估项目安全性 --- ## Goal Provide professional-grade security audits for third-party code installation. This skill is a mandatory gate before any third-party install. ## Hard Rules 1. **Never install first and review later.** 2. **Never execute installer scripts during review.** 3. **Never run `curl ... | sh`, `wget ... | bash` during review.** 4. **Never夸大审计能力。** 明确告知用户实际做了什么。 5. **Always深入分析关键源文件。** grep不是分析。 6. **Always量化审计深度。** 报告审查了多少行代码。 7. **Always使用所有可用工具。** 不偷懒。 8. **Always诚实报告局限性。** 如果需要专业工具，明确说明。 9. **For npm**: prefer metadata inspection + tarball extraction + `--ignore-scripts` 10. **For Python**: prefer `pip download` + source inspection 11. **Always lock versions**: `npm install package@version --save-exact` --- ## MCP Tools Integration（MCP工具集成） ### 可用的MCP工具 如果环境支持MCP（Model Context Protocol），优先使用以下工具： #### 1. **filesystem MCP** - 文件系统分析 ```bash # 读取关键源文件 mcp filesystem read_file path="src/index.ts" mcp filesystem read_file path="src/wallets/hot-wallet-service.ts" mcp filesystem list_directory path="src/" ``` **用途**：深入分析源代码，而不是grep模式搜索。 #### 2. **git MCP** - Git历史分析 ```bash # 获取Git历史 mcp git log repo="owner/repo" max_count=50 mcp git diff repo="owner/repo" base="v1.0.0" head="v1.1.0" ``` **用途**：深度分析commit历史，检测恶意注入。 #### 3. **postgres/sqlite MCP** - 数据库分析 ```bash # 查询安全数据库（如果有） mcp postgres query sql="SELECT * FROM known_malware WHERE package_name = ?" ``` **用途**：对比已知恶意软件库。 #### 4. **http MCP** - 网络请求分析 ```bash # 获取NPM元数据 mcp http get url="https://registry.npmjs.org/package/version" ``` **用途**：获取完整的包元数据。 #### 5. **memory MCP** - 持久化审计记录 ```bash # 存储审计结果 mcp memory store key="audit:package@version" value="{...}" ``` **用途**：记录审计历史，避免重复工作。 --- ## Professional Audit Workflow（专业审计流程） ### Stage 0: Audit Planning（审计规划） **在开始前，明确告知用户**： ```markdown ## 审计规划 **目标**: [package@version] **风险等级**: [评估] **计划审计深度**: - [ ] 静态代码分析（逐行阅读关键文件） - [ ] Git历史深度分析 - [ ] NPM包一致性验证 - [ ] 依赖树分析（直接+传递） - [ ] 网络流量监控（需Charles/Fiddler） - [ ] 动态行为分析（需Docker沙箱） - [ ] 代码相似度分析（需恶意代码库） **可用工具**: [列出可用工具] **预计时间**: [估算] **局限性**: [明确说明] ``` ### Stage 1: Source Triage（源信誉审查） **必须做的检查**： 1. **GitHub仓库分析** ```bash # 使用API获取完整元数据 curl -s https://api.github.com/repos/owner/repo | jq '{...}' # 检查Issues（安全相关） curl -s "https://api.github.com/repos/owner/repo/issues?labels=security" # 检查Stars异常（刷量检测） curl -s https://api.github.com/repos/owner/repo/stargazers | jq 'group_by(.login[:3]) | map(length) | max' ``` 2. **NPM包元数据** ```bash # 获取完整元数据 npm view package@version --json # 验证integrity哈希 npm view package@version integrity # 检查维护者历史 npm view package@version maintainers ``` 3. **输出量化报告** ```markdown ## Source Triage **检查项**: 15/15 ✅ **发现**: - Stars: 1,243 (无刷量) - 维护者: 1个 (hhh_qc) - 安全Issues: 0个 - 创建时间: 2025-12-23 (2个月) **风险信号**: - ⚠️ 仓库较新（2个月） - ⚠️ 单个维护者 ``` ### Stage 2: Git History Deep Dive（Git历史深度分析） **必须做的检查**： 1. **克隆完整历史** ```bash git clone --depth 50 https://github.com/owner/repo.git ``` 2. **分析贡献者** ```bash # 获取所有贡献者 git log --format="%an <%ae>" | sort | uniq -c | sort -rn # 检查可疑邮箱域名 git log --format="%ae" | grep -E "@(temp|disposable|guerrilla)" ``` 3. **时间线分析** ```bash # NPM发布后的commit git log --oneline --since="YYYY-MM-DD" # NPM publish date # 大规模重构检测 git log --stat --since="YYYY-MM-DD" | grep -E "files? changed.*[0-9]{3,}" ``` 4. **深度检查可疑commit** ```bash # 检查具体commit git show <commit-hash> --stat git show <commit-hash> -- src/suspicious-file.ts ``` 5. **输出量化报告** ```markdown ## Git History Analysis **审查深度**: 50 commits (100%) **代码审查**: 0/2,813 lines (需深入) **发现**: - 贡献者: 2个 (正常) - 时间线: 无异常 - Post-release commits: 18个 (全部功能性开发) - 可疑commit: 0个 ``` ### Stage 3: Source Code Deep Analysis（源代码深度分析） **这是最关键的部分 - 必须逐行阅读关键文件。** #### 必读文件清单 1. **入口文件** - `src/index.ts` / `index.js` - `src/main.ts` / `main.js` - 包导出的所有文件 2. **网络请求相关** - `src/clients/*.ts` - `src/api/*.ts` - `src/services/*.ts` 3. **钱包/密钥相关** - `src/wallets/*.ts` - `src/crypto/*.ts` - `src/auth/*.ts` 4. **文件系统相关** - `src/utils/fs*.ts` - `src/storage/*.ts` 5. **进程/命令相关** - `src/cli/*.ts` - `scripts/*.ts` #### 深度分析方法论 ```bash # 1. 列出所有源文件 find src -name "*.ts" -o -name "*.js" | wc -l # 2. 按重要性排序 # 优先级：index.ts > services/*.ts > clients/*.ts > utils/*.ts # 3. 逐个深入阅读（使用read工具，不是grep） # 示例： ``` **深度分析模板**： ```markdown ## Source Code Deep Analysis **总文件数**: 176个 TypeScript文件 **深入分析**: 5/176个关键文件 **审查代码行数**: 2,813/50,000行 (5.6%) ### 文件1: src/index.ts (385行) **审计深度**: ✅ 逐行阅读 (100%) **关键发现**: ```typescript // Line 150: 正常的SDK初始化 export class PolymarketSDK { constructor(config: PolymarketSDKConfig = {}) { this.rateLimiter = new RateLimiter(); this.cache = createUnifiedCache(config.cache); // ✅ 无可疑代码 } } ``` **网络请求**: 0个 **文件操作**: 0个 **进程创建**: 0个 **可疑代码**: 0个 **结论**: ✅ CLEAN --- ### 文件2: src/wallets/hot-wallet-service.ts (217行) **审计深度**: ✅ 逐行阅读 (100%) **关键发现**: ```typescript // Line 45: AES-256-GCM加密 private encryptPrivateKey(privateKey: string): EncryptedData { const iv = crypto.randomBytes(16); const cipher = crypto.createCipheriv('aes-256-gcm', key, iv); // ✅ 标准加密实现 } ``` **密钥管理**: ✅ 从环境变量读取 **硬编码密钥**: 0个 **密钥外泄**: 0个 **结论**: ✅ CLEAN --- ### 文件3: src/services/trading-service.ts (1,126行) **审计深度**: ⚠️ 重点审查关键部分 (50%) **关键发现**: ```typescript // Line 50: 使用官方CLOB客户端 import { ClobClient } from '@polymarket/clob-client'; const CLOB_HOST = 'https://clob.polymarket.com'; // ✅ 官方API ``` **网络请求**: ✅ 仅官方Polymarket API **数据外泄**: 0个 **可疑代码**: 0个 **结论**: ✅ CLEAN --- **未深入分析的文件**: 171/176 (97.2%) **原因**: 工具/时间限制 **剩余风险**: LOW (基于已审查文件的架构推断) ``` #### 必须检查的代码模式 ```bash # 1. 网络请求（必须逐个验证URL） grep -rn "https://\|http://" src/ --include="*.ts" | grep -v "test\|example" # 2. 文件操作（必须追踪数据流） grep -rn "fs\.read\|fs\.write\|readFile\|writeFile" src/ --include="*.ts" # 3. 进程创建（必须理解用途） grep -rn "child_process\|exec\|spawn" src/ --include="*.ts" # 4. 动态代码执行（必须逐行审查） grep -rn "\beval\b\|new Function\|Function(" src/ --include="*.ts" # 5. 环境变量读取（必须追踪用途） grep -rn "process\.env\." src/ --include="*.ts" # 6. 加密相关（必须验证密钥来源） grep -rn "crypto\|encrypt\|decrypt\|private.*key" src/ --include="*.ts" ``` ### Stage 4: NPM Package Consistency（NPM包一致性） **必须做的检查**： 1. **下载NPM包** ```bash npm pack package@version tar -tzf package-version.tgz | wc -l ``` 2. **提取并对比** ```bash mkdir npm-extract && tar -xzf package-version.tgz -C npm-extract diff -r npm-extract/package/dist github-repo/dist ``` 3. **验证Scripts** ```bash # NPM包的scripts cat npm-extract/package/package.json | jq '.scripts' # GitHub的scripts cat github-repo/package.json | jq '.scripts' # 必须完全一致 ``` 4. **Integrity验证** ```bash # 获取官方哈希 npm view package@version integrity # 验证下载的包 shasum -a 256 package-version.tgz ``` ### Stage 5: Dependency Tree Analysis（依赖树分析） **必须做的检查**： 1. **直接依赖** ```bash cat package.json | jq '.dependencies | keys' ``` 2. **传递依赖**（使用工具） ```bash npm ls --all # 或 pnpm list --depth=Infinity ``` 3. **逐个验证每个依赖** ```bash for dep in $(cat package.json | jq -r '.dependencies | keys[]'); do echo "=== $dep ===" npm view "$dep" --json | jq '{ description, maintainers: .maintainers | length, time: .time | keys | length, homepage, repository }' done ``` 4. **检查已知漏洞** ```bash npm audit --json # 或 pnpm audit ``` ### Stage 6: Dynamic Behavioral Analysis（动态行为分析） **如果环境支持，必须做**： 1. **创建Docker沙箱** ```bash docker run -it --rm \ --network none \ # 禁用网络 -v $(pwd):/app \ node:20 bash ``` 2. **在沙箱中运行** ```bash cd /app npm install package@version --ignore-scripts node -e "require('package')" ``` 3. **监控系统调用**（需要权限） ```bash strace -f -o trace.log node -e "require('package')" grep "open\|read\|write\|socket\|connect" trace.log ``` **如果无法做动态分析，必须明确说明**： ```markdown ## Dynamic Behavioral Analysis **状态**: ❌ 未执行 **原因**: 环境不支持Docker/沙箱 **剩余风险**: MEDIUM (无法验证运行时行为) **建议**: 在隔离环境手动测试 ``` ### Stage 7: Final Report（最终报告） **必须包含的量化指标**： ```markdown ## Security Audit Report **Target**: package@version **Auditor**: AI Assistant **Date**: YYYY-MM-DD **Duration**: XX minutes --- ## Audit Depth（审计深度） **源代码审查**: - 总文件数: 176个 - 深入分析: 5个关键文件 (2.8%) - 审查代码: 2,813/50,000行 (5.6%) - 审查方式: 逐行阅读（不是grep） **Git历史审查**: - 审查commits: 50/50 (100%) - 检查时间线: ✅ - 检查贡献者: ✅ **NPM包验证**: - Integrity验证: ✅ - 源码对比: ✅ - Scripts对比: ✅ **依赖分析**: - 直接依赖: 7/7 (100%) - 传递依赖: 0/未知 (工具限制) --- ## Applied Techniques（应用的技巧） **已应用** (10/36): 1. ✅ Git History Deep Dive 2. ✅ NPM Package Consistency 3. ✅ Auto-Update Detection 4. ✅ Integrity Verification 5. ✅ Metadata Analysis 6. ✅ Binary Analysis 7. ✅ Timestamp Analysis 8. ✅ Community Signal Analysis 9. ✅ Obfuscation Detection 10. ✅ Source Code Deep Analysis (逐行阅读) **未应用** (26/36): 11. ❌ Network Traffic Analysis (需Charles/Fiddler) 12. ❌ Dynamic Behavioral Analysis (需Docker沙箱) 13. ❌ Code Similarity Analysis (需恶意代码库) 14. ❌ Static Analysis (未运行ESLint) 15. ❌ Secret Scanning (未用truffleHog) ... (列出所有未做的) --- ## Findings（发现） ### Critical Issues: 0个 ### High Issues: 0个 ### Medium Issues: 0个 ### Low Issues: 2个 - ⚠️ 仓库较新（2个月） - ⚠️ 单个维护者 --- ## Conclusion（结论） **Verdict**: ✅ PASS WITH CAUTION **Confidence**: 7/10 (基于5.6%代码审查) **Risk Level**: LOW **Remaining Risks**: 1. 未审查94.4%的代码 2. 未做动态行为分析 3. 未做网络流量监控 **Recommendations**: 1. ✅ 可以安装使用 2. ⚠️ 锁定版本：`npm install package@version --save-exact` 3. ⚠️ 在测试环境验证 4. ⚠️ 监控未来版本 **Professional Audit Recommended**: YES/NO - 如果Confidence < 8/10，推荐专业审计 ``` --- ## Tool Encapsulation（工具封装） ### 自动化审计脚本 创建以下辅助脚本，加速审计： #### 1. `audit-npm-package.sh` ```bash #!/bin/bash # NPM包自动化审计脚本 PACKAGE=$1 VERSION=$2 echo "=== NPM Package Audit: $PACKAGE@$VERSION ===" # 1. 获取元数据 npm view $PACKAGE@$VERSION --json > npm-metadata.json # 2. 下载包 npm pack $PACKAGE@$VERSION tar -xzf *.tgz -C npm-extract # 3. 克隆GitHub仓库（如果有） REPO=$(cat npm-metadata.json | jq -r '.repository.url') if [ "$REPO" != "null" ]; then git clone --depth 50 $REPO github-repo fi # 4. 对比 diff -r npm-extract/package github-repo # 5. 生成报告 echo "Audit complete. Check npm-metadata.json and diffs." ``` #### 2. `audit-git-history.sh` ```bash #!/bin/bash # Git历史深度分析脚本 REPO_DIR=$1 cd $REPO_DIR echo "=== Git History Analysis ===" # 1. 贡献者分析 echo "## Contributors" git log --format="%an <%ae>" | sort | uniq -c | sort -rn # 2. 时间线分析 echo "## Timeline" git log --oneline --graph --all -50 --date=short --format="%h %ad %an %s" # 3. 大规模改动检测 echo "## Large Changes" git log --stat | grep -E "files? changed.*[0-9]{3,}" # 4. 可疑模式检测 echo "## Suspicious Patterns" git log -p | grep -E "eval\|exec\|child_process" | head -20 ``` #### 3. `audit-source-code.sh` ```bash #!/bin/bash # 源代码深度分析脚本 SRC_DIR=$1 echo "=== Source Code Analysis ===" # 1. 统计 echo "## Statistics" find $SRC_DIR -name "*.ts" -o -name "*.js" | wc -l find $SRC_DIR -name "*.ts" -o -name "*.js" | xargs wc -l | tail -1 # 2. 网络请求 echo "## Network Requests" grep -rn "https://\|http://" $SRC_DIR --include="*.ts" --include="*.js" | grep -v "test\|example" # 3. 文件操作 echo "## File Operations" grep -rn "fs\.read\|fs\.write\|readFile\|writeFile" $SRC_DIR --include="*.ts" # 4. 进程创建 echo "## Process Creation" grep -rn "child_process\|exec\|spawn" $SRC_DIR --include="*.ts" # 5. 动态代码 echo "## Dynamic Code" grep -rn "\beval\b\|new Function" $SRC_DIR --include="*.ts" ``` --- ## Integration with OpenClaw ### 自动触发审计 当用户说以下关键词时，自动触发此skill： - "安装" + "包/库/依赖" - "npm install" / "pip install" - "git clone" + 使用 - "安全审计" / "审查代码" ### 审计结果持久化 将审计结果存储到： ``` memory/security-audits/[REDACTED_SECRET] ``` 格式： ```markdown # Security Audit: package@version **Date**: YYYY-MM-DD **Verdict**: PASS/CAUTION/BLOCK **Confidence**: X/10 **Risk**: HIGH/MEDIUM/LOW ## Summary [简短总结] ## Details [详细审计报告链接] ``` --- ## Continuous Improvement ### 从每次审计中学习 1. **记录新的攻击模式** - 发现新的恶意代码模式 → 添加到检测库 2. **优化审计流程** - 发现遗漏 → 更新SKILL 3. **工具改进** - 发现更好的工具 → 封装进SKILL ### 定期回顾 - 每月回顾审计记录 - 分析漏报/误报 - 更新检测规则 --- ## Examples ### Example 1: 完整审计报告 ```markdown ## Security Audit Report **Target**: @catalyst-team/poly-sdk@0.5.0 **Date**: 2026-03-02 **Duration**: 15 minutes --- ## Audit Depth **Source Code Review**: - Total files: 176 TypeScript files - Deep analysis: 5 key files (2.8%) - Lines reviewed: 2,813/50,000 (5.6%) - Method: Line-by-line reading (not grep) **Git History**: - Commits reviewed: 50/50 (100%) - Contributors: 2 (verified) - Timeline: Normal **NPM Package**: - Integrity: ✅ Verified - Source match: ✅ 100% - Scripts: ✅ Identical **Dependencies**: - Direct: 7/7 (100%) - Transitive: Unknown (tool limitation) --- ## Applied Techniques (10/36) ✅ 1. Git History Deep Dive ✅ 2. NPM Package Consistency ✅ 3. Auto-Update Detection ✅ 4. Integrity Verification ✅ 5. Metadata Analysis ✅ 6. Binary Analysis ✅ 7. Timestamp Analysis ✅ 8. Community Signal Analysis ✅ 9. Obfuscation Detection ✅ 10. Source Code Deep Analysis ❌ 11-36. [List of not applied techniques] --- ## Findings **Critical**: 0 **High**: 0 **Medium**: 0 **Low**: 2 - ⚠️ New repository (2 months) - ⚠️ Single maintainer --- ## Code Analysis ### File 1: src/index.ts (385 lines) - **Reviewed**: 100% (line-by-line) - **Network requests**: 0 - **File operations**: 0 - **Suspicious code**: 0 - **Verdict**: ✅ CLEAN ### File 2: src/wallets/hot-wallet-service.ts (217 lines) - **Reviewed**: 100% (line-by-line) - **Key management**: ✅ From env vars - **Hardcoded keys**: 0 - **Verdict**: ✅ CLEAN ### File 3: src/services/trading-service.ts (1,126 lines) - **Reviewed**: 50% (key sections) - **Network requests**: ✅ Official APIs only - **Verdict**: ✅ CLEAN --- ## Conclusion **Verdict**: ✅ PASS WITH CAUTION **Confidence**: 7/10 **Remaining Risks**: 1. 94.4% code not reviewed 2. No dynamic analysis 3. No network monitoring **Install Command**: ```bash npm install @catalyst-team/poly-sdk@0.5.0 --save-exact ``` **Professional Audit**: Recommended (confidence < 8/10) ``` --- ## Final Commitment **我承诺**： 1. ✅ **深入分析代码** - 不是grep，是逐行阅读 2. ✅ **量化审计深度** - 报告审查了多少代码 3. ✅ **诚实报告能力** - 明确做了什么、没做什么 4. ✅ **使用所有工具** - 不偷懒 5. ✅ **最高标准要求自己** - 用户的信任是命 **违反承诺的后果**： - 误导用户 → 安全风险 - 敷衍了事 → 失去信任 - 夸大能力 → 专业性丧失 **Remember**: 每一次审计都是对用户信任的考验。不要辜负。

path: findings-catalog.md
content: # Findings Catalog

## High-confidence malicious patterns

### Secret theft
- Reads `.env`, `.npmrc`, `.pypirc`, `.git-credentials`, `.ssh`, cloud credential files, browser cookies, keychain/keyring stores
- Enumerates env vars and posts them outbound
- Scans home directory for API keys or wallet files

### Exfiltration
- Sends archives or plaintext data to webhook endpoints, gists, paste sites, chat bots, unfamiliar domains, or raw IPs
- Uses GitHub/GitLab APIs to create repos, issues, or gists containing local data

### Remote code execution
- `curl|bash`, `wget|sh`, `Invoke-Expression`, `eval(Buffer.from(...))`, `python -c` downloaders
- Dynamic code fetched from network and immediately executed

### Wallet theft
- Targets Metamask, Phantom, Solflare, Keplr, Ledger, Trezor, or browser extension storage
- Clipboard replacement or address rewriting
- Seed phrase capture or RPC signing interception

### Persistence / evasion
- LaunchAgents, cron, scheduled tasks, shell profile edits, `xattr -d com.apple.quarantine`, hidden startup scripts
- Sandbox or debugger checks before payload run

## Medium-confidence suspicious patterns

### Supply-chain anomalies
- Brand-new package with mature-sounding name
- Sudden maintainer transfer or unusual release burst
- Package name one character away from a popular dependency
- Mixed public/private registry hints suggesting dependency confusion

### Unsafe automation patterns
- GitHub Actions not pinned to full SHA
- Secrets broadly exposed to third-party actions
- Installer scripts that fetch binaries from arbitrary URLs

### Human-targeted social engineering
- README tells user to bypass warnings, disable security features, or paste opaque terminal commands
- Skill instructions try to persuade the agent to skip review or ignore policy

## Usually benign but worth checking

- telemetry endpoints with clear product context
- minified bundles from normal frontend builds
- native modules used by clearly documented features
- install scripts used only for compilation with transparent source


path: report-template.md
content: # Review Report Template

## Target
- Name:
- Source:
- Version / Tag / Commit:
- Ecosystem:

## Source Triage
- Provenance:
- Trust signals:
- Risk signals:
- Preliminary verdict:

## Pre-Download Findings
- [severity] file or page: finding

## Post-Download Findings
- [severity] path: finding

## Verdict
- PASS / CAUTION / REVIEW_REQUIRED / BLOCK

## Why
- 1.
- 2.
- 3.

## Safe Next Step
- Proceed with install / Proceed only in isolated environment / Hold for manual review / Block installation


path: README.md
content: # Security Audit Tools v2.0

Professional security audit toolkit for npm packages and GitHub repositories.

## 🎯 Recommended Method: Option A (Quick Scan + Deep Review)

**Best Practice**: 5-second Quick Scan + 1-hour Deep Review of critical files.

### Workflow

1. **Quick Scan** (5 seconds) – 100% code coverage
```bash
python3 tools/quick_scan.py <target_directory>

```


2. **Deep Review** (1 hour) – Line-by-line inspection of key files:
* **Wallet Services** (Private key handling)
* **Network Clients** (External APIs)
* **Transaction Services** (Fund operations)
* **Entry Points** (Overall architecture)


3. **Comprehensive Assessment** (5 minutes) – Quantitative reporting

**Advantages**:

* ✅ **Controllable Time** (1 hour vs. 10 hours)
* ✅ **High Reliability** (9/10)
* ✅ **Detects Logical Vulnerabilities**
* ✅ **100% Code Coverage** (via Quick Scan)

---

## Tools

### 1. quick_scan.py (New) ⚡

**Ultra-fast Security Scanner** – Scans all code in 5 seconds.

**Usage**:

```bash
python3 tools/quick_scan.py <path-to-source>

```

**Example**:

```bash
# Scan npm package
python3 tools/quick_scan.py ./npm-extract/package/src

# Scan GitHub repo
python3 tools/quick_scan.py ./github-repo/src

```

**Detection Items** (10 Critical Patterns):

1. ✅ Dynamic code execution (`eval`/`Function`)
2. ✅ Process creation (`exec`/`spawn`)
3. ✅ File operations (`fs.read`/`write`)
4. ✅ Suspicious network requests (Non-official domains)
5. ✅ Hardcoded private keys
6. ✅ Hardcoded Secrets/Tokens
7. ✅ Obfuscated code (`hex`/`base64`)
8. ✅ Data exfiltration patterns
9. ✅ Command injection
10. ✅ Dangerous npm packages (`shelljs`/`sudo`)

**Output**:

```text
Files scanned: 53
Lines scanned: 32,789
Total findings: 0
Risk score: 0/100
✅ LOW RISK - Safe to install

```

**Risk Scoring**:

* **0-39**: ✅ LOW RISK (Safe to install)
* **40-69**: ⚠️ MEDIUM RISK (Manual review required)
* **70-100**: ❌ HIGH RISK (Installation prohibited)

**Key Features**:

* ✅ **Extremely Fast**: Scans 30,000+ lines in 5 seconds.
* ✅ **100% Coverage**: Comprehensive code analysis.
* ✅ **Automated**: Detects 10 types of malicious patterns.
* ✅ **Quantitative**: Provides a risk score (0-100).
* ✅ **Reportable**: Supports JSON output.

---

### 2. audit-npm-package.sh

Automated NPM package audit tool.

**Usage**:

```bash
./tools/audit-npm-package.sh <package-name> <version>

```

**Example**:

```bash
./tools/audit-npm-package.sh @catalyst-team/poly-sdk 0.5.0

```

**What it does**:

1. Fetches NPM metadata.
2. Downloads and extracts the package.
3. Clones GitHub repository (if available).
4. Compares NPM package content with GitHub source.
5. Verifies integrity hashes.

**Output**: Creates an `audit-<package>-<version>/` directory containing all audit data.

---

### 3. audit-git-history.sh

Deep Git history analysis tool.

**Usage**:

```bash
./tools/audit-git-history.sh <path-to-git-repo>

```

**What it does**:

1. **Contributor Analysis**: Detects suspicious emails/identities.
2. **Timeline Analysis**: Monitors commit frequency and anomalies.
3. **Large Change Detection**: Identifies mass refactors or hidden injections.
4. **Suspicious Patterns**: Flags `eval`, `exec`, and `spawn` in history.
5. **Recent Changes**: Analyzes the latest commits for high-risk updates.

---

### 4. audit-source-code.sh

Deep source code analysis tool.

**Usage**:

```bash
./tools/audit-source-code.sh <path-to-source>

```

**What it does**:

1. **Statistics**: File counts and lines of code.
2. **Network**: Analyzes external request patterns.
3. **File System**: Detects sensitive I/O operations.
4. **Processes**: Monitors shell command execution.
5. **Dynamic Execution**: Identifies `eval()` or `new Function()`.
6. **Environment Variables**: Tracks sensitive data usage.
7. **Secrets**: Patterns for encryption keys and secrets.
8. **Obfuscation**: Identifies minified or encoded malicious payloads.
9. **Wallets**: Searches for crypto wallet/key patterns.
10. **Imports**: Comprehensive dependency analysis.

---

## Complete Audit Workflow (Updated)

### Recommended Workflow: Option A

**The fastest and most reliable approach**:

```bash
# Step 1: Quick Scan (5 seconds)
python3 tools/quick_scan.py ./audit-package-0.5.0/npm-extract/package/src

# Step 2: If LOW RISK, proceed to analyze key metadata
# Use helper tools:
./tools/audit-git-history.sh ./audit-package-0.5.0/github-repo
./tools/audit-source-code.sh ./audit-package-0.5.0/npm-extract/package/src

# Step 3: Manual Deep Review (1 hour)
# Focus on: Wallets, Networking, Transactions, and Entry Files.

# Step 4: Final assessment and report generation.

```

**Total Time**: ~1 Hour

**Reliability**: 9/10

---

### Traditional Workflow (Legacy/Backup)

1. **Run NPM Package Audit**:
`./tools/audit-npm-package.sh <name> <version>`
2. **Analyze Git History**:
`./tools/audit-git-history.sh ./github-repo`
3. **Analyze Source Code**:
`./tools/audit-source-code.sh ./src`
4. **Manual Deep Dive**:
Use tool outputs to review network requests, encryption, and key handling.

**Total Time**: ~2-3 Hours

---

## Quick Reference

### Tool Comparison

| Tool | Time | Coverage | Detection Type | Primary Use |
| --- | --- | --- | --- | --- |
| **quick_scan.py** | 5s | 100% | Obvious Malice | Initial Screening |
| audit-npm-package.sh | 30s | NPM Meta | Package Integrity | Verification |
| audit-git-history.sh | 10s | Git History | Suspicious Commits | Historical Audit |
| audit-source-code.sh | 10s | 100% | 9-Dimension Scan | Pattern Scanning |
| **Manual Review** | 1h | 10% | Logical Flaws | Code Comprehension |

### Use Cases

* **Scenario 1: Daily NPM Review**
Run `quick_scan.py`. If **LOW RISK**, install. If **MEDIUM/HIGH**, escalate.
* **Scenario 2: Critical Dependency Audit**
Follow **Option A** complete workflow + 1 hour manual review.
* **Scenario 3: Suspicious Code Investigation**
Use `audit-source-code.sh` to locate "HIGH/CRITICAL" hits and verify manually.

---

## Requirements

* **Node.js**: npm, node
* **Git**: git CLI
* **Unix tools**: shasum, grep, awk, sed, jq
* **Python**: python3 (for Quick Scan)

---

## Limitations

**These tools do NOT replace**:

* Professional third-party security audits.
* Dynamic behavior analysis (Docker sandboxing).
* Real-time network monitoring (Charles/Fiddler).
* Known malicious code database similarity checks.

**They DO provide**:

* ✅ Automated initial screening.
* ✅ Pattern-based detection.
* ✅ Risk scoring and prioritization.
* ✅ Focused guidance for manual reviewers.

---

## Version History

### v2.0 (2026-03-02)

* ✅ Added `quick_scan.py` - Ultra-fast security scanner.
* ✅ Introduced **Option A** workflow (Quick Scan + Deep Review).
* ✅ Optimized audit flow (1 hour vs. 10 hours).

### v1.0 (2026-03-02)

* ✅ Initial release with 3 core audit scripts.

---

**Current Version**: v2.0

**Last Updated**: 2026-03-02 21:48

**Author**: Anonymvs1234



path: tools/audit-git-history.sh
content: #!/bin/bash
# Git History Deep Analysis Script
# Usage: ./audit-git-history.sh /path/to/repo

set -e

REPO_DIR=$1

if [ -z "$REPO_DIR" ]; then
    echo "Usage: $0 <path-to-git-repo>"
    echo "Example: $0 ./github-repo"
    exit 1
fi

cd "$REPO_DIR"

echo "=== Git History Deep Analysis ==="
echo "Repository: $(pwd)"
echo ""

# 1. Contributors analysis
echo "[1/5] Analyzing contributors..."
echo "## All Contributors"
git log --format="%an <%ae>" | sort | uniq -c | sort -rn | head -20

echo ""
echo "## Suspicious Email Domains"
SUSPICIOUS_DOMAINS="temp|disposable|guerrilla|10minutemail|throwaway"
git log --format="%ae" | grep -iE "$SUSPICIOUS_DOMAINS" || echo "✅ No suspicious email domains found"

# 2. Timeline analysis
echo ""
echo "[2/5] Analyzing timeline..."
echo "## Recent Commits (Last 50)"
git log --oneline --graph --all -50 --date=short --format="%h %ad %an %s"

echo ""
echo "## Commit Frequency by Author"
git shortlog -sn --all

# 3. Large changes detection
echo ""
echo "[3/5] Detecting large changes..."
echo "## Commits with >100 files changed"
git log --all --oneline --stat | grep -B1 -E "files? changed.*[1-9][0-9]{2,}" | head -20 || echo "✅ No large-scale changes found"

echo ""
echo "## Commits with >1000 insertions"
git log --all --oneline --stat | grep -B1 -E "insertions.*[1-9][0-9]{3,}" | head -20 || echo "✅ No massive insertions found"

# 4. Suspicious patterns
echo ""
echo "[4/5] Detecting suspicious patterns..."
echo "## Dynamic Code Execution"
git log -p --all | grep -E "^\+.*\beval\b|^\+.*new Function\(" | head -10 || echo "✅ No eval/Function() found"

echo ""
echo "## Process Creation"
git log -p --all | grep -E "^\+.*child_process|^\+.*\.exec\(|^\+.*\.spawn\(" | head -10 || echo "✅ No process creation found"

echo ""
echo "## File System Access"
git log -p --all | grep -E "^\+.*fs\.read|^\+.*fs\.write|^\+.*readFile|^\+.*writeFile" | head -10 || echo "✅ No file system access found"

echo ""
echo "## Network Requests"
git log -p --all | grep -E "^\+.*https?://" | grep -v "^\+.*#" | grep -v "^\+.*polymarket\|github\|example\.com" | head -10 || echo "✅ No suspicious network requests found"

# 5. Recent changes analysis
echo ""
echo "[5/5] Analyzing recent changes..."
LATEST_COMMIT=$(git log -1 --format="%H")
echo "## Latest Commit: $LATEST_COMMIT"
git show $LATEST_COMMIT --stat

echo ""
echo "## Commits in Last 7 Days"
RECENT_COUNT=$(git log --oneline --since="7 days ago" | wc -l)
echo "Found $RECENT_COUNT commits in the last 7 days"

if [ $RECENT_COUNT -gt 20 ]; then
    echo "⚠️  High commit frequency detected"
    git log --oneline --since="7 days ago"
fi

# Summary
echo ""
echo "=== Analysis Summary ==="
TOTAL_COMMITS=$(git rev-list --all --count)
TOTAL_AUTHORS=$(git log --format="%an" | sort -u | wc -l)
TOTAL_EMAILS=$(git log --format="%ae" | sort -u | wc -l)

echo "Total commits: $TOTAL_COMMITS"
echo "Total authors: $TOTAL_AUTHORS"
echo "Total emails: $TOTAL_EMAILS"
echo ""
echo "Risk indicators:"
echo "- Multiple suspicious domains: Check for temp emails"
echo "- Large changes: Investigate mass refactors"
echo "- High frequency: Possible automation or rushed development"
echo ""
echo "✅ Analysis complete"


path: tools/audit-source-code.sh
content: #!/bin/bash
# Source Code Deep Analysis Script
# Usage: ./audit-source-code.sh /path/to/src

set -e

SRC_DIR=$1

if [ -z "$SRC_DIR" ]; then
    echo "Usage: $0 <path-to-source-directory>"
    echo "Example: $0 ./npm-extract/package/src"
    exit 1
fi

cd "$SRC_DIR"

echo "=== Source Code Deep Analysis ==="
echo "Source directory: $(pwd)"
echo ""

# 0. Statistics
echo "[0/9] Gathering statistics..."
TOTAL_FILES=$(find . -name "*.ts" -o -name "*.js" | wc -l | tr -d ' ')
TOTAL_LINES=$(find . -name "*.ts" -o -name "*.js" | xargs wc -l 2>/dev/null | tail -1 | awk '{print $1}')

echo "Total TypeScript/JavaScript files: $TOTAL_FILES"
echo "Total lines of code: $TOTAL_LINES"
echo ""

# 1. Network requests
echo "[1/9] Analyzing network requests..."
NETWORK_COUNT=$(grep -r "https://\|http://" . --include="*.ts" --include="*.js" 2>/dev/null | grep -v "test\|example\|\.d\.ts" | wc -l | tr -d ' ')

echo "Found $NETWORK_COUNT network request patterns"
echo ""

if [ $NETWORK_COUNT -gt 0 ]; then
    echo "## Network Requests (excluding common domains)"
    grep -rn "https://\|http://" . --include="*.ts" --include="*.js" 2>/dev/null | \
        grep -v "test\|example\|\.d\.ts" | \
        grep -v "polymarket\|github\|example\.com\|localhost\|127\.0\.0\.1" | \
        head -20 || echo "✅ Only known domains found"
    echo ""
fi

# 2. File operations
echo "[2/9] Analyzing file operations..."
FILE_OPS=$(grep -r "fs\.read\|fs\.write\|readFile\|writeFile\|readdir" . --include="*.ts" --include="*.js" 2>/dev/null | wc -l | tr -d ' ')

echo "Found $FILE_OPS file operation patterns"
echo ""

if [ $FILE_OPS -gt 0 ]; then
    echo "## File Operations"
    grep -rn "fs\.read\|fs\.write\|readFile\|writeFile" . --include="*.ts" --include="*.js" 2>/dev/null | head -20
    echo ""
fi

# 3. Process creation
echo "[3/9] Analyzing process creation..."
PROCESS_COUNT=$(grep -r "child_process\|exec(\|spawn(\|execSync" . --include="*.ts" --include="*.js" 2>/dev/null | grep -v "test\|example\|\.d\.ts\|execute.*rate" | wc -l | tr -d ' ')

echo "Found $PROCESS_COUNT process creation patterns"
echo ""

if [ $PROCESS_COUNT -gt 0 ]; then
    echo "## Process Creation (excluding normal usage)"
    grep -rn "child_process\|exec(\|spawn(" . --include="*.ts" --include="*.js" 2>/dev/null | \
        grep -v "test\|example\|\.d\.ts\|execute.*rate" | \
        head -20
    echo ""
fi

# 4. Dynamic code execution
echo "[4/9] Analyzing dynamic code execution..."
DYNAMIC_COUNT=$(grep -r "\beval\b\|new Function\|Function(" . --include="*.ts" --include="*.js" 2>/dev/null | grep -v "test\|example\|\.d\.ts" | wc -l | tr -d ' ')

echo "Found $DYNAMIC_COUNT dynamic code execution patterns"
echo ""

if [ $DYNAMIC_COUNT -gt 0 ]; then
    echo "## Dynamic Code Execution (CRITICAL)"
    grep -rn "\beval\b\|new Function\|Function(" . --include="*.ts" --include="*.js" 2>/dev/null | \
        grep -v "test\|example\|\.d\.ts" | \
        head -20
    echo ""
    echo "⚠️  WARNING: Dynamic code execution detected - requires manual review"
    echo ""
fi

# 5. Environment variables
echo "[5/9] Analyzing environment variable access..."
ENV_COUNT=$(grep -r "process\.env\." . --include="*.ts" --include="*.js" 2>/dev/null | grep -v "test\|example\|\.d\.ts\|comment" | wc -l | tr -d ' ')

echo "Found $ENV_COUNT environment variable accesses"
echo ""

if [ $ENV_COUNT -gt 0 ]; then
    echo "## Environment Variables Used"
    grep -rn "process\.env\." . --include="*.ts" --include="*.js" 2>/dev/null | \
        grep -v "test\|example\|\.d\.ts" | \
        sed 's/process\.env\.//g' | \
        awk -F: '{print $1 ":" $2 ":" $3}' | \
        sort -u | \
        head -20
    echo ""
fi

# 6. Encryption and secrets
echo "[6/9] Analyzing encryption and secrets..."
CRYPTO_COUNT=$(grep -r "crypto\|encrypt\|decrypt\|private.*key\|secret\|password" . --include="*.ts" --include="*.js" 2>/dev/null | grep -v "test\|example\|\.d\.ts\|comment" | wc -l | tr -d ' ')

echo "Found $CRYPTO_COUNT encryption/secret-related patterns"
echo ""

if [ $CRYPTO_COUNT -gt 0 ]; then
    echo "## Encryption and Secrets (requires manual review)"
    grep -rn "crypto\|encrypt\|private.*key" . --include="*.ts" --include="*.js" 2>/dev/null | \
        grep -v "test\|example\|\.d\.ts" | \
        head -20
    echo ""
fi

# 7. Obfuscation patterns
echo "[7/9] Detecting obfuscation..."
OBFUSCATION=$(grep -r "\\x[0-9a-f]\{2\}\|atob\|btoa\|Buffer.*base64\|split.*join" . --include="*.ts" --include="*.js" 2>/dev/null | grep -v "test\|example\|\.d\.ts" | wc -l | tr -d ' ')

echo "Found $OBFUSCATION potential obfuscation patterns"
echo ""

if [ $OBFUSCATION -gt 50 ]; then
    echo "⚠️  WARNING: High obfuscation score - requires manual review"
    echo ""
fi

# 8. Wallet/Key patterns
echo "[8/9] Detecting wallet/key patterns..."
WALLET_COUNT=$(grep -r "wallet\|mnemonic\|seed.*phrase\|keystore\|private.*key" . --include="*.ts" --include="*.js" 2>/dev/null | grep -v "test\|example\|\.d\.ts\|comment" | wc -l | tr -d ' ')

echo "Found $WALLET_COUNT wallet/key-related patterns"
echo ""

if [ $WALLET_COUNT -gt 0 ]; then
    echo "## Wallet/Key Patterns (requires manual review)"
    grep -rn "private.*key\|mnemonic\|seed.*phrase" . --include="*.ts" --include="*.js" 2>/dev/null | \
        grep -v "test\|example\|\.d\.ts\|comment" | \
        head -20
    echo ""
fi

# 9. Import analysis
echo "[9/9] Analyzing imports..."
echo "## Most imported packages"
grep -rh "^import.*from\|require(" . --include="*.ts" --include="*.js" 2>/dev/null | \
    grep -oE "'[^']+'" | \
    tr -d "'" | \
    sort | \
    uniq -c | \
    sort -rn | \
    head -20

echo ""
echo "## External package dependencies"
grep -rh "^import.*from\|require(" . --include="*.ts" --include="*.js" 2>/dev/null | \
    grep -oE "'@[^/]+" | \
    tr -d "'" | \
    sort | \
    uniq -c | \
    sort -rn | \
    head -10

# Summary
echo ""
echo "=== Analysis Summary ==="
echo "Files analyzed: $TOTAL_FILES"
echo "Lines of code: $TOTAL_LINES"
echo ""
echo "Security Indicators:"
echo "- Network requests: $NETWORK_COUNT"
echo "- File operations: $FILE_OPS"
echo "- Process creation: $PROCESS_COUNT"
echo "- Dynamic code: $DYNAMIC_COUNT $(if [ $DYNAMIC_COUNT -gt 0 ]; then echo '⚠️ CRITICAL'; else echo '✅'; fi)"
echo "- Environment vars: $ENV_COUNT"
echo "- Crypto/secrets: $CRYPTO_COUNT"
echo "- Obfuscation score: $OBFUSCATION $(if [ $OBFUSCATION -gt 50 ]; then echo '⚠️ WARNING'; else echo '✅'; fi)"
echo "- Wallet/key patterns: $WALLET_COUNT"
echo ""

# Risk assessment
RISK_SCORE=0
[ $DYNAMIC_COUNT -gt 0 ] && RISK_SCORE=$((RISK_SCORE + 30))
[ $PROCESS_COUNT -gt 5 ] && RISK_SCORE=$((RISK_SCORE + 20))
[ $OBFUSCATION -gt 50 ] && RISK_SCORE=$((RISK_SCORE + 20))
[ $WALLET_COUNT -gt 10 ] && RISK_SCORE=$((RISK_SCORE + 15))

echo "Risk Score: $RISK_SCORE/100"
if [ $RISK_SCORE -ge 50 ]; then
    echo "⚠️  HIGH RISK - Manual review required"
elif [ $RISK_SCORE -ge 30 ]; then
    echo "⚠️  MEDIUM RISK - Review recommended"
else
    echo "✅ LOW RISK"
fi

echo ""
echo "✅ Analysis complete"


path: tools/audit-npm-package.sh
content: #!/bin/bash
# NPM Package Automated Audit Script
# Usage: ./audit-npm-package.sh package-name version

set -e

PACKAGE=$1
VERSION=$2

if [ -z "$PACKAGE" ] || [ -z "$VERSION" ]; then
    echo "Usage: $0 <package-name> <version>"
    echo "Example: $0 @catalyst-team/poly-sdk 0.5.0"
    exit 1
fi

PACKAGE_SAFE=$(echo $PACKAGE | tr '/' '-')
OUTPUT_DIR="audit-${PACKAGE_SAFE}-${VERSION}"

echo "=== NPM Package Audit: $PACKAGE@$VERSION ==="
echo "Output directory: $OUTPUT_DIR"
echo ""

# Create output directory
mkdir -p "$OUTPUT_DIR"
cd "$OUTPUT_DIR"

# 1. Get NPM metadata
echo "[1/6] Fetching NPM metadata..."
npm view $PACKAGE@$VERSION --json > npm-metadata.json
echo "✅ Saved to npm-metadata.json"

# 2. Download NPM package
echo ""
echo "[2/6] Downloading NPM package..."
npm pack $PACKAGE@$VERSION
TARBALL=$(ls *.tgz | head -1)
echo "✅ Downloaded: $TARBALL"

# 3. Extract package
echo ""
echo "[3/6] Extracting package..."
mkdir -p npm-extract
tar -xzf $TARBALL -C npm-extract
FILE_COUNT=$(find npm-extract -type f | wc -l)
echo "✅ Extracted $FILE_COUNT files"

# 4. Clone GitHub repository (if available)
echo ""
echo "[4/6] Checking GitHub repository..."
REPO_URL=$(cat npm-metadata.json | jq -r '.repository.url // empty')

if [ -n "$REPO_URL" ]; then
    # Convert git+https to https
    REPO_URL=$(echo $REPO_URL | sed 's/git+https/https/' | sed 's/\.git$//')
    echo "Found repository: $REPO_URL"

    git clone --depth 50 $REPO_URL github-repo 2>&1 | grep -E "Cloning|Receiving" || echo "Repository clone failed or not needed"
else
    echo "⚠️  No GitHub repository found in package metadata"
fi

# 5. Compare NPM vs GitHub (if available)
echo ""
echo "[5/6] Comparing NPM package with GitHub source..."
if [ -d "github-repo" ]; then
    # Compare package.json
    echo "Comparing package.json scripts..."
    NPM_SCRIPTS=$(cat npm-extract/package/package.json | jq -S '.scripts')
    GITHUB_SCRIPTS=$(cat github-repo/package.json | jq -S '.scripts')

    if [ "$NPM_SCRIPTS" == "$GITHUB_SCRIPTS" ]; then
        echo "✅ Scripts match"
    else
        echo "❌ Scripts MISMATCH:"
        diff <(echo "$NPM_SCRIPTS") <(echo "$GITHUB_SCRIPTS")
    fi

    # Count differences
    DIFF_COUNT=$(diff -r npm-extract/package github-repo 2>/dev/null | grep -c "^diff" || echo "0")
    echo "Found $DIFF_COUNT differences"
else
    echo "⚠️  Skipping comparison (no GitHub repo)"
fi

# 6. Verify integrity
echo ""
echo "[6/6] Verifying integrity..."
EXPECTED_HASH=$(cat npm-metadata.json | jq -r '.dist.integrity')
echo "Expected integrity: $EXPECTED_HASH"

# Calculate actual hash
if command -v shasum &> /dev/null; then
    ACTUAL_HASH=$(shasum -a 512 $TARBALL | awk '{print $1}')
    ACTUAL_HASH_ALGO="sha512"

    if echo "$EXPECTED_HASH" | grep -q "$ACTUAL_HASH"; then
        echo "✅ Integrity verified"
    else
        echo "⚠️  Integrity hash mismatch"
        echo "Expected: $EXPECTED_HASH"
        echo "Actual:   $ACTUAL_HASH_ALGO-$ACTUAL_HASH"
    fi
else
    echo "⚠️  shasum not available, skipping integrity check"
fi

# Summary
echo ""
echo "=== Audit Summary ==="
echo "Package: $PACKAGE@$VERSION"
echo "Files: $FILE_COUNT"
echo "Size: $(du -sh npm-extract | cut -f1)"
echo ""
echo "Next steps:"
echo "1. Review npm-metadata.json"
echo "2. Inspect files in npm-extract/"
echo "3. Run source code analysis tools"
echo "4. Check Git history (if GitHub repo available)"
echo ""
echo "Output saved to: $OUTPUT_DIR/"


path: tools/quick_scan.py
content: #!/usr/bin/env python3
"""
Quick Security Scanner - 5分钟扫描所有代码
仅检测明显恶意模式，非深度审查
"""

import os
import re
import json
from pathlib import Path
from collections import defaultdict

class QuickSecurityScanner:
    def __init__(self, target_dir):
        self.target_dir = Path(target_dir)
        self.findings = defaultdict(list)
        self.stats = {
            'files_scanned': 0,
            'lines_scanned': 0,
            'total_findings': 0
        }
    
    def scan_all(self):
        """扫描所有文件"""
        for ext in ['*.ts', '*.js', '*.tsx', '*.jsx']:
            for file in self.target_dir.rglob(ext):
                if 'node_modules' in str(file) or '__tests__' in str(file):
                    continue
                self.scan_file(file)
        
        return self.generate_report()
    
    def scan_file(self, file_path):
        """扫描单个文件"""
        self.stats['files_scanned'] += 1
        
        try:
            with open(file_path, 'r', encoding='utf-8') as f:
                lines = f.readlines()
                self.stats['lines_scanned'] += len(lines)
                
                for line_num, line in enumerate(lines, 1):
                    self.check_patterns(file_path, line_num, line)
        except Exception as e:
            self.findings['errors'].append(f"{file_path}: {str(e)}")
    
    def check_patterns(self, file_path, line_num, line):
        """检查恶意模式"""
        
        # 1. 动态代码执行
        if re.search(r'\beval\s*\(|new\s+Function\s*\(', line):
            self.add_finding('CRITICAL', 'Dynamic Code Execution', file_path, line_num, line.strip())
        
        # 2. 进程创建
        if re.search(r'child_process|\.exec\s*\(|\.spawn\s*\(|execSync|spawnSync', line):
            self.add_finding('HIGH', 'Process Creation', file_path, line_num, line.strip())
        
        # 3. 文件系统操作
        if re.search(r'fs\.read|fs\.write|readFile|writeFile|unlink|rmdir', line):
            self.add_finding('MEDIUM', 'File Operation', file_path, line_num, line.strip())
        
        # 4. 网络请求（可疑域名）
        if re.search(r'https?://', line):
            if not re.search(r'polymarket|github|example|localhost|127\.0\.0\.1|polygon-rpc|etherscan|goldsky', line):
                self.add_finding('HIGH', 'Suspicious Network Request', file_path, line_num, line.strip())
        
        # 5. 硬编码私钥
        if re.search(r'private[_-]?key\s*[=:]\s*["\']0x[a-f0-9]{64}["\']', line, re.I):
            self.add_finding('CRITICAL', 'Hardcoded Private Key', file_path, line_num, line.strip())
        
        # 6. 硬编码密钥
        if re.search(r'(api[_-]?key|secret|token)\s*[=:]\s*["\'][a-zA-Z0-9]{20,}["\']', line, re.I):
            if 'process.env' not in line:
                self.add_finding('HIGH', 'Hardcoded Secret', file_path, line_num, line.strip())
        
        # 7. 混淆代码
        if re.search(r'\\x[0-9a-f]{2}{10,}|atob\(|btoa\(|Buffer.*from.*base64', line):
            self.add_finding('MEDIUM', 'Obfuscation Detected', file_path, line_num, line.strip())
        
        # 8. 数据外泄模式
        if re.search(r'(fetch|axios|http\.get|http\.post).*\+.*private', line, re.I):
            self.add_finding('CRITICAL', 'Potential Data Exfiltration', file_path, line_num, line.strip())
        
        # 9. 命令注入
        if re.search(r'\$\s*\{.*\}.*exec|`.*`.*exec', line):
            self.add_finding('CRITICAL', 'Command Injection', file_path, line_num, line.strip())
        
        # 10. 危险的npm包
        if re.search(r'require\s*\(\s*["\'](?:shelljs|node-pty|sudo)', line):
            self.add_finding('HIGH', 'Dangerous Package', file_path, line_num, line.strip())
    
    def add_finding(self, severity, category, file_path, line_num, line):
        """添加发现"""
        self.findings[severity].append({
            'category': category,
            'file': str(file_path.relative_to(self.target_dir)),
            'line': line_num,
            'code': line[:100]  # 截断
        })
        self.stats['total_findings'] += 1
    
    def generate_report(self):
        """生成报告"""
        report = {
            'stats': self.stats,
            'findings': dict(self.findings),
            'risk_score': self.calculate_risk_score()
        }
        return report
    
    def calculate_risk_score(self):
        """计算风险分数（0-100）"""
        score = 0
        score += len(self.findings.get('CRITICAL', [])) * 30
        score += len(self.findings.get('HIGH', [])) * 15
        score += len(self.findings.get('MEDIUM', [])) * 5
        score += len(self.findings.get('LOW', [])) * 1
        return min(100, score)

def main():
    import sys
    
    if len(sys.argv) < 2:
        print("Usage: python quick_security_scan.py <target_directory>")
        sys.exit(1)
    
    target_dir = sys.argv[1]
    
    print(f"=== Quick Security Scan ===")
    print(f"Target: {target_dir}")
    print(f"Scanning all code...\n")
    
    scanner = QuickSecurityScanner(target_dir)
    report = scanner.scan_all()
    
    # 打印统计
    print(f"Files scanned: {report['stats']['files_scanned']}")
    print(f"Lines scanned: {report['stats']['lines_scanned']:,}")
    print(f"Total findings: {report['stats']['total_findings']}")
    print(f"Risk score: {report['risk_score']}/100\n")
    
    # 打印发现
    for severity in ['CRITICAL', 'HIGH', 'MEDIUM', 'LOW']:
        if severity in report['findings'] and report['findings'][severity]:
            print(f"\n{severity}:")
            for finding in report['findings'][severity][:10]:  # 只显示前10个
                print(f"  [{finding['category']}] {finding['file']}:{finding['line']}")
                print(f"    {finding['code']}")
    
    # 保存完整报告
    with open('/tmp/security_scan_report.json', 'w') as f:
        json.dump(report, f, indent=2)
    
    print(f"\nFull report saved to: /tmp/security_scan_report.json")
    
    # 返回风险等级
    if report['risk_score'] >= 70:
        print("\n❌ HIGH RISK - Do not install")
        sys.exit(1)
    elif report['risk_score'] >= 40:
        print("\n⚠️  MEDIUM RISK - Manual review required")
        sys.exit(0)
    else:
        print("\n✅ LOW RISK - Safe to install")
        sys.exit(0)

if __name__ == '__main__':
    main()


Security scan verdicts — ClawScan: suspicious | static analysis: suspicious | VirusTotal: clean | SkillSpector: suspicious (CRITICAL)