RHEL6基础四十一之selinux和iptables基础

SELinux

简介:

   SELinux 全称 Security Enhanced Linux (安全强化 Linux),是 MAC (Mandatory Access Control,强制访问控制系统)的一个实现,目的在于明确的指明某个进程可以访问哪些资源(文件、网络端口等)。强制访问控制系统的用途在于增强系统抵御 0-Day ***(利用尚未公开的漏洞实现的***行为)的能力。所以它不是网络防火墙或 ACL 的替代品,在用途上也不重复。举例来说,系统上的 Apache 被发现存在一个漏洞,使得某远程用户可以访问系统上的敏感文件(比如 /etc/passwd 来获得系统已存在用户),而修复该安全漏洞的 Apache 更新补丁尚未释出。此时 SELinux 可以起到弥补该漏洞的缓和方案。因为 /etc/passwd 不具有 Apache 的访问标签,所以 Apache 对于 /etc/passwd 的访问会被 SELinux 阻止。

特点:

  • 控制策略是可查询而非程序不可见的。

  • 可以热更改策略而无需重启或者停止服务。

  • 可以从进程初始化、继承和程序执行三个方面通过策略进行控制。

  • 控制范围覆盖文件系统、目录、文件、文件启动描述符、端口、消息接口和网络接口。

SELinux 运行状态:

[root@justin ~]# setenforce
usage:  setenforce [ Enforcing | Permissive | 1 | 0 ]
[root@justin ~]#

返回结果有两种:Enforcing和Permissive ,Permissive 代表仅记录安全警告但不阻止可疑行为,Enforcing 代表记录警告且阻止可疑行为。还有一种状态为Disabled,Disabled 代表 SELinux 被禁用

查看SELinux 运行状态:

[root@justin ~]# getenforce
Enforcing
[root@justin ~]#

RHEL 默认设置为 Enforcing。或者直接sestatus查看状态

[root@justin home]# sestatus
SELinux status:                 Enforcing

设置SELinux 运行状态:

[root@justin ~]# setenforce Permissive
[root@justin ~]# getenforce
Permissive
[root@justin ~]# setenforce Enforcing
[root@justin ~]# getenforce
Enforcing
[root@justin ~]# setenforce0
[root@justin ~]# getenforce
Permissive
[root@justin ~]#

该命令可以立刻改变 SELinux 运行状态,在 Enforcing 和 Permissive 之间切换,结果保持至关机。若是想要永久变更系统 SELinux 运行环境,可以通过更改配置文件 /etc/sysconfig/selinux 、/etc/selinux/config 实现。注意当从 Disabled 切换到 Permissive 或者 Enforcing 模式后需要重启计算机。若要判断某一程序无法运行是否是selinux所致可以先将其设置为permisive再运行,如仍无法运行表明与其无关

SELinux 运行策略:

103433554.png

通过改变变量 SELINUXTYPE 的值实现修改策略,targeted 代表仅针对预制的几种网络服务和访问请求使用 SELinux 保护,mls 代表所有网络服务和访问请求都要经过 SELinux。RHEL 默认设置为 targeted,包含了对几乎所有常见网络服务的 SELinux 策略配置,已经默认安装并且可以无需修改直接使用。


IPTables

iptables结构:

Rules(规则)-> Chains(链) -> Tables ->iptables

iptables由Tables(表)组成,Tables(表)由Chains(链)组成,Chains(链)由Rules(规则)组成。iptables表从上到下匹配,头一个规则如果匹配了,下一个相关规则无效, 表和链中的规则是从上到下都顺序来匹配的,前面的优先于后面的。Rules(规则)包括一个条件和一个目标(target),如果满足条件,就执行目标(target)中的规则或者特定值,如果不满足条件,就判断下一条Rules。

iptables相关文件路径:

   启动脚本:/etc/rc.d/init.d/iptables ;规则文件:/etc/sysconfig/iptables ;配置文件:/etc/sysconfig/iptables-config

iptables命令书写原则:

   链名  必须  大写
   表名  必须  小写
   动作  必须  大写
   匹配  必须  小写

iptables命令格式:

iptables  [-t 表名]  管理选项  [链名]  [条件匹配]  [-j 目标动作或跳转]

表选项用于指定命令应用于哪个iptables内置表,iptables内置包括filter表、nat表、mangle表和Raw表。

iptables的表与链:

wKioL1jUu8CAjL3aAABWzgCE2IQ122.png

规则表的先后顺序:raw→mangle→nat→filter

规则链的先后顺序:

                  入站顺序:PREROUTING→INPUT

                  出站顺序:OUTPUT→POSTROUTING

                  转发顺序:PREROUTING→FORWARD→POSTROUTING

 iptables的基本参数:

参数     作用

