resetfirewall.sh 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/bin/bash
  2. #
  3. BADGUYS_FILE="" # The location you want to download the list (/path/to/badguys.txt)
  4. CHAIN_NAME="CINSARMY_IPS" # The firewall chain to hold the cinsarmy rules
  5. FIREWALL="/sbin/iptables" # The path to iptables
  6. RULESFILE="" # The path to your iptables rules file (/path/to/iptables.rules)
  7. ETH="" # The name of the ethernet port you'd like to apply these rules to. Example: eth1
  8. echo "Clearing firewall."
  9. $FIREWALL -P INPUT ACCEPT
  10. $FIREWALL -P FORWARD ACCEPT
  11. $FIREWALL -P OUTPUT ACCEPT
  12. $FIREWALL -t nat -F
  13. $FIREWALL -t mangle -F
  14. $FIREWALL -F
  15. $FIREWALL -X
  16. echo "Restoring firewall from iptables.rules."
  17. $FIREWALL-restore < $RULESFILE
  18. echo "Restoring fail2ban rules."
  19. sudo systemctl restart fail2ban
  20. echo "Downloading cinsarmy IP list..."
  21. wget -c https://cinsscore.com/list/ci-badguys.txt -O $BADGUYS_FILE
  22. echo "Making sure it downloaded."
  23. # Check if the file exists
  24. if [[ ! -f "$BADGUYS_FILE" ]]; then
  25. echo "File not found: $BADGUYS_FILE"
  26. exit 1
  27. fi
  28. echo "Processing the list..."
  29. # Check if the chain exists, and creates it if it's not there
  30. if $FIREWALL -L $CHAIN_NAME -n &> /dev/null; then
  31. echo "Chain $CHAIN_NAME already exists."
  32. $FIREWALL -F $CHAIN_NAME
  33. else
  34. echo "Creating chain $CHAIN_NAME."
  35. $FIREWALL -N $CHAIN_NAME
  36. fi
  37. # Read each line (IP address) from the file and block it
  38. while IFS= read -r ip; do
  39. # Check if the line is not empty
  40. if [[ -n "$ip" ]]; then
  41. # Block the IP address using iptables
  42. $FIREWALL -A $CHAIN_NAME -i $ETH -s "$ip" -j DROP
  43. # echo "Blocked IP: $ip"
  44. fi
  45. done < "$BADGUYS_FILE"
  46. # Jump to the our chain from the FORWARD CHAIN
  47. $FIREWALL -A FORWARD -j $CHAIN_NAME
  48. # Delete the bad guys file
  49. rm $BADGUYS_FILE
  50. echo "Bad guys list has been processed, and deleted."
  51. echo "Checking for return rule at the end of our chain."
  52. # Check if the rule already exists
  53. RETURNRULE="-A $CHAIN_NAME -p tcp -j RETURN"
  54. if $FIREWALL -S $CHAIN_NAME | grep -q "$RETURNRULE"; then
  55. echo "Rule already exists: $RETURNRULE"
  56. else
  57. # Add the rule if it doesn't exist
  58. $FIREWALL $RETURNRULE
  59. echo "Rule added: $RETURNRULE"
  60. fi
  61. echo "Done.