> For the complete documentation index, see [llms.txt](https://0xsec.gitbook.io/0xsec/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://0xsec.gitbook.io/0xsec/ctf-writeups/2025-08-12-gameboy.md).

# GameBoy (WHYCTF2025)

### Initial Analysis

We initially got a .rar archive file and extracting ti gave a file named `chall.gb` which has extension of .gb which means its a GameBoy rom file So we likely have to find the flag from this rom file. For that i googled and found some GBA emulators with debugger but the one which i found useful was [mGBA](https://mgba.io/) as we can write into the memory using its debugger.I tried using `strings` but didn't found anything useful.Then i imported that rom into gba emulator to find how the game works and found out that we can only press keys `Right,Left,Up,Down,Multi(when multiple keys are pressed),A,B`.

<figure><img src="https://704281641-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHmcGF1MgN1IYT3jBclZr%2Fuploads%2Fgit-blob-d3e7a09d1a4655409647d54a59e2b2f5b801d497%2Fgameboy_1.png?alt=media" alt=""><figcaption></figcaption></figure>

<p align="center"><em>Figure 1: mGBA</em></p>

I found that we can use a plugin [GhidraBoy](https://github.com/Gekkio/GhidraBoy) by Gekkio to disassemble the rom file in ghidra so I added that to ghidra.

### Deep Dive

I opened the challenge rom file in ghidra and clicked on analyze after that i got this view these initial lines of code are for initilisation of rom into gba.

<figure><img src="https://704281641-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHmcGF1MgN1IYT3jBclZr%2Fuploads%2Fgit-blob-f1e4a72765261d21844f9dd9aa1db9e99b48f913%2Fghidra_1.png?alt=media" alt=""><figcaption></figcaption></figure>

<p align="center"><em>Figure 2: Rom in Ghidra</em></p>

When you scroll down you will find a function call `FUN_036b` which is main function of the game

<figure><img src="https://704281641-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHmcGF1MgN1IYT3jBclZr%2Fuploads%2Fgit-blob-f1a08516dd7a2201878aca7647e9dff2ae07fb5f%2Fghidra_2.png?alt=media" alt=""><figcaption></figcaption></figure>

<p align="center"><em>Figure 3: Main Game Function</em></p>

In the main game function the first thing i noticed was strings `Press START` and `NOW THE CHEAT PATTERN` and in the function `FUN_05e1` which they were used is most likely to the `printFunction` so rename it accordingly.

<figure><img src="https://704281641-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHmcGF1MgN1IYT3jBclZr%2Fuploads%2Fgit-blob-23d164ece32e6c04830aeea63cbe2d01ae95c6d1%2Fghidra_3.png?alt=media" alt=""><figcaption></figcaption></figure>

<p align="center"><em>Figure 4: Main Game Function-1</em></p>

There is another function in-between strings which is taking `0x80` as argument which is a loop for key inputs and also note the variable `uStack_f` which is storing `0xb<-->11` next things is analyze the do while loop there was a function `FUN_057e` which is `KeyInputFunction` and is stored in variable `local_5` .It then checks your input with `null` if you pressed a key or not, again there's a `FUN_0247` which converts the raw input data of gamepad from p1 register to readable button name.

Then comes a nested if-statement which checks your InputKey/Button with the correct number of sequence `local_4` which was stored in `cStack_10` if its correct then it increments `local_4` by 1. It then checks if the `local_4` is equal to `0xb` if it satisfies then it calls `FUN_02c9` which is likely to be the `flagFunction` otherwise your correct\_sequence revert back to zero and this loop continues.

### Getting the flag

Till now we have understood the working of the game rom and concluded if have to get the flag we have to set the `local_4` which no of correct sequence to `11 or 0xb`.Now its time to do it

Open the mGBA emulator load the rom and open the debugger now we have to set the breakpoint where it increments the `local_4` which is at address `$03cc`

<figure><img src="https://704281641-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHmcGF1MgN1IYT3jBclZr%2Fuploads%2Fgit-blob-85daa25cbcd61d849eaa5820e179a62a14a8cdda%2Fdbg_1.png?alt=media" alt=""><figcaption></figcaption></figure>

<p align="center"><em>Figure 5: Setting Breakpoint</em></p>

Now contniue the debugger and play the game untill you hit the breakpoint and do `dis` in order to confirm it.

<figure><img src="https://704281641-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHmcGF1MgN1IYT3jBclZr%2Fuploads%2Fgit-blob-11a7f3a2cda7072bf786bbe0bf16d1b9f4bfcb75%2Fdbg_2.png?alt=media" alt=""><figcaption></figcaption></figure>

<p align="center"><em>Figure 6: Hitting Breakpoint</em></p>

Do `next` and go to next instruction which loads the incremented value into `A` register and then do its comparision with the `0xb`. Now do `status` to check the current status of the registers and now we have to write `0xb` to the `HL` register to do it `w/1 hl 0x0b` which writes the `local_4` to 11 and continue.

YOU GOT THE FLAG!!!!

<figure><img src="https://704281641-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHmcGF1MgN1IYT3jBclZr%2Fuploads%2Fgit-blob-3d163936e960403243717c0371a34c2ef632ae04%2Fflag.png?alt=media" alt=""><figcaption></figcaption></figure>

<p align="center"><em>Figure 5: Flag</em></p>

Thanks For Reading
