firewall.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/bin/sh
  2. # A Sample OpenVPN-aware firewall.
  3. # eth0 is connected to the internet.
  4. # eth1 is connected to a private subnet.
  5. # Change this subnet to correspond to your private
  6. # ethernet subnet. Home will use HOME_NET/24 and
  7. # Office will use OFFICE_NET/24.
  8. PRIVATE=10.0.0.0/24
  9. # Loopback address
  10. LOOP=127.0.0.1
  11. # Delete old iptables rules
  12. # and temporarily block all traffic.
  13. iptables -P OUTPUT DROP
  14. iptables -P INPUT DROP
  15. iptables -P FORWARD DROP
  16. iptables -F
  17. # Set default policies
  18. iptables -P OUTPUT ACCEPT
  19. iptables -P INPUT DROP
  20. iptables -P FORWARD DROP
  21. # Prevent external packets from using loopback addr
  22. iptables -A INPUT -i eth0 -s $LOOP -j DROP
  23. iptables -A FORWARD -i eth0 -s $LOOP -j DROP
  24. iptables -A INPUT -i eth0 -d $LOOP -j DROP
  25. iptables -A FORWARD -i eth0 -d $LOOP -j DROP
  26. # Anything coming from the Internet should have a real Internet address
  27. iptables -A FORWARD -i eth0 -s 192.168.0.0/16 -j DROP
  28. iptables -A FORWARD -i eth0 -s 172.16.0.0/12 -j DROP
  29. iptables -A FORWARD -i eth0 -s 10.0.0.0/8 -j DROP
  30. iptables -A INPUT -i eth0 -s 192.168.0.0/16 -j DROP
  31. iptables -A INPUT -i eth0 -s 172.16.0.0/12 -j DROP
  32. iptables -A INPUT -i eth0 -s 10.0.0.0/8 -j DROP
  33. # Block outgoing NetBios (if you have windows machines running
  34. # on the private subnet). This will not affect any NetBios
  35. # traffic that flows over the VPN tunnel, but it will stop
  36. # local windows machines from broadcasting themselves to
  37. # the internet.
  38. iptables -A FORWARD -p tcp --sport 137:139 -o eth0 -j DROP
  39. iptables -A FORWARD -p udp --sport 137:139 -o eth0 -j DROP
  40. iptables -A OUTPUT -p tcp --sport 137:139 -o eth0 -j DROP
  41. iptables -A OUTPUT -p udp --sport 137:139 -o eth0 -j DROP
  42. # Check source address validity on packets going out to internet
  43. iptables -A FORWARD -s ! $PRIVATE -i eth1 -j DROP
  44. # Allow local loopback
  45. iptables -A INPUT -s $LOOP -j ACCEPT
  46. iptables -A INPUT -d $LOOP -j ACCEPT
  47. # Allow incoming pings (can be disabled)
  48. iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
  49. # Allow services such as www and ssh (can be disabled)
  50. iptables -A INPUT -p tcp --dport http -j ACCEPT
  51. iptables -A INPUT -p tcp --dport ssh -j ACCEPT
  52. # Allow incoming OpenVPN packets
  53. # Duplicate the line below for each
  54. # OpenVPN tunnel, changing --dport n
  55. # to match the OpenVPN UDP port.
  56. #
  57. # In OpenVPN, the port number is
  58. # controlled by the --port n option.
  59. # If you put this option in the config
  60. # file, you can remove the leading '--'
  61. #
  62. # If you taking the stateful firewall
  63. # approach (see the OpenVPN HOWTO),
  64. # then comment out the line below.
  65. iptables -A INPUT -p udp --dport 1194 -j ACCEPT
  66. # Allow packets from TUN/TAP devices.
  67. # When OpenVPN is run in a secure mode,
  68. # it will authenticate packets prior
  69. # to their arriving on a tun or tap
  70. # interface. Therefore, it is not
  71. # necessary to add any filters here,
  72. # unless you want to restrict the
  73. # type of packets which can flow over
  74. # the tunnel.
  75. iptables -A INPUT -i tun+ -j ACCEPT
  76. iptables -A FORWARD -i tun+ -j ACCEPT
  77. iptables -A INPUT -i tap+ -j ACCEPT
  78. iptables -A FORWARD -i tap+ -j ACCEPT
  79. # Allow packets from private subnets
  80. iptables -A INPUT -i eth1 -j ACCEPT
  81. iptables -A FORWARD -i eth1 -j ACCEPT
  82. # Keep state of connections from local machine and private subnets
  83. iptables -A OUTPUT -m state --state NEW -o eth0 -j ACCEPT
  84. iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  85. iptables -A FORWARD -m state --state NEW -o eth0 -j ACCEPT
  86. iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
  87. # Masquerade local subnet
  88. iptables -t nat -A POSTROUTING -s $PRIVATE -o eth0 -j MASQUERADE