247CTF - The Flag Bootloader
Can you unlock the secret boot sequence hidden within our flag bootloader to recover the flag?
Analysis
1 | ❯ file flag.com |
This is a 512-byte boot sector image. Using xxd to view the hex data:
1 | 00000000: eb21 b800 10cd 16c3 60b4 0e8a 043c 0074 .!......`....<.t |
The 55 aa signature at the end confirms it is a standard boot sector.
Boot Sector Fundamentals
When a computer powers on and completes the Power-On Self-Test (POST), the BIOS reads the first sector (512 bytes) of the disk into physical memory between 0x7C00 and 0x7DFF. It then sets the CPU’s Instruction Pointer (IP) to 0x7C00 to begin execution.
Therefore, the base address of this program in memory is 0x7C00. When performing static analysis or debugging, any hardcoded physical addresses must have 0x7C00 subtracted to find their corresponding file offset.
Static Analysis and Memory Mapping
Analyzing the assembly code reveals two critical memory locations:
1. Flag Ciphertext Area (0x7DAA)
Calculate the file offset:
$$0x7DAA - 0x7C00 = 0x01AA$$
Looking at the hex dump at 0x01AA:
1 | 000001a0: 6420 636f 6465 210a 0d00 3234 3743 5446 d code!...247CTF |
The ciphertext indeed begins here, with the first 7 bytes representing the 247CTF{ prefix.
2. Input Buffer (0x7DEC)
Calculate the file offset:
$$0x7DEC - 0x7C00 = 0x01EC$$
The region of null bytes before the 55 aa signature is used directly as a buffer for keyboard input.
Core Logic Deconstruction
Focusing on sub_1016A in IDA (the primary decryption logic):
1 | seg000:016B mov bx, 7DECh ; BX points to the input buffer |
Logic Summary:
- The program requires a 16-character unlock code.
- Each character is validated via arithmetic/logical operations (XOR, ADD, SUB).
- Validated characters serve as XOR keys to decrypt the subsequent ciphertext region.
- Each key character decrypts 2 bytes of ciphertext.
Solution Script
We can simulate the assembly operations to recover the unlock code and then use it to XOR the ciphertext bytes.
1 | #!/usr/bin/env python3 |