freedombone-utils-firewall 29KB

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