freedombone-utils-firewall 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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-2018 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[0]}") == "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[0]}"
  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[0]}") == "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[0]}"
  187. }
  188. function firewall_drop_telnet {
  189. if [[ $(is_completed "${FUNCNAME[0]}") == "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[0]}"
  199. }
  200. function configure_firewall_ping {
  201. if [[ $(is_completed "${FUNCNAME[0]}") == "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[0]}"
  213. }
  214. function configure_internet_protocol {
  215. if [[ $(is_completed "${FUNCNAME[0]}") == "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';
  256. echo 'net.ipv4.tcp_keepalive_probes = 9';
  257. echo 'net.ipv4.tcp_keepalive_intvl = 75';
  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[0]}"
  309. }
  310. function mesh_firewall {
  311. # shellcheck disable=SC2154
  312. FIREWALL_FILENAME="${rootdir}/etc/systemd/system/meshfirewall.service"
  313. MESH_FIREWALL_SCRIPT=${rootdir}/usr/bin/mesh-firewall
  314. { echo '#!/bin/bash';
  315. echo 'iptables -P INPUT ACCEPT';
  316. echo 'ip6tables -P INPUT ACCEPT';
  317. echo 'iptables -F';
  318. echo 'ip6tables -F';
  319. echo 'iptables -t nat -F';
  320. echo 'ip6tables -t nat -F';
  321. echo 'iptables -X';
  322. echo 'ip6tables -X';
  323. echo 'iptables -P INPUT DROP';
  324. echo 'ip6tables -P INPUT DROP';
  325. echo 'iptables -A INPUT -i lo -j ACCEPT';
  326. echo 'ip6tables -A INPUT -i lo -j ACCEPT';
  327. echo 'iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT';
  328. echo 'ip6tables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT';
  329. echo '';
  330. echo '# Make sure incoming tcp connections are SYN packets';
  331. echo 'iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP';
  332. echo 'ip6tables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP';
  333. echo '';
  334. echo '# Drop packets with incoming fragments';
  335. echo 'iptables -A INPUT -f -j DROP';
  336. echo 'ip6tables -A INPUT -f -j DROP';
  337. echo '';
  338. echo '# Drop bogons';
  339. echo 'iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP';
  340. echo 'ip6tables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP';
  341. echo 'iptables -A INPUT -p tcp --tcp-flags ALL FIN,PSH,URG -j DROP';
  342. echo 'ip6tables -A INPUT -p tcp --tcp-flags ALL FIN,PSH,URG -j DROP';
  343. echo 'iptables -A INPUT -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP';
  344. echo 'ip6tables -A INPUT -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP';
  345. echo '';
  346. echo '# Incoming malformed NULL packets:';
  347. echo 'iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP';
  348. echo 'ip6tables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP';
  349. echo '';
  350. echo "iptables -A INPUT -p tcp --dport $TOX_PORT -j ACCEPT";
  351. echo "ip6tables -A INPUT -p tcp --dport $TOX_PORT -j ACCEPT";
  352. echo "iptables -A INPUT -p udp --dport $ZERONET_PORT -j ACCEPT";
  353. echo "ip6tables -A INPUT -p udp --dport $ZERONET_PORT -j ACCEPT";
  354. echo "iptables -A INPUT -p tcp --dport $ZERONET_PORT -j ACCEPT";
  355. echo "ip6tables -A INPUT -p tcp --dport $ZERONET_PORT -j ACCEPT";
  356. echo "iptables -A INPUT -p udp --dport $TRACKER_PORT -j ACCEPT";
  357. echo "ip6tables -A INPUT -p udp --dport $TRACKER_PORT -j ACCEPT";
  358. echo "iptables -A INPUT -p tcp --dport $TRACKER_PORT -j ACCEPT";
  359. echo "ip6tables -A INPUT -p tcp --dport $TRACKER_PORT -j ACCEPT";
  360. echo "iptables -A INPUT -p udp --dport 1900 -j ACCEPT";
  361. echo "ip6tables -A INPUT -p udp --dport 1900 -j ACCEPT"; } > "$MESH_FIREWALL_SCRIPT"
  362. chmod +x "$MESH_FIREWALL_SCRIPT"
  363. { echo '[Unit]';
  364. echo 'Description=Mesh Firewall';
  365. echo '';
  366. echo '[Service]';
  367. echo 'Type=oneshot';
  368. echo 'ExecStart=/usr/bin/mesh-firewall';
  369. echo 'RemainAfterExit=no';
  370. echo '';
  371. echo 'TimeoutSec=30';
  372. echo '';
  373. echo '[Install]';
  374. echo 'WantedBy=multi-user.target'; } > "$FIREWALL_FILENAME"
  375. chmod +x "$FIREWALL_FILENAME"
  376. chroot "$rootdir" systemctl enable meshfirewall
  377. }
  378. function firewall_add {
  379. firewall_name=$(string="$1" ; echo "${string// /-}")
  380. firewall_port=$2
  381. firewall_protocol="$3"
  382. if ! grep -q "${firewall_name}=${firewall_port}" "$FIREWALL_CONFIG"; then
  383. echo "${firewall_name}=${firewall_port}" >> "$FIREWALL_CONFIG"
  384. if [ ! "${firewall_protocol}" ]; then
  385. if ! iptables -C INPUT -p udp --dport "${firewall_port}" -j ACCEPT; then
  386. iptables -A INPUT -p udp --dport "${firewall_port}" -j ACCEPT
  387. fi
  388. if ! iptables -C INPUT -p tcp --dport "${firewall_port}" -j ACCEPT; then
  389. iptables -A INPUT -p tcp --dport "${firewall_port}" -j ACCEPT
  390. fi
  391. else
  392. if [[ "${firewall_protocol}" == *"udp"* ]]; then
  393. if ! iptables -C INPUT -p udp --dport "${firewall_port}" -j ACCEPT; then
  394. iptables -A INPUT -p udp --dport "${firewall_port}" -j ACCEPT
  395. fi
  396. fi
  397. if [[ "${firewall_protocol}" == *"tcp"* ]]; then
  398. if ! iptables -C INPUT -p tcp --dport "${firewall_port}" -j ACCEPT; then
  399. iptables -A INPUT -p tcp --dport "${firewall_port}" -j ACCEPT
  400. fi
  401. fi
  402. fi
  403. save_firewall_settings
  404. fi
  405. }
  406. function firewall_add_range {
  407. firewall_name=$(string="$1" ; echo "${string// /-}")
  408. firewall_port_start=$2
  409. firewall_port_end=$3
  410. firewall_protocol="$4"
  411. if ! grep -q "${firewall_name}=${firewall_port_start}:${firewall_port_end}" "$FIREWALL_CONFIG"; then
  412. echo "${firewall_name}=${firewall_port_start}:${firewall_port_end}" >> "$FIREWALL_CONFIG"
  413. if [ ! "${firewall_protocol}" ]; then
  414. if ! iptables -C INPUT -p udp --dport "${firewall_port_start}":"${firewall_port_end}" -j ACCEPT; then
  415. iptables -A INPUT -p udp --dport "${firewall_port_start}":"${firewall_port_end}" -j ACCEPT
  416. fi
  417. if ! iptables -C INPUT -p tcp --dport "${firewall_port_start}":"${firewall_port_end}" -j ACCEPT; then
  418. iptables -A INPUT -p tcp --dport "${firewall_port_start}":"${firewall_port_end}" -j ACCEPT
  419. fi
  420. else
  421. if [[ "${firewall_protocol}" == *"udp"* ]]; then
  422. if ! iptables -C INPUT -p udp --dport "${firewall_port_start}":"${firewall_port_end}" -j ACCEPT; then
  423. iptables -A INPUT -p udp --dport "${firewall_port_start}":"${firewall_port_end}" -j ACCEPT
  424. fi
  425. fi
  426. if [[ "${firewall_protocol}" == *"tcp"* ]]; then
  427. if ! iptables -C INPUT -p tcp --dport "${firewall_port_start}":"${firewall_port_end}" -j ACCEPT; then
  428. iptables -A INPUT -p tcp --dport "${firewall_port_start}":"${firewall_port_end}" -j ACCEPT
  429. fi
  430. fi
  431. fi
  432. save_firewall_settings
  433. fi
  434. }
  435. function firewall_remove {
  436. firewall_port=$1
  437. firewall_protocol="$2"
  438. if [ ! -f "$FIREWALL_CONFIG" ]; then
  439. return
  440. fi
  441. if grep -q "=${firewall_port}" "$FIREWALL_CONFIG"; then
  442. if [ ! "${firewall_protocol}" ]; then
  443. iptables -D INPUT -p udp --dport "${firewall_port}" -j ACCEPT
  444. iptables -D INPUT -p tcp --dport "${firewall_port}" -j ACCEPT
  445. else
  446. if [[ "${firewall_protocol}" == *"udp"* ]]; then
  447. iptables -D INPUT -p udp --dport "${firewall_port}" -j ACCEPT
  448. fi
  449. if [[ "${firewall_protocol}" == *"tcp"* ]]; then
  450. iptables -D INPUT -p tcp --dport "${firewall_port}" -j ACCEPT
  451. fi
  452. fi
  453. sed -i "/=${firewall_port}/d" "$FIREWALL_CONFIG"
  454. save_firewall_settings
  455. fi
  456. }
  457. function domain_to_hex_string {
  458. domain="$1"
  459. ctr=1
  460. segment=$(echo "$domain" | awk -F '.' -v value="$ctr" '{print $value}')
  461. while [ ${#segment} -gt 0 ]
  462. do
  463. characters=$(echo -n "$segment" | wc -c)
  464. hexnum=$(echo "obase=16; $characters" | bc)
  465. echo -n "|"
  466. if [ "$(echo -n "$hexnum" | wc -c)" -lt 2 ]; then
  467. echo -n "0"
  468. fi
  469. echo -n "$hexnum|$segment"
  470. ctr=$((ctr + 1))
  471. segment=$(echo "$domain" | awk -F '.' -v value="$ctr" '{print $value}')
  472. done
  473. echo ""
  474. }
  475. function firewall_block_domain {
  476. blocked_domain="$1"
  477. if [[ "$blocked_domain" == *'@'* ]]; then
  478. # Don't try to block email/microblog addresses
  479. echo "${blocked_domain}" >> "$FIREWALL_DOMAINS"
  480. return
  481. fi
  482. if ! grep -q "$blocked_domain" "$FIREWALL_DOMAINS"; then
  483. hexstr=$(domain_to_hex_string "$blocked_domain")
  484. if ! iptables -C INPUT -p udp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP; then
  485. iptables -A INPUT -p udp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
  486. iptables -A INPUT -p tcp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
  487. iptables -A OUTPUT -p udp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
  488. iptables -A OUTPUT -p tcp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
  489. iptables -I FORWARD -p udp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
  490. iptables -I FORWARD -p tcp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
  491. echo "${blocked_domain}" >> "$FIREWALL_DOMAINS"
  492. save_firewall_settings
  493. fi
  494. # run the blocking rules now
  495. if [ -f /usr/bin/gnusocial-firewall ]; then
  496. /usr/bin/gnusocial-firewall
  497. fi
  498. if [ -f /usr/bin/postactiv-firewall ]; then
  499. /usr/bin/postactiv-firewall
  500. fi
  501. if [ -f /usr/bin/pleroma-blocking ]; then
  502. /usr/bin/pleroma-blocking
  503. fi
  504. fi
  505. }
  506. function firewall_block_ip {
  507. blocked_ip="$1"
  508. if [[ "$blocked_ip" == *'@'* ]]; then
  509. # Don't try to block email/microblog addresses
  510. return
  511. fi
  512. if ! grep -q "$blocked_ip" "$FIREWALL_DOMAINS"; then
  513. if ! iptables -C INPUT -s "$blocked_ip" -j DROP; then
  514. iptables -A INPUT -s "$blocked_ip" -j DROP
  515. iptables -A OUTPUT -s "$blocked_ip" -j DROP
  516. echo "${blocked_ip}" >> "$FIREWALL_DOMAINS"
  517. save_firewall_settings
  518. fi
  519. fi
  520. }
  521. function firewall_unblock_ip {
  522. blocked_ip="$1"
  523. if [[ "$blocked_ip" == *'@'* ]]; then
  524. # Don't try to block email/microblog addresses
  525. return
  526. fi
  527. if grep -q "$blocked_ip" "$FIREWALL_DOMAINS"; then
  528. iptables -D INPUT -s "$blocked_ip" -j DROP
  529. iptables -D OUTPUT -s "$blocked_ip" -j DROP
  530. sed -i "/$blocked_ip/d" "$FIREWALL_DOMAINS"
  531. echo "${blocked_ip}" >> "$FIREWALL_DOMAINS"
  532. save_firewall_settings
  533. fi
  534. }
  535. function firewall_refresh_blocklist {
  536. if [ ! -f "/root/${PROJECT_NAME}-firewall-domains.cfg" ]; then
  537. return
  538. fi
  539. while read -r blocked_domain; do
  540. firewall_block_domain "$blocked_domain"
  541. done <"/root/${PROJECT_NAME}-firewall-domains.cfg"
  542. }
  543. function firewall_unblock_domain {
  544. unblocked_domain="$1"
  545. if grep -q "${unblocked_domain}" "$FIREWALL_DOMAINS"; then
  546. if [[ "${unblocked_domain}" != *'@'* ]]; then
  547. hexstr=$(domain_to_hex_string "$unblocked_domain")
  548. iptables -D INPUT -p udp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
  549. iptables -D INPUT -p tcp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
  550. iptables -D OUTPUT -p udp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
  551. iptables -D OUTPUT -p tcp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
  552. iptables -D FORWARD -p udp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
  553. iptables -D FORWARD -p tcp --dport 53 -m string --hex-string "$hexstr" --algo bm -j DROP
  554. save_firewall_settings
  555. fi
  556. sed -i "/${unblocked_domain}/d" "$FIREWALL_DOMAINS"
  557. fi
  558. }
  559. function firewall_drop_spoofed_packets {
  560. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  561. return
  562. fi
  563. iptables -t mangle -A PREROUTING -s 224.0.0.0/3 -j DROP
  564. iptables -t mangle -A PREROUTING -s 169.254.0.0/16 -j DROP
  565. iptables -t mangle -A PREROUTING -s 172.16.0.0/12 -j DROP
  566. iptables -t mangle -A PREROUTING -s 192.0.2.0/24 -j DROP
  567. iptables -t mangle -A PREROUTING -s 10.0.0.0/8 -j DROP
  568. iptables -t mangle -A PREROUTING -s 240.0.0.0/5 -j DROP
  569. iptables -t mangle -A PREROUTING -s 127.0.0.0/8 ! -i lo -j DROP
  570. function_check save_firewall_settings
  571. save_firewall_settings
  572. mark_completed "${FUNCNAME[0]}"
  573. }
  574. function firewall_rate_limits {
  575. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  576. return
  577. fi
  578. # Limit connections per source IP
  579. iptables -A INPUT -p tcp -m connlimit --connlimit-above 111 -j REJECT --reject-with tcp-reset
  580. # Limit RST packets
  581. iptables -A INPUT -p tcp --tcp-flags RST RST -m limit --limit 2/s --limit-burst 2 -j ACCEPT
  582. iptables -A INPUT -p tcp --tcp-flags RST RST -j DROP
  583. # Limit new TCP connections per second per source IP
  584. iptables -A INPUT -p tcp -m conntrack --ctstate NEW -m limit --limit 60/s --limit-burst 20 -j ACCEPT
  585. iptables -A INPUT -p tcp -m conntrack --ctstate NEW -j DROP
  586. # SSH brute-force protection
  587. iptables -A INPUT -p tcp --dport ssh -m conntrack --ctstate NEW -m recent --set
  588. iptables -A INPUT -p tcp --dport ssh -m conntrack --ctstate NEW -m recent --update --seconds 60 --hitcount 10 -j DROP
  589. function_check save_firewall_settings
  590. save_firewall_settings
  591. mark_completed "${FUNCNAME[0]}"
  592. }
  593. # NOTE: deliberately no exit 0