安装IPTABLES

yum -y install iptables-services

iptables的四表五链

四表:

  1. Filter:默认表

    使用的链:INPUT、FORWARD、OUTPUT

  2. NAT:当遇到创建新的数据包链接时,将参考这个表

    使用的链:PRTEROUTING、OUTPUT、POSTROUTING

  3. Managle:用于改变数据包

    使用的链:INPUT、FORWARD、PREROUTING、OUTPUT、POSTROUTING

  4. raw:此表不常用,基本忽略不计

五链:

  1. INPUT:用于处理输入的数据包
  2. OUTPUT:处理输出的数据包
  3. FORWARD:处理转发的数据包
  4. PREROUTING:用于目标地址转换
  5. POSTROUTING: 用于源地址转换

iptables的语法格式

iptables 命令的基本语法格式如下:

[root@liangxu ~]# iptables [-t table] COMMAND [chain] CRETIRIA -j ACTION

各参数的含义为:

-t:指定需要维护的防火墙规则表 filter、nat、mangle或raw。在不使用 -t 时则默认使用 filter 表。
COMMAND:子命令,定义对规则的管理。
chain:指明链表。
CRETIRIA:匹配参数。
ACTION:触发动作。
iptables 命令常用的选项及各自的功能如表 :

选项功能
-A添加规则
-D删除规则
-I插入规则
-F清空规则
-L列出添加的规则
-R替换规则(修改)
-Z清空防火墙数据表统计信息
-P设置链默认规则

iptables 命令常用匹配参数及各自的功能

参数功能
取反
-p匹配协议
-d匹配目的地址
-s匹配源地址
-i匹配入站网卡
-o匹配出站网卡
--sport匹配源端口
--dport匹配目的端口
--src-range匹配源地址范围
--dst-range匹配目标地址范围
--limit匹配数据表速率
--mac-source匹配源mac地址
--sports匹配多个源端口
--dports匹配多个目标端口
--stste匹配状态(INVALID、ESTABLISHED、NEW、RELATED)
--string匹配应用层子串

拓展匹配条件

扩展匹配条件指通用匹配之外的匹配条件。

隐含扩展匹配条件

-p tcp : 可直接使用tcp扩展模块的专用选项;
  –sport port[:port] --source-port匹配报文源端口;可以给出多个端口,但只能是连续的端口范围 ;
  –dport port[:port] --destination-port匹配报文目标端口;可以给出多个端口,但只能是连续的端口范围 ;
  –tcp-flags mask comp 匹配报文中的tcp协议的标志位;Flags are: SYN ACK FIN RST URG PSH ALL NONE;
    mask:要检查的FLAGS list,以逗号分隔;
    comp:在mask给定的诸多的FLAGS中,其值必须为1的FLAGS列表,余下的其值必须为0;
  –syn --tcp-flags SYN,ACK,FIN,RST SYN
-p udp: 可直接使用udp协议扩展模块的专用选项:
  –sport port[:port] --source-port
  –dport port[:port] --destination-port

-p icmp
  –icmp-type {type[/code]|typename}

显式扩展匹配条件

-m option选项指定扩展匹配的类型

1、multiport

以离散或连续的 方式定义多端口匹配条件,最多15个;

–sports port[,port|,port:port]… --source-ports :指定多个源端口;
–dports port[,port|,port:port]… --destination-ports:指定多个目标端口;

如:

iptables -A FORWARD -p tcp -m multiport --dport 53,80,443,465,993,20,21,137,138,139,444,445,4500:5000  -j ACCEPT

2、state限制收发包的状态

–state [state:NEW, ESTABLISHED, INVALID, RELATED、UNTRACKED].

NEW: 新连接请求;
ESTABLISHED:已建立的连接;
INVALID:无法识别的连接;
RELATED:相关联的连接,当前连接是一个新请求,但附属于某个已存在的连接;
UNTRACKED:未追踪的连接;

如:

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

3、limit

限制收发数据包的速率

–limit rate[/second|/minute|/hour|/day] 某段时间内封包的平均流量
–limit-burst number 一次同时涌入的封包是否超过给定值

如:

#iptables -A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT

4、comment

–comment “内容” 添加注释

如:

