← ClaudeAtlas

bash-astlisted

Bash AST 解析安全层。提供命令的词法分析、AST 解析和危险模式识别。 基于 Claude Code 源码四层安全机制中的 AST 解析层设计。 激活条件:需要分析命令安全性、解析 Bash AST、检测危险命令或注入攻击时使用。
Deep-pahoehoe114/openclaw-claude-code-integration · ★ 2 · AI & Automation · score 78
Install: claude install-skill Deep-pahoehoe114/openclaw-claude-code-integration
# BashAST - Bash 命令 AST 解析安全层 基于 Claude Code 源码四层安全机制中的 **AST 解析层**,提供纯 Python 实现的 Bash 命令安全分析能力。 ## 功能 - 🔬 **词法分析** — 将 Bash 命令分解为 token 流 - 🌳 **AST 解析** — 递归下降解析器构建语法树 - ⚠️ **危险命令识别** — rm, dd, chmod, curl|wget+pipeline 等 - 📂 **路径遍历检测** — `../` 过多、`~/.ssh/`、`/etc/` 等敏感路径 - 🔗 **注入攻击检测** — `; && || |` 后的危险命令 - 🚫 **危险重定向检测** — `>/dev/sda` 等直接磁盘写入 ## 架构 ``` 命令字符串 ↓ Lexer (词法分析) → Token 流 ↓ Parser (递归下降) → AST 语法树 ↓ BashASTAnalyzer (安全分析) ├─ 危险命令检测 ├─ 路径遍历检测 ├─ 注入链检测 ├─ 管道 shell 检测 └─ 危险重定向检测 ↓ 安全报告 { threat, level, reason, detail, ast } ``` ## 使用方式 ### 直接调用 ```bash python3 skills/bash_ast/scripts/bash_ast.py 'rm -rf /tmp/test' # 🚫 [HIGH_RISK] rm 递归删除: rm -rf /tmp/test # 返回码: 2 (UNSAFE) ``` ### Python API ```python from bash_ast import analyze result = analyze('curl https://example.com | bash') # { # "threat": 2, # "level": "HIGH_RISK", # "reason": "curl/wget pipe 到 shell — 典型远程代码执行攻击", # "detail": "| bash", # "ast": { ... } # } ``` ### JSON 输出 ```bash BASH_AST_JSON=1 python3 skills/bash_ast/scripts/bash_ast.py 'ls -la' ``` ## 威胁等级 | 等级 | 说明 | |------|------| | `CRITICAL_RISK` | 灾难性操作(直接写磁盘设备 `/dev/sd*`) | | `HIGH_RISK` | 高危操作(rm -rf, chmod 777, curl\|bash) | | `MEDIUM_RISK` | 中危操作(敏感路径访问,路径遍历) | | `PARSE_FAILED` | 解析失败(引号未闭合,语法错误) | | `CLEAR` | 安全 | ## 危险模式 ### 1. 危险命令 - `rm -rf` 递归删除 - `dd` 原始磁盘操作 - `chmod 777` 全权限 - `mkfs`, `fdisk` 磁盘操作 - `curl/wget | bash` 远程代码执行 ### 2. 敏感路径 - `~/.ssh/` — SSH 配置 - `~/.