下的iptables详解及配置

一.防火墙简介

 能够确保信息安全的一种设备,设备上有一些特定的规则,允许或拒绝数据包通过。通过防火墙可以隔离风险区域与安全区域的连接,同时不会妨碍风险区域的访问。当然需要注意的是世界上没有绝对的安全,防火墙也只是启到一定的安全防护。大多数的安全风险还是在内网当中!

二.防火墙分类

(1)软件防火墙:件防火墙需要运行在特定的计算机上,而且需要计算机的操作系统的支持。

(2)硬件防火墙:硬件防火墙其实就是一个普通pc机的架构,然后上面跑有专门的操作系统。

(3)芯片级防火墙:这种防火墙基于专门的硬件平台,没有操作系统,专有的ASIC芯片使它们比其他类的防火墙速度更快,处理能力极强,性能更高,但是价格昂贵。


三.Iptables简介

(1)用户空间:由管理员制定规则(netfilter组件)

(2)内核空间:规则会题交给内核空间,内核就按照这些规则去过滤数据包。(iptables组件)

四.Iptables的表和链

(1)filter 默认的,能够实现数据包的过滤,该表还包含三条链:

@1.INPUT:到达本机的数据包

@2.OUTPUT:从本机出去的数据包

@3.FORWARD:经过本机的数据包


(2)nat 网络地址转换,

源地址转换

目的地址转换

PANT跟SNAT 差不多,不一样的是SNAT的源地址是固定的,而PNAT的源地址是不固定的,当使用ppp或pppoe的方式连接互联网的时候一般适应这个

(3)mangle 打标记,表主要是修改数据包头部信息的


@1.PREROUTING链,
,也称为路由前,
@2.POSTROUTING链,
,也称为路由后,
@3.OUTPUT链
          从本机出去的时间包路由前
@4.INPUT链
,路由后
@5.FORWARD链
,最后一次路由判断之前改变数据包

五.Iptables的状态


1.NEW状态
状态的数据包说明这个数据包是收到的第一个数据包。
2.ESTABLISHED状态
,一个数据包的状态就从NEW变为ESTABLEISHED,而且该状态会继续匹配这个连接后继数据包。
3.RELATED状态
ESTABLSHED状态的连接有关系的时候,就会被认为是RELATED,也就是说一个链接想要是RELATED状态,首先要有一个ESTABLISHED的连接。
4.INVALID状态
,一般这中数据包要被拒绝的。

六.Iptables的命令使用

1.命令:

-A 顺序添加,添加一条新规则

-I 插入,插入一条新规则 -I 后面加一数字表示插入到哪行

-R 修改,-D 后面加一数字表示删除哪行

-D 删除,删除一条新规则 -D 后面加一数字表示删除哪行

-N   新建一个链

删除一个自定义链,删除之前要保证次链是空的,而且没有被引用

-L 查看

         @1.iptables -L -n 以数字的方式显示

         @2. iptables -L -v显示详细信息

         @3. iptables -L -x 显示精确信息

-E   重命名链

-F 清空链中的所有规则

-Z   清除链中使用的规则

-P 设置默认规则


2.匹配条件:

隐含匹配:

   -p  tcp udp icmp

   --sport指定源端口

   --dport指定目标端


源地址


目的地址


-i 数据包进入的网卡


-o 数据包出口的网卡

扩展匹配:

-m state --state   匹配状态的

-m mutiport --source-port   端口匹配 ,指定一组端口

-m limit --limit 3/minute   每三分种一次

-m limit --limit-burst  5   只匹配5个数据包

-m string --string --algo bm|kmp --string"xxxx"  匹配字符串

-mtime--timestart 8:00 --timestop 12:00  表示从哪个时间到哪个时间段

-mtime--days    表示那天

-m mac --mac-sourcexx:xx:xx:xx:xx:xx 匹配源MAC地址

-m layer7 --l7proto qq   表示匹配腾讯qq的 当然也支持很多协议,这个默认是没有的,需要我们给内核打补丁并重新编译内核及iptables才可以使用 -m layer7 这个显示扩展匹配


3.动作:

-j

直接丢掉

允许通过

REJECT 丢掉,但是回复信息

LOG --log-prefix"说明信息,自己随便定义" ,记录日志

SNAT       源地址转换

DNAT       目标地址转换

REDIRECT   重定向

MASQUERAED  地址伪装

六.具体实例

1.常见服务的端口号及协议

ssh tcp 22
    dhcp udp 67 68
    DNS tcp/udp 53
    http tcp 80
    samba  udp 137 138  tcp 139 445
    nfs  2049 111 /etc/sysconfig/nfs
    ftp  tcp 20 21 >1024
    squid tcp 3128
    mysql tcp 3306
    smtp 25
    pop3 110
    imap 143


2.应用实例

(1)开启22端口.

