hook_post-iptable-rules 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/bin/bash
  2. host6=$(dig AAAA +short <TPL:SERVER_NAME> | tail -n1)
  3. host4=$(dig A +short <TPL:SERVER_NAME> | tail -n1)
  4. # IPv6
  5. sudo ip6tables -w -N vpnclient_in
  6. sudo ip6tables -w -N vpnclient_out
  7. sudo ip6tables -w -N vpnclient_fwd
  8. sudo ip6tables -w -A vpnclient_in -p icmpv6 -j ACCEPT
  9. sudo ip6tables -w -A vpnclient_in -s fd00::/8,fe80::/10 -j ACCEPT
  10. sudo ip6tables -w -A vpnclient_in -p tcp --dport 22 -j ACCEPT
  11. sudo ip6tables -w -A vpnclient_in -p tcp --dport 443 -j ACCEPT
  12. sudo ip6tables -w -A vpnclient_in -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
  13. sudo ip6tables -w -A vpnclient_in -j DROP
  14. if [ ! -z "${host6}" ]; then
  15. sudo ip6tables -w -A vpnclient_out -d ${host6} -p <TPL:PROTO> --dport <TPL:SERVER_PORT> -j ACCEPT
  16. fi
  17. for i in <TPL:DNS0> <TPL:DNS1>; do
  18. if [[ "${i}" =~ : ]]; then
  19. sudo ip6tables -w -A vpnclient_out -p udp -d "${i}" --dport 53 -j ACCEPT
  20. fi
  21. done
  22. sudo ip6tables -w -A vpnclient_out -d fd00::/8,fe80::/10 -j ACCEPT
  23. sudo ip6tables -w -A vpnclient_out -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
  24. sudo ip6tables -w -A vpnclient_out -j DROP
  25. sudo ip6tables -w -A vpnclient_fwd -j DROP
  26. sudo ip6tables -w -I INPUT 1 -i <TPL:WIRED_DEVICE> -j vpnclient_in
  27. sudo ip6tables -w -I OUTPUT 1 -o <TPL:WIRED_DEVICE> -j vpnclient_out
  28. sudo ip6tables -w -I FORWARD 1 -o <TPL:WIRED_DEVICE> -j vpnclient_fwd
  29. # IPv4
  30. sudo iptables -w -N vpnclient_in
  31. sudo iptables -w -N vpnclient_out
  32. sudo iptables -w -N vpnclient_fwd
  33. sudo iptables -w -A vpnclient_in -p icmp -j ACCEPT
  34. sudo 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
  35. sudo iptables -w -A vpnclient_in -p tcp --dport 22 -j ACCEPT
  36. sudo iptables -w -A vpnclient_in -p tcp --dport 443 -j ACCEPT
  37. sudo iptables -w -A vpnclient_in -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
  38. sudo iptables -w -A vpnclient_in -j DROP
  39. if [ ! -z "${host4}" ]; then
  40. sudo iptables -w -A vpnclient_out -d ${host4} -p <TPL:PROTO> --dport <TPL:SERVER_PORT> -j ACCEPT
  41. fi
  42. for i in <TPL:DNS0> <TPL:DNS1>; do
  43. if [[ "${i}" =~ \. ]]; then
  44. sudo iptables -w -A vpnclient_out -p udp -d "${i}" --dport 53 -j ACCEPT
  45. fi
  46. done
  47. sudo 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
  48. sudo iptables -w -A vpnclient_out -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
  49. sudo iptables -w -A vpnclient_out -j DROP
  50. sudo iptables -w -A vpnclient_fwd -j DROP
  51. sudo iptables -w -I INPUT 1 -i <TPL:WIRED_DEVICE> -j vpnclient_in
  52. sudo iptables -w -I OUTPUT 1 -o <TPL:WIRED_DEVICE> -j vpnclient_out
  53. sudo iptables -w -I FORWARD 1 -o <TPL:WIRED_DEVICE> -j vpnclient_fwd
  54. exit 0