-P     设置默认策略:iptables -P INPUT (DROP|ACCEPT)

-F     清空规则链

-L     查看规则链

-A     在规则链的末尾加入新规则

-I num     在规则链的头部加入新规则

-D num     删除某一条规则

-s     匹配来源地址IP/MASK,加叹号"!"表示除这个IP外。

-d     匹配目标地址

-i     网卡名称 匹配从这块网卡流入的数据

-o     网卡名称 匹配从这块网卡流出的数据

-p     匹配协议,如tcp,udp,icmp

--dport num     匹配目标端口号

--sport num     匹配来源端口号

                                 

1. Filter表------ 过滤作用,iptables的默认表。

Filter表具有以下三种内建链:

  • INPUT链 – 处理来自外部的数据。匹配目的ip是本机的数据包

  • OUTPUT链 – 处理向外发送的数据。匹配从本机出去的数据包

  • FORWARD链 – 将数据转发到本机的其他网卡设备上。匹配通过本机的数据包,从外部来,通过本机,又转发到外部

2. NAT表------做网络地址转换的.network address translator

NAT表有三种内建链:

  • PREROUTING链 – 处理刚到达本机并在路由转发前的数据包。也就是把从外来的访问重定向到其他的机子上,比如内部SERVER,或者DMZ。它会转换数据包中的目标IP地址(destination ip address),通常用于DNAT(destination NAT)。用于修改目的地址 destination nat, --to-destination ip:port;因为路由时只检查数据包的目的ip地址,所以必须在路由之前就进行目的PREROUTING DNAT;
     
             系统先PREROUTING DNAT翻译——>再过滤(FORWARD)——>最后路由。
     
             路由和过滤(FORWARD)中match 的目的地址,都是针对被PREROUTING DNAT之后的。

  • eg、一个FTP SERVER从内部192.168.100.125:21映射到216.94.87.37:2121的例子

  • iptables -t nat -A PREROUTING -p tcp -d 216.94.87.37 --dport 2121 -j DNAT --to-destination 192.168.100.125:21
  • POSTROUTING链 – 处理即将离开本机的数据包。也就是在路由以后在执行该链中的规则。
     
               系统先路由——>再过滤(FORWARD)——>最后才进行POSTROUTING SNAT地址翻译,其match 源地址是翻译前的。它会转换数据包中的源IP地址(source ip address),通常用于SNAT(source NAT)。用于修改源地址 source nat,--to-source ip:port

  • iptables -t nat -A POSTROUTING -p tcp -s 192.168.100.125 --sport 21 -j SNAT --to-source 216.94.87.37:2121
  • iptables -t nat -A PREROUTING -s 192.168.1.0/24 -p tcp --dport 80 -j REDIRECT --to-ports 8080
  • OUTPUT链 – 处理本机产生的数据包。

DNAT------destination network address translation,目的网络地址转换,重写包的目的IP地址,即目标网络地址转换

only valid in  PREROUTING

--to-destination ipaddr[-ipaddr][:port-port]

实例说明:有个web服务器放在内网配置内网ip,前端有个防火墙配置公网ip,互联网上的访问者使用公网ip来访问这个网站,当访问的时候,客户端发出一个数据包,这个数据包的报头里边,目标地址写的是防火墙的公网ip,防火墙会把这个数据包的报头改写一次,将目标地址改写成web服务器的内网ip,然后再把这个数据包发送到内网的web服务器上,这样,数据包就穿透了防火墙,并从公网ip变成了一个对内网地址的访问了,即DNAT,基于目标的网络地址转换

外部接口ip:210.83.2.206
web服务器 : ip 192.168.1.4

iptables -t nat -A PREROUTING -d 210.83.2.206 -p tcp --dport 80 -j DNAT --to 192.168.1.4

SNAT------source network address translation,即源地址目标转换

实例说明:多个PC机使用ADSL路由器共享上网,每个PC机都配置了内网IP,PC机访问外部网络的时候,路由器将数据包的报头中的源地址替换成路由器的ip,当外部网络的服务器比如网站web服务器接到访问请求的时候他的日志记录下来的是路由器的ip地址,而不是pc机的内网ip,这是因为,这个服务器收到的数据包的报头里边的“源地址”,已经被替换了,即SNAT,基于源地址的地址转换


案例:将访问10.10.2.166:80的数据包转发到10.10.2.32:21,即客户访问10.10.2.166的80端口直接跳转到10.10.2.32的21端口,

步骤:

Port_Forward:10.10.2.166

1、开启Port_Forward服务器的转发功能

[root@Port_Forward sysconfig]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1  
[root@Port_Forward sysconfig]# sysctl -p
error: "net.bridge.bridge-nf-call-ip6tables" is an unknown key
error: "net.bridge.bridge-nf-call-iptables" is an unknown key
error: "net.bridge.bridge-nf-call-arptables" is an unknown key
[root@Port_Forward sysconfig]# modprobe bridge
[root@Port_Forward sysconfig]# sysctl -p

