NiKaro127

iBlockList to IPTables

Dec 1st, 2012
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.84 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Blacklist's names & URLs arrays
  4. bl_name=([0]='spyware')
  5. bl_url=([0]='http://list.iblocklist.com/?list=bt_spyware&fileformat=p2p&archiveformat=gz')
  6. bl_name=(${bl_name[*]} [1]='webexploit')
  7. bl_url=(${bl_url[*]} [1]='http://list.iblocklist.com/?list=ghlzqtqxnzctvvajwwag&fileformat=p2p&archiveformat=gz')
  8. bl_name=(${bl_name[*]} [2]='spider')
  9. bl_url=(${bl_url[*]} [2]='http://list.iblocklist.com/?list=bt_spider&fileformat=p2p&archiveformat=gz')
  10. bl_name=(${bl_name[*]} [3]='hijacked')
  11. bl_url=(${bl_url[*]} [3]='http://list.iblocklist.com/?list=bt_hijacked&fileformat=p2p&archiveformat=gz')
  12. bl_name=(${bl_name[*]} [4]='dshield')
  13. bl_url=(${bl_url[*]} [4]='http://list.iblocklist.com/?list=bt_dshield&fileformat=p2p&archiveformat=gz')
  14. bl_name=(${bl_name[*]} [5]='bogon')
  15. bl_url=(${bl_url[*]} [5]='http://list.iblocklist.com/?list=bt_bogon&fileformat=p2p&archiveformat=gz')
  16.  
  17. # For each blacklist set above
  18. for i in {0..5}; do
  19.     # Download blacklist
  20.     wget --output-document=/tmp/blacklist_${bl_name[$i]}.gz -w 3 ${bl_url[$i]} -q
  21.    
  22.     # Delete current iptables chain link
  23.     iptables -D INPUT -j ${bl_name[$i]}
  24.     # Flush current iptables chain
  25.     iptables -F ${bl_name[$i]}
  26.     # Delete current iptables chain
  27.     iptables -X ${bl_name[$i]}
  28.     # Create current iptables chain
  29.     iptables -N ${bl_name[$i]}
  30.     # Link current iptables chain to INPUT chain
  31.     iptables -A INPUT -j ${bl_name[$i]}
  32.    
  33.     # Read blacklist
  34.     while read line; do
  35.         # Drop description, keep only IP range
  36.             ip_range=`echo -n $line | sed -e 's/.*:\(.*\)-\(.*\)/\1-\2/'`;
  37.         # Test if it's an IP range
  38.         if [[ $ip_range =~ ^[0-9].*$ ]]; then
  39.             # Add to the blacklist
  40.             iptables -A ${bl_name[$i]} -m iprange --src-range $ip_range -j DROP
  41.         fi
  42.     done < <(zcat /tmp/blacklist_${bl_name[$i]}.gz | iconv -f latin1 -t utf-8 - | dos2unix)
  43. done
  44.  
  45. # Delete files
  46. rm /tmp/blacklist*
  47.  
  48. exit 0
Advertisement
Add Comment
Please, Sign In to add comment