将请求头中Cookie的source改为1,得到源代码~
@error_reporting(0);
$flag = "flag{xxxxxxxxxxxxxxxxxxxxxxxxxxxx}";
$secret_key = "xxxxxxxxxxxxxxxx"; // the key is safe! no one can know except me
$username = $_POST["username"];
$password = $_POST["password"];
header("hash_key:" . $hash_key);
if (!empty($_COOKIE["getflag"])) {
if (urldecode($username) === "D0g3" && urldecode($password) != "D0g3") {
//cookie中的getflag的值要为hash(secret_key||("D0g3" . password)))
//hashpump就是通过将原来数据填充成块后,再添加一段自定义内容,得到最终结果与后端生成的一致
//通过攻击库构造password,提前得到hash结果,后端验证的时候,就可以通过
if ($COOKIE["getflag"] === md5($secret_key . urldecode($username . $password))) {
echo "Great! You're in!\n";
die ("<!-- The flag is ". $flag . "-->");
}
else {
die ("Go out! Hacker!");
}
}
else {
die ("LEAVE! You're not one of us!");
}
}
//头部返回的hash是hash(secret_key||("D0g3" . "D0g3"))的结果
setcookie("sample-hash", md5($secret_key . urldecode("D0g3" . "D0g3")), time() + (60 * 60 * 24 * 7));
if (empty($_COOKIE["source"])) {
setcookie("source", 0, time() + (60 * 60 * 24 * 7));
}
else {
echo "<source_code>";
}
}
可以使用哈希长度扩展攻击,使用攻击库
hashpump
Input Signature: c3ef608fdc59d9143c39664ade7556d5
Input Data: D0g3
Input Key Length: 20 源码文件中16个‘x’+'D0g3' =20个字符
Input Data to Add: kinnisoy(自定义)
会得到:
15e64be7722f2e46d78d483264ebc5c8
D0g3\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\x00\x00\x00\x00\x00\x00\x00kinnisoy
第一个是新的签名,把它设置到cookies的getflag里。
第二个先把\x替换为%后,post提交
D0g3%80%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%c0%00%00%00%00%00%00%00kinnisoy