resetfirewall.sh 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/bin/bash
  2. #
  3. BADGUYS_FILE="/root/ci-badguys.txt"
  4. CHAIN_NAME="CINSARMY_IPS"
  5. FIREWALL="/sbin/iptables"
  6. ETH="eth1"
  7. echo "Clearing firewall."
  8. $FIREWALL -P INPUT ACCEPT
  9. $FIREWALL -P FORWARD ACCEPT
  10. $FIREWALL -P OUTPUT ACCEPT
  11. $FIREWALL -t nat -F
  12. $FIREWALL -t mangle -F
  13. $FIREWALL -F
  14. $FIREWALL -X
  15. echo "Restoring firewall from iptables.rules."
  16. /sbin/iptables-restore < /etc/iptables.rules
  17. echo "Restoring fail2ban rules."
  18. sudo systemctl restart fail2ban
  19. echo "Downloading cinsarmy IP list..."
  20. wget -c https://cinsscore.com/list/ci-badguys.txt -O $BADGUYS_FILE
  21. echo "Making sure it downloaded."
  22. # Check if the file exists
  23. if [[ ! -f "$BADGUYS_FILE" ]]; then
  24. echo "File not found: $BADGUYS_FILE"
  25. exit 1
  26. fi
  27. echo "Processing the list..."
  28. # Check if the chain exists, and creates it if it's not there
  29. if $FIREWALL -L $CHAIN_NAME -n &> /dev/null; then
  30. echo "Chain $CHAIN_NAME already exists."
  31. $FIREWALL -F $CHAIN_NAME
  32. else
  33. echo "Creating chain $CHAIN_NAME."
  34. $FIREWALL -N $CHAIN_NAME
  35. fi
  36. # Read each line (IP address) from the file and block it
  37. while IFS= read -r ip; do
  38. # Check if the line is not empty
  39. if [[ -n "$ip" ]]; then
  40. # Block the IP address using iptables
  41. $FIREWALL -A $CHAIN_NAME -i $ETH -s "$ip" -j DROP
  42. # echo "Blocked IP: $ip"
  43. fi
  44. done < "$BADGUYS_FILE"
  45. # Jump to the our chain from the FORWARD CHAIN
  46. $FIREWALL -A FORWARD -j $CHAIN_NAME
  47. # Delete the bad guys file
  48. rm $BADGUYS_FILE
  49. echo "Bad guys list has been processed, and deleted."