Installing proxy certificate in Android emulator from command line

Why?

Sometimes you want to inspect/modify the traffic of your Android app for debugging purpose or check behavior of your App with different data scenario. Without the proxy, you either need to rebuild your app or create/maintain the data set at the serve them from server. However, with proxy you can just change the data on the fly with proxy application such as MITMproxy, Fiddler, Charles etc.

How?

For physical device

  1. Navigate to Android’s network settings and set the proxy server and port.
  2. Install the proxy certificate on your device either by downloading it from a web address or form device storage.

The same steps will work for the emulators as well.

But is there a simpler and automated way to do this ?

And the answer is ofcource we can

We are going to install the proxy certficates as a system certificate so you don’t have to configure certificate details in app’s menifest file.(for Android 6.0+, details )

Steps

We are going to install MITM proxy for this demo

  1. Start MITM proxy

    1
    mitmproxy
  2. Execute below command to start Android emulator

    1
    2
    3
    4
    emulator -avd <avd-name> -http-proxy <proxy-address>:<proxy-port> -writable-system & ## make sure that you don't miss '-writable-system'

    # e.g. $ emulator -avd proxy_avd -http-proxy http://127.0.0.1:8080 -writable-system
    # This is tested on macos
  3. We are now going to push MITM’s custom proxy certificate to Android system

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    # This script does not work for android 14 emulator
    adb root
    adb remount
    file=~/.mitmproxy/mitmproxy-ca-cert.pem
    filename=$(openssl x509 -noout -subject_hash_old -in $file) ## creating filename for .0 file
    openssl x509 -in $file > $filename.0
    openssl x509 -in $file -text -fingerprint -noout >> $filename.0
    adb reboot
    adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed) ]]; do sleep 1; done;'
    adb root
    adb remount
    adb push $filename.0 /system/etc/security/cacerts
    adb remount

And its done, You can verify the MITM ceritificate in system certificate list.
MITM cert in android system

You will start seeing every http requests made from the emulator, from all apps. Start Poking around with the network calls made from emulotor.


Installing proxy certificate in Android emulator from command line
https://automationlog.in/Installing-proxy-certificate-in-Android-emulator-from-command-line.html
Author
Prabhash Singh
Posted on
May 20, 2017
Licensed under