freedombone-utils-firewall 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Firewall functions
  12. #
  13. # TODO: in future investigate using nftables
  14. #
  15. # License
  16. # =======
  17. #
  18. # Copyright (C) 2014-2016 Bob Mottram <bob@freedombone.net>
  19. #
  20. # This program is free software: you can redistribute it and/or modify
  21. # it under the terms of the GNU Affero General Public License as published by
  22. # the Free Software Foundation, either version 3 of the License, or
  23. # (at your option) any later version.
  24. #
  25. # This program is distributed in the hope that it will be useful,
  26. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. # GNU Affero General Public License for more details.
  29. #
  30. # You should have received a copy of the GNU Affero General Public License
  31. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  32. FIREWALL_CONFIG=$HOME/${PROJECT_NAME}-firewall.cfg
  33. function save_firewall_settings {
  34. iptables-save > /etc/firewall.conf
  35. ip6tables-save > /etc/firewall6.conf
  36. printf '#!/bin/sh\n' > /etc/network/if-up.d/iptables
  37. printf 'iptables-restore < /etc/firewall.conf\n' >> /etc/network/if-up.d/iptables
  38. printf 'ip6tables-restore < /etc/firewall6.conf\n' >> /etc/network/if-up.d/iptables
  39. if [ -f /etc/network/if-up.d/iptables ]; then
  40. chmod +x /etc/network/if-up.d/iptables
  41. fi
  42. }
  43. function global_rate_limit {
  44. if ! grep -q "tcp_challenge_ack_limit" /etc/sysctl.conf; then
  45. echo 'net.ipv4.tcp_challenge_ack_limit = 999999999' >> /etc/sysctl.conf
  46. else
  47. sed -i 's|net.ipv4.tcp_challenge_ack_limit.*|net.ipv4.tcp_challenge_ack_limit = 999999999|g' /etc/sysctl.conf
  48. fi
  49. sysctl -p -q
  50. }
  51. function enable_ipv6 {
  52. # endure that ipv6 is enabled and can route
  53. sed -i 's/net.ipv6.conf.all.disable_ipv6.*/net.ipv6.conf.all.disable_ipv6 = 0/g' /etc/sysctl.conf
  54. #sed -i "s/net.ipv6.conf.all.accept_redirects.*/net.ipv6.conf.all.accept_redirects = 1/g" /etc/sysctl.conf
  55. #sed -i "s/net.ipv6.conf.all.accept_source_route.*/net.ipv6.conf.all.accept_source_route = 1/g" /etc/sysctl.conf
  56. sed -i "s/net.ipv6.conf.all.forwarding.*/net.ipv6.conf.all.forwarding=1/g" /etc/sysctl.conf
  57. echo 1 > /proc/sys/net/ipv6/conf/all/forwarding
  58. }
  59. function configure_firewall {
  60. if [ $INSTALLING_MESH ]; then
  61. mesh_firewall
  62. return
  63. fi
  64. if grep -q "RELATED" /etc/firewall.conf; then
  65. # recreate the firewall to remove RELATED
  66. sed -i "/firewall/d" $COMPLETION_FILE
  67. fi
  68. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  69. return
  70. fi
  71. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  72. # docker does its own firewalling
  73. return
  74. fi
  75. iptables -P INPUT ACCEPT
  76. ip6tables -P INPUT ACCEPT
  77. iptables -F
  78. ip6tables -F
  79. iptables -t nat -F
  80. ip6tables -t nat -F
  81. iptables -X
  82. ip6tables -X
  83. iptables -P INPUT DROP
  84. ip6tables -P INPUT DROP
  85. iptables -A INPUT -i lo -j ACCEPT
  86. iptables -A INPUT -m conntrack --ctstate ESTABLISHED -j ACCEPT
  87. # Make sure incoming tcp connections are SYN packets
  88. iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP
  89. # Drop packets with incoming fragments
  90. iptables -A INPUT -f -j DROP
  91. # Drop bogons
  92. iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
  93. iptables -A INPUT -p tcp --tcp-flags ALL FIN,PSH,URG -j DROP
  94. iptables -A INPUT -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP
  95. # Incoming malformed NULL packets:
  96. iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
  97. mark_completed $FUNCNAME
  98. }
  99. function configure_firewall_ping {
  100. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  101. return
  102. fi
  103. # Only allow ping for mesh installs
  104. if [[ $SYSTEM_TYPE != "mesh"* ]]; then
  105. return
  106. fi
  107. iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
  108. iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT
  109. function_check save_firewall_settings
  110. save_firewall_settings
  111. mark_completed $FUNCNAME
  112. }
  113. function configure_internet_protocol {
  114. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  115. return
  116. fi
  117. if [[ $SYSTEM_TYPE == "mesh"* ]]; then
  118. return
  119. fi
  120. sed -i "s/#net.ipv4.tcp_syncookies=1/net.ipv4.tcp_syncookies=1/g" /etc/sysctl.conf
  121. sed -i "s/#net.ipv4.conf.all.accept_redirects = 0/net.ipv4.conf.all.accept_redirects = 0/g" /etc/sysctl.conf
  122. sed -i "s/#net.ipv6.conf.all.accept_redirects = 0/net.ipv6.conf.all.accept_redirects = 0/g" /etc/sysctl.conf
  123. sed -i "s/#net.ipv4.conf.all.send_redirects = 0/net.ipv4.conf.all.send_redirects = 0/g" /etc/sysctl.conf
  124. sed -i "s/#net.ipv4.conf.all.accept_source_route = 0/net.ipv4.conf.all.accept_source_route = 0/g" /etc/sysctl.conf
  125. sed -i "s/#net.ipv6.conf.all.accept_source_route = 0/net.ipv6.conf.all.accept_source_route = 0/g" /etc/sysctl.conf
  126. sed -i "s/#net.ipv4.conf.default.rp_filter=1/net.ipv4.conf.default.rp_filter=1/g" /etc/sysctl.conf
  127. sed -i "s/#net.ipv4.conf.all.rp_filter=1/net.ipv4.conf.all.rp_filter=1/g" /etc/sysctl.conf
  128. sed -i "s/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=0/g" /etc/sysctl.conf
  129. sed -i "s/#net.ipv6.conf.all.forwarding=1/net.ipv6.conf.all.forwarding=0/g" /etc/sysctl.conf
  130. if ! grep -q "ignore pings" /etc/sysctl.conf; then
  131. echo '# ignore pings' >> /etc/sysctl.conf
  132. echo 'net.ipv4.icmp_echo_ignore_all = 1' >> /etc/sysctl.conf
  133. echo 'net.ipv6.icmp_echo_ignore_all = 1' >> /etc/sysctl.conf
  134. fi
  135. if ! grep -q "disable ipv6" /etc/sysctl.conf; then
  136. echo '# disable ipv6' >> /etc/sysctl.conf
  137. echo 'net.ipv6.conf.all.disable_ipv6 = 1' >> /etc/sysctl.conf
  138. fi
  139. if ! grep -q "net.ipv4.tcp_synack_retries" /etc/sysctl.conf; then
  140. echo 'net.ipv4.tcp_synack_retries = 2' >> /etc/sysctl.conf
  141. echo 'net.ipv4.tcp_syn_retries = 1' >> /etc/sysctl.conf
  142. fi
  143. if ! grep -q "keepalive" /etc/sysctl.conf; then
  144. echo '# keepalive' >> /etc/sysctl.conf
  145. echo 'net.ipv4.tcp_keepalive_probes = 9' >> /etc/sysctl.conf
  146. echo 'net.ipv4.tcp_keepalive_intvl = 75' >> /etc/sysctl.conf
  147. echo 'net.ipv4.tcp_keepalive_time = 7200' >> /etc/sysctl.conf
  148. fi
  149. mark_completed $FUNCNAME
  150. }
  151. function mesh_firewall {
  152. FIREWALL_FILENAME=${rootdir}/etc/systemd/system/meshfirewall.service
  153. MESH_FIREWALL_SCRIPT=${rootdir}/usr/bin/mesh-firewall
  154. echo '#!/bin/bash' > $MESH_FIREWALL_SCRIPT
  155. echo 'iptables -P INPUT ACCEPT' >> $MESH_FIREWALL_SCRIPT
  156. echo 'ip6tables -P INPUT ACCEPT' >> $MESH_FIREWALL_SCRIPT
  157. echo 'iptables -F' >> $MESH_FIREWALL_SCRIPT
  158. echo 'ip6tables -F' >> $MESH_FIREWALL_SCRIPT
  159. echo 'iptables -t nat -F' >> $MESH_FIREWALL_SCRIPT
  160. echo 'ip6tables -t nat -F' >> $MESH_FIREWALL_SCRIPT
  161. echo 'iptables -X' >> $MESH_FIREWALL_SCRIPT
  162. echo 'ip6tables -X' >> $MESH_FIREWALL_SCRIPT
  163. echo 'iptables -P INPUT DROP' >> $MESH_FIREWALL_SCRIPT
  164. echo 'ip6tables -P INPUT DROP' >> $MESH_FIREWALL_SCRIPT
  165. echo 'iptables -A INPUT -i lo -j ACCEPT' >> $MESH_FIREWALL_SCRIPT
  166. echo 'iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT' >> $MESH_FIREWALL_SCRIPT
  167. echo '' >> $MESH_FIREWALL_SCRIPT
  168. echo '# Make sure incoming tcp connections are SYN packets' >> $MESH_FIREWALL_SCRIPT
  169. echo 'iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP' >> $MESH_FIREWALL_SCRIPT
  170. echo '' >> $MESH_FIREWALL_SCRIPT
  171. echo '# Drop packets with incoming fragments' >> $MESH_FIREWALL_SCRIPT
  172. echo 'iptables -A INPUT -f -j DROP' >> $MESH_FIREWALL_SCRIPT
  173. echo '' >> $MESH_FIREWALL_SCRIPT
  174. echo '# Drop bogons' >> $MESH_FIREWALL_SCRIPT
  175. echo 'iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP' >> $MESH_FIREWALL_SCRIPT
  176. echo 'iptables -A INPUT -p tcp --tcp-flags ALL FIN,PSH,URG -j DROP' >> $MESH_FIREWALL_SCRIPT
  177. echo 'iptables -A INPUT -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP' >> $MESH_FIREWALL_SCRIPT
  178. echo '' >> $MESH_FIREWALL_SCRIPT
  179. echo '# Incoming malformed NULL packets:' >> $MESH_FIREWALL_SCRIPT
  180. echo 'iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP' >> $MESH_FIREWALL_SCRIPT
  181. echo '' >> $MESH_FIREWALL_SCRIPT
  182. echo "iptables -A INPUT -p tcp --dport $TOX_PORT -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  183. echo "iptables -A INPUT -i $WIFI_INTERFACE -p udp --dport $ZERONET_PORT -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  184. echo "iptables -A INPUT -i $WIFI_INTERFACE -p tcp --dport $ZERONET_PORT -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  185. echo "iptables -A INPUT -i $WIFI_INTERFACE -p udp --dport $TRACKER_PORT -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  186. echo "iptables -A INPUT -i $WIFI_INTERFACE -p tcp --dport $TRACKER_PORT -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  187. echo "iptables -A INPUT -i $WIFI_INTERFACE -p udp --dport 1900 -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  188. chmod +x $MESH_FIREWALL_SCRIPT
  189. echo '[Unit]' > $FIREWALL_FILENAME
  190. echo 'Description=Mesh Firewall' >> $FIREWALL_FILENAME
  191. echo '' >> $FIREWALL_FILENAME
  192. echo '[Service]' >> $FIREWALL_FILENAME
  193. echo 'Type=oneshot' >> $FIREWALL_FILENAME
  194. echo 'ExecStart=/usr/bin/mesh-firewall' >> $FIREWALL_FILENAME
  195. echo 'RemainAfterExit=no' >> $FIREWALL_FILENAME
  196. echo '' >> $FIREWALL_FILENAME
  197. echo 'TimeoutSec=30' >> $FIREWALL_FILENAME
  198. echo '' >> $FIREWALL_FILENAME
  199. echo '[Install]' >> $FIREWALL_FILENAME
  200. echo 'WantedBy=multi-user.target' >> $FIREWALL_FILENAME
  201. chmod +x $FIREWALL_FILENAME
  202. chroot "$rootdir" systemctl enable meshfirewall
  203. }
  204. function firewall_add {
  205. firewall_name=$(echo "$1" | sed "s| |-|g")
  206. firewall_port=$2
  207. firewall_protocol="$3"
  208. if ! grep -q "${firewall_name}=${firewall_port}" $FIREWALL_CONFIG; then
  209. echo "${firewall_name}=${firewall_port}" >> $FIREWALL_CONFIG
  210. if [ ! ${firewall_protocol} ]; then
  211. iptables -A INPUT -p udp --dport ${firewall_port} -j ACCEPT
  212. iptables -A INPUT -p tcp --dport ${firewall_port} -j ACCEPT
  213. else
  214. if [[ "${firewall_protocol}" == *"udp"* ]]; then
  215. iptables -A INPUT -p udp --dport ${firewall_port} -j ACCEPT
  216. fi
  217. if [[ "${firewall_protocol}" == *"tcp"* ]]; then
  218. iptables -A INPUT -p tcp --dport ${firewall_port} -j ACCEPT
  219. fi
  220. fi
  221. save_firewall_settings
  222. fi
  223. }
  224. function firewall_add_range {
  225. firewall_name=$(echo "$1" | sed "s| |-|g")
  226. firewall_port_start=$2
  227. firewall_port_end=$3
  228. firewall_protocol="$4"
  229. if ! grep -q "${firewall_name}=${firewall_port_start}:${firewall_port_end}" $FIREWALL_CONFIG; then
  230. echo "${firewall_name}=${firewall_port_start}:${firewall_port_end}" >> $FIREWALL_CONFIG
  231. if [ ! ${firewall_protocol} ]; then
  232. iptables -A INPUT -p udp --dport ${firewall_port_start}:${firewall_port_end} -j ACCEPT
  233. iptables -A INPUT -p tcp --dport ${firewall_port_start}:${firewall_port_end} -j ACCEPT
  234. else
  235. if [[ "${firewall_protocol}" == *"udp"* ]]; then
  236. iptables -A INPUT -p udp --dport ${firewall_port_start}:${firewall_port_end} -j ACCEPT
  237. fi
  238. if [[ "${firewall_protocol}" == *"tcp"* ]]; then
  239. iptables -A INPUT -p tcp --dport ${firewall_port_start}:${firewall_port_end} -j ACCEPT
  240. fi
  241. fi
  242. save_firewall_settings
  243. fi
  244. }
  245. function firewall_remove {
  246. firewall_port=$1
  247. firewall_protocol="$2"
  248. if [ ! -f $FIREWALL_CONFIG ]; then
  249. return
  250. fi
  251. if grep -q "=${firewall_port}" $FIREWALL_CONFIG; then
  252. if [ ! ${firewall_protocol} ]; then
  253. iptables -D INPUT -p udp --dport ${firewall_port} -j ACCEPT
  254. iptables -D INPUT -p tcp --dport ${firewall_port} -j ACCEPT
  255. else
  256. if [[ "${firewall_protocol}" == *"udp"* ]]; then
  257. iptables -D INPUT -p udp --dport ${firewall_port} -j ACCEPT
  258. fi
  259. if [[ "${firewall_protocol}" == *"tcp"* ]]; then
  260. iptables -D INPUT -p tcp --dport ${firewall_port} -j ACCEPT
  261. fi
  262. fi
  263. sed -i "/=${firewall_port}/d" $FIREWALL_CONFIG
  264. save_firewall_settings
  265. fi
  266. }
  267. # NOTE: deliberately no exit 0