Skip to contentSkip to conversation

--policy DROP kills everything?

DISCUSSION David Busby 4 participants 10 messages Jun 8, 2005 – Jun 10, 2005

lore
Share & tools
lore
#0 lore raw

I have this these rules on a host, to protect only this host.

# Generated by iptables-save v1.2.11 on Tue Jun 7 23:03:58 2005 *filter
:INPUT DROP [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -s 127.0.0.0/255.0.0.0 -i lo -j ACCEPT
-A INPUT -d 192.168.42.2 -i eth0 -p tcp -m tcp --dport 22 -j ACCEPT -A INPUT -d 192.168.42.2 -i eth0 -p tcp -m tcp --dport 80 -j ACCEPT COMMIT
# Completed on Tue Jun 7 23:03:58 2005

These are the loaded modules: iptable_filter, ip_tables

I cannot make an SSH or HTTP connection to the box.
I was under the impression that a policy of DROP will drop the packets if they dont match a rule.
If I switch the policy to ACCEPT then no problem. If I add a last rule as DROP then it also fails.
If I put a LOG or ULOG before the DROP rule then I can see packes destined for port 80 making it to log, shouldn't ACCEPT have passed them through? What stupid little thing did I miss?

imperium root # iptables --version
iptables v1.2.11
imperium root # uname -a
Linux imperium 2.6.10-gentoo-r6-edoceo #4 Sun May 1 03:48:25 PDT 2005 i686 AMD Athlon(TM) XP 1700+ AuthenticAMD GNU/Linux

/djb

#1 lore raw
Quoted 10 lines

# Generated by iptables-save v1.2.11 on Tue Jun 7 23:03:58 2005 *filter

Quoted 3 lines

INPUT DROP [0:0]
FORWARD ACCEPT [0:0]
OUTPUT ACCEPT [0:0]

(quoted diff) +0 -3
-A INPUT -s 127.0.0.0/255.0.0.0 -i lo -j ACCEPT
-A INPUT -d 192.168.42.2 -i eth0 -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -d 192.168.42.2 -i eth0 -p tcp -m tcp --dport 80 -j ACCEPT

COMMIT # Completed on Tue Jun 7 23:03:58 2005

Quoted 3 lines

These are the loaded modules: iptable_filter, ip_tables

I cannot make an SSH or HTTP connection to the box.

Add this on top of the other INPUT rules :

-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

Quoted 2 lines

I was under the impression that a policy of DROP will drop the packets if they dont match a rule.

That is correct. But..
A new ssh packet comes in and is accepted by --dport 22. The other packets in the same connection have state ESTABLISHED and you're not allowing those packets. That's why they are dropped.

Quoted 1 line

ACCEPT have passed them through? What stupid little thing did I miss?

The one rule above ;o).

Gr,
Rob

#2 lore raw
Quoted 5 lines · Rob Sterenborg

Add this on top of the other INPUT rules :

-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

Ok so I changed the rules to this:

Chain INPUT (policy DROP)

target     prot opt source               destination
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
ACCEPT     all  --  127.0.0.0/8          0.0.0.0/0
ACCEPT     udp  --  192.168.42.1         192.168.42.2        udp spt:53
ACCEPT     tcp  --  0.0.0.0/0            192.168.42.2        tcp dpt:22
ACCEPT     tcp  --  0.0.0.0/0            192.168.42.2        tcp dpt:80
LOG        all  --  0.0.0.0/0            0.0.0.0/0           LOG level warning

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

But it won't accept new connections on port 80 or 22. The Established SSH conection is fine. I can see new connections to port 80 or 22 in my logs, see:

IN=eth0 OUT= MAC=00:01:03:d2:db:0b:00:11:5b:50:ff:a4:08:00 SRC=192.168.42.34 DST=192.168.42.2 LEN=48 TOS=0x00 PREC=0x00 TTL=128 ID=18259 DF PROTO=TCP SPT=3704 DPT=80 WINDOW=65535 RES=0x00 SYN URGP=0

