> For the complete documentation index, see [llms.txt](https://pikachuuu1436.gitbook.io/re_fun/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pikachuuu1436.gitbook.io/re_fun/ctf-challenges/grey-cat-the-flag-2022/flappy-o.md).

# Flappy-o

### Challenge Description

#### Flappy-o:

I know you cheated in flappy-js. This time the game is written in C, I don't think you can cheat so easily. Or can you?

Show me your skills by getting a score of at least 64.

MD5 (flappybird) = f1f36482358dc35992f076e6ea483df8

* daniellimws

#### Flappy-o 2:

This challenge uses the same binary as `flappy-o`.

Every 10000 points you reach, you unlock a character of the bonus flag. This is the real test of your skills.

MD5 (flappybird) = f1f36482358dc35992f076e6ea483df8

* daniellimws

### Challenge Details

In this challenge, we were given a 64 bit ELF file to work on and make the game show the flag for us:

![Flappy Bird!](https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2F1DS7QC415rekJecfZJDx%2Fimage.png?alt=media\&token=268746f0-9d1d-4e62-97d7-7cdf64900ae9)

Opening the program in a decompiler will show us the game loop:

![](https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2F5ODu6UA0DTpZSmizqhl1%2Fimage.png?alt=media\&token=d02142bc-6ad0-4686-a562-042db6184b5b)

Basically here it shows all the handlers for the game from processing user input to drawing the current state of the game. For this challenge, I will probably be only interested in the updateAndDrawFlag since it probably relates to the flag.

Below is the flag function:

<div align="left"><img src="https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2FqYnbeACVzfzMz5ImTka2%2Fimage.png?alt=media&amp;token=75ffa158-df3d-4a32-8906-b3228f0117b1" alt=""></div>

For flag1, one character will be decrypted everytime `actualscore` is incremented while for flag2, 4 characters will be decrypted everytime `actualscore` is incremented by 10000.

What is important to note is that lfsr2 is also run everytime `actualscore` is incremented which uses some global variables which will affect genFlag2 which is the decrypt function for flag2:

![](https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2FnOgZJg57EzKNEEOBv2ua%2Fimage.png?alt=media\&token=8d913799-4b23-4c8d-b4a7-0f9f4184c81b)

![](https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2FnXdaE49XqGphRHskFhZ6%2Fimage.png?alt=media\&token=fb69a3bf-485b-4fe8-b997-611b47bf7ec3)

lfsr2 is a seed generator that change everytime it is run, hence when solving flappy o2, this information is important also :D

### Challenge solution

Theres actually a lot of solutions to both of these challenges. One is to actually just patch it such that you don't die and remove the sleep run the game for a while (1000000 iq ngl xD)

For this writeup, I will be showing how to patch the game to get the flag (pAin)

#### Flappy o

For flappy o, simply change ++isScore in the game loop to score+= 8 by patching the following instructions:

<div align="left"><img src="https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2Fr5enwKgGBCiCJwA6FCjJ%2Fimage.png?alt=media&amp;token=37c57444-9130-40b1-81f0-cb3a7f0c7234" alt="before"></div>

to ->

<div align="left"><img src="https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2F7Q7cKj31P5hhhNtqfZjM%2Fimage.png?alt=media&amp;token=be8decc2-dd45-4edc-87da-ac61c242c2b5" alt="after!"></div>

Voila!

![Im lazy to type the flag out](https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2Fwl5gUST2oREiOxsxY0vJ%2Fimage.png?alt=media\&token=24b29ffb-d3e2-4748-a74c-10182454b949)

flag: whatever is in that picture above^

Flappy o 2:

This one is a bit more tricky as we nid to account for the 10000 points sadness. For this we need to do the following:

1. Change score+=8 back to score += 1
2. Change actual score = score\*10000 (so that decryption will happen every tick xD)
3. patch such that lfsr2 will be run 10000 times per tick oso.

The first step is the easiest part of it:

<div align="left"><img src="https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2FhOpbKvKV76GF4XSdoHQ4%2Fimage.png?alt=media&amp;token=2a7cdaf4-0305-42b6-ba57-1525c13d259e" alt=""></div>

For the 2nd step, I had to change the `sar 3` for score to `imul 0x2710`to make it:

```
actualscore = score * 10000
```

and also remove the anticheat check as actual score will increment by 10000 by tick which is forbidden by the check as it only allows actual score to be incremented by 1 per tick:

![](https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2FwbVcsRjeEN21UFshqYjg%2Fimage.png?alt=media\&token=de65cb29-5661-496f-84eb-e057a1368712)

<div align="left"><img src="https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2FjfSL9c1TN4FVMyWzxQpv%2Fimage.png?alt=media&amp;token=677376fe-de57-45e3-8de9-6d984f10560f" alt=""></div>

to patch out the check, I changed the jz to an unconditional jump `jmp` to always execute the flag decryption function no matter what.&#x20;

<div align="left"><img src="https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2FAOu49uUTO1GVLR8DWMpW%2Fimage.png?alt=media&amp;token=b793234f-f489-49c6-82f8-8e995da06b27" alt="before"></div>

<div align="left"><img src="https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2FcH3Mw7s0yBSPzcbTnr1R%2Fimage.png?alt=media&amp;token=4cf1f9be-ddc1-4460-8a55-aea0da05f9de" alt="after"></div>

For the last step, I have to remove the first flag decryption function to make way for the for loop I have to insert in to run lfsr2 10000 times. Heres the asm instructions I used:

```nasm
mov     ecx, 0
cmp     ecx, 0x2710
jz      short loc_555899B6283F
mov     edi, 1
call    lfsr2
inc     ecx
jmp     short loc_555899B62829
```

This will form the for loop to call lfsr2 9999 times. Together with the lfsr2 that is called every tick, lfsr2 will be called 10000 times per tick!

Heres the final decompiled version of the patched flag function:

<div align="left"><img src="https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2FrgvUrqj1JnZBnpQ3gyx8%2Fimage.png?alt=media&amp;token=dec26781-9fa9-4d1f-9a48-5768c78e2b85" alt=""></div>

Running the patched version will give us the flag:

![](https://328607669-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdGNpbZRd2MxlpSieDi2n%2Fuploads%2F0imC0hlJY0z63GmF5Cjl%2Fimage.png?alt=media\&token=b36cf4ad-821d-4c58-9f12-c7d29210b0d6)

Flag: whatever is in that pic above XDDD
