分类目录归档:基础

如何理解EdgeOS/VyOS防火墙

防火墙基础

防火墙是建立在网络会话的基础上,对网络流量进行访问控制的重要工具。

通常一个会话,可以由5个元素来确定,即源地址和端口,目的地址和端口,以及协议类型,这常常成为五元组。防火墙规则通常就会基于这五大元素展开。

根据防火墙的会话跟踪机制,进入防火墙的包又可以分为四类。

  • 新建New:在已有的会话中找不到匹配的会话,并且TCP包的SYN位ON。
  • 已建立Established:属于已有的会话。
  • 相关Related:这个包不属于任何已有的会话,但是和现有会话有关,例如ftp的数据连接是依附于控制连接的。
  • 非法Invalid:凡是不属于以上三类的包都属于非法包。

下面我们就三种不同的应用场景介绍在VyOS上防火墙的配置与使用。

场景一:内网访问互联网

我们先看看相应的流量走向图。

内网的流量(绿色)首先经过DNAT,一般访问外网的流量无需DNAT,然后进入LAN口eth1的防火墙IN规则。如果没有配置规则,缺省都是accept,就是允许任何流量进入。经过内部路由后,流量再经过WAN口OUT规则过滤(缺省accept),再经过SNAT(masquerate)把源地址切换成WAN口外网地址,最后发送出去。

返程流量(蓝色),正好走反过来的路线。WAN口IN方向,缺省规则是丢弃,因为我们不允许外来未知流量进入,并且要丢弃非法状态的流量包。

因此,要让回程的流量正常通过,我们需要在WAN口防火墙IN的规则集里,配置一条允许已经建立连接和相关连接的规则。

下面是对应的防火墙配置:

configure
set firewall name WAN_IN default-action 'drop'

set firewall name WAN_IN rule 10 action 'accept'
set firewall name WAN_IN rule 10 description 'Allow established/related'
set firewall name WAN_IN rule 10 state established 'enable'
set firewall name WAN_IN rule 10 state related 'enable'

set firewall name WAN_IN rule 20 action 'drop'
set firewall name WAN_IN rule 20 description 'Drop invalid state'
set firewall name WAN_IN rule 20 state invalid 'enable'

set interfaces ethernet eth0 firewall in name 'WAN_IN'
commit;save;exit

场景二:端口转发

内网有一个http服务器,IP地址是10.0.0.100/24,开放公网访问。我们知道http/https用的端口是80/433。

我们还是可以参考场景一的示意图,只不过流量走向正好反过来。要让外网发起的访问内部网站的流量通过,我们需要配置一条防火墙规则,允许符合条件(目的地址和端口,协议)的流量通过。注意执行顺序防火墙排在DNAT之后,所以目的地址应当是转换过的内网地址。

返程不需要设置规则,因为WAN口OUT的缺省规则是accept,允许外向流量。

下面是参考配置。

configure
set firewall name WAN_IN rule 100 action 'accept'
set firewall name WAN_IN rule 100 destination port 80,443
set firewall name WAN_IN rule 100 destination address 10.0.0.100
set firewall name WAN_IN rule 100 protocol tcp
set firewall name WAN_IN rule 100 log disable
set firewall name WAN_IN rule 100 description "allow web access"
commit;save;exit

场景三:IPSec VPN

IPSec的流量是终结在路由器内部的,如下图:

IPSec分成IKE协商阶段,会用到两个固定的端口,500/udp和4500/udp。隧道建立后,流量会封装在ESP包里。流量从隧道出来,也是被视作来源于WAN口,还需要在LOCAL和IN规则集里各配置一条对应的规则,这里要用到匹配IPSec流量的关键字 ipsec match-ipsec。对应的规则如下:

configure
set firewall name WAN_LOCAL default-action 'drop'

set firewall name WAN_LOCAL rule 200 action 'accept'
set firewall name WAN_LOCAL rule 200 description 'ipsec ike'
set firewall name WAN_LOCAL rule 200 destination port '500'
set firewall name WAN_LOCAL rule 200 log 'disable'
set firewall name WAN_LOCAL rule 200 protocol 'udp'

