Cyber Apocalypse 2024 Hacker Royale
CYBER APOCALYPSE 2024 HACKER ROYALE
CRYPTOGRAPHY WRITEUP
Author:
Pham Quoc Trung
Used Language:
Python3
Problem Solving:
Dynastic
Description:
You find yourself trapped inside a sealed gas chamber, and suddenly, the air is pierced by the sound of a distorted voice played through a pre-recorded tape. Through this eerie transmission, you discover that within the next 15 minutes, this very chamber will be inundated with lethal hydrogen cyanide. As the tape’s message concludes, a sudden mechanical whirring fills the chamber, followed by the ominous ticking of a clock. You realise that each beat is one step closer to death. Darkness envelops you, your right hand restrained by handcuffs, and the exit door is locked. Your situation deteriorates as you realise that both the door and the handcuffs demand the same passcode to unlock. Panic is a luxury you cannot afford; swift action is imperative. As you explore your surroundings, your trembling fingers encounter a torch. Instantly, upon flipping the switch, the chamber is bathed in a dim glow, unveiling cryptic letters etched into the walls and a disturbing image of a Roman emperor drawn in blood. Decrypting the letters will provide you the key required to unlock the locks. Use the torch wisely as its battery is almost drained out!
Attachments:
source.py
output.txt
Solution:
Cơ chế mã hóa của bài này khá đơn giản. Với mỗi kí tự là chữ cái trong flag, ta sẽ thực hiện lần lượt 2 việc:
Tính ra giá trị
chi
bằng cách lấy giá trị ascii của chữ cái đó trừ đi 0x41 (65)Lấy ra giá trị char của
chi + i
bằng cách chia lấy dư cho 26 và cộng thêm 0x41, vớii
là index của chữ cái đó trong flagCác chữ cái lấy được sẽ tạo ra ciphertext.
Sau một hồi phân tích dựa trên bảng ASCII trên, mình nhận ra chỉ cần sửa đoạn from_identity_map(chi + i)
thành from_identity_map(chi - i)
là có thể lấy được flag ban đầu. Dưới đây là script giải mã:
Flag: HTB{DID_YOU_KNOW_ABOUT_THE_TRITHEMIUS_CIPHER?!_IT_IS_SIMILAR_TO_CAESAR_CIPHER}
Makeshift
Description:
Weak and starved, you struggle to plod on. Food is a commodity at this stage, but you can’t lose your alertness - to do so would spell death. You realise that to survive you will need a weapon, both to kill and to hunt, but the field is bare of stones. As you drop your body to the floor, something sharp sticks out of the undergrowth and into your thigh. As you grab a hold and pull it out, you realise it’s a long stick; not the finest of weapons, but once sharpened could be the difference between dying of hunger and dying with honour in combat.
Attachments:
source.py
output.txt
Solution:
Bài này thì đầu tiên flag sẽ bị reverse, sau đó với mỗi cụm 3 ký tự của flag, ký tự đầu tiên sẽ bị đẩy xuống cuối. Vậy thì để giải, ta chỉ cần đẩy ký tự cuối của mỗi cụm 3 ký tự về đầu tiên và đảo ngược flag cuối cùng thu được.
Flag: HTB{4_b3tTeR_w3apOn_i5_n3edeD!?!}
Primary Knowledge
Description:
Surrounded by an untamed forest and the serene waters of the Primus river, your sole objective is surviving for 24 hours. Yet, survival is far from guaranteed as the area is full of Rattlesnakes, Spiders and Alligators and the weather fluctuates unpredictably, shifting from scorching heat to torrential downpours with each passing hour. Threat is compounded by the existence of a virtual circle which shrinks every minute that passes. Anything caught beyond its bounds, is consumed by flames, leaving only ashes in its wake. As the time sleeps away, you need to prioritise your actions secure your surviving tools. Every decision becomes a matter of life and death. Will you focus on securing a shelter to sleep, protect yourself against the dangers of the wilderness, or seek out means of navigating the Primus’ waters?
Attachments:
source.py
output.txt
Solution:
Đây là một bài về RSA đã biết sẵn n
, e
và c
. Ở đây có thể thấy n
là một số nguyên tố, áp dụng kiến thức về hàm phi Euler ta có thể dễ dàng tính ra phi = n - 1
, từ đó tính ra d
và lấy được flag.
Flag: HTB{0h_d4mn_4ny7h1ng_r41s3d_t0_0_1s_1!!!}
Iced TEA
Description:
Locked within a cabin crafted entirely from ice, you're enveloped in a chilling silence. Your eyes land upon an old notebook, its pages adorned with thousands of cryptic mathematical symbols. Tasked with deciphering these enigmatic glyphs to secure your escape, you set to work, your fingers tracing each intricate curve and line with determination. As you delve deeper into the mysterious symbols, you notice that patterns appear in several pages and a glimmer of hope begins to emerge. Time is flying and the temperature is dropping, will you make it before you become one with the cabin?
Attachments:
source.py
output.txt:
Solution:
Đây là một bài về Tiny Encryption Algorithm (https://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm). Có 2 mode là ECB và CBC, và nó sẽ được chọn dựa trên lúc khai báo object. Nhìn vào hàm main, ta thấy chỉ có giá trị KEY
được truyền vào nên mode của bài này là ECB.
Đầu tiên, ta sẽ nhìn vào hàm encrypt_block
:
Có thể thấy, với mỗi block dài 8 bytes của flag, ta sẽ tiến hành mã hóa lần lượt như sau:
Chia block thành 2 blocks 4 bytes
m0
vàm1
. Chuyển dữ liệu của 2 block này từ bytes thành long.Gán KEY vào biến
K
và khởi tạo mặt nạmsk = (1 << (BLOCK_SIZE//2)) - 1
. Ở đâyBLOCK_SIZE = 64
, ta có:1 << 64 // 2 = 0b100000000000000000000000000000000 (33-bits)
0b100000000000000000000000000000000 - 1 = 0b11111111111111111111111111111111 (32-bits) hay 0xFFFFFFFF
Mặt nạ này có tác dụng đảm bảo các block con sẽ có độ dài là 4 bytes ở đoạn sau
self.DELTA
là một hằng số, thường được chọn để đảm bảo việc phân phối đồng đều các giá trị trong quá trình mã hóaQuá trình mã hóa diễn ra qua 32 vòng lặp. Trong mỗi vòng lặp, giá trị của
m0
vàm1
được cập nhật dựa trên công thức tính có sử dụng phép cộng, phép XOR, và các phép dịch bit. Các phép toán này kết hợp giữam0
vàm1
với các khóa con (K[0]
,K[1]
,K[2]
,K[3]
) và giá trị đếms
, được tăng dần sau mỗi vòng lặp bằng cách cộng vớiself.DELTA
. Mỗi lần cập nhậtm0
vàm1
, chúng được giới hạn bởimsk
để đảm bảo kết quả nằm trong phạm vi kích thước khối.Gộp 2 giá trị thu được sau cùng của
m0
vàm1
với nhau. Sử dụng mặt nạ để đảm bảo kích thước là 64-bits. Kết quả trả về được chuyển lại về dạng bytes
Dựa vào phân tích, mình có thể tiến hành giải mã như sau:
Chuyển cipher từ bytes về dạng long
Tách số thu được ra làm 2 để lấy được
m1
vàm0
. Các giá trị mặt nạ vàs
vẫn giữ nguyênQuá trình giải mã diễn ra qua 32 vòng lặp. Ta chỉ đơn giản là làm ngược lại các bước của code mã hóa.
Ghép 2 giá trị thu được của
m0
vàm1
lại với nhau về chuyển lại về dạng bytes.
Dưới đây là code giải mã block
Về hàm encrypt
thì chỉ đơn giản là chia message thành từng block 8 bytes và tiến hành mã hóa. Mode ECB và CBC có thể hình dung giống sơ đồ, với khối mã hóa chính là hàm encrypt_block
Cách giải mã:
Code giải mã:
Dài dòng nãy giờ rồi, bài này đã cho ta biết KEY
và cipher c
. Mode sử dụng là ECB. Đơn giản là giải mã thôi
Final Script:
Flag: HTB{th1s_1s_th3_t1ny_3ncryp710n_4lg0r1thm_____y0u_m1ght_h4v3_4lr34dy_s7umbl3d_up0n_1t_1f_y0u_d0_r3v3rs1ng}
Tsayaki
Description:
You find yourself in the middle of a deadly ancient maze. The maze sprawls before you, its secrets veiled in shadows, its gates locked tight against intruders. With thousands of keys shimmering under the harsh light, you steel yourself for the daunting challenge ahead. Each chamber of the maze presents a new puzzle to unravel, each gate a barrier to overcome. Armed with determination and resolve, you set forth into the labyrinth's depths, knowing that your survival hinges on unlocking the path forward by finding the proper key. With each new chamber you enter, you are greeted with a cup of tea—a brief respite from the perilous journey that lies ahead. But the tea is not the only gift bestowed upon you in these chambers. With each cup, you receive a hint that will guide you on how to move on. NOTE: 'tea.py' can be found in the challenge 'Iced Tea'
Attachments:
source.py
Solution:
Bài này import class TEA chính là class Cipher trong bài trước. Dựa vào dòng cipher = TEA(key, IV)
, có thể thấy lần này mode được sử dụng là CBC.
Về chương trình mà đoạn code này thực hiện, nó sẽ như sau:
Server sẽ cung cấp cho chúng ta một message dạng hex
Ta được yêu cầu gửi lên ciphertext và key của chúng ta tạo từ message trên. Nếu ciphertext của server cũng giống với ciphertext của ta thì sẽ là hợp lệ. Cái khó ở bước này là ta bị yêu cầu nhập 4 keys cho 1 message sao cho nó phải trả về cùng 1 ciphertext. Các keys không được trùng nhau.
Lặp lại như thế 10 lần (các ciphertext cũng không được giống nhau), ta sẽ thu được flag
Trước hết, để có thể tạo ra ciphertext đúng thì ta phải tìm ra được IV. Nhìn vào code chương trình, có thể thấy với ciphertext và key đúng định dạng, nếu ciphertext không trùng với ciphertext của hệ thống thì ta sẽ có được ciphertext đúng.
Từ đây, ta đã có plaintext, ciphertext tương ứng và đã biết thuật toán của khối mã hóa. Hãy nhìn vào sơ đồ mã hóa
Với 3 dữ kiện trên, ta có thể tính ra IV = D(ct1) XOR pt1
, với ct1 là block đầu của ciphertext, pt1 là block đầu của plaintext. IV
được import từ secret
, nên giá trị này sẽ không đổi (và mình cũng đã test)
Last updated