29.3NAT介绍

NAT介绍

1、 NAT: network address translation[^foot]
PREROUTING,INPUT,OUTPUT,POSTROUTING
请求报文:修改源/目标IP,由定义如何修改
响应报文:修改源/目标IP,根据跟踪机制自动实现
2、 SNAT:source NAT POSTROUTING, INPUT
让本地网络中的主机通过某一特定地址访问外部网络,实现地址伪装
请求报文:修改源IP
3、 DNAT:destination N AT PREROUTING , OUTPUT
把本地网络中的主机上的某服务开放给外部网络访问(发布服务和端口映射), 但隐藏真实IP
请求报文:修改目标IP
4、 PNAT: port nat,端口和IP都进行修改


SNAT
1、 nat表的target:
2、 SNAT:固定IP
–to-source [ipaddr[-ipaddr]][:port[-port]]
–random
iptables -t nat -A POSTROUTING -s LocalNET ! -d LocalNet -j SNAT –to-source ExtIP
示例:
iptables -t nat -A POSTROUTING -s 10.0.1.0/24 ! –d 10.0.1.0/24 -j SNAT – to-source 172.18.1.6-172.18.1.9
iptables -t nat -A POSTROUTING -s 192.168.31.0/24 -j SNAT –to-source 172.18.0.123
3、 MASQUERADE:动态IP,如拨号网络
–to-ports port[-port]
–random
iptables -t nat -A POSTROUTING -s LocalNET ! -d LocalNet -j MASQUERADE
4、示例:
iptables -t nat -A POSTROUTING -s 10.0.1.0/24 ! –d 10.0.1.0/24 -j MASQUERADE

DNAT
1、 DNAT
–to-destination [ipaddr[-ipaddr]][:port[-port]]
2、 iptables -t nat -A PREROUTING -d ExtIP -p tcp|udp –dport PORT -j
DNAT –to-destination InterSeverIP[:PORT]
3、 示例:
iptables -t nat -A PREROUTING -s 0/0 -d 172.18.100.6 -p tcp –dport 22 -j DNAT –to-destination 10.0.1.22
iptables -t nat -A PREROUTING -s 0/0 -d 172.18.100.6 -p tcp –dport 80 -j DNAT –to-destination 10.0.1.22:8080

转发
1、 REDIRECT:
NAT表
可用于:PREROUTING OUTPUT 自定义链
通过改变目标IP和端口,将接受的包转发至不同端口
–to-ports port[-port]
示例:
iptables -t nat -A PREROUTING -d 172.16.100.10 -p tcp –dport 80 -j REDIRECT –to-ports 8080


SNAT与PNAT简单说明

一个局域网内有A和B两台主机,连接在交换机上,交换机与路由器(一般都具有NAT功能)相连接,通过路由器连接到互联网,当局域网的主机A访问互联网上的www.dhy.com时,通过把局域网的私有ip地址转换为公司的公网ip地址(NAT实现),实现访问互联网。同时当B主机也访问互联网时,也利用路由器的NAT转换为公司的同一个公网ip地址,但A和B都转换为一个公网ip地址存在矛盾,故而实际上都是利用PNAT来把A,B两台局域网内的主机转换为公网ip地址,公网ip相同,但端口号不同,从而实现对应关系。路由器则记录下了A和B通过PNAT把私有ip地址转换公网ip的记录,以便回应包可以通过路由器到达局域网的对应主机,如图
这里写图片描述

DNAT使用

DNAT:把本地网络中的主机上的某服务开放给外部网络访问(发布服务和端口映射), 但隐藏真实IP
如公司局域网内提供web服务,互联网上的用户访问web服务,如图
这里写图片描述

实验(DNAT)

这里写图片描述
1、内网可以ping通外网,外网ping不同内网,且外网主机ip是固定的

[root@cos17-LAN ~ ]#route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.31.7    0.0.0.0         UG    100    0        0 eth0
192.168.31.0    0.0.0.0         255.255.255.0   U     100    0        0 eth0

[root@222-internet ~ ]#route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.18.0.0      0.0.0.0         255.255.0.0     U     100    0        0 eth1

