Block Ciphers
Cryptohack.org
BLOCK CIPHERS WRITEUP
Author:
Pham Quoc Trung
Used Language:
Python3
Problem Solving:
MODES OF OPERATION STARTER
The previous set of challenges showed how AES performs a keyed permutation on a block of data. In practice, we need to encrypt messages much longer than a single block. A mode of operation describes how to use a cipher like AES on longer messages.
All modes have serious weaknesses when used incorrectly. The challenges in this category take you to a different section of the website where you can interact with APIs and exploit those weaknesses. Get yourself acquainted with the interface and use it to take your next flag!
source.py
from Crypto.Cipher import AES
KEY = ?
FLAG = ?
@chal.route('/block_cipher_starter/decrypt/<ciphertext>/')
def decrypt(ciphertext):
ciphertext = bytes.fromhex(ciphertext)
cipher = AES.new(KEY, AES.MODE_ECB)
try:
decrypted = cipher.decrypt(ciphertext)
except ValueError as e:
return {"error": str(e)}
return {"plaintext": decrypted.hex()}
@chal.route('/block_cipher_starter/encrypt_flag/')
def encrypt_flag():
cipher = AES.new(KEY, AES.MODE_ECB)
encrypted = cipher.encrypt(FLAG.encode())
return {"ciphertext": encrypted.hex()}
# ciphertext = 24e49b0a571db106b3392b0dc7b422b6d284081583603de51865f289806d855aEncrypt => Decrypt => To_bytes
Flag: crypto{bl0ck_c1ph3r5_4r3_f457_!}
PASSWORDS AS KEYS
It is essential that keys in symmetric-key algorithms are random bytes, instead of passwords or other predictable data. The random bytes should be generated using a cryptographically-secure pseudorandom number generator (CSPRNG). If the keys are predictable in any way, then the security level of the cipher is reduced and it may be possible for an attacker who gets access to the ciphertext to decrypt it.
Just because a key looks like it is formed of random bytes, does not mean that it necessarily is. In this case the key has been derived from a simple password using a hashing function, which makes the ciphertext crackable.
For this challenge you may script your HTTP requests to the endpoints, or alternatively attack the ciphertext offline. Good luck!
source.py
Ở đây, key được lấy từ một file words khá dài và được lấy random. Vì vậy, mình sẽ thử với tất cả key trong đó luôn
Flag: crypto{k3y5__r__n07__p455w0rdz?}
ECB CBC WTF
Here you can encrypt in CBC but only decrypt in ECB. That shouldn't be a weakness because they're different modes... right?
source.py
Flag được mã hóa bằng AES_CBC, tuy nhiên giải mã lại là AES_ECB. Nghe có vẻ khá chuối.
Ở đây, mình để ý ciphertext = iv.hex() + encrypted.hex(), vì vậy 32 kí tự hex đầu chính là iv. Dựa vào iv đó, mình sẽ giải mã CBC với iv và phương thức decrypt là ECB. Dưới đây là code thực hiện:
Flag: crypto{3cb_5uck5_4v01d_17_!!!!!}
ECB Oracle
ECB is the most simple mode, with each plaintext block encrypted entirely independently. In this case, your input is prepended to the secret flag and encrypted and that's it. We don't even provide a decrypt function. Perhaps you don't need a padding oracle when you have an "ECB oracle"?
source.py
Bài này thì đơn giản chỉ là ECB Oracle. Đấm thoi
Flag: crypto{p3n6u1n5_h473_3cb}
FLIPPING COOKIE
You can get a cookie for my website, but it won't help you read the flag... I think.
source.py
Để ý ciphertext = iv.hex() + encrypted.hex() nên ta sẽ tìm ra được iv. VIệc đó tính sau, giờ hãy nhìn vào hàm check_admin. Sau khi giải mã, nếu trong cookie có admin=True thì mới trả về flag. Vậy phải làm như nào ta.
Do yếu tố liên quan tới admin nằm ở trong block đầu tiên, thứ mà mình có thể kiểm soát thông qua IV. Ở đây mình sẽ có như sau:
Nếu sử dụng iv tùy chỉnh, quá trình giải mã sẽ như này:
chúng ta muốn plaintext_1_new có dạng b'admin=True;expir', vậy phải làm mất đi plaintext_1 và iv. Khi đó, new_iv sẽ phải như sau:
Code thực thi như sau:
Flag: crypto{4u7h3n71c4710n_15_3553n714l}
LAZY CBC
I'm just a lazy dev and want my CBC encryption to work. What's all this talk about initialisations vectors? Doesn't sound important.
source.py
Với bài này, tác giả đã sử dụng chính KEY để làm IV. Điều này vô tình tạo ra lỗ hổng. Ở đây mình sẽ khai thác dựa vào hàm receive.
Ở đây, mình sẽ dựa trên bài viết này
Code:
Flag: crypto{50m3_p30pl3_d0n7_7h1nk_IV_15_1mp0r74n7_?}
Triple DES
Data Encryption Standard was the forerunner to AES, and is still widely used in some slow-moving areas like the Payment Card Industry. This challenge demonstrates a strange weakness of DES which a secure block cipher should not have.
source.py
Khái niệm Weak Key: https://en.wikipedia.org/wiki/Weak_key#Weak_keys_in_DES
Khi sử dụng Weak Key, điều này có thể xảy ra:
Nghĩa là chỉ cần mã hóa message 2 lần bằng Weak Key, ta sẽ nhận lại chính message.
Áp dụng vào bài, mình sẽ thử dùng từng tổ hợp cặp khóa cho tới khi ra flag.
Bonus giải thích kĩ hơn 3DES: link
Flag: crypto{n0t_4ll_k3ys_4r3_g00d_k3ys}
© 2023,Pham Quoc Trung. All rights reserved.
Last updated