nRnYqfrb5pJuTP5YAohj

IP Tables with TUN VPN

May 30th, 2019
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.64 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # $IPT script : 0.2
  4.  
  5. IPT="/sbin/iptables"
  6. TUN0_IP=$(ifconfig tun0 | awk '/t addr:/{gsub(/.*:/,"",$2);print$2}')
  7.  
  8. # Flush old rules, old custom tables
  9. echo "Stopping firewall and allowing everyone.."
  10. $IPT -F
  11. $IPT -X
  12. $IPT -t nat -F
  13. $IPT -t nat -X
  14. $IPT -t mangle -F
  15. $IPT -t mangle -X
  16. $IPT -P INPUT ACCEPT
  17. $IPT -P FORWARD ACCEPT
  18. $IPT -P OUTPUT ACCEPT
  19.  
  20. # set default policy to drop
  21. echo "Setting default policies.."
  22. $IPT -P INPUT DROP
  23. $IPT -P OUTPUT DROP
  24. $IPT -P FORWARD DROP
  25.  
  26. # allow loopback
  27. echo "Allow loopback.."
  28. $IPT -A INPUT -i lo -j ACCEPT
  29. $IPT -A OUTPUT -o lo -j ACCEPT
  30.  
  31. # https://arashmilani.com/post?id=53
  32. # allow VPN to tun0 etc.
  33. echo "Allow VPN to TUN0 on $TUN0_IP."
  34. $IPT -A INPUT -i tun+ -j ACCEPT
  35. $IPT -A FORWARD -i tun+ -j ACCEPT
  36. $IPT -A FORWARD -i tun+ -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
  37. $IPT -A FORWARD -i eth0 -o tun+ -m state --state RELATED,ESTABLISHED -j ACCEPT
  38. $IPT -t nat -A POSTROUTING -s $TUN0_IP/24 -o eth0 -j MASQUERADE
  39. $IPT -A OUTPUT -o tun+ -j ACCEPT
  40.  
  41. # drop invalid packets
  42. echo "Drop invalid packets.."
  43. $IPT -A INPUT  -m state --state INVALID -j DROP
  44. $IPT -A OUTPUT -m state --state INVALID -j DROP
  45. $IPT -A FORWARD -m state --state INVALID -j DROP
  46.  
  47. # allow established, related packets we've already seen
  48. echo "Allow established connections.."
  49. $IPT -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  50. $IPT -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  51.  
  52. # input chain
  53. echo "Allow inbound port 22.."
  54. $IPT -A INPUT -p tcp --dport 22 -m state --state NEW -s 0.0.0.0/0 -m comment --comment "SSH" -j ACCEPT
  55. $IPT -A INPUT -s 192.168.1.0/24 -m state --state NEW -p udp --dport 123 -m comment --comment "NTP" -j ACCEPT
  56. $IPT -A INPUT -p tcp --dport 6881:6999 -m comment --comment "BITTORRENT" -j ACCEPT
  57.  
  58. # output chain
  59. echo "Permit the following on the output chain.."
  60. $IPT -A OUTPUT -p tcp -m tcp --dport 21 -m comment --comment "FTP" -j ACCEPT
  61. $IPT -A OUTPUT -p tcp -m tcp --dport 22 -m comment --comment "SSH" -j ACCEPT
  62. $IPT -A OUTPUT -p tcp -m tcp --dport 53 -m comment --comment "DNS-TCP" -j ACCEPT
  63. $IPT -A OUTPUT -p udp -m udp --dport 53 -m comment --comment "DNS-UDP" -j ACCEPT
  64. $IPT -A OUTPUT -p udp -m udp --dport 67:68 -m comment --comment "DHCP" -j ACCEPT
  65. $IPT -A OUTPUT -p tcp -m tcp --dport 80 -m comment --comment "HTTP" -j ACCEPT
  66. $IPT -A OUTPUT -p udp -m udp --dport 123 -m comment --comment "NTP" -j ACCEPT
  67. $IPT -A OUTPUT -p tcp -m tcp --dport 389 -m comment --comment "LDAP" -j ACCEPT
  68. $IPT -A OUTPUT -p tcp -m tcp --dport 443 -m comment --comment "HTTPS" -j ACCEPT
  69. $IPT -A OUTPUT -p tcp -m tcp --dport 465 -m comment --comment "SMTPS" -j ACCEPT
  70. $IPT -A OUTPUT -p tcp -m tcp --dport 587 -m comment --comment "SMTPS" -j ACCEPT
  71. $IPT -A OUTPUT -p tcp -m tcp --dport 636 -m comment --comment "LDAPS" -j ACCEPT
  72. $IPT -A OUTPUT -p tcp -m tcp --dport 993 -m comment --comment "IMAPS" -j ACCEPT
  73. $IPT -A OUTPUT -p tcp --source-port 6881:6999 -m comment --comment "BITTORRENT" -j ACCEPT
  74. $IPT -A OUTPUT -p tcp -m tcp --dport 8080 -m comment --comment "HTTP-PROXY" -j ACCEPT
  75. $IPT -A OUTPUT -p tcp -m tcp --dport 9000 -m comment --comment "HTTP-PORTAINER" -j ACCEPT
  76. $IPT -A OUTPUT -p tcp -m tcp --dport 9100 -m comment --comment "PRINTING" -j ACCEPT
  77.  
  78. # allow icmp packets (e.g. ping...)
  79. echo "Allow ICMP.."
  80. $IPT -A INPUT -p icmp -m state --state NEW -j ACCEPT
  81. $IPT -A OUTPUT -p icmp -m state --state NEW -j ACCEPT
  82.  
  83. # Enable logging
  84. echo "Allow logging of dropped packets.."
  85. $IPT -N LOGGING
  86. $IPT -A INPUT -j LOGGING
  87. $IPT -A OUTPUT -j LOGGING
  88. $IPT -A LOGGING -m limit --limit 2/min -j LOG --log-prefix "IPTables-Dropped: " --log-level 4
  89. $IPT -A LOGGING -j DROP
  90.  
  91. echo "Finished."
Advertisement
Add Comment
Please, Sign In to add comment