writeup PwnTheBox hash

140 阅读1分钟

本文已参与 「新人创作礼」 活动,一起开启掘金创作之路。

题目链接: ce.pwnthebox.com/challenges?…

将请求头中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

在这里插入图片描述


其他writeup2:

考点:哈希扩展攻击
打开题目,为登录界面,尝试登录,然后抓包,发现cookie中字段:source=0,修改为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") {        if ($COOKIE["getflag"] === md5($secret_key . urldecode($username . $password))) {            echo "Great! You're in!\n";            die ("");        }        else {            die ("Go out! Hacker!");        }    }    else {        die ("LEAVE! You're not one of us!");    }}​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 "";    }}

同时得到

hash_key=c3ef608fdc59d9143c39664ade7556d5$secret是密文,长度为16,如果再算上后面第一个D0g3,长度就是20

因此使用hashpump

hashpump -s c3ef608fdc59d9143c39664ade7556d5 -d D0g3 -k 20 -a pcat

得到:

0506b1f2bdc477575917cb3c285bea59
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\x00pcat

第一个是新的签名,把它设置到cookies的getflag里。
第二个先把\x替换为%后,post提交就可以通过了
图片.png