set firewall name WAN_LOCAL rule 210 action 'accept'
set firewall name WAN_LOCAL rule 210 description 'ipsec esp'
set firewall name WAN_LOCAL rule 210 log 'disable'
set firewall name WAN_LOCAL rule 210 protocol 'esp'

set firewall name WAN_LOCAL rule 220 action 'accept'
set firewall name WAN_LOCAL rule 220 description 'ipsec nat-t'
set firewall name WAN_LOCAL rule 220 destination port '4500'
set firewall name WAN_LOCAL rule 220 log 'disable'
set firewall name WAN_LOCAL rule 220 protocol 'udp'

set firewall name WAN_LOCAL rule 230 action 'accept'
set firewall name WAN_LOCAL rule 230 destination address 10.0.0.0/24
set firewall name WAN_LOCAL rule 230 source address 10.1.0.0/24
set firewall name WAN_LOCAL rule 230 ipsec match-ipsec
set firewall name WAN_LOCAL rule 230 log 'disable'
set firewall name WAN_LOCAL rule 230 protocol 'all'

set firewall name WAN_IN rule 200 action 'accept'
set firewall name WAN_IN rule 200 destination address 10.0.0.0/24
set firewall name WAN_IN rule 200 source address 10.1.0.0/24
set firewall name WAN_IN rule 200 ipsec match-ipsec
set firewall name WAN_IN rule 200 log 'disable'
set firewall name WAN_IN rule 200 protocol 'all'

set interfaces ethernet eth0 firewall local name 'WAN_LOCAL'
commit;save;exit

从应用的视角谈NAT

简介

前段时间,有朋友让我介绍以下在VyOS下如何配置端口转发NAT。我查了一下资料,发现市面上介绍NAT的文章都过于学术化,甚至有些晦涩难懂。有鉴于此,本文试图从应用的视角来介绍常见的NAT及在VyOS上的配置。

从技术的角度来区分,我们常用的NAT可以分为源地址转换SNAT和目标地址转换DNAT两种类型。VyOS/EdgeOS上,两种NAT与接口和防火墙的关系可以参考下图。

下面我们就介绍NAT的各种应用场景以及配置方法。

NAT应用

SNAT

顾名思义,SNAT主要应用于从内网访问外网的场景。我们知道,一般运营商只给宽带接入分配一个IP地址。流量从WAN口出去,就必须转换为外网IP地址,否则远端服务器的回传流量无法路由回本地。

在出口为静态地址,例如192.0.0.1时,可以使用SNAT。VyOS的配置如下:

configure
set nat source rule 100 outbound-interface eth0
set nat source rule 100 translation address 192.0.0.1
commit;save;exit

Masquerade是SNAT的一种实现形式,主要应用于出口是动态地址的场景。

configure
set nat source rule 100 outbound-interface pppoe0
set nat source rule 100 translation address 'masquerade'
commit;save;exit

在某些特殊场景下,我们希望排除NAT,例如IPSec是按照两端地址进行匹配的,如果相应的流量NAT后,就源地址就会变化,导致流量无法进入隧道,这时候就需要使用下面的配置对相应的流量排除。注意,和防火墙策略一样,NAT策略是按照规格序号从小到大先后执行的,因此,需要把排除策略放到前面。

configure
set nat source rule 10 destination address '10.0.0.0/24'
set nat source rule 10 'exclude'
set nat source rule 10 outbound-interface 'eth0'
set nat source rule 10 source address '192.168.0.0/24'
commit;save;exit

端口转发

有些时候,我们会在家里部署NAS,视频监控等应用。当我们出门在外,希望能访问家里的DNS或者查看监控视频,这时候就需要用到DNAT,因为回访的流量是从外到内,目标地址是家里路由器的外网地址,进入到内网,需要转化成指定的内网设备地址。这就是我们常说的端口转发,是DNAT最常见的应用场景。

