| 1 | #!/bin/sh
|
|---|
| 2 |
|
|---|
| 3 | echo "please configure your interfaces and iptables rules"
|
|---|
| 4 | exit 1
|
|---|
| 5 | ### Interfaces
|
|---|
| 6 | WAN=eth0.1
|
|---|
| 7 | LAN=eth0.0
|
|---|
| 8 | WLAN=wlan0
|
|---|
| 9 |
|
|---|
| 10 | ######################################################################
|
|---|
| 11 | ### Default ruleset
|
|---|
| 12 | ######################################################################
|
|---|
| 13 |
|
|---|
| 14 | ### Create chains
|
|---|
| 15 | iptables -N input_rule
|
|---|
| 16 | iptables -N forwarding_rule
|
|---|
| 17 | iptables -t nat -N prerouting_rule
|
|---|
| 18 | iptables -t nat -N postrouting_rule
|
|---|
| 19 |
|
|---|
| 20 | ### Default policy
|
|---|
| 21 | iptables -P INPUT DROP
|
|---|
| 22 | iptables -P FORWARD DROP
|
|---|
| 23 |
|
|---|
| 24 | ### INPUT
|
|---|
| 25 | ### (connections with the router as destination)
|
|---|
| 26 |
|
|---|
| 27 | # base case
|
|---|
| 28 | iptables -A INPUT -m conntrack --ctstate INVALID -j DROP
|
|---|
| 29 | iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
|
|---|
| 30 | iptables -A INPUT -p tcp --tcp-flags SYN SYN \! --tcp-option 2 -j DROP
|
|---|
| 31 |
|
|---|
| 32 | # custom rules
|
|---|
| 33 | iptables -A INPUT -j input_rule
|
|---|
| 34 |
|
|---|
| 35 | # allow access from anything but WAN
|
|---|
| 36 | iptables -A INPUT ${WAN:+\! -i $WAN} -j ACCEPT
|
|---|
| 37 | # allow icmp messages
|
|---|
| 38 | iptables -A INPUT -p icmp -j ACCEPT
|
|---|
| 39 |
|
|---|
| 40 | # reject
|
|---|
| 41 | iptables -A INPUT -p tcp -j REJECT --reject-with tcp-reset
|
|---|
| 42 | iptables -A INPUT -j REJECT --reject-with icmp-port-unreachable
|
|---|
| 43 |
|
|---|
| 44 | ### OUTPUT
|
|---|
| 45 | ### (connections with the router as source)
|
|---|
| 46 |
|
|---|
| 47 | # base case
|
|---|
| 48 | iptables -A OUTPUT -m conntrack --ctstate INVALID -j DROP
|
|---|
| 49 | iptables -A OUTPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
|
|---|
| 50 |
|
|---|
| 51 | ### FORWARD
|
|---|
| 52 | ### (connections routed through the router)
|
|---|
| 53 |
|
|---|
| 54 | # base case
|
|---|
| 55 | iptables -A FORWARD -m conntrack --ctstate INVALID -j DROP
|
|---|
| 56 | iptables -A FORWARD -p tcp -o $WAN --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
|
|---|
| 57 | iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
|
|---|
| 58 |
|
|---|
| 59 | # custom rules
|
|---|
| 60 | iptables -A FORWARD -j forwarding_rule
|
|---|
| 61 | iptables -t nat -A PREROUTING -j prerouting_rule
|
|---|
| 62 | iptables -t nat -A POSTROUTING -j postrouting_rule
|
|---|
| 63 |
|
|---|
| 64 | # allow LAN to INTERNET
|
|---|
| 65 | iptables -A FORWARD -i $LAN -o $WAN -j ACCEPT
|
|---|
| 66 | # allow WLAN to INTERNET
|
|---|
| 67 | #iptables -A FORWARD -i $WLAN -o $WAN -j ACCEPT
|
|---|
| 68 | # Allow WLAN to LAN
|
|---|
| 69 | #iptables -A FORWARD -i $WLAN -o $LAN -j ACCEPT
|
|---|
| 70 |
|
|---|
| 71 | ### MASQUERADING
|
|---|
| 72 | echo 1 > /proc/sys/net/ipv4/ip_dynaddr
|
|---|
| 73 | iptables -t nat -A POSTROUTING -o $WAN -j MASQUERADE
|
|---|
| 74 |
|
|---|
| 75 | ######################################################################
|
|---|
| 76 | ### Default ruleset end
|
|---|
| 77 | ######################################################################
|
|---|
| 78 |
|
|---|
| 79 | ###
|
|---|
| 80 | ### Connections to the router
|
|---|
| 81 | ###
|
|---|
| 82 |
|
|---|
| 83 | # ssh
|
|---|
| 84 | #iptables -A input_rule -i $WAN -p tcp -s <a.b.c.d> --dport 22 -j ACCEPT
|
|---|
| 85 |
|
|---|
| 86 | # IPSec
|
|---|
| 87 | #iptables -A input_rule -i $WAN -p esp -s <a.b.c.d> -j ACCEPT
|
|---|
| 88 | #iptables -A input_rule -i $WAN -p udp -s <a.b.c.d> --dport 500 -j ACCEPT
|
|---|
| 89 |
|
|---|
| 90 | # OpenVPN
|
|---|
| 91 | #iptables -A input_rule -i $WAN -p udp -s <a.b.c.d> --dport 1194 -j ACCEPT
|
|---|
| 92 |
|
|---|
| 93 | # PPTP
|
|---|
| 94 | #iptables -A input_rule -i $WAN -p gre -j ACCEPT
|
|---|
| 95 | #iptables -A input_rule -i $WAN -p tcp --dport 1723 -j ACCEPT
|
|---|
| 96 |
|
|---|
| 97 | ###
|
|---|
| 98 | ### VPN traffic
|
|---|
| 99 | ###
|
|---|
| 100 |
|
|---|
| 101 | # IPSec
|
|---|
| 102 | #iptables -A forwarding_rule -o ipsec+ -j ACCEPT
|
|---|
| 103 | #iptables -A forwarding_rule -i ipsec+ -j ACCEPT
|
|---|
| 104 |
|
|---|
| 105 | # OpenVPN
|
|---|
| 106 | #iptables -A forwarding_rule -o tun+ -j ACCEPT
|
|---|
| 107 | #iptables -A forwarding_rule -i tun+ -j ACCEPT
|
|---|
| 108 |
|
|---|
| 109 | ###
|
|---|
| 110 | ### Port forwardings to LAN
|
|---|
| 111 | ###
|
|---|
| 112 |
|
|---|
| 113 | #iptables -t nat -A prerouting_rule -i $WAN -p tcp --dport 3389 -j DNAT --to 192.168.1.10
|
|---|
| 114 | #iptables -A forwarding_rule -i $WAN -p tcp --dport 3389 -d 192.168.1.10 -j ACCEPT
|
|---|