5 # some system-specific config...
15 SUBNET6
='2001:470:1f05:cb8::/64'
20 # note that behavior between v4 and v6 is slightly different
25 IPTABLES
=$(which iptables)
26 IP6TABLES
=$(which ip6tables)
29 SYSCTL
=/usr
/sbin
/sysctl
30 F2B_CTL
="/etc/init.d/fail2ban"
32 if [ "commit" != "$1" ]; then
33 IPTABLES
="echo ${IPTABLES}"
34 IP6TABLES
="echo ${IP6TABLES}"
37 SYSCTL
="echo ${SYSCTL}"
38 F2B_CTL
="echo ${F2B_CTL}"
41 # fail2ban writes its own chains, don't flush tables before shutting it down
43 if f2b_pid
=$(cat /var/run/fail2ban/fail2ban.pid)
45 f2b_comm
=`ps -o comm= -p ${f2b_pid}`
47 if [ "fail2ban-server" = "${f2b_comm}" ]; then
53 function sysctl_set
(){
54 if [ "$2" != $($SYSCTL -ne "$1") ]
56 echo "setting $1 to $2"
63 sysctl_set net.ipv4.ip_forward
1
64 sysctl_set net.ipv6.conf.all.forwarding
1
65 # disable routing triangulation; queries go out same interface
66 sysctl_set net.ipv4.conf.all.rp_filter
1
67 # log malformed packets
68 #${SYSCTL} -w net.ipv4.conf.all.log_martians=1
69 sysctl_set net.ipv4.conf.all.log_martians
0
71 sysctl_set net.ipv4.conf.all.send_redirects
0
72 sysctl_set net.ipv4.conf.all.accept_redirects
0
73 # disable source routed packets
74 sysctl_set net.ipv4.conf.all.accept_source_route
0
76 sysctl_set net.ipv4.tcp_syncookies
1
78 if [ ${f2b_needs_attention} -eq 1 ]; then
86 ${IPTABLES} -F FORWARD
87 ${IPTABLES} -F -t mangle
95 $IP6TABLES -F -t mangle
99 $IPTABLES -P INPUT DROP
100 $IPTABLES -P OUTPUT ACCEPT
101 $IPTABLES -P FORWARD ACCEPT
103 $IP6TABLES -P INPUT DROP
104 $IP6TABLES -P OUTPUT DROP
105 $IP6TABLES -P FORWARD DROP
107 .
/shaper.sh
${EXT_IF}
109 # reserve a special place in hell for some people
110 $IPTABLES -N xenophobe
111 $IPTABLES -A xenophobe
-m conntrack
--ctstate ESTABLISHED
,RELATED
-j RETURN
112 $IPTABLES -A xenophobe
-j REJECT
--reject-with icmp
-port-unreachable
114 $IP6TABLES -N xenophobe
115 $IP6TABLES -A xenophobe
-m conntrack
--ctstate ESTABLISHED
,RELATED
-j RETURN
116 $IP6TABLES -A xenophobe
-j REJECT
--reject-with icmp6
-port-unreachable
118 # create ipsets for v4 and v6
119 for s
in xenophobe sinokorea
121 $IPSET create
"$s" -exist hash:net counters
122 $IPSET create
"$s"6 -exist hash:net family inet6 counters
126 $IPSET create
"$s" -exist hash:net
127 $IPSET create
"$s"6 -exist hash:net family inet6
130 # create ipsets shared by v4 and v6
131 for s
in allowed_udp allowed_tcp
133 $IPSET create
"$s" -exist bitmap
:port range
0-65535
141 # allow local traffics
142 $IPTABLES -A INPUT
-i lo
-j ACCEPT
143 $IP6TABLES -A INPUT
-i lo
-j ACCEPT
144 $IP6TABLES -A OUTPUT
-o lo
-j ACCEPT
146 # allow anything out to v6
147 $IP6TABLES -A OUTPUT
-o ${EXT6_IF} -j ACCEPT
149 # allow all internal traffic in
150 $IP6TABLES -I INPUT
-i ${INT6_IF} -j ACCEPT
153 $IPTABLES -A INPUT
-p icmp
-j ACCEPT
154 $IP6TABLES -A INPUT
-p ipv6
-icmp -j ACCEPT
155 $IP6TABLES -A OUTPUT
-p ipv6
-icmp -j ACCEPT
156 $IP6TABLES -A FORWARD
-p ipv6
-icmp -j ACCEPT
158 # drop source-route headered v6
159 $IP6TABLES -A INPUT
-m rt
--rt-type 0 -j DROP
|| echo "MISSING RT MATCH" 1>&2
161 # drop bad packets; these are all illegal combinations
162 for flags
in 'ALL FIN,URG,PSH' 'ALL ALL' 'ALL SYN,RST,ACK,FIN,URG' 'ALL NONE' 'SYN,RST SYN,RST' 'SYN,FIN SYN,FIN'
164 $IPTABLES -A INPUT
-p tcp
--tcp-flags ${flags} -j DROP
167 # allow trusted things
168 $IPTABLES -A INPUT
-m set --match-set trusted src
-j ACCEPT
169 $IP6TABLES -A INPUT
-m set --match-set trusted6 src
-j ACCEPT
171 # drop sketchy things
172 $IPTABLES -A INPUT
-m set --match-set xenophobe src
-j xenophobe
173 $IP6TABLES -A INPUT
-m set --match-set xenophobe6 src
-j xenophobe
175 # drop asia from ssh and smtp
176 $IPTABLES -A INPUT
-m set --match-set sinokorea src
-m multiport
-p tcp
--dports ssh,smtp
-j xenophobe
177 $IP6TABLES -A INPUT
-m set --match-set sinokorea6 src
-m multiport
-p tcp
--dports ssh,smtp
-j xenophobe
179 # don't forward packets in
180 $IPTABLES -A FORWARD
-i ${EXT_IF} -m conntrack
--ctstate NEW
,INVALID
-j DROP
182 # forward from internal site subnet
183 $IP6TABLES -A FORWARD
-i ${INT6_IF} -o ${EXT6_IF} -s ${SUBNET6} -m conntrack
--ctstate NEW
-j ACCEPT
185 # allow things we've dealt with
186 $IPTABLES -A INPUT
-m conntrack
--ctstate RELATED
,ESTABLISHED
-j ACCEPT
187 $IP6TABLES -A INPUT
-m conntrack
--ctstate RELATED
,ESTABLISHED
-j ACCEPT
188 $IP6TABLES -A FORWARD
-m conntrack
--ctstate ESTABLISHED
,RELATED
-j ACCEPT
190 # accept ipv6 link-local
191 $IP6TABLES -A INPUT
-s fe80
::/10 -j ACCEPT
192 $IP6TABLES -A OUTPUT
-s fe80
::/10 -j ACCEPT
194 # accept ipv6 multicast
195 $IP6TABLES -A INPUT
-s ff00
::/8 -j ACCEPT
196 $IP6TABLES -A OUTPUT
-s ff00
::/8 -j ACCEPT
199 $IPTABLES -t nat
-A POSTROUTING
-o ${EXT_IF} -j SNAT
--to ${EXT}
201 # accept internal network traffic
202 $IPTABLES -A INPUT
-i ${INT_IF} -j ACCEPT
204 # accept list of external ports
205 $IPTABLES -A INPUT
-i ${EXT_IF} -p tcp
-m set --match-set allowed_tcp dst
-j ACCEPT
206 $IPTABLES -A INPUT
-i ${EXT_IF} -p udp
-m set --match-set allowed_udp dst
-j ACCEPT
207 $IP6TABLES -A INPUT
-i ${EXT6_IF} -p tcp
-m set --match-set allowed_tcp dst
-j ACCEPT
208 $IP6TABLES -A INPUT
-i ${EXT6_IF} -p udp
-m set --match-set allowed_udp dst
-j ACCEPT
211 # inserts, so stack order matters
216 if [ ${f2b_needs_attention} -eq 1 ]; then