dvwa上传php文件,DVWA之文件上传漏洞

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

509e78d96957

图片.png

LOW

服务端代码:

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为空,则返回的文件名包含后缀名,反之不包含后缀名。

可以看到,服务器对上传文件的类型、内容没有做任何的检查、过滤,存在明显的文件上传漏洞,生成上传路径后,服务器会检查是否上传成功并返回相应提示信息。

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

上传文件MM.php(一句话木马)

509e78d96957

图片.png

509e78d96957

图片.png

打开中国菜刀,右键添加,

参数名(一句话木马口令)MUMA

509e78d96957

图片.png

配置信息不知道可不填

然后菜刀就会通过向服务器发送包含MUMA参数的post请求,在服务器上执行任意命令,获取webshell权限。

可以下载、修改服务器的所有文件。

509e78d96957

图片.png

Medium(中)

源代码:

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级别的代码对上传文件的类型、大小做了限制,要求文件类型必须是jpeg或者png,大小不能超过100000B(约为97.6KB)。

1.文件包含+文件上传

因为采用的是一句话木马,所以文件大小不会有问题,至于文件类型的检查,尝试修改文件名为MM.png。

509e78d96957

图片.png

上传成功

上菜刀:

509e78d96957

图片.png

509e78d96957

图片.png

虽然成功上传了文件,但是并不能成功获取webshell权限,

中国菜刀的原理是向上传文件发送包含MUMA参数的post请求,通过控制MUMA参数来执行不同的命令,而这里服务器将木马文件解析成了图片文件,因此向其发送post请求时,服务器只会返回这个“图片”文件,并不会执行相应命令。

那么如何让服务器将其解析为php文件呢?我们想到文件包含漏洞程。这里可以借助Medium级别的文件包含漏洞来获取webshell权限,打开中国菜刀,右键添加,在地址栏中输入

509e78d96957

图片.png

2.抓包修改文件类型

上传Mm.png文件,抓包。

509e78d96957

图片.png

修改为php文件。。

509e78d96957

图片.png

上传成功

上菜刀:

509e78d96957

图片.png

3.截断绕过规则

在php版本小于5.3.4的服务器中,当Magic_quote_gpc选项为off时,可以在文件名中使用%00截断,所以可以把上传文件命名为MM.php%00.png。

High

源代码:

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.
';

}

}

?>

strtolower() : 函数把字符串转换为小写。

strrpos(string,find,start) :函数返回字符串find在另一字符串string中最后一次出现的位置,如果没有找到字符串则返回false,可选参数start规定在何处开始搜索。

getimagesize(string filename) :函数会通过读取文件头,返回图片的长、宽等信息,如果没有相关的图片文件头,函数会报错。

可以看到,High级别的代码读取文件名中最后一个”.”后的字符串,期望通过文件名来限制文件类型,因此要求上传文件名形式必须是”.jpg”、”.jpeg” 、”*.png”之一。同时,getimagesize函数更是限制了上传文件的文件头必须为图像类型。

1.采用%00截断的方法可以轻松绕过文件名的检查,但是需要将上传文件的文件头伪装成图片,只能在php版本小于5.3.4的服务器中

2.借助High级别的文件包含漏洞来完成攻击。

首先利用copy将一句话木马文件php.php与图片文件timg.jpg合并

509e78d96957

图片.png

509e78d96957

图片.png

生成新图片

509e78d96957

图片.png

记事本打开后,可以看到一句话木马

509e78d96957

图片.png

但是上传不了,怎么看都没错,突然发现是图片太大了,随便找了张

509e78d96957

图片.png

会报错。。

然后换张小的,,

509e78d96957

图片.png

509e78d96957

图片.png

上传成功。。

上菜刀,右键添加shell,地址栏填入

没成功

509e78d96957

图片.png

不知道什么原因

Impossible

服务器核心代码:

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();

?>

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攻击,同时对文件的内容作了严格的检查,导致攻击者无法上传含有恶意脚本的文件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值