HackTheBox Inizialization Writeup
Explore the basics of cybersecurity in the Inizialization Challenge on Hack The Box. This very-easy-level Challenge introduces encryption reversal and file handling concepts in a clear and accessible way, perfect for beginners.
Provided Output
messages.txt
1
2
3
4
5
6
[
'This is some public information that can be read out loud.',
'No one can crack our encryption algorithm.',
'HTB{?????????????????????????????????????????????}',
'Secret information is encrypted with Advanced Encryption Standards.',
]
output.txt
1
2
3
4
2ac199d1395745812e3e5d3c4dc995cd2f2a076426b70fd5209cdd5ddc0a0c372feb3909956a791702180f591a63af184c27a6ba2fd61c1741ea0818142d0b92
30c6d0cd775b16c23c3f103a1fd883c4632c11366fbc07d92088cc5ddc0a0c373aef3f12c7606c114f546c7f6e00c87a
36fdb2d97d0a5bcf0225586a1e8abfc62d3057273aab5ae5309d8c4ade060a236aed070d817b2c14110e590b1b27ef5d4d35ddc001b47d6c2bca00101c25039a
2dcc93d07c4a16c833375f2b00d894c62c2d442d3cf90cd43183c559c10006372cea2c1595487c0f4314091c0c268b120f3aaabe7bd31c0c05977a7f7c4f6ce6f59392e0e522e66500e153f7a6f914c7
Provided Script
source.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python3
import os
from Crypto.Util import Counter
from Crypto.Util.Padding import pad
from Crypto.Cipher import AES
class AdvancedEncryption:
def __init__(self, block_size):
self.KEYS = self.generate_encryption_keys()
self.CTRs = [Counter.new(block_size) for i in range(len(MSG))] # nonce reuse : avoided!
def generate_encryption_keys(self):
keys = [[b'\x00']*16] * len(MSG)
for i in range(len(keys)):
for j in range(len(keys[i])):
keys[i][j] = os.urandom(1)
return keys
def encrypt(self, i, msg):
key = b''.join(self.KEYS[i])
ctr = self.CTRs[i]
cipher = AES.new(key, AES.MODE_CTR, counter=ctr)
return cipher.encrypt(pad(msg.encode(), 16))
def main():
AE = AdvancedEncryption(128)
with open('output.txt', 'w') as f:
for i in range(len(MSG)):
ct = AE.encrypt(i, MSG[i])
f.write(ct.hex()+'\n')
if __name__ == '__main__':
with open('messages.txt') as f:
MSG = eval(f.read())
main()
Proof of Concept (PoC)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/env python3
from Crypto.Util.Padding import unpad
from pwn import xor
def main():
with open('messages.txt', 'r') as f:
plaintexts = eval(f.read())
with open('output.txt', 'r') as f:
ciphertexts = [bytes.fromhex(line.strip()) for line in f.readlines()]
keystream = xor(plaintexts[0].encode(), ciphertexts[0])
flag = xor(keystream, ciphertexts[2])
print("Decrypted flag:", flag)
if __name__ == "__main__":
main()
Summary
Inizialization Challenge on Hack The Box demonstrates a vulnerability in AES encryption due to reused keys and nonces in CTR mode. In this challenge, several plaintext messages are encrypted with unique keys but identical CTR counters, leading to keystream reuse. The PoC exploits this by XORing a known plaintext with its ciphertext to derive the keystream, which is then applied to another ciphertext to decrypt and reveal the hidden flag.
This post is licensed under CC BY 4.0 by the author.