freedombone-utils-firewall 28KB

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