URI绑定攻击(URI Binding Attack)是一种针对反向代理服务器的安全威胁,攻击者通过精心构造的URI请求,绕过代理服务器的安全限制,直接访问后端服务器的敏感资源。
当Nginx作为反向代理时,攻击者可能利用以下方式进行URI绑定攻击:
../
等路径遍历字符访问非预期资源http://example.com/proxy/http://internal-server/secret
http://example.com/proxy/%2e%2e/admin
http://example.com:80@malicious.com/
location /proxy/ {
proxy_pass http://backend/; # 注意结尾的斜杠
proxy_set_header Host $host;
}
location / {
limit_except GET POST {
deny all;
}
}
merge_slashes on; # 默认开启,合并多个斜杠
location /safe/ {
proxy_pass http://backend/;
# 拒绝包含../的请求
if ($request_uri ~* "\.\.") {
return 403;
}
}
server {
listen 80;
server_name example.com;
if ($host !~* ^example\.com$) {
return 444; # 关闭连接
}
}
map $request_uri $bad_uri {
default 0;
"~*\.\." 1;
"~*//" 1;
# 添加其他恶意模式
}
server {
if ($bad_uri) {
return 403;
}
}
http {
large_client_header_buffers 4 8k;
client_header_buffer_size 1k;
}
保持Nginx更新以获取最新的安全补丁:
sudo apt-get update && sudo apt-get upgrade nginx
通过以上措施,可以显著降低Nginx反向代理环境中的URI绑定攻击风险。