Igfray/pantheon-ssrf-guard

GitHub: Igfray/pantheon-ssrf-guard

专为 Python 标准库 HTTP 客户端设计的轻量级 SSRF 出站防护库,通过双层校验机制有效抵御 DNS rebinding 攻击。

Stars: 0 | Forks: 0

# pantheon-ssrf-guard [![测试](https://static.pigsec.cn/wp-content/uploads/repos/cas/6b/6b52945adbf8d9e421fe243515ae54cfbd3da263f16b1eabda37cdc0b797b8eb.svg)](https://github.com/Igfray/pantheon-ssrf-guard/actions/workflows/ci.yml) [![PyPI](https://img.shields.io/pypi/v/pantheon-ssrf-guard)](https://pypi.org/project/pantheon-ssrf-guard/) [![Python](https://img.shields.io/badge/python-3.10%2B-blue)](https://pypi.org/project/pantheon-ssrf-guard/) [![许可证](https://img.shields.io/badge/license-Apache%202.0-blue)](LICENSE) 一个轻量级、无依赖的 **SSRF 出站防护**库,专为 Python 标准库 HTTP 客户端设计——它能够**防御 DNS rebinding**。 提取自 [PANTHEON](https://pantheonlabs.co.uk),一个多租户 AI 基础架构。在那里,它是每条出站路径(链接导入、上传的 URL 获取、外部 MCP 服务器传输)上唯一的标准防护。 ## 为什么仅靠预检查是不够的 常见的“封锁内部主机”方法会在连接前解析主机名,并拒绝私有 IP。攻击者可以通过 **DNS rebinding** 绕过它:在你检查时,域名解析为一个公共 IP,而当 socket 实际开启时,它却解析到了 `127.0.0.1` / `169.254.169.254`(云元数据)/ 或某个内部服务。 `pantheon-ssrf-guard` 使用了**双层防护**: 1. **`host_is_public(host)`** —— 快速预检查:如果*任何*解析出的地址不是公共的,则拒绝。 2. **`GuardedHTTP(S)Connection`** —— 在**连接时**,重新检查 socket *实际到达*的 IP。这是预检查无法提供的层,也是它能够消除 rebind 时间差的关键。 该分类器的兜底逻辑是 `not ip.is_global`,因此它还会拒绝所有明显标志遗漏的内容——包括 RFC 6598 共享/CGN 空间(`100.64.0.0/10`,被 Kubernetes/内部负载均衡器以及阿里云/腾讯云/Oracle 的元数据端点 `100.100.100.200` 使用),以及基准测试、文档和 NAT64 地址段。 ## 安装 ``` pip install pantheon-ssrf-guard # or: copy the single ssrf_guard.py file into your project ``` ## 使用 ``` from ssrf_guard import guarded_opener, SsrfBlocked opener = guarded_opener() # https-capable; redirects refused by default (each hop re-checked if allowed) try: with opener.open("https://example.com/data.json", timeout=10) as resp: body = resp.read() except SsrfBlocked: ... # the target resolved (or rebound) to a non-public address — refused before any data was sent ``` 如果你正在构建自己的客户端,我们也导出了更底层的组件: ``` from ssrf_guard import host_is_public, ip_is_public, peer_is_public from ssrf_guard import GuardedHTTPHandler, GuardedHTTPSHandler ``` ## 它*不能*做什么 - 它不提供代理、缓存或限流功能——它只拒绝非公共目的地。 - 它只保护 `urllib`/`http.client`。对于 `httpx`/`requests`,你需要在其传输层应用相同的逻辑(此处的分类函数是可复用的)。 - 它不是 WAF;它是专门用于阻止向内部基础设施进行服务器端请求伪造(SSRF)的特定控制措施。 ## 许可证 Apache-2.0。详见 `LICENSE`。
标签:逆向工具