v-add-firewall-ban 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/bin/bash
  2. # info: add firewall blocking rule
  3. # options: IP CHAIN
  4. #
  5. # The function adds new blocking rule to system firewall
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Importing system variables
  10. source /etc/profile
  11. # Argument definition
  12. ip=$1
  13. chain=$(echo $2|tr '[:lower:]' '[:upper:]')
  14. # Defining absolute path for iptables and modprobe
  15. iptables="/sbin/iptables"
  16. # Includes
  17. source $VESTA/func/main.sh
  18. source $VESTA/conf/vesta.conf
  19. #----------------------------------------------------------#
  20. # Verifications #
  21. #----------------------------------------------------------#
  22. check_args '2' "$#" 'IP CHAIN'
  23. is_format_valid 'ip' 'chain'
  24. is_system_enabled "$FIREWALL_SYSTEM" 'FIREWALL_SYSTEM'
  25. #----------------------------------------------------------#
  26. # Action #
  27. #----------------------------------------------------------#
  28. # Checking server ip
  29. if [ -e "$VESTA/data/ips/$ip" ] || [ "$ip" = '127.0.0.1' ]; then
  30. exit
  31. fi
  32. # Checking ip exclusions
  33. excludes="$VESTA/data/firewall/excludes.conf"
  34. check_excludes=$(grep "^$ip$" $excludes 2>/dev/null)
  35. if [ ! -z "$check_excludes" ]; then
  36. exit
  37. fi
  38. # Checking ip in banlist
  39. conf="$VESTA/data/firewall/banlist.conf"
  40. check_ip=$(grep "IP='$ip' CHAIN='$chain'" $conf 2>/dev/null)
  41. if [ ! -z "$check_ip" ]; then
  42. exit
  43. fi
  44. # Adding chain
  45. $BIN/v-add-firewall-chain $chain
  46. # Generating timestamp
  47. time_n_date=$(date +'%T %F')
  48. time=$(echo "$time_n_date" |cut -f 1 -d \ )
  49. date=$(echo "$time_n_date" |cut -f 2 -d \ )
  50. # Adding ip to banlist
  51. echo "IP='$ip' CHAIN='$chain' TIME='$time' DATE='$date'" >> $conf
  52. $iptables -I fail2ban-$chain 1 -s $ip \
  53. -j REJECT --reject-with icmp-port-unreachable 2>/dev/null
  54. # Changing permissions
  55. chmod 660 $conf
  56. #----------------------------------------------------------#
  57. # Vesta #
  58. #----------------------------------------------------------#
  59. # Logging
  60. log_event "$OK" "$ARGUMENTS"
  61. exit