2、配置iptables

[root@Port_Forward sysconfig]# iptables -t nat -A PREROUTING -d 10.10.2.166 -p tcp --dport 80 -j DNAT --to-destination 10.10.2.32:21
[root@Port_Forward sysconfig]# iptables -t nat -A POSTROUTING -s0.0.0.0/0 -p tcp --dport 21 -j SNAT --to-source 10.10.2.166
[root@Port_Forward sysconfig]#   iptables -t nat -A OUTPUT -p tcp --dport 80 -j DNAT --to 10.10.2.32:21
[root@Port_Forward sysconfig]# iptables -L -t nat
[root@Port_Forward sysconfig]# /etc/init.d/iptables save
[root@Port_Forward ~]# uname -r
2.6.32-431.el6.x86_64
[root@Port_Forward ~]# cat /etc/issue
CentOS release 6.5 (Final)
Kernel \r on an \m
[root@Port_Forward ~]#

以上配置经测试成功,如果不加iptables -t nat -A OUTPUT -p tcp --dport 80 -j DNAT --to 10.10.2.32:21这条规则,在本机telnet 10.10.2.166 80是不通的,外网访问需要经过PREROUTING链,但是localhost以及10.10.2.166本机ip地址不经过该链,因此需要用OUTPUT。

wKioL1dg-riAynEBAACQPtlWeKA975.png

如何查看iptables关于nat的日志

[root@Port_Forward ~]# ptables -t mangle -A POSTROUTING -j LOG --log-level 4 --log-prefix "OUT PACKETS:"
[root@Port_Forward ~]# vim /etc/syslog.conf     #/Centos6为/etc/rsyslog.conf
kern.warning /var/log/iptables    #文末添加此行
[root@Port_Forward ~]# /etc/init.d/rsyslog restart

kern.=notice  /var/log/iptables  选项(kern表示内核信息; .=表示匹配; notice为消息 )

所有通过iptables的包就会被记录在/var/log/iptables里面,默认在/var/log/messages文件中。

- log-level 4: 记录的级别. 级别4为警告(warning).对于为什么日志级别为4,可能参看#man syslog的8种级别,这里面的日志级别要与iptables的--log-level 的日志级别要对应.

[root@localhost ~]# man syslog
 The loglevel
       #define KERN_EMERG    "<0>"  /* system is unusable               */
EMERG 致命级,紧急 - 系统无法使用。  "Child cannot open lock file. Exiting"
       #define KERN_ALERT    "<1>"  /* action must be taken immediately */
ALERT 警戒级,必须立即采取措施。 "getpwuid: couldn't determine user name from uid"
       #define KERN_CRIT     "<2>"  /* critical conditions              */
crit 临界级,致命情况。 "socket: Failed to get a socket, exiting child"       
       #define KERN_ERR      "<3>"  /* error conditions                 */
error  错误级,错误情况。 "Premature end of script headers"       
       #define KERN_WARNING  "<4>"  /* warning conditions               */
warn  告警级,警告情况。 "child process 1234 did not exit, sending another SIGHUP"      
       #define KERN_NOTICE   "<5>"  /* normal but significant condition */
notice 注意级,一般重要情况。 "httpd: caught SIGBUS, attempting to dump core in ..."   
       #define KERN_INFO     "<6>"  /* informational                    */
info 通知级,普通信息。 "Server seems busy, (you may need to increase StartServers, or Min/MaxSpareServers)..."       
       #define KERN_DEBUG    "<7>"  /* debug-level messages             */
debug 调试级,出错级别信息 Opening config file ..."   
当指定了特定级别时,所有级别高于它的信息也会同时报告。比如说,当指定了LogLevel info时,所有 notice和warn级别的信息也会被记录。


- log-prefix ‘OUT PACKETS:’: 这里定义了在日志输出信息前加上OUT PACKETS:前缀. 

eg:举一个例子, 丢弃和记录所有来自IP地址65.55.11.2的连接信息到/var/log/iptables.log文件.

[root@localhost ~]# iptables -A INPUT -s 64.55.11.2 -m limit --limit 5/m --limit-burst 7 -j LOG –log-prefix ‘** HACKERS **’ --log-level 4
[root@localhost ~]# iptables -A INPUT -s 64.55.11.2 -j DROP
[root@localhost ~]# vim /etc/rsyslog.conf
kern.warning /var/log/iptables.log
[root@localhost ~]#  /etc/init.d/rsyslog restart

确认所要增加到LOG里面的数据包在之前没有丢弃过;先经过LOG,再经过DROP,不然系统日志无法记录日志信息。

