freedombone-utils-firewall 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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-2017 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. FIREWALL_DOMAINS=$HOME/${PROJECT_NAME}-firewall-domains.cfg
  34. FIREWALL_EIFACE=eth0
  35. EXTERNAL_IPV4_ADDRESS=
  36. function save_firewall_settings {
  37. iptables-save > /etc/firewall.conf
  38. ip6tables-save > /etc/firewall6.conf
  39. printf '#!/bin/sh\n' > /etc/network/if-up.d/iptables
  40. printf 'iptables-restore < /etc/firewall.conf\n' >> /etc/network/if-up.d/iptables
  41. printf 'ip6tables-restore < /etc/firewall6.conf\n' >> /etc/network/if-up.d/iptables
  42. if [ -f /etc/network/if-up.d/iptables ]; then
  43. chmod +x /etc/network/if-up.d/iptables
  44. fi
  45. }
  46. function firewall_block_bad_ip_ranges {
  47. if [ $INSTALLING_MESH ]; then
  48. return
  49. fi
  50. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  51. return
  52. fi
  53. # There are various blocklists out there, but they're difficult
  54. # to verify. Indiscriminately blocking ranges without evidence
  55. # would be a bad idea.
  56. # From Wikipedia and elsewhere: US military addresses
  57. iptables -A INPUT -s 6.0.0.0/8 -j DROP
  58. iptables -A OUTPUT -s 6.0.0.0/8 -j DROP
  59. iptables -A INPUT -s 7.0.0.0/8 -j DROP
  60. iptables -A OUTPUT -s 7.0.0.0/8 -j DROP
  61. iptables -A INPUT -s 11.0.0.0/8 -j DROP
  62. iptables -A OUTPUT -s 11.0.0.0/8 -j DROP
  63. iptables -A INPUT -s 21.0.0.0/8 -j DROP
  64. iptables -A OUTPUT -s 21.0.0.0/8 -j DROP
  65. iptables -A INPUT -s 22.0.0.0/8 -j DROP
  66. iptables -A OUTPUT -s 22.0.0.0/8 -j DROP
  67. iptables -A INPUT -s 26.0.0.0/8 -j DROP
  68. iptables -A OUTPUT -s 26.0.0.0/8 -j DROP
  69. iptables -A INPUT -s 28.0.0.0/8 -j DROP
  70. iptables -A OUTPUT -s 28.0.0.0/8 -j DROP
  71. iptables -A INPUT -s 29.0.0.0/8 -j DROP
  72. iptables -A OUTPUT -s 29.0.0.0/8 -j DROP
  73. iptables -A INPUT -s 30.0.0.0/8 -j DROP
  74. iptables -A OUTPUT -s 30.0.0.0/8 -j DROP
  75. iptables -A INPUT -s 33.0.0.0/8 -j DROP
  76. iptables -A OUTPUT -s 33.0.0.0/8 -j DROP
  77. iptables -A INPUT -s 55.0.0.0/8 -j DROP
  78. iptables -A OUTPUT -s 55.0.0.0/8 -j DROP
  79. iptables -A INPUT -s 214.0.0.0/8 -j DROP
  80. iptables -A OUTPUT -s 214.0.0.0/8 -j DROP
  81. iptables -A INPUT -s 215.0.0.0/8 -j DROP
  82. iptables -A OUTPUT -s 215.0.0.0/8 -j DROP
  83. save_firewall_settings
  84. mark_completed $FUNCNAME
  85. }
  86. function global_rate_limit {
  87. if ! grep -q "tcp_challenge_ack_limit" /etc/sysctl.conf; then
  88. echo 'net.ipv4.tcp_challenge_ack_limit = 999999999' >> /etc/sysctl.conf
  89. sysctl -p -q
  90. else
  91. if ! grep -q "net.ipv4.tcp_challenge_ack_limit = 999999999" /etc/sysctl.conf; then
  92. sed -i 's|net.ipv4.tcp_challenge_ack_limit.*|net.ipv4.tcp_challenge_ack_limit = 999999999|g' /etc/sysctl.conf
  93. sysctl -p -q
  94. fi
  95. fi
  96. }
  97. function enable_ipv6 {
  98. # endure that ipv6 is enabled and can route
  99. sed -i 's/net.ipv6.conf.all.disable_ipv6.*/net.ipv6.conf.all.disable_ipv6 = 0/g' /etc/sysctl.conf
  100. #sed -i "s/net.ipv6.conf.all.accept_redirects.*/net.ipv6.conf.all.accept_redirects = 1/g" /etc/sysctl.conf
  101. #sed -i "s/net.ipv6.conf.all.accept_source_route.*/net.ipv6.conf.all.accept_source_route = 1/g" /etc/sysctl.conf
  102. sed -i "s/net.ipv6.conf.all.forwarding.*/net.ipv6.conf.all.forwarding=1/g" /etc/sysctl.conf
  103. echo 1 > /proc/sys/net/ipv6/conf/all/forwarding
  104. }
  105. function firewall_disable_vpn {
  106. firewall_remove VPN 1194
  107. iptables -D INPUT -i ${FIREWALL_EIFACE} -m state --state NEW -p tcp --dport 1194 -j ACCEPT
  108. iptables -D INPUT -i tun+ -j ACCEPT
  109. iptables -D FORWARD -i tun+ -j ACCEPT
  110. iptables -D FORWARD -i tun+ -o ${FIREWALL_EIFACE} -m state --state RELATED,ESTABLISHED -j ACCEPT
  111. iptables -D FORWARD -i ${FIREWALL_EIFACE} -o tun+ -m state --state RELATED,ESTABLISHED -j ACCEPT
  112. iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -o ${FIREWALL_EIFACE} -j MASQUERADE
  113. iptables -D OUTPUT -o tun+ -j ACCEPT
  114. save_firewall_settings
  115. }
  116. function firewall_enable_vpn {
  117. firewall_add VPN 1194 tcp
  118. iptables -A INPUT -i ${FIREWALL_EIFACE} -m state --state NEW -p tcp --dport 1194 -j ACCEPT
  119. iptables -A INPUT -i tun+ -j ACCEPT
  120. iptables -A FORWARD -i tun+ -j ACCEPT
  121. iptables -A FORWARD -i tun+ -o ${FIREWALL_EIFACE} -m state --state RELATED,ESTABLISHED -j ACCEPT
  122. iptables -A FORWARD -i ${FIREWALL_EIFACE} -o tun+ -m state --state RELATED,ESTABLISHED -j ACCEPT
  123. iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o ${FIREWALL_EIFACE} -j MASQUERADE
  124. iptables -A OUTPUT -o tun+ -j ACCEPT
  125. save_firewall_settings
  126. }
  127. function configure_firewall {
  128. if [ $INSTALLING_MESH ]; then
  129. mesh_firewall
  130. return
  131. fi
  132. if grep -q "RELATED" /etc/firewall.conf; then
  133. # recreate the firewall to remove RELATED
  134. sed -i "/firewall/d" $COMPLETION_FILE
  135. fi
  136. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  137. return
  138. fi
  139. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  140. # docker does its own firewalling
  141. return
  142. fi
  143. iptables -P INPUT ACCEPT
  144. ip6tables -P INPUT ACCEPT
  145. iptables -F
  146. ip6tables -F
  147. iptables -t nat -F
  148. ip6tables -t nat -F
  149. iptables -X
  150. ip6tables -X
  151. iptables -P INPUT DROP
  152. ip6tables -P INPUT DROP
  153. iptables -P FORWARD DROP
  154. ip6tables -P FORWARD DROP
  155. iptables -A INPUT -i lo -j ACCEPT
  156. iptables -A INPUT -m conntrack --ctstate ESTABLISHED -j ACCEPT
  157. # Drop invalid packets
  158. iptables -t mangle -A PREROUTING -m conntrack --ctstate INVALID -j DROP
  159. # Make sure incoming tcp connections are SYN packets
  160. iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP
  161. iptables -t mangle -A PREROUTING -p tcp ! --syn -m conntrack --ctstate NEW -j DROP
  162. # Drop SYN packets with suspicious MSS value
  163. iptables -t mangle -A PREROUTING -p tcp -m conntrack --ctstate NEW -m tcpmss ! --mss 536:65535 -j DROP
  164. # Drop packets with incoming fragments
  165. iptables -A INPUT -f -j DROP
  166. # Drop bogons
  167. iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
  168. iptables -A INPUT -p tcp --tcp-flags ALL FIN,PSH,URG -j DROP
  169. iptables -A INPUT -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP
  170. iptables -t mangle -A PREROUTING -p tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j DROP
  171. iptables -t mangle -A PREROUTING -p tcp --tcp-flags FIN,SYN FIN,SYN -j DROP
  172. iptables -t mangle -A PREROUTING -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
  173. iptables -t mangle -A PREROUTING -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
  174. iptables -t mangle -A PREROUTING -p tcp --tcp-flags FIN,RST FIN,RST -j DROP
  175. iptables -t mangle -A PREROUTING -p tcp --tcp-flags FIN,ACK FIN -j DROP
  176. iptables -t mangle -A PREROUTING -p tcp --tcp-flags ACK,URG URG -j DROP
  177. iptables -t mangle -A PREROUTING -p tcp --tcp-flags ACK,FIN FIN -j DROP
  178. iptables -t mangle -A PREROUTING -p tcp --tcp-flags ACK,PSH PSH -j DROP
  179. iptables -t mangle -A PREROUTING -p tcp --tcp-flags ALL ALL -j DROP
  180. iptables -t mangle -A PREROUTING -p tcp --tcp-flags ALL NONE -j DROP
  181. iptables -t mangle -A PREROUTING -p tcp --tcp-flags ALL FIN,PSH,URG -j DROP
  182. iptables -t mangle -A PREROUTING -p tcp --tcp-flags ALL SYN,FIN,PSH,URG -j DROP
  183. iptables -t mangle -A PREROUTING -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP
  184. # Incoming malformed NULL packets:
  185. iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
  186. mark_completed $FUNCNAME
  187. }
  188. function firewall_drop_telnet {
  189. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  190. return
  191. fi
  192. # telnet isn't enabled as an input and we can also
  193. # drop any outgoing telnet, just in case
  194. iptables -A OUTPUT -p tcp --dport telnet -j REJECT
  195. iptables -A OUTPUT -p udp --dport telnet -j REJECT
  196. function_check save_firewall_settings
  197. save_firewall_settings
  198. mark_completed $FUNCNAME
  199. }
  200. function configure_firewall_ping {
  201. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  202. return
  203. fi
  204. # Only allow ping for mesh installs
  205. if [[ $SYSTEM_TYPE != "mesh"* ]]; then
  206. return
  207. fi
  208. iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
  209. iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT
  210. function_check save_firewall_settings
  211. save_firewall_settings
  212. mark_completed $FUNCNAME
  213. }
  214. function configure_internet_protocol {
  215. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  216. return
  217. fi
  218. if [[ $SYSTEM_TYPE == "mesh"* ]]; then
  219. return
  220. fi
  221. sed -i "s/#net.ipv4.tcp_syncookies.*/net.ipv4.tcp_syncookies=1/g" /etc/sysctl.conf
  222. sed -i "s/#net.ipv4.conf.all.accept_redirects.*/net.ipv4.conf.all.accept_redirects = 0/g" /etc/sysctl.conf
  223. sed -i "s/#net.ipv6.conf.all.accept_redirects.*/net.ipv6.conf.all.accept_redirects = 0/g" /etc/sysctl.conf
  224. sed -i "s/#net.ipv4.conf.all.send_redirects.*/net.ipv4.conf.all.send_redirects = 0/g" /etc/sysctl.conf
  225. sed -i "s/#net.ipv4.conf.all.accept_source_route.*/net.ipv4.conf.all.accept_source_route = 0/g" /etc/sysctl.conf
  226. sed -i "s/#net.ipv6.conf.all.accept_source_route.*/net.ipv6.conf.all.accept_source_route = 0/g" /etc/sysctl.conf
  227. sed -i "s/#net.ipv4.conf.default.rp_filter.*/net.ipv4.conf.default.rp_filter=1/g" /etc/sysctl.conf
  228. sed -i "s/#net.ipv4.conf.all.rp_filter.*/net.ipv4.conf.all.rp_filter=1/g" /etc/sysctl.conf
  229. sed -i "s/#net.ipv4.ip_forward.*/net.ipv4.ip_forward=0/g" /etc/sysctl.conf
  230. sed -i "s/#net.ipv6.conf.all.forwarding.*/net.ipv6.conf.all.forwarding=0/g" /etc/sysctl.conf
  231. sed -i "s/# net.ipv4.tcp_syncookies.*/net.ipv4.tcp_syncookies=1/g" /etc/sysctl.conf
  232. sed -i "s/# net.ipv4.conf.all.accept_redirects.*/net.ipv4.conf.all.accept_redirects = 0/g" /etc/sysctl.conf
  233. sed -i "s/# net.ipv6.conf.all.accept_redirects.*/net.ipv6.conf.all.accept_redirects = 0/g" /etc/sysctl.conf
  234. sed -i "s/# net.ipv4.conf.all.send_redirects.*/net.ipv4.conf.all.send_redirects = 0/g" /etc/sysctl.conf
  235. sed -i "s/# net.ipv4.conf.all.accept_source_route.*/net.ipv4.conf.all.accept_source_route = 0/g" /etc/sysctl.conf
  236. sed -i "s/# net.ipv6.conf.all.accept_source_route.*/net.ipv6.conf.all.accept_source_route = 0/g" /etc/sysctl.conf
  237. sed -i "s/# net.ipv4.conf.default.rp_filter.*/net.ipv4.conf.default.rp_filter=1/g" /etc/sysctl.conf
  238. sed -i "s/# net.ipv4.conf.all.rp_filter.*/net.ipv4.conf.all.rp_filter=1/g" /etc/sysctl.conf
  239. sed -i "s/# net.ipv4.ip_forward.*/net.ipv4.ip_forward=0/g" /etc/sysctl.conf
  240. sed -i "s/# net.ipv6.conf.all.forwarding.*/net.ipv6.conf.all.forwarding=0/g" /etc/sysctl.conf
  241. if ! grep -q "ignore pings" /etc/sysctl.conf; then
  242. echo '# ignore pings' >> /etc/sysctl.conf
  243. echo 'net.ipv4.icmp_echo_ignore_all = 1' >> /etc/sysctl.conf
  244. echo 'net.ipv6.icmp_echo_ignore_all = 1' >> /etc/sysctl.conf
  245. fi
  246. if ! grep -q "disable ipv6" /etc/sysctl.conf; then
  247. echo '# disable ipv6' >> /etc/sysctl.conf
  248. echo 'net.ipv6.conf.all.disable_ipv6 = 1' >> /etc/sysctl.conf
  249. fi
  250. if ! grep -q "net.ipv4.tcp_synack_retries" /etc/sysctl.conf; then
  251. echo 'net.ipv4.tcp_synack_retries = 2' >> /etc/sysctl.conf
  252. echo 'net.ipv4.tcp_syn_retries = 1' >> /etc/sysctl.conf
  253. fi
  254. if ! grep -q "keepalive" /etc/sysctl.conf; then
  255. echo '# keepalive' >> /etc/sysctl.conf
  256. echo 'net.ipv4.tcp_keepalive_probes = 9' >> /etc/sysctl.conf
  257. echo 'net.ipv4.tcp_keepalive_intvl = 75' >> /etc/sysctl.conf
  258. echo 'net.ipv4.tcp_keepalive_time = 7200' >> /etc/sysctl.conf
  259. fi
  260. if ! grep -q "net.ipv4.conf.default.send_redirects" /etc/sysctl.conf; then
  261. echo "net.ipv4.conf.default.send_redirects = 0" >> /etc/sysctl.conf
  262. else
  263. sed -i "s|# net.ipv4.conf.default.send_redirects.*|net.ipv4.conf.default.send_redirects = 0|g" /etc/sysctl.conf
  264. sed -i "s|#net.ipv4.conf.default.send_redirects.*|net.ipv4.conf.default.send_redirects = 0|g" /etc/sysctl.conf
  265. sed -i "s|net.ipv4.conf.default.send_redirects.*|net.ipv4.conf.default.send_redirects = 0|g" /etc/sysctl.conf
  266. fi
  267. if ! grep -q "net.ipv4.conf.all.secure_redirects" /etc/sysctl.conf; then
  268. echo "net.ipv4.conf.all.secure_redirects = 0" >> /etc/sysctl.conf
  269. else
  270. sed -i "s|# net.ipv4.conf.all.secure_redirects.*|net.ipv4.conf.all.secure_redirects = 0|g" /etc/sysctl.conf
  271. sed -i "s|#net.ipv4.conf.all.secure_redirects.*|net.ipv4.conf.all.secure_redirects = 0|g" /etc/sysctl.conf
  272. sed -i "s|net.ipv4.conf.all.secure_redirects.*|net.ipv4.conf.all.secure_redirects = 0|g" /etc/sysctl.conf
  273. fi
  274. if ! grep -q "net.ipv4.conf.default.accept_source_route" /etc/sysctl.conf; then
  275. echo "net.ipv4.conf.default.accept_source_route = 0" >> /etc/sysctl.conf
  276. else
  277. sed -i "s|# net.ipv4.conf.default.accept_source_route.*|net.ipv4.conf.default.accept_source_route = 0|g" /etc/sysctl.conf
  278. sed -i "s|#net.ipv4.conf.default.accept_source_route.*|net.ipv4.conf.default.accept_source_route = 0|g" /etc/sysctl.conf
  279. sed -i "s|net.ipv4.conf.default.accept_source_route.*|net.ipv4.conf.default.accept_source_route = 0|g" /etc/sysctl.conf
  280. fi
  281. if ! grep -q "net.ipv4.conf.default.secure_redirects" /etc/sysctl.conf; then
  282. echo "net.ipv4.conf.default.secure_redirects = 0" >> /etc/sysctl.conf
  283. else
  284. sed -i "s|# net.ipv4.conf.default.secure_redirects.*|net.ipv4.conf.default.secure_redirects = 0|g" /etc/sysctl.conf
  285. sed -i "s|#net.ipv4.conf.default.secure_redirects.*|net.ipv4.conf.default.secure_redirects = 0|g" /etc/sysctl.conf
  286. sed -i "s|net.ipv4.conf.default.secure_redirects.*|net.ipv4.conf.default.secure_redirects = 0|g" /etc/sysctl.conf
  287. fi
  288. if ! grep -q "net.ipv4.conf.default.accept_redirects" /etc/sysctl.conf; then
  289. echo "net.ipv4.conf.default.accept_redirects = 0" >> /etc/sysctl.conf
  290. else
  291. sed -i "s|# net.ipv4.conf.default.accept_redirects.*|net.ipv4.conf.default.accept_redirects = 0|g" /etc/sysctl.conf
  292. sed -i "s|#net.ipv4.conf.default.accept_redirects.*|net.ipv4.conf.default.accept_redirects = 0|g" /etc/sysctl.conf
  293. sed -i "s|net.ipv4.conf.default.accept_redirects.*|net.ipv4.conf.default.accept_redirects = 0|g" /etc/sysctl.conf
  294. fi
  295. # Randomize kernel
  296. if ! grep -q "kernel.randomize_va_space" /etc/sysctl.conf; then
  297. echo "kernel.randomize_va_space=2" >> /etc/sysctl.conf
  298. else
  299. sed -i 's|kernel.randomize_va_space.*|kernel.randomize_va_space=2|g' /etc/sysctl.conf
  300. fi
  301. # Turn off the tcp_timestamps
  302. if ! grep -q "net.ipv4.tcp_timestamps" /etc/sysctl.conf; then
  303. echo "net.ipv4.tcp_timestamps=0" >> /etc/sysctl.conf
  304. else
  305. sed -i 's|net.ipv4.tcp_timestamps.*|net.ipv4.tcp_timestamps=0|g' /etc/sysctl.conf
  306. fi
  307. /sbin/sysctl -p
  308. mark_completed $FUNCNAME
  309. }
  310. function mesh_firewall {
  311. FIREWALL_FILENAME=${rootdir}/etc/systemd/system/meshfirewall.service
  312. MESH_FIREWALL_SCRIPT=${rootdir}/usr/bin/mesh-firewall
  313. echo '#!/bin/bash' > $MESH_FIREWALL_SCRIPT
  314. echo 'iptables -P INPUT ACCEPT' >> $MESH_FIREWALL_SCRIPT
  315. echo 'ip6tables -P INPUT ACCEPT' >> $MESH_FIREWALL_SCRIPT
  316. echo 'iptables -F' >> $MESH_FIREWALL_SCRIPT
  317. echo 'ip6tables -F' >> $MESH_FIREWALL_SCRIPT
  318. echo 'iptables -t nat -F' >> $MESH_FIREWALL_SCRIPT
  319. echo 'ip6tables -t nat -F' >> $MESH_FIREWALL_SCRIPT
  320. echo 'iptables -X' >> $MESH_FIREWALL_SCRIPT
  321. echo 'ip6tables -X' >> $MESH_FIREWALL_SCRIPT
  322. echo 'iptables -P INPUT DROP' >> $MESH_FIREWALL_SCRIPT
  323. echo 'ip6tables -P INPUT DROP' >> $MESH_FIREWALL_SCRIPT
  324. echo 'iptables -A INPUT -i lo -j ACCEPT' >> $MESH_FIREWALL_SCRIPT
  325. echo 'iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT' >> $MESH_FIREWALL_SCRIPT
  326. echo '' >> $MESH_FIREWALL_SCRIPT
  327. echo '# Make sure incoming tcp connections are SYN packets' >> $MESH_FIREWALL_SCRIPT
  328. echo 'iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP' >> $MESH_FIREWALL_SCRIPT
  329. echo '' >> $MESH_FIREWALL_SCRIPT
  330. echo '# Drop packets with incoming fragments' >> $MESH_FIREWALL_SCRIPT
  331. echo 'iptables -A INPUT -f -j DROP' >> $MESH_FIREWALL_SCRIPT
  332. echo '' >> $MESH_FIREWALL_SCRIPT
  333. echo '# Drop bogons' >> $MESH_FIREWALL_SCRIPT
  334. echo 'iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP' >> $MESH_FIREWALL_SCRIPT
  335. echo 'iptables -A INPUT -p tcp --tcp-flags ALL FIN,PSH,URG -j DROP' >> $MESH_FIREWALL_SCRIPT
  336. echo 'iptables -A INPUT -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP' >> $MESH_FIREWALL_SCRIPT
  337. echo '' >> $MESH_FIREWALL_SCRIPT
  338. echo '# Incoming malformed NULL packets:' >> $MESH_FIREWALL_SCRIPT
  339. echo 'iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP' >> $MESH_FIREWALL_SCRIPT
  340. echo '' >> $MESH_FIREWALL_SCRIPT
  341. echo "iptables -A INPUT -p tcp --dport $TOX_PORT -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  342. echo "iptables -A INPUT -i $WIFI_INTERFACE -p udp --dport $ZERONET_PORT -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  343. echo "iptables -A INPUT -i $WIFI_INTERFACE -p tcp --dport $ZERONET_PORT -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  344. echo "iptables -A INPUT -i $WIFI_INTERFACE -p udp --dport $TRACKER_PORT -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  345. echo "iptables -A INPUT -i $WIFI_INTERFACE -p tcp --dport $TRACKER_PORT -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  346. echo "iptables -A INPUT -i $WIFI_INTERFACE -p udp --dport 1900 -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  347. chmod +x $MESH_FIREWALL_SCRIPT
  348. echo '[Unit]' > $FIREWALL_FILENAME
  349. echo 'Description=Mesh Firewall' >> $FIREWALL_FILENAME
  350. echo '' >> $FIREWALL_FILENAME
  351. echo '[Service]' >> $FIREWALL_FILENAME
  352. echo 'Type=oneshot' >> $FIREWALL_FILENAME
  353. echo 'ExecStart=/usr/bin/mesh-firewall' >> $FIREWALL_FILENAME
  354. echo 'RemainAfterExit=no' >> $FIREWALL_FILENAME
  355. echo '' >> $FIREWALL_FILENAME
  356. echo 'TimeoutSec=30' >> $FIREWALL_FILENAME
  357. echo '' >> $FIREWALL_FILENAME
  358. echo '[Install]' >> $FIREWALL_FILENAME
  359. echo 'WantedBy=multi-user.target' >> $FIREWALL_FILENAME
  360. chmod +x $FIREWALL_FILENAME
  361. chroot "$rootdir" systemctl enable meshfirewall
  362. }
  363. function firewall_add {
  364. firewall_name=$(echo "$1" | sed "s| |-|g")
  365. firewall_port=$2
  366. firewall_protocol="$3"
  367. if ! grep -q "${firewall_name}=${firewall_port}" $FIREWALL_CONFIG; then
  368. echo "${firewall_name}=${firewall_port}" >> $FIREWALL_CONFIG
  369. if [ ! ${firewall_protocol} ]; then
  370. iptables -C INPUT -p udp --dport ${firewall_port} -j ACCEPT
  371. if [ ! "$?" = "0" ]; then
  372. iptables -A INPUT -p udp --dport ${firewall_port} -j ACCEPT
  373. fi
  374. iptables -C INPUT -p tcp --dport ${firewall_port} -j ACCEPT
  375. if [ ! "$?" = "0" ]; then
  376. iptables -A INPUT -p tcp --dport ${firewall_port} -j ACCEPT
  377. fi
  378. else
  379. if [[ "${firewall_protocol}" == *"udp"* ]]; then
  380. iptables -C INPUT -p udp --dport ${firewall_port} -j ACCEPT
  381. if [ ! "$?" = "0" ]; then
  382. iptables -A INPUT -p udp --dport ${firewall_port} -j ACCEPT
  383. fi
  384. fi
  385. if [[ "${firewall_protocol}" == *"tcp"* ]]; then
  386. iptables -C INPUT -p tcp --dport ${firewall_port} -j ACCEPT
  387. if [ ! "$?" = "0" ]; then
  388. iptables -A INPUT -p tcp --dport ${firewall_port} -j ACCEPT
  389. fi
  390. fi
  391. fi
  392. save_firewall_settings
  393. fi
  394. }
  395. function firewall_add_range {
  396. firewall_name=$(echo "$1" | sed "s| |-|g")
  397. firewall_port_start=$2
  398. firewall_port_end=$3
  399. firewall_protocol="$4"
  400. if ! grep -q "${firewall_name}=${firewall_port_start}:${firewall_port_end}" $FIREWALL_CONFIG; then
  401. echo "${firewall_name}=${firewall_port_start}:${firewall_port_end}" >> $FIREWALL_CONFIG
  402. if [ ! ${firewall_protocol} ]; then
  403. iptables -C INPUT -p udp --dport ${firewall_port_start}:${firewall_port_end} -j ACCEPT
  404. if [ ! "$?" = "0" ]; then
  405. iptables -A INPUT -p udp --dport ${firewall_port_start}:${firewall_port_end} -j ACCEPT
  406. fi
  407. iptables -C INPUT -p tcp --dport ${firewall_port_start}:${firewall_port_end} -j ACCEPT
  408. if [ ! "$?" = "0" ]; then
  409. iptables -A INPUT -p tcp --dport ${firewall_port_start}:${firewall_port_end} -j ACCEPT
  410. fi
  411. else
  412. if [[ "${firewall_protocol}" == *"udp"* ]]; then
  413. iptables -C INPUT -p udp --dport ${firewall_port_start}:${firewall_port_end} -j ACCEPT
  414. if [ ! "$?" = "0" ]; then
  415. iptables -A INPUT -p udp --dport ${firewall_port_start}:${firewall_port_end} -j ACCEPT
  416. fi
  417. fi
  418. if [[ "${firewall_protocol}" == *"tcp"* ]]; then
  419. iptables -C INPUT -p tcp --dport ${firewall_port_start}:${firewall_port_end} -j ACCEPT
  420. if [ ! "$?" = "0" ]; then
  421. iptables -A INPUT -p tcp --dport ${firewall_port_start}:${firewall_port_end} -j ACCEPT
  422. fi
  423. fi
  424. fi
  425. save_firewall_settings
  426. fi
  427. }
  428. function firewall_remove {
  429. firewall_port=$1
  430. firewall_protocol="$2"
  431. if [ ! -f $FIREWALL_CONFIG ]; then
  432. return
  433. fi
  434. if grep -q "=${firewall_port}" $FIREWALL_CONFIG; then
  435. if [ ! ${firewall_protocol} ]; then
  436. iptables -D INPUT -p udp --dport ${firewall_port} -j ACCEPT
  437. iptables -D INPUT -p tcp --dport ${firewall_port} -j ACCEPT
  438. else
  439. if [[ "${firewall_protocol}" == *"udp"* ]]; then
  440. iptables -D INPUT -p udp --dport ${firewall_port} -j ACCEPT
  441. fi
  442. if [[ "${firewall_protocol}" == *"tcp"* ]]; then
  443. iptables -D INPUT -p tcp --dport ${firewall_port} -j ACCEPT
  444. fi
  445. fi
  446. sed -i "/=${firewall_port}/d" $FIREWALL_CONFIG
  447. save_firewall_settings
  448. fi
  449. }
  450. function domain_to_hex_string {
  451. domain="$1"
  452. ctr=1
  453. segment=$(echo "$domain" | awk -F '.' -v value="$ctr" '{print $value}')
  454. while [ ${#segment} -gt 0 ]
  455. do
  456. characters=$(echo -n "$segment" | wc -c)
  457. hexnum=$(echo "obase=16; $characters" | bc)
  458. echo -n "|"
  459. if [ $(echo -n "$hexnum" | wc -c) -lt 2 ]; then
  460. echo -n "0"
  461. fi
  462. echo -n "$hexnum|$segment"
  463. ctr=$((ctr + 1))
  464. segment=$(echo "$domain" | awk -F '.' -v value="$ctr" '{print $value}')
  465. done
  466. echo ""
  467. }
  468. function firewall_block_domain {
  469. blocked_domain="$1"
  470. if [[ "$blocked_domain" == *'@'* ]]; then
  471. # Don't try to block email/microblog addresses
  472. echo "${blocked_domain}" >> $FIREWALL_DOMAINS
  473. return
  474. fi
  475. if ! grep -q "$blocked_domain" $FIREWALL_DOMAINS; then
  476. hexstr=$(domain_to_hex_string $blocked_domain)
  477. iptables -C INPUT -p udp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
  478. if [ ! "$?" = "0" ]; then
  479. iptables -A INPUT -p udp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
  480. iptables -A INPUT -p tcp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
  481. iptables -A OUTPUT -p udp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
  482. iptables -A OUTPUT -p tcp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
  483. iptables -I FORWARD -p udp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
  484. iptables -I FORWARD -p tcp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
  485. echo "${blocked_domain}" >> $FIREWALL_DOMAINS
  486. save_firewall_settings
  487. fi
  488. # run the blocking rules now
  489. if [ -f /usr/bin/gnusocial-firewall ]; then
  490. /usr/bin/gnusocial-firewall
  491. fi
  492. if [ -f /usr/bin/postactiv-firewall ]; then
  493. /usr/bin/postactiv-firewall
  494. fi
  495. fi
  496. }
  497. function firewall_block_ip {
  498. blocked_ip="$1"
  499. if [[ "$blocked_ip" == *'@'* ]]; then
  500. # Don't try to block email/microblog addresses
  501. return
  502. fi
  503. if ! grep -q "$blocked_ip" $FIREWALL_DOMAINS; then
  504. iptables -C INPUT -s $blocked_ip -j DROP
  505. if [ ! "$?" = "0" ]; then
  506. iptables -A INPUT -s $blocked_ip -j DROP
  507. iptables -A OUTPUT -s $blocked_ip -j DROP
  508. echo "${blocked_ip}" >> $FIREWALL_DOMAINS
  509. save_firewall_settings
  510. fi
  511. fi
  512. }
  513. function firewall_unblock_ip {
  514. blocked_ip="$1"
  515. if [[ "$blocked_ip" == *'@'* ]]; then
  516. # Don't try to block email/microblog addresses
  517. return
  518. fi
  519. if grep -q "$blocked_ip" $FIREWALL_DOMAINS; then
  520. iptables -D INPUT -s $blocked_ip -j DROP
  521. iptables -D OUTPUT -s $blocked_ip -j DROP
  522. sed -i '/$blocked_ip/d' $FIREWALL_DOMAINS
  523. echo "${blocked_ip}" >> $FIREWALL_DOMAINS
  524. save_firewall_settings
  525. fi
  526. }
  527. function firewall_refresh_blocklist {
  528. if [ ! -f /root/${PROJECT_NAME}-firewall-domains.cfg ]; then
  529. return
  530. fi
  531. while read blocked_domain; do
  532. firewall_block_domain $blocked_domain
  533. done </root/${PROJECT_NAME}-firewall-domains.cfg
  534. }
  535. function firewall_unblock_domain {
  536. unblocked_domain="$1"
  537. if grep -q "${unblocked_domain}" $FIREWALL_DOMAINS; then
  538. if [[ "${unblocked_domain}" != *'@'* ]]; then
  539. hexstr=$(domain_to_hex_string $unblocked_domain)
  540. iptables -D INPUT -p udp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
  541. iptables -D INPUT -p tcp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
  542. iptables -D OUTPUT -p udp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
  543. iptables -D OUTPUT -p tcp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
  544. iptables -D FORWARD -p udp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
  545. iptables -D FORWARD -p tcp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
  546. save_firewall_settings
  547. fi
  548. sed -i "/${unblocked_domain}/d" $FIREWALL_DOMAINS
  549. fi
  550. }
  551. function firewall_drop_spoofed_packets {
  552. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  553. return
  554. fi
  555. iptables -t mangle -A PREROUTING -s 224.0.0.0/3 -j DROP
  556. iptables -t mangle -A PREROUTING -s 169.254.0.0/16 -j DROP
  557. iptables -t mangle -A PREROUTING -s 172.16.0.0/12 -j DROP
  558. iptables -t mangle -A PREROUTING -s 192.0.2.0/24 -j DROP
  559. iptables -t mangle -A PREROUTING -s 10.0.0.0/8 -j DROP
  560. iptables -t mangle -A PREROUTING -s 240.0.0.0/5 -j DROP
  561. iptables -t mangle -A PREROUTING -s 127.0.0.0/8 ! -i lo -j DROP
  562. function_check save_firewall_settings
  563. save_firewall_settings
  564. mark_completed $FUNCNAME
  565. }
  566. function firewall_rate_limits {
  567. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  568. return
  569. fi
  570. # Limit connections per source IP
  571. iptables -A INPUT -p tcp -m connlimit --connlimit-above 111 -j REJECT --reject-with tcp-reset
  572. # Limit RST packets
  573. iptables -A INPUT -p tcp --tcp-flags RST RST -m limit --limit 2/s --limit-burst 2 -j ACCEPT
  574. iptables -A INPUT -p tcp --tcp-flags RST RST -j DROP
  575. # Limit new TCP connections per second per source IP
  576. iptables -A INPUT -p tcp -m conntrack --ctstate NEW -m limit --limit 60/s --limit-burst 20 -j ACCEPT
  577. iptables -A INPUT -p tcp -m conntrack --ctstate NEW -j DROP
  578. # SSH brute-force protection
  579. iptables -A INPUT -p tcp --dport ssh -m conntrack --ctstate NEW -m recent --set
  580. iptables -A INPUT -p tcp --dport ssh -m conntrack --ctstate NEW -m recent --update --seconds 60 --hitcount 10 -j DROP
  581. function_check save_firewall_settings
  582. save_firewall_settings
  583. mark_completed $FUNCNAME
  584. }
  585. # NOTE: deliberately no exit 0