攻防世界-web-wtf.sh-150(路径穿越、cookie欺骗、上传后门)

题目来源:csaw-ctf-2016-quals
题目描述:题目环境是一个论坛,有注册、登录、发布、评论的功能。

flag1

进入环境, 在展示文章的页面 post.wtf 下发现路径穿越漏洞,可以获得网站源码

源代码中搜索flag关键字发现,当登录用户为admin时,可以get_flag1

先尝试注册一个账号并登录,发现如下 cookie

在泄露的源码里得知有 users 目录

继续尝试路径穿越

发现自己注册的账号以及 admin 都被显示了出来,对比之前的 cookie,发现这个就是TOKEN的值。

admin 的TOKEN也知道了,可以进行 cookie 欺骗。

故登录 admin 所需的东西如下

登录成功,获得 flag1:xctf{cb49256d1ab48803

 

flag2

因为 wtf 不是常规的网页文件,故寻找解析 wtf 文件的代码:

通过源码可以发现对bash命令的调用,访问http://220.249.52.133:32374/wtf.sh得到linux脚本代码。

进行审计后可发现,上传wtf文件并执行,就可以控制服务器。

function include_page {
    # include_page pathname
    local pathname=$1
    local cmd=
    [[ ${pathname(-4)} = '.wtf' ]];
    local can_execute=$;
    page_include_depth=$(($page_include_depth+1))
    if [[ $page_include_depth -lt $max_page_include_depth ]]
    then
        local line;
        while read -r line; do
            # check if we're in a script line or not ($ at the beginning implies script line)
            # also, our extension needs to be .wtf
            [[ $ = ${line01} && ${can_execute} = 0 ]];
            is_script=$;
            # execute the line.
            if [[ $is_script = 0 ]]
            then
                cmd+=$'n'${line#$};
            else
                if [[ -n $cmd ]]
                then
                    eval $cmd  log Error during execution of ${cmd};
                    cmd=
                fi
                echo $line
            fi
        done  ${pathname}
    else
        echo pMax include depth exceeded!p
    fi
}

继续进行审计,发现reply函数也存在路径穿越漏洞。

function reply {
    local post_id=$1;
    local username=$2;
    local text=$3;
    local hashed=$(hash_username "${username}");
    curr_id=$(for d in posts/${post_id}/*; do basename $d; done | sort -n | tail -n 1);
    next_reply_id=$(awk '{print $1+1}' <<< "${curr_id}");
    next_file=(posts/${post_id}/${next_reply_id});
    echo "${username}" > "${next_file}";
    echo "RE: $(nth_line 2 < "posts/${post_id}/1")" >> "${next_file}";
    echo "${text}" >> "${next_file}";
    # add post this is in reply to to posts cache
    echo "${post_id}/${next_reply_id}" >> "users_lookup/${hashed}/posts";
}

这是评论功能的后台代码,下面这行代码把用户名写在了评论文件的内容中

echo "${username}" > "${next_file}";

如果用户名是一段可执行代码,而且写入的文件后缀是wtf,那么这个文件就能够执行我们想要的代码。 (wtf.sh只运行文件扩展名为.wtf的脚本和前缀为’$'的行)

先普通地评论一下,知晓评论发送的数据包的结构

在普通评论的基础上,进行路径穿越,上传后门sh.wtf

%09是水平制表符,必须添加,不然后台会把我们的后门当做目录去解析。

访问后门,发现成功写入用户名

为了写入恶意代码,我们得让用户名里携带代码,故

首先注册这样一个用户

${find,/,-iname,get_flag2}

这里前面加个$是wtf文件执行命令的写法

寻找 所有目录下 不分大小写,名为 get_flag2 的文件

登录,写入后门

访问后门路径,执行代码,寻找get_flag2(因为之前获得 flag1 的时候是 get_flag1)

获得 flag2 所在的路径:/usr/bin/get_flag2 

继续注册新用户

$/usr/bin/get_flag2 

登录,写入后门

访问后门路径,执行代码,获得 flag2:149e5ec49d3c29ca}

故最终的flag为:xctf{cb49256d1ab48803149e5ec49d3c29ca}

 

参考:https://blog.cindemor.com/post/ctf-fairy-1.html

posted @ 2020-11-19 10:14  zhengna  阅读(991)  评论(0编辑  收藏  举报