hook_post-iptable-rules 3.0 KB

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