1+ ###############################################################################
2+ # The MIT License
3+ #
4+ # Copyright 2012-2014 Jakub Jirutka <jakub@jirutka.cz>.
5+ #
6+ # Permission is hereby granted, free of charge, to any person obtaining a copy
7+ # of this software and associated documentation files (the "Software"), to deal
8+ # in the Software without restriction, including without limitation the rights
9+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+ # copies of the Software, and to permit persons to whom the Software is
11+ # furnished to do so, subject to the following conditions:
12+ #
13+ # The above copyright notice and this permission notice shall be included in
14+ # all copies or substantial portions of the Software.
15+ #
16+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+ # THE SOFTWARE.
23+ #
24+
25+ ###############################################################################
26+ #
27+ # Basic iptables/IPv4 template for an ordinary servers
28+ #
29+ # This file is in iptables-restore format. See the man pages for
30+ # iptables-restore(8) and iptables-save(8).
31+ #
32+ # The following is a set of firewall rules that should be applicable to Linux
33+ # servers running within departments. It is intended to provide a useful
34+ # starting point from which to devise a comprehensive firewall policy for
35+ # a host.
36+ #
37+ # Parts 1 and 3 of these rules are the same for each host, whilst part 2 can be
38+ # populated with rules specific to particular hosts. The optional part 4 is
39+ # prepared for a NAT rules, e.g. for port forwarding, redirect, masquerade...
40+ #
41+ # This template is based on http://jdem.cz/v64a3 from University of Leicester.
42+ #
43+ # For the newest version go to https://gist.github.com/jirutka/3742890.
44+ #
45+ # @author Jakub Jirutka <jakub@jirutka.cz>
46+ # @version 1.3.1
47+ # @date 2014-01-28
48+ #
49+
50+ ###############################################################################
51+ # 1. COMMON HEADER #
52+ # #
53+ # This section is a generic header that should be suitable for most hosts. #
54+ ###############################################################################
55+
56+ *filter
57+
58+ # Base policy
59+ :INPUT DROP [0:0]
60+ :FORWARD DROP [0:0]
61+ :OUTPUT ACCEPT [0:0]
62+
63+ # Loopback device.
64+ -A INPUT -i lo -j ACCEPT
65+
66+ # Continue connections that are already established or related to an established connection.
67+ -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
68+
69+ # Drop non-conforming packets, such as malformed headers, etc.
70+ -A INPUT -m conntrack --ctstate INVALID -j DROP
71+
72+ # Block remote packets claiming to be from a loopback address.
73+ -A INPUT -s 127.0.0.0/8 ! -i lo -j DROP
74+
75+ # Drop all packets that are going to broadcast, multicast or anycast address.
76+ -A INPUT -m addrtype --dst-type BROADCAST -j DROP
77+ -A INPUT -m addrtype --dst-type MULTICAST -j DROP
78+ -A INPUT -m addrtype --dst-type ANYCAST -j DROP
79+ -A INPUT -d 224.0.0.0/4 -j DROP
80+
81+ # Chain for preventing SSH brute-force attacks.
82+ # Permits 10 new connections within 5 minutes from a single host then drops
83+ # incomming connections from that host. Beyond a burst of 100 connections we
84+ # log at up 1 attempt per second to prevent filling of logs.
85+ -N SSHBRUTE
86+ -A SSHBRUTE -m recent --name SSH --set
87+ -A SSHBRUTE -m recent --name SSH --update --seconds 300 --hitcount 10 -m limit --limit 1/second --limit-burst 100 -j LOG --log-prefix "iptables[SSH-brute]: "
88+ -A SSHBRUTE -m recent --name SSH --update --seconds 300 --hitcount 10 -j DROP
89+ -A SSHBRUTE -j ACCEPT
90+
91+ # Chain for preventing ping flooding - up to 6 pings per second from a single
92+ # source, again with log limiting. Also prevents us from ICMP REPLY flooding
93+ # some victim when replying to ICMP ECHO from a spoofed source.
94+ -N ICMPFLOOD
95+ -A ICMPFLOOD -m recent --set --name ICMP --rsource
96+ -A ICMPFLOOD -m recent --update --seconds 1 --hitcount 6 --name ICMP --rsource --rttl -m limit --limit 1/sec --limit-burst 1 -j LOG --log-prefix "iptables[ICMP-flood]: "
97+ -A ICMPFLOOD -m recent --update --seconds 1 --hitcount 6 --name ICMP --rsource --rttl -j DROP
98+ -A ICMPFLOOD -j ACCEPT
99+
100+ ###############################################################################
101+ # 2. HOST SPECIFIC RULES #
102+ # #
103+ # This section is a good place to enable your host-specific services. #
104+ # ! DO NOT FORGOT TO COPY THESE RULES TO firewall.ip6tables TO ALLOW IPV6 ! #
105+ ###############################################################################
106+
107+ ###############################################################################
108+ # 3. GENERAL RULES #
109+ # #
110+ # This section contains general rules that should be suitable for most hosts. #
111+ ###############################################################################
112+
113+ # Accept worldwide access to SSH and use SSHBRUTE chain for preventing
114+ # brute-force attacks.
115+ -A INPUT -p tcp --dport 22 --syn -m conntrack --ctstate NEW -j SSHBRUTE
116+
117+ # Permit useful IMCP packet types.
118+ # Note: RFC 792 states that all hosts MUST respond to ICMP ECHO requests.
119+ # Blocking these can make diagnosing of even simple faults much more tricky.
120+ # Real security lies in locking down and hardening all services, not by hiding.
121+ -A INPUT -p icmp --icmp-type 0 -m conntrack --ctstate NEW -j ACCEPT
122+ -A INPUT -p icmp --icmp-type 3 -m conntrack --ctstate NEW -j ACCEPT
123+ -A INPUT -p icmp --icmp-type 8 -m conntrack --ctstate NEW -j ICMPFLOOD
124+ -A INPUT -p icmp --icmp-type 11 -m conntrack --ctstate NEW -j ACCEPT
125+
126+ # Do not log packets that are going to ports used by SMB
127+ # (Samba / Windows Sharing).
128+ -A INPUT -p udp -m multiport --dports 135,445 -j DROP
129+ -A INPUT -p udp --dport 137:139 -j DROP
130+ -A INPUT -p udp --sport 137 --dport 1024:65535 -j DROP
131+ -A INPUT -p tcp -m multiport --dports 135,139,445 -j DROP
132+
133+ # Do not log packets that are going to port used by UPnP protocol.
134+ -A INPUT -p udp --dport 1900 -j DROP
135+
136+ # Do not log late replies from nameservers.
137+ -A INPUT -p udp --sport 53 -j DROP
138+
139+ # Good practise is to explicately reject AUTH traffic so that it fails fast.
140+ -A INPUT -p tcp --dport 113 --syn -m conntrack --ctstate NEW -j REJECT --reject-with tcp-reset
141+
142+ # Prevent DOS by filling log files.
143+ -A INPUT -m limit --limit 1/second --limit-burst 100 -j LOG --log-prefix "iptables[DOS]: "
144+
145+ COMMIT
146+
147+ ###############################################################################
148+ # 4. HOST SPECIFIC NAT RULES #
149+ # #
150+ # Uncomment this section if you want to use NAT table, e.g. for port #
151+ # forwarding, redirect, masquerade... #
152+ ###############################################################################
153+
154+ #*nat
155+
156+ # Base policy
157+ #:PREROUTING ACCEPT [0:0]
158+ #:POSTROUTING ACCEPT [0:0]
159+ #:OUTPUT ACCEPT [0:0]
160+
161+ # Redirect port 21 to local port 2121
162+ #-A PREROUTING -i eth0 -p tcp --dport 21 -j REDIRECT --to-port 2121
163+
164+ # Forward port 8080 to port 80 on host 192.168.1.10
165+ #-A PREROUTING -i eth0 -p tcp --dport 8080 -j DNAT --to-destination 192.168.1.10:80
166+
167+ #COMMIT
0 commit comments