> For the complete documentation index, see [llms.txt](https://arne-ctf.gitbook.io/ctf/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://arne-ctf.gitbook.io/ctf/2023/nahamconctf/mobile-red-light-green-light.md).

# Mobile - Red Light Green Light

Medium | 464 points | 86 solves

## 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="https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2FwR5XwGmROYOp5m12KLw2%2Fimage.png?alt=media&amp;token=0b8c1bb0-6fd0-4dd7-a1fa-a3d804c42dfe" 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="https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2FVLjG0DxTCsiozbbH9Lcq%2Fimage.png?alt=media&amp;token=346c70f4-e8ea-4ed6-98d4-8159c99b5719" 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="https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2FHoR07KibPJiK6yHjlOvU%2Fimage.png?alt=media&amp;token=0117f95f-4043-4af7-ac62-b008869a7ffb" 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="https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2FAvFvopCiNILSacdtawx9%2Fimage.png?alt=media&amp;token=3c980ef3-2187-438b-9c97-e3d42de84f5e" 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="https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2FMv6k3oTdw5AYU2UpE8jJ%2Fimage.png?alt=media&amp;token=0c294276-e9d2-4f26-b4f3-535e330994fb" 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="https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2FinY5GhfRAxZzeffrypSN%2Fimage.png?alt=media&amp;token=05e5707b-22d1-492f-b96d-1edd1f5fc3de" 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="https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2Fm1eNXESm06aESHpZZfEW%2Fimage.png?alt=media&amp;token=aeef1a17-fb77-4f52-9937-42b8fce76a2a" 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="https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2FQCSzlpZOcFCOZOFdkQhK%2Fimage.png?alt=media&amp;token=3076267c-96bb-4d81-af72-edeafa13db85" 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="https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2F1wJvJqZ0DXx3RPzNzoZz%2Fimage.png?alt=media&amp;token=f45df01c-9c4f-4f6a-b468-2a61a563b0dc" alt=""><figcaption><p>Inside AndroidManifest.xml</p></figcaption></figure>

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

<figure><img src="https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2FoKyigWtYhgCVELGMsU2J%2Fimage.png?alt=media&amp;token=8b5da603-c393-4a66-9acc-a641314e3521" 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="https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2F2TpthHggeSVCztMm9UMR%2Fimage.png?alt=media&amp;token=a5869897-f974-4b4a-bd18-8c7942f1758a" 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="https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2FQ31lLDB2CO7Ibfa3wt2x%2Fimage.png?alt=media&amp;token=13b4bb7c-76f1-4c3f-808b-f1f5a9d5bc1a" 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="https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2FLP3IZoc65m3mialItY4q%2Fimage.png?alt=media&amp;token=171a2fb4-58ad-4f05-9cd2-a3e99c4c3de2" alt=""><figcaption><p>Keystore generation</p></figcaption></figure>

6. Zipalign APK

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

<figure><img src="https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2FN33KZS372AGklpmc2Tx1%2Fimage.png?alt=media&amp;token=7a2fed1f-7a39-416a-b55b-947965ebf6ad" alt=""><figcaption><p>zipalignment</p></figcaption></figure>

7. Sign APK

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

<figure><img src="https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2Fcotbh6m36ctXSdstq7Jk%2Fimage.png?alt=media&amp;token=cff66539-cfb3-4fb6-b6c3-4ed7d1e44d67" 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="https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2FD5tKWQmToZ9FPIfWhE5T%2Fimage.png?alt=media&amp;token=8448acc6-b3be-4d2d-964f-a1ce78a36de5" alt=""><figcaption><p>Install apk using adb</p></figcaption></figure>

<figure><img src="https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2FRoaBZoSfjgBuKbP0ZxmR%2Fimage.png?alt=media&amp;token=dad53630-2271-4e75-bb9f-0f9b9f9a0be4" alt="" width="563"><figcaption><p>redlightgreenlight apk installed</p></figcaption></figure>

<figure><img src="https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2F0oLjX0PFp62MaKKgdFL3%2Fimage.png?alt=media&amp;token=fd17f7a3-ddd4-44be-8738-cfde1d2892a0" alt="" width="246"><figcaption><p>flag</p></figcaption></figure>

Flag: `flag{29b9edf8fd1e28ea8cd4faa37a6dbf25}`
