Few months ago I flashed my Android device with Cyanogenmod-based LegacyXperia ROM. Before that I was using anonymization script, I created that was able to change MAC address, block some Play Store’s services sending unknown binary data and masking device hostname (used by DHCP). Obviously after upgrade, the script stopped working, so I tried to make it work again.
Apparently newer Android systems changed its way of turning wifi on and off (before, MAC changing was just invoking ip command), so now wlan0 interface is not present when it is off and after turning it on device will most likely authenticate to any known network, effectively leaking its HW address. Because of that I needed to find another way.
A bit of research and compiling iw tool later, I found out that when wifi is being turned off by GUI, wifi card kernel module is removed from kernel, so card’s interface as well as its physical device disappear from system. So the first step to make it work is to insert module back (wl12xx_sdio in my case, it seems to be very popular card on Android devices so probably you have the same). Inserting the module, however generates another problem: GUI cannot manipulate wifi now, so we have to do everything manually. The next steps are to invoke wpa_supplicant to authenticate to network and starting DHCP client daemon.
Doing this that way allows us to do one additional thing, increasing our privacy. Since we have to start supplicant ourselves, we can provide custom config to it. And if we have to write script anyway, we could have separate scripts for every network we know. Then we will be (almost) sure that we are connecting to the network we want.
The connecting script is available from my new repository with Android scripts for increasing privacy. I hope there will be more of them in future. As I have written in first paragraph, there are few other things to do. Or maybe there is something more I do not know about, yet…
PS: this script was tested only on Cyanogenmod 10.2 (based on Android 4.3) and may not work on new (5.*) systems.