[root@localhost ~]# iptables -A INPUT -p tcp -d 192.168.0.253 --dport 22 -j LOG --log-level 5 --log-prefix "IPTABLES:"
[root@localhost ~]# 
[root@localhost ~]# vim /etc/rsyslog.conf
kern.=notice /var/log/firewall.log  选项(kern表示内核信息;.=表示匹配;notice为消息 )
[root@localhost ~]#  /etc/init.d/rsyslog restart
[root@localhost ~]#

snat的出口ip的地址范围可以是一个,也可以是多个。

如下命令表示把所有10.8.0.0网段的数据包SNAT成192.168.5.3/192.168.5.4/192.168.5.5等几个ip然后发出去

[root@localhost ~]# iptables -t nat -A POSTROUTING -s 10.8.0.0/255.255.255.0 -o eth0 -j SNAT --to-source 192.168.5.3-192.168.5.5

但是,对于SNAT,不管是几个地址,必须明确的指定要SNAT的ip    

假如当前系统用的是ADSL动态拨号方式,那么每次拨号,出口ip192.168.5.3都会改变   而且改变的幅度很大,不一定是192.168.5.3到192.168.5.5范围内的地址   

这个时候如果按照现在的方式来配置iptables就会出现问题了    因为每次拨号后,服务器地址都会变化,而iptables规则内的ip是不会随着自动变化的   每次地址变化后都必须手工修改一次iptables,把规则里边的固定ip改成新的ip  这样是非常不好用的   

 MASQUERADE就是针对这种场景而设计的,他的作用是,从服务器的网卡上,自动获取当前ip地址来做NAT   

比如下边的命令:    

iptables -t nat -A POSTROUTING -s 10.8.0.0/255.255.255.0 -o eth0 -j MASQUERADE   

如此配置的话,不用指定SNAT的目标ip了    

不管现在eth0的出口获得了怎样的动态ip,MASQUERADE会自动读取eth0现在的ip地址然后做SNAT出去   

 这样就实现了很好的动态SNAT地址转换  


3. Mangle表------指定如何处理数据包,它能改变TCP头中的QoS位。

Mangle表具有5个内建链:PREROUTING、OUTPUT、FORWARD、INPUT、POSTROUTING

4. Raw表------用于处理异常

Raw具有2个内建链:PREROUTING chain、OUTPUT chain

目标参数-j:

-j指定了当与规则(Rule)匹配时如何处理数据包,可能的值是ACCEPT, DROP, QUEUE, RETURN,还可以指定其他链(Chain)作为目标

ACCEPT – 允许防火墙接收数据包
DROP – 防火墙丢弃包
QUEUE – 防火墙将数据包移交到用户空间
RETURN – 防火墙停止执行当前链中的后续Rules,并返回到调用链(the calling chain)中

REDIRECT -将数据包重新转向到本机或另一台主机的某个端口,通常用功能实现透明代理或对外开放内网某些服务
SNAT -源地址转换,即改变数据包的源地址
DNAT -目标地址转换,即改变数据包的目的地址

条件规则参数:

-A或—append <链名> 在规则列表的最后增加1条规则
-I或--insert <链名> 在指定的位置插入1条规则,根据给出的规则序号向所选链中插入规则。如果序号为1,规则会被插入链的头部,默认序号就是1。其实跟-A的效果一样;

-D或--delete <链名> 从规则列表中删除1条规则

[root@localhost ~]# iptables -L --line-number
7    ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:http 
8    ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:5901 
9    ACCEPT     udp  --  anywhere             anywhere            state NEW udp dpt:5901 
10   REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited 
[root@localhost ~]# iptables -D INPUT 8    #删除编号为8的规则
[root@localhost ~]# iptables -L --line-number
6    ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:zabbix-agent 
7    ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:http 
8    ACCEPT     udp  --  anywhere             anywhere            state NEW udp dpt:5901 
9    REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited

或者使用-D替换-A删除此条规则,删除此条规则时直接用 -D 来代替- A, 其余的都不变即可

[root@Port_Forward ~]# iptables -t nat -A PREROUTING -p tcp -d 10.10.2.166 --dport 9968 -j DNAT --to-destination 192.168.100.168:6201
[root@Port_Forward ~]# iptables -t nat -D PREROUTING -p tcp -d 10.10.2.166 --dport 9968 -j DNAT --to-destination 192.168.100.168:6201


-R或--replace <链名> 替换规则列表中的某条规则
-P或--policy <链名> 定义默认策略
-F或--flush <链名> 删除表中所有规则,如果没有指定链,则清空指定表中的所有链。如果什么都没有指定,就清空默认表所有的链。
-Z或--zero <链名> 将表中数据包计数器和流量计数器归零

