NAT有两种方式:
一种是source nat或者叫srcnat,源地址NAT,经常用于把内部网络的私网地址转换成公网地址,以便实现对公网地址的共用
一种是destination nat或者叫dstnat,目标地址NAT,常用与把访问的目标地址映射到内网的私网地址,以实现公网用户对内网资源的访问。
简单的srcnat应用:
常用的srcnat应用主要有两种:
一:masquerade方式,masquerade方式会自动寻找路由设备的IP地址去替代IP数据包的源地址
[admin@MikroTik] > ip firewall nat
[admin@MikroTik] /ip firewall nat> add chain=srcnat action=masquerade
[admin@MikroTik] /ip firewall nat> print
Flags: X - disabled, I - invalid, D - dynamic
0
chain=srcnat action=masquerade
二:src-nat方式,src-nat方式会采用参数“to-addresses”中的特定值去替代IP数据包的源地址
假定:路由的外网地址172.16.0.2/24
[admin@MikroTik] /ip> firewall nat
[admin@MikroTik] /ip firewall nat> add chain=srcnat action=src-nat to-addresses=172.16.0.2
[admin@MikroTik] /ip firewall nat> print
Flags: X - disabled, I - invalid, D - dynamic
0
chain=srcnat action=src-nat to-addresses=172.16.0.2
三:dstnat:端口映射
假定内网有一台WEB服务器:192.168.2.100,需要对外提供WEB资源
[admin@MikroTik] > ip
[admin@MikroTik] /ip> firewall nat
[admin@MikroTik] /ip firewall nat> add chain=dstnat dst-address=172.16.0.2 proto
col=tcp
dst-port=80 action=dst-nat to-addresses=192.168.2.100 to-ports=80
[admin@MikroTik] /ip firewall nat> print
Flags: X - disabled, I - invalid, D - dynamic
0
chain=dstnat action=dst-nat to-addresses=192.168.2.100 to-ports=80
protocol=tcp dst-address=172.16.0.2 dst-port=80
1
chain=srcnat action=masquerade
四:NAT功能举例:
实验结构:在外地机房存在一台ROS的有PPTP VPN服务器
实现功能:假定当从本地访问所需资源出现延时过高或者线路不稳定时,通过拨号连接至VPN服务后,从VPN服务器所在的ISP做中转,同时利用VPN服务器的NAT功能对不同的VPN授
权用户进行不同的访问限制,同时VPN服务器只具备单个网卡接口
本地路由器只需要实现正常共享上网(实现“ROS菜鸟系列1”功能)即可,
外地VPN服务器的构建:
实现第一步:先构建VPN服务器:
[admin@MikroTik] /interface> print
Flags: D - dynamic, X - disabled, R - running, S - slave
#
NAME
TYPE
MTU
0
R
ether1
ether
1500
[admin@MikroTik] /interface> set 0 name=wan
[admin@MikroTik] > interface print
Flags: D - dynamic, X - disabled, R - running, S - slave
#
NAME
TYPE
MTU
0
R
wan
ether
1500
[admin@MikroTik] > ip address add address=211.2.123.42/28 interface=wan
[admin@MikroTik] > ip address print
Flags: X - disabled, I - invalid, D - dynamic
#
ADDRESS
NETWORK
BROADCAST
INTERFACE
0
211.2.123.42/28
211.2.123.32
211.2.123.47
wan
[admin@MikroTik] > ip route
[admin@MikroTik] /ip route> add gateway=211.2.123.33
[admin@MikroTik] /ip route> print
Flags: X - disabled, A - active, D - dynamic,
C - connect, S - static, r - rip, b - bgp, o - ospf, m - mme,
B - blackhole, U - unreachable, P - prohibit
#
DST-ADDRESS
PREF-SRC
G GATEWAY
DISTANCE IN..
0 A S
0.0.0.0/0
r 211.2.123.33
1
wan
1 ADC
211.2.123.32/28
211.2.123.42
0
wan
[admin@MikroTik] > ip pool
[admin@MikroTik] /ip pool> add name=limit ranges=192.168.100.2-192.168.100.254
[admin@MikroTik] /ip pool> add name=nolimit ranges=192.168.200.2-192.168.200.254
[admin@MikroTik] /ip pool> print
# NAME
RANGES
0 limit
192.168.100.2-192.168.100.254
1 nolimit
192.168.200.2-192.168.200.254
增加地址池,为不同的授权用户动态分配不同的地址段
[admin@MikroTik] /ppp profile> add name=limitprofiles local-address=192.168.100.1 remote
-address=limit dns-server=61.139.2.69 idle-timeout=00:10:00 rate-limit=512K/512K only-on
e=yes
[admin@MikroTik] /ppp profile> add name=nolimitprofiles local-address=192.168.200.1 remo
te-address=nolimit dns-server=61.139.2.69 idle-timeout=00:10:00 rate-limit=1M/1M only-on
e=yes
[admin@MikroTik] /ppp profile> print
Flags: * - default
0 * name="default" use-compression=default use-vj-compression=default
use-encryption=default only-one=default change-tcp-mss=yes
1
name="limitprofiles" local-address=192.168.100.1 remote-address=limit
idle-timeout=10m use-compression=default use-vj-compression=default
use-encryption=default only-one=yes change-tcp-mss=default rate-limit=512K/512K
dns-server=61.139.2.69
2
name="nolimitprofiles" local-address=192.168.200.1 remote-address=nolimit
idle-timeout=10m use-compression=default use-vj-compression=default
use-encryption=default only-one=yes change-tcp-mss=default rate-limit=1M/1M
dns-server=61.139.2.69
3 * name="default-encryption" use-compression=default use-vj-compression=default
use-encryption=yes only-one=default change-tcp-mss=yes
添加VPN用户属性,建立不同的用户类型
[admin@MikroTik] /ppp profile> ..
[admin@MikroTik] /ppp> secret
[admin@MikroTik] /ppp secret> add name=user1 password=user1 profile=limitprofiles service=pptp
[admin@MikroTik] /ppp secret> add name=user2 password=user2 profile=nolimitprofiles service=pptp
[admin@MikroTik] /ppp secret> print
Flags: X - disabled
#
NAME
SERVICE CALLER-ID
PASSWORD
PROFILE
REMOTE-ADDRESS
0
user1
pptp
user1
limitprofiles
1
user2
pptp
user2
nolimitpro...
[admin@MikroTik] /interface pptp-server server> set enabled=yes authentication=pap,chap,mschap1,mschap2
[admin@MikroTik] /interface pptp-server server> print
enabled: yes
max-mtu: 1460
max-mru: 1460
mrru: disabled
authentication: pap,chap,mschap1,mschap2
keepalive-timeout: 30
default-profile: default-encryption
[admin@MikroTik] /interface pptp-server server> /
[admin@MikroTik] > ip firewall address-list
[admin@MikroTik] /ip firewall address-list> add list=qq address=202.104.129.251-202.104.129.254
[admin@MikroTik] /ip firewall address-list> add list=qq address=218.18.95.160-218.18.95.227
[admin@MikroTik] /ip firewall address-list> add list=qq address=202.96.170.163-202.96.170.166
[admin@MikroTik] /ip firewall address-list> print
Flags: X - disabled, D - dynamic
#
LIST
ADDRESS
0
qq
202.104.129.251-202.104.129.254
1
qq
218.18.95.160-218.18.95.227
2
qq
202.96.170.163-202.96.170.166
添加地址列表
[admin@MikroTik] /ip firewall nat> add chain=srcnat src-address=192.168.200.0/24 action=masquerade out-interface=wan
[admin@MikroTik] /ip firewall nat> add chain=srcnat src-address=192.168.100.0/24 dst-address-list=qq action=masquerade out-interface=wan
[admin@MikroTik] /ip firewall nat> print
Flags: X - disabled, I - invalid, D - dynamic
0
chain=srcnat action=masquerade src-address=192.168.200.0/24 out-interface=wan
1
chain=srcnat action=masquerade src-address=192.168.100.0/24 dst-address-list=qq
out-interface=wan
利用NAT分类不同用户类型访问不同资源
以上的例子是所有使用limitprofile用户组的用户,在进行VPN拨号授权通过认证后,只能通过VPN线路去访问目标地址为QQ列表里面的资源,无法利用VPN线路去访问其他的资源,
ROS的操作员可以通过对QQ地址列表里面目标地址的添加和删减,从而实现授权用户对不同目标地址的访问限制。
善用NAT与其他策略的配合使用,可以实现其他的更多功能,如:上下行的数据分流等,
一种是source nat或者叫srcnat,源地址NAT,经常用于把内部网络的私网地址转换成公网地址,以便实现对公网地址的共用
一种是destination nat或者叫dstnat,目标地址NAT,常用与把访问的目标地址映射到内网的私网地址,以实现公网用户对内网资源的访问。
简单的srcnat应用:
常用的srcnat应用主要有两种:
一:masquerade方式,masquerade方式会自动寻找路由设备的IP地址去替代IP数据包的源地址
[admin@MikroTik] > ip firewall nat
[admin@MikroTik] /ip firewall nat> add chain=srcnat action=masquerade
[admin@MikroTik] /ip firewall nat> print
Flags: X - disabled, I - invalid, D - dynamic
0
二:src-nat方式,src-nat方式会采用参数“to-addresses”中的特定值去替代IP数据包的源地址
假定:路由的外网地址172.16.0.2/24
[admin@MikroTik] /ip> firewall nat
[admin@MikroTik] /ip firewall nat> add chain=srcnat action=src-nat to-addresses=172.16.0.2
[admin@MikroTik] /ip firewall nat> print
Flags: X - disabled, I - invalid, D - dynamic
0
三:dstnat:端口映射
假定内网有一台WEB服务器:192.168.2.100,需要对外提供WEB资源
[admin@MikroTik] > ip
[admin@MikroTik] /ip> firewall nat
[admin@MikroTik] /ip firewall nat> add chain=dstnat dst-address=172.16.0.2 proto
col=tcp
[admin@MikroTik] /ip firewall nat> print
Flags: X - disabled, I - invalid, D - dynamic
0
1
四:NAT功能举例:
实验结构:在外地机房存在一台ROS的有PPTP VPN服务器
实现功能:假定当从本地访问所需资源出现延时过高或者线路不稳定时,通过拨号连接至VPN服务后,从VPN服务器所在的ISP做中转,同时利用VPN服务器的NAT功能对不同的VPN授
权用户进行不同的访问限制,同时VPN服务器只具备单个网卡接口
本地路由器只需要实现正常共享上网(实现“ROS菜鸟系列1”功能)即可,
外地VPN服务器的构建:
实现第一步:先构建VPN服务器:
[admin@MikroTik] /interface> print
Flags: D - dynamic, X - disabled, R - running, S - slave
#
0
[admin@MikroTik] /interface> set 0 name=wan
[admin@MikroTik] > interface print
Flags: D - dynamic, X - disabled, R - running, S - slave
#
0
[admin@MikroTik] > ip address add address=211.2.123.42/28 interface=wan
[admin@MikroTik] > ip address print
Flags: X - disabled, I - invalid, D - dynamic
#
0
[admin@MikroTik] > ip route
[admin@MikroTik] /ip route> add gateway=211.2.123.33
[admin@MikroTik] /ip route> print
Flags: X - disabled, A - active, D - dynamic,
C - connect, S - static, r - rip, b - bgp, o - ospf, m - mme,
B - blackhole, U - unreachable, P - prohibit
#
0 A S
1 ADC
[admin@MikroTik] > ip pool
[admin@MikroTik] /ip pool> add name=limit ranges=192.168.100.2-192.168.100.254
[admin@MikroTik] /ip pool> add name=nolimit ranges=192.168.200.2-192.168.200.254
[admin@MikroTik] /ip pool> print
# NAME
0 limit
1 nolimit
增加地址池,为不同的授权用户动态分配不同的地址段
[admin@MikroTik] /ppp profile> add name=limitprofiles local-address=192.168.100.1 remote
-address=limit dns-server=61.139.2.69 idle-timeout=00:10:00 rate-limit=512K/512K only-on
e=yes
[admin@MikroTik] /ppp profile> add name=nolimitprofiles local-address=192.168.200.1 remo
te-address=nolimit dns-server=61.139.2.69 idle-timeout=00:10:00 rate-limit=1M/1M only-on
e=yes
[admin@MikroTik] /ppp profile> print
Flags: * - default
0 * name="default" use-compression=default use-vj-compression=default
1
2
3 * name="default-encryption" use-compression=default use-vj-compression=default
添加VPN用户属性,建立不同的用户类型
[admin@MikroTik] /ppp profile> ..
[admin@MikroTik] /ppp> secret
[admin@MikroTik] /ppp secret> add name=user1 password=user1 profile=limitprofiles service=pptp
[admin@MikroTik] /ppp secret> add name=user2 password=user2 profile=nolimitprofiles service=pptp
[admin@MikroTik] /ppp secret> print
Flags: X - disabled
#
0
1
[admin@MikroTik] /interface pptp-server server> set enabled=yes authentication=pap,chap,mschap1,mschap2
[admin@MikroTik] /interface pptp-server server> print
[admin@MikroTik] /interface pptp-server server> /
[admin@MikroTik] > ip firewall address-list
[admin@MikroTik] /ip firewall address-list> add list=qq address=202.104.129.251-202.104.129.254
[admin@MikroTik] /ip firewall address-list> add list=qq address=218.18.95.160-218.18.95.227
[admin@MikroTik] /ip firewall address-list> add list=qq address=202.96.170.163-202.96.170.166
[admin@MikroTik] /ip firewall address-list> print
Flags: X - disabled, D - dynamic
#
0
1
2
添加地址列表
[admin@MikroTik] /ip firewall nat> add chain=srcnat src-address=192.168.200.0/24 action=masquerade out-interface=wan
[admin@MikroTik] /ip firewall nat> add chain=srcnat src-address=192.168.100.0/24 dst-address-list=qq action=masquerade out-interface=wan
[admin@MikroTik] /ip firewall nat> print
Flags: X - disabled, I - invalid, D - dynamic
0
1
利用NAT分类不同用户类型访问不同资源
以上的例子是所有使用limitprofile用户组的用户,在进行VPN拨号授权通过认证后,只能通过VPN线路去访问目标地址为QQ列表里面的资源,无法利用VPN线路去访问其他的资源,
ROS的操作员可以通过对QQ地址列表里面目标地址的添加和删减,从而实现授权用户对不同目标地址的访问限制。
善用NAT与其他策略的配合使用,可以实现其他的更多功能,如:上下行的数据分流等,