配置端口转发时,需要指定相关的协议(tcp/udp/tcp_udp)以及相应的端口号。常用的端口号可以参见Wikipedia。端口转发只适用于TCP或者UDP协议。

下面的配置将协议为TCP或者UDP,并且端口为52800或者52810的流量转发到192.168.0.100主机上。

configure
set nat destination rule 100 description 'demo dnat application'
set nat destination rule 100 inbound-interface pppoe0
set nat destination rule 100 protocol tcp_udp
set nat destination rule 100 port 52800,52810
set nat destination rule 100 translation address 192.168.0.100
commit;save;exit

1:1 NAT

在商业应用中,如果你有两个或以上的静态IP地址,你可以将其中一个IP匹配到某个服务器或者第二级路由器上。这时候就会用到1:1NAT,也就是一个外网IP对应一个内网IP,也被称作双向NAT。由于1:1NAT不受限于协议,所有流量都会被转发到内网IP,相应的该内网IP的出口流量也都使用指定的外网IP。

下面的范例中,把WAN口eth1的一个IP地址(192.0.2.30),转化为内网eth0上另一个IP地址(192.168.1.10)。

configure
set nat destination rule 2000 description '1-to-1 NAT example'
set nat destination rule 2000 destination address '192.0.2.30'
set nat destination rule 2000 inbound-interface 'eth1'
set nat destination rule 2000 translation address '192.168.1.10'
set nat source rule 2000 description '1-to-1 NAT example'
set nat source rule 2000 outbound-interface 'eth1'
set nat source rule 2000 source address '192.168.1.10'
set nat source rule 2000 translation address '192.0.2.30'
commit;save;exit

Hairping NAT

假定我们在内网运行了一个web服务器,我们可以通过DNAT开启外网访问。如果我们也要从内网访问,如果使用同样的URL,而DNS解析出来的目标地址是我们的外网地址,导致内网无法访问。这时候,就可以使用Hairping NAT来解决这个问题。

Hairping NAT的工作原理是在内网接口eth1配置地址转换,将所有来自eth1,并且是访问本地的特定端口的流量(原目标地址根据DNS解析为本地WAN口IP),转换目标地址为服务器的内网IP。

同时还要配置一个返程策略,让返回流量的源地址变换为外网IP,这样,从客户端看起来,就好像直接访问了该外网IP。

首先,我们配置一个常规的端口转发:

configure
set nat destination rule 100 description 'Regular destination NAT from external'
set nat destination rule 100 destination port 80
set nat destination rule 100 inbound-interface 'pppoe0'
set nat destination rule 100 protocol 'tcp'
set nat destination rule 100 translation address '192.168.0.100'

接下来我们配置Hairping NAT 的DNAT部分,让内网来的流量重新定位到服务器的内网IP。

set nat destination rule 110 description 'NAT Reflection: INSIDE'
set nat destination rule 110 destination port '80'
set nat destination rule 110 inbound-interface 'eth1'
set nat destination rule 110 protocol 'tcp'
set nat destination rule 110 translation address '192.168.0.100'

最后,我们再为Hairping NAT配置相应的返程策略。

set nat source rule 110 description 'NAT Reflection: INSIDE'
set nat source rule 110 destination address '192.168.0.0/24'
set nat source rule 110 outbound-interface 'eth1'
set nat source rule 110 protocol 'tcp'
set nat source rule 110 source address '192.168.0.0/24'
set nat source rule 110 translation address 'masquerade'

防火墙策略

由于WAN口的缺省防火墙策略是drop,也就是拒绝未经许可的流量。因此对于所有的DNAT,还需要配置相匹配的防火墙策略。防火墙的策略配置,请参考如何理解EdgeOS/VyOS防火墙一文。

介绍VyOS

前言

国内同学熟悉VyOS应该不算太多。在软路由里,如果把某快比做当年的D-Link,那和VyOS相对应的应该就是Cisco了。

