Skip to content

FortID CTF 2026

Meteor Shower

Writeup for Meteor Shower, an NES rev challenge from FortID CTF 2026.

FortID CTF 2026

Meteor Shower

Category: Reverse Engineering

Author: ipaljak

Description:

Great Scott! The biggest meteor storm in the galaxy is headed straight for your sector. ☄️ 🚀

Note: wrap the phrase into FortID{}, and use _ to separate the words.

TL;DR

The ROM is a small NES game where you dodge meteors 180 times. Winning the game gives nothing. Hidden behind it is a cheat code: the game silently records every A and B press, and after 95 presses it checks them against data in the ROM. The correct 95 presses, read five at a time as Bacon’s cipher, spell the flag phrase.

First look

We get a single file, meteor_shower.nes. The first 16 bytes are the iNES header:

Text
$ xxd -l 16 meteor_shower.nes
00000000: 4e45 531a 0201 0000 0100 0000 0000 0000  NES.............

Running it in an emulator shows a title screen, and then a game where you move a ship with the D-pad while meteors fall. A timer counts down from 180.

I survived once to see what happens (was fun). no flag, so the answer had to be somewhere in the code.

Loading it in Ghidra

I loaded the ROM in Ghidra with an NES loader extension. refer here [external]. The loader reads the vectors at the end of the ROM and labels reset and vblank for you, so that is where I started.

One thing that confused me at first: the listing opens on the RAM block, which is all ?? because RAM is empty in a static file. The actual code is in the PRG block at $8000.

The main loop

After the usual hardware setup and RAM clearing, reset falls into the main loop. Cleaned up, it looks like this:

C
do {
    while (frame_ready == 0) {}      // wait for vblank
    frame_ready = 0;
    FUN_80c1();                      // read controller
    FUN_80ef();                      // ???
    if (screen_to_draw != 0) FUN_83d4();
    if      (game_state == 0) FUN_81a8();   // title
    else if (game_state == 1) FUN_8255();   // playing
    else if (game_state == 2 || game_state == 3) FUN_81ba();  // game over / survived
    FUN_837e();                      // sprites
} while (true);

DAT_0004 is the game state. States 0 to 3 are handled here, but two things stood out:

  • FUN_80ef runs every single frame, before the state switch, no matter which screen you are on.
  • Nothing in this loop handles state 4, but the game clearly knows about it (see below).

The controller reader

FUN_80c1 latches the controller through $4016, reads it 8 times, and ORs in a value from a table at $8601 for each button that is down. The table is:

01 02 04 08 10 20 40 80

The NES always reports buttons in the order A, B, Select, Start, Up, Down, Left, Right. So A is bit 0x01 and B is bit 0x02. The function stores the held buttons in DAT_0001 and the newly pressed buttons in DAT_0003.

(refer here [external])

The movement code only ever tests 0x10, 0x20, 0x40 and 0x80 (the D-pad), and the menus test 0x08 (Start). The game itself never uses A or B.

The hidden recorder

That is what makes FUN_80ef suspicious. It is the only place that looks at A and B:

C
void FUN_80ef(void) {
    if (game_state != 4 && (buttons_pressed & 3) != 0) {
        if      ((buttons & 3) == 1) bit = 0;     // A
        else if ((buttons & 3) == 2) bit = 1;     // B
        else return;

        if (bit) input_bits[byte_idx] |= bit_mask;    // buffer at $0400
        bit_mask >>= 1;
        if (bit_mask == 0) { byte_idx++; bit_mask = 0x80; }

        count++;
        if (count == 0x5f) {
            if (FUN_8158()) {          // result
                game_state = 4;
                screen_to_draw = 5;
                return;
            }
            FUN_8143();                // wipe the buffer and start over
        }
    }
}

So every A press is stored as a 0 and every B press as a 1, packed into a buffer at $0400. After 0x5f = 95 presses, FUN_8158 decides whether we enter the secret state 4.

The checker

C
void FUN_8158(void) {
    mismatch = 0;
    i = 0;
    idx = 0xb;
    do {
        my_bit = (input_bits[i >> 3] & mask_table[i & 7]) != 0;
        mismatch |= ((table_8718[idx] ^ table_8798[idx] ^ i) & 1) ^ my_bit;
        idx = (idx + 0x25) & 0x7f;
        i++;
    } while (i != 0x5f);
}

For each of the 95 presses, the expected bit is:

(table_8718[idx] ^ table_8798[idx] ^ i) & 1

Getting the presses

The tables are at $8718 and $8798. After dropping the 16 byte header, a ROM address maps to a file position by subtracting 0x8000.

Python
import sys

rom = open(sys.argv[1], "rb").read()[16:]   # skip the 16-byte header

presses = ""
idx = 0x0B
for i in range(95):
    bit = (rom[0x718 + idx] ^ rom[0x798 + idx] ^ i) & 1
    presses += "AB"[bit]                    # 0 -> A, 1 -> B
    idx = (idx + 0x25) & 0x7F

print(presses)
$ python3 presses.py meteor_shower.nes
BAABAAAABAABAAAAABAAABBABBAABBABAAAAAAAAABBBBABBBABAABBAABAAABBABBAABBABAAAAAAAAAABAABAABABAABB

Seeing the easter egg

Typing 95 presses by hand is difficult, and a single mistake silently resets the buffer. FCEUX [external] can run Lua scripts, so I let it type the code on the title screen:

lua
local seq = "BAABAAAABAABAAAAABAAABBABBAABBABAAAAAAAAABBBBABBBABAABBAABAAABBABBAABBABAAAAAAAAAABAABAABABAABB"

for _ = 1, 120 do emu.frameadvance() end

for c in seq:gmatch(".") do
  for f = 1, 8 do
    joypad.set(1, {A = (f <= 4 and c == "A"), B = (f <= 4 and c == "B")})
    emu.frameadvance()
  end
end
$ fceux --loadlua meteor.lua meteor_shower.nes

Each button is held for 4 frames and released for 4. The release matters because the game only counts newly pressed buttons.

After the last press the screen changes:

Bacon and eggs

“Bacon” plus “five at a time” plus a secret made only of A’s and B’s is Bacon’s cipher. Each letter is five symbols of A or B, which is the same as a 5 bit binary number with A = 0 and B = 1. And 95 presses is exactly 19 groups of five.

SCIENTIAPOTENTIAEST

“Scientia potentia est” is Latin for “knowledge is power”, a phrase related to Francis Bacon :0

Flag

FortID{SCIENTIA_POTENTIA_EST}

Challenge files

Discussion

Loading discussion…

Related solves

Keep reading

Complete archive