当て身 Atemi

A Cybersecurity blog by shinris3n
👊 Writeups 👊 News 👊 Resources

Part of the Ninpwn Network
shinris3n
<< back

11 September 2022

Gotta Crack Them All

Challenge Source: CSAW2022
Challenge Category: Crypto

Challenge Text:

As an intern in the security department, you want to show the admin what a major security issue there is by having all passwords being from a wordlist (even if it is one the admin created) as well as potential issues with stream ciphers. 
Here's the list of encrypted passwords (including the admin's), the encryption algorithm and your password. Can you crack them all and get the admin's password?
Here is the web service that the admin made to encrypt a password: nc crypto.chal.csaw.io 5002

NOTE: The flag is just the admin's password.

Solution:

with open('key.txt','rb') as f:
  key = f.read()

def encrypt(plain):
  return b''.join((ord(x) ^ y).to_bytes(1,'big') for (x,y) in zip(plain,key))
┌──(shinris3n㉿kodachi)-[~/sec/CTFs/CSAW2022]
└─$ python3                                                                                                 
Python 3.9.13 (main, Jun  8 2022, 09:45:57) 
[GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> with open('encrypted_passwords.txt','rb') as f:
...     passwords = f.read()
... 
>>> print(passwords)
b'cr\xcb\xaa\xc0Si\x83\xf0\xb8\xe6\x99\xbf\nlz\xd7\xa6\xdeWr\x83\xe3\xb8\xe0\x97\nks\xcc\xa3\xcbZr\xc0\xc0\xf4\xc2\x8f\xb4\x7f\xab.\xcd\n`z\xd5\xbd\xc5Xb\x83\xe9\xb6\xe0\x91\xacp\nkw\xc4\xba\xc5Ba\xcb\xd5\xf4\xc5\x9d\xb9y\xb1\nkz\xc6\xb9\xd9Du\xcb\x8a\x9e\xe0\x9d\xbeo\xee\x03\xcf\xddd\n{w\xca\xba\xc7_u\xc9\x8a\x89\xfd\x95\xbes\xadj\xfe\xdcv\xf6\xe4\xd2\xaa\n{u\xc0\xac\xdfSw\x83\xe3\xb8\xe0\x97\xe0U\xa0"\n`t\xca\xbd\xcd\x1bK\xdd\xde\xba\xfa\x95\xae1\x84/\xc1\xdc{\nzs\xdc\xbd\xc9Dr\xc1\xd5\xf4\xd5\x8e\xa2i\x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9q\xa0v\xde\xc7<\xa7\n{~\xc4\xa9\xdeW6\xf9\xc6\xad\xf7\x8e\n'
┌──(shinris3n㉿kodachi)-[~]
└─$ nc crypto.chal.csaw.io 5002                                                                             
You can encrypt a pre-approved password using this service.

What is the password you would like to encrypt?

>> 0
This is not a pre-approved password! >:(
It will not be encrypted.


┌──(shinris3n㉿kodachi)-[~]
└─$ nc crypto.chal.csaw.io 5002                                                                             
You can encrypt a pre-approved password using this service.

What is the password you would like to encrypt?

>> Cacturne-Grass-Dark   
The encrypted password is: b'kz\xc6\xb9\xd9Du\xcb\x8a\x9e\xe0\x9d\xbeo\xee\x03\xcf\xddd'
Would you like to go again? (Y/N)
known_encrypted_pws = []
leaked_pw = "Cacturne-Grass-Dark"
encrypted_leaked_pw_from_service = b'kz\xc6\xb9\xd9Du\xcb\x8a\x9e\xe0\x9d\xbeo\xee\x03\xcf\xddd'

with open('encrypted_passwords.txt','rb') as f:
  for line in f:
    known_encrypted_pws.append(f.readline()[:-1])

def decrypt(plain, key):
  return b''.join((ord(x) ^ y).to_bytes(1,'big') for (x,y) in zip(plain,key))

def decrypt_bytes(encrypted, key):
    return bytes([a ^ b for a, b in zip(encrypted, key)])

for count, encrypted_pw in enumerate(known_encrypted_pws):
  if encrypted_leaked_pw_from_service == encrypted_pw:
    print("Encrypted Password", count, "Matches Result Provided via the Web Service:", encrypted_pw)
    key = decrypt("Cacturne-Grass-Dark", encrypted_pw)
    print("Key =", key)

print ('Decrypted Password List:')
for encrypted_pw in known_encrypted_pws:
  try:
    print(decrypt_bytes(encrypted_pw, key))
  except:
    print('@#$%^!!!')
┌──(shinris3n㉿kodachi)-[~/sec/CTFs/CSAW2022]
└─$ python3 decrypt.py                                                                                      
Encrypted Password 2 Matches Result Provided via the Web Service: b'kz\xc6\xb9\xd9Du\xcb\x8a\x9e\xe0\x9d\xbeo\xee\x03\xcf\xddd'
Key = b'(\x1b\xa5\xcd\xac6\x1b\xae\xa7\xd9\x92\xfc\xcd\x1c\xc3G\xae\xaf\x0f'
Decrypted Password List:
b'Darkrai-Dark'
b'Happiny-Normal'
b'Cacturne-Grass-Dark'
b'Sneasel-Dark-Ice'
b'Rhyperior-Ground-Ro'
b'Chinchou-Water-Elec'
b'Excadrill-Ground-St'
b'Kricketune-Bug'
b'Pikipek-Normal-Flyi'
b'Basculin-Water'
b'Togetic-Fairy-Flyin'
b'Riolu-Fighting'
b'Spritzee-Fairy'
b'Silvally-Normal'
b'Wyrdeer-Normal-Psyc'
b'Herdier-Normal'
b'Thwackey-Grass'
b'Bronzong-Steel-Psyc'
b'Chespin-Grass'
b'Tornadus-Flying'
b'Combusken-Fire-Figh'
b'Carnivine-Grass'
b'Grubbin-Bug'
b'Goomy-Dragon'
b'1n53cu2357234mc1ph3'
b''
┌──(shinris3n㉿kodachi)-[~]
└─$ nc crypto.chal.csaw.io 5002
You can encrypt a pre-approved password using this service.

What is the password you would like to encrypt?

>> Chinchou-Water-Electric
The encrypted password is: b'ks\xcc\xa3\xcf^t\xdb\x8a\x8e\xf3\x88\xa8n\xee\x02\xc2\xcal\xe1\xfe\xd2\xaa'
Would you like to go again? (Y/N)
likely_full_password = "Chinchou-Water-Electric"
encrypted_cracked_pw_from_service = b'ks\xcc\xa3\xcf^t\xdb\x8a\x8e\xf3\x88\xa8n\xee\x02\xc2\xcal\xe1\xfe\xd2\xaa'
extended_key = decrypt(likely_full_password, encrypted_cracked_pw_from_service)
print("Extended Key =", extended_key)
print ('Decrypted Password List:')
for encrypted_pw in known_encrypted_pws:
  try:
    print(decrypt_bytes(encrypted_pw, extended_key))
  except:
    print('@#$%^!!!')
Extended Key = b'(\x1b\xa5\xcd\xac6\x1b\xae\xa7\xd9\x92\xfc\xcd\x1c\xc3G\xae\xaf\x0f\x95\x8c\xbb\xc9'
Decrypted Password List:
b'Darkrai-Dark'
b'Happiny-Normal'
b'Cacturne-Grass-Dark'
b'Sneasel-Dark-Ice'
b'Rhyperior-Ground-Rock'
b'Chinchou-Water-Electric'
b'Excadrill-Ground-Steel'
b'Kricketune-Bug'
b'Pikipek-Normal-Flying'
b'Basculin-Water'
b'Togetic-Fairy-Flying'
b'Riolu-Fighting'
b'Spritzee-Fairy'
b'Silvally-Normal'
b'Wyrdeer-Normal-Psychic'
b'Herdier-Normal'
b'Thwackey-Grass'
b'Bronzong-Steel-Psychic'
b'Chespin-Grass'
b'Tornadus-Flying'
b'Combusken-Fire-Fighting'
b'Carnivine-Grass'
b'Grubbin-Bug'
b'Goomy-Dragon'
b'1n53cu2357234mc1ph32'
b''
Tags: CSAW2022, Crypto