给了源码
<?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中
先看一下$secret = bin2hex(random_bytes(64));
// 一般配合bin2hex()函数使用
// bin2hex()把ASCII字符串转换为十六进制值
echo bin2hex(random_bytes(10));
// 输出
c95ddb113d282ead7209
bin2hex() 函数把 ASCII 字符的字符串转换为十六进制值。
<?php
$str = bin2hex("Hello World!");
echo($str);
?>
//48656c6c6f20576f726c6421
对比时采用了php 可防止时序攻击的字符串比较函数 hash_equals()
bool hash_equals ( string $known_string , string $user_string )
比较两个字符串,无论它们是否相等,本函数的时间消耗是恒定的。
本函数可以用在需要防止时序攻击的字符串比较场景中, 例如,可以用在比较 crypt() 密码哈希值的场景。
要想通过guess变量与secret相同来获取flag是不太可能的
我们再看上面
if (isset($_GET['source'])) {
highlight_file(basename($_SERVER['PHP_SELF']));
exit();
}
但是上面对路径名做了过滤,过滤了config.php/* 结尾,相当于要求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/ (忽略传参)
绕过正则可以使用不存在于ascii码表中的字符,比如中文符号?、《》、中文等,例如index.php/config.php/??source(第一个为中文问号),此时正则就会失效,SERVER获取到的就是index.php/config.php(忽略传参),经过basenmae后就是config.php,这样既可以绕过正则匹配,也可以绕过basename的过滤
payload:
/index.php/config.php/??source