Web – 150 pts (86 solves) – Chall author: Eth007

A classic example of how not to use a cookie-based login system for your webpage, how not to hash stored passwords, and how not to create passwords to begin with… A singular un-salted hashed password is easy to recognise and reverse. Being able to login succesfully to a non-admin account we steal its cookie and bit-flip our way to the admin page!

Check out write-ups by my teammates on K3RN3L4RMY.com

Exploration

On first glance the website shows nothing but a login page. But on second glance, it does too… So let’s instead have a look at the provided back-end code.

Immediately we can see a small hard-coded ‘user-database’ with singular un-salted SHA512 hashed passwords.

users = {
'just_a_normal_user' : 'b109f3bbbc244eb82441917ed06d618b9008dd09b3befd1b5e07394c706a8bb980b1d7785e5976ec049b46df5f1326af5a2ea6d103fd07c95385ffab0cacbc86',
}

return False
return True


Doing a quick reverse lookup of the hashes we find 4 out of the 6 passwords. Please never, ever, ever, ever, store passwords this way. Pinky swear!

users = {
'ImaginaryCTFUser' : 'idk',
'Eth007' : 'supersecure',
'firepwny' : 'pwned',
':roocursion:' : '',
}


Now that we can login, we can steal one of the user’s cookies, which are created using a constant key and AES-CTR.

@app.route('/backend', methods=['GET', 'POST'])
def backend():
if request.method == 'POST':
resp = make_response(redirect('/home'))
nonce = urandom(8)
cipher = AES.new(key, AES.MODE_CTR, nonce=nonce) # my friend told me that cbc had some weird bit flipping attack? ctr sounds way cooler anyways
return resp
else:
return make_response(redirect('/home'))

@app.route('/home', methods=['GET'])
def home():
cipher = AES.new(key, AES.MODE_CTR, nonce=nonce)
else:


AES-CTR is a stream cipher! All (unauthenticated) stream ciphers are malleable, i.e. susceptible to a bit-flipping attack. A stream cipher will produce the exact same keystream every time the same key and nonce are used. Therefore all we have to do is capture a valid cookie encrypted using the server’s key, and XOR it such that the underlying plain text becomes whatever we need to get in. So let’s get flipping!

Exploitation

A user’s cookie is structered (in hex) as follows.

nonce | AES_CTR( PAD( username ) )


For our guy ‘just_a_normal_user’, this would become

nonce | AES_CTR( b'just_a_normal_user\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e' )


Because a stream cipher simply XORs its key stream with the data it wants to encrypt, we can recover the keystream by XORing a stolen cookie with the known plain text. We logged into their account and stole their cookie. Nom nom!

6812284b5ba1dd61 | eed72265a689b7809673193143e3896fdc03610ce858a3b9941ae08bdfd8dca5


This means that for the nonce ‘6812284b5ba1dd61’ we now know the keystream that will be generated using the server’s key.

stolen_cookie ^ pad('just_a_normal_user') = 84a25111f9e8e8eef90174502fbcfc1cb9716f02e656adb79a14ee85d1d6d2ab


Finally, for our forged cookie (are those even edible? :S) we XOR the recovered keystream with our payload. Note that we only need the first half of our recovered keystream as ‘admin’ is shorter than the AES block size of 16 bytes.

rec_keystream ^ pad('admin') = e5c63c7897e3e3e5f20a7f5b24b7f717


Combine it with the nonce and send it on its way to login as the admin!

6812284b5ba1dd61e5c63c7897e3e3e5f20a7f5b24b7f717


Ta-da!

ictf{d0nt_r3us3_k3ystr34ms_b72bfd21}