freedombone-utils-firewall 11KB

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