参考文章:https://www.freebuf.com/articles/web/149761.html
参考文章:https://joychou.org/web/nginx-http-forward-proxy-ssrf.html
参考文章:https://joychou.org/operations/nginx-config-security.html
Ningx.conf配置一共分为4部分:1、顶级配置 2、Events 模块 3、http部分 4、Server部分
默认的nginx.conf配置如下:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;#日志存储位置
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;#配置文件目录
server {
listen 80 default_server;#监听端口
listen [::]:80 default_server;
server_name _;#域名
root /usr/share/nginx/html;#web目录存储的位置
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
1、不安全配置导致的任意文件读取/目录遍历:
触发点:这个常见于Nginx做反向代理的情况,动态的部分被proxy_pass
传递给后端端口,而静态文件需要Nginx来处理。假设静态文件存储在/home/目录下,而该目录在url中名字为files,那么就需要用alias设置目录的别名
location /files {
alias /home/;
}
为/files
设置一个别名为/home/
,
访问:http://116.85.41.113/files../etc/passwd
,发现成功读取!
这里相当于/home/../etc/passwd
,如果/files
为/files/
那么肯定就是正常解析,但是这里没有闭合导致了任意文件读取
alias的目录遍历:只能跳到上一层目录,读取上一层目录及其任意子目录的文件,不能任意目录遍历读取任意文件,有局限性!
修复方法:结尾都带上/
字符!
如果当前目录是可遍历的,也就是再加上
autoindex on;
2、$uri导致的CRLF注入:
触发点:在实际业务场景中经常需要在nginx中配置路径跳转。比如用户访问http://x.com
需要自动跳转到https://x.com
或者是访问http://x.com
自动跳转到http://www.x.com
实现条件:
1、需要进行302跳转
2、还需要知道用户请求的路径内容
这里有有三个可以表示URI的变量:
$uri
$document_uri
$request_uri
$uri
和$document_uri
表示的是解码以后的请求路径,并且不带参数
而$request_uri表示的是完整的URI,带参数,并且没有解码过
如果在nginx.conf中配置了下列的代码:
location /test {
return 302 http://$host:443$uri;
}
这里$host为请求域,$uri为请求路径,这里知道$uri会进行解码,那么当我们进行注入%0d%0a
的时候,就会触发CRLF漏洞
注意:该漏洞除了发生在return的后面,也可能发生在rewrite、add_header、proxy_set_header、proxy_pass的后门
修复方式:将$uri/$document_uri都改为$request_uri
3、proxy_pass反向代理导致的SSRF:
触发点:SSRF(服务端请求伪造)漏洞常出现在反向代理的配置中,反向代理的语法如下:proxy_pass http://ip:port/uri/;
不安全配置如下:
location ~ /([a-zA-Z0-9.:%]+) {
proxy_pass http://$1;
}
如果攻击者可以操控IP, 将其修改成内网IP地址即可造成SSRF漏洞。
请求成功为:
请求失败为:
最大的特点:无回显!
4、proxy_pass正向代理导致的SSRF:
不安全配置如下:
proxy_pass http://$host$request_uri;
最大的特点:有回显!