Advertisement
teuk

netfilter_drop_ipset_zeus_spamhaus.sh

Feb 6th, 2017
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.88 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # +-----------------------------------------------------------------------+
  4. # | netfilter_drop_ipset_zeus_spamhaus.sh                                 |
  5. # |                                                                       |
  6. # | Create ipset lists from zeus project and spamhaus                     |
  7. # | Add netfilter INPUT rules matching ipset lists to DROP unwanted ips   |
  8. # |                                                                       |
  9. # | Add this script in /etc/cron.daily/netfilter_drop_ipset_zeus_spamhaus |
  10. # | #!/bin/sh                                                             |
  11. # | /path/to/netfilter_drop_ipset_zeus_spamhaus.sh                        |
  12. # |                                                                       |
  13. # +-----------------------------------------------------------------------+
  14.  
  15. LOGFILE=/var/log/$(basename $0 .sh).log
  16. DROP_RULE_POSITION=6 # Set the correct position according to your netfilter INPUT rules
  17. ZEUS_BLOCKLIST_URL='https://zeustracker.abuse.ch/blocklist.php?download=ipblocklist'
  18. ZEUS_BLOCKLIST_FILE=/etc/zeus-list.txt
  19. SPAMHAUS_BLOCKLIST_URL='https://www.spamhaus.org/drop/drop.txt'
  20. SPAMHAUS_BLOCKLIST_FILE=/etc/spamhaus-list.txt
  21.  
  22. function message {
  23.     if [ ! -z "$1" ]; then
  24.         echo "[$(date +'%d/%m/%Y %H:%M:%S')] $*" | tee -a ${LOGFILE}
  25.     fi
  26. }
  27.  
  28. function ok_failed {
  29.     if [ ! -z "$1" ] && [ $1 -eq 0 ]; then
  30.         message "OK"
  31.     else
  32.         message "Failed"
  33.         exit $?
  34.     fi
  35. }
  36.  
  37. message "[+] Check if wget command is available"
  38. which wget &>/dev/null
  39. ok_failed $?
  40.  
  41. message "[+] Check if ipset command is available"
  42. which ipset &>/dev/null
  43. ok_failed $?
  44.  
  45. message "[+] Download https://zeustracker.abuse.ch/blocklist.php?download=ipblocklist"
  46. /usr/bin/wget --no-check-certificate ${ZEUS_BLOCKLIST_URL} -O ${ZEUS_BLOCKLIST_FILE} &>/dev/null
  47. ok_failed $?
  48. sed -i -r -e '/^[#;].*$/d;/^\s*$/d' ${ZEUS_BLOCKLIST_FILE}
  49.  
  50. message "[+] Download ${SPAMHAUS_BLOCKLIST_URL} to ${SPAMHAUS_BLOCKLIST_FILE}"
  51. /usr/bin/wget --no-check-certificate ${SPAMHAUS_BLOCKLIST_URL} -O ${SPAMHAUS_BLOCKLIST_FILE} &>/dev/null
  52. ok_failed $?
  53. sed -i -r -e '/^[#;].*$/d;/^\s*$/d' ${SPAMHAUS_BLOCKLIST_FILE}
  54. sed -i -r -e 's/\s*;.*$//' ${SPAMHAUS_BLOCKLIST_FILE}
  55.  
  56. ZEUS_IPSET_IPS="zeus_ipset_ips"
  57. message "[+] Check ipset list ${ZEUS_IPSET_IPS}"
  58. ipset list ${ZEUS_IPSET_IPS} &>/dev/null
  59. if [ $? -ne 0 ]; then
  60.     message "[+] Create ipset iphash ${ZEUS_IPSET_IPS}"
  61.     ipset create ${ZEUS_IPSET_IPS} iphash
  62.     ok_failed $?
  63.     message "[+] Add DROP netfilter rule at position ${DROP_RULE_POSITION}"
  64.     iptables -I INPUT ${DROP_RULE_POSITION} -m set --match-set ${ZEUS_IPSET_IPS} src -j DROP
  65.     ok_failed $?
  66. else
  67.     message "[+] Flush ipset ${ZEUS_IPSET_IPS}"
  68.     ipset flush ${ZEUS_IPSET_IPS}
  69. fi
  70.  
  71. message "[+] Adding IPs to be blocked from ${ZEUS_BLOCKLIST_FILE} $(wc -l ${ZEUS_BLOCKLIST_FILE} | awk '{print $1}') entries"
  72. for i in $(cat ${ZEUS_BLOCKLIST_FILE})
  73.     do
  74.         ipset add ${ZEUS_IPSET_IPS} $i
  75.     done
  76. message "Done."
  77.  
  78. SPAMHAUS_IPSET_IPRANGES="spamhaus_ipset_ipranges"
  79. message "[+] Check ipset list ${SPAMHAUS_IPSET_IPRANGES}"
  80. ipset list ${SPAMHAUS_IPSET_IPRANGES} &>/dev/null
  81. if [ $? -ne 0 ]; then
  82.     message "[+] Create ipset hash:net ${SPAMHAUS_IPSET_IPRANGES}"
  83.     ipset create ${SPAMHAUS_IPSET_IPRANGES} hash:net
  84.     ok_failed $?
  85.     message "[+] Add DROP netfilter rule at position ${DROP_RULE_POSITION}"
  86.     iptables -I INPUT ${DROP_RULE_POSITION} -m set --match-set ${SPAMHAUS_IPSET_IPRANGES} src -j DROP
  87.     ok_failed $?
  88. else
  89.     message "[+] Flush ipset ${SPAMHAUS_IPSET_IPRANGES}"
  90.     ipset flush ${SPAMHAUS_IPSET_IPRANGES}
  91. fi
  92.  
  93. message "[+] Adding IP RANGES to be blocked from ${SPAMHAUS_BLOCKLIST_FILE} $(wc -l ${SPAMHAUS_BLOCKLIST_FILE} | awk '{print $1}') entries"
  94. for i in $(cat ${SPAMHAUS_BLOCKLIST_FILE})
  95.     do
  96.         ipset add ${SPAMHAUS_IPSET_IPRANGES} $i
  97.     done
  98. message "Done."
  99.  
  100. message "[+] Netfilter INPUT rules"
  101. iptables -L INPUT -n -v --line-numbers | tee -a ${LOGFILE}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement