Nginx Shell Script To Block Spamhaus Lasso Drop Spam IP Address

by on January 10, 2010 · 1 comment

  1. #!/bin/bash
  2. # A Nginx Shell Script To Block Spamhaus Lasso Drop Spam IP Address
  3. # Run this script once a day and drop all spam network IPs (netblock) with http 403 client error.
  4. # The script will get executed every day via /etc/cron.daily (make sure crond
  5. # is running).
  6. # -------------------------------------------------------------------------
  7. # Copyright (c) 2008 nixCraft project <http://cyberciti.biz/fb/>
  8. # This script is licensed under GNU GPL version 2.0 or above
  9. # -------------------------------------------------------------------------
  10. # This script is part of nixCraft shell script collection (NSSC)
  11. # Visit http://bash.cyberciti.biz/ for more information.
  12. # -------------------------------------------------------------------------
  13. # Last updated on Jan/11/2010
  14. # -------------------------------------------------------------------------
  15. # tmp file
  16. FILE="/tmp/drop.lasso.txt.$$"
  17.  
  18. # nginx config file - path to nginx drop conf file
  19. OUT=/usr/local/nginx/conf/drop.lasso.conf
  20.  
  21. URL="http://www.spamhaus.org/drop/drop.lasso"
  22. # reload command
  23. NGINX="/usr/local/nginx/sbin/nginx -s reload"
  24.  
  25. # remove old file
  26. [[ -f $FILE ]] && /bin/rm -f $FILE
  27.  
  28. # emply nginx deny file
  29. >$OUT
  30.  
  31. # get database
  32. /usr/bin/wget --output-document=$FILE "$URL"
  33.  
  34. # format in nginx deny netblock; format
  35. /bin/egrep -v '^;' $FILE | awk '{ print "deny " $1";"}' >>$OUT
  36.  
  37. # reload nginx
  38. /bin/sync && ${NGINX}

How Do I Use This Script?

Download and save this script to /etc/cron.daily/, enter:

  1. cd /etc/cron.daily/
  2. wget http://bash.cyberciti.biz/dl/500.sh.zip
  3. unzip 500.sh.zip
  4. mv 500.sh nginx.drop.lasso
  5. chmod +x nginx.drop.lasso
  6. rm 500.sh.zip

Edit nginx.conf (/usr/local/nginx/conf/nginx.conf) and add the following line:

  1. ## Block lasso spammers ##
  2. include drop.lasso.conf;
  3. ## Block lasso spammers ##

Save and close the file. Run the script:

  1. /etc/cron.daily/nginx.drop.lasso


4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

  • Enzo

    what is the utilitie of this script?, please can you explain a little?

    regards, nice blog.

Previous Script:

Next Script: