Nginx ND001 nginx-path-traversal

62 阅读1分钟

这个常见于Nginx做反向代理的情况,动态的部分被proxy_pass传递给后端端口,而静态文件需要Nginx来处理。

假设静态文件存储在/home/目录下,而该目录在url中名字为files,那么就需要用alias设置目录的别名:

location /files {
    alias /home/;
}

此时,访问http://example.com/files/readme.txt,就可以获取/home/readme.txt文件。

但我们注意到,url上/files没有加后缀/,而alias设置的/home/是有后缀/的,这个/就导致我们可以从/home/目录穿越到他的上层目录:进而我们获得了一个任意文件下载漏洞。

如何解决这个漏洞?只需要保证 location 和 alias 的值都有后缀/或都没有这个后缀。

同时建议应该尽量避免打开文件共享功能,设置autoindex off;,这样在使用浏览器访问时就会报403

漏洞复现

payload:

GET /file../../../etc/passwd HTTP/1.1
Host: app.xxxxx.com:8080
User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0
Accept-Encoding: gzip