swapniluneva/ssrf-agent-guard
GitHub: swapniluneva/ssrf-agent-guard
一个 Node.js HTTP/HTTPS 安全代理模块,通过 DNS 前后校验和 IP 策略过滤来防御服务器端请求伪造(SSRF)攻击。
Stars: 1 | Forks: 0
# ssrf-agent-guard
[](https://www.npmjs.com/package/ssrf-agent-guard)
[](https://www.npmjs.com/package/ssrf-agent-guard)
[](https://codecov.io/gh/swapniluneva/ssrf-agent-guard)
#### ssrf-agent-guard 是一个 Node.js 安全代理,旨在强化 **Axios、Fetch 和 Got** 请求,以防范 SSRF(服务器端请求伪造)攻击。它包装了 http.Agent 和 https.Agent,以强制执行 DNS 前后主机/IP 检查,阻止对云元数据端点、私有 IP 和不安全域名的访问。
## 功能
* 阻止对内部/私有 IP 的请求
* 检测并阻止云服务提供商的元数据端点(AWS、GCP、Azure、Oracle、DigitalOcean、Kubernetes)
* DNS 重绑定检测
* 基于策略的域名过滤(白名单、黑名单、TLD 封锁)
* 多种操作模式(阻止、报告、允许)
* 支持自定义日志记录
* 完全使用 TypeScript 编写,并附带类型定义
## 文档
有关完整的 API 文档,请参阅 [API.md](./API.md)。
有关被阻止的 IP 范围和安全原理的详细信息,请参阅 [IP_RANGES.md](./IP_RANGES.md)。
有关特定框架的示例,请参阅 [示例](./examples) 目录:
- [Express.js](./examples/express)
- [Fastify](./examples/fastify)
- [NestJS](./examples/nestjs)
## 安装说明
```
npm install ssrf-agent-guard
# 或使用 yarn
yarn add ssrf-agent-guard
```
## 用法
### axios
```
const ssrfAgentGuard = require('ssrf-agent-guard');
const url = 'https://127.0.0.1'
axios.get(
url, {
httpAgent: ssrfAgentGuard(url),
httpsAgent: ssrfAgentGuard(url)
})
.then((response) => {
console.log(`Success`);
})
.catch((error) => {
console.log(`${error.toString().split('\n')[0]}`);
})
.then(() => {
});
```
### node-fetch
```
const ssrfAgentGuard = require('ssrf-agent-guard');
const url = 'https://127.0.0.1'
fetch(url, {
agent: ssrfAgentGuard(url)
})
.then((response) => {
console.log(`Success`);
})
.catch(error => {
console.log(`${error.toString().split('\n')[0]}`);
});
```
### 高级配置
```
const ssrfAgentGuard = require('ssrf-agent-guard');
const agent = ssrfAgentGuard('https://api.example.com', {
mode: 'block', // 'block' | 'report' | 'allow'
blockCloudMetadata: true, // Block AWS/GCP/Azure metadata endpoints
detectDnsRebinding: true, // Detect DNS rebinding attacks
policy: {
allowDomains: ['*.trusted.com'], // Only allow these domains
denyDomains: ['evil.com'], // Block these domains
denyTLD: ['local', 'internal'] // Block these TLDs
},
logger: (level, msg, meta) => {
console.log(`[${level}] ${msg}`, meta);
}
});
```
## 开发说明
```
# 安装依赖
npm install
# build
npm run build
# 运行测试
npm test
```
## 致谢:
* SSRF 防范技术:[SSRF 速查表](https://cheatsheetseries.owasp.org/cheatsheets/Server_Side_Request_Forgery_Prevention_Cheat_Sheet.html)
* 实现灵感来源于 [ssrf-req-filter](https://github.com/y-mehta/ssrf-req-filter/)
## 许可证
MIT © [Swapnil Srivastava](https://swapniluneva.github.io)
标签:CISA项目, DNS重绑定检测, GNU通用公共许可证, MITM代理, Node.js, SSRF防护, Web安全, 中间件, 网络安全, 自动化攻击, 蓝队分析, 隐私保护