0x01 简介
开发人员没有对用户上传的文件进行严格的过滤和验证,以致于用户可上传恶意脚本,从而产生危害。
0x02 File Upload-Low
Low级别应该是未对上传的文件做任何验证,存在任意文件上传漏洞。

image.png
掏出我的大宝贝连接试试。

image.png
毫无悬念的连上了,看下源码先。
<?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 '<pre>Your image was not uploaded.</pre>';
}
else {
// Yes!
echo "<pre>{$target_path} succesfully uploaded!</pre>";
}
}
?>
可以看到确实没做任何校验
0x03 File Upload-Medium
第二关做了后缀名的限制,只允许上传png和jpg格式的文件,抓包测下是不是只限制了MIME类型。

image.png
将MIME改为image/jpeg后就能成功绕过限制了。

image.png

image.png
掏出我的大宝贝试试

image.png
成功了,老规矩,看看源码。
<?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 '<pre>Your image was not uploaded.</pre>';
}
else {
// Yes!
echo "<pre>{$target_path} succesfully uploaded!</pre>";
}
}
else {
// Invalid file
echo '<pre>Your image was not uploaded. We can only accept JPEG or PNG images.</pre>';
}
}
?>
0x04 File Upload-High
抓个包改MIME类型试试先。

image.png
没能绕过,看来是做了黑名单或者白名单限制,琢磨下先。
试着上传了一个shell.xxx的文件,发现上传失败,应该是做了白名单限制

image.png
随手试了下%00截断,绕过失败。
又收集了中间件信息,也没找到可以配合的解析漏洞,看看源码先。
<?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 '<pre>Your image was not uploaded.</pre>';
}
else {
// Yes!
echo "<pre>{$target_path} succesfully uploaded!</pre>";
}
}
else {
// Invalid file
echo '<pre>Your image was not uploaded. We can only accept JPEG or PNG images.</pre>';
}
}
?>
看到源码,限制上传的文件格式为诶jpg、jpeg和png。绕过白名单可以从以下着手:
- %00截断,需要PHP版本小于5.3.4且magic_quotes_gpc关闭
- ::$DATA 需要Windows+php环境,Windows应该有版本限制,未能查到相关资料
- 先上传php:.jpg格式的空白文件,然后在>>>.php写入恶意代码
- 图片马配合解析漏洞
- 图片马配合文件包含漏洞
目前学艺不精,能想到的通关方法是上传图片马然后配合前面的文件包含漏洞getshell,后续有其他绕过姿势,将会更新。
0x05 中间件常见解析漏洞

image.png
0x06 文件上传防范方法
- 白名单限制上传的文件格式
- 禁止文件上传目录的执行权限
- 修补中间件的安全漏洞
- 重命名上传后的文件名称
- 隐藏上传后的文件地址