[Zer0pts2020]Can you guess it?
查看源码:
<?php
include 'config.php'; // FLAG is defined in config.php
if (preg_match('/config\.php\/*$/i', $_SERVER['PHP_SELF'])) {
exit("I don't know what you are thinking, but I won't let you read it :)");
}
if (isset($_GET['source'])) {
highlight_file(basename($_SERVER['PHP_SELF']));
exit();
}
$secret = bin2hex(random_bytes(64));
if (isset($_POST['guess'])) {
$guess = (string) $_POST['guess'];
if (hash_equals($secret, $guess)) {
$message = 'Congratulations! The flag is: ' . FLAG;
} else {
$message = 'Wrong.';
}
}
?>
提示了flag在config.php中
$_SERVER用于获取当前执行脚本的文件名,与 document root 有关。例如,在地址为 http://c.biancheng.net/test.php/foo.bar 的脚本中使用 $_SERVER[‘PHP_SELF’]将得到 /test.php/foo.bar
如果url是:http://xxx/index.php/a.php/a/b/c/d/?a=1
$_SERVER[‘PHP_SELF’]的值就是index.php/a.php/a/b/c/d/ (忽略传参)
第一个if做了正则的判断,判断$_SERVER获取到的值是否以config.php/* 结尾。相当于要求config.php/后不能有自读,如果是则退出。
结合提示,猜测我们需要绕过限制,读取到config.php
第二个if如果传入了source参数,就highlight_file 高亮显示$_SERVER中经过basename后提取到的文件
绕过正则可以使用不存在于ascii码表中的字符,比如中文符号?、《》、中文等,例如index.php/config.php/??source(第一个为中文问号),此时正则就会失效,SERVER获取到的就是index.php/config.php(忽略传参),经过basenmae后就是config.php,这样既可以绕过正则匹配,也可以绕过basename的过滤