防火墙管理之iptables

1.iptables简介

        iptables是配置netfilter过滤功能的用户空间工具。netfilter才是防火墙真正的安全框架,netfilter位于内核空间。我们可以理解iptables为一种命令行工具。

        iptables处理数据包的定义方法有,放行(accept)、拒绝(reject)、丢弃(drop)等。

        netfilter才是真正的防火墙,它是内核的一部分,那它要起作用,就需要在内核中设置关卡,所有进出报文都要通过这些关卡,经过检查,符合放行条件的才能放行,反之阻止。

2.iptables的四表五链

       2.1 四表:filter、mangle、nat、raw,默认表为filter。

        filter:用来对数据包进行过滤;

        nat:用来修改数据包的IP地址、端口信息;网络地址转换

        mangle:用来修改数据包服务类型、生存周期,为数据包设置标记,实现流量整形、策略路由等。报文解析封装。

        raw:决定是否对数据包进行状态跟踪

        优先级关系:raw--mangle--nat--filter

        2.2五链:PREROUTING、INPUT、FORWARD、OUTPUT、POSTROUTING

        input:收到访问本机地址的数据包时,将应用此链中规则;

        output:本机向外发送数据包时,应用此链中规则;        

        forward:收到需要转发给其它地址的数据包时,应用此链中规则;

        prerouting:在对数据包做路由选择之前,将应用此链中规则;

        postrouting:在对数据包路由选择之后,将应用此链中的规则

        2.3链表关系(钩子关系)

链(钩子)
rawREROUTING、OUTPUT

mangle

REROUTING、INPUT、FORWARD、OUTPUT、POSTROUTING
natREROUTING、OUTPUT、POSTROUTING(centos7中还有INPUT)
filterINPUT、FORWARD、OUTPUT

        一般场景报文流向:

        到本机某进程的报文:PREROUTINGX->INPUT

        由本机转发的报文:PREROUTING->FORWARD->POSTROUTING

        由本机的某进程发出报文(一般为响应报文):OUTPUT->POSTROUTING

        2.4 iptables处理数据包流程

 

        2.5 iptables规则查询

        -t:跟表名

        -n:不解析IP地址

        -v:显示计数器信息、数据包数量和大小

        --line-numbers:显示规则序号

        -L:链名

# iptables -t filter -nvL --line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination     
1      382 28397 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
2        0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0       
3        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0       
4        2   104 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
5       15  1170 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

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 244 packets, 31201 bytes)
num   pkts bytes target     prot opt in     out     source               destination     

3.防火墙的启动

        centos7默认是firewalld而不是iptables,若要使用iptables,需要先禁用firewalld再安装iptables并启用

        禁用firewalld:

#systemctl status firewalld
#systemctl stop firewalld
#systemctl disable firewalld

        下次启用firewalld并让它自己启动:

#systemctl start firewalld
#systemctl enable firewalld

        安装iptables:

#yum install iptables-services
#systemctl start iptables
#systemctl enable iptables

        如果使用centos6,其默认为iptables,启动其服务

#services iptables start

4.增加规则

        4.1基本规则使用

        在指定表的指定链的尾部添加一条规则,-A+链名表示在对应链的末尾添加规则,(-t可以省略,默认为filter):

        iptables  -t  表名 -A  链名 匹配条件 -j 动作

#iptables -t filter -A INPUT -s 192.168.42.139 -j DROP

        在指定表的指定链的首部添加一条规则,-I+链名表示在对应链的开头添加规则:

        iptables -t 表名 -I 链名 匹配条件 -j 动作

#iptables -t filter -I INPUT -s 192.168.42.139 -j ACCEPT

        在指定表的指定链的指定位置添加一条规则:

        iptables -t 表名 -I 链名 规则序号 匹配条件 -j 动作

#iptables -t filter -I INPUT 3 -s 192.168.42.139 -j REJECT

       允许所有ssh访问

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

     仅允许B主机对A 主机的apache访问

iptables -t filter -A INPUT -s 192.168.42.137 -p tcp --dport 80 -j ACCEPT

         4.2处理动作

        ACCEPT:允许数据包通过。

        DROP:丢弃数据包,不给任何回应信息。

        REJECT:拒绝数据包通过,必要会给发送端一个响应信息,客户端刚请求就会收到拒绝信息。

        SNAT:源地址转换。

        MASQUERADE:SNAT特殊形式,适用于动态、临时会变的IP上。

        DNAT:目标地址转换。

        REDIRECT:在本机做端口映射。

        LOG:在/var/log/messages文件中记录日志信息,然后将数据包传递给下一条规则。即除了记录外不对数据包做任何其它操作,任然让下一条规则取匹配。

5.删除规则

        iptables -t 表名 -D 链名 规则序号

        删除INPUT中的第三条

# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
# iptables -t filter -D INPUT 3
 iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

        按照具体匹配条件与动作进行删除。

# iptables -t filter -A INPUT -p tcp --dport 80 -s 192.168.42.137 -j ACCEPT
# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
ACCEPT     tcp  --  192.168.42.137       0.0.0.0/0            tcp dpt:80

iptables -t filter -D INPUT -p tcp --dport 80 -s 192.168.42.137 -j ACCEPT
iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

        删除指定表指定链中的所有规则(慎用)

        iptables -t 表名 -F 链名

        删除所有规则

        iptables -F

6.修改规则

        修改规定表中指定链的指定规则,-R+链名

        iptables -t 表名 -R 链名 规则序号 规则原本匹配条件 -j 动作

