首页 » IDC运维 » Iptables 过滤数据包-应用举例

Iptables 过滤数据包-应用举例

 

Iptables 应用举例

保存设定的规则

iptables-save > /etc/sysconfig/iptables

查看当前规则

iptables  -t filter -nvL 

查看当前规则,并显示规则序号

iptables -t nat -nvL --line-numbers

查看表的链的默认规则,简版

iptables -t nat -S

允许来自其他主机的ssh连接,假设默认ssh端口号为22

iptables -t filter -A INPUT -p tcp -m tcp --dport 22  -j ACCEPT

允许192.168.1.0-192.168.1.100段之间的ip走tcp协议访问90端口

iptables -t filter -A INPUT -p tcp -m iprange --src-range 192.168.1.0-192.168.1.100   --dport 90  -j ACCEPT

允许192.168.1.0/24这个段走tcp协议访问80端口

iptables -t filter -A INPUT -s 192.168.1.0/24 -p tcp --dport 80 -j ACCEPT

禁止任何主机ping我(直接丢弃icmp数据包)

iptables -t filter -A INPUT -p icmp -j DROP

禁止任何主机ping我(回复主机不可达)

iptables -t filter -A INPUT -p icmp -j REJECT --reject-with host-unreach

限制 icmp 速率,一分钟平均只能 ping 20 次,最多 30 次

iptables -t filter -A INPUT -p icmp -m limit --limit 20/min --limit-burst 30 -j ACCEPT
iptables -t filter -A INPUT -p icmp -j REJECT --reject-with host-unreach # 否则直接丢弃

删除INPUT里序号为2的规则

iptables -t filter -D INPUT 2

阻止windows蠕虫的攻击

iptables -I INPUT -j DROP -p tcp -s 0.0.0.0/0 -m string --algo kmp --string "cmd.exe"

防止SYN洪水攻击

iptables -A INPUT -p tcp --syn -m limit --limit 5/second -j ACCEPT
配合默认规则DROP使用或者在后面跟一条规则,
iptables -A INPUT -p tcp --syn -j DROP

防止各种端口扫描(配合默认规则DROP使用或者在后面跟一条DROP规则)

iptables -A FORWARD -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 100/s -j ACCEPT

防止ping洪水攻击(配合默认规则DROP使用或者在后面跟一条DROP规则)

iptables -A FORWARD -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT

允许本机可以访问本机

iptables -I INPUT -i lo -j ACCEPT

允许访问外网(当默认规则为DROP时,必需加入这一条规则,主机才能访问外网)

iptables -I INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

原文链接:Iptables 过滤数据包-应用举例,转载请注明来源!

0