v-update-firewall 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #!/bin/bash
  2. # info: update system firewall rules
  3. # options: NONE
  4. # labels: panel
  5. #
  6. # example: v-update-firewall
  7. #
  8. # The function updates iptables rules
  9. #----------------------------------------------------------#
  10. # Variable&Function #
  11. #----------------------------------------------------------#
  12. # Defining absolute path for iptables and modprobe
  13. iptables="/sbin/iptables"
  14. modprobe="/sbin/modprobe"
  15. sysctl="/sbin/sysctl"
  16. # Includes
  17. source /etc/profile.d/hestia.sh
  18. # shellcheck source=/usr/local/hestia/func/main.sh
  19. source $HESTIA/func/main.sh
  20. # shellcheck source=/usr/local/hestia/func/firewall.sh
  21. source $HESTIA/func/firewall.sh
  22. # shellcheck source=/usr/local/hestia/conf/hestia.conf
  23. source $HESTIA/conf/hestia.conf
  24. #----------------------------------------------------------#
  25. # Verifications #
  26. #----------------------------------------------------------#
  27. is_system_enabled "$FIREWALL_SYSTEM" 'FIREWALL_SYSTEM'
  28. #----------------------------------------------------------#
  29. # Action #
  30. #----------------------------------------------------------#
  31. # Self heal iptables links
  32. heal_iptables_links
  33. # Checking local IPv4 rules
  34. rules="$HESTIA/data/firewall/rules.conf"
  35. if [ ! -e "$rules" ]; then
  36. exit
  37. fi
  38. # Checking conntrack module avaiabilty
  39. $modprobe nf_conntrack >/dev/null 2>&1
  40. if [ $? -ne 0 ]; then
  41. $sysctl net.netfilter.nf_conntrack_max >/dev/null 2>&1
  42. if [ $? -ne 0 ]; then
  43. conntrack='no'
  44. fi
  45. fi
  46. $modprobe nf_conntrack_ftp >/dev/null 2>&1
  47. if [ $? -ne 0 ]; then
  48. conntrack_ftp='no'
  49. fi
  50. # Checking custom OpenSSH port
  51. sshport=$(grep '^Port ' /etc/ssh/sshd_config | head -1 | cut -d ' ' -f 2)
  52. if [[ "$sshport" =~ ^[0-9]+$ ]] && [ "$sshport" -ne "22" ]; then
  53. sed -i "s/PORT='22'/PORT=\'$sshport\'/" $rules
  54. fi
  55. # Load ipset lists before adding Hestia iptables rules
  56. [ -x "$(which ipset)" ] && $BIN/v-update-firewall-ipset
  57. # Creating temporary file
  58. tmp=$(mktemp)
  59. # Flushing INPUT chain
  60. echo "$iptables -P INPUT ACCEPT" >> $tmp
  61. echo "$iptables -F INPUT" >> $tmp
  62. # Enabling stateful support
  63. if [ "$conntrack" != 'no' ] || grep --quiet container=lxc /proc/1/environ; then
  64. str="$iptables -A INPUT -m state"
  65. str="$str --state ESTABLISHED,RELATED -j ACCEPT"
  66. echo "$str" >> $tmp
  67. fi
  68. # Handling local traffic
  69. for ip in $(ls $HESTIA/data/ips); do
  70. echo "$iptables -A INPUT -s $ip -j ACCEPT" >> $tmp
  71. done
  72. echo "$iptables -A INPUT -s 127.0.0.1 -j ACCEPT" >> $tmp
  73. # Pasring iptables rules
  74. IFS=$'\n'
  75. for line in $(sort -r -n -k 2 -t \' $rules); do
  76. parse_object_kv_list "$line"
  77. if [ "$SUSPENDED" = 'no' ]; then
  78. proto="-p $PROTOCOL"
  79. port="--dport $PORT"
  80. state=""
  81. action="-j $ACTION"
  82. if [[ "$IP" =~ ^ipset: ]]; then
  83. ipset_name="${IP#ipset:}"
  84. $(v-list-firewall-ipset plain | grep "^$ipset_name\s" >/dev/null) || log_event $E_NOTEXIST "ipset object ($ipset_name) not found"
  85. ip="-m set --match-set '${ipset_name}' src"
  86. else
  87. ip="-s $IP"
  88. fi
  89. # Adding multiport module
  90. if [[ "$PORT" =~ ,|-|: ]] ; then
  91. port="-m multiport --dports ${PORT//-/:}"
  92. fi
  93. # Accepting all dst ports
  94. if [[ "$PORT" = "0" ]] || [ "$PROTOCOL" = 'ICMP' ]; then
  95. port=""
  96. fi
  97. # Checking FTP for contrack module
  98. if [ "$TYPE" = "FTP" ] || [ "$PORT" = '21' ]; then
  99. if [ "$conntrack_ftp" != 'no' ]; then
  100. state="-m conntrack --ctstate NEW"
  101. else
  102. port="-m multiport --dports 20,21,12000:12100"
  103. fi
  104. ftp="yes"
  105. fi
  106. # Adding firewall rule
  107. echo "$iptables -A INPUT $proto $port $ip $state $action" >> $tmp
  108. fi
  109. done
  110. # Switching chain policy to DROP
  111. echo "$iptables -P INPUT DROP" >> $tmp
  112. # Adding hestia chain
  113. echo "$iptables -N hestia" >> $tmp
  114. # Applying rules
  115. bash $tmp 2>/dev/null
  116. # Deleting temporary file
  117. rm -f $tmp
  118. # Checking custom trigger
  119. if [ -x "$HESTIA/data/firewall/custom.sh" ]; then
  120. bash $HESTIA/data/firewall/custom.sh
  121. fi
  122. # Checking fail2ban support
  123. if [ ! -z "$FIREWALL_EXTENSION" ]; then
  124. for chain in $(cat $HESTIA/data/firewall/chains.conf 2>/dev/null); do
  125. parse_object_kv_list "$chain"
  126. if [[ "$PORT" =~ ,|-|: ]] ; then
  127. port="-m multiport --dports $PORT"
  128. else
  129. port="--dport $PORT"
  130. fi
  131. echo "$iptables -N fail2ban-$CHAIN" >> $tmp
  132. echo "$iptables -F fail2ban-$CHAIN" >> $tmp
  133. echo "$iptables -I fail2ban-$CHAIN -s 0.0.0.0/0 -j RETURN" >> $tmp
  134. echo "$iptables -I INPUT -p $PROTOCOL $port -j fail2ban-$CHAIN" >>$tmp
  135. done
  136. bash $tmp 2>/dev/null
  137. rm -f $tmp
  138. for ban in $(cat $HESTIA/data/firewall/banlist.conf 2>/dev/null); do
  139. parse_object_kv_list "$ban"
  140. echo -n "$iptables -I fail2ban-$CHAIN 1 -s $IP" >> $tmp
  141. echo " -j REJECT --reject-with icmp-port-unreachable" >> $tmp
  142. done
  143. bash $tmp 2>/dev/null
  144. rm -f $tmp
  145. fi
  146. # Saving rules to the master iptables file
  147. if [ -d "/etc/sysconfig" ]; then
  148. /sbin/iptables-save > /etc/sysconfig/iptables
  149. if [ -z "$(ls /etc/rc3.d/S*iptables 2>/dev/null)" ]; then
  150. /sbin/chkconfig iptables on
  151. fi
  152. else
  153. /sbin/iptables-save > /etc/iptables.rules
  154. routable="/usr/lib/networkd-dispatcher/routable.d/10-hestia-iptables"
  155. preup="/etc/network/if-pre-up.d/hestia-iptables"
  156. # Recreate the Hestia iptables rules loading script
  157. rm -f $routable $preup
  158. if dpkg-query -W -f'${Status}' "netplan*" 2>/dev/null | grep -q "ok installed" && [ -d /etc/netplan ] && [ -n "$(ls -A /etc/netplan 2>/dev/null)" ]; then
  159. echo '#!/bin/sh' > $routable
  160. echo '' >> $routable
  161. echo 'if [ "$IFACE" = "'$(ip route list | awk '/default .+/ {print $5}' | uniq)'" ]; then' >> $routable
  162. echo ' [ -x "'$(which ipset)'" ] && '"${HESTIA}/bin/v-update-firewall-ipset" >> $routable
  163. echo ' /sbin/iptables-restore < /etc/iptables.rules' >> $routable
  164. echo 'fi' >> $routable
  165. echo '' >> $routable
  166. echo "exit 0" >> $routable
  167. chmod +x $routable
  168. else
  169. echo '#!/bin/sh' > $preup
  170. echo '' >> $preup
  171. echo 'if [ "$IFACE" = "'$(ip route list | awk '/default .+/ {print $5}' | uniq)'" ]; then' >> $preup
  172. echo ' [ -x "'$(which ipset)'" ] && '"${HESTIA}/bin/v-update-firewall-ipset" >> $preup
  173. echo ' /sbin/iptables-restore < /etc/iptables.rules' >> $preup
  174. echo 'fi' >> $preup
  175. echo '' >> $preup
  176. echo "exit 0" >> $preup
  177. chmod +x $preup
  178. fi
  179. fi
  180. #----------------------------------------------------------#
  181. # Hestia #
  182. #----------------------------------------------------------#
  183. exit