系统的INPUT和OUTPUT默认策略为DROP;
iptables -P INPUT DROP iptables -P OUTPUT DROP [root@localhost ~]# iptables -L -n Chain INPUT (policy DROP) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy DROP) target prot opt source destination
1、限制本地主机的web服务器在周一不允许访问;新请求的速率不能超过100个每秒;web服务器包含了admin字符串的页面不允许访问;web服务器仅允许响应报文离开本机;
iptables -A INPUT -d 10.0.0.2 -p tcp --dport 80 -m time --weekdays Mon -m limit --limit 100/second -m string --algo bm --string "admin" -j DROP iptables -I OUTPUT -s 10.0.0.2 -p tcp --sport 80 -m string --algo bm --string "admin" -j DROP iptables -A OUTPUT -m state --state ESTABLISHED -j ACCEPT [root@localhost html]# iptables -L -n Chain INPUT (policy ACCEPT) target prot opt source destination DROP tcp -- 0.0.0.0/0 10.0.0.2 tcp dpt:80 TIME on Mon limit: avg 100/sec burst 5 STRING match "admin" ALGO name bm TO 65535 Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination DROP tcp -- 10.0.0.2 0.0.0.0/0 tcp spt:80 STRING match "admin" ALGO name bm TO 65535 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state ESTABLISHED
2、在工作时间,即周一到周五的8:30-18:00,开放本机的ftp服务给172.16.0.0网络中的主机访问;数据下载请求的次数每分钟不得超过5个;
iptables -I INPUT -p tcp -d 172.16.0.0 -m time --weekdays 1,2,3,4,5 --timestart 8:30 --timestop 18:00 -m limit --limit-burst 5 -m multiport --dports 20,21 -m state --state NEW,ESTABLISHED -j ACCEPT iptables -A OUTPUT -m state --state ESTABLISHED -j ACCEPT [root@localhost ~]# iptables -L -n Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT tcp -- 0.0.0.0/0 172.16.0.0 TIME from 08:30:00 to 18:00:00 on Mon,Tue,Wed,Thu,Fri limit: avg 3/hour burst 5 multiport dports 20,21 state NEW,ESTABLISHED Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state ESTABLISHED
3、开放本机的ssh服务给172.16.x.1-172.16.x.100中的主机,x为你的座位号,新请求建立的速率一分钟不得超过2个;仅允许响应报文通过其服务端口离开本机;
iptables -I INPUT -p tcp --dport 22 -m iprange --src-range 172.16.100.1-172.16.100.100 -m limit --limit 2/minute -j ACCEPT iptables -A OUTPUT -m state --state ESTABLISHED -j ACCEPT [root@localhost ~]# iptables -L -n Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 source IP range 172.16.100.1-172.16.100.100 limit: avg 2/min burst 5 Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state ESTABLISHED
4、拒绝TCP标志位全部为1及全部为0的报文访问本机;
iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP [root@localhost ~]# iptables -L -n Chain INPUT (policy ACCEPT) target prot opt source destination DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp flags:0x3F/0x3F DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp flags:0x3F/0x00 Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination
5、允许本机ping别的主机;但不开放别的主机ping本机;
iptables -I INPUT -p icmp --icmp-type 0 -j ACCEPT iptables -I OUTPUT -p icmp --icmp-type 8 -j ACCEPT [root@localhost ~]# iptables -L -n Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 icmp type 0 Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 icmp type 8
6、判断下述规则的意义:
# iptables -N clean_in 自定义链---->clean_in # iptables -A clean_in -d 255.255.255.255 -p icmp -j DROP 增加新规则---->丢弃广播域的包 # iptables -A clean_in -d 172.16.255.255 -p icmp -j DROP 增加新规则---->丢弃ping 172.16.255.255的包 # iptables -A clean_in -p tcp ! --syn -m state --state NEW -j DROP 增加新规则---->丢弃syn标志不为1,链接状态为新建链接的包 # iptables -A clean_in -p tcp --tcp-flags ALL ALL -j DROP 丢弃tcp标志全部为1的报文 # iptables -A clean_in -p tcp --tcp-flags ALL NONE -j DROP 丢弃tcp标志全部为0的报文 # iptables -A clean_in -d 172.16.100.7 -j RETURN 在clean_in链中找不到匹配报文时回到主链 # iptables -A INPUT -d 172.16.100.7 -j clean_in 目标为172.16.100.7的报文交给clean_in处理 # iptables -A INPUT -i lo -j ACCEPT 指定流入报文接口为lo # iptables -A OUTPUT -o lo -j ACCEPT 指定流出报文接口为lo # iptables -A INPUT -i eth0 -m multiport -p tcp --dports 53,113,135,137,139,445 -j DROP 指定流入报文接口为eth0,协议为tcp,目标端口为53,113,135,137,139,445的报文丢弃 # iptables -A INPUT -i eth0 -m multiport -p udp --dports 53,113,135,137,139,445 -j DROP 指定流入报文接口为eth0,协议为udp,目标端口为53,113,135,137,139,445的报文丢弃 # iptables -A INPUT -i eth0 -p udp --dport 1026 -j DROP 指定流入报文接口为eth0,协议为udp,目标端口为1026的报文丢弃 # iptables -A INPUT -i eth0 -m multiport -p tcp --dports 1433,4899 -j DROP 指定流入报文接口为eth0,协议为tcp,指定多个端口1433,4899,报文丢弃 # iptables -A INPUT -p icmp -m limit --limit 10/second -j ACCEPT 限定ping速率为每秒10次
7、通过tcp_wrapper控制vsftpd仅允许172.16.0.0/255.255.0.0网络中的主机访问,但172.16.100.3除外;对所被被拒绝的访问尝试都记录在/var/log/tcp_wrapper.log日志文件中;
vim /etc/hosts.allow vsftpd:172.16.0.0/255.255.0.0 EXCEPT 172.16.100.3 vim /etc/hosts.deny vsftpd:ALL :spawn /bin/echo `date` login attempt from %c to %s, %d >> /var/log/tcp_wrapper.log
原创文章,作者:oranix,如若转载,请注明出处:http://www.178linux.com/76618