#iptables -A INPUT -s 10.10.10.10 -p tcp -dport 18888 -m comment --comment "监控" -j ACCEPT

5、iprange

以连续地址块的方式来指明多IP地址匹配条件;

–src-range from[-to]
–dst-range from[-to]

如:

iptables -I INPUT -d 10.10.10.10 -p tcp -m multiport --dports 22,80,139,3306 -m iprange --src-range 172.16.0.61-172.16.0.70 -j REJECT

6、time

匹配数据包到达的时间

–timestart hh:mm[:ss]
–timestop hh:mm[:ss]
–weekdays day[,day…]
–monthdays day[,day…]
–datestart YYYY[-MM[-DD[Thh[:mm[:ss]]]]]
–datestop YYYY[-MM[-DD[Thh[:mm[:ss]]]]]
–kerneltz:使用内核配置的时区而非默认的UTC;

7、string

匹配数据包中的字符

–string pattern
–hex-string pattern
–from offset
–to offset

如:

# iptables -I OUTPUT -m string --algo bm --string "gay" -j REJECT

8、connlimit

用于限制同一IP可建立的连接数目

–connlimit-upto n
–connlimit-above n

如:

# iptables -I INPUT -d 10.10.10.20 -p tcp --syn --dport 22 -m connlimit --connlimit-above 2 -j REJECT

iptables命令出发的动作及功能

参数功能
ACCEPT允许数据包通过
DROP丢弃数据包
REJECT拒绝数据包通过
LOG将数据包信息记录syslog日志
DNAT目标地址转换
SNAT源地址转换
MASQUERADE地址欺骗
REDIRECT重定向

iptables会按照顺序来以此执行iptables防火墙规则。

如果发现没有匹配的目录会按顺序向下执行匹配规则操作

如果所有的规则都没匹配到,会按照默认策略进行处理

iptables保存

iptables-save > /etc/sysconfig/iptables 	# 保存在默认文件中
iptables-save > 文件名称和相应目录  			  # 自定义保存
service iptables save 						# 保存规则

iptables 引起 ssh 登陆变慢等问题,参考这个文章

iptables设置导致SSH连接速度变慢? - yaxin1989 - 博客园 (cnblogs.com)

修改规则

iptables -R | 修改链 iptables -P

#修改规则
iptables -R INPUT 5 -s 192.168.50.90 -j DROP
#修改链的动作
iptables -P FORWARD DROP
-R:表示修改指定的链,虽然指定了编号,但-s选项以及对应的源地址不可省略,必须指定规则对应的原本的匹配条件(如果有多个匹配条件,都需要指定)

-P:表示修改指定的链

命令没有使用-s指定对应规则中原本的源地址,那么在修改完成后,修改的规则中的源地址会自动变为0.0.0.0/0(此IP表示匹配所有网段的IP地址),而此时万一-j对应的动作又为REJECT,那么所有IP的请求都被拒绝了(因为没有指定原本的源地址,当前规则的源地址自动变为0.0.0.0/0),如果正在使用ssh远程到服务器上进行iptables设置,那么ssh请求也将会被阻断!


例:

[root@localhost]# iptables --line-number -nvL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        3   180 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
2        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
3        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
4       97 12543 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
5        0     0 ACCEPT     all  --  *      *       192.168.50.90        0.0.0.0/0           
[root@localhost]# iptables -R INPUT 5 -s 192.168.50.90 -j DROP
[root@localhost]# iptables --line-number -nvL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        3   180 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
2        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
3        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
4       97 12543 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
5        0     0 DROP       all  --  *      *       192.168.50.90        0.0.0.0/0         

[root@localhost]# iptables --line-number -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        3   180 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
2        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
3        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
4      105 13503 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
5        0     0 DROP       all  --  *      *       192.168.50.90        0.0.0.0/0           

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 4 packets, 360 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
[root@localhost]# iptables -P FORWARD DROP
[root@localhost]# iptables --line-number -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        3   180 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
2        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
3        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
4      106 13581 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
5        0     0 DROP       all  --  *      *       192.168.50.90        0.0.0.0/0           

Chain FORWARD (policy DROP 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination   


删除规则与修改类似
先查该规则的序号
iptables --line-number -nvL 表
在使用-D参数:iptables -D 表 序号 		来进行删除规则