[root@localhost ~]# iptables -nvL
......
 pkts bytes target     prot opt in     out     source               destination         
    3   252 DROP       icmp --  *      *       0.0.0.0/0            10.15.98.190        
[root@localhost ~]# iptables -Z
[root@localhost ~]# iptables -nvL
......       
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       icmp --  *      *       0.0.0.0/0            10.15.98.190        
[root@localhost ~]#

-L或--list <链名> 查看当前表的所有规则,默认查看的是filter表,如果要查看NAT表,可以加上-t NAT参数

-n 不对ip地址进行反查,加上这个参数显示速度会快很多

-v 输出详细信息,包含通过该规则的数据包数量,总字节数及相应的网络接口

–line-number 显示规则的序列号,这个参数在删除或修改规则时会用到

[root@Port_Forward ~]# iptables -L INPUT --line-number
[root@Port_Forward ~]# iptables -L
[root@Port_Forward ~]# iptables -L -t nat
[root@Port_Forward ~]# iptables -nvL -t nat --line-number


描述规则参数:

-p 协议(protocol)

  • 指定规则的协议,如tcp, udp, icmp等,可以使用all来指定所有协议。如果不指定-p参数,则默认是all值。可以使用协议名(如tcp),或者是协议值(比如6代表tcp)来指定协议。映射关系请查看/etc/protocols还可以使用–protocol参数代替-p参数,如-p tcp

-s 源地址(source)

  • 指定数据包的源地址,参数可以使IP地址、网络地址、主机名,例如:-s 192.168.1.101指定IP地址,-s 192.168.1.10/24指定网络地址,如果不指定-s参数,就代表所有地址,还可以使用–src或者–source

    [root@justin ~]# iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT

-d 目的地址(destination)

  • 指定目的地址,参数和-s相同,还可以使用–dst或者–destination如:... -d 192.168.1.101

  • [root@localhost ~]# iptables -A OUTPUT -p tcp -d 192.168.1.101 -j ACCEPT

-i 输入接口(input interface)

  • -i代表输入接口(input interface),-i指定了要处理来自哪个接口的数据包,这些数据包即将进入INPUT, FORWARD, PREROUTE链

例如:-i eth0指定了要处理经由eth0进入的数据包,如果不指定-i参数,那么将处理进入所有接口的数据包,如果出现! -i eth0,那么将处理所有经由eth0以外的接口进入的数据包,如果出现-i eth+,那么将处理所有经由eth开头的接口进入的数据包,还可以使用–in-interface参数

-o 输出(out interface)

  • -o代表”output interface”,-o指定了数据包由哪个接口输出,这些数据包即将进入FORWARD, OUTPUT, POSTROUTING链,如果不指定-o选项,那么系统上的所有接口都可以作为输出接口,如果出现! -o eth0,那么将从eth0以外的接口输出,如果出现-i eth+,那么将仅从eth开头的接口输出,还可以使用–out-interface参数

-m module_name

引用模块的显示扩展,默认是隐含扩展,不要使用 -m

-m multiport:

      --sport
     --dport
     --ports

表示启用多端口扩展,不启用该扩展就只能使用连续的端口如21:88,之后我们就可以启用比如 --dports21,23,80。可以使用扩展模块来进行数据包的匹配,语法为 -m module_name, 如:-m tcp 的意思是使用 tcp 扩展模块的功能 (tcp扩展模块提供了 --dport, --tcp-flags, --sync等功能),

[root@justin ~]# iptables -A INPUT -p tcp -m multiport --dports 110,80,25,445,1863,5222 -j ACCEPT

-m state

        --state :

指定要匹配数据包的状态,有4种状态:INVALID、ESTABLISHED、NEW和RELATED。这个匹配操作必须由-mstate明确指定才能使用

[root@justin ~]# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

允许任何主机状态为NEW,ESTABLISHED的状态连接

[root@justin ~]# iptables -A INPUT -s 0.0.0.0/0.0.0.0 -d 192.168.80.139 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
[root@justin ~]# iptables -A INPUT -d 192.168.80.140 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT

当写成0.0.0.0/0.0.0.0 表示任意地址的时候是可以省略

允许本机的ESTABLISHED状态出去,为了防止有反弹时***,让服务器主动向外发起请求

[root@justin ~]# iptables -A OUTPUT -s 192.168.80.140 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

现在在本机ping 127.0.0.1,发现时不同的,所以要允许本机

[root@justin ~]# iptables -I INPUT 1 -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT
[root@justin ~]# iptables -I OUTPUT 1 -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT

 -m recent

    –name #设定列表名称,默认DEFAULT。
    –rsource #源地址,此为默认。
    –rdest #目的地址
    –seconds #指定时间内
    –hitcount #命中次数
    –set #将地址添加进列表,并更新信息,包含地址加入的时间戳。
    –rcheck #检查地址是否在列表,以第一个匹配开始计算时间。
    –update #和rcheck类似,以最后一个匹配计算时间。
    –remove #在列表里删除相应地址,后跟列表名称及地址

