问题原文
原帖链接 · Shuzheng · CC BY-SA 4.0
How can
iptablesboth have(ACCEPT, all, anywhere, anywhere)and(DROP, all, anywhere, anywhere)in its INPUT chain?
How is it meaningful for iptables to both have rules for ACCEPT and DROP all traffic in its INPUT chain with a default policy of DROP?
In this case, will traffic actually be accepted or dropped? I see that special rules exists for ssh and http, so they would naturally take precendece, because they are more specific?
# iptables -L
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT all -- anywhere anywhere
ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
DROP all -- anywhere anywhere ctstate INVALID
ACCEPT tcp -- anywhere anywhere tcp dpt:ssh ctstate NEW,ESTABLISHED
ACCEPT tcp -- anywhere anywhere tcp dpt:http ctstate NEW,ESTABLISHED
ACCEPT icmp -- anywhere anywhere
Chain FORWARD (policy DROP)
target prot opt source destination
Chain OUTPUT (policy DROP)
target prot opt source destination
ACCEPT all -- anywhere anywhere
ACCEPT all -- anywhere anywhere ctstate ESTABLISHED
ACCEPT tcp -- anywhere anywhere tcp spt:ssh ctstate ESTABLISHED
ACCEPT tcp -- anywhere anywhere tcp spt:http ctstate ESTABLISHED
iptables-save:
iptables-save
# Generated by iptables-save v1.6.1 on Sun Jun 23 10:21:50 2019
*filter
:INPUT DROP [1665:309354]
:FORWARD DROP [0:0]
:OUTPUT DROP [10:520]
-A INPUT -i lo -j ACCEPT
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A INPUT -m conntrack --ctstate INVALID -j DROP
-A INPUT -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A OUTPUT -o lo -j ACCEPT
-A OUTPUT -m conntrack --ctstate ESTABLISHED -j ACCEPT
-A OUTPUT -p tcp -m tcp --sport 22 -m conntrack --ctstate ESTABLISHED -j ACCEPT
-A OUTPUT -p tcp -m tcp --sport 80 -m conntrack --ctstate ESTABLISHED -j ACCEPT
COMMIT
# Completed on Sun Jun 23 10:21:50 2019
已采纳解答
原帖链接 · HBruijn · CC BY-SA 4.0
Please note the small print in the addendum to the -L flag in iptables and use the -v option:
-L,--list [chain]
List all rules in the selected chain. ... The exact rules are suppressed until you useiptables -L -v
Personally I prefer dumping the complete rule-set with iptables-save to get a quick overview as iptables -L also only displays the filter table by default and you have to explicitly request for instance the nat table.
Very useful in discussing iptables rules sets is to add line-numbers to your output and to print numeric ip-addresses and port numbers: [sudo] iptables -L -v -n --line-numbers
Because ip-tables -L doesn't display the exact rules and omits the interface the rule to trust all traffic on the loopback interface created with -A INPUT -i lo -j ACCEPT gets displayed as:
# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere
rather than:
# iptables -L -v -n --line-number
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 75890 6101K ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
Also note how when the rule was created with iptables -A INPUT -i lo -j ACCEPT without any restrictions on ip-address (ranges) with a either -s ip-address[/netmask] and/or -d ip-address[/netmask] the rule applies to any source and destination ip-address (the 0.0.0.0/0 network/netmask).
Now although that was not exactly the case to answer to your original question:
How can iptables have completely contrary rules in the same chain?
Because neither the kernel nor the iptables command to manipulate the rules interpret the rule set as a whole.
Each packet is checked sequentially against the rules in the order they are listed in each relevant chain. On the first rule that is a dispositive match that processing stops.
That means that you as an administrator can easily create rules (in an incorrect order) that appear completely contrary but will never become applicable, as a packet will always to an earlier rule.
The kernel will happily accept:
iptables -I INPUT 1 -s 10.2.3.4/32 -d 10.3.4.5/32 -j DROP
iptables -I INPUT 2 -s 10.2.3.4/32 -d 10.3.4.5/32 -j DROP
iptables -I INPUT 3 -s 10.2.3.4/32 -d 10.3.4.5/32 -j ACCEPT
iptables -I INPUT 4 -s 10.2.3.4/32 -d 10.3.4.5/32 -j DROP
(where the last three are completely irrelevant.) or something less contrived such as:
iptables -I INPUT 1 -p tcp -m tcp --dport 22 -j ACCEPT
iptables -I INPUT 2 -p tcp -m tcp --dport 22 -s 10.1.0.0/16 -j DROP
You can have a rule "accept all SSH connections" followed by a rule "don't except SSH connections from 10.1.0.0/16". Unfortunately that second rule, while perfectly valid, will never work, as SSH connections from a host with ip-address 10.1.2.3 will always first match on the "accept all SSH connections" rule.
Once you switch the order of those rules... Then things work as expected:
iptables -I INPUT 1 -p tcp -m tcp --dport 22 -s 10.1.0.0/16 -j DROP
iptables -I INPUT 2 -p tcp -m tcp --dport 22 -j ACCEPT
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
