> For the complete documentation index, see [llms.txt](https://thamizhiniyancs.gitbook.io/writeups/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://thamizhiniyancs.gitbook.io/writeups/hackthebox/tracks/intro-to-android-exploitation/apkey.md).

# APKey

APKey writeup by Thamizhiniyan C S

## Overview

Hey everyone, in this write-up we will be solving an HTB challenge APKey.

Link to the challenge: <https://app.hackthebox.com/challenges/240>

Let’s Start!!!!!!

***

## Initial Setup

First download and extract the given file.

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FVi8DbI8rYm68TEs49yYV%2FUntitled.png?alt=media&amp;token=44f83d67-8264-41c6-90de-2f088ace4eda" alt=""><figcaption></figcaption></figure>

Next I opened the apk with `jadx-gui`.

{% embed url="<https://github.com/skylot/jadx>" %}

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2F9y8X4hBlTw0sD7dXe3Rw%2FUntitled%201.png?alt=media&amp;token=eb618ce9-13e3-4332-b2aa-ac18c03ed08e" alt=""><figcaption></figcaption></figure>

I checked the `AndroidManifest.xml` file and found that we need at least API version 16 to run this app and the target API version is 30 i.e., Android 10. So, I installed the app on Android 10 emulator.

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2Fr4ylSWeyL1yITfrpQcyK%2FUntitled%202.png?alt=media&amp;token=e53a101d-3ea0-4b06-ab1a-93c194cc316c" alt=""><figcaption></figcaption></figure>

***

## Application Interaction

Now let’s take a look at the app.

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FpPzEL3s8dUQKnxTqIhq0%2FUntitled%203.png?alt=media&amp;token=dccbf060-ac79-46f5-a595-20ea28d10e13" alt=""><figcaption></figcaption></figure>

It opened with a login page, and if we enter wrong credentials, it throws a toast stating “Wrong Credentials”.

Nothing else is found in the app.

***

## Code Analysis

Now let’s take a look at the source code of the application.

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FNZlKgcTHdCuxwFity11F%2FUntitled%204.png?alt=media&amp;token=05ed38c5-2efa-41be-bccf-8a4216a17ac6" alt=""><figcaption></figcaption></figure>

From the MainActivity class file, we can see a if condition, which looks out for the user `admin` and checks whether the md5 version of the entered password matches the predefined hash in the second if condition, and if the condition satisfies the application throws a toast with the key.

* The if condition checks whether the MD5 version of the code that we input is equal to the MD5 hashed VIP code. For that, it uses the `equals` method.
* Now to bypass this check, we can modify the `equals` method to `notEqual`, so that even if the input is empty or invalid the flag will be shown.
* To modify the above condition, we have to first decompile the `apk` and modify the `smali` code of this particular if condition and recompile the `apk` and sign it with a key, to bypass the check.

***

## Modifying the Code

To perform the above task, I used `APKlab` tool to Modify the apk.

{% embed url="<https://github.com/APKLab/APKLab>" %}

First open vscode and use the shortcut key `ctrl + shift + p` to open the command pallet and search for `APKLab: Open an APK` option and click it.

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FFDuSBs53Dnzyol3UnxWe%2FUntitled%205.png?alt=media&amp;token=8fc64f31-82c7-4d82-8938-3004a9186b5e" alt=""><figcaption></figcaption></figure>

Now locate the apk file and select it.

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FHq2nwqxTQ8XyP9tPrvRI%2FUntitled%206.png?alt=media&amp;token=69607d28-09bc-493c-b2e1-c7a2e9c805c4" alt=""><figcaption></figcaption></figure>

Next leave the defaults in the popup and click OK.

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FI4o9Ys8urfrtuyLO6eYo%2FUntitled%207.png?alt=media&amp;token=31ebbef5-5c1e-4dcd-8ba4-4661a3a03acf" alt=""><figcaption></figcaption></figure>

After pressing ok, `APKLab` will decompile the android application and will load a new window with the decompiled files.

I used the search feature in the vscode to find the condition, by looking out for the MD5 hashed version of the password to which our input code is compared.

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FZkyHPMq3lfCbSeqawCfh%2FUntitled%208.png?alt=media&amp;token=c759b3a5-4450-4811-bf7d-8666f36d5530" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FUKrue3cOIATX3Yt7Zsm4%2FUntitled%209.png?alt=media&amp;token=925192ad-afdb-47ad-93b2-5e86a55d40c7" alt=""><figcaption></figcaption></figure>

From the results, we have found the if condition. Now its time to modify the if condition.

Replace the `if-eqz` to `if-nez`, which means `not equals` and save the file.

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2Fn7LPLMZ2nHVt9fZJx5bN%2FUntitled%2010.png?alt=media&amp;token=29001719-e5f3-440d-9db7-74eab8cf2152" alt=""><figcaption></figcaption></figure>

***

## Rebuiding and Installing the APK

Now it’s time to compile it into apk,sign the apk and install the apk onto the emulator.

To do that select the `apktool.yml` file in the file explorer → right click to view the options → click on the option `APKLab: Rebuild and Install the APK`

{% hint style="info" %}
Before performing the above task, don’t forget to uninstall the original version of the app from the emulator.

<img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FZJ5n5zIIfKBlwnozdh8C%2FUntitled%2011.png?alt=media&amp;token=085effc8-4807-4907-88ee-7470af6b7a92" alt="" data-size="original">
{% endhint %}

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FPlPHUKdWyc4v3xopcSaT%2FUntitled%2012.png?alt=media&amp;token=5cfa3e3c-956e-4850-a12c-e2d8a3da361d" alt=""><figcaption></figcaption></figure>

After the build process is completed and the app is successfully installed, you should see a output similar to this:

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FFGRditiFOKGR8kSPsaoo%2FUntitled%2013.png?alt=media&amp;token=0700a2ca-7762-4341-9001-e5eaac282885" alt=""><figcaption></figcaption></figure>

Now open the app and type the user name as `admin` and enter some random password.

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FPXxlUVUnoO894U5cCy1R%2FUntitled%2014.png?alt=media&amp;token=af8763d2-9be0-484d-9377-ab9f26d3eb91" alt=""><figcaption></figcaption></figure>

You can see the toast message with the flag.

We have successfully obtained the flag.

Thank You !!!!!!
