Documenting Problems That Were Difficult To Find The Answer To

Filtering EasyList For Hosts-File Style Adblock

If you have an Android (rooted CyanogenMod) phone you may be wondering how to block advertising from your web browser.

You may have heard of the hosts file trick that makes the phone assume certain (advertising) domains are hosted at the localhost ( – which is the phone itself and not running its own web server and thus will instantly respond to any such requests with a “cannot connect” type response – quickly and simply filtering out advertisement domains.

We can make use of Adblock filtering lists such as EasyList by looking for any whole-domain rules, extracting the domain name, and putting into the hosts file.

The script is as follows:

#!/usr/bin/perl -w

use strict;

my %hosts = ();
while ( <> ) {
    if ( $_ =~ m/^\|\|([a-z][a-z0-9-_.]+\.([a-z]{2,3}))\^\s*$/ ) {
        $hosts{$1} = 1;

foreach my $host ( sort keys %hosts ) {
    print( "\t$host\n" );

We can use this by downloading the EasyList (as, say, easylist.txt). Then:

user@host:~> perl easylist.txt >easylist.hosts

As of 2014-08-18 there were 5029 such hosts in the list. You can add them to a file. This file should look something like (all domains except localhost are fictionalised in this example):	localhost

You must ensure that your hosts file always has a localhost entry in it.

Next, connect your phone via USB and shell to it. The steps are:

  • push the new hosts file to the SD card (temporary location)
  • shell to the phone, become super user
  • find the /system mount path, remount /system as rw (read-write)
  • copy the old /etc/hosts file (backup)
  • copy the new /sdcard/ file to /etc/hosts
  • remount /system as ro (read-only)
user@host:~> adb push /sdcard/
1572 KB/s (236687 bytes in 0.147s)

user@host:~> adb shell
shell@p880:/ $ su
root@p880:/ # mount |grep system
/dev/block/platform/sdhci-tegra.3/by-name/APP \
  /system \
  ext4 \
  ro,seclabel,noatime,nodiratime,user_xattr,acl,barrier=0,data=ordered,noauto_da_alloc \
  0 0
root@p880:/ # mount -o rw,remount \
  /dev/block/platform/sdhci-tegra.3/by-name/APP \
root@p880:/ # cd /etc
root@p880:/etc # cp hosts hosts.old
root@p880:/etc # cp /sdcard/ ./hosts
root@p880:/etc # mount -o ro,remount \
  /dev/block/platform/sdhci-tegra.3/by-name/APP \
root@p880:/etc # exit
shell@p880:/ $ exit

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: