doyensec/safeurl
GitHub: doyensec/safeurl
一个 Go 语言的 SSRF 防护库,通过包装 net/http.Client 并校验请求目标的 IP、端口、主机和 CIDR 等信息来阻止服务端请求伪造攻击。
Stars: 112 | Forks: 12
# `safeurl`
一个 Go 库,旨在帮助开发者保护其应用程序免受 [服务端请求伪造](https://owasp.org/www-community/attacks/Server_Side_Request_Forgery) (SSRF) 攻击。它对 Go 原生的 `net/http.Client` 实现了 `safeurl.Client` 包装器,并根据配置的允许和阻止列表对传入请求执行验证。它还实现了针对 [DNS 重绑定](https://en.wikipedia.org/wiki/DNS_rebinding) 攻击的缓解措施。
### 配置选项
可以通过 `safeurl.Config` 结构体对 `safeurl.Client` 进行配置。它支持配置以下选项:
```
AllowedPorts - list of ports the application is allowed to connect to
AllowedSchemes - list of schemas the application can use
AllowedHosts - list of hosts the application is allowed to communicate with
BlockedIPs - list of IP addresses the application is not allowed to connect to
AllowedIPs - list of IP addresses the application is allowed to connect to
AllowedCIDR - list of CIDR ranges the application is allowed to connect to
BlockedCIDR - list of CIDR ranges the application is not allowed to connect to
IsIPv6Enabled - specifies wether communication through IPv6 is enabled
AllowSendingCredentials - specifies wether HTTP credentials should be sent
Custom `http.Transport` - allows callers to configure a custom implementation `http.Transport` for the library
IsDebugLoggingEnabled - enables debug logs
```
**注意**:传递给 `SetTransport` 的 `http.Transport` 实例,如果为以下属性定义了自定义实现,将会导致 `panic()`:`Dial`、`DialTLS` 或 `DialTLSContext`。
### 如何使用 safeurl.Client?
首先,你需要引入 `safeurl` 模块。为此,只需将 `github.com/doyensec/safeurl` 添加到你项目的 `go.mod` 文件中即可。
示例:
```
import (
"fmt"
"github.com/doyensec/safeurl"
)
func main() {
config := safeurl.GetConfigBuilder().
SetAllowedHosts("example.com").
Build()
client := safeurl.Client(config)
resp, err := client.Get("https://example.com")
if err != nil {
fmt.Errorf("request return error: %v", err)
}
// read response body
}
```
### 运行测试
要成功运行所有单元测试,你需要运行一个本地的 DNS 和 HTTP 服务器。这可以通过执行以下命令来完成:
```
go run testing/servers.go
```
一旦服务器启动并运行,就可以使用以下命令运行单元测试:
```
go test -v
```
## 致谢
该工具由 [Doyensec LLC](https://www.doyensec.com) 的 Viktor Chuchurski 和 Alessandro Cotto 在我们的 [25% 研究时间](https://doyensec.com/careers.html) 内创建。

标签:CISA项目, EVTX分析, Go, Ruby工具, SSRF防护, Web安全, 安全防护, 日志审计, 蓝队分析