freedombone-utils-firewall 30KB

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