系统环境

因为网上关于dvwa的相关资料都比较旧,所以想重新过一边dvwa的各个类型的漏洞,顺带初步入门php代码审计相关的知识。环境安装可以直接参照网上教程新手指南:手把手教你如何搭建自己的渗透测试环境,已经写的非常详细,可以直接按照教程一步步安装。

简介

文件上传 File Upload,通常是由于对上传文件的类型,内容没有进行严格的过滤,检查,使得攻击者可以通过上传木马获取服务器的webshell权限,因此文件上传带来的危害常常是毁灭性的,Apache,Tomcat,Nginx等都爆出过文件上传漏洞。

文件上传

接下来对四种级别的代码进行分析

Low级别服务器端核心代码

<

pre><?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 '

Your image was not uploaded.

';
}
else {
// Yes!
echo "

{$target_path} succesfully uploaded!

";
}
}

?>

basename(path,suffix)
函数返回路径中的文件名部分,如果可选参数suffix为空,则返回的文件名包含后缀,反之不包含后缀名。可以看到,服务器对文件上传文件的类型,内容没有做任何的检查,过滤,存在非常明显的文件上传漏洞,生成上传路径后,服务器会检查是否上传成功并返回相应的提示信息。

漏洞利用

文件上传漏洞的利用是有限制条件的,首先必须是要能够成功上传木马文件,其次是上传文件必须能够被执行,最后就是上传文件的路径必须可知。这里三个条件全部满足。
上传一句话木马

image.png

上传成功,并成功返回了上传路径

image.png

打开webshell工具

image.png

然后工具就会通过向服务器发送包含apple参数的post的请求,在服务器上执行任意命令,获取webshell权限,可以下载,修改服务器的任意文件。
也可以打开服务器的虚拟终端

image.png

Medium服务器端核心代码

<

pre><?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 '

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.

';
}
}

?>

可以看到,Medium级别的代码对文件上传的类型,大小做了限制,要求文件类型必须是jepg或者是png,大小不超过100000b

漏洞利用

1.还是采用上一个文件的php,因为有文件类型校验,所以尝试将修改文件名为a.png。因为利用文件上传漏洞必须满足上传木马,木马可执行,文件路径可知,三个条件,如果木马上传以png文件类型上传将不能执行,所以这里想办法绕过客户端限制后通过抓包修改重新将文件的后缀名修改成php,接下来的步骤和low级别的操作的相同。

image.png

打开菜刀读取文件

image.png

2.截断绕过规则
在php版本小于5.3.4的服务器中,当Magic_quote_gpc选项为off时,可以在文件名中使用%00截断,所以可以把上传文件命名为hack.php%00.png。
可以看到,包中的文件类型为image/png,可以通过文件类型检
服务器会认为其文件名为hack.php,就会解析为php文件,但是由于实验的php版本高于5.3.4.所以暂时无法验证

image.png

High服务器端核心代码

<

pre><?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 '

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.

';
}
}

?>

strrpos(string,find,start)
函数返回字符串find在另一字符串string中最后一次出现的位置,如果没有找到字符串则返回false,可选参数start规定在何处开始搜索。
getimagesize(string filename)
函数会通过读取文件头,返回图片的长、宽等信息,如果没有相关的图片文件头,函数会报错。
可以看到,High级别的代码读取文件名中最后一个”.”后的字符串,期望通过文件名来限制文件类型,因此要求上传文件名形式必须是”.jpg”、”.jpeg” 、”*.png”之一。同时,getimagesize函数更是限制了上传文件的文件头必须为图像类型。

漏洞利用

image.png

image.png

image.png

文件上传成功后接下里来就和low级别一致

image.png

Impossible服务器端核心代码

<

pre><?php if( isset( _POST[ 'Upload' ] ) ) {
// Check Anti-CSRF token
checkToken(
_REQUEST[ 'user_token' ], _SESSION[ 'session_token' ], 'index.php' );

// File informationuploaded_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();

?>

in_get(varname)
函数返回相应选项的值
imagecreatefromjpeg ( filename )
函数返回图片文件的图像标识,失败返回false
imagejpeg ( image , filename , quality)
从image图像以filename为文件名创建一个JPEG图像,可选参数quality,范围从 0(最差质量,文件更小)到 100(最佳质量,文件最大)。
imagedestroy( img )
函数销毁图像资源
可以看到,Impossible级别的代码对上传文件进行了重命名(为md5值,导致%00截断无法绕过过滤规则),加入Anti-CSRF token防护CSRF攻击,同时对文件的内容作了严格的检查,导致攻击者无法上传含有恶意脚本的文件。

参考文档

文章来源于互联网:新手教程-DVWA全级别教程之文件上传

发表评论