目录
6.1 关于Prerouting和Postrouting表链的理解
6.1.1 Prerouting和Postrouting的关系
6.1.2 Prerouting和Postrouting例子
前面花了几章讲了ros常用命令,相信大家都知道了学习命令的正确打开方式:不要死背,不断使用?帮助和tab补全。
本章开始讲一下ros firewall防火墙一些基础知识,我上传的csdn中的ros入门教程中第九章讲了防火墙过滤和第十三章讲了Mangle分类标记,这里我就不重复已有的东西了。
本章主要是讲防火墙基础知识的理解,还有相关的资料
一、为什么要讲防火墙的基础理解
主要我发现很多人都防火墙的理解不到位,主要还是基础不扎实的原因,总是理解有些偏差。包括我刚刚学的时候,同一接口一会儿变出口,一会儿变入口,而且每个教程讲的好像都不太一样。
当理解有偏差,讲的不一样的时候,直接去看官网原版是不错的选择。
二、学习防火墙方面的资料
ros 防火墙是基于linux的iptables的,先理解ros官方的Firewall,再去看一下linux的iptables结合起来会理解会更深入一点。
我发现一个专讲iptables防火墙系列的网站,感觉不错,我已经上传到csdn中供下载,初学者只需要看第1章《iptables详解(1):iptables概念》其实就是理解一张图的大概流程
三、firewall基础理解:连接状态
ros防火墙的连接状态,ros官网文档对防火墙已经做出一解释,
- NEW - The NEW state tells us that the packet is the first packet that we see. This means that the first packet that the conntrack module sees, within a specific connection, will be matched. For example, if we see an SYN packet and it is the first packet in a connection that we see, it will match;
- ESTABLISHED - The ESTABLISHED state has seen traffic in both directions and will then continuously match those packets. ESTABLISHED connections are fairly easy to understand. The only requirement to get into an ESTABLISHED state is that one host sends a packet and that it, later on, gets a reply from the other host. The NEW state will upon receipt of the reply packet to or through the firewall change to the ESTABLISHED state;
- RELATED - A connection is considered RELATED when it is related to another already ESTABLISHED connection. For a connection to be considered as RELATED, we must first have a connection that is considered ESTABLISHED. The ESTABLISHED connection will then spawn a connection outside of the main connection. The newly spawned connection will then be considered RELATED, for example, a packet that begins the FTP data connection;
- INVALID - The INVALID state means that the packet can't be identified or that it does not have any state. It is suggested to DROP everything in this state;
- UNTRACKED - A packet that was set to bypass connection tracking in the Firewall RAW table;
如果英文差的话可以使用google在线翻译,用中文简单表达就是
new – 一个数据包开始建立一个新的连接。我们看到的第一个数据包。
established – 匹配一个数据包属于已建立的连接。进入ESTABLISHED状态的唯一要求是,一台主机发送一个数据包,然后再从另一台主机获得答复。 当收到到防火墙或通过防火墙的回复数据包时,新状态将变为已建立状态
related – 一个数据包与之相关联的连接,但并不是当前存在连接的一部分,例如:ICMP errors信息或一个FTP数据包的数据连接
invalid – 一个数据包在连接跟踪列表中无法判断状态,由于这个原因非法的数据包将不会加入到NAT转换,但当进行路由时,仍然会包含源地址,这里强烈建议使用connection-state=invalid ,在filter的forward和input链表丢弃这类数据包。
untracked –在firewall raw设置数据包设置为绕过连接跟踪(connection tracking)
四、防火墙Filters过滤表概念
4.1 filters过滤表中表链概念
filter 表中的规则可以被哪些链使用:INPUT,FORWARD,OUTPUT,如下图所示:
官网的概念如下:
- input - used to process packets entering the router through one of the interfaces with the destination IP address which is one of the router's addresses. Packets passing through the router are not processed against the rules of the input chain
- forward - used to process packets passing through the router
- output - used to process packets originated from the router and leaving it through one of the interfaces. Packets passing through the router are not processed against the rules of the output chain
简单理解就是:
input:进入。目标地址是路由器的数据包。即进入路由器的数据包。无论是在哪个接口都行。
forward :转发。通过路由器的数据包。
output :输出。路由器本身发出的数据包,数据源地址为路由器本身。
PS:这里的“进入”,“输出”,都是以路由器本身为参考物。这个理解很重要,路由器一个接口可以是进口,也可以出口,要不网卡支持双工变得毫无意义。
4.2 所涉及的流程图
在ros官方文档包流程图中有介绍上面3个表链的流量图
4.2.1 input表链流程图
4.2.2 forward流程图
4.2.3 output流程图
五、NAT地址转换表概念
5.1 sant和dant
nat地址转换主要是snat和dnat,概念如下:
- source NAT or srcnat. This type of NAT is performed on packets that are originated from a natted network. A NAT router replaces the private source address of an IP packet with a new public IP address as it travels through the router. A reverse operation is applied to the reply packets travelling in the other direction.
- destination NAT or dstnat. This type of NAT is performed on packets that are destined for the natted network. It is most commonly used to make hosts on a private network to be accessible from the Internet. A NAT router performing dstnat replaces the destination IP address of an IP packet as it travels through the router towards a private network.
源NAT或srcnat.该NAT类型表现为从路由器发出时替换一个数据包的源IP地址,即一个NAT路由器将发到公网的私网I旧地址替换为路由器的公网IP,同样反向的操作也可以应用于内网方向。
目标NAT或dstnat.该NAT类型表现为从进入路由器时替换一个数据包的目标IP地址,即一个NAT路由器从公网访问路由器的IF替换为内网访问私网地址。
基于iptables的RouterOS是按照目的地址来选择的,因此dst-nat是在PREROUTING链表上进行的,而src-nat是在数据包发送出去的时候才进行,因此是在POSTROUTING链表上进行的。(将在后面的mangle表讲PREROUTING和POSTROUTING表链)
5.2 Masquerade伪装
NAT action masquerade is a unique subversion of srcnat. It was designed for specific use in situations when public IP can randomly change, for example, DHCP server change assigned IP or PPPoE tunnel after disconnect gets different IP, in short - when public IP is dynamic.
Nat设置中masquerade一种独特的srcnat类型,应用于公网IP地址随机变动的环境,用发送数据的网卡上的IP来替换源IP,例如PPPoE拨号动态获取IP和DHCP动态获取IP等
PS:这里说明了如果做masquerade伪装的话,只需要配置一个snat就行了
六、Mangle表
6.1 mangle表理解
Mangle是一种“标记”,它使用特殊标记来标记数据包以备将来处理。 RouterOS中的许多其他功能都使用这些标记,例如 队列树,NAT,路由。 他们根据其标记识别数据包并进行相应处理。 损坏标记仅存在于路由器内,不会通过网络传输。
此外,mangle工具用于修改IP标头中的某些字段,例如TOS(DSCP)和TTL字段。
主要是涉及五个链
- The PREROUTING chain: Rules in this chain apply to packets as they just arrive on the network interface;
- The INPUT chain: Rules in this chain apply to packets just before they’re given to a local process;
- The OUTPUT chain: The rules here apply to packets just after they’ve been produced by a process;
- The FORWARD chain: The rules here apply to any packets that are routed through the current host;
- The POSTROUTING chain: The rules in this chain apply to packets as they just leave the network interface;
• Prerouting:路由前,常用于标记策略和端口路由。此链中的规则适用于刚到达网络接口的数据包。这个数据包可以是进路由器的数据包或者经常路由器的数据包。(所有的数据包进来的时侯都先由这个链处理)
• Input:进入路由器的数据。
• Foreward:通过路由转发,用于修改TTL、TCP-MSS和流量控制规则
• Output:数据输出
• Postrouting:路由后。适用于离开网络接口的数据包。这个数据包可以是路由器发出的包(所有的数据包出来的时侯都先由这个链处理)
PS:Prerouting例子可以看我上传的ros教程中的6.5 RouterOS策略路由
6.1 关于Prerouting和Postrouting表链的理解
我相信有不少的人被Prerouting和Postrouting搞晕了,都不知道什么时候使用前者,什么时候用后者,要理解这个用iptables 角度会更好
6.1.1 Prerouting和Postrouting的关系
你首先要明白什么是"PREROUTING" ,什么是"POSTROUTING",我们可以简单的用下面的关系来表示:
源地址发送数据--> {PREROUTING-->路由规则-->POSTROUTING} -->目的地址接收到数据
为了方便我这里弄一个拓扑图,如下图所示:
我们的pc机为内网的192.168.2.10/24,ros内网关为192.168.2.6,外网地址为202.105.60.10,为了方便理解我使用iptabels了
先说NAT中Prerouting的使用,这个是属于dnat,上面pc访问百度,百度返回数据,路由器要从nat反到pc机把目标地址转为pc地址,使用iptables命令如下:
iptables -t nat -A PREROUTING -i eth1 -d 14.215.177.38 -j DNAT --to 192.168.2.10
你访问百度会(解析成14.215.177.38,因使用CDN每个地方IP会不同),linux路由器会在“路由规则”之前将目的地址改为192.168.2.10,并且Linux路由器(iptables)会同时记录下这个连接,并在数据从192.168.2.10返回时,经过linux路由器将数据发送到那台发出请求的机器。所以你的"POSTROUTING"规则没有起作用。
简单来,因pc机是内网,先得内网做srcnat转成路由器用外网IP请示,百度响应结果返回给路由器,并不是pc机,进入的是Input表链,而不是Foreward,所以不经过POSTROUTING。
而"POSTROUTING"是“路由规则”之后的动作。
PS:iptables语法,也可以使用帮助 “man iptables”和“iptables --help”
iptables -t 表名 <-A/I/D/R> 规则链名 [规则号] <-i/o 网卡名> -p 协议名 <-s 源IP/源子网> --sport 源端口 <-d 目标IP/目标子网> --dport 目标端口 -j 动作
表名包括:
- raw:高级功能,如:网址过滤。
- mangle:数据包修改(QOS),用于实现服务质量。
- net:地址转换,用于网关路由器。
- filter:包过滤,用于防火墙规则。
规则链名包括:
- INPUT链:处理输入数据包。
- OUTPUT链:处理输出数据包。
- PORWARD链:处理转发数据包。
- PREROUTING链:用于目标地址转换(DNAT)。
- POSTOUTING链:用于源地址转换(SNAT)。
动作包括:
- accept:接收数据包。
- DROP:丢弃数据包。
- REDIRECT:重定向、映射、透明代理。
- SNAT:源地址转换。
- DNAT:目标地址转换。
- MASQUERADE:IP伪装(NAT),用于ADSL。
- LOG:日志记录。
6.1.2 Prerouting和Postrouting例子
PREROUTING的应用
一般情况下,PREROUTING应用在普通的NAT中(也就是SNAT),如:你用ADSL上网,这样你的网络中只有一个公网IP地址(如:202.105.60.10),但你的局域网中的用户还要上网(局域网IP地址为:192.168.2.0/24),这时你可以使用PREROUTING(SNAT)来将局域网中用户的IP地址转换成202.105.60.10,使他们也可以上网:
iptables -t nat -A PREROUTING -s 192.168.2.0/24 -j SNAT 202.105.60.10
POSTROUTING的应用
POSTROUTING用于将你的服务器放在防火墙之后,作为保护服务器使用,例如:
A.你的服务器IP地址为:192.168.2.10;
B.你的防火墙(Linux & iptables)地址为192.168.2.6和202.105.60.10
Internet上的用户可以正常的访问202.105.60.10,但他们无法访问192.168.2.10,这时在Linux防火墙里可以做这样的设置:
iptables -t nat -A POSTROUTING -d 202.105.60.10 -j DNAT 192.168.2.10
PS:最要紧的是我们要记住PREROUTING是“路由规则”之前的动作,POSTROUTING是“路由规则”之后的动作!
七、包流程图
所有表都讲完了,我找了一个ros包流程图,每个流程涉及到的表及表链也标出来,如下所示: