Skip to content

Instantly share code, notes, and snippets.

@NotoriousPyro
Last active May 17, 2024 09:04
Show Gist options
  • Select an option

  • Save NotoriousPyro/c5496a7564af31a327b16efaed15d848 to your computer and use it in GitHub Desktop.

Select an option

Save NotoriousPyro/c5496a7564af31a327b16efaed15d848 to your computer and use it in GitHub Desktop.
Spam IP blocker for iptables and bash (list by spamhaus.org). See how to use this here: https://pyronexus.com/2017/01/11/blocking-spammers-with-bash-and-iptables/
#!/usr/bin/env bash
# List variables
blocklist_name="Spamhaus" # MUST BE UNIQUE IN IPTABLES
blocklist="http://www.spamhaus.org/drop/drop.lasso"
# No more variables. Edit below here at your own risk.
PATH="/sbin:$PATH"
printf "Retrieving block list: %s... " "$blocklist"
list=`curl -sSL "$blocklist" | cut -d ";" -f 1 | grep -v "^$"`
printf "Done.\n\n"
printf "Applying block list: \n"
iptables -D INPUT -j $blocklist_name 2> /dev/null
iptables -D FORWARD -j $blocklist_name 2> /dev/null
iptables -F $blocklist_name 2> /dev/null
iptables -X $blocklist_name 2> /dev/null
iptables -N $blocklist_name 2> /dev/null
for line in $list; do
printf " Blocking: %s\n" "$line"
iptables -A $blocklist_name -s $line -j DROP
done
iptables -I INPUT -j $blocklist_name
iptables -I FORWARD -j $blocklist_name
printf "Block list applied.\n\n"
@asterismo
Copy link

Blocking: 221.132.192.0/18
Blocking: 223.0.0.0/15
Blocking: 223.169.0.0/16
Blocking: 223.173.0.0/16
Blocking: 223.201.0.0/16
Blocking: 223.254.0.0/16
Block list applied.

/tmp/spamscript.sh: 30: [: unexpected operator

@NotoriousPyro
Copy link
Author

Interesting. What did you enter for the variables at the top and did you pass --save ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment