freedombone-utils-firewall 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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_firewall_for_avahi {
  110. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  111. return
  112. fi
  113. iptables -A INPUT -p tcp --dport 548 -j ACCEPT
  114. iptables -A INPUT -p udp --dport 548 -j ACCEPT
  115. iptables -A INPUT -p tcp --dport 5353 -j ACCEPT
  116. iptables -A INPUT -p udp --dport 5353 -j ACCEPT
  117. iptables -A INPUT -p tcp --dport 5354 -j ACCEPT
  118. iptables -A INPUT -p udp --dport 5354 -j ACCEPT
  119. function_check save_firewall_settings
  120. save_firewall_settings
  121. mark_completed $FUNCNAME
  122. }
  123. function configure_firewall_for_dns {
  124. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  125. return
  126. fi
  127. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  128. # docker does its own firewalling
  129. return
  130. fi
  131. iptables -A INPUT -p udp -m udp --dport 1024:65535 --sport 53 -j ACCEPT
  132. function_check save_firewall_settings
  133. save_firewall_settings
  134. mark_completed $FUNCNAME
  135. }
  136. function configure_firewall_for_web_access {
  137. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  138. return
  139. fi
  140. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  141. # docker does its own firewalling
  142. return
  143. fi
  144. if [[ $ONION_ONLY != "no" ]]; then
  145. return
  146. fi
  147. firewall_remove 80 tcp
  148. firewall_remove 443 tcp
  149. mark_completed $FUNCNAME
  150. }
  151. function configure_firewall_for_web_server {
  152. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  153. return
  154. fi
  155. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  156. # docker does its own firewalling
  157. return
  158. fi
  159. if [[ $ONION_ONLY != "no" ]]; then
  160. return
  161. fi
  162. firewall_add HTTP 80 tcp
  163. firewall_add HTTPS 443 tcp
  164. mark_completed $FUNCNAME
  165. }
  166. function configure_firewall_for_ssh {
  167. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  168. return
  169. fi
  170. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  171. # docker does its own firewalling
  172. return
  173. fi
  174. firewall_add SSH 22 tcp
  175. firewall_add SSH ${SSH_PORT} tcp
  176. mark_completed $FUNCNAME
  177. }
  178. function configure_firewall_for_git {
  179. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  180. return
  181. fi
  182. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  183. # docker does its own firewalling
  184. return
  185. fi
  186. if [[ $ONION_ONLY != "no" ]]; then
  187. return
  188. fi
  189. firewall_add Git 9418 tcp
  190. mark_completed $FUNCNAME
  191. }
  192. function configure_internet_protocol {
  193. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  194. return
  195. fi
  196. if [[ $SYSTEM_TYPE == "mesh"* ]]; then
  197. return
  198. fi
  199. sed -i "s/#net.ipv4.tcp_syncookies=1/net.ipv4.tcp_syncookies=1/g" /etc/sysctl.conf
  200. sed -i "s/#net.ipv4.conf.all.accept_redirects = 0/net.ipv4.conf.all.accept_redirects = 0/g" /etc/sysctl.conf
  201. sed -i "s/#net.ipv6.conf.all.accept_redirects = 0/net.ipv6.conf.all.accept_redirects = 0/g" /etc/sysctl.conf
  202. sed -i "s/#net.ipv4.conf.all.send_redirects = 0/net.ipv4.conf.all.send_redirects = 0/g" /etc/sysctl.conf
  203. sed -i "s/#net.ipv4.conf.all.accept_source_route = 0/net.ipv4.conf.all.accept_source_route = 0/g" /etc/sysctl.conf
  204. sed -i "s/#net.ipv6.conf.all.accept_source_route = 0/net.ipv6.conf.all.accept_source_route = 0/g" /etc/sysctl.conf
  205. sed -i "s/#net.ipv4.conf.default.rp_filter=1/net.ipv4.conf.default.rp_filter=1/g" /etc/sysctl.conf
  206. sed -i "s/#net.ipv4.conf.all.rp_filter=1/net.ipv4.conf.all.rp_filter=1/g" /etc/sysctl.conf
  207. sed -i "s/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=0/g" /etc/sysctl.conf
  208. sed -i "s/#net.ipv6.conf.all.forwarding=1/net.ipv6.conf.all.forwarding=0/g" /etc/sysctl.conf
  209. if ! grep -q "ignore pings" /etc/sysctl.conf; then
  210. echo '# ignore pings' >> /etc/sysctl.conf
  211. echo 'net.ipv4.icmp_echo_ignore_all = 1' >> /etc/sysctl.conf
  212. echo 'net.ipv6.icmp_echo_ignore_all = 1' >> /etc/sysctl.conf
  213. fi
  214. if ! grep -q "disable ipv6" /etc/sysctl.conf; then
  215. echo '# disable ipv6' >> /etc/sysctl.conf
  216. echo 'net.ipv6.conf.all.disable_ipv6 = 1' >> /etc/sysctl.conf
  217. fi
  218. if ! grep -q "net.ipv4.tcp_synack_retries" /etc/sysctl.conf; then
  219. echo 'net.ipv4.tcp_synack_retries = 2' >> /etc/sysctl.conf
  220. echo 'net.ipv4.tcp_syn_retries = 1' >> /etc/sysctl.conf
  221. fi
  222. if ! grep -q "keepalive" /etc/sysctl.conf; then
  223. echo '# keepalive' >> /etc/sysctl.conf
  224. echo 'net.ipv4.tcp_keepalive_probes = 9' >> /etc/sysctl.conf
  225. echo 'net.ipv4.tcp_keepalive_intvl = 75' >> /etc/sysctl.conf
  226. echo 'net.ipv4.tcp_keepalive_time = 7200' >> /etc/sysctl.conf
  227. fi
  228. mark_completed $FUNCNAME
  229. }
  230. function mesh_firewall {
  231. FIREWALL_FILENAME=${rootdir}/etc/systemd/system/meshfirewall.service
  232. MESH_FIREWALL_SCRIPT=${rootdir}/usr/bin/mesh-firewall
  233. echo '#!/bin/bash' > $MESH_FIREWALL_SCRIPT
  234. echo 'iptables -P INPUT ACCEPT' >> $MESH_FIREWALL_SCRIPT
  235. echo 'ip6tables -P INPUT ACCEPT' >> $MESH_FIREWALL_SCRIPT
  236. echo 'iptables -F' >> $MESH_FIREWALL_SCRIPT
  237. echo 'ip6tables -F' >> $MESH_FIREWALL_SCRIPT
  238. echo 'iptables -t nat -F' >> $MESH_FIREWALL_SCRIPT
  239. echo 'ip6tables -t nat -F' >> $MESH_FIREWALL_SCRIPT
  240. echo 'iptables -X' >> $MESH_FIREWALL_SCRIPT
  241. echo 'ip6tables -X' >> $MESH_FIREWALL_SCRIPT
  242. echo 'iptables -P INPUT DROP' >> $MESH_FIREWALL_SCRIPT
  243. echo 'ip6tables -P INPUT DROP' >> $MESH_FIREWALL_SCRIPT
  244. echo 'iptables -A INPUT -i lo -j ACCEPT' >> $MESH_FIREWALL_SCRIPT
  245. echo 'iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT' >> $MESH_FIREWALL_SCRIPT
  246. echo '' >> $MESH_FIREWALL_SCRIPT
  247. echo '# Make sure incoming tcp connections are SYN packets' >> $MESH_FIREWALL_SCRIPT
  248. echo 'iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP' >> $MESH_FIREWALL_SCRIPT
  249. echo '' >> $MESH_FIREWALL_SCRIPT
  250. echo '# Drop packets with incoming fragments' >> $MESH_FIREWALL_SCRIPT
  251. echo 'iptables -A INPUT -f -j DROP' >> $MESH_FIREWALL_SCRIPT
  252. echo '' >> $MESH_FIREWALL_SCRIPT
  253. echo '# Drop bogons' >> $MESH_FIREWALL_SCRIPT
  254. echo 'iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP' >> $MESH_FIREWALL_SCRIPT
  255. echo 'iptables -A INPUT -p tcp --tcp-flags ALL FIN,PSH,URG -j DROP' >> $MESH_FIREWALL_SCRIPT
  256. echo 'iptables -A INPUT -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP' >> $MESH_FIREWALL_SCRIPT
  257. echo '' >> $MESH_FIREWALL_SCRIPT
  258. echo '# Incoming malformed NULL packets:' >> $MESH_FIREWALL_SCRIPT
  259. echo 'iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP' >> $MESH_FIREWALL_SCRIPT
  260. echo '' >> $MESH_FIREWALL_SCRIPT
  261. echo "iptables -A INPUT -p tcp --dport $TOX_PORT -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  262. echo "iptables -A INPUT -i $WIFI_INTERFACE -p udp --dport $ZERONET_PORT -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  263. echo "iptables -A INPUT -i $WIFI_INTERFACE -p tcp --dport $ZERONET_PORT -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  264. echo "iptables -A INPUT -i $WIFI_INTERFACE -p udp --dport $TRACKER_PORT -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  265. echo "iptables -A INPUT -i $WIFI_INTERFACE -p tcp --dport $TRACKER_PORT -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  266. echo "iptables -A INPUT -i $WIFI_INTERFACE -p udp --dport 1900 -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  267. chmod +x $MESH_FIREWALL_SCRIPT
  268. echo '[Unit]' > $FIREWALL_FILENAME
  269. echo 'Description=Mesh Firewall' >> $FIREWALL_FILENAME
  270. echo '' >> $FIREWALL_FILENAME
  271. echo '[Service]' >> $FIREWALL_FILENAME
  272. echo 'Type=oneshot' >> $FIREWALL_FILENAME
  273. echo 'ExecStart=/usr/bin/mesh-firewall' >> $FIREWALL_FILENAME
  274. echo 'RemainAfterExit=no' >> $FIREWALL_FILENAME
  275. echo '' >> $FIREWALL_FILENAME
  276. echo 'TimeoutSec=30' >> $FIREWALL_FILENAME
  277. echo '' >> $FIREWALL_FILENAME
  278. echo '[Install]' >> $FIREWALL_FILENAME
  279. echo 'WantedBy=multi-user.target' >> $FIREWALL_FILENAME
  280. chroot "$rootdir" systemctl enable meshfirewall
  281. }
  282. function firewall_add {
  283. firewall_name=$(echo "$1" | sed "s| |-|g")
  284. firewall_port=$2
  285. firewall_protocol="$3"
  286. if ! grep -q "${firewall_name}=${firewall_port}" $FIREWALL_CONFIG; then
  287. echo "${firewall_name}=${firewall_port}" >> $FIREWALL_CONFIG
  288. if [ ! ${firewall_protocol} ]; then
  289. iptables -A INPUT -p udp --dport ${firewall_port} -j ACCEPT
  290. iptables -A INPUT -p tcp --dport ${firewall_port} -j ACCEPT
  291. else
  292. if [[ "${firewall_protocol}" == *"udp"* ]]; then
  293. iptables -A INPUT -p udp --dport ${firewall_port} -j ACCEPT
  294. fi
  295. if [[ "${firewall_protocol}" == *"tcp"* ]]; then
  296. iptables -A INPUT -p tcp --dport ${firewall_port} -j ACCEPT
  297. fi
  298. fi
  299. save_firewall_settings
  300. fi
  301. }
  302. function firewall_remove {
  303. firewall_port=$1
  304. firewall_protocol="$2"
  305. if [ ! -f $FIREWALL_CONFIG ]; then
  306. return
  307. fi
  308. if grep -q "=${firewall_port}" $FIREWALL_CONFIG; then
  309. if [ ! ${firewall_protocol} ]; then
  310. iptables -D INPUT -p udp --dport ${firewall_port} -j ACCEPT
  311. iptables -D INPUT -p tcp --dport ${firewall_port} -j ACCEPT
  312. else
  313. if [[ "${firewall_protocol}" == *"udp"* ]]; then
  314. iptables -D INPUT -p udp --dport ${firewall_port} -j ACCEPT
  315. fi
  316. if [[ "${firewall_protocol}" == *"tcp"* ]]; then
  317. iptables -D INPUT -p tcp --dport ${firewall_port} -j ACCEPT
  318. fi
  319. fi
  320. sed -i "/=${firewall_port}/d" $FIREWALL_CONFIG
  321. save_firewall_settings
  322. fi
  323. }
  324. # NOTE: deliberately no exit 0