Linux Netfilter discussions
 help / color / mirror / Atom feed
From: "Neal P. Murphy" <neal.p.murphy@alum.wpi.edu>
Cc: netfliter list <netfilter@vger.kernel.org>
Subject: Re: Can I use iptables instead of hosts to block adservers?
Date: Fri, 13 Oct 2017 21:37:19 -0400	[thread overview]
Message-ID: <20171013213719.3b2e9e20@playground> (raw)
In-Reply-To: <20171014010204.GA1316@waltdnes.org>

On Fri, 13 Oct 2017 21:02:04 -0400
"Walter Dnes" <waltdnes@waltdnes.org> wrote:

>   I downloaded a large hostfile blocking list recently.  Out of
> curiousity, I ran it through a bash script, which fed each hostname to
> the "host" command.  Approximately 95% of the host queries returned...
> 
> Host <hostname> not found: 3(NXDOMAIN)
> 
>   Think about it for a minute.  The people who run adservers *KNOW*
> about hostfiles.  It's trivial to set up a script to rotate subdomain
> names like a.doubleclick.net, b.doubleclick.net, c.doubleclick.net,
> abc.doubleclick.net, etc, etc.  Even domain names can be rotated
> through, and aliased.  All the names in the downloaded hostsfile were
> probably valid at one time or another, but they age out rather quickly.
> This strategy...
> a) gets around hostfile-based blocking
> b) penalizes hostfile-based blocking by slowing users' computers as they
>    scan through an oversized list full of dead subdomain names
> 
>   Rather than blocking by ephemeral names, howsabout blocking by IP
> address?  I don't think IPV4 addresses are plentiful enough for jumping
> around.  This is where iptables comes in.  Let's start off with a
> script that uses the 5% of valid addresses that I found.  Setup...
> 
> * create chain ADBLOCKLOG with rules
>   iptables -A ADBLOCKLOG -j LOG --log-prefix "ADBLOCK:" --log-level 6
>   iptables -A ADBLOCKLOG -j DROP
> 
> * create chain ADBLOCK with rule
>   iptables -I -j ACCEPT
> 
> * list adserver addresses as follows...
> iptables -I ADBLOCK -d <ipaddress1>/32 -j ADBLOCKLOG
> iptables -I ADBLOCK -d <ipaddress2>/32 -j ADBLOCKLOG
> iptables -I ADBLOCK -d <ipaddress3>/32 -j ADBLOCKLOG
> etc, etc
> 
> * if adjacent IP addresses show up, we can aggregate them to /31 or /30
>   or /29, etc.
> 
>   The last rule in the OUTPUT chain is changed to a jump to the ADBLOCK
> chain so that all output is filtered.  If a packet "runs the gauntlet"
> successfully, it hits the ACCEPT rule.
> 
> Questions...
> 1) Has this been done before, and am I re-inventing the wheel?

Yes, it's been done.

> 2) Is there a major showstopper problem with this idea?

Netfilter starts becoming inefficient after about 16 entries.

> 3) Any suggestions for improvements?

Use ipset; it's efficient to thousands and even tens of thousands of addresses.

I hacked a script for Smoothwall Express that fetches a few free blocklists, consolidates them, and puts their entries into host and net ipsets. Then it sets netfilter to check incoming packets for src or dst addrs among those entries. It works quite nicely.

  parent reply	other threads:[~2017-10-14  1:37 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-14  1:02 Can I use iptables instead of hosts to block adservers? Walter Dnes
2017-10-14  1:24 ` Jean Weisbuch
2017-10-14  1:37 ` Neal P. Murphy [this message]
2017-10-14  7:38 ` Imran Geriskovan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171013213719.3b2e9e20@playground \
    --to=neal.p.murphy@alum.wpi.edu \
    --cc=netfilter@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox