Cybersecurity

Android: DNS Setup for Developing and Testing Against Local Web Services

Coalfire Cybersecurity Team

March 19, 2021
Compliance Android tile

This content is provided "as is" and is more than a year old. No representations are made that the content is up-to date or error-free. Please see the latest on this topic here.

Most “interesting” smartphone applications do not run only on the smartphone device; they rely on supporting web services that can be run both by the deploying organization and 3rd parties. One of the challenges we have run into when developing Android application is setting up a suitable development environment because of issues resolving DNS entries for test versions of services.

Scenario: We have a development workstation where we want to run the Android application being developed as well as a development version of a web service. Let’s say the production web service runs at www.smartphonesdumbapps.com and we need to emulate that in our workstation environment.

First thought: Let’s set our workstation /etc/hosts file so that it points services.smartphonesdumbapps.com to localhost.

Great idea, except that the Android emulator does not use the workstation’s /etc/hosts files to resolve DNS entries. And localhost for the emulator is different than our workstation’s localhost. Being a Linux system, it has its very own fancy /etc/hosts file that is uses to support its DNS operations. So we need to change the emulator’s /etc/hosts file. But what do we want to map services.smartphonesdumbapps.com to?

Android emulator networking has a couple of special IP addresses it maintains to help you route traffic to different useful locations. For our purposes we want to route our traffic to 10.0.2.2 because that goes to the host workstation’s loopback interface.

So now we know we want to add an entry to the Android emulator’s /etc/hosts file to map services.smartphonesdumbapps.com to 10.0.2.2. Should be easy – let’s fire up the emulator, pull the original /etc/hosts file over to our workstation, make our update and then push it back to the emulator.

So we do:

$ emulator -avd AndroidPhone_test &

$ adb pull /etc/hosts ./

{modify local copy of hosts to include an entry for 10.0.2.2 for services.smartphonesdumbapps.com}

$ adb push hosts /etc/hosts

That should just about take care of it for us, except that we see:

failed to copy ‘hosts’ to ‘/etc/hosts’: Read-only file system

Crap. Guess we need to remount the filesystem to be read/write. (See Mount a filesystem read-write for more information)

So to mount the filesystem read-write we do:

$ adb shell

# mount -o rw,remount -t yaffs2 /dev/block/mtdblock0 /system

# exit

That should take care of things. But it doesn’t because you see:

failed to copy ‘hosts’ to ‘/etc/hosts’: Out of memory

Crap! Crap! Crap! My workstation has about a million billion gigs of memory and my emulator setup ought to as well. What is going on? As it turns out we need to run the emulator with a larger partition size (More info on Android emulator partition sizes here)

So we need to turn off the old emulator and start a new one with a bigger partition size. Once we have the old emulator turned off, we run the new one with:

$ emulator -avd AndroidPhone_test -partition-size 128 &

Then we mount the filesystem read-write with:

$ adb shell

# mount -o rw,remount -t yaffs2 /dev/block/mtdblock0 /system

# exit

Finally we pull and push the emulator /etc/hosts file with:

$ adb pull /etc/hosts ./

{modify local copy of hosts to include an entry for 10.0.2.2 for services.smartphonesdumbapps.com}

$ adb push hosts /etc/hosts

The whole sequence ends up looking like:

$ emulator -avd AndroidPhone_test -partition-size 128 &

$ adb shell

# mount -o rw,remount -t yaffs2 /dev/block/mtdblock0 /system

# exit

$ adb pull /etc/hosts ./

{modify local copy of hosts to include an entry for 10.0.2.2 for services.smartphonesdumbapps.com}

$ adb push hosts /etc/hosts

Now when the Android application tries to resolve services.smartphonesdumbapps.com it will send that traffic to the local development version running on the workstation. Much ado? Yes. About nothing? Well … this wasted an hour or so of my time a couple months ago so I figured it couldn’t hurt to get up on the web in a consolidated post.

Contact us for help building and securing your smartphone applications.