[root@firewall ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
[root@firewall ~]# sysctl -a | grep ip_for
net.ipv4.ip_forward = 1
net.ipv4.ip_forward_use_pmtu = 0

[root@firewall ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.18.0.0      0.0.0.0         255.255.0.0     U     101    0        0 eth1
192.168.31.0    0.0.0.0         255.255.255.0   U     100    0        0 eth0

#ping不通
[root@cos17-LAN ~ ]#ping 172.18.0.222
PING 172.18.0.222 (172.18.0.222) 56(84) bytes of data.
#有去无回
[root@222-internet ~ ]#tcpdump -i eth0 icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
21:57:01.676066 IP 192.168.31.17 > 222-internet: ICMP echo request, id 12214, seq 121, length 64
21:57:02.675997 IP 192.168.31.17 > 222-internet: ICMP echo request, id 12214, seq 122, length 64

#SNAT地址转换
[root@firewall ~]# iptables -t nat -A POSTROUTING -s 192.168.31.0/24 -j SNAT --to-source 172.18.0.123

#重新ping
[root@cos17-LAN ~ ]#ping 172.18.0.222
PING 172.18.0.222 (172.18.0.222) 56(84) bytes of data.
64 bytes from 172.18.0.222: icmp_seq=1 ttl=63 time=0.625 ms
64 bytes from 172.18.0.222: icmp_seq=2 ttl=63 time=0.490 ms

[root@222-internet ~ ]#tcpdump -i eth0 icmp  #centos7
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
22:21:47.378705 IP 192.168.31.17 > cos27.localdomain: ICMP echo request, id 12520, seq 14, length 64
22:21:47.379839 IP cos27.localdomain > 192.168.31.17: ICMP echo reply, id 12520, seq 14, length 64


[root@cos6 ~ ]#route -n  #ip为172.18.0.22 ,centos6
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.18.0.0      0.0.0.0         255.255.0.0     U     1      0        0 eth1
[root@cos6 ~ ]#tcpdump -i eth1 icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth1, link-type EN10MB (Ethernet), capture size 65535 bytes
22:15:35.342464 IP 172.18.0.123 > 172.18.0.22: ICMP echo request, id 12508, seq 1, length 64
22:15:35.344066 IP 172.18.0.22 > 172.18.0.123: ICMP echo reply, id 12508, seq 1, length 64

2、内网可以ping通外网,外网ping不同内网,且外网主机未知
#若地址不固定,[root@222-internet ~ ]的ip是动态的,可以用地址伪装

[root@firewall ~]# systemctl stop firewalld
#删除第一次添加的策略
[root@firewall ~]# iptables -t nat -D POSTROUTING 1
#确定删除了
[root@firewall ~]# iptables -vnL -t nat --line-number
#ping不通外网
[root@cos17-LAN ~ ]#ping 172.18.0.222
PING 172.18.0.222 (172.18.0.222) 56(84) bytes of data.
#公网ip不固定
[root@firewall ~]# iptables -t nat -A POSTROUTING -s 192.168.31.0/24 -j MASQUERADE 
[root@firewall ~]# iptables -vnL -t nat
#内网可以ping通外网
[root@cos17-LAN ~ ]#ping 172.18.0.222
PING 172.18.0.222 (172.18.0.222) 56(84) bytes of data.
64 bytes from 172.18.0.222: icmp_seq=1 ttl=63 time=0.520 ms
#外网ping不同内网
[root@222-internet ~ ]#ping 192.168.31.17
connect: Network is unreachable

3、外网可以访问内网httpd服务

[root@cos17-LAN ~ ]#yum install httpd
[root@cos17-LAN ~ ]#echo lanserver > /var/www/html/index.html
[root@cos17-LAN ~ ]#systemctl start httpd
[root@firewall ~]# curl 192.168.31.17
lanserver

#清空之前的策略
[root@firewall ~]# iptables -t nat -F

[root@222-internet ~ ]#curl 192.168.31.17
curl: (7) Failed to connect to 192.168.31.17: Network is unreachable
[root@222-internet ~ ]#curl 172.18.0.123
curl: (7) Failed connect to 172.18.0.123:80; Connection refused

[root@firewall ~]# iptables -t nat -A PREROUTING -d 172.18.0.123  -p tcp --dport 80 -j DNAT --to-destination  192.168.31.17
[root@222-internet ~ ]#curl 172.18.0.123
lanserver

#172.18.0.222在访问,目的地址被替换,源地址未替换 DNAT
[root@cos17-LAN ~ ]#cat /var/log/httpd/access_log 
192.168.31.7 - - [21/Aug/2018:22:44:57 +0800] "GET / HTTP/1.1" 403 4897 "-" "curl/7.29.0"
172.18.0.222 - - [21/Aug/2018:22:48:17 +0800] "GET / HTTP/1.1" 200 10 "-" "curl/7.29.0"
#若内网httpd服务端口为8080,则
[root@firewall ~]# iptables -t nat -F
[root@firewall ~]# iptables -t nat -A PREROUTING -d 172.18.0.123  -p tcp --dport 80 -j DNAT --to-destination  192.168.31.17:8080
[root@222-internet ~ ]#curl 172.18.0.123
lanserver
端口转发

iptables -t nat -A PREROUTING -d 192.168.31.17 -p tcp –dport 90 -j REDIRECT –to-ports 8080

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值