Gentoo Archives: gentoo-user

From: Tanstaafl <tanstaafl@×××××××××××.org>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] IPTables question... simple as possible for starters
Date: Mon, 30 Dec 2013 11:08:34
Message-Id: 52C1540C.2050804@libertytrek.org
In Reply to: Re: [gentoo-user] IPTables question... simple as possible for starters by shawn wilson
1 On 2013-12-29 1:39 PM, shawn wilson <ag4ve.us@×××××.com> wrote:
2 > On Sun, Dec 29, 2013 at 1:07 PM, Tanstaafl <tanstaafl@×××××××××××.org> wrote:
3 >> Hi all,
4 >>
5 >> Ok, I'm setting up a new server, and I'd like to rethink my iptables rules.
6 >>
7 >> I'd like to start with something fairly simple:
8 >>
9 >> 1. Allow connections from anywhere ONLY to certain ports
10 >>
11 >> ie, for encrypted IMAP/SMTP connections from users
12 >>
13 >> 2. Allow connections from only certain IP addresses to certain ports
14 >>
15 >> ie, for limiting SSH access
16 >>
17 >
18 > I'd reverse the order that #1 and #2 appear.
19
20 Well, I was just writing that as a general description. Looking in the rules
21
22 >> 3. DROP ALL other connection attempts
23 >>
24 >> ie, I don't want to see these disallowed attempts in the logs
25 >>
26 >> In order to keep my rules more manageable, I have a commented text file that
27 >> I manually edit whenever modifying my rules, then I do an 'iptables-restore
28 >> < /path/to/iptables-rules' to update them.
29 >>
30 >> My first question is about a trick I learned some time ago (but don't
31 >> remember where)...
32 >>
33 >> For the ports for which I want to allow only restricted access, I have
34 >> something like:
35 >>
36 >> #######################
37 >> # bgn exceptions blocks
38 >> #######################
39 >> :f_22_I - [0:0]
40 >> :f_25_I - [0:0]
41 >> :f_22_O - [0:0]
42 >> :f_25_O - [0:0]
43 >>
44 >> Am I correct that the above are what are called 'chains' in iptables speak?
45 >>
46 >
47 > That defines non-kernel chains but you still need to jump to them from
48 > INPUT/OUTPUT or whatever. So, something like:
49 > -A -m tcp -p tcp --dport 22 --sport 1024:65535 -j f_22_I
50
51 Well, yeah... I didn't post my entire ruleset... ;)
52 ^^^^^ I just
53 >> And am I also correct that the above adds each rule to the named chain in
54 >> order, and that the order is significant?
55 >
56 > Yep - like ACLs, rules are processed from top down. ACCEPT, REJECT,
57 > and DROP are end points when they match.
58
59 Good, thanks.
60
61 >> Then... assuming that I have all of the specific rules after these set up to
62 >> allow just the traffic I want, and I wanted to add a final rule that just
63 >> silently DROPped all other inbound connection attempts, it would be:
64 >>
65 >> -A INPUT -j DROP
66
67 > What you're looking for is the policy which are by default ACCEPT on
68 > all kernel rules and which you change in the save file with something
69 > like this:
70 > :INPUT DROP [0:0]
71 >
72 > And, just so that there's no confusion, you should state the policy of
73 > OUTPUT and FORWARD at the top of your save file along with INPUT - see
74 > the output of iptables-save as an example of what your file should
75 > look like.
76
77 Ok, well, maybe I should have posted my entire ruleset...
78
79 I have this above where I define my chains:
80
81 #
82 *filter
83 :INPUT DROP [0:0]
84 :FORWARD DROP [0:0]
85 :OUTPUT DROP [0:0]
86 #
87
88 Does it matter where this goes?
89
90 And then above that, I have something else that I've never understood:
91
92 *mangle
93 :PREROUTING ACCEPT [1378800222:449528056411]
94 :INPUT ACCEPT [1363738727:447358082301]
95 :FORWARD ACCEPT [0:0]
96 :OUTPUT ACCEPT [1221121261:1103241097263]
97 :POSTROUTING ACCEPT [1221116979:1103240864155]
98 -A PREROUTING -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG
99 FIN,PSH,URG -j DROP
100 -A PREROUTING -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j DROP
101 -A PREROUTING -p tcp -m tcp --tcp-flags SYN,RST SYN,RST -j DROP
102 -A PREROUTING -p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -j DROP
103 COMMIT
104
105 > Also, if you're creating a chain just to do the same thing with
106 > different addresses, look at using ipset. Then you just:
107 > ipset create ssh_in iphash
108 > ipset add ssh_in 1.2.3.4
109 >
110 > and then this works:
111 > -A -m set --match-set ssh_in src -j ACCEPT
112 >
113 > ipset has the same save/load type things as ipt (minor differences
114 > with how you handle reload, but google or ask if you want to know).
115 > The set needs to be in place before the ipt rule is added, so ipset
116 > comes first in your boot sequence.
117
118 Thanks, looks interesting and useful...
119
120 So much to learn, so little time... ;)

Replies

Subject Author
Re: [gentoo-user] IPTables question... simple as possible for starters Pandu Poluan <pandu@××××××.info>