[root@tx1 ~]# service iptables start
Flushing firewall rules:                                   [  OK  ]
Setting chains to policy ACCEPT: filter                    [  OK  ]
Unloading iptables modules:                                [  OK  ]
[root@tx1 ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT
[root@tx1 ~]# iptables -A OUTPUT -p tcp --dport 22 -j ACCEPT(如果OUTPUT链默认为DROP这条就一定要加上,以下相同)
只开放一个人的远程连接:
[root@tx1 ~]# iptables -A INPUT -p tcp --dport 22 -s 192.168.8.71 -j ACCEPT


(2)如果我们只开设了web服务

注:本文部分内容为转载

[root@tx1 ~]# iptables -P INPUT DROP #我们用-P来拦截主机上所有通讯
[root@tx1 ~]#iptables -A INPUT -p tcp --dport 80 -j ACCEPT#打开80端口的tcp协议[root@tx1 ~]# iptables -L
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:http 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@tx1 ~]# /etc/init.d/iptables save
Saving firewall rules to /etc/sysconfig/iptables:          [  OK  ]
(3)不允许ping
[root@tx1 ~]# iptables -A INPUT -p icmp -j DROP

(4)删除3号规则
[root@tx1 ~]# iptables -L --line-number
Chain INPUT (policy DROP)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  192.168.8.71         anywhere            tcp dpt:ssh 
2    DROP       icmp --  anywhere             anywhere            
3    ACCEPT     tcp  --  192.168.8.71         anywhere            tcp dpt:ssh 

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         
[root@tx1 ~]# iptables -D INPUT 3
[root@tx1 ~]# iptables -L --line-number
Chain INPUT (policy DROP)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  192.168.8.71         anywhere            tcp dpt:ssh 
2    DROP       icmp --  anywhere             anywhere            

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination      
(5)防止广播包进入局域网:
[root@tx1 ~]# iptables  -A INPUT -s 255.255.255.255 -i eth0 -j DROP [root@tx1 ~]# iptables -A INPUT -s 224.0.0.0/224.0.0.0 -i eth0 -j DROP 
[root@tx1 ~]# iptables -A INPUT -d 0.0.0.0 -i eth0 -j DROP 
(6)修改第3条规则:
[root@tx1 ~]# iptables -R INPUT 3 -s 192.168.8.72 -j ACCEPT
(7)在第3条规则上面插入一条新的规则:
[root@tx1 ~]# iptables -I INPUT 3 -s 192.168.8.73 -j ACCEPT
(8)把ping的信息定向到日志:
[root@tx1 ~]# iptables -A INPUT -p icmp -s 172.16.13.13 -j LOG
[root@tx1 ~]#  tail -0f /var/log/messages
(9)连续端口的写法:25:110
 iptables -A INPUT -p tcp --dport 25:110 -j ACCEPT
(10)允许loopback!(不然会导致DNS无法正常关闭等问题)
[root@tx1 ~]# iptables -A INPUT -i lo -p all -j ACCEPT
[root@tx1 ~]# iptables -A OUTPUT -o lo -p all -j ACCEPT(output链为DROP的情况下)
(11)减少不安去的端口连接
有些些特洛伊***会扫描端口31337到31340(即***语言中的 elite 端口)上的服务。既然合法服务都不使用这些非标准端口来通信,所以拒绝这些端口的连接是有必要的
[root@tx1 ~]#  iptables -A OUTPUT -p tcp --sport 31337:31338 -j DROP
[root@tx1 ~]#  iptables -A OUTPUT -p tcp --sport 31339:31340 -j DROP

3.FORWARD链实例(要开启转发功能,有两块网卡)
注:在做NAT时,FORWARD默认规则是DROP时,必须要做的:
[root@tx1 ~]# iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
[root@tx1 ~]# iptables -A FORWARD -i eth1 -o eh0 -j ACCEP
(1)丢弃坏的TCP包
[root@tx1 ~]# iptables -A FORWARD -p TCP ! --syn -m state --state NEW -j DROP
(2)处理IP碎片数量,防止***,允许每秒100个
[root@tx1 ~]# iptables -A FORWARD -f -m limit --limit 100/s --limit-burst 100 -j ACCEPT
(3)设置ICMP包过滤,允许每秒1个包,限制触发条件是10个包.
[root@tx1 ~]# iptables -A FORWARD -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
[root@tp rc.d]# iptables -t nat -L
如果你想清除,命令是
[root@tx1 ~]# iptables -F -t nat
[root@tx1 ~]# iptables -X -t nat
[root@tx1 ~]# iptables -Z -t nat
(4)添加规则
[root@tx1 ~]# iptables -t nat -A PREROUTING -i eth0 -s 10.0.0.0/8 -j DROP
[root@tx1 ~]#  iptables -t nat -A PREROUTING -i eth0 -s 172.16.0.0/12 -j DROP
[root@tx1 ~]# iptables -t nat -A PREROUTING -i eth0 -s 192.168.0.0/16 -j DROP
(5)禁用FTP(21)端口

[root@tx1 ~]# iptables -t nat -A PREROUTING -p tcp --dport 21 -j DROP
(6)拒绝非法连接
[root@tx1 ~]# iptables -A INPUT     -m state --state INVALID -j DROP
[root@tx1 ~]# iptables -A OUTPUT    -m state --state INVALID -j DROP
[root@tx1 ~]# iptables -A FORWARD -m state --state INVALID -j DROP
(7)接收已经建立的连接
[root@tx1 ~]# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
[root@tx1 ~]# iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT


转载于:https://blog.51cto.com/tanxin/1261245