summaryrefslogtreecommitdiffstats
path: root/server/setup/02-firewall/etc/iptables/ip4tables_bad_fwdmz_good-secure
blob: f6b1ad19bd0aa7bf9c5b7b518671228135a62779 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#! /bin/sh

#set -x

action=$1
shift

#
#* Single end device with untrusted inet connection
#

ETH_EXTERN=eth0
IP_EXTERN_SELF=$( /sbin/ip -o -f inet addr show dev $ETH_EXTERN scope global | awk ' { print $4;  } ' )
IP_EXTERN_GW=$( /sbin/ip -o -f inet route show dev $ETH_EXTERN | grep default | awk ' { print $3;  } ' )

##
##

IPTABLES=/sbin/iptables

if [ "$action" != "start" -a "$action" != "stop" -a "$action" != "restart" ] ; then
        echo usage $0 \( start \| stop \| restart \)
        echo

elif [ "$action" = "restart" ] ; then
        $0 stop
        $0 start

elif [ "$action" = "stop" ] ; then

        echo "IPTABLES rules down" 

        $IPTABLES -F acl_external_input
        $IPTABLES -F acl_srv_connect
        $IPTABLES -F acl_srv_web
        $IPTABLES -F acl_srv_shared
        $IPTABLES -F acl_srv_email
        $IPTABLES -F acl_srv_login_sec
        $IPTABLES -F INPUT
        $IPTABLES -F FORWARD

        $IPTABLES -X acl_external_input
        $IPTABLES -X acl_srv_connect
        $IPTABLES -X acl_srv_web
        $IPTABLES -X acl_srv_shared
        $IPTABLES -X acl_srv_email
        $IPTABLES -X acl_srv_login_sec

        $IPTABLES -P INPUT   ACCEPT
        $IPTABLES -P FORWARD ACCEPT