VyOS源自当年开源的Vyatta OS,后者被收购后,网上开发者在原来Vayatta的基础上继续开发,形成了今天的VyOS。EdgeRouter上搭载的EdgeOS也是源自Vyatta OS,因此二者非常类似,但是在一些扩展功能上,有一些区别。

因此,VyOS和EdgeRouter可以说是绝配,EdgeRouter以高稳定,高性能,低价格(千兆转发的EdgeRouter-X售价不到400元人民币) 的硬件而著称;VyOS就非常适合虚拟化的场合。

安装VyOS

VyOS可以安装在软路由机器上,也可以安装在VMWare虚拟化平台上,在主流的VPS市场,例如AWS,Azure,GCP,都可以找到相应的安装源。

我们也提供了一个自己编译好基于最新的equuleus版本,有兴趣的朋友可以在这里下载VyOS-1.2.6.iso光盘映像。接下来要用一个Windows下的工具软件Rufus将光盘映像写入到可启动的U盘上,你可以从官网下载,也可以使用提供的下载。写盘的时候,选择MBR分区,支持BIOS或EFI启动即可。第一次刻写会提示你是否升级到最新的Syslinux引导模块,点击确认升级即可开始刻录。

启动后,显示器会进入命令行界面,使用vyos作为用户名和密码登录。登录后,我们需要将系统安装在本地硬盘上,输入命令“install image”可以启动安装程序。

[email protected]:~$ install image
Welcome to the VyOS install program.  This script
will walk you through the process of installing the
VyOS image to a local hard drive.
Would you like to continue? (Yes/No) [Yes]: 
Probing drives: OK
Looking for pre-existing RAID groups...none found.
The VyOS image will require a minimum 2000MB root.
Would you like me to try to partition a drive automatically
or would you rather partition it manually with parted?  If
you have already setup your partitions, you may skip this step

Partition (Auto/Parted/Skip) [Auto]: 

I found the following drives on your system:
 sda	4294MB


Install the image on? [sda]:

This will destroy all data on /dev/sda.
Continue? (Yes/No) [No]: Yes

Looking for config files from previous installations on sda1...
How big of a root partition should I create? (2000MB - 4294MB) [4294]MB: 

Creating filesystem on /dev/sda1: |
OK
Done!
Mounting /dev/sda1...
What would you like to name this image? [1.2.5]: OK.  This image will be named: 1.2.5
Copying squashfs image...
Copying kernel and initrd images...
Done!
I found the following configuration files:
    /opt/vyatta/etc/config/config.boot
    /opt/vyatta/etc/config.boot.default
Which one should I copy to sda? [/opt/vyatta/etc/config/config.boot]: 

Copying /opt/vyatta/etc/config/config.boot to sda.
Enter password for administrator account
Enter password for user 'vyos':
Retype password for user 'vyos':
I need to install the GRUB boot loader.
I found the following drives on your system:
 sda	4294MB


Which drive should GRUB modify the boot partition on? [sda]:

Setting up grub: OK
Done!
[email protected]:~$ 

一路回答Yes, 最后输入你为VyOS新设置的密码。完成安装后,一定要输入”reboot”命令重启系统,否则你的设置无法保存到新建立的硬盘分区。

第一次配置

重启后,我们登录系统进行第一次配置。我们需要完成以下任务:

  • 配置WAN口并添加缺省路由
  • 设置系统DNS服务器
  • 启用SSH
  • 设置WAN口防火墙策略
  • 配置本地局域网

配置WAN口

DHCP

使用DHCP获取IP地址时,系统会自动获取缺省路由和DNS服务器设置,因此我们只需要启用DHCP模式就好。

configure
set interfaces ethernet eth0 address dhcp

静态地址

使用静态地址时,需要设置缺省路由以及系统DNS服务器:

configure
set interfaces ethernet eth0 address 192.168.1.100/24
set system name-server '8.8.8.8'
set protocols static route 0.0.0.0/0 next-hop 192.168.1.254 distance 1

PPPOE

PPPOE模式下,需要设置相应接口的MTU,一般是1492个字节。

