# Mobile - Red Light Green Light

## Description

You are stuck in a game of red light green light, to win you need to press the button when the light is green. Wait all you want, the light has never changed to green for me :(

## Downloads

{% file src="/files/94aYq7gNrzmNZtbPFvUU" %}

## Solution

Decompile the .apk using an [online decompiler](http://www.javadecompilers.com/apk).

<figure><img src="/files/r5Cb7hQfZ93xU8kgxEHK" alt=""><figcaption><p>Decompile .apk</p></figcaption></figure>

Head over to `sources -> com -> nahamcon2023 -> redlightgreenlight` and open up the `MainActivity.java` file to find out what the app is doing.

<figure><img src="/files/1aLFeigStZMM8FLebQfT" alt=""><figcaption><p>Decompiled .java file from .dex format</p></figcaption></figure>

The main logic of the app is in the `checkLight()` function.

<figure><img src="/files/Lw7EJhk60qaANNIOOe7l" alt=""><figcaption><p><code>checklight()</code></p></figcaption></figure>

It is also worth looking at the `decrypt()` function in the `Decrypt.java` file.

<figure><img src="/files/M8AEow6kFIPXgMpDurKu" alt=""><figcaption><p><code>decrypt()</code></p></figcaption></figure>

In summary, this is what the app is doing:

1. Get the decryption key using the Java native function `getKey()`
2. Check if the light is red
3. If the light is red, the app will show that "You cannot move right now\..."
4. If the light is not red, the app will decrypt the AES-encrypted image with the key from step 1 and display the decrypted image

The first thing I attempted was to look into the native `getKey()` function and that function can be found in the following shared library file: **resources\lib\x86\_64\libredlightgreenlight.so**

<figure><img src="/files/7RuLhzISvXEM3SnEuzVM" alt=""><figcaption><p>The native function as seen in IDA</p></figcaption></figure>

The decompilation is only a few lines of code but we will quickly see that the return value goes through a `fastcall` before returning.

<figure><img src="/files/XZDq1bakOG2qkjRhkinm" alt=""><figcaption><p><code>__fastcall</code> at line 46</p></figcaption></figure>

At this point I tried to use Frida to call this native function and to see the return value dynamically but unfortunately, I couldn't get Frida to attach to the app without crashing it.

Instead, I worked on modifying the .apk instead. Firstly, we need **Apktool** and the instructions on how to install it can be found [here](https://ibotpeaches.github.io/Apktool/install/). Let's take a look again at the `checkLight()` function.

<figure><img src="/files/L5Q3Y0oGruIv7icaBzow" alt=""><figcaption><p><code>checkLight()</code></p></figcaption></figure>

Our goal is to modify line 33 and flip the predicate from `if (!this.red)` to `if (this.red)`.&#x20;

The steps:

1. Using **Apktool**, we can decompile the .apk once again and get the smali files.

```
$ apktool d red_light_green_light.apk
```

<figure><img src="/files/YF6Jun1UiBq4RrcQQfrg" alt=""><figcaption><p>Decompile using Apktool</p></figcaption></figure>

2. Inside the decompiled folder, open up the **AndroidManifest.xml** file and change the `android:extractNativeLibs="false"` property to `true`. I am not sure why but if you skip this step, you will have issues installing the apk later.

<figure><img src="/files/RplaKnzCOJnqATFed5gA" alt=""><figcaption><p>Inside AndroidManifest.xml</p></figcaption></figure>

3. Next, navigate to: **red\_light\_green\_light\smali\com\nahamcon2023\redlightgreenlight**

<figure><img src="/files/3p9UeMVMZLTidHO8qUBM" alt=""><figcaption><p>smali files</p></figcaption></figure>

3. Open up `MainActivity.smali` and what we are looking for is at line 115.

<figure><img src="/files/D8NJ0bV74WKgMjU3K5Hf" alt=""><figcaption><p><code>if (!this.red)</code> in smali</p></figcaption></figure>

4. Modify the predicate to `if-eqz` and rebuild the apk using **Apktool**

```
$ apktool b <folder_name> -o <output_apk_name>
```

<figure><img src="/files/HcoC1Sqd5sHdw35LPHyt" alt=""><figcaption><p>Rebuild .apk</p></figcaption></figure>

5. Generate keystore for signing the .apk

```
keytool -genkey -v -keystore my.keystore -alias redlightks -sigalg MD5withRSA -keyalg RSA -keysize 2048 -validity 7300
```

<figure><img src="/files/HQCBPLAjnLwIqLD70wme" alt=""><figcaption><p>Keystore generation</p></figcaption></figure>

6. Zipalign APK

```
zipalign.exe -v 4 <input_apk> <output_apk>
```

<figure><img src="/files/dLBDIn87VJQLpllbqm83" alt=""><figcaption><p>zipalignment</p></figcaption></figure>

7. Sign APK

```
apksigner.bat sign --ks <keystore_name> <apk_to_sign>
```

<figure><img src="/files/JKEwuf7U0F1TbFA0iSuF" alt=""><figcaption><p>Sign APK</p></figcaption></figure>

Now we have successfully modified the .apk, launch our device in Android Studio and we can use **adb** to install the modified .apk (alternatively we can drag-and-drop the apk into the device but we won't be able to see the errors this way.)

<figure><img src="/files/q57RCqsKBk771I7UAjza" alt=""><figcaption><p>Install apk using adb</p></figcaption></figure>

<figure><img src="/files/9GdQqACvJh9TEfkZuLx5" alt="" width="563"><figcaption><p>redlightgreenlight apk installed</p></figcaption></figure>

<figure><img src="/files/AocAn6cGRtPPVmBDfHGn" alt="" width="246"><figcaption><p>flag</p></figcaption></figure>

Flag: `flag{29b9edf8fd1e28ea8cd4faa37a6dbf25}`


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://arne-ctf.gitbook.io/ctf/2023/nahamconctf/mobile-red-light-green-light.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
