Intercepting Network Requests from a Mobile App

Rabi Siddique
Level Up Coding
Published in
9 min readJul 4, 2023

--

Intercepting networking requests

Sometimes, you may need to test your mobile app and review all the network requests associated with it. On the web, this task is quite simple by pressing Ctrl + Shift + I to open the developer tools. From there, you can navigate to the network tab and examine all the HTTP requests related to the webpage as follows:

Network requests in the browser

However, on the mobile development side, the process is not as straightforward. Unlike on the web, there isn’t a built-in mechanism to easily inspect HTTP requests by opening developer tools.

In this blog post, we will explore the fascinating world of network request interception through the use of proxies. We will guide you through the step-by-step process of setting up and configuring a proxy server that will act as an intermediary between your device and the network.

By leveraging the capabilities of a proxy server, we gain the ability to intercept, inspect, and analyse all incoming network requests. This opens up a world of possibilities for debugging, performance optimization, and security analysis of your applications.

To make it easier to follow, I have divided the process into five steps. These are:

  1. Setting up Android Emulator
  2. Proxy Setup
  3. Preparing the Certificate
  4. Deploying the Certificate
  5. Setting Proxy on the Android Emulator

Let’s explore each step in detail.

1-Setting up Android Emulator

  • To begin the process, you will need to install Android Studio. You can download it by following this link: Android Studio Download Page.
  • Once you have successfully installed, launch the Android studio. On the toolbar at the top of the window, locate and click on the Tools menu. After that select Device Manager from the dropdown menu:
Select Device Manager from the dropdown
  • After clicking on the Device Manager, a new panel will appear:
Device Manager Panel
  • Click Create Device:
Clicking Create Device
  • Next, you need to select a device from the list that does not have the Play Store logo:
Selecting the hardware
  • After selecting a device without the Play Store logo, the next step is to choose an API level that matches your testing requirements. The API level represents the version of the Android platform that the device or emulator will simulate:
API Level
  • After proceeding to the next stage, you have the option to customize the AVD (Android Virtual Device) settings, including the device name, RAM allocation, and storage capacity.
AVD Settings
  • Once you have finished configuring the AVD settings, you can proceed to run the device. Click on the “Run” button to start the emulator and launch your virtual device:
Run the device

If you prefer using the command line to run the emulator, there is an alternative method you can follow. Open a command prompt or terminal on your computer, and you can utilize a few commands to achieve this.

To begin, use the command emulator -list-avds to display a list of all the installed emulators on your system. This command will provide you with the names of the available devices. From the list, identify the name of the device you want to run.

Once you have identified the device you wish to launch, you can use the emulator -avd deviceName command. Replace deviceName in the command with the actual name of your desired device. This command will start the emulator with the specified device.

It is important to ensure that you have set up all the required environment variables for the emulator commands to work properly. This will ensure that the commands are recognized and executed successfully.

Listing devices

After setting up the Android emulator, you have the option to download and install the APK file of your desired app from APKMirror. APKMirror is a reputable website where you can find a wide range of APK files for various Android applications. Once the APK file is downloaded, you can proceed to install it on the Android emulator. You can either drag and drop the downloaded APK file into the emulator window or use the emulator’s file transfer mechanism to import the APK file.

2-Proxy Setup

Before proceeding with the installation of the proxy, it is essential to ensure that OpenSSL and Android Debug Bridge (ADB) are properly set up. Here are the steps you should follow:

  • Start by installing OpenSSL on your system if you haven’t already done so. Refer to the documentation specific to your operating system for the installation process. Once installed, you can verify the installation by running the command openssl version in your terminal or Command Prompt. This will display the OpenSSL version if it is installed correctly.
Checking OpenSSL version
  • Next, it’s important to confirm that you have access to the Android Debug Bridge (ADB) from your terminal or Command Prompt. Use the command adb version to check if ADB is accessible and properly configured. If the command returns the ADB version, it means that it is set up correctly.
adb version

Once you have successfully set up OpenSSL and confirmed access to ADB, you can proceed with the installation of the proxy. In this tutorial, we will be downloading and using Proxyman as the example tool. However, you can apply the same principles with other similar tools like Charles Proxy or mitmProxy.

3-Preparing the Certificate

To decrypt the app requests, Proxyman generates a custom SSL certificate. To obtain this certificate and configure it for use with Android OS, follow these steps:

  • Open your web browser and visit the following link. This link will allow you to download the Proxyman SSL certificate. Once the certificate is downloaded, locate the folder where it was saved:
