> 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/apkrypt.md).

# APKrypt

APKrypt writeup by Thamizhiniyan C S

## Overview

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

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

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%2FANTOCh7mpesXifzx6OC5%2FUntitled.png?alt=media&amp;token=98c91eee-dddb-41d7-9237-382edc21f587" alt=""><figcaption></figcaption></figure>

From the README.txt file, we can see that the application supports API level 29 or earlier.

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FZCoGgg6ZFpP89JO2z6d9%2FUntitled%201.png?alt=media&amp;token=24f7d147-145b-4de8-bf89-3911f39d9492" alt=""><figcaption></figcaption></figure>

Next I used `apktool` to extract the apk file.

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FzQm7v4yzjzMcfZoS3nyh%2FUntitled%202.png?alt=media&amp;token=5e8e2b80-dbf3-4694-8ac5-15bc6b39ce0d" alt=""><figcaption></figcaption></figure>

After extracting the apk, I first took a look at the `AndroidManifest.xml` file to check whether there is any minimum SDK version is required to run the app.

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2F7IeGfoftp3hiAqQuGcxd%2FUntitled%203.png?alt=media&amp;token=0df7abb3-672e-4e88-a40e-3c74a1f53e2f" alt=""><figcaption></figcaption></figure>

There was nothing mentioned about that. So, In my case I installed the apk on an Android 10 / API 29 Virtual Device with Google API’s.

To install the `apk`, I used `adb`.

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FGhdq3ldr9F3WRYPDkxYo%2FUntitled%204.png?alt=media&amp;token=9f002ebc-3522-4a26-9f07-80c596e3944f" alt=""><figcaption></figcaption></figure>

***

## Application Interaction

Next I just take a look at the app and its functionality.

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2F4uu55DCYOCZa0TrdwlLm%2FUntitled%205.png?alt=media&amp;token=c808f978-272b-44b2-8b95-312f7b7f7cfc" alt=""><figcaption></figcaption></figure>

The app loads with a page, where we have to enter the VIP code to get the ticket. There is nothing else in the page except a submit button. So I moved on to analyse the decompiled `smali` code.

I first opened the apk with `jadx-gui`, to get the basic understanding of the application.

***

## Code Analysis

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

In the `MainActivity` class file, you can find the main function which checks whether the VIP code is valid or not and if valid, it returns the flag.

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FvoOFxf3oZBJH70j91sez%2FUntitled%206.png?alt=media&amp;token=e1563477-e0bd-411b-9a78-7e39d61f866f" alt=""><figcaption></figcaption></figure>

* 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.

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

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

***

## Modifying the APK

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%2FyeiY1raOc9p4dHufvCRp%2FUntitled%207.png?alt=media&amp;token=9e7593a0-6473-40d6-8cd9-739fdaad7eca" 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%2FQdrVTpkdnFZ52bfVgXzE%2FUntitled%208.png?alt=media&amp;token=f02afc17-13a8-49fe-bccd-a172ae298491" 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%2FkKWH1G9OzYu3fJSlm2yR%2FUntitled%209.png?alt=media&amp;token=66ff5ad9-9363-411b-a815-307a473d3149" 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 VIP code 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%2FETTfUTTCw6rOsoWV6Q1r%2FUntitled%2010.png?alt=media&amp;token=158875ac-f620-407f-af5f-281a001e99ce" 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%2F5p334iclWCR8hrP04PcN%2FUntitled%2011.png?alt=media&amp;token=249db8ed-ba73-4484-8778-1d15cd7bb570" alt=""><figcaption></figcaption></figure>

***

## Rebuilding and Signing the APK

Now it’s time to compile and sign the apk.

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

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FLekfiISFROu8i7tTgQCc%2FUntitled%2012.png?alt=media&amp;token=864d746e-ac13-4a9d-aa3b-1979081e0463" alt=""><figcaption></figcaption></figure>

After the build process is completed 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%2FLhpNfoff4kb0S4iZySvC%2FUntitled%2013.png?alt=media&amp;token=353f84a0-7a79-4116-baba-a1f96816cdfc" alt=""><figcaption></figcaption></figure>

***

## Installing the Modified APK

Now its time to install app. Before installing the modified version of the app, ensure that you have uninstalled the unmodified version of the app.

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2Fl6AUqdrKLoWqXF6VXIfB%2FUntitled%2014.png?alt=media&amp;token=509532c2-9424-47db-8e50-29bbce17a462" alt=""><figcaption></figcaption></figure>

After installing the app, open the app and press the submit button \[ with or without the VIP code ( Both should WORK ) ].

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FMTuC5L7oufdaXFWeTDrT%2FUntitled%2015.png?alt=media&amp;token=ffc30286-a0a5-421a-8b34-5d619498c723" alt=""><figcaption></figcaption></figure>

You can see the toast message with the flag.

We have successfully obtained the flag.

Thank You !!!!!!
