v-update-firewall 6.3 KB

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