开放重定向检测 —— 原理、示例与自动化检测
登录成功、支付完成这类场景常带一个 next / redirect 参数。如果跳转地址直接取自参数且不做域名校验,攻击者就能构造一条指向自己站点的「官方链接」,用来钓鱼或窃取 token。
漏洞危害
- 钓鱼:链接域名是我们,落地页是攻击者的仿冒登录页
- 绕过邮件与风控的白名单校验,把用户导向恶意站点
- 配合 OAuth 流程可窃取授权码
代码示例(Go)
❌ 危险写法
// 危险写法:跳转地址直接取参数
func afterLogin(w http.ResponseWriter, r *http.Request) {
next := r.URL.Query().Get("next")
http.Redirect(w, r, next, http.StatusFound)
// ?next=https://evil.example/login 即被带走
}
✅ 安全写法
// 安全写法:只允许站内相对路径
func afterLogin(w http.ResponseWriter, r *http.Request) {
next := r.URL.Query().Get("next")
if !strings.HasPrefix(next, "/") || strings.HasPrefix(next, "//") {
next = "/dashboard"
}
http.Redirect(w, r, next, http.StatusFound)
}
煋鉴如何检测
- 重定向目标来自请求参数,且没有域名白名单或站内路径校验
- 用 Contains / HasPrefix("http") 判断,可被 //evil.com、@ 绕过
- OAuth 回调 redirect_uri 未做精确匹配