#iptables -t filter -R INPUT 3 -S 192.168.42.137 -j ACCEPT

        设置指定表的指定链的默认策略:

        iptables -t 表名 -P 链名 动作

#iptables -t filter -P FORWARD ACCEPT

7.保存规则

        7.1 centos7上:

        保存:

# iptables-save > /etc/sysconfig/iptables

        重载(现有规则会被覆盖):

#iptables-restore < /etc/sysconfig/iptables

        7.2centos6上:

#services iptables save
#iptables-save > /etc/sysconfig/iptables
#iptables-restore < /etc/sysconfig/iptables

8.匹配条件

        当规则中同时存在多个匹配条件时,多个条件之间默认存在“与”的关系,即报文必须同时满足所有条件,才能被规则匹配。

        -s:用于匹配报文的源地址,可同时指定多个源地址,每个IP用逗号隔开,也可以指定为一个网段

# iptables -t filter -I OUTPUT -s 192.168.42.137,192.168.42.138 -j DROP
# iptables -t filter -I INPUT -s 192.168.42.0/24 -j ACCEPT
# iptables -t filter -I INPUT ! -s 192.168.42.0/24 -j ACCEPT

        -d:用于匹配报文的目标地址,可以同时指定多个目标地址,每个IP用逗号隔开,也可以指定为一个网段

# iptables -t filter -I OUTPUT -d 192.168.42.137,192.168.42.138 -j DROP
# iptables -t filter -I INPUT -d 192.168.42.0/24 -j ACCEPT
# iptables -t filter -I INPUT ! -d 192.168.42.0/24 -j ACCEPT

  9.iptables进阶使用

        9.1 limit限制流量

        -m limit --limit 1000/s设置最大平均匹配速率

        -m limit --limit-burst 15 设置一开始匹配的最大数据包数量

        -m limit --limit 5/m --limit-burst 15一开始能匹配的数量包数量为15个,每匹配到一个,limit-burst值减1,所以匹配到15个时,该值为0,以后没过12s,limit-burst值会加1,表示又能匹配到1个数据包

        --limit-burst:类比“令牌桶”算法,指定令牌桶中令牌的最大数量

        --limit:类比“令牌桶”算法,用于指定令牌桶中的新生令牌的频率,单位s、m

、h、d。

        注意:--limit-burst的值要比--limit的大,limit本身没有丢包的功能,所以需要第二条规则一起才能实现限速功能。

        9.2 time:在特定时间内匹配

        --monthdays day1[,day2] 在每个月的特定天匹配

        --timestart hh:mm:ss 在每天的特定时间开始匹配

        --timestop hh:mm:ss 在指定时间停止匹配

        --weekdays day1[,day2] 在每个星期的指定工作日匹配,值可以是1-7

#iptables -A INPUT -i eth0 -m time --weekdays 1,2,3 -j ACCEPT
#iptables -A INPUT -i eth0 -j DROP 
#iptables -I FORWARD -s 192.168.42.139 -m time --timestart 16:10 --timestop 18:10 -j ACCEPT
#iptables -I FORWARD -d 192.168.42.139 -m time --timestart 16:10 --timestop 18:10 -j ACCEPT

        9.3 state:状态限制

        -m --state NEW:第一次连接

        RELATED:相关联的连接

        ESTABLISHED:已连接

        INVALID:无法识别的

        UNTRACKED:无法追踪的

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

        9.4 connlimit:连接限制

        限制单个IP地址连接服务的数量,当不指定IP地址时代表每个IP地址

        --connlimit-above:限制上限

        --connlimit-mask:按照网段做限制

#iptables -t filter -A INPUT -p tcp -m tcp --dport 22 -m connlimit --connlimit-above 2 -j REJECT
# iptables -t filter -A INPUT -p tcp -m tcp --dport 22 -m connlimit --connlimit-above 2 --connlimit-mask 24 -j REJECT

       9.5 string模块

        --algo:指定对应的匹配算法,可用算法bm、kmp

        --string:指定需要匹配的字符串

#iptables -t filter -I INPUT -p tcp --sport 80 -m string --algo bm --string "OOXX" -j REJECT

       9.6 iprange 

        使用iprange模块,可以指定一段连续的IP地址范围,用于匹配报文的源地址或者目标地址

        --src-range:源地址范围

        --dst-range:目标地址范围

#iptables -t filter -A INPUT -m iprange --src-range 192.168.42.1-192.168.42.130 -j DROP
#iptables -t filter -A INPUT -m iprange --dst-range 192.168.42.1-192.168.42.130 -j DROP
#iptables -t filter -A INPUT -m iprange !--src-range 192.168.42.1-192.168.42.130 -j DROP

10.白名单和黑名单

        报文在经过iptables的链时会匹配链中规则,匹配到规则执行对应动作,若链中规则都无法匹配到当前报文,则使用链的默认策略,链的默认策略通常为ACCEPT或DROP。

        当链的默认策略为ACCEPT时,(黑名单机制)对应的链中没有任何规则,就接受所有报文;对应链中存在规则,但规则没匹配到报文,报文还是会被接受。

        当链的默认策略时DROP时,(白名单机制)对应链中没有配置任何规则,就表示拒绝所有报文,对应链存在规则,但这些规则没有匹配到报文,报文还是会被拒绝

        10.1设置白名单

#iptables -A INPUT -s 192.168.42.147 -j ACCEPT

          设置默认拒绝(最好不要用)

iptables -P INPUT DROP

        10.2设置黑名单

#iptables -A INPUT -p tcp -s 192.168.42.147 -j DROP

        设置拒绝所有IP访问(慎用)

#iptables -A INPUT -p tcp -j DROP

        

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值