configure
set interfaces pppoe pppoe0 authentication password '123456'
set interfaces pppoe pppoe0 authentication user '051212345678'
set interfaces pppoe pppoe0 description 'China Unioncom'
set interfaces pppoe pppoe0 mtu '1492'
set interfaces pppoe pppoe0 source-interface 'eth0'

启用SSH

set service ssh port '22'

配置WAN口防火墙策略

除了本地出口流量外,我们只允许从互联网端ping和ssh访问路由器。

set firewall all-ping 'enable'
set firewall name WAN_IN default-action 'drop'
set firewall name WAN_IN rule 10 action 'accept'
set firewall name WAN_IN rule 10 description 'Allow established/related'
set firewall name WAN_IN rule 10 state established 'enable'
set firewall name WAN_IN rule 10 state related 'enable'
set firewall name WAN_IN rule 20 action 'drop'
set firewall name WAN_IN rule 20 description 'Drop invalid state'
set firewall name WAN_IN rule 20 state invalid 'enable'
set firewall name WAN_LOCAL default-action 'drop'
set firewall name WAN_LOCAL rule 10 action 'accept'
set firewall name WAN_LOCAL rule 10 description 'Allow established/related'
set firewall name WAN_LOCAL rule 10 state established 'enable'
set firewall name WAN_LOCAL rule 10 state related 'enable'
set firewall name WAN_LOCAL rule 20 action 'drop'
set firewall name WAN_LOCAL rule 20 description 'Drop invalid state'
set firewall name WAN_LOCAL rule 20 state invalid 'enable'
set firewall name WAN_LOCAL rule 30 action 'accept'
set firewall name WAN_LOCAL rule 30 description 'Ping from internet'
set firewall name WAN_LOCAL rule 30 icmp type-name 'echo-request'
set firewall name WAN_LOCAL rule 30 log 'disable'
set firewall name WAN_LOCAL rule 30 protocol 'icmp'
set firewall name WAN_LOCAL rule 40 action 'accept'
set firewall name WAN_LOCAL rule 40 description 'allow remote management'
set firewall name WAN_LOCAL rule 40 destination port '22'
set firewall name WAN_LOCAL rule 40 protocol 'tcp'

然后我们将防火墙策略绑定在WAN口上。

set interfaces ethernet eth0 firewall in name 'WAN_IN'
set interfaces ethernet eth0 firewall local name 'WAN_LOCAL'

配置本地局域网

除了设置相应端口的静态地址外,我们还启用了相应的DHCP服务。

set interfaces ethernet eth1 address 192.168.3.254/24
set service dhcp-server shared-network-name LAN subnet 192.168.3.0/24 default-route 192.168.3.254
set service dhcp-server shared-network-name LAN subnet 192.168.3.0/24 range 0 start 192.168.3.5
set service dhcp-server shared-network-name LAN subnet 192.168.3.0/24 range 0 stop 192.168.3.200
set service dhcp-server shared-network-name LAN subnet 192.168.3.0/24 dns-server 192.168.3.254

DNS代理服务

设置系统上游DNS服务器。

set system name-server 1.1.1.1
set system name-server 8.8.4.4

配置DNS代理,使用系统上游DNS服务器,监听LAN口地址。

set service dns forwarding system
set service dns forwarding listen-address 192.168.3.254
set service dns forwarding allow-from 192.168.3.0/24

出口NAT

set nat source rule 1000 outbound-interface eth0
set nat source rule 1000 translation address 'masquerade'

commit;save;exit

小贴士

有些时候,我们想参考现有的配置,你可以使用以下命令将配置转化为命令。

show configuration commands

升级系统:

add system image <url>

显示目前LAN口DHCP地址租用清单:

show dhcp server leases

配置编辑命令

  • edit:进入子项目编辑状态,例如edit interfaces ethernet eth1
  • copy:复制子项目,例如 copy rule 10 to rule 100
  • rename: 给子项目重命名,例如 rename rule 10 to rule 100