美文网首页
nginx目录遍历漏洞

nginx目录遍历漏洞

作者: zhuchunyan_aiji | 来源:发表于2020-09-08 21:43 被阅读0次

    https://www.freebuf.com/articles/web/149761.html

    nginx作为下载文件服务器

    server {
            listen       8082;        #端口
            server_name  localhost;   #服务名
            root    /dev/shm/update;  #显示的根索引目录
            autoindex on;             #开启索引功能
            autoindex_exact_size off; # 关闭计算文件确切大小(单位bytes),只显示大概大小(单位kb、mb、gb)
            autoindex_localtime on;   # 显示本机时间而非 GMT 时间
    }
    或者
    server {
            listen       8082;        #端口
            server_name  localhost;   #服务名
          location / { 
               root    /dev/shm/update;  #显示的根索引目录
              autoindex on;             #开启索引功能
              autoindex_exact_size off; # 关闭计算文件确切大小(单位bytes),只显示大概大小(单位kb、mb、gb)
              autoindex_localtime on;   # 显示本机时间而非 GMT 时间
    
        } 
    }
    
    

    nginx目录遍历漏洞

    http://ip:8082/ ,显示/dev/shm/update/目录下所有文件,说明存在漏洞
    解决:
    autoindex off,重启nginx后
    http://ip:8082/ 就不显示目录下的文件

    ngnix 漏洞:任意文件读取

    这个常见于Nginx做反向代理的情况,动态的部分被proxy_pass传递给后端端口,而静态文件需要Nginx来处理。假设静态文件存储在/home/目录下,而该目录在url中名字为files,那么就需要用alias设置目录的别名:

    location /files {
        alias /home/;
    }
    

    此时访问http://127.0.0.1:8080/files/1.txt,就可以获取/home/1.txt 文件。
    我们发现,url上/files没有加后缀/ ,而alias设置的/home/是有后缀/的,这个 /就导致我们可以从/home/目录穿越到他的上层目录,造成任意文件下载:
    http://127.0.0.1:8080/files../etc/hosts
    修复方法:不写成上面那种有漏洞的形式,比如可以写成结尾都带着/字符。

    location /files/ {
        alias /home/;
    }
    

    0x01$uri导致的CRLF注入

    在实际业务场景中经常需要在nginx中配置路径跳转。

    比如用户访问http://x.com 自动跳转到https://x.com 或者是访问 http://x.com 自动跳转到 http://www.x.com

    在跳转的过程中,我们需要保证用户访问的页面不变,所以需要从Nginx获取用户请求的文件路径,有三个可以表示uri的变量:

    $uri
    $document_uri
    $request_uri
    

    uri 和document_uri表示的是解码以后的请求路径,不带参数,$request_uri表示的是完整的URI(没有解码),如果在nginx.conf中配置了下列的代码:

    location /test {
                 return 302 http://$host:81$uri;
                     }
    

    因为$uri是解码以后的请求路径,所以可能就会包含换行符,也就造成了一个CRLF注入漏洞
    该漏洞除了发生在 return后面,也可能发生在rewrite、add_header、p roxy_set_header、proxy_pass之后。

    修复方式:将uri或者document_uri改为 $request_uri。

    alias/root 区别使用

    https://blog.csdn.net/zhuchunyan_aijia/article/details/53033050

    相关文章

      网友评论

          本文标题:nginx目录遍历漏洞

          本文链接:https://www.haomeiwen.com/subject/rkdfektx.html