ubuntuusers.de

iptables 2

Autor:
Vanger
Datum:
1. August 2011 00:17
Code:
  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
#!/bin/bash
# Flush all chains
# Note: In most cases this is not necessary
# /sbin/iptables  -F
# /sbin/ip6tables -F

# Delete all user-specific chains
# Note: In most cases this is not necessary
# /sbin/iptables  -X
# /sbin/ip6tables -X

# Zero all chains
# Note: In most cases this is not necessary
# /sbin/iptables  -Z
# /sbin/ip6tables -Z

# Set default policies
/sbin/iptables  -P INPUT DROP
/sbin/ip6tables -P INPUT DROP
/sbin/iptables  -P FORWARD DROP
/sbin/ip6tables -P FORWARD DROP
/sbin/iptables  -P OUTPUT DROP
/sbin/ip6tables -P OUTPUT DROP



###
# loopback interface
#

# Allow unlimited traffic on the loopback interface
/sbin/iptables  -A INPUT -i lo -j ACCEPT
/sbin/ip6tables -A INPUT -i lo -j ACCEPT
/sbin/iptables  -A OUTPUT -o lo -j ACCEPT
/sbin/ip6tables -A OUTPUT -o lo -j ACCEPT



###
# eth0
# Incoming traffic
#

# Create new chain INPUT_eth0
/sbin/iptables  -N INPUT_eth0
/sbin/ip6tables -N INPUT_eth0

# Pass every traffic which is targeted to us to chain INPUT_eth0
/sbin/iptables  -A INPUT -i eth0 -d 78.47.245.180 -j INPUT_eth0
# @TODO: /sbin/ip6tables -A INPUT -i eth0 -d 2a01:4f8:d12:1b00::/64 -j INPUT_eth0
/sbin/ip6tables -A INPUT -i eth0 -j INPUT_eth0

# Allow Hetzners DHCP service
/sbin/iptables  -A INPUT -i eth0 -p udp -s 0.0.0.0 --sport 68 -d 255.255.255.255 --dport 67 -j ACCEPT

# Log and drop every traffic which is remaining
/sbin/iptables  -A INPUT -i eth0 -m limit --limit 1/sec -j LOG --log-prefix "DROP_INPUT4_unknown: "
/sbin/ip6tables -A INPUT -i eth0 -m limit --limit 1/sec -j LOG --log-prefix "DROP_INPUT6_unknown: "
/sbin/iptables  -A INPUT -i eth0 -j DROP
/sbin/ip6tables -A INPUT -i eth0 -j DROP



###
# INPUT_eth0
# Input chain of interface eth0
#

# Allow everything from net TELTA-DYNAMIC-NET
# ISP of Daniel Rudolf (TELTA Citynetz Eberswalde GmbH)
/sbin/iptables  -A INPUT_eth0 -m iprange --src-range 80.78.180.0-80.78.191.255 -j ACCEPT
/sbin/ip6tables -A INPUT_eth0 -m iprange --src-range 2002:504e:b400::0000-2002:504e:bfff::ffff -j ACCEPT

# Previously initiated and accepted exchanges bypass rule
/sbin/iptables  -A INPUT_eth0 -m state --state RELATED -j ACCEPT
/sbin/ip6tables -A INPUT_eth0 -m state --state RELATED -j ACCEPT

# Service: Apache HTTP
# Allow TCP destination port 80
/sbin/iptables  -A INPUT_eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
/sbin/ip6tables -A INPUT_eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT

# Service: Apache HTTPS
# Allow TCP destination port 443
/sbin/iptables  -A INPUT_eth0 -p tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT
/sbin/ip6tables -A INPUT_eth0 -p tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT

# Service: Bind DNS Server
# Allow UDP destination port 53
/sbin/iptables  -A INPUT_eth0 -p udp --dport 53 -m state --state NEW,ESTABLISHED -j ACCEPT
/sbin/ip6tables -A INPUT_eth0 -p udp --dport 53 -m state --state NEW,ESTABLISHED -j ACCEPT

