文件上传
1.文件上传漏洞概述
文件上传是Web应用必备功能之一。
如果服务器配置不当或者没有进行足够的过滤,Web用户就可以上传任意文件,包括恶意脚本文件,exe程序等等,这就造成了文件上传漏洞。
1.1漏洞的成因
服务器配置不当。Web应用开放了文件上传功能,没有对上传的文件做足够的限制和过滤。在程序开发部署时,没有考虑到系统的特性或组件的漏洞,从而导致限制被绕过。
1.2漏洞危害
上传漏洞最直接威胁:上传任意文件(包括恶意脚本、程序等)直接上传后门文件,导致网站沦陷通过恶意文件,利用其它漏洞拿到管理员权限(提权),导致服务器沦陷。
通过文件上传漏洞获得的网站后门,叫webshell
2.webshell解析
命令解释器
powershell
cmd
bash
sh
webshell是一个网站的后门,也是一个命令解释器通过web方式(HTTP协议)传递命令信息到服务器,并且继承了Web用户的权限,在服务器端执行命令。Webshell本质上是服务端可运行的脚本文件,后缀名通常为.php/.asp/.aspx/.jsp…Webshell接受来自于web用户的命令,然后在服务端运行。
2.1小马
一句话***,需要与中国菜刀配合。
特点:短小精悍功能强大
三大基本功能:文件管理虚拟终端数据库管理
2.2大马代码量比较大(与小马对比)注:asp大马上传到phpstudy可以运行
2.3GetShellGetShell是获取WebShell的过程或结果。文件上传漏洞的利用是GetShell的主要方式,但不是唯一手段
3.文件上传漏洞***
3.1黑白名单策略黑白名单是最常用,也是最重要的安全策略之一。黑白名单策略类似于一个列表,列表中写了一些条件或者规则。黑名单:非法条件 | 白名单:合法条件类似于手机号码的黑白名单
3.2毫无防护
源代码:
if( isset( $_POST[ 'Upload' ] ) ) {
// Where are we going to be writing to?
$target_path = DVWA_WEB_PAGE_TO_ROOT . "hackable/uploads/";
$target_path .= basename( $_FILES[ 'uploaded' ][ 'name' ] );
// Can we move the file to the upload folder?
if( !move_uploaded_file( $_FILES[ 'uploaded' ][ 'tmp_name' ], $target_path ) ) {
// No
echo '
Your image was not uploaded.';
}
else {
// Yes!
echo "
{$target_path} succesfully uploaded!";
}
}
?>
代码审计对文件上传没有做任何过滤任意文件上传
3.3文件类型检测
源代码:
if( isset( $_POST[ 'Upload' ] ) ) {
// Where are we going to be writing to?
$target_path = DVWA_WEB_PAGE_TO_ROOT . "hackable/uploads/";
$target_path .= basename( $_FILES[ 'uploaded' ][ 'name' ] );
// File information
$uploaded_name = $_FILES[ 'uploaded' ][ 'name' ];
$uploaded_type = $_FILES[ 'uploaded' ][ 'type' ];
$uploaded_size = $_FILES[ 'uploaded' ][ 'size' ];
// Is it an image?
if( ( $uploaded_type == "image/jpeg" || $uploaded_type == "image/png" ) &&
( $uploaded_size < 100000 ) ) {
// Can we move the file to the upload folder?
if( !move_uploaded_file( $_FILES[ 'uploaded' ][ 'tmp_name' ], $target_path ) ) {
// No
echo '
Your image was not uploaded.';
}
else {
// Yes!
echo "
{$target_path} succesfully uploaded!";
}
}
else {
// Invalid file
echo '
Your image was not uploaded. We can only accept JPEG or PNG images.';
}
}
?>
代码审计:上传的文件没有重命名Content-Type类型白名单判断Content-Type类型客户端可控(客户端用户可以修改此类型)可以利用Burp抓包、改包
3.4文件后缀名和文件内容检测
源代码:
if( isset( $_POST[ 'Upload' ] ) ) {
// Where are we going to be writing to?
$target_path = DVWA_WEB_PAGE_TO_ROOT . "hackable/uploads/";
$target_path .= basename( $_FILES[ 'uploaded' ][ 'name' ] );
// File information
$uploaded_name = $_FILES[ 'uploaded' ][ 'name' ];
$uploaded_ext = substr( $uploaded_name, strrpos( $uploaded_name, '.' ) + 1);
$uploaded_size = $_FILES[ 'uploaded' ][ 'size' ];
$uploaded_tmp = $_FILES[ 'uploaded' ][ 'tmp_name' ];
// Is it an image?
if( ( strtolower( $uploaded_ext ) == "jpg" || strtolower( $uploaded_ext ) == "jpeg" || strtolower( $uploaded_ext ) == "png" ) &&
( $uploaded_size < 100000 ) &&
getimagesize( $uploaded_tmp ) ) {
// Can we move the file to the upload folder?
if( !move_uploaded_file( $uploaded_tmp, $target_path ) ) {
// No
echo '
Your image was not uploaded.';
}
else {
// Yes!
echo "
{$target_path} succesfully uploaded!";
}
}
else {
// Invalid file
echo '
Your image was not uploaded. We can only accept JPEG or PNG images.';
}
}
?>
代码审计:上传的文件没有重命名文件后缀名白名单检测使用getimagesize函数进行文件内容检测,只检测文件头部。图片***需要其他漏洞配合
3.5图片***#cmd.exeCopy imgName /b+yjh/a newImgName图片***没有办法直接利用,需要配合其他漏洞,但是需要知道的是,图片***中包含了恶意代码
3.6完全防御
源代码:
if( isset( $_POST[ 'Upload' ] ) ) {
// Check Anti-CSRF token
checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );
// File information
$uploaded_name = $_FILES[ 'uploaded' ][ 'name' ];
$uploaded_ext = substr( $uploaded_name, strrpos( $uploaded_name, '.' ) + 1);
$uploaded_size = $_FILES[ 'uploaded' ][ 'size' ];
$uploaded_type = $_FILES[ 'uploaded' ][ 'type' ];
$uploaded_tmp = $_FILES[ 'uploaded' ][ 'tmp_name' ];
// Where are we going to be writing to?
$target_path = DVWA_WEB_PAGE_TO_ROOT . 'hackable/uploads/';
//$target_file = basename( $uploaded_name, '.' . $uploaded_ext ) . '-';
$target_file = md5( uniqid() . $uploaded_name ) . '.' . $uploaded_ext;
$temp_file = ( ( ini_get( 'upload_tmp_dir' ) == '' ) ? ( sys_get_temp_dir() ) : ( ini_get( 'upload_tmp_dir' ) ) );
$temp_file .= DIRECTORY_SEPARATOR . md5( uniqid() . $uploaded_name ) . '.' . $uploaded_ext;
// Is it an image?
if( ( strtolower( $uploaded_ext ) == 'jpg' || strtolower( $uploaded_ext ) == 'jpeg' || strtolower( $uploaded_ext ) == 'png' ) &&
( $uploaded_size < 100000 ) &&
( $uploaded_type == 'image/jpeg' || $uploaded_type == 'image/png' ) &&
getimagesize( $uploaded_tmp ) ) {
// Strip any metadata, by re-encoding image (Note, using php-Imagick is recommended over php-GD)
if( $uploaded_type == 'image/jpeg' ) {
$img = imagecreatefromjpeg( $uploaded_tmp );
imagejpeg( $img, $temp_file, 100);
}
else {
$img = imagecreatefrompng( $uploaded_tmp );
imagepng( $img, $temp_file, 9);
}
imagedestroy( $img );
// Can we move the file to the web root from the temp folder?
if( rename( $temp_file, ( getcwd() . DIRECTORY_SEPARATOR . $target_path . $target_file ) ) ) {
// Yes!
echo "
${target_file} succesfully uploaded!";
}
else {
// No
echo '
Your image was not uploaded.';
}
// Delete any temp files
if( file_exists( $temp_file ) )
unlink( $temp_file );
}
else {
// Invalid file
echo '
Your image was not uploaded. We can only accept JPEG or PNG images.';
}
}
// Generate Anti-CSRF token
generateSessionToken();
?>
代码审计:检测Token值,防止数据包重放文件重命名文件后缀名白名单检测Concent-Type类型白名单检测文件内容头部检测二次渲染,生成新文件删除缓存文件
4.文件上传漏洞利用条件
前提条件:
Web服务器开启文件上传功能,web用户可以使用该功能Web用户(www-data|apache)对目标目录具有可写权限,甚至具有执行权限。一般情况下,web目录都有执行权限。“完美利用”意味着文件可以执行,也就是说代码可以被服务器解析。服务器开启了PUT方法
5.GetShell的一些影响因素以upload-labs来探讨相关问题
5.1其他可执行的文件后缀名
phpphp|php5|php4|php3|php2|php1
html:html|htm|phtml|pht
asp:asp|aspx|asa|asax|ascx|ashx|asmx|.cer
jsp:jsp|jspa|jspx|jsw|jsv|jspf|.jtml
swf:
5.2 .htaccess***
.htaccess是Apache服务器分布式配置文件如果某一个目录下存在该文件,.htaccess文件中的配置会覆盖掉Apache全局配置文件httpd.conf的配置,作用域是当前目录及其子目录有效。将.jpg文件当做PHP文件解析AddType application/x-https-php.jpg
匹配固定的文件名
SetHandler application/x-httpd-php
FilesMatch >
文件名中包含php关键字
4.php.pngAddHandler php5-script php
5.3系统特性
忽略大小写脚本程序对大小写敏感,PHP和php是完全不同的两个文件后缀名Windows系统对大小写不敏感
忽略文件名末尾的点#windows[info.php.][info.php]
忽略文件名末尾的空格#windows[info.php ][info.php]
数据流::$DATA#windows#数据流会被系统忽略[info.php::$DATA][ info.php]
5.3绕过一次过滤
[info.php. .][info.php. .][info.php.][info.php.]
[info.pphpphp][info.pphpphp][info.php]
5.4PHP特性:00截断Null字符在C语言中代表字符串的“结束”
5.5GetShell其他思路在能够成功登陆网站后台情况下
修改网站后台脚本文件
直接将一句话***写入网站后台脚本文件(PHP文件)中,找到路径即可直接连接。
直接修改网站配置(上传白名单)通过网站后台,将php等后缀名写进白名单任意文件上传
6.编辑器上传漏洞作为网站的组件,可以编写网页,也就是常说的网页编辑器。很多开源编辑器也存在文件上传漏洞。
FCKeditoreWebEditor…
7.开源CMS文件上传内容管理系统,网站的模板。
WordPress(WP)JoomlaDrupalmetinfo(v5.0.4)dedeCMS(织梦)
8.文件上传漏洞的防御
8.1代码角度
采用白名单策略,严格限制上传文件的后缀名。上传文件重命名,尽量少的从客户端获取信息。尽量避免web用户修改上传白名单文件内容检测进行二次渲染,过滤掉图片马中的恶意代码避免文件包含漏洞严格处理文件路径,防御00截断漏洞避开 、.、::$DATA等windows特性
8.2服务器角度
禁用web容器PUT方法及时更新web容器,防止解析漏洞产生严格控制权限,执行权限与写权限分离(主流)建立单独的文件储存服务器,类似于站库分离
即使文件上传到服务器,也可以将危险降到最低。