This year, we participated in the DEF CON CTF Quals. This is a short write up of the BIOS challenge. You can find the solution for this year’s Hashit challenge and a write up of last year’s DEF CON CTF Quals here.
The BIOS Challenge
One of the reverse engineering challenges in the DEF CON 30 CTF (Capture The Flag) Qualifiers was called BIOS.
The task was to extract a flag from the 768KiB binary.
A quick look around the strings in the file reveals that this image is based on version 1.16.0 of SeaBIOS, the open source BIOS.
Running the Code
QEMU, the open source machine emulator can load custom BIOS images, so let’s give that a try:
…and we have liftoff:
The BIOS code itself displays the bios of the CTF organizers (pun most definitely intended).
There is a simple menu where a cursor moved by the Up/Down keys selects a person and SPACE or Enter shows their info consisting of a picture and text. SPACE or Enter turns to the next page of the text and jumps back to the main menu from the last page.
(Thanks Lightning! This was fun!)
The binary contains a number of files in a Coreboot Flash container, identified by the magic
LARCHIVE. The filenames provide clear indication what each of them are for. There are a number of supporting files, such as:
names.txt lists the names of the CTF organizing team and for each there is a
.txt and a
.bmp file with their bios.
Files with a
.lzma suffix are LZMA compressed, and one file stands out from the rest:
This in very promising!
The name suggests it is an LZMA-compressed block which was encrypted some way. Let’s find out where and how.
Time to fire up the disassembler!
Looking around, we find that the code was most likely compiled with inlining enabled as the function boundaries are not very clear. Still, with the source code for SeaBIOS we can identify a fair chunk of the code with relative ease. String references in functions often give strong clues about their function and make it easy to match them to the source code.
Binary strings are rich sources of information for reverse engineering. They don’t disappoint this time either. We quickly spot references to
NAUTILUS INSTITUTE BIOS OF BIO'S which is the part where the main menu is built for example. Spending a bit of time with this part of the code allows us to identify some common functions, for example, the text display and keyboard input.
A more interesting string is
nautilus, which is referenced only once in the whole code! As it turns out it is used for comparison with a sequence of keystrokes. A-ha!
If the word
nautilus is entered at the right time, another input loop follows and expects a five-digit number. All input is timed, however, and the loop simply exits if the keys are not pressed fast enough. If the password and the five-digit number are entered in a timely manner, a romfile is loaded and its content is decrypted using the number entered as the key. There are no checks and the decrypted data is not used for anything. Clearly, this bit of code is here to show how the decryption is performed.
The binary analysis is done now.
Decrypting the Flag
The five-digit number means that there are only 100,000 possible seeds which makes this problem easy to brute-force.
We also know that the flag is LZMA-compressed so we can look for typical LZMA header values to check our key.
The seed finder completes very quickly and yields a number of hits:
31337, we should have seen that coming… 😉
We could adapt the tool to perform the actual decryption, but the original binary already does this and allows us to take a quick detour into debugging with QEMU and GDB.
QEMU’s GDB interface can be activated with GDB with a simple command-line switch:
After this QEMU is running, the program is stopped, waiting for GDB to connect.
At the GDB prompt we connect and start the program:
Now we only need a breakpoint right after the decryption and a way to enter the password and seed fast enough.
Placing the breakpoint is a bit tricky because the BIOS code is relocated at runtime, so the offset seen in the disassembly does not always match what is in the memory. One way to find out the translation is to look for a sequence of opcode bytes in the memory.
We are looking for the
free() call right after decryption, located at
0xFFFFB992 in our disassembly.
In order to place breakpoints near the real addresses, we need to calculate them by adjusting for the difference. Fortunately, GDB can do arithmetic, so this is easy to do. For example, now that we know to place a breakpoint at the location seen as
0xFFFFB952 in our disassembly, we simply say:
With the breakpoint in place, we now need to enter the password and the seed to get the decrypted block. There is a simple trick to make that happen. While QEMU is still in a stopped state (before we say
continue in GDB), we can set the emulation speed to 1% in the Speed menu. This will slow execution down enough for us to be able to type in what is needed before the splash screen disappears.
Once the breakpoint hits, it is time to get our prize:
Making Sense of the Flag
Now, with the decrypted block in place, we can try and decompress it:
Uh-oh, looks bad! But, it turns out that the resulting file is usable nevertheless.
Let’s see what we got:
A quick search later we learn about the APF image format developed by Doug Rattmann at Aperture Laboratories in 1985.
We are faced with two options: a) implement our own APF converter or b) use an existing one. To save time, we opt for one written in QBASIC, running in DOS.
Oh, no, not again. Maybe we should call Doug Rattmann on EXT. 3270. 😉
Well, it turns out that the original Aperture Science viewer from 1985 is really picky about line endings. After a quick fix we can enjoy the fruit of our labour:
…well, almost. It turned out that the correct flag is, in fact,
Descent ][ Rules, which this particular font does not make easy to see.
Since then, we learned that the LZMA decompression error message can be fixed by setting the archive file size (at offset 5) to 0xffffffffffffffff.
The encryption scheme is, apparently, Linear-feedback shift register.
This was a triumph,
We’re making a note here;
For more, follow CUJO AI Labs on Twitter!