# Service: Bind DNS Server Zone Transfers
# Allow TCP destination port 53
# Attention: Don't forget to set the secondary nameservers IP address!
# /sbin/iptables  -A INPUT_eth0 -p tcp -s $SECONDARY_NAMESERVER_IP_ADDRESS --dport 53 -m state --state NEW,ESTABLISHED -j ACCEPT
# /sbin/ip6tables -A INPUT_eth0 -p tcp -s $SECONDARY_NAMESERVER_IP_ADDRESS --dport 53 -m state --state NEW,ESTABLISHED -j ACCEPT

# Service: ICMP
/sbin/iptables  -A INPUT_eth0 -p icmp -m state --state NEW,ESTABLISHED -j ACCEPT
/sbin/ip6tables -A INPUT_eth0 -p ipv6-icmp -m state --state NEW,ESTABLISHED -j ACCEPT

# Service: SSH
# Allow TCP destination port 22
/sbin/iptables  -A INPUT_eth0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
/sbin/ip6tables -A INPUT_eth0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT

# Application: DNS Client
# Allow UDP source port 53
/sbin/iptables  -A INPUT_eth0 -p udp --sport 53 -m state --state ESTABLISHED -j ACCEPT
/sbin/ip6tables -A INPUT_eth0 -p udp --sport 53 -m state --state ESTABLISHED -j ACCEPT

# Application: NTP
# Allow UDP source port 123
/sbin/iptables  -A INPUT_eth0 -p udp --sport 123 -m state --state ESTABLISHED -j ACCEPT
/sbin/ip6tables -A INPUT_eth0 -p udp --sport 123 -m state --state ESTABLISHED -j ACCEPT

# Application: WWW
# Allow TCP source port 80
/sbin/iptables  -A INPUT_eth0 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT
/sbin/ip6tables -A INPUT_eth0 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT

# Log and drop every traffic which is remaining
/sbin/iptables  -A INPUT_eth0 -m limit --limit 1/sec -j LOG --log-prefix "DROP_INPUT4_eth0: "
/sbin/ip6tables -A INPUT_eth0 -m limit --limit 1/sec -j LOG --log-prefix "DROP_INPUT6_eth0: "
/sbin/iptables  -A INPUT_eth0 -j DROP
/sbin/ip6tables -A INPUT_eth0 -j DROP



###
# eth0
# Forwarded traffic
#

# Log and drop every traffic
/sbin/iptables  -A FORWARD -o eth0 -m limit --limit 1/sec -j LOG --log-prefix "DROP_FORWARD4_eth0: "
/sbin/ip6tables -A FORWARD -o eth0 -m limit --limit 1/sec -j LOG --log-prefix "DROP_FORWARD6_eth0: "
/sbin/iptables  -A FORWARD -o eth0 -j DROP
/sbin/ip6tables -A FORWARD -o eth0 -j DROP



###
# eth0
# Outgoing traffic
#

# Create new chain OUTPUT_eth0
/sbin/iptables  -N OUTPUT_eth0
/sbin/ip6tables -N OUTPUT_eth0

# Pass every traffic which is sent by us to chain OUTPUT_eth0
/sbin/iptables  -A OUTPUT -o eth0 -s 78.47.245.180 -j OUTPUT_eth0
# @TODO: /sbin/ip6tables -A OUTPUT -o eth0 -s 2a01:4f8:d12:1b00::/64 -j OUTPUT_eth0
/sbin/ip6tables -A OUTPUT -o eth0 -j OUTPUT_eth0

# Log and drop every traffic which is remaining
/sbin/iptables  -A OUTPUT -o eth0 -j LOG --log-prefix "DROP_OUTPUT4_unknown: "
/sbin/ip6tables -A OUTPUT -o eth0 -j LOG --log-prefix "DROP_OUTPUT6_unknown: "
/sbin/iptables  -A OUTPUT -o eth0 -j DROP
/sbin/ip6tables -A OUTPUT -o eth0 -j DROP



