freedombone-utils-firewall 26KB

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