v-update-firewall 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #!/bin/bash
  2. # info: update system firewall rules
  3. # options: NONE
  4. #
  5. # The function updates iptables rules
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Defining absolute path for iptables and modprobe
  10. iptables="/sbin/iptables"
  11. modprobe="/sbin/modprobe"
  12. sysctl="/sbin/sysctl"
  13. # Includes
  14. source /etc/profile.d/vesta.sh
  15. source $VESTA/func/main.sh
  16. source $VESTA/conf/vesta.conf
  17. #----------------------------------------------------------#
  18. # Verifications #
  19. #----------------------------------------------------------#
  20. is_system_enabled "$FIREWALL_SYSTEM" 'FIREWALL_SYSTEM'
  21. #----------------------------------------------------------#
  22. # Action #
  23. #----------------------------------------------------------#
  24. # Checking local IPv4 rules
  25. rules="$VESTA/data/firewall/rules.conf"
  26. ports="$VESTA/data/firewall/ports.conf"
  27. if [ ! -e "$rules" ]; then
  28. exit
  29. fi
  30. $sysctl net.netfilter.nf_conntrack_max >/dev/null 2>&1
  31. if [ $? -ne 0 ]; then
  32. conntrack='no'
  33. fi
  34. # Checking conntrack module avaiabilty
  35. $modprobe nf_conntrack >/dev/null 2>&1
  36. $modprobe nf_conntrack_ftp >/dev/null 2>&1
  37. if [ $? -ne 0 ]; then
  38. conntrack_ftp='no'
  39. fi
  40. # Checking custom OpenSSH port
  41. sshport=$(grep '^Port ' /etc/ssh/sshd_config | head -1 | cut -d ' ' -f 2)
  42. if [[ "$sshport" =~ ^[0-9]+$ ]] && [ "$sshport" -ne "22" ]; then
  43. sed -i "s/PORT='22'/PORT=\'$sshport\'/" $rules
  44. fi
  45. # Creating temporary file
  46. tmp=$(mktemp)
  47. # Flushing INPUT chain
  48. echo "$iptables -P INPUT ACCEPT" >> $tmp
  49. echo "$iptables -F INPUT" >> $tmp
  50. # Enabling stateful support
  51. if [ "$conntrack" != 'no' ]; then
  52. str="$iptables -A INPUT -m state"
  53. str="$str --state ESTABLISHED,RELATED -j ACCEPT"
  54. echo "$str" >> $tmp
  55. fi
  56. # Handling local traffic
  57. for ip in $(ls $VESTA/data/ips); do
  58. echo "$iptables -A INPUT -s $ip -j ACCEPT" >> $tmp
  59. done
  60. echo "$iptables -A INPUT -s 127.0.0.1 -j ACCEPT" >> $tmp
  61. # Pasring iptables rules
  62. IFS=$'\n'
  63. for line in $(sort -r -n -k 2 -t \' $rules); do
  64. eval $line
  65. if [ "$SUSPENDED" = 'no' ]; then
  66. proto="-p $PROTOCOL"
  67. port="--dport $PORT"
  68. ip="-s $IP"
  69. state=""
  70. action="-j $ACTION"
  71. # Adding multiport module
  72. if [[ "$PORT" =~ ,|-|: ]] ; then
  73. port="-m multiport --dports ${PORT//-/:}"
  74. fi
  75. # Accepting all dst ports
  76. if [[ "$PORT" = "0" ]] || [ "$PROTOCOL" = 'ICMP' ]; then
  77. port=""
  78. fi
  79. # Checking FTP for contrack module
  80. if [ "$TYPE" = "FTP" ] || [ "$PORT" = '21' ]; then
  81. if [ "$conntrack_ftp" != 'no' ]; then
  82. state="-m conntrack --ctstate NEW"
  83. else
  84. port="-m multiport --dports 20,21,12000:12100"
  85. fi
  86. ftp="yes"
  87. fi
  88. # Adding firewall rule
  89. echo "$iptables -A INPUT $proto $port $ip $state $action" >> $tmp
  90. fi
  91. done
  92. # Switching chain policy to DROP
  93. echo "$iptables -P INPUT DROP" >> $tmp
  94. # Adding vesta chain
  95. echo "$iptables -N vesta" >> $tmp
  96. # Applying rules
  97. bash $tmp 2>/dev/null
  98. # Deleting temporary file
  99. rm -f $tmp
  100. # Checking custom trigger
  101. if [ -x "$VESTA/data/firewall/custom.sh" ]; then
  102. bash $VESTA/data/firewall/custom.sh
  103. fi
  104. # Checking fail2ban support
  105. if [ ! -z "$FIREWALL_EXTENSION" ]; then
  106. for chain in $(cat $VESTA/data/firewall/chains.conf 2>/dev/null); do
  107. eval $chain
  108. if [[ "$PORT" =~ ,|-|: ]] ; then
  109. port="-m multiport --dports $PORT"
  110. else
  111. port="--dport $PORT"
  112. fi
  113. echo "$iptables -N fail2ban-$CHAIN" >> $tmp
  114. echo "$iptables -F fail2ban-$CHAIN" >> $tmp
  115. echo "$iptables -I fail2ban-$CHAIN -s 0.0.0.0/0 -j RETURN" >> $tmp
  116. echo "$iptables -I INPUT -p $PROTOCOL $port -j fail2ban-$CHAIN" >>$tmp
  117. done
  118. bash $tmp 2>/dev/null
  119. rm -f $tmp
  120. for ban in $(cat $VESTA/data/firewall/banlist.conf 2>/dev/null); do
  121. eval $ban
  122. echo -n "$iptables -I fail2ban-$CHAIN 1 -s $IP" >> $tmp
  123. echo " -j REJECT --reject-with icmp-port-unreachable" >> $tmp
  124. done
  125. bash $tmp 2>/dev/null
  126. rm -f $tmp
  127. fi
  128. # Saving rules to the master iptables file
  129. if [ -e "/etc/redhat-release" ]; then
  130. /sbin/iptables-save > /etc/sysconfig/iptables
  131. if [ -z "$(ls /etc/rc3.d/S*iptables 2>/dev/null)" ]; then
  132. /sbin/chkconfig iptables on
  133. fi
  134. else
  135. /sbin/iptables-save > /etc/iptables.rules
  136. preup="/etc/network/if-pre-up.d/iptables"
  137. if [ ! -e "$preup" ]; then
  138. echo '#!/bin/sh' > $preup
  139. echo "/sbin/iptables-restore < /etc/iptables.rules" >> $preup
  140. echo "exit 0" >> $preup
  141. chmod +x $preup
  142. fi
  143. fi
  144. # Worarkound for OpenVZ
  145. if [ -e "/proc/vz/veinfo" ]; then
  146. dig @8.8.8.8 google.com +time=1 +tries=1 >/dev/null 2>&1
  147. if [ "$?" -ne 0 ]; then
  148. $BIN/v-stop-firewall
  149. fi
  150. fi
  151. #----------------------------------------------------------#
  152. # Vesta #
  153. #----------------------------------------------------------#
  154. exit