solution.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import requests
  2. from binascii import hexlify, unhexlify
  3. url = 'http://192.168.0.222:5000'
  4. # Any registered username and password
  5. username = 'myBLfLEDraYh3Dq'
  6. password = '9GQLqu39EviKw'
  7. # default comment and any much longer string
  8. original = 'No description yet'
  9. description = 'stringadiversamapiulungastringadiversamapiulungastringa'.encode('ascii')
  10. # do login
  11. s = requests.session()
  12. r = s.post(url + '/login', data={'username': username, 'password': password})
  13. cur_iv = s.cookies['iv']
  14. ciphertext1 = s.cookies['session']
  15. # loop trough 256 requests to obtain a two time pad
  16. for i in range(int(cur_iv), int(cur_iv)+256):
  17. s.post(url + '/user', data={'description': description})
  18. if s.cookies['iv'] == cur_iv:
  19. ciphertext2 = s.cookies['session']
  20. for i in range(0, len(ciphertext1), 2):
  21. if ciphertext1[i:i+1] != ciphertext2[i:i+1]:
  22. break
  23. # obtain new cookis with the original comment to flip the fals in true
  24. s.post(url + '/user', data={'description': original})
  25. cur_iv = s.cookies['iv']
  26. uid = s.cookies['uid']
  27. description = description.hex()
  28. ciphertext1 = ciphertext1[i:]
  29. ciphertext2 = ciphertext2[i:i+len(description)]
  30. # xor our known plaintext with the given ciphertext to recover part of the rc4 stream
  31. key = '{:x}'.format(int(ciphertext2, 16) ^ int(description, 16))[0:len(ciphertext1)]
  32. # xor our known rc4 stream with the default comment on the same iteration to decrypt the final parte of the cookie
  33. plaintext = unhexlify('{:x}'.format(int(key, 16) ^ int(ciphertext1, 16)))
  34. # print the plaintext and notice it has a show_flag parameter
  35. print('[*] Decrypted first cookie: ' + plaintext.decode('ascii'))
  36. # xor 'true ' with 'false' to calculate the value to use to flip the ciphertext
  37. flip = '{:x}'.format(int('true '.encode('ascii').hex(), 16) ^ int('false'.encode('ascii').hex(), 16))
  38. cur_session = s.cookies['session']
  39. # xor to obtain 'true ' from false'
  40. flip = '{:x}'.format(int(flip, 16) ^ int(cur_session[-12:-2], 16))
  41. session = cur_session[0:-12] + flip + cur_session[-2:]
  42. # get the flag!
  43. flag = requests.get(url + '/user', cookies={'iv': cur_iv, 'uid': uid, 'session': session})
  44. print('[*] Got the flag: ' + flag.text)