elif [ "$action" = "start" ] ; then

        echo "IPTABLES rules up" 

        # Anti-spoofing 
        #
        # Since we don't have any asymmetric routing, 
        # we can simply turn on anti-spoofing for all interfaces. 
        #
        #for f in /proc/sys/net/ipv4/conf/*ppp*/rp_filter; do echo 1 > $f; done

        echo 1 > /proc/sys/net/ipv4/conf/$ETH_EXTERN/rp_filter

        /sbin/modprobe iptable_filter
        /sbin/modprobe iptable_mangle
        /sbin/modprobe iptable_nat
        /sbin/modprobe ip_tables
        /sbin/modprobe ipt_LOG
        /sbin/modprobe ipt_MASQUERADE
        /sbin/modprobe ipt_NETMAP
        /sbin/modprobe ipt_REDIRECT
        /sbin/modprobe ipt_REJECT
        /sbin/modprobe ipt_ULOG
        /sbin/modprobe nf_conntrack_ipv4
        /sbin/modprobe nf_conntrack_sane
        /sbin/modprobe nf_conntrack_sip
        /sbin/modprobe nf_conntrack_snmp
        /sbin/modprobe nf_conntrack_tftp
        /sbin/modprobe nf_nat_ftp
        /sbin/modprobe nf_nat_h323
        /sbin/modprobe nf_nat_irc
        /sbin/modprobe nf_nat
        /sbin/modprobe nf_nat_pptp
        /sbin/modprobe nf_nat_sip
        /sbin/modprobe nf_nat_snmp_basic
        /sbin/modprobe nf_nat_tftp

        $IPTABLES -P INPUT   DROP
        $IPTABLES -P FORWARD DROP
        $IPTABLES -P OUTPUT  ACCEPT

        $IPTABLES -N acl_external_input
        $IPTABLES -N acl_srv_connect
        $IPTABLES -N acl_srv_web
        $IPTABLES -N acl_srv_shared
        $IPTABLES -N acl_srv_email
        $IPTABLES -N acl_srv_login_sec

        ###################################################################
        ###################################################################
        #

        # INPUT -> user chains
        #
        # Unfortunately, we only know (in the FORWARD chain) the outgoing interface. 
        # Thus, to figure out what interface the packet came in on, 
        # we use the source address (the anti-spoofing prevents address faking). 
        #
        # Note that we log anything which doesn't match any of these (obviously, this should never happen). 
        #

        #
        # INPUT Allow FIREWALL itself !
        #
        $IPTABLES -p all -A INPUT  -i $ETH_EXTERN         -s $IP_EXTERN_SELF                   -j ACCEPT
        $IPTABLES -p all -A INPUT  -i lo                                                       -j ACCEPT

        $IPTABLES -p all -A INPUT  -i $ETH_EXTERN                                              -j acl_external_input
        $IPTABLES -p all -A INPUT  -i $ETH_EXTERN   -j LOG --log-level debug --log-prefix "FW4-IN: rej acl_ext input "
        $IPTABLES -p all -A INPUT  -i $ETH_EXTERN                                              -j REJECT

        #
        # INPUT Log & Reject any
        #
        $IPTABLES -p all -A INPUT                        -j LOG --log-level debug --log-prefix "FW4-IN: rej ANY input "
        $IPTABLES -p all -A INPUT                                                              -j REJECT

        #
        ###################################################################
        ###################################################################
        ###################################################################
        #

        # FORWARD -> user chains
        #

        #
        # FORWARD Allow FIREWALL itself !
        #
        $IPTABLES -p all -A FORWARD  -i $ETH_EXTERN         -s $IP_EXTERN_SELF                 -j ACCEPT
        $IPTABLES -p all -A FORWARD  -i lo                                                     -j ACCEPT

        $IPTABLES -p all -A FORWARD  -i $ETH_EXTERN                                             -j acl_external_input
        $IPTABLES -p all -A FORWARD  -i $ETH_EXTERN   -j LOG --log-level debug --log-prefix "FW4-FWD: rej acl_ext input "
        $IPTABLES -p all -A FORWARD  -i $ETH_EXTERN                                            -j REJECT

        #
        # FORWARD Log & Reject any
        #
        $IPTABLES -p all -A FORWARD    -j LOG --log-level debug --log-prefix "FW4-FWD: rej ANY input "
        $IPTABLES -p all -A FORWARD                                                            -j REJECT

        #
        #
        #########################################################################################################################
        #########################################################################################################################
        #########################################################################################################################
        #
        # acl_extern_
        #

        ipaddr_file=$(dirname $0)/badbots_ipaddr.txt
        for ipaddr in `awk -e ' { i=index($1,"#"); if ( 0 == i ) { print $1; } } ' $ipaddr_file` ; do
            $IPTABLES -p all -A acl_external_input -s $ipaddr -j RETURN
        done

        #
        # Allow fragments (second etc. parts of a huge packet ..)
        # Allow icmp notification: 3/4 destination-unreachable/fragmentation-needed
        #
        # ATTENTION .. can be anything (ICMP), but the very 1st will be filtered ok !
        #
        $IPTABLES  -A acl_external_input   -p icmp --icmp-type echo-request -m limit --limit 5/s  -j ACCEPT

        $IPTABLES  -A acl_external_input   -p icmp --icmp-type destination-unreachable         -j ACCEPT
        $IPTABLES  -A acl_external_input   -p icmp --icmp-type time-exceeded                   -j ACCEPT
        $IPTABLES  -A acl_external_input   -p icmp --icmp-type parameter-problem               -j ACCEPT
	    $IPTABLES  -A acl_external_input   -p icmp --icmp-type timestamp-request               -j ACCEPT

        $IPTABLES -p all -A acl_external_input                                                 -j acl_srv_connect     # includes: allow to answer ..
        $IPTABLES -p all -A acl_external_input                                                 -j acl_srv_login_sec
        $IPTABLES -p all -A acl_external_input                                                 -j acl_srv_web
        $IPTABLES -p all -A acl_external_input                                                 -j acl_srv_shared
        $IPTABLES -p all -A acl_external_input                                                 -j acl_srv_email

        # --syn == "--tcp-flags SYN,RST,ACK SYN"
        #$IPTABLES -p tcp -A acl_external_input        ! --syn -j LOG --log-level debug --log-prefix "FW4-Ext: rej INET !syn "
        #$IPTABLES -p tcp -A acl_external_input        ! --syn                                 -j REJECT
        #$IPTABLES -p all -A acl_external_input -m state ! --state ESTABLISHED,RELATED   -j LOG --log-level debug --log-prefix "FW4-Ext: rej INET !resp "
        #$IPTABLES -p all -A acl_external_input -m state ! --state ESTABLISHED,RELATED         -j REJECT

        #
        #########################################################################################################################
        #########################################################################################################################
        #########################################################################################################################
        #
        # INPUT Allow acl_srv_shared 
        #
        # FTP, SAMBA, GNUnet clients
        #

        # ANONYMOUS FTP
        #
        #$IPTABLES -p tcp -A acl_srv_shared        --dport ftp-data:ftp                        -j ACCEPT
        #$IPTABLES -p udp -A acl_srv_shared        --dport ftp-data:ftp                        -j ACCEPT

        $IPTABLES -p tcp -A acl_srv_shared        --dport git                                  -j ACCEPT
        $IPTABLES -p udp -A acl_srv_shared        --dport git                                  -j ACCEPT

        #
        #
        #########################################################################################################################
        #
        # INPUT Allow acl_srv_login_sec 
        #
        # SSH
        #

        # SSH
        #
        #$IPTABLES -p tcp -A acl_srv_login_sec        --dport ssh                              -j LOG --log-level debug --log-prefix "FW4-ACL_SSH: "
        $IPTABLES -p tcp -A acl_srv_login_sec        --dport ssh                               -j ACCEPT
        $IPTABLES -p udp -A acl_srv_login_sec        --dport ssh                               -j ACCEPT

        #
        #
        #########################################################################################################################
        #
        # INPUT Allow acl_srv_email 
        #
        # POP3s, SMTP
        #

        # IMAPs
        #
        $IPTABLES -p tcp -A acl_srv_email        --dport imaps                                 -j ACCEPT

        # POP3s
        #
        $IPTABLES -p tcp -A acl_srv_email        --dport pop3s                                 -j ACCEPT

        # ident
        #
        $IPTABLES -p tcp -A acl_srv_email        --dport ident                                 -j ACCEPT

        # SMTPs
        #
        $IPTABLES -p tcp -A acl_srv_email        --dport smtp                                  -j ACCEPT
        $IPTABLES -p tcp -A acl_srv_email        --dport smtps                                 -j ACCEPT
        $IPTABLES -p tcp -A acl_srv_email        --dport sieve                                 -j ACCEPT

        #
        #########################################################################################################################
        #
        # INPUT Allow acl_srv_web 
        #
        # HTTP, NTP
        #

        # HTTP
        #
        $IPTABLES -p tcp -A acl_srv_web        --dport http                                    -j ACCEPT
        $IPTABLES -p tcp -A acl_srv_web        --dport https                                   -j ACCEPT
        #$IPTABLES -p tcp -A acl_srv_web        --dport http-alt                               -j ACCEPT
        #$IPTABLES -p tcp -A acl_srv_web        --dport webcache                               -j ACCEPT
        #$IPTABLES -p tcp -A acl_srv_web        --dport squid                                  -j ACCEPT

        #
        #
        #########################################################################################################################
        #
        # INPUT Allow acl_srv_connect 
        #
        # ICMP, NTP, DHCP, DNS, ANSWER
        #

        # IPSEC
        #
        $IPTABLES -p ah  -A acl_srv_connect                                                    -j ACCEPT
        $IPTABLES -p esp -A acl_srv_connect                                                    -j ACCEPT

        # DHCP .. TFTP
        #
        $IPTABLES -p udp -A acl_srv_connect -i $ETH_EXTERN --dport mdns        -s $IP_EXTERN_SELF  -j ACCEPT # 
        $IPTABLES -p udp -A acl_srv_connect -i $ETH_EXTERN --dport bootps:tftp -s $IP_EXTERN_GW    -j ACCEPT #
        $IPTABLES -p tcp -A acl_srv_connect -i $ETH_EXTERN --dport 67:69       -s $IP_EXTERN_GW    -j ACCEPT #

        # DNS
        #
        # $IPTABLES -p tcp -A acl_srv_connect        --dport mdns                              -j ACCEPT
        # $IPTABLES -p udp -A acl_srv_connect        --dport mdns                              -j ACCEPT
        # $IPTABLES -p tcp -A acl_srv_connect        --dport domain                            -j ACCEPT
        # $IPTABLES -p udp -A acl_srv_connect        --dport domain                            -j ACCEPT
        # $IPTABLES -p tcp -A acl_srv_connect        --dport netbios-ns:netbios-ssn            -j ACCEPT
        # $IPTABLES -p udp -A acl_srv_connect        --dport netbios-ns:netbios-ssn            -j ACCEPT

        # iperf
        #
        #$IPTABLES -p tcp  -A acl_srv_connect         --dport 5001                              -j ACCEPT
        #$IPTABLES -p udp  -A acl_srv_connect         --dport 5001                              -j ACCEPT

        #
        # acl_srv_connect Allow * to answer,
        # only for known connections - no new unknown ones !
        #
        $IPTABLES -p all  -A acl_srv_connect          -m state   --state ESTABLISHED,RELATED   -j ACCEPT

        #
        #
        #########################################################################################################################
fi