Das folgende Skript
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 |
#!/bin/bash ### BEGIN INIT INFO # Provides: iptables_firewall # Required-Start: $local_fs $network # Required-Stop: $local_fs # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: iptables firewall initialisation # Description: Initializes rules for iptables ### END INIT INFO IPTABLES="/sbin/iptables" case "$1" in start) echo "Initializing firewall..." # Logging options. #------------------------------------------------------------------------------ LOG="LOG --log-level debug --log-tcp-sequence --log-tcp-options" LOG="$LOG --log-ip-options" # Defaults for rate limiting #------------------------------------------------------------------------------ RLIMIT="-m limit --limit 3/s --limit-burst 30" # Default policies. #------------------------------------------------------------------------------ # Drop everything by default. $IPTABLES -P INPUT DROP $IPTABLES -P FORWARD DROP $IPTABLES -P OUTPUT DROP # Set the nat/mangle/raw tables' chains to ACCEPT $IPTABLES -t nat -P PREROUTING ACCEPT $IPTABLES -t nat -P OUTPUT ACCEPT $IPTABLES -t nat -P POSTROUTING ACCEPT $IPTABLES -t mangle -P PREROUTING ACCEPT $IPTABLES -t mangle -P INPUT ACCEPT $IPTABLES -t mangle -P FORWARD ACCEPT $IPTABLES -t mangle -P OUTPUT ACCEPT $IPTABLES -t mangle -P POSTROUTING ACCEPT # Cleanup. #------------------------------------------------------------------------------ # Delete all $IPTABLES -F $IPTABLES -t nat -F $IPTABLES -t mangle -F # Delete all $IPTABLES -X $IPTABLES -t nat -X $IPTABLES -t mangle -X # Zero all packets and counters. $IPTABLES -Z $IPTABLES -t nat -Z $IPTABLES -t mangle -Z # Custom user-defined chains. #------------------------------------------------------------------------------ # LOG packets, then ACCEPT. $IPTABLES -N ACCEPTLOG $IPTABLES -A ACCEPTLOG -j $LOG $RLIMIT --log-prefix "FW-ACCEPT " $IPTABLES -A ACCEPTLOG -j ACCEPT # LOG packets, then DROP. $IPTABLES -N DROPLOG $IPTABLES -A DROPLOG -j $LOG $RLIMIT --log-prefix "FW-DROP " $IPTABLES -A DROPLOG -j DROP # LOG packets, then REJECT. # TCP packets are rejected with a TCP reset. $IPTABLES -N REJECTLOG $IPTABLES -A REJECTLOG -j $LOG $RLIMIT --log-prefix "FW-REJECT " $IPTABLES -A REJECTLOG -p tcp -j REJECT --reject-with tcp-reset $IPTABLES -A REJECTLOG -j REJECT # Only allows RELATED ICMP types # (destination-unreachable, time-exceeded, and parameter-problem). # TODO: Rate-limit this traffic? # TODO: Allow fragmentation-needed? # TODO: Test. $IPTABLES -N RELATED_ICMP $IPTABLES -A RELATED_ICMP -p icmp --icmp-type destination-unreachable -j ACCEPT $IPTABLES -A RELATED_ICMP -p icmp --icmp-type time-exceeded -j ACCEPT $IPTABLES -A RELATED_ICMP -p icmp --icmp-type parameter-problem -j ACCEPT $IPTABLES -A RELATED_ICMP -j DROPLOG # Make It Even Harder To Multi-PING $IPTABLES -A INPUT -p icmp -m limit --limit 1/s --limit-burst 2 -j ACCEPT $IPTABLES -A OUTPUT -p icmp -j ACCEPT # Only allow the minimally required/recommended parts of ICMP. Block the rest. #------------------------------------------------------------------------------ # Allow all ESTABLISHED ICMP traffic. $IPTABLES -A INPUT -p icmp -m state --state ESTABLISHED -j ACCEPT $RLIMIT $IPTABLES -A OUTPUT -p icmp -m state --state ESTABLISHED -j ACCEPT $RLIMIT # Allow some parts of the RELATED ICMP traffic, block the rest. $IPTABLES -A INPUT -p icmp -m state --state RELATED -j RELATED_ICMP $RLIMIT $IPTABLES -A OUTPUT -p icmp -m state --state RELATED -j RELATED_ICMP $RLIMIT # Allow incoming ICMP echo requests (ping), but only rate-limited. $IPTABLES -A INPUT -p icmp --icmp-type echo-request -j ACCEPT $RLIMIT # Allow outgoing ICMP echo requests (ping), but only rate-limited. $IPTABLES -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT $RLIMIT # Drop any other ICMP traffic. $IPTABLES -A INPUT -p icmp -j DROPLOG $IPTABLES -A OUTPUT -p icmp -j DROPLOG $IPTABLES -A FORWARD -p icmp -j DROPLOG # Selectively allow certain special types of traffic. #------------------------------------------------------------------------------ # Allow loopback interface to do anything. $IPTABLES -A INPUT -i lo -j ACCEPT $IPTABLES -A OUTPUT -o lo -j ACCEPT # Allow incoming connections related to existing allowed connections. $IPTABLES -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # Allow outgoing connections EXCEPT invalid $IPTABLES -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # Miscellaneous. #------------------------------------------------------------------------------ # Explicitly drop invalid incoming traffic $IPTABLES -A INPUT -m state --state INVALID -j DROP # Drop invalid outgoing traffic, too. $IPTABLES -A OUTPUT -m state --state INVALID -j DROP # If we would use NAT, INVALID packets would pass - BLOCK them anyways $IPTABLES -A FORWARD -m state --state INVALID -j DROP # Erlaube bestimmte ausgehende Anfragen (OUTPUT), blocke den Rest #------------------------------------------------------------------------------ # DNS OUTPUT $IPTABLES -A OUTPUT -m state --state NEW -p udp --dport 53 -j ACCEPT $IPTABLES -A OUTPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT # HTTP OUTPUT $IPTABLES -A OUTPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT # HTTPS OUTPUT $IPTABLES -A OUTPUT -m state --state NEW -p tcp --dport 443 -j ACCEPT # SMTP OUTPUT $IPTABLES -A OUTPUT -m state --state NEW -p tcp --dport 25 -j ACCEPT # SMTPS OUTPUT $IPTABLES -A OUTPUT -m state --state NEW -p tcp --dport 465 -j ACCEPT # Erlaube ausgehende "submission" (RFC 2476) Anfragen. $IPTABLES -A OUTPUT -m state --state NEW -p tcp --dport 587 -j ACCEPT # POP3S OUTPUT #$IPTABLES -A OUTPUT -m state --state NEW -p tcp --dport 995 -j ACCEPT # IMAP OUTPUT $IPTABLES -A OUTPUT -m state --state NEW -p tcp --dport 143 -j ACCEPT # IMAPS OUTPUT $IPTABLES -A OUTPUT -m state --state NEW -p tcp --dport 993 -j ACCEPT # SSH OUTPUT $IPTABLES -A OUTPUT -m state --state NEW -p tcp --dport 22 -j ACCEPT # FTP OUTPUT $IPTABLES -A OUTPUT -m state --state NEW -p tcp --dport 21 -j ACCEPT # SMB OUTPUT #$IPTABLES -A OUTPUT -p udp -m multiport --destination-port 137,138 -d 192.168.1.0/24 -j ACCEPTLOG #$IPTABLES -A OUTPUT -p tcp -m multiport --destination-port 139,445 -d 192.168.1.0/24 -j ACCEPTLOG # NFS OUTPUT #$IPTABLES -A OUTPUT -m state --state NEW -p udp --dport 111 -j ACCEPT #$IPTABLES -A OUTPUT -m state --state NEW -p tcp --dport 111 -j ACCEPT #$IPTABLES -A OUTPUT -m state --state NEW -p tcp --dport 2049 -j ACCEPT #$IPTABLES -A OUTPUT -m state --state NEW -p udp --dport 2049 -j ACCEPT #$IPTABLES -A OUTPUT -m state --state NEW -p tcp --dport 32764:32769 -j ACCEPT #$IPTABLES -A OUTPUT -m state --state NEW -p udp --dport 32764:32769 -j ACCEPT # NTP OUTPUT $IPTABLES -A OUTPUT -m state --state NEW -p udp --dport 123 -j ACCEPT # WHOIS OUTPUT $IPTABLES -A OUTPUT -m state --state NEW -p tcp --dport 43 -j ACCEPT # CVS OUTPUT #$IPTABLES -A OUTPUT -m state --state NEW -p tcp --dport 2401 -j ACCEPT # MySQL OUTPUT #$IPTABLES -A OUTPUT -m state --state NEW -p tcp --dport 3306 -j ACCEPT # rsync OUTPUT $IPTABLES -A OUTPUT -p tcp --dport 873 -m state --state NEW -d 192.168.1.0/24 -j ACCEPT # SVN OUTPUT #$IPTABLES -A OUTPUT -m state --state NEW -p tcp --dport 3690 -j ACCEPT # Mumble OUTPUT #$IPTABLES -A OUTPUT -m state --state NEW -p tcp --dport 64738 -j ACCEPT #$IPTABLES -A OUTPUT -m state --state NEW -p udp --dport 64738 -j ACCEPT # Erlaube bestimmte eingehende Anfragen (INPUT), blocke den Rest #------------------------------------------------------------------------------ # DNS INPUT #$IPTABLES -A INPUT -m state --state NEW -p udp --dport 53 -j ACCEPT #$IPTABLES -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT # HTTP INPUT #$IPTABLES -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT # HTTPS INPUT #$IPTABLES -A INPUT -m state --state NEW -p tcp --dport 443 -j ACCEPT # Webmin, INPUT #$IPTABLES -A INPUT -m state --state NEW -p tcp --dport 10000 -j ACCEPT # POP3 INPUT #$IPTABLES -A INPUT -m state --state NEW -p tcp --dport 110 -j ACCEPT # IMAP4 INPUT #$IPTABLES -A INPUT -m state --state NEW -p tcp --dport 143 -j ACCEPT # POP3S INPUT #$IPTABLES -A INPUT -m state --state NEW -p tcp --dport 995 -j ACCEPT # SMTP INPUT #$IPTABLES -A INPUT -m state --state NEW -p tcp --dport 25 -j ACCEPT # SSH INPUT $IPTABLES -A INPUT -m state --state NEW -p tcp --dport 22 -j ACCEPT # FTP INPUT #modprobe ip_conntrack_ftp #$IPTABLES -A INPUT -m state --state NEW -p tcp --dport 21 -j ACCEPT # FTPS INPUT (ebenfalls FTP erlauben) # Port-Range ist konfigurationsabhaengig (s. /etc/proftpd/proftpd.conf, PassivePorts) #$IPTABLES -A INPUT -p tcp --sport 1024: --dport 52500:52510 -m state --state NEW -j ACCEPT #$IPTABLES -A OUTPUT -p tcp --sport 52500:52510 --dport 1024: -m state --state NEW -j ACCEPT # SMB INPUT #$IPTABLES -A INPUT -p udp -m multiport --destination-port 137,138 -s 192.168.1.0/24 -j ACCEPTLOG #$IPTABLES -A INPUT -p tcp -m multiport --destination-port 139,445 -s 192.168.1.0/24 -j ACCEPTLOG # NFS INPUT # Achtung: Erfordert Rekonfiguration der Dienste (s. https://wiki.debian.org/SecuringNFS) #$IPTABLES -A INPUT -p udp -m multiport --dport 111 -s 192.168.1.0/24 -j ACCEPT #$IPTABLES -A INPUT -p tcp -m multiport --dport 111 -s 192.168.1.0/24 -j ACCEPT #$IPTABLES -A INPUT -p udp -m multiport --dport 2049 -s 192.168.1.0/24 -j ACCEPT #$IPTABLES -A INPUT -p tcp -m multiport --dport 2049 -s 192.168.1.0/24 -j ACCEPT #$IPTABLES -A INPUT -p udp -m multiport --dport 32764:32769 -s 192.168.1.0/24 -j ACCEPT #$IPTABLES -A INPUT -p tcp -m multiport --dport 32764:32769 -s 192.168.1.0/24 -j ACCEPT # Mumble INPUT #$IPTABLES -A INPUT -m state --state NEW -p tcp --dport 64738 -j ACCEPT #$IPTABLES -A INPUT -m state --state NEW -p udp --dport 64738 -j ACCEPT # MySQL INPUT #$IPTABLES -A INPUT -m state --state NEW -p tcp --dport 3306 -j ACCEPT # rsync INPUT (pruefen) #$IPTABLES -A INPUT -p tcp --dport 873 -m state --state NEW,ESTABLISHED -s 192.168.1.0/24 -j ACCEPT # DHCP INPUT #$IPTABLES -A INPUT -p udp --dport 67 -J ACCEPTLOG # Explicitly log and reject everything else. #------------------------------------------------------------------------------ # Use REJECT instead of REJECTLOG if you don't need/want logging. $IPTABLES -A INPUT -j REJECT $IPTABLES -A OUTPUT -j REJECT $IPTABLES -A FORWARD -j REJECT # Exit gracefully. #------------------------------------------------------------------------------ echo "Firewall initialized and active." ;; stop) echo "Flushing iptables rules..." sleep 1 iptables -F iptables -X iptables -t nat -F iptables -t nat -X iptables -t mangle -F iptables -t mangle -X iptables -P INPUT ACCEPT iptables -P FORWARD ACCEPT iptables -P OUTPUT ACCEPT echo "Firewall stopped." ;; *) echo "Usage: /etc/init.d/iptables_init {start|stop}" exit 2 esac exit 0 |