Locate to the folder where the file is downloaded
  • To ensure that the certificate file has the correct format expected by Android OS, you need to rename it. Open the Command Prompt and navigate to the folder where the certificate is located. After that run this command:
hashed_name=`openssl x509 -inform PEM -subject_hash_old -in proxyman-ca.pem | head -1` && cp proxyman-ca.pem $hashed_name.0This command uses OpenSSL to generate a hashed name for the certificate and then renames the file accordingly. By following these steps, you obtain the custom SSL certificate from Proxyman and rename it to the format expected by Android OS. This certificate is essential for decrypting app requests and enabling Proxyman to intercept and analyze the network traffic.

This command will not work on Windows Power shell so I ran this command on Git Bash terminal:

Running the command on Git bash

The command executed successfully and it generated a file with .0extension in the same folder:

Getting the Android Certificate

4-Deploying the Certificate

This .0 file we generated in the previous step is the certificate which are are going to add in our Android Emulator. To do this, we will start by opening the terminal and list the available AVDs:

emulator -list-avds

From the available AVDs, we will run the desired AVD with the following command:

emulator -avd <avd_name_here> -writable-system

Note that we are using -writable-system flag to start the AVD with writable system permissions, allowing modifications. After this we need to open another terminal and run the following command:

adb root

This command restarts the ADB daemon with root privileges. After that we run this command to disable secure boot verification on the device:

adb shell avbctl disable-verification

Next we reboot the device with the command:

adb reboot

Once the device has rebooted, run the following command to restart ADB as root again:

adb root

Perform a remount of partitions as read-write by executing the following command:

adb remount

If ADB prompts you to reboot the device, run adb reboot again, followed by adb root and adb remount. Now we are ready to push the renamed SSL certificate file, the .0file we generated previously to the device using the following command, replacing <path_to_certificate> with the actual path to the certificate file:

adb push <path_to_certificate> /system/etc/security/cacerts

This command copies the certificate file to the appropriate directory on the device. Now we will set the correct permissions for the certificate file by running the following command:

adb shell chmod 664 /system/etc/security/cacerts/<name_of_pushed_certificate>

Now we need to reboot the device one final time using the following command:

adb reboot

The device will restart, and the SSL certificate will be deployed. We have successfully deployed the SSL certificate into your Android Emulator. This will allow the emulator to recognize and trust the certificate, enabling Proxyman to intercept and analyse the network traffic from the device.

Now to validate the certificate on your Android device, you can navigate to the Trusted Credentialssection in the Encryption & Credentialssettings of your Android device, you can find the Proxyman LLCcertificate listed under the Systemtab. This confirms that the certificate has been successfully installed and recognized by your Android device:

Validating the certificate

5-Setting Proxy on the Android Emulator

The final step involves we configure our Android Emulator to make use of the Proxyman’s proxy that we have setup. For this purpose, launch Proxyman on your computer. In Proxyman, navigate to the Certificatemenu. Select Install Certificate on Android from the dropdown. Choose Physical Devicesfrom the sub-menu. Note that you don’t need to worry about the term Physical Devicesas we won’t be following that specific guide. We only need a few essential details from this menu:

Physical Devices on Proxyman

In the Android setup guide, make sure to copy both the IP address and the port number. These details will be needed for the subsequent steps of the process:

Copy the IP and Port Number

The next is to go to the Android Emulator and click the ellipsis icon as shown in the screenshot:

Clicking the ellipsis

Click on the Settingsoption located in the sidebar. Once you have clicked on Settings,you can proceed to navigate to the Proxytab:

Navigating to settings

In the proxy tab, check the Manual proxy configuration option. After that paste the IP address and port that we have copied from Proxyman Android setup guide step and hit the apply button:

Configuring IP and Port number

Congratulations! 🎉 We have successfully completed all the necessary setup steps to intercept HTTP requests from our Android device. Now, we can launch Proxyman and run apps on your Android Emulator. As we use the apps, Proxyman will capture and display all the relevant details and data related to the network requests, providing us with valuable insights and visibility into the app’s communication.

Intercepting Network Requests using Proxyman

Thank you for reading. I hope this post is helpful to you. If you have any further questions, don’t hesitate to reach out. I’m always happy to help.

Let’s connect:
LinkedIn
Twitter

Level Up Coding

Thanks for being a part of our community! Before you go:

🔔 Follow us: Twitter | LinkedIn | Newsletter

🚀👉 Join the Level Up talent collective and find an amazing job

--

--

A passionate Software Engineer who intends to be the best and nothing less.