*********Linux 防火墙 ************Linix IPTables 的使用:
***
******iptables 概述:(类似windows 的 TCP/ IP 筛选。) Linux 内核2.4 中,实现了一个具有包过滤、数据包处理、网络地址转换等防火墙功能框架的 netfilter , 而iptables 则是基于netfilter 基础上的防火墙安全管理工具。
****
******netfilter 的主要安全功能有:1.包过滤(Packet Tracking )。2.网络地址转换。(network Address Translation )3.连接追踪(Connection Tracking )。4.包变换(Packet Tracking )。
***1.包过滤:通过检查每一个包的头部,如何决定如何处理包:丢弃、通过、转发。
***2.网络地址转换:netfilter 提供了两种不同的网络地址转换方式:NAT(SNAT ) 和 目标 NAT (DNAT ) 。SNAT 是指修改包的源地址(改变连接的源IP) 。DNAT 是指修改包的目的地址(改变连接的目的IP)。端口转发、负载均衡和透明代理 都属于DNAT 。地址伪装是SNAT 的一个特殊形式。
***3.连接追踪:通过对数据包的前后状态信息比较来控制数据包处理。
***4.包交换:netfilter 可以改变数据包的内容,如 TTL , 标志符。
******iptables 结构图:
******
Netfilter规则表—filter nat mangle
***
filter,用于路由网络数据包。是默认的,也就是说如果没有指定-t参数,当创建一条新规则时,它会默认存放到该表内。
INPUT 网络数据包流向服务器
OUTPUT 网络数据包从服务器流出
FORWARD 网络数据包经服务器路由
***
nat,用于NAT表.NAT(Net Address Translation )是一种IP地址转换方法。
PREROUTING 网络数据包到达服务器时可以被修改
OUTPUT 网络数据包由服务器流出
POSTROUTING 网络数据包在即将从服务器发出时可以被修改
***
mangle,用于修改网络数据包的表,如TOS(Type Of Service),TTL(Time To Live),等.
INPUT 网络数据包流向服务器
OUTPUT 网络数据包流出服务器
FORWARD 网络数据包经由服务器转发
PREROUTING 网络数据包到达服务器时可以被修改
POSTROUTING 网络数据包在即将从服务器发出时可以被修改
***
******iptables 配置命令:
建立规则
#iptables -A INPUT -s xxx.xxx.xxx.xx -p tcp --dport 80 -j DROP
移除规则
#iptables -D INPUT -s xxx.xxx.xxx.xx -p tcp --dport 80 -j DROP
常用操作命令:
-A 或 -append 在所选链尾加入一条或多条规则
-D 或 -delete 在所选链尾部删除一条或者多条规则
-R 或 -replace 在所选链中替换一条匹配规则
-I 或 -insert 以给出的规则号在所选链中插入一条或者多条规则. 如果规则号为1,即在链头部.
-L 或 -list 列出指定链中的所有规则,如果没有指定链,将列出链中的所有规则.
-F 或 -flush 清除指定链和表中的所由规则, 假如不指定链,那么所有链都将被清空.
-N 或 -new-chain 以指定名创建一条新的用户自定义链,不能与已有链名相同.
-X 或 -delete-chain 删除指定的用户定义帘,必需保证链中的规则都不在使用时才能删除,若没有指定链,则删除所有用户链.
-P 或 -policy 为永久帘指定默认规则(内置链策略),用户定义帘没有缺省规则,缺省规则也使规则链中的最后一条规则,用-L显示时它在第一行显示.
-C 或 -check 检查给定的包是否与指定链的规则相匹配.
-Z 或 -zero 将指定帘中所由的规则包字节(BYTE)计数器清零.
-h 显示帮助信息.
设置链的默认策略。一般有两种方法。
1)首先允许所有的包,然后再禁止有危险的包通过放火墙。
#iptables -P INPUT ACCEPT
#iptables -P OUTPUT ACCEPT
#iptables -P FORWARD ACCEPT
2)首先禁止所有的包,然后根据需要的服务允许特定的包通过防火墙。
#iptables -P INPUT DROP
#iptables -P OUTPUT DROP
#iptables -P FORWARD DROP
添加规则
# iptables -A INPUT -p tcp –dport 22 -j ACCEPT
# iptables -A OUTPUT -p tcp –sport 22 -j ACCEPT
开启22端口, 允许SSH登录
如开启80端口:
# iptables -A INPUT -p tcp –dport 80 -j ACCEPT
# iptables -A OUTPUT -p tcp –sport 80 -j ACCEPT
禁止某个IP访问
# iptables -I INPUT -s x.x.x.x -j DROP
也可进行更细致的设置, 如只允许192.168.1.14的机器进行SSH连接:
# iptables -A INPUT -p tcp –dport 22 -s 192.168.1.14 -j ACCEPT
如果要允许或限制一段IP地址可用192.168.1.0/24 表示192.168.1.1-255端的所有IP.
防止同步包洪水(Sync Flood)
# iptables -A FORWARD -p tcp –syn -m limit –limit 1/s -j ACCEPT
防止各种端口扫描
# iptables -A FORWARD -p tcp –tcp-flags SYN,ACK,FIN,RST RST -m limit –limit 1/s -j ACCEPT
Ping 洪水***(Ping of Death)
# iptables -A FORWARD -p icmp –icmp-type echo-request -m limit –limit 1/s -j ACCEPT
重启防火墙:
service iptables restart
******************************************************iptables 详细********************************************************************
Chain INPUT (policy ACCEPT)
target prot opt source destination
target prot opt source destination
target prot opt source destination
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 icmp type 255
ACCEPT esp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT ah -- 0.0.0.0/0 0.0.0.0/0
ACCEPT udp -- 0.0.0.0/0 224.0.0.251 udp dpt:5353
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:631
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:80
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:25
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
可以看出我在安装linux时,选择了有防火墙,并且开放了22,80,25端口.
Chain INPUT (policy ACCEPT)
target prot opt source destination
target prot opt source destination
target prot opt source destination
[root@tp ~]# iptables -X 清除预设表filter中使用者自定链中的规则
Chain INPUT (policy ACCEPT)
target prot opt source destination
target prot opt source destination
target prot opt source destination
上面的意思是,当超出了IPTABLES里filter表里的两个链规则(INPUT,FORWARD)时,不在这两个规则里的数据包怎么处理呢,那就是DROP(放弃).应该说这样配置是很安全的.我们要控制流入数据包
如果做了邮件服务器,开启25,110端口.
[root@tp ~]# iptables -A INPUT -p tcp --dport 25 -j ACCEPT
如果做了FTP服务器,开启21端口
IPTABLES -A OUTPUT -o lo -p all -j ACCEPT(如果是OUTPUT DROP)
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
target prot opt source destination
SNAT all -- 192.168.0.0/24 anywhere to:211.101.46.235
target prot opt source destination
[root@tp sysconfig]# iptables -t nat -A PREROUTING -i eth0 -s 172.16.0.0/12 -j DROP
[root@tp sysconfig]# iptables -t nat -A PREROUTING -i eth0 -s 192.168.0.0/16 -j DROP
如果我们想,比如阻止MSN,QQ,BT等的话,需要找到它们所用的端口或者IP,(个人认为没有太大必要)
[root@tp ~]# iptables -A INPUT -m state --state INVALID -j DROP
[root@tp ~]# iptables -A OUTPUT -m state --state INVALID -j DROP
[root@tp ~]# iptables-A FORWARD -m state --state INVALID -j DROP
允许所有已经建立的和相关的连接
[root@tp ~]# iptables-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
[root@tp ~]# iptables-A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
[root@tp ~]# /etc/rc.d/init.d/iptables save
这样就可以写到/etc/sysconfig/iptables文件里了.写入后记得把防火墙重起一下,才能起作用.
[root@tp ~]# service iptables restart