60秒10个新连接,超过丢弃数据包。 

[root@justin ~]# iptables -A INPUT -p tcp --dport 80 --syn -m recent --name webpool --rcheck --seconds 60 --hitcount 10 -j DROP

60秒10个新连接,超过记录日志。 

[root@justin ~]# iptables -A INPUT -p tcp --dport 80 --syn -m recent --name webpool --rcheck --seconds 60 --hitcount 10 -j LOG --log-prefix 'DDOS:' --log-ip-options

控制单个IP在一定的时间(比如60秒)内允许新建立的连接数

[root@justin ~]# iptables -A INPUT -p tcp --dport 80 -m recent --name BAD_HTTP_ACCESS --update --seconds 60 --hitcount 30 -j REJECT
[root@justin ~]# iptables -A INPUT -p tcp --dport 80 -m recent --name BAD_HTTP_ACCESS --set -j ACCEPT

-m iprange 指定IP段
      --src-range ip-ip
      --dst-range ip-ip

指定192.168.80.130-192.168.80.150 可以访问本机的23端口

[root@justin ~]# iptables -A INPUT -d 192.168.80.140 -m iprange --src-range 192.168.80.130-192.168.80.150 -p tcp --dport 23 -m state --state NEW -j ACCEPT


-m connlimit 连接限定
      --comlimit-above # 限定最大连接个数

[root@justin ~]# iptables -I INPUT 2 -d 192.168.80.140 -p tcp --dport 22 -m state --state NEW -m connlimit ! --connlimit-above 2 -j ACCEPT

这里connlimit ! --connlimit-above 2 -j ACCEPT 表示只允许SSH本机2次,connlimit --connlimit-above 2 -j DROP 表示超过2次后拒绝,这里因为默认权限是拒绝的所以用!取反

[root@justin ~]# iptables -I INPUT -p tcp --dport 80 -m connlimit --connlimit-above 10 -j DROP

限制与80端口连接的IP最大连接数为10

-m limit 现在连接速率,也就是限定匹配数据包的个数
      --limit  指定速率
      --limit-burst # 峰值速率,最大限定

[root@justin ~]# iptables -A INPUT -d 192.168.80.140 -p tcp --dport 80 -m state --state  NEW -m limit --limit 1/sec --limit-burst 3 -j ACCEPT
[root@justin ~]# iptables -A INPUT -d 192.168.80.140 -p tcp --dport 80 -m state --state  NEW -m limit --limit 10/m --limit-burst 3 -j ACCEPT

/second /minute /hour /day 

表示对web服务的新连接请求,平均每秒只允许1个,最多一次进3个

iptables -A INPUT -p icmp -m limit --limit 3/m --limit-burst 5  -j ACCEPT

最开始有5个包可以通过,然后每隔一定时间会允许一个包通过

iptables -A INPUT -p icmp -m limit --limit 6/m --limit-burst 5 -j ACCEPT
iptables -P INPUT DROP

然后从另一部主机上ping这部主机,就会发生如下的现象:

首先我们能看到前四个包的回应都非常正常,然后从第五个包开始,我们每10秒能收到一个正常的回应。这是因为我们设定了单位时间(在这里是每分钟)内允许通过的数据包的个数是每分钟6个,也即每10秒钟一个;其次我们又设定了事件触发阀值为5,所以我们的前四个包都是正常的,只是从第五个包开始,限制规则开始生效,故只能每10秒收到一个正常回应。

假设我们停止ping,30秒后又开始ping,这时的现象是:

前两个包是正常的,从第三个包开始丢包,这是因为在这里我的允许一个包通过的周期是10秒,如果在一个周期内系统没有收到符合条件的包,系统的触发值就会恢复1,所以如果我们30秒内没有符合条件的包通过,系统的触发值就会恢复到3,如果5个周期内都没有符合条件的包通过,系统都触发值就会完全恢复。

-m comment --comment “要添加的注释文字”  为新的iptables规则添加注释

[root@localhost ~]# iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -m comment --comment "Tomcat port" -j ACCEPT
[root@localhost ~]# iptables -nL
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:8080 /* Tomcat port */ 
[root@localhost ~]#

-m string 按字符串限定

      --algo bm|kmp  指定算法bm或kmp
      --string "STRING" 指定字符串本身

-–sport 源端口(source port)跟-p一起使用------针对 -p tcp 或者 -p udp

  • 缺省情况下,将匹配所有端口,可以指定端口号或者端口名称,例如”–sport 22″与”–sport ssh”。/etc/services文件描述了上述映射关系。从性能上讲,使用端口号更好,使用冒号可以匹配端口范围,如”–sport 22:100″

–-dport 目的端口(destination port)跟-p一起使用-----针对-p tcp 或者 -p udp

  • 参数和–-sport类似,还可以使用”–destination-port”如:... -p udp --dport 53

-–tcp-flags ------TCP标志 针对-p tcp

  • 可以指定由逗号分隔的多个参数,有效值可以是:SYN, ACK, FIN, RST, URG, PSH,可以使用ALL或者NONE

-–icmp-type------ICMP类型 和-p icmp一起使用

[root@localhost ~]# iptables -I INPUT -p icmp --icmp-type 8 -j DROP
[root@localhost ~]#
  • --icmp-type 这个选项是要跟-p icmp 一起使用的,后面指定类型编号。

wKioL1dFBJihEkXaAACXlXZiQx4259.png

wKioL1dFBJjQhN3sAABR2EL4Iso970.png


设定预设规则

[root@Zabbix_server ~]# iptables -P INPUT DROP
[root@Zabbix_server ~]# iptables -P OUTPUT ACCEPT
[root@Zabbix_server ~]# iptables -P FORWARD DROP

设置默认规则后当未在链(INPUT,FORWARD、FORW)里设置规则时,数据包就会采取预设规则处理,如果是远程设置上面规则且未事先允许22端口ACCEPT,设置完远程就会断掉,此时需要到本地开启22端口

[root@Zabbix_server ~]# iptables -A INPUT -p tcp --dport 22 -j ACCETP

如果上面的OUTPUT也设置成了DROP也需要开启

[root@Zabbix_server ~]# iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT

其他端口类似

iptables常用命令:

查看现有iptables规则:

[root@justin ~]# iptables --list   ;默认filter表
Chain INPUT (policy ACCEPT)
target     prot opt source               destination    
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:5801
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:5901
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination    
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination    
[root@justin ~]# iptables -t nat --list
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination    
Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination    
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination    
[root@justin ~]# iptables -L FORWARD ;查看filter表FORWARD链规则
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination    
DROP       all  --  anywhere             10.15.72.74    
[root@justin ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination     
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:5801
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:5901
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination     
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination     
[root@justin ~]# 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:5801
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:5901
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination     
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination     
[root@justin ~]#

v:显示详细信息,包括每条规则的匹配包数量和匹配字节数
x:在 v 的基础上,禁止自动单位换算(K、M)

n:只显示IP地址和端口号,不将ip解析为域名

使用iptables -L -n --line-number 可以查看到防火墙规则的顺序

[root@localhost libexec]# iptables -L -n --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
2    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
3    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
4    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
5    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

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

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         
[root@localhost libexec]#

在num为5的位置插入一条规则允许5666端口

[root@localhost libexec]# /sbin/iptables -I INPUT 5 -p tcp --dport 5666 -j ACCEPT
[root@localhost libexec]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
[root@localhost libexec]# iptables -L -n --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
2    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
3    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
4    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
5    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:5666 
6    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

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

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         
[root@localhost libexec]#

插入一条规则开放5666到6000端口

[root@localhost ~]# /sbin/iptables -I INPUT 5 -p tcp --dport 5666:6000 -j ACCEPT

清除现有iptables规则:

[root@justin ~]# iptables -F
[root@justin ~]# iptables -L   ;查看现有iptables规则
Chain INPUT (policy ACCEPT)
target     prot opt source               destination    
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination    
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination    
[root@justin ~]# iptables -t nat -F PREROUTING  ;清除nat表PREROUTING链中规则
[root@justin ~]#

-F    清除预设表filter中的所有规则链的规则

-X    清除预设表filter中使用者自定链中的规则

添加iptables规则:

[root@justin ~]# iptables -I INPUT -p tcp --dport 5901 -j ACCEPT
[root@justin ~]# iptables -I INPUT -p tcp --dport 5801 -j ACCEPT

保存iptables规则:

[root@justin ~]# /etc/rc.d/init.d/iptables save
或者
[root@justin ~]# service iptables save
iptables:将防火墙规则保存到 /etc/sysconfig/iptables:     [确定]
[root@justin ~]# service iptables restart

iptables实例:

1、禁止访问某网站

添加iptables规则禁止用户访问IP地址为10.15.72.74的网站,然后查看filter表的FORWARD链规则列表。

[root@justin ~]# iptables -t filter -I FORWARD -d 10.15.72.74 -j DROP
[root@justin ~]# iptables -L FORWARD
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination    
DROP       all  --  anywhere             10.15.72.74    
[root@justin ~]#

2、禁止某主机上网

添加iptables规则禁止192.168.2.0子网里所有的客户机上网,然后查看filter表的FORWARD链规则列表。

[root@justin ~]# iptables -I FORWARD -s 192.168.2.0/24 -j DROP
[root@justin ~]# iptables -L FORWARD
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination    
DROP       all  --  192.168.2.0/24       anywhere       
DROP       all  --  anywhere             10.15.72.74    
[root@justin ~]#

3、禁止主机访问某些服务

禁止192.168.1.0子网里所有的客户机使用FTP协议下载(即封闭TCP协议的21端口),然后查看filter表的FORWARD链规则列表。

[root@justin ~]# iptables -I FORWARD -s 192.168.1.0/24 -p tcp --dport 21 -j DROP
[root@justin ~]# iptables -L FORWARD
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination    
DROP       tcp  --  192.168.1.0/24       anywhere            tcp dpt:ftp
DROP       all  --  192.168.2.0/24       anywhere       
DROP       all  --  anywhere             10.15.72.74    
[root@justin ~]#

4、强制访问指定的站点

强制所有的客户机访问10.15.72.75这台Web服务器,然后查看nat表的PREROUTING链规则列表。

[root@justin ~]# echo "1" /proc/sys/net/ipv4/ip_forward 
[root@justin ~]# iptables -t nat -I PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to 10.15.72.75:80
[root@justin ~]# iptables -L FORWARD

第一个命令涉及到了内核参数相关的配置文件,它的目的是为了打开路由转发功能,否则无法实现我们的应用。否则无法实现我们的应用。

发布内网192.168.16.200主机的Web服务,Internet用户通过访问ppp0的IP地址即可访问该主机的Web服务

[root@justin ~]# iptables -t nat -I PREROUTING -i ppp0 -p tcp --dport 80 -j DNAT --to-destination 192.168.16.200:80
[root@justin ~]# iptables -t nat -L PREROUTING
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination    
DNAT       tcp  --  anywhere             anywhere            tcp dpt:http to:192.168.16.200:80
DNAT       tcp  --  anywhere             anywhere            tcp dpt:http to:10.15.72.75:80
[root@justin ~]#

5、丢弃坏的TCP包

[root@Zabbix_server ~]# iptables -A FORWARD -p TCP ! --syn -m state --state NEW -j DROP

6、处理IP碎片数量,防止***,允许每秒100个

[root@Zabbix_server ~]# iptables -A FORWARD -f -m limit --limit 100/s --limit-burst 100 -j ACCEPT

7、设置ICMP包过滤,允许每秒1个包,限制触发条件是10个包.

[root@Zabbix_server ~]# iptables -A FORWARD -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT

8、禁ping

[root@Zabbix ~]# echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all
[root@Zabbix ~]# sysctl -p

或者

9、禁止UDP发包

有的网站被恶意放上UDP发包工具***别人 导致流量大量流失,一般服务器只有DNS使用udp协议,其它则可禁用UDP数据包外出,

[root@localhost ~]# iptables -I OUTPUT -p udp --dport 53 -d 202.96.209.5 -j ACCEPT
[root@localhost ~]# iptables -I OUTPUT -p udp --dport 53 -d 202.96.209.133 -j ACCEPT
[root@localhost ~]# iptables -A OUTPUT -p udp -j DROP
[root@localhost ~]# service iptables save

“53”,为DNS所需要的UDP端口,××ד202.96.209.*”部分为DNS IP,根据您服务器的设定来定

10、 iptables的MAC地址过滤 

允许MAC地址为XX:XX:XX:XX:XX:XX主机访问22端口:

[root@localhost ~]# iptables -A INPUT -p tcp --destination-port 22 -m mac --mac-source XX:XX:XX:XX:XX:XX -j ACCEPT

允许IP地址为192.168.1.21,MAC地址为XX:XX:XX:XX:XX:XX的主机通信,拒绝多有其他主机:

[root@localhost ~]# iptables -A INPUT -s 192.168.1.21 -m mac --mac-source XX:XX:XX:XX:XX:XX -j ACCEPT
[root@localhost ~]# iptables -P INPUT DROP


为避免操作失误,操作前先行备份:

[root@localhost ~]# cp /etc/sysconfig/iptables /etc/sysconfig/iptablesbak
[root@localhost ~]# cp /etc/sysconfig/iptablesbak /etc/sysconfig/iptables
cp: overwrite `/etc/sysconfig/iptables'? y
[root@localhost ~]# iptables-save > fire.rule
[root@localhost ~]# iptables-restore < fire.rule 
[root@localhost ~]#

两种备份方法都可以


centos7 启用iptables

--关闭firewall:

[root@localhost ~]# systemctl stop firewalld.service            #停止firewall

[root@localhost ~]# systemctl disable firewalld.service        #禁止firewall开机启动

--安装安装iptables防火墙

[root@localhost ~]# yum install iptables-services            #安装


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值