# Pinned

## Overview

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

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

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%2FtumesAYCLuJz5hKT1WAy%2FUntitled.png?alt=media&#x26;token=147a3d7c-5bfc-439a-b835-caa7d46a93b7" alt=""><figcaption></figcaption></figure>

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

First I used `apktool` to extract the contents of the `apk` file.

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FVPACdG9LCqkU2kX4s8O6%2FUntitled%201.png?alt=media&#x26;token=8272cbee-b469-4ebe-8030-16603e233a83" alt=""><figcaption></figcaption></figure>

After extracting the apk, I checked the `AndroidManifest.xml` file to look out for minimum required API level. But there was no minimum API level mentioned in the `AndroidManifest.xml` file.

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2Fv2Us0YcOZOIH1eV6alAA%2FUntitled%202.png?alt=media&#x26;token=5891666e-fb16-495e-b3fc-93baaf7f1385" alt=""><figcaption></figcaption></figure>

So, I decided to use Android 6 ( API 23 ).

Next I installed the `pinned.apk` using adb on to the emulated android 6 device.

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2F5MBSZR30QDSfGXwoFRLG%2FUntitled%203.png?alt=media&#x26;token=07b7b314-46c1-4139-a278-ebd99280c7a5" alt=""><figcaption></figcaption></figure>

***

## Application Interaction

I opened the application in the android emulator and it opened with credentials filled by default.

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FBjixMPi2qmmAc8e4Nk0H%2FUntitled%204.png?alt=media&#x26;token=6fba0c1b-0377-4642-bae9-aa5ba44268b5" alt=""><figcaption></figcaption></figure>

I clicked on LOGIN and it toasted a message that “You are logged in”.

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FrcGkjD6Gp0Ix3at43d0J%2FUntitled%205.png?alt=media&#x26;token=c6900723-7555-4be5-ad3e-a697190ab097" alt=""><figcaption></figcaption></figure>

From the challenge description, we can see that the user had tried to intercept the request, but the connection seems to be secure.

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2F6bqf10wDyzYdCbJAp8MA%2FUntitled%206.png?alt=media&#x26;token=a6e5db37-4dda-4e45-a5e8-e5acdb881726" alt=""><figcaption></figcaption></figure>

***

## Intercepting Requests

Let’s try to intercept the login request using burpsuite.

For that check this guide:&#x20;

