Practice
Hack The Boo 2023 - Practice
CRYPTOGRAPHY WRITEUP
Author:
Pham Quoc Trung
Used Language:
Python3
Problem Solving:
Hexoding
In order to be a successful ghost in the modern society, a ghost must fear nothing. Caspersky always loved scaring people, but he could not reach his maximum potential because he was fearful of cryptography. This is why he wants to join the Applied Cryptography Academy of Ghosts. To gain admission, the professors give you a challenge that you need to solve. They try to spook you with weird functions, but don't be scared; the challenge can be solved even without the source code. Can you help Caspersky pass the entrance exams?
Attachments: source.py
from secret import FLAG
HEX_CHARS = '0123456789abcdef'
B64_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
def to_hex(data):
data = int.from_bytes(data, 'big')
encoded = ''
while data:
i = data % 16
encoded = HEX_CHARS[i] + encoded
data >>= 4
return '0' * (len(encoded) % 2) + encoded
def to_base64(data):
padding_length = 0
if len(data) % 3 != 0:
padding_length = (len(data) + 3 - len(data) % 3) - len(data)
data += b'\x00' * padding_length
bits = ''.join([bin(c)[2:].zfill(8) for c in data])
blocks = [bits[i:i+6] for i in range(0, len(bits), 6)]
encoded = ''
for block in blocks:
encoded += B64_CHARS[int(block, 2)]
return encoded[:-padding_length] + '=' * padding_length
def main():
first_half = FLAG[:len(FLAG)//2]
second_half = FLAG[len(FLAG)//2:]
hex_encoded = to_hex(first_half)
base64_encoded = to_base64(second_half)
with open('output.txt', 'w') as f:
f.write(f'{hex_encoded}\n{base64_encoded}')
main()output.txt
Đọc lướt qua thì output chỉ đơn giản là 1 dòng hex và 1 dòng base64 là 2 mảnh của flag. Đáp lên CyberChef là ra flag thoai.
Flag: HTB{kn0w1ng_h0w_t0_1d3nt1fy_3nc0d1ng_sch3m3s_1s_cruc14l_f0r_a_crypt0gr4ph3r___4ls0_d0_n0t_c0nfus3_enc0d1ng_w1th_encryp510n!}
SPG
After successfully joining the academy, there is a process where you have to log in to eclass in order to access notes in each class and get the current updates for the ongoing prank labs. When you attempt to log in, though, your browser crashes, and all your files get encrypted. This is yet another prank for the newcomers. The only thing provided is the password generator script. Can you crack it, unlock your files, and log in to the spooky platform?
Attachments: source.py
output.txt
Mấu chốt của bài này là tìm ra MASTER_KEY làm key cho vào AES_CBC để giải ra flag.
Ban đầu, MASTER_KEY được chuyển thành 1 số nguyên master_key theo Little-Endian. Password được tạo ra bằng cách chia ALPHABET ra làm 2 nửa. Nếu bit cuối cùng của master_key = 1 thì thêm vào password kí tự ngẫu nhiên ở nửa trái của ALPHABET, nếu là 0 thì ngược lại. Ta hoàn toàn có thể khôi phục từng bit của master_key bằng cách so sánh từng kí tự của password xem nó thuộc nửa nào của ALPHABET. Nếu nửa bên trái thì bit cuối cùng của master_key bằng 1, ngược lại thì là 0.
Sau khi ra được master_key, ta chuyển nó sang dạng bytes và đảo ngược lại (vì là Little-Endian) sẽ ra được MASTER_KEY ban đầu.
Dưới đây là code thực thi:
Flag: HTB{00ps___n0t_th4t_h4rd_t0_r3c0v3r_th3_m4st3r_k3y_0f_my_p4ssw0rd_g3n3r4t0r}
yence
During the pranking labs, the ghosts create a spooky encryption algorithm, and at midnight, they go outside to scare people by encrypting every device they own. To assess the situation at the end of the night, the professors have developed a spooky meter to measure how much people were spooked by the ransomware. The goal is to create irreversible ransomware that inflicts maximum damage. However, having interacted with humans over the past months, you've grown fond of them and don't want to harm them. You even managed to befriend a human who fell victim to such an attack. Can you help your friend unlock his files?
Attachments: source.py
messages.txt
output.txt
Ở đây, tác giả sử dụng AES_CTR cho từng message một (mỗi lần encrypt là một lần gọi AES_CTR mới). Key của mỗi cái cũng là 16-bytes random, gần như không thể brute-force nổi. Có thể nói rằng thử thách này không thể khai thác bằng các lỗ hổng CTR thông thường. Vậy thử đọc kĩ code xem có điều gì vô tình tạo ra điểm yếu không?
Và mình tìm ra có điều bất thường ở đoạn code này:
initial_value =i, và i được chạy trong range(len(MSG)), ở đây là từ 0 tới 3. Điều này tạo ra nguy hiểm gì?
Để hiểu phần sau mình sẽ để lại sơ đồ AES_CTR ở đây:
Ở đây mình có tính được độ dài của 4 output lần lượt là 64, 48, 64 và 80-bytes. Dựa trên code trên, counter của từng cái sẽ đếm theo thứ tự như sau:
Ứng với 1 giá trị của iv, block cypher encryption sẽ luôn không đổi. Thứ ta cần tìm là message thứ 3 chứa flag, 3 message còn lại đã biết rõ. Vì vậy, ta có thể tính các block encrypt có iv = 2,3,4,5 từ các message đã biết để tìm ra flag. Để ví dụ:
block encryption iv = 2=block thứ 3 của msg1XORblock thứ 3 của cipher1Khi đó,
block đầu tiên của msg3=block encryption iv = 2XORblock đầu của cipher3Tương tự với các block còn lại
Code thực thi:
Flag: HTB{m4k3_sur3_y0u_1n1t14l1z3_4rr4ys_th3_r1ght_w4y}
© 2023,Pham Quoc Trung. All rights reserved.
Last updated