亞寵展、全球?qū)櫸锂a(chǎn)業(yè)風(fēng)向標(biāo)——亞洲寵物展覽會(huì)深度解析
2098
2025-04-01
3.4 使用OpenVPN創(chuàng)建遠(yuǎn)程訪問的虛擬專用網(wǎng)絡(luò)
在上個(gè)實(shí)踐中,我們創(chuàng)建了兩臺(tái)具有公網(wǎng)IP的服務(wù)器之間的虛擬專用網(wǎng)絡(luò),進(jìn)行安全的數(shù)據(jù)傳輸。在本案例中,我們將創(chuàng)建遠(yuǎn)程訪問(Remote Access)模式的虛擬專用網(wǎng)絡(luò)。
在某些文檔中,遠(yuǎn)程訪問被稱為Road Warrior(可以翻譯為“移動(dòng)辦公”),是指為經(jīng)常不在辦公室的駐場人員或者遠(yuǎn)程辦公的人員提供訪問服務(wù)器資源或者辦公網(wǎng)絡(luò)資源的通道。在這些場景中,遠(yuǎn)程訪問者一般沒有公網(wǎng)IP,他們使用內(nèi)網(wǎng)地址通過防火墻設(shè)備進(jìn)行網(wǎng)絡(luò)地址轉(zhuǎn)換后連接互聯(lián)網(wǎng)。
在本例中,我們使用的物理網(wǎng)絡(luò)結(jié)構(gòu)圖如圖3-5所示。
圖3-5 遠(yuǎn)程訪問模式虛擬專用網(wǎng)絡(luò)物理網(wǎng)絡(luò)結(jié)構(gòu)圖
創(chuàng)建遠(yuǎn)程訪問模式的虛擬專用網(wǎng)絡(luò)的操作步驟如下。
1)在服務(wù)器a.b.c.239上生成CA證書、服務(wù)器證書、客戶端證書。
在OpenVPN 2.0.9的源碼包中有相關(guān)的腳本可以輔助我們進(jìn)行證書的生成和管理。
我們首先從http://build.openvpn.net/downloads/releases/openvpn-2.0.9.tar.gz下載該代碼。使用如下命令:
wget http://build.openvpn.net/downloads/releases/openvpn-2.0.9.tar.gz
解壓縮后,進(jìn)入以下目錄:
[root@localhost easy-rsa]# cd openvpn-2.0.9/easy-rsa
[root@localhost easy-rsa]# ls
2.0?????? build-dh???? build-key?????? build-key-pkcs12? build-req?????? clean-all? make-crl???? README????? revoke-full? vars
build-ca? build-inter? build-key-pass? build-key-server? build-req-pass ?list-crl?? openssl.cnf? revoke-crt? sign-req???? Windows
生成如下CA證書:
[root@localhost easy-rsa]# . vars #初始化環(huán)境變量
NOTE: when you run ./clean-all, I will be doing a rm -rf on /root/openvpn/openvpn-2.0.9/easy-rsa/keys
[root@localhost easy-rsa]# ./clean-all #刪除舊的文件
[root@localhost easy-rsa]# ./build-ca #創(chuàng)建root CA
Generating a 1024 bit RSA private key
...........................++++++
....++++++
writing new private key to 'ca.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [KG]:CN #填寫國家代碼
State or Province Name (full name) [NA]:SH #填寫省份
Locality Name (eg, city) [BISHKEK]:SH #填寫城市
Organization Name (eg, company) [OpenVPN-TEST]:XUFENG-INFO #填寫組織名
Organizational Unit Name (eg, section) []:DEVOPS #填寫部門名稱
Common Name (eg, your name or your server's hostname) []:cert.xufeng.info
Email Address [me@myhost.mydomain]:xufengnju@163.com #填寫管理員郵箱地址
Common Name (eg, your name or your server's hostname) []:cert.xufeng.info是最重要的字段,相當(dāng)于發(fā)證機(jī)關(guān)root CA的組織代碼。務(wù)必保持唯一。
生成OpenVPN服務(wù)器證書和私鑰如下:
[root@localhost easy-rsa]# ./build-key-server vpnserver #extension = server
Generating a 1024 bit RSA private key
...........++++++
..........................................++++++
writing new private key to 'vpnserver.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [KG]:CN
State or Province Name (full name) [NA]:SH
Locality Name (eg, city) [BISHKEK]:SH
Organization Name (eg, company) [OpenVPN-TEST]:XUFENG-INFO
Organizational Unit Name (eg, section) []:VPN
Common Name (eg, your name or your server's hostname) []:vpnserver.xufeng.info
Email Address [me@myhost.mydomain]:xufengnju@163.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /root/openvpn/openvpn-2.0.9/easy-rsa/openssl.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName?????????? :PRINTABLE:'CN'
stateOrProvinceName?? :PRINTABLE:'SH'
localityName????????? :PRINTABLE:'SH'
organizationName????? :PRINTABLE:'XUFENG-INFO'
organizationalUnitName:PRINTABLE:'VPN'
commonName??????????? :PRINTABLE:'vpnserver.xufeng.info'
emailAddress????????? :IA5STRING:'xufengnju@163.com'
Certificate is to be certified until Dec? 8 06:56:36 2025 GMT (3650 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
Common Name (eg, your name or your server's hostname) []:vpnserver.xufeng.info是最重要的字段,相當(dāng)于虛擬專用網(wǎng)絡(luò)服務(wù)器的標(biāo)識(shí)。建議使用虛擬專用網(wǎng)絡(luò)服務(wù)器的完整域名(Fully Qualified Domain Name,F(xiàn)QDN),例如vpnserver.xufeng.info。
生成客戶端需要的證書和私鑰如下:
[root@localhost easy-rsa]# ./build-key vpnclient2
Generating a 1024 bit RSA private key
........................++++++
......++++++
writing new private key to 'vpnclient1.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [KG]:CN
State or Province Name (full name) [NA]:SH
Locality Name (eg, city) [BISHKEK]:SH
Organization Name (eg, company) [OpenVPN-TEST]:XUFENG-INFO
Organizational Unit Name (eg, section) []:VPN
Common Name (eg, your name or your server's hostname) []:vpnclient2.xufeng.info
Email Address [me@myhost.mydomain]:xufengnju@163.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /root/openvpn/openvpn-2.0.9/easy-rsa/openssl.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName?????????? :PRINTABLE:'CN'
stateOrProvinceName?? :PRINTABLE:'SH'
localityName????????? :PRINTABLE:'SH'
organizationName????? :PRINTABLE:'XUFENG-INFO'
organizationalUnitName:PRINTABLE:'VPN'
commonName??????????? :PRINTABLE:'vpnclient2.xufeng.info'
emailAddress????????? :IA5STRING:'xufengnju@163.com'
Certificate is to be certified until Dec? 8 06:57:53 2025 GMT (3650 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
Common Name (eg, your name or your server's hostname) []:vpnclient2.xufeng.info是最重要的字段,相當(dāng)于虛擬專用網(wǎng)絡(luò)客戶端的標(biāo)識(shí)。建議使用虛擬專用網(wǎng)絡(luò)客戶端的完整域名或用戶的郵箱名加域名。
2)在服務(wù)器a.b.c.239配置OpenVPN,配置文件是server.conf。配置文件的內(nèi)容如下:
port 1194 #使用1194端口進(jìn)行監(jiān)聽
proto udp #使用UDP協(xié)議
dev tun #使用IP路由模式
ca????? /etc/openvpn/ca.crt #指定CA證書位置
cert??? /etc/openvpn/vpnserver.crt #指定服務(wù)器端證書位置
key???? /etc/openvpn/vpnserver.key #指定服務(wù)器端私鑰位置
dh????? /etc/openvpn/dh1024.pem #使用Diffie-Hellman算法進(jìn)行加密密鑰計(jì)算
server 172.16.100.0 255.255.255.0 #客戶端連接上虛擬專用網(wǎng)絡(luò)后從此網(wǎng)段分配隧道IP
client-config-dir /etc/openvpn/ccd #使用此目錄對(duì)各個(gè)虛擬專用網(wǎng)絡(luò)客戶端進(jìn)行細(xì)粒度控制
route 192.168.20.0 255.255.255.0 #配置服務(wù)器增加一條到客戶端網(wǎng)絡(luò)的路由
client-to-client #允許不同的客戶端進(jìn)行互相訪問,使用OpenVPN內(nèi)部路由
keepalive 10 120 #每10s發(fā)送保活,120s內(nèi)未收到保活信息時(shí)向OpenVPN進(jìn)程發(fā)送SIGUSR1信號(hào)
#在TLS控制通道的通信協(xié)議上增加一層HMAC(Hash-based Message Authentication Code)防止dos攻擊
tls-auth??? /etc/openvpn/ta.key 0
comp-lzo #啟用壓縮
max-clients 100 #最大用戶數(shù)
user nobody #執(zhí)行OpenVPN進(jìn)程的用戶
group nobody #執(zhí)行OpenVPN進(jìn)程的組
persist-key #收到信號(hào)SIGUSR1時(shí)不重新讀取key文件
persist-tun #收到信號(hào)SIGUSR1時(shí)不關(guān)閉tun虛擬網(wǎng)口和重新打開
#創(chuàng)建并修改權(quán)限,使nobody可以讀寫 /var/log/openvpn
status /var/log/openvpn/status.log? #指定狀態(tài)日志位置
log-append? /var/log/openvpn/openvpn.log #指定運(yùn)行日志位置
verb 4 #設(shè)置日志級(jí)別為一般級(jí)別,會(huì)記錄正常連接信息和報(bào)錯(cuò)
我們來看看/etc/openvpn/ccd下文件vpnclient2.xufeng.info中的內(nèi)容:
ifconfig-push 172.16.100.9 172.16.100.10 #指定客戶端的IP為172.16.100.9
iroute 10.192.168.20.0 255.255.255.0 #加一條內(nèi)部路由
push "route 10.168.103.0 255.255.255.0" #把該路由推送到客戶端執(zhí)行
1)ccd目錄下的文件必須以客戶端證書的Common Name為文件名。
2)ccd目錄可以對(duì)每個(gè)不同的客戶端進(jìn)行細(xì)粒度控制。
3)iroute是必需的。在server.conf中的--route指令把包從內(nèi)核路由到OpenVPN,進(jìn)入OpenVPN以后,--iroute指令把包路由到該指定的客戶端。
啟動(dòng)OpenVPN服務(wù)器進(jìn)程。使用如下的命令:
openvpn --daemon --config /etc/openvpn/server.conf
3)在192.168.20.96上安裝OpenVPN GUI,并部署配置文件。
在https://openvpn.net/index.php/download/community-downloads.html頁面進(jìn)行下載。
在32位Windows 7 系統(tǒng)上,我們通過以下鏈接進(jìn)行下載并安裝:
https://swupdate.openvpn.org/community/releases/openvpn-install-2.3.9-I601-i686.exe
在安裝過程中,可能會(huì)出現(xiàn)確認(rèn)界面,如圖3-6所示。
圖3-6 OpenVPN安裝確認(rèn)界面
請(qǐng)勾選“始終信任來自‘OpenVPN Technologies,Inc.’的軟件(A)”。
安裝完成后,在目錄C:\Program Files\OpenVPN\config下面部署如下文件,如圖3-7所示。
圖3-7 客戶端文件部署
vpnclient.ovpn內(nèi)容如下:
client #指定角色為客戶端
dev tun #和服務(wù)器端一致
proto udp #和服務(wù)器端一致
remote a.b.c.239 1194 #指定服務(wù)器端IP和端口
resolv-retry infinite #連接失敗時(shí)重復(fù)嘗試
nobind #不指定本地端口
persist-key #收到信號(hào)SIGUSR1時(shí)不重新讀取key文件
persist-tun #收到信號(hào)SIGUSR1時(shí)不關(guān)閉tun虛擬網(wǎng)口和重新打開
ca ca.crt #指定CA證書位置
cert??? vpnclient2.crt #指定客戶端證書位置
key???? vpnclient2.key #指定客戶端私鑰位置
ns-cert-type server #要求服務(wù)器端的證書的擴(kuò)展屬性為server
#在TLS控制通道的通信協(xié)議上增加一層HMAC(Hash-based Message Authentication Code)防止dos攻擊
tls-auth ta.key 1
comp-lzo #啟用壓縮
verb 4 #設(shè)置日志級(jí)別為一般級(jí)別,會(huì)記錄正常連接信息和報(bào)錯(cuò)
keepalive 10 120 #每10s發(fā)送保活,120s內(nèi)未收到保活信息時(shí)向OpenVPN進(jìn)程發(fā)送SIGUSR1信號(hào)
log-append openvpn.log #指定log位置
經(jīng)過以上3個(gè)步驟后,客戶端192.168.20.96可以使用虛擬隧道和虛擬專用網(wǎng)絡(luò)服務(wù)器進(jìn)行通信。但此時(shí)無法與10.168.103.171通信。為了實(shí)現(xiàn)客戶端192.168.20.96可以與10.168.103.171通信,必須在a.b.c.239這個(gè)虛擬專用網(wǎng)絡(luò)服務(wù)器上執(zhí)行以下的操作:
#啟用ip_forward
sed -e 's/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/g' /etc/sysctl.conf
sysctl -p
#增加iptables對(duì)tun0的轉(zhuǎn)發(fā)支持
iptables -A FORWARD -i tun0 -j ACCEPT
#加入網(wǎng)絡(luò)地址轉(zhuǎn)換的轉(zhuǎn)發(fā)
iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE #eth1為服務(wù)器內(nèi)網(wǎng)端口
iptables -t nat -A POSTROUTING -o tun0 -j MASQUERADE #tun0為虛擬隧道端口
同時(shí)在10.168.103.171服務(wù)器上執(zhí)行以下的操作:
route add -net 192.168.20.0/24 gw 10.168.103.239
4)在192.168.20.96上,連接OpenVPN服務(wù)器并進(jìn)行網(wǎng)絡(luò)測試。
連接后,我們?cè)?92.168.20.96上可以看到它獲得的隧道IP地址,如圖3-8所示。
圖3-8 客戶端獲得的隧道IP地址
由此可見,它獲得的隧道IP地址和服務(wù)器端配置文件/etc/openvpn/ccd/vpnclient2.xufeng.info中使用ifconfig-push指令配置的完全一致。
它獲得的路由如圖3-9所示。
圖3-9 客戶端獲得的路由
在遠(yuǎn)程訪問模式下,從虛擬專用網(wǎng)絡(luò)客戶端192.168.20.96使用ICMP ping服務(wù)器Host:a.b.c.239所在局域網(wǎng)中的一臺(tái)服務(wù)器10.168.103.171的虛擬網(wǎng)絡(luò)數(shù)據(jù)流圖如圖3-10所示。
圖3-10 遠(yuǎn)程訪問模式下虛擬網(wǎng)絡(luò)數(shù)據(jù)流圖
可以看到,OpenVPN起到虛擬路由器的作用,使用net30的模式,建立起遠(yuǎn)程訪問者和虛擬專用網(wǎng)絡(luò)服務(wù)器之間的虛擬專用網(wǎng)絡(luò)。方框中的IP包標(biāo)示出了在虛擬專用網(wǎng)絡(luò)客戶端發(fā)出的包到達(dá)虛擬專用網(wǎng)絡(luò)服務(wù)器時(shí)經(jīng)過網(wǎng)絡(luò)地址轉(zhuǎn)換的情況。此時(shí),在服務(wù)器10.168.103.171上看到的ICMP的來源IP地址是虛擬專用網(wǎng)絡(luò)服務(wù)器(Host:a.b.c.239)的內(nèi)網(wǎng)IP地址10.168.103.239。
在服務(wù)器10.168.103.171使用tcpdump抓取ICMP網(wǎng)絡(luò)通信的結(jié)果如下:
# tcpdump -vvv -nnn -i em1 -c 3 icmp
tcpdump: listening on em1, link-type EN10MB (Ethernet), capture size 65535 bytes
10:38:35.015495 IP (tos 0x0, ttl 127, id 654, offset 0, flags [none], proto ICMP (1), length 60)
10.168.103.239 > 10.168.103.171: ICMP echo request, id 1, seq 9923, length 40 #源地址已經(jīng)被轉(zhuǎn)換成VPN服務(wù)器的內(nèi)網(wǎng)地址
10:38:35.016139 IP (tos 0x0, ttl 64, id 64964, offset 0, flags [none], proto ICMP (1), length 60)
10.168.103.171 > 10.168.103.239: ICMP echo reply, id 1, seq 9923, length 40
10:38:36.017624 IP (tos 0x0, ttl 127, id 655, offset 0, flags [none], proto ICMP (1), length 60)
10.168.103.239 > 10.168.103.171: ICMP echo request, id 1, seq 9924, length 40 #源地址已經(jīng)被轉(zhuǎn)換成虛擬專用網(wǎng)絡(luò)服務(wù)器的內(nèi)網(wǎng)地址
3 packets captured
4 packets received by filter
0 packets dropped by kernel
網(wǎng)絡(luò) 虛擬專用網(wǎng)絡(luò) VPN 虛擬化
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。