{% content-ref url="<https://app.gitbook.com/s/7mqWWcMJ2puXtqlIgHfA/>" %}
[Burpsuite Proxy Setup for Android](https://app.gitbook.com/s/7mqWWcMJ2puXtqlIgHfA/)
{% endcontent-ref %}

Now Drop the above request and try to intercept the login request by trying to login from the pinned app.

But I can’t able to intercept the request. Instead burpsuite thrown the following error:

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FlHu8ypbCHknSvZLjuHB8%2FUntitled%207.png?alt=media&#x26;token=525760b9-97e6-4e16-a539-a8ea643831b6" alt=""><figcaption></figcaption></figure>

***

## Code Analysis

So let’s take a look at the code. I used `jadx` viewer to view the code. You can get it from here:

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

Open the apk in `jadx` Viewer. I first checked the `MainActivity.class` file and found the function that checks the credentials.

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FYjDJDPqdbPYpQ3RJkYlc%2FUntitled%208.png?alt=media&#x26;token=37195a0e-f451-4997-8e32-07be8780b8b6" alt=""><figcaption></figcaption></figure>

And also I found another function above the validation function which was generating SSL connection.

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FHPfFUoCoqhro9hbL7apC%2FUntitled%209.png?alt=media&#x26;token=559b57b3-513e-4cc8-a324-bf9f1a8a1b96" alt=""><figcaption></figcaption></figure>

On further investing I found the `run()` function which runs while generating SSL connection in the `MainActivity` file:

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2Fvz3OlqOAJbeGCbhapqXm%2FUntitled%2010.png?alt=media&#x26;token=d37b4460-97e8-40ab-97ee-b6dae0d66487" alt=""><figcaption></figcaption></figure>

The above function is a custom certificate pinning function which checks whether the certificate used is valid or not. To make burpsuite intercept the login request, we have to bypass the above check.

***

## SSL Pinning Bypass

On searching for SSL pinning bypass in google, found the following:&#x20;

{% embed url="<https://codeshare.frida.re/@pcipolloni/universal-android-ssl-pinning-bypass-with-frida/>" %}

### Frida Installation

To use the above exploit, first we need to install `Frida`. Follow the below commands to install `frida`:

```bash
python3 -m venv env
source ./env/bin/activate
pip3 install frida-tools
```

### Firda Server Setup

After installing frida on your local system, next you have to install frida server on the android device. To do that follow the below steps:

1. Download the latest frida server release for android that matches your android device’s architecture from the following link: <https://github.com/frida/frida/releases>
2. The file you download will be in `.xz` format. To extract it follow the steps:
   1. `sudo apt-get install xz-utils`
   2. `unxz frida-server-16.1.4-android-x86_64.xz`
   3. Rename the extracted file to `frida-server` using the command: `mv frida-server-16.1.4-android-x86_64 ./frida-server`
3. Next we have to push this `frida-server` file to the android device using the following command: `adb push frida-server /data/local/tmp/`
4. Now we have change the permissions of the `frida-server` file using the command: `adb shell "chmod 755 /data/local/tmp/frida-server"`
5. Now open `adb shell` and move to the directory where we pushed the `frida-server` and run the `frida-server` using the command: `./frida-server`.

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FmPUAE4cVr1xoQloYfQIn%2FUntitled%2011.png?alt=media&#x26;token=79b8033a-3e9d-410b-80aa-d70c35ac3110" alt=""><figcaption></figcaption></figure>

6. Now open a new terminal and again initialise the python environment using the command: `source ./env/bin/activate`
7. Now run the command: `frida-ps -U`, to check whether the frida server is running. This command should list all the packages.

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FA8DyratqDjLjYUk3j8dp%2FUntitled%2012.png?alt=media&#x26;token=cbc9d606-86d2-44b8-a0be-b0656cf79ccd" alt=""><figcaption></figcaption></figure>

Now we have successfully established the connection to the frida-server. Now its time to run the exploit. Before running the exploit, let’s take a look at it:

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FDehoRyLXTvOfyZYGVbpt%2FUntitled%2013.png?alt=media&#x26;token=cab1c60b-93a4-4fad-ab21-2e094fb8aa0c" alt=""><figcaption></figcaption></figure>

We can see that it is generating the SSL using a custom certificate, which is the burpsuite ca certificate in our case.

So to successfully run the exploit first we have to push the burp certificate to the location mentioned in the script, with the same name as mentioned in the script. Since we have the burp suite certificate already in our local machine which we downloaded and renamed as `cacert.pem` , we can use the same.

To push the certificate to the android device, use the following command:

`adb push ./cacert.pem /data/local/tmp/cert-der.crt`

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FETGcBAxHU5ordy6mhktf%2FUntitled%2014.png?alt=media&#x26;token=8f7f7636-1341-40e1-9bff-dac0ff7d47d1" alt=""><figcaption></figcaption></figure>

After successfully pushing the file, its time to execute the exploit. Make sure that you have turned burp intercept on and also make sure that you have connected to burp proxy in the android device.

### Exploitation

Now run the following command to execute the payload:

`frida --codeshare pcipolloni/universal-android-ssl-pinning-bypass-with-frida -U -f com.example.pinned`

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FdGkMvat4u3L5orGGRbnF%2FUntitled%2015.png?alt=media&#x26;token=ef64ad14-e76e-48bf-87b0-e9367a0df270" alt=""><figcaption></figcaption></figure>

The exploit has executed successfully and is waiting for the request.

Now go to the Pinned application and try to login and check the captured request in burp:

<figure><img src="https://3766366075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmgBRtrRN7KBbA6FISaV1%2Fuploads%2FipuJAQmQerW4LQ4XZ7NQ%2FUntitled%2016.png?alt=media&#x26;token=52215805-5d74-48ad-9063-44e86cfd56cf" alt=""><figcaption></figcaption></figure>

We have successfully obtained the flag……

Thank you…….
