其实iptables并不是一个很神秘的东西,只是掌握其配置起来比较困难,因为他的语法规则比较繁琐,配置起来比较麻烦,但是如果能掌握其基本用法,配置和使用起来慢慢就会得心应手。本文将从iptables的基本规则和用法详细讲起,然后附加一下案例来对iptables详细的理解,希望本文对你学习iptables有一点帮助下面我们一块来揭开iptables的神秘面纱。
说的iptables,稍微对iptables有一点了解的人都知道,其实iptables就是linux自带的一种防火墙策略,所以我们先来了解一下防火墙分类。
防火墙分类:
防火墙有硬件防火墙 --》华为,思科,天融信,神州数码等。
软件防火墙,--》微软的RSA等
主机防火墙,网络防火墙等等
包过滤型防火墙: 安全性较好,但是效率不高
根据tcp首部或者ip首部数据进行判断
几种状态:NEW(第一次请求连接 syn=1,ack=0) ESTABLISHED (已建立的连 接syn=0,ack=1) INVALID (非法的连接) RELATED (相关连的连接)
应用层网关防火墙: 效率好了 但是安全性不佳 是根据数据包传输的实际数据内容进行过滤判断的。
iptables 工作在第三层,即网络层 但是iptables的规则却在kernel中
iptables的命令与配置详解
iptables 命令使用规则有种好记的法则就是
iptables –t 表名 指令 链表名 描述 –j 动作
即iptables [-t TABLE] COMMAND CHAIN [creteria] -j ACTION
链接管理类:
默认策略:
清空计数器:
- [root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eth0 -j ACCEPT
- [root@localhost ~]# iptables -t nat -L -v
- Chain PREROUTING (policy ACCEPT 3 packets, 905 bytes)
- pkts bytes target prot opt in out source destination
- Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
- pkts bytes target prot opt in out source destination
- 1 108 ACCEPT all -- any eth0 192.168.0.0/24 anywhere #新添加的规则
- Chain OUTPUT (policy ACCEPT 1 packets, 108 bytes)
- pkts bytes target prot opt in out source destination
- #########ssh远程连接 #########
- [root@localhost ~]# iptables -A INPUT -d 192.168.0.100/32 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
- [root@localhost ~]# iptables -L -n
- Chain INPUT (policy ACCEPT)
- target prot opt source destination
- ACCEPT tcp -- 0.0.0.0/0 192.168.0.100 tcp dpt:22 state NEW,ESTABLISHED
- Chain FORWARD (policy ACCEPT)
- target prot opt source destination
- Chain OUTPUT (policy ACCEPT)
- target prot opt source destination
- [root@localhost ~]# iptables -A OUTPUT -s 192.168.0.100 -p tcp --sport 22 -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 192.168.0.100 tcp dpt:22 state NEW,ESTABLISHED
- Chain FORWARD (policy ACCEPT)
- target prot opt source destination
- Chain OUTPUT (policy ACCEPT)
- target prot opt source destination
- ACCEPT tcp -- 192.168.0.100 0.0.0.0/0 tcp spt:22 state ESTABLISHED
- [root@localhost ~]# iptables -P INPUT DROP
- [root@localhost ~]# iptables -P OUTPUT DROP
- [root@localhost ~]# iptables -L -n
- Chain INPUT (policy DROP)
- target prot opt source destination
- ACCEPT tcp -- 0.0.0.0/0 192.168.0.100 tcp dpt:22 state NEW,ESTABLISHED
- Chain FORWARD (policy ACCEPT)
- target prot opt source destination
- Chain OUTPUT (policy DROP)
- target prot opt source destination
- ACCEPT tcp -- 192.168.0.100 0.0.0.0/0 tcp spt:22 state ESTABLISHED
- [root@localhost ~]# ping 127.0.0.1
- PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
- ping: sendmsg: Operation not permitted
- ping: sendmsg: Operation not permitted
- ping: sendmsg: Operation not permitted
- ping: sendmsg: Operation not permitted
- ping: sendmsg: Operation not permitted
- ping: sendmsg: Operation not permitted
- --- 127.0.0.1 ping statistics ---
- 6 packets transmitted, 0 received, 100% packet loss, time 5005ms
- [root@localhost ~]# iptables -I INPUT 1 -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT
- [root@localhost ~]# iptables -I OUTPUT 1 -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT
- [root@localhost ~]# ping 127.0.0.1
- PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
- 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=1.94 ms
- 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.161 ms
- 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.089 ms
- 64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.105 ms
- --- 127.0.0.1 ping statistics ---
- 4 packets transmitted, 4 received, 0% packet loss, time 3003ms
- rtt min/avg/max/mdev = 0.089/0.574/1.942/0.790 ms
- #web服务器访问
- [root@localhost ~]# iptables -A INPUT -d 192.168.0.100/32 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
- [root@localhost ~]# iptables -A OUTPUT -s 192.168.0.100 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT
- #此时就可以访问内部网络中的web服务器。
- [root@localhost ~]# iptables -L -n -v
- Chain INPUT (policy DROP 1784 packets, 162K bytes)
- pkts bytes target prot opt in out source destination
- 8 672 ACCEPT all -- * * 127.0.0.1 127.0.0.1
- 1107 80788 ACCEPT tcp -- * * 0.0.0.0/0 192.168.0.100 e NEW,ESTABLISHED
- 0 0 ACCEPT tcp -- * * 0.0.0.0/0 192.168.0.100 e NEW,ESTABLISHED
- Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
- pkts bytes target prot opt in out source destination
- Chain OUTPUT (policy DROP 65 packets, 4044 bytes)
- pkts bytes target prot opt in out source destination
- 8 672 ACCEPT all -- * * 127.0.0.1 127.0.0.1
- 582 62676 ACCEPT tcp -- * * 192.168.0.100 0.0.0.0/0 e ESTABLISHED
- 0 0 ACCEPT tcp -- * * 192.168.0.100 0.0.0.0/0 e ESTABLISHED
- [root@localhost ~]# iptables -I INPUT 2 -d 192.168.0.100 -p tcp -m state --state NEW,ESTABLISHED-m multiport --destination-ports 22,80 -j ACCEPT
- #这条规则把2,3 整合在一起,起到规则优化的作用
- #万能规则
- [root@localhost ~]# iptables -I OUTPUT 1 -m state --state ESTABLISHED -j ACCEPT
- [root@localhost ~]# iptables -L -n -v
此时就可以把OUTPUT链中的规则3,4 删除了,这样就减少了对规则条数的匹配,从而也对iptables起到了优化作用。
-m iprange 指定ip地址范围,
- [root@localhost ~]# iptables -A INPUT -m iprange --src-range 192.168.0.100-192.168.0.200 -p tcp --dport 23 -m state --state NEW -j ACCEPT
- [root@localhost ~]# iptables -L -n -v
- Chain INPUT (policy DROP 3745 packets, 340K bytes)
- pkts bytes target prot opt in out source destination
- 8 672 ACCEPT all -- * * 127.0.0.1 127.0.0.1
- 1335 87098 ACCEPT tcp -- * * 0.0.0.0/0 192.168.0.100 state NEW,ESTABLISHED multiport dports 22,80
- 1481 108K ACCEPT tcp -- * * 0.0.0.0/0 192.168.0.100 tcp dpt:22 state NEW,ESTABLISHED
- 0 0 ACCEPT tcp -- * * 0.0.0.0/0 192.168.0.100 tcp dpt:80 state NEW,ESTABLISHED
- 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 source IP range 192.168.0.100-192.168.0.200 tcp dpt:23 state NEW
- [root@localhost ~]# iptables -A INPUT -s 192.168.0.100 -p tcp --dport 80 -m state --state NEW -m limit --limit 3/minute --limit-burst 5 -j ACCAPT
- [root@localhost ~]# iptables -L -n -v
- Chain INPUT (policy DROP 3745 packets, 340K bytes)
- pkts bytes target prot opt in out source destination
- 8 672 ACCEPT all -- * * 127.0.0.1 127.0.0.1
- 1651 109K ACCEPT tcp -- * * 0.0.0.0/0 192.168.0.100 state NEW,ESTABLISHED multiport dports 22,80
- 1481 108K ACCEPT tcp -- * * 0.0.0.0/0 192.168.0.100 tcp dpt:22 state NEW,ESTABLISHED
- 0 0 ACCEPT tcp -- * * 0.0.0.0/0 192.168.0.100 tcp dpt:80 state NEW,ESTABLISHED
- 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 source IP range 192.168.0.100-192.168.0.200 tcp dpt:23 state NEW
- 0 0 ACCAPT tcp -- * * 192.168.0.100 0.0.0.0/0 tcp dpt:80 state NEW limit: avg 3/min burst 5
- # 平均没分钟三次连接,最大达到五次,超过则不予响应
- Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
- pkts bytes target prot opt in out source destination
- Chain OUTPUT (policy DROP 452 packets, 27288 bytes)
- pkts bytes target prot opt in out source destination
- 0 0 ACCEPT all -- * * 127.0.0.1 127.0.0.1
- 1521 147K ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state ESTABLISHED
- [root@localhost ~]# iptables -I OUTPUT 1 -m string --algo kmp --string "magedu" -j REJECT
- [root@localhost ~]# iptables -L -n
- Chain INPUT (policy DROP)
- target prot opt source destination
- ACCEPT all -- 127.0.0.1 127.0.0.1
- ACCEPT tcp -- 0.0.0.0/0 192.168.0.100 state NEW,ESTABLISHED multiport dports 22,80
- ACCEPT tcp -- 0.0.0.0/0 192.168.0.100 tcp dpt:22 state NEW,ESTABLISHED
- ACCEPT tcp -- 0.0.0.0/0 192.168.0.100 tcp dpt:80 state NEW,ESTABLISHED
- ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 source IP range 192.168.0.100-192.168.0.200 tcp dpt:23 state NEW
- DROP tcp -- 192.168.0.100 0.0.0.0/0 tcp dpt:80 state NEW limit: avg 3/min burst 5
- Chain FORWARD (policy ACCEPT)
- target prot opt source destination
- Chain OUTPUT (policy DROP)
- target prot opt source destination
- REJECT all -- 0.0.0.0/0 0.0.0.0/0 STRING match "magedu" ALGO name kmp TO 65535reject-with icmp-port-unreachable
- ACCEPT all -- 127.0.0.1 127.0.0.1
- ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state ESTABLISHED
- /etc/rc.d/init.d/iptables
- service iptables start: 启用保存的规则
- service iptables stop:
- /etc/sysconfig/iptables: 保存规则的文件
- /etc/sysconfig/iptables-config:向iptables脚本提供配置文件的文件
- service iptables save
- iptables-save > /etc/sysconfig/iptables.test #使用数据流重定向到到该文件
- iptables-restore < /etc/sysconfig/iptables.test #读取规则
利用iptables的recent模块来抵御DOS攻击
- #ssh: 远程连接
- iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 3 -j DROP
- iptables -I INPUT -p tcp --dport 22 -m state --state NEW -m recent --set --name SSH
- iptables -I INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 300 --hitcount 3 --name SSH -j DROP
- [root@localhost ~]# iptables -t -nat -A POSTROUTING -s 192.168.0.0/24 -oeth0 -j MASQUERADE
- #使用地址伪装来为内部网络snat,是内部网络能访问互联网
- [root@localhost ~]# iptables -P FORWARD DROP
- #关闭forward链,因为过滤策略将在此链上做
- [root@localhost ~]# iptables -A FORWARD -m iprange --src-range 192.168.0.10-192.168.0.20
- -m time --timestart 08:00 --timestop 20:00 -p tcp --dport 21 -j ACCEPT
- #工程部在上班时间只能访问ftp服务器,但是现在还不能访问到,需要对端口关联才可以
- [root@localhost ~]# iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
- #实现端口相关联
- [root@localhost ~]# modprobe ip_nat_ftp
- [root@localhost ~]# iptables -A FORWARD -s 192.168.0.0/24 -m time --timestart 20:01
- --timestop 07:59 -j ACCEPT
- #下班时间对各个部门无限制