###
# OUTPUT_eth0
# Output chain of interface eth0
#

# Allow unlimited outbound traffic
# Attention: This is not allowed! We're never establishing new connections.
# If we have to establish a new connection, create a new service-specific chain.
# /sbin/iptables  -A OUTPUT_eth0 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
# /sbin/ip6tables -A OUTPUT_eth0 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT

# Previously initiated and accepted exchanges bypass rule
/sbin/iptables  -A OUTPUT_eth0 -m state --state RELATED -j ACCEPT
/sbin/ip6tables -A OUTPUT_eth0 -m state --state RELATED -j ACCEPT

# Service: Apache HTTP
# Allow TCP source port 80
/sbin/iptables  -A OUTPUT_eth0 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT
/sbin/ip6tables -A OUTPUT_eth0 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT

# Service: Apache HTTPS
# Allow TCP source port 443
/sbin/iptables  -A OUTPUT_eth0 -p tcp --sport 443 -m state --state ESTABLISHED -j ACCEPT
/sbin/ip6tables -A OUTPUT_eth0 -p tcp --sport 443 -m state --state ESTABLISHED -j ACCEPT

# Service: Bind DNS Server
# Allow UDP source port 53
/sbin/iptables  -A OUTPUT_eth0 -p udp --sport 53 -m state --state ESTABLISHED -j ACCEPT
/sbin/ip6tables -A OUTPUT_eth0 -p udp --sport 53 -m state --state ESTABLISHED -j ACCEPT

# Service: Bind DNS Server Zone Transfers
# Allow TCP source port 53
# Attention: Don't forget to set the primary nameservers IP address!
# /sbin/iptables  -A OUTPUT_eth0 -p tcp --sport 53 -d $PRIMARY_NAMESERVER_IP_ADDRESS -m state --state NEW,ESTABLISHED -j ACCEPT
# /sbin/ip6tables -A OUTPUT_eth0 -p tcp --sport 53 -d $PRIMARY_NAMESERVER_IP_ADDRESS -m state --state NEW,ESTABLISHED -j ACCEPT

# Service: ICMP
/sbin/iptables  -A OUTPUT_eth0 -p icmp -m state --state ESTABLISHED -j ACCEPT
/sbin/ip6tables -A OUTPUT_eth0 -p ipv6-icmp -m state --state ESTABLISHED -j ACCEPT

# Service: SSH
# Allow TCP source port 22
/sbin/iptables  -A OUTPUT_eth0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
/sbin/ip6tables -A OUTPUT_eth0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

# Application: DNS Client
# Allow UDP destination port 53
/sbin/iptables  -A OUTPUT_eth0 -p udp --dport 53 -m state --state NEW,ESTABLISHED -j ACCEPT
/sbin/ip6tables -A OUTPUT_eth0 -p udp --dport 53 -m state --state NEW,ESTABLISHED -j ACCEPT

# Application: NTP
# Allow UDP destination port 123 from source port 123
/sbin/iptables  -A OUTPUT_eth0 -p udp --sport 123 --dport 123 -m state --state NEW,ESTABLISHED -j ACCEPT
/sbin/ip6tables -A OUTPUT_eth0 -p udp --sport 123 --dport 123 -m state --state NEW,ESTABLISHED -j ACCEPT

# Application: WWW
# Allow TCP destination port 80
/sbin/iptables  -A INPUT_eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
/sbin/ip6tables -A INPUT_eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT

# Log and drop every traffic which is remaining
/sbin/iptables  -A OUTPUT_eth0 -j LOG --log-prefix "DROP_OUTPUT4_eth0: "
/sbin/ip6tables -A OUTPUT_eth0 -j LOG --log-prefix "DROP_OUTPUT6_eth0: "
/sbin/iptables  -A OUTPUT_eth0 -j DROP
/sbin/ip6tables -A OUTPUT_eth0 -j DROP