FlareOn6 - Challenge 3: Write-Up
Tags: security, ctf
Published: 2019-09-28 17:42:42

---
title: FlareOn6 - Challenge 3: Write-Up
published: true
description: My write up for the 3rd challenge of the flareon6 reverse engineering CTF.
tags: security, ctf
---

Hello,

In this short article I will show you how I solved the third challenge of [flareon6](http://www.flare-on.com/).

The challenge is to make the bear dance. You have a tamagotchi like app and you need to keep your bear pet alive and happy.

Since the challenge is Android oriented, I've used the following tools to solve it:

* [JadX](https://github.com/skylot/jadx) - Decompiler
* [Apktool](https://ibotpeaches.github.io/Apktool/) - Decode and Rebuild

The first thing I've tried was to download JadX and look at the source code, I thought I could easily solve the challenge by patching it using Apktool, but that didn't work.

To patch the file, I used Apktool to decompiler, modified the smali code and rebuild the apk.

To decode the apk, I've ran the following command:

```bash
apktool d flarebear.apk
```

And to rebuild the apk:

```bash
apktool b flarebear
```

I don't know the ins and out of the smali language and when I've patched the files I studied the other smali source code and keep modification at a minimum. JadX helped me very much, since Java code is much easier to read and finding that block in smali is not that hard when you have Java code side by side.

Patching the apk didn't work because the program has an encrypted resource file and it uses some internal state stored in shared preferences in order to generate the password required to decrypt the resource file. I patched the app to jump directly to the `win()` function and that caused it to crash, as the generated password was not the correct one.

![win function's source-code](https://thepracticaldev.s3.amazonaws.com/i/60jzq28pobg3jiqasf2w.png)

After studying the source code a bit more with JadX, figured that the shared state can be modified by pressing the feed, play and clean buttons. Pressing the correct combination of those buttons will generate the correct password.

--

The following piece of source code gives us exactly what we need to press in order to solve the challenge and get the flag:

![the right combination.jpg](https://thepracticaldev.s3.amazonaws.com/i/v2197yjc1sa0jxywz4pu.png)

Now, we can check the `clean(), feed() and play()` function to see how they affect the shared prefs of the app. Each function affects it in some way:

![doFeed source code.jpg](https://thepracticaldev.s3.amazonaws.com/i/yk1gl6uadd5k00uanhal.png)

This is the part where I got stuck, I knew this would yield an equation to solve but I have forgotten how to solve the equation. I stopped doing math since the first year of college and I got very rusty. I tried to solve the equation mathematically but I failed. Nothing that I've tried gave good results.

Nonetheless, I've asked Twitter for help and a brilliant twitter user told me to put the values in Excel and play with them, so that's what I did.

![excell.jpg](https://thepracticaldev.s3.amazonaws.com/i/xb79vq8a53mjubu6ie4x.png)

After pressing the buttons I got the bear to dance and he gave me the flag which unlocked challenge 4.

Thanks for reading!