Blocking spammers with bash and iptables

Well, Pyronexus started to gain a bit more attention recently – by bots at least – either that or Google’s reCaptcha just stopped working as well.

So I started to look for new ideas. Since I cannot stand spammers I thought the best way to avoid them was to block them. I knew iptables would suit my needs but it isn’t always the easiest to configure.

A quick google search later and I came across a post of an author who’d encountered the same problem and had already beaten me to the solution.

However, I noticed some areas where his script could improved. So here it is if anyone else needs it :).

  1. Create a directory for the script to reside in and create a new file:

    mkdir -p /opt/scripts
    nano /opt/scripts/
  2. Paste the following into it, and edit the variables such as iptables save path:
  3. Save the file, then make it executable:

    chmod +x /opt/scripts/
  4. Now, edit your crontab and add a new job to update every day or so (not too much, you don’t want to overload their servers by constantly updating). You can use my crontab example which runs every night at midnight:

    @reboot /opt/scripts/
    @daily /opt/scripts/
  5. Give it a test run and list the iptables rules (It will take a while as they resolve into hostnames, but you will see the Spamhaus chain appearing – press CTRL + C to cancel the listing):

    iptables --list-rules

Original script:

Leave a Comment

Skip to toolbar