IN=eth0 OUT= MAC=00:01:03:d2:db:0b:00:11:5b:50:ff:a4:08:00 SRC=192.168.42.34 DST=192.168.42.2 LEN=48 TOS=0x00 PREC=0x00 TTL=128 ID=18526 DF PROTO=TCP SPT=3705 DPT=22 WINDOW=65535 RES=0x00 SYN URGP=0

Why they don't jump to accept? They match a rule.

So then I adjusted #1 above to this

ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state NEW,RELATED,ESTABLISHED

Doesn't that now allow any new connection to any port on any protocol making my FW worthless? Then I tried moving the RELATED,ESTABLISHED to the last rule like this:

imperium root # iptables -L INPUT -n
Chain INPUT (policy DROP)

target     prot opt source               destination
ACCEPT     all  --  127.0.0.0/8          0.0.0.0/0
ACCEPT     udp  --  192.168.42.1         192.168.42.2        udp spt:53
ACCEPT     tcp  --  0.0.0.0/0            192.168.42.2        tcp dpt:22
ACCEPT     tcp  --  0.0.0.0/0            192.168.42.2        tcp dpt:80
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
LOG        all  --  0.0.0.0/0            0.0.0.0/0           LOG flags 0 level 4

But I still cannot get new connections in and my estabilshed SSH is still OK.

Should I not use policy DROP but add a last rule of DROP? Which is the "right" way?
I guess I learned so far that I need to accept new and established. But I cannot seem to get the rules to work. I've been up and down the man page and samples but seem to be missing one more stupid little thing :( Please help.

/djb

#3 lore raw
Quoted 21 lines
Quoted 4 lines

Add this on top of the other INPUT rules :

-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

Ok so I changed the rules to this:

Chain INPUT (policy DROP)

target     prot opt source               destination
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0
state RELATED,ESTABLISHED
ACCEPT     all  --  127.0.0.0/8          0.0.0.0/0
ACCEPT     udp  --  192.168.42.1         192.168.42.2

udp spt:53
ACCEPT tcp -- 0.0.0.0/0 192.168.42.2
tcp dpt:22
ACCEPT tcp -- 0.0.0.0/0 192.168.42.2
tcp dpt:80
LOG all -- 0.0.0.0/0 0.0.0.0/0
LOG level warning

Quoted 7 lines

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

But it won't accept new connections on port 80 or 22. The

I'm not quite sure if that's true : you have a rule that says :
-A INPUT -d 192.168.42.2 -p tcp --dport 22 -j ACCEPT I have similar rules and it accepts just fine.

Quoted 13 lines

Established SSH conection is fine.
I can see new connections to port 80 or 22 in my logs, see:

IN=eth0 OUT= MAC=00:01:03:d2:db:0b:00:11:5b:50:ff:a4:08:00 SRC=192.168.42.34 DST=192.168.42.2 LEN=48 TOS=0x00 PREC=0x00 TTL=128 ID=18259 DF PROTO=TCP SPT=3704 DPT=80 WINDOW=65535 RES=0x00 SYN URGP=0

IN=eth0 OUT= MAC=00:01:03:d2:db:0b:00:11:5b:50:ff:a4:08:00 SRC=192.168.42.34 DST=192.168.42.2 LEN=48 TOS=0x00 PREC=0x00 TTL=128 ID=18526 DF PROTO=TCP SPT=3705 DPT=22 WINDOW=65535 RES=0x00 SYN URGP=0

Quoted 6 lines

Why they don't jump to accept? They match a rule.

So then I adjusted #1 above to this

ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
state NEW,RELATED,ESTABLISHED

Um, for me it's quite early in the morning but if I read this correct, you just accepted from everything to everything. That is not what you want I think.

Quoted 2 lines

Doesn't that now allow any new connection to any port on any protocol making my FW worthless?

Yes, but you allow everything in this rule so it's indeed worthless.

Quoted 18 lines

Then I tried moving the RELATED,ESTABLISHED to the last rule like this:

imperium root # iptables -L INPUT -n
Chain INPUT (policy DROP)

target     prot opt source               destination
ACCEPT     all  --  127.0.0.0/8          0.0.0.0/0
ACCEPT     udp  --  192.168.42.1         192.168.42.2

udp spt:53
ACCEPT tcp -- 0.0.0.0/0 192.168.42.2
tcp dpt:22
ACCEPT tcp -- 0.0.0.0/0 192.168.42.2
tcp dpt:80
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
state RELATED,ESTABLISHED
LOG all -- 0.0.0.0/0 0.0.0.0/0
LOG flags 0 level 4

Quoted 2 lines

But I still cannot get new connections in and my estabilshed SSH is still OK.

If -m state is not specified then, in my experience, -m state --state NEW is assumed (someone please correct me if I'm telling nonsense here) so your rules *will* allow new connections.

ESTABLISHED cannot be accepted if there hasn't been a NEW that has been accepted.

Quoted 2 lines

Should I not use policy DROP but add a last rule of DROP? Which is the "right" way?

That is essentially what policy DROP is doing ; if no rules match : DROP it.
You can set it to ACCEPT and add an ending rule that drops or rejects all packets not matched (you have more flexibility if working that way), but the idea is the same.

Quoted 5 lines

I guess I learned so far that I need to accept new and established. But I cannot seem to get the rules to work. I've been up and down the man page and samples but seem to be missing one more stupid little thing :(
Please help.

Somewhere you said ipt_state isn't loaded. Is it loaded now ??

Gr,
Rob

#4 lore raw
Quoted 3 lines · Rob Sterenborg

If -m state is not specified then, in my experience, -m state --state NEW is assumed (someone please correct me if I'm telling nonsense here) so your rules *will* allow new connections.

that is; indeed, nonsense.

-j

--
"Peter: And this is where the Pilgrims landed at Fraggle Rock."
--Family Guy

#5 lore raw

Re: specifying -m state --state NEW (Was : --policy DROP kills everything?)

Quoted 6 lines · Jason Opperisano
Quoted 3 lines · Rob Sterenborg

If -m state is not specified then, in my experience, -m state --state NEW is assumed (someone please correct me if I'm telling nonsense here) so your rules *will* allow new connections.

that is; indeed, nonsense.

In that case I don't understand why both rules seem to do the same job. Both :

$ipt -A INPUT -i eth0 -p tcp --dport 22 -j ACCEPT

and :

$ipt -A INPUT -i eth0 -m state --state NEW -p tcp --dport 22 -j ACCEPT

only work in combination with :

$ipt -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

At least, here it does.

I was just looking at Oskar's tutorial where I read that the state matches are explicit thus have to be specifically loaded for them to work. Does this means that if I don't specify a state match in a rule, the rule just won't match any state ?

Assuming that that's true in the first rule, a packet coming in on port 22 would be accepted regardless of state ?
In practice I know that that's not true because when I use the first rule without accepting RELATED or ESTABLISHED, I *cannot* connect to sshd. When I add the RELATED,ESTABLISHED accept rule, I *can* connect to sshd. (And this is how I would expect it to work.)

So, what is the difference in specifying and not specifying --state NEW in a rule ?

Gr,
Rob

#6 lore raw

Re: specifying -m state --state NEW (Was : --policy DROP kills everything?)

Quoted 15 lines · Rob Sterenborg
Quoted 6 lines · Jason Opperisano
Quoted 3 lines · Rob Sterenborg

If -m state is not specified then, in my experience, -m state --state NEW is assumed (someone please correct me if I'm telling nonsense here) so your rules *will* allow new connections.

that is; indeed, nonsense.

In that case I don't understand why both rules seem to do the same job. Both :

$ipt -A INPUT -i eth0 -p tcp --dport 22 -j ACCEPT

and :

Quoted 7 lines

$ipt -A INPUT -i eth0 -m state --state NEW -p tcp --dport 22 -j ACCEPT

only work in combination with :

$ipt -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

At least, here it does.

using "-m state --state NEW" in your rules pretty much forces you into using the "-m state --state RELATED,ESTABLISHED" rule (this is not a bad thing). the first rule:

  -A INPUT -i eth0 -p tcp --dport 22 -j ACCEPT

will not consider the state of the packet in the decision to match this rule. it will match NEW, ESTABLISHED, RELATED, etc...as long as the input interface is eth0 and the destination port is TCP 22.

Quoted 4 lines

I was just looking at Oskar's tutorial where I read that the state matches are explicit thus have to be specifically loaded for them to work. Does this means that if I don't specify a state match in a rule, the rule just won't match any state ?

correct. just like not specifying "-d w.x.y.z" means the rule will match any destination IP address; not specifying "-m state --state X" means the rule will match a packet in any state.

Quoted 2 lines

Assuming that that's true in the first rule, a packet coming in on port 22 would be accepted regardless of state ?

yes.

Quoted 4 lines

In practice I know that that's not true because when I use the first rule without accepting RELATED or ESTABLISHED, I *cannot* connect to sshd. When I add the RELATED,ESTABLISHED accept rule, I *can* connect to sshd. (And this is how I would expect it to work.)

i would question your testing methodology, as you can run a functional iptables firewall without using state matches at all.

Quoted 2 lines

So, what is the difference in specifying and not specifying --state NEW in a rule ?

the difference is whether you want to match only NEW packets or not. commercial firewalls like Cisco PIX and Check Point FW-1 enforce something along the lines of:

  -p tcp --tcp-flags ALL SYN -m state --state NEW

in the decision of whether a packet should be allowed to traverse the filter rules or not.

in the world of netfilter--you write the rules, you decide how specific the match will be. if you want to say, this rule only matches NEW TCP SYN packets, then use a construct like the above--you can even use variables to simplify your scripts, like so:

  TCP_REQ="-p tcp --tcp-flags ALL SYN -m state --state NEW"
  iptables -A INPUT $TCP_REQ --dport 22 -j ACCEPT

-j

--
"Brian: Congratulations, Peter. You're the Spalding Gray of crap."
--Family Guy

#7 lore raw

Re: specifying -m state --state NEW (Was : --policy DROP kills everything?)

Quoted 7 lines · Jason Opperisano
Quoted 4 lines

In practice I know that that's not true because when I use the first rule without accepting RELATED or ESTABLISHED, I *cannot* connect to sshd. When I add the RELATED,ESTABLISHED accept rule, I *can* connect to sshd. (And this is how I would expect it to work.)

i would question your testing methodology, as you can run a functional iptables firewall without using state matches at all.

Hmm. True.

I tested this before I sent the email and I had the following rules :

-P INPUT DROP
-P OUTPUT ACCEPT

-A INPUT -i eth1 -p tcp --dport 22 -j ACCEPT
# Skip a few rules that allow other services ;

# these should not interfere the test

I could not connect from any of my PC's.
When I added :

-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

I could connect from any of my PC's.

Quoted 12 lines
Quoted 2 lines

So, what is the difference in specifying and not specifying --state NEW in a rule ?

the difference is whether you want to match only NEW packets or not. commercial firewalls like Cisco PIX and Check Point FW-1 enforce something along the lines of:

  -p tcp --tcp-flags ALL SYN -m state --state NEW

in the decision of whether a packet should be allowed to traverse the filter rules or not.

Quoted 7 lines

in the world of netfilter--you write the rules, you decide how specific the match will be. if you want to say, this rule only matches NEW TCP SYN packets, then use a construct like the above--you can even use variables to simplify your scripts, like so:

  TCP_REQ="-p tcp --tcp-flags ALL SYN -m state --state NEW"
  iptables -A INPUT $TCP_REQ --dport 22 -j ACCEPT

=== From next post ===

Quoted 13 lines

just tested:

  # iptables-save
  # Generated by iptables-save v1.3.1 on Sat Jun 11 01:47:21 2005
  *filter
  :INPUT DROP [13:1519]
  :FORWARD ACCEPT [0:0]
  :OUTPUT ACCEPT [88:8463]
  -A INPUT -p icmp -j ACCEPT
  -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
  COMMIT
  # Completed on Sat Jun 11 01:47:21 2005
Quoted 6 lines

i can both ping and ssh to this machine. the only thing i can think of that would lead you to believe that you need the "--state RELATED,ESTABLISHED" rule in INPUT is because without it--the replies to the OUTPUT packets will be dropped. things like responses to DNS queries which will make your ssh connection take much longer if you
have "UseDNS yes" in sshd_config. just a thought.

<me slaps self with a big hammer> UseDNS *is* set...

Thanks for explaining.

Gr,
Rob

#8 lore raw

Re: specifying -m state --state NEW (Was : --policy DROP kills everything?)

In reply to Rob Sterenborg
Quoted 7 lines · Rob Sterenborg

In that case I don't understand why both rules seem to do the same job. Both :

$ipt -A INPUT -i eth0 -p tcp --dport 22 -j ACCEPT

and :

Quoted 7 lines

$ipt -A INPUT -i eth0 -m state --state NEW -p tcp --dport 22 -j ACCEPT

only work in combination with :

$ipt -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

At least, here it does.

just tested:

  # iptables-save
  # Generated by iptables-save v1.3.1 on Sat Jun 11 01:47:21 2005
  *filter
  :INPUT DROP [13:1519]
  :FORWARD ACCEPT [0:0]
  :OUTPUT ACCEPT [88:8463]
  -A INPUT -p icmp -j ACCEPT
  -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
  COMMIT
  # Completed on Sat Jun 11 01:47:21 2005

i can both ping and ssh to this machine. the only thing i can think of that would lead you to believe that you need the "--state RELATED,ESTABLISHED" rule in INPUT is because without it--the replies to the OUTPUT packets will be dropped. things like responses to DNS queries which will make your ssh connection take much longer if you have "UseDNS yes" in sshd_config. just a thought.

-j

--
"Stewie: Mark my words, your uppance shall come."
--Family Guy

#9 lore raw
In reply to David Busby
Quoted 10 lines · David Busby

I have this these rules on a host, to protect only this host.

# Generated by iptables-save v1.2.11 on Tue Jun 7 23:03:58 2005 *filter
:INPUT DROP [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]

(quoted diff) +0 -3
-A INPUT -s 127.0.0.0/255.0.0.0 -i lo -j ACCEPT
-A INPUT -d 192.168.42.2 -i eth0 -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -d 192.168.42.2 -i eth0 -p tcp -m tcp --dport 80 -j ACCEPT
Quoted 3 lines

COMMIT
# Completed on Tue Jun 7 23:03:58 2005

I used loaded these same rules changing only the ip address to match mine and tested only SSH (didn't have apache running). It worked for me, however, the DNS reverse lookup for the by SSH daemon had to time out which took around 10-20 seconds (I'm sure there is a real fixed number there I'm just to lazy to look it up). Ensure the eth0 really is 192.168.42.2 and be patient. I don't know if there would be issues with the web server you have with this or not as I didn't test. The bottom line is that this is an incomplete firewall set as SSH uses DNS and, even though it may be configured to ignore the result it still does the query, making it wait for a timeout is a problem.

While not part of your question I'd like to address some comments made here about connection tracking (all that --state stuff):
For the record, once a connection tracking module is loaded it tracks connections, it does nothing to them it just tracks the numbers. You not need to see --state NEW in a rule to actually start tracking. The --state things are there so you can =query= the state of the packet relative to the connection tracker, you can see if this is a NEW packet, an ESTABLISHED connection or a connection/packet RELATED to another (for example FTP and FTP Data channels). The module tracks the connections the --state allows us to apply the information to our firewall rules. This confused me for quite a while too but now that I get it, it is really quite obvious and simple.