hook_post-iptable-rules 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/bin/bash
  2. if [[ -z "${trusted_ip}" && -z "${trusted_ip6}" ]]; then
  3. server_names=$(grep -o -P '^\s*remote\s+\K([^\s]+)' /etc/openvpn/client.conf | sort | uniq)
  4. # In case an ip has been provided in ovpn conf
  5. host4=""
  6. host6=""
  7. for i in ${server_names}; do
  8. if [[ "${i}" =~ : ]]; then
  9. host6+=" ${i}"
  10. elif [[ "${i}" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
  11. host4+=" ${i}"
  12. else
  13. host6+=" $(dig AAAA +short "${i}" @127.0.0.1 | grep -v '\.$' | grep -v "timed out")"
  14. host4+=" $(dig A +short "${i}" @127.0.0.1 | grep -v '\.$' | grep -v "timed out")"
  15. fi
  16. done
  17. else
  18. if [[ -n "${trusted_ip6}" ]]; then
  19. host6=${trusted_ip6}
  20. fi
  21. if [[ -n "${trusted_ip}" ]]; then
  22. host4=${trusted_ip}
  23. fi
  24. fi
  25. wired_device=$(ip route | awk '/default via/ { print $5; }')
  26. dns=$(grep -o -P '^\s*nameserver\s+\K[a-fA-F\d.:]+$' /etc/resolv.dnsmasq.conf | sort | uniq)
  27. # IPv6
  28. ip6tables -w -N vpnclient_in
  29. ip6tables -w -N vpnclient_out
  30. ip6tables -w -N vpnclient_fwd
  31. ip6tables -w -A vpnclient_in -p icmpv6 -j ACCEPT
  32. ip6tables -w -A vpnclient_in -s fd00::/8,fe80::/10 -j ACCEPT
  33. ip6tables -w -A vpnclient_in -p tcp --dport 22 -j ACCEPT
  34. ip6tables -w -A vpnclient_in -p tcp --dport 443 -j ACCEPT
  35. ip6tables -w -A vpnclient_in -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
  36. ip6tables -w -A vpnclient_in -j DROP
  37. if [[ ! -z "${host6}" ]]; then
  38. for i in ${host6}; do
  39. ip6tables -w -A vpnclient_out -d "${i}" -j ACCEPT
  40. done
  41. fi
  42. for i in ${dns}; do
  43. if [[ "${i}" =~ : ]]; then
  44. ip6tables -w -A vpnclient_out -p udp -d "${i}" --dport 53 -j ACCEPT
  45. fi
  46. done
  47. ip6tables -w -A vpnclient_out -d fd00::/8,fe80::/10 -j ACCEPT
  48. ip6tables -w -A vpnclient_out -p icmpv6 -j ACCEPT
  49. ip6tables -w -A vpnclient_out -p udp --dport 5353 -d ff02::fb -j ACCEPT
  50. ip6tables -w -A vpnclient_out -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
  51. ip6tables -w -A vpnclient_out -j DROP
  52. ip6tables -w -A vpnclient_fwd -j DROP
  53. ip6tables -w -I INPUT 1 -i $wired_device -j vpnclient_in
  54. ip6tables -w -I OUTPUT 1 -o $wired_device -j vpnclient_out
  55. ip6tables -w -I FORWARD 1 -o $wired_device -j vpnclient_fwd
  56. # IPv4
  57. iptables -w -N vpnclient_in
  58. iptables -w -N vpnclient_out
  59. iptables -w -N vpnclient_fwd
  60. iptables -w -A vpnclient_in -p icmp -j ACCEPT
  61. iptables -w -A vpnclient_in -s 10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,169.254.0.0/16 -j ACCEPT
  62. iptables -w -A vpnclient_in -p tcp --dport 22 -j ACCEPT
  63. iptables -w -A vpnclient_in -p tcp --dport 443 -j ACCEPT
  64. iptables -w -A vpnclient_in -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
  65. iptables -w -A vpnclient_in -j DROP
  66. if [[ ! -z "${host4}" ]]; then
  67. for i in ${host4}; do
  68. iptables -w -A vpnclient_out -d "${i}" -j ACCEPT
  69. done
  70. fi
  71. for i in ${dns}; do
  72. if [[ "${i}" =~ \. ]]; then
  73. iptables -w -A vpnclient_out -p udp -d "${i}" --dport 53 -j ACCEPT
  74. fi
  75. done
  76. iptables -w -A vpnclient_out -d 10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,169.254.0.0/16 -j ACCEPT
  77. iptables -w -A vpnclient_out -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT
  78. iptables -w -A vpnclient_out -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
  79. iptables -w -A vpnclient_out -j DROP
  80. iptables -w -A vpnclient_fwd -j DROP
  81. iptables -w -I INPUT 1 -i $wired_device -j vpnclient_in
  82. iptables -w -I OUTPUT 1 -o $wired_device -j vpnclient_out
  83. iptables -w -I FORWARD 1 -o $wired_device -j vpnclient_fwd
  84. exit 0