v-update-firewall 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. # Pasring iptables rules
  51. IFS=$'\n'
  52. for line in $(sort -r -n -k 2 -t \' $rules); do
  53. eval $line
  54. if [ "$SUSPENDED" = 'no' ]; then
  55. proto="-p $PROTOCOL"
  56. port="--dport $PORT"
  57. ip="-s $IP"
  58. state=""
  59. action="-j $ACTION"
  60. # Adding multiport module
  61. if [[ "$PORT" =~ ,|-|: ]] ; then
  62. port="-m multiport --dports ${PORT//-/:}"
  63. fi
  64. # Accepting all dst ports
  65. if [[ "$PORT" = "0" ]] || [ "$PROTOCOL" = 'ICMP' ]; then
  66. port=""
  67. fi
  68. # Checking FTP for contrack module
  69. if [ "$TYPE" = "FTP" ] || [ "$PORT" = '21' ]; then
  70. if [ "$conntrack_ftp" != 'no' ]; then
  71. state="-m conntrack --ctstate NEW"
  72. else
  73. port="-m multiport --dports 20,21,12000:12100"
  74. fi
  75. ftp="yes"
  76. fi
  77. # Adding firewall rule
  78. echo "$iptables -A INPUT $proto $port $ip $state $action" >> $tmp
  79. fi
  80. done
  81. # Handling local traffic
  82. for ip in $(ls $VESTA/data/ips); do
  83. echo "$iptables -A INPUT -s $ip -j ACCEPT" >> $tmp
  84. done
  85. echo "$iptables -A INPUT -s 127.0.0.1 -j ACCEPT" >> $tmp
  86. IFS=$'\n'
  87. for p_rule in $(cat $ports); do
  88. eval $p_rule
  89. rule="$iptables -A INPUT -p $PROTOCOL"
  90. echo "$rule --sport $PORT -j ACCEPT" >> $tmp
  91. done
  92. # Enabling stateful support
  93. if [ "$conntrack" != 'no' ]; then
  94. str="$iptables -A INPUT -p tcp -m state"
  95. str="$str --state ESTABLISHED,RELATED -j ACCEPT"
  96. echo "$str" >> $tmp
  97. fi
  98. # Switching chain policy to DROP
  99. echo "$iptables -P INPUT DROP" >> $tmp
  100. # Adding vesta chain
  101. echo "$iptables -N vesta" >> $tmp
  102. # Applying rules
  103. bash $tmp 2>/dev/null
  104. # Deleting temporary file
  105. rm -f $tmp
  106. # Checking custom trigger
  107. if [ -x "$VESTA/data/firewall/custom.sh" ]; then
  108. bash $VESTA/data/firewall/custom.sh
  109. fi
  110. # Checking fail2ban support
  111. if [ ! -z "$FIREWALL_EXTENSION" ]; then
  112. for chain in $(cat $VESTA/data/firewall/chains.conf 2>/dev/null); do
  113. eval $chain
  114. if [[ "$PORT" =~ ,|-|: ]] ; then
  115. port="-m multiport --dports $PORT"
  116. else
  117. port="--dport $PORT"
  118. fi
  119. echo "$iptables -N fail2ban-$CHAIN" >> $tmp
  120. echo "$iptables -F fail2ban-$CHAIN" >> $tmp
  121. echo "$iptables -I fail2ban-$CHAIN -s 0.0.0.0/0 -j RETURN" >> $tmp
  122. echo "$iptables -I INPUT -p $PROTOCOL $port -j fail2ban-$CHAIN" >>$tmp
  123. done
  124. bash $tmp 2>/dev/null
  125. rm -f $tmp
  126. for ban in $(cat $VESTA/data/firewall/banlist.conf 2>/dev/null); do
  127. eval $ban
  128. echo -n "$iptables -I fail2ban-$CHAIN 1 -s $IP" >> $tmp
  129. echo " -j REJECT --reject-with icmp-port-unreachable" >> $tmp
  130. done
  131. bash $tmp 2>/dev/null
  132. rm -f $tmp
  133. fi
  134. # Saving rules to the master iptables file
  135. if [ -e "/etc/redhat-release" ]; then
  136. /sbin/iptables-save > /etc/sysconfig/iptables
  137. if [ -z "$(ls /etc/rc3.d/S*iptables 2>/dev/null)" ]; then
  138. /sbin/chkconfig iptables on
  139. fi
  140. else
  141. /sbin/iptables-save > /etc/iptables.rules
  142. preup="/etc/network/if-pre-up.d/iptables"
  143. if [ ! -e "$preup" ]; then
  144. echo '#!/bin/sh' > $preup
  145. echo "/sbin/iptables-restore < /etc/iptables.rules" >> $preup
  146. echo "exit 0" >> $preup
  147. chmod +x $preup
  148. fi
  149. fi
  150. #----------------------------------------------------------#
  151. # Vesta #
  152. #----------------------------------------------------------#
  153. exit