一直想寫一片關於VPN配置方面的文章,由於時間等等很多問題,一直沒有落實,現在終於和大家見面了,由於本人知識有限也並非專業從事這方面工作的,難免有錯誤,望指正,共同提高。
自從認識VPN以來,一直在找尋一個容易配置、功能強大、支援多系統的VPN程式,由於一直沒有找到合適的軟體,Linux下的FreeSWAN、 OpenBSD FreeBSD下的IPSec、Windows下的PPTP都試了試,始終不能滿足自己的要求,直到OpenVPN的出現。
關於各種VPN軟體或者硬體的優缺點,在這裡我就不再敘述了,因為這個摻雜著很多人為的因素在裡邊(蘿蔔好吃還是青菜好吃?),一個軟體:穩定、符合自己的要求、自己用的習慣就是一個好軟體。
對於OpenVPN,在CU的VPN版面也有過很多的討論,其中也不乏精華的文章,但是都是使用Static key驗證的,從字面上就可以看出來Static key使用的就是預先產生的key對資料進行加密和解密,也就是常說的對稱試加密,加密和解密雙方必須預Crowdsourced Security Testing道加密的Key。本文討論的是基於TLS加密 方式,使用CA驗證VPN Client的身份,OpenVPN使用TLS加密是通過使用公開密鑰(非對稱金鑰,加密解密使用不同的key,一個稱為Public key,另一個是Private key)對資料進行加密的,對於TLS傳輸的工作原理,大家可以去Google一下,資料一大堆。對於OpenVPN使用TLS mode,首先Server和Client要有相同CA簽發的認證,雙方通過交換認證驗證雙方的合法性以決定是否建立VPN串連,然後使用對方CA把自己 目前使用的資料加密方法(類似於密鑰)加密後發送給對方,由於使用對方CA加密的,所以只有對方CA對應的Private key才能解密該字串,保證了此密鑰的安全性,並且此密鑰定期改變,對於竊聽者來說,可能還沒有破解出密鑰,通訊雙方已經更換密鑰了。
我個人對OpenVPN的感覺(或者說是我使用OpenVPN的理由),NAT穿透力特強,支援HTTP代理,對動態地址支援很好,可配置性強,配置安全,開源便於二次開發...其他沒有想好:)。
關於OpenVPN的相關內容可以在http://openvpn.net上找到。
下面開始正式討論OpenVPN的安裝和配置方法,本文是在Fedora Core 2環境下配置的,由於機器在網路中不是在網關的位置,所以使用NAT方式來訪問內網,否則還涉及到配置網路裝置的路由。假設我的VPN Server有2塊網卡,eth0對外,IP:61.1.1.2 eth1對內,IP:192.168.1.2,內網地址:192.168.0.0/16
本文除設定檔中行首的"#"是注釋外,其他行首的"#"都是提示符,如果在非設定檔一行中第二次出現"#"說明後面的是注釋,書寫命令時可以省略。
擷取並安裝Openvpn:
首先檢查系統是否安裝lzo即時壓縮公用程式
$rpm -qa | grep lzo
如果沒有安裝可以在http://www.oberhumer.com/opensource/lzo/找到並安裝,安裝方法詳見壓縮包中的 INSTALL檔案,當然也可以用rpm包安裝,記住一定要安裝lzo-devel開頭的那個包,因為OpenVPN需要使用lzo的標頭檔。
$wget http://mesh.dl.sourceforge.net/s ... vpn-2.0_rc16.tar.gz
$tar -zxvf openvpn-2.0_rc16.tar.gz
$cd openvpn-2.0_rc16
$./configure
$make
$su
#make install
按照INSTALL檔案中的說明,做如下操作:
#mknod /dev/net/tun c 10 200 #建立一個tun裝置
#echo "alias char-major-10-200 tun" >;>; /etc/modprobe.conf
#echo 1 >; /proc/sys/net/ipv4/ip_forward #開啟系統的轉寄功能
接下來就產生伺服器用戶端需要使用的keys了,為了方便,我們使用OpenVPN包內建的指令碼產生。
#mkdir /etc/openvpn
#cp -r easy-rsa /etc/openvpn #切換到OpenVPN原始碼目錄執行
修改vars 檔案
-------------CUT Here-------------
# easy-rsa parameter settings
# NOTE: If you installed from an RPM,
# don't edit this file in place in
# /usr/share/openvpn/easy-rsa --
# instead, you should copy the whole
# easy-rsa directory to another location
# (such as /etc/openvpn) so that your
# edits will not be wiped out by a future
# OpenVPN package upgrade.
# This variable should point to
# the top level of the easy-rsa
# tree.
export D=`pwd`
# This variable should point to
# the openssl.cnf file included
# with easy-rsa.
export KEY_CONFIG=$D/openssl.cnf
# Edit this variable to point to
# your soon-to-be-created key
# directory.
#
# WARNING: clean-all will do
# a rm -rf on this directory
# so make sure you define
# it correctly!
export KEY_DIR=$D/keys
# Issue rm -rf warning
echo NOTE: when you run ./clean-all, I will be doing a rm -rf on $KEY_DIR
# Increase this to 2048 if you
# are paranoid. This will slow
# down TLS negotiation performance
# as well as the one-time DH parms
# generation process.
export KEY_SIZE=1024
# These are the default values for fields
# which will be placed in the certificate.
# Don't leave any of these fields blank.
# 定義你所在的國家,2個字元
export KEY_COUNTRY=CN
# 你所在的省份
export KEY_PROVINCE=Liaoning
# 你所在的城市
export KEY_CITY=Shenyang
# 你所在的組織
export KEY_ORG="ELM OpenVPN ORG"
# 你的單位
export KEY_OU="OpenVPN Service"
# 你的郵件地址
export KEY_EMAIL="elm@elm.freetcp.com"
-------------CUT Here-----------------
修改後儲存,下面我們開始什成keys,以下為shell命令 "#" 為提示符
#. vars #使修改的變數生效
NOTE: when you run ./clean-all, I will be doing a rm -rf on /etc/openvpn/easy-rsa/keys
(在每次執行其它指令碼前,如./clean-ca ,都要執行一次vars檔案)
#./clean-all #初始化keys目錄,建立所需要的檔案和目錄
#./build-ca #什成Root CA認證,用於簽發Server和Client認證,請保護好keys/ca.key檔案。
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) [CN]: #如果無需修改,直接斷行符號
State or Province Name (full name) [Liaoning]:
Locality Name (eg, city) [Shenyang]:
Organization Name (eg, company) [ELM OpenVPN ORG]:
Organizational Unit Name (eg, section) [OpenVPN Service]:
Common Name (eg, your name or your server's hostname) []:OpenVPN Root CA
Email Address [elm@elm.freetcp.com]:
# ls keys
ca.crt ca.key index.txt serial
我們可以看到ca.crt ca.key檔案已經什成了。
下面我們為伺服器產生 Diffie-Hellman 檔案
# ./build-dh #TLS server 需要使用的一個檔案
Generating DH parameters, 1024 bit long safe prime, generator 2
This is going to take a long time
.. .............................................................. .................................................................... .... ........ ......... .................................................... . .................................................................................................................................................................. ....................................... ................................. ............. ................................................................................. ....................................................... ............................. * * *
建立並簽發VPN Server使用的CA
# ./build-key-server server # server 為建立後的檔案名稱,分別為server.crt server.key
Generating a 1024 bit RSA private key
......................
...............
writing new private key to 'server.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) [CN]:
State or Province Name (full name) [Liaoning]:
Locality Name (eg, city) [Shenyang]:
Organization Name (eg, company) [ELM OpenVPN ORG]:
Organizational Unit Name (eg, section) [OpenVPN Service]:
Common Name (eg, your name or your server's hostname) []:Server No.1
Email Address [elm@elm.freetcp.com]:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /etc/openvpn/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:'Liaoning'
localityName :PRINTABLE:'Shenyang'
organizationName :PRINTABLE:'ELM OpenVPN ORG'
organizationalUnitName:PRINTABLE:'OpenVPN Service'
commonName :PRINTABLE:'Server No.1'
emailAddress :IA5STRING:'elm@elm.freetcp.com'
Certificate is to be certified until Feb 26 14:43:44 2015 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
接下來為VPN Client頒發CA認證,如果以後要為其他Client頒發認證,直接使用build-key命令簽發新認證。
# ./build-key elm
Generating a 1024 bit RSA private key
........
....................
writing new private key to 'elm.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) [CN]:
State or Province Name (full name) [Liaoning]:
Locality Name (eg, city) [Shenyang]:
Organization Name (eg, company) [ELM OpenVPN ORG]:
Organizational Unit Name (eg, section) [OpenVPN Service]:
Common Name (eg, your name or your server's hostname) []:ELM #注意Common Name最好不要相同,如果相同[server要加duplicate-cn選項],那麼Email地址也不能相同
Email Address [elm@elm.freetcp.com]:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /etc/openvpn/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:'Liaoning'
localityName :PRINTABLE:'Shenyang'
organizationName :PRINTABLE:'ELM OpenVPN ORG'
organizationalUnitName:PRINTABLE:'OpenVPN Service'
commonName :PRINTABLE:'ELM'
emailAddress :IA5STRING:'elm@elm.freetcp.com'
Certificate is to be certified until Feb 26 14:45:36 2015 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
為防止惡意攻擊(如DOS、UDP port flooding),我們產生一個"HMAC firewall"
#openvpn --genkey --secret keys/ta.key
產生憑證撤銷鏈檔案,防止日後有人丟失認證,被非法使用者接入VPN
#./make-crl vpncrl.pem
Using configuration from /etc/openvpn/easy-rsa/openssl.cnf
到現在為止,一切準備就緒,下面開始寫設定檔,為了縮小篇幅,把原有注釋都去掉了。
Server使用的設定檔server.conf
----------------CUT Here-------------
#申明本機使用的IP地址,也可以不說明
;local a.b.c.d
#申明使用的連接埠,預設1194
port 1194
#申明使用的協議,預設使用UDP,如果使用HTTP proxy,必須使用TCP協議
;proto tcp
proto udp
#申明使用的裝置可選tap和tun,tap是二層裝置,支援鏈路層協議。
#tun是ip層的點對點通訊協定 (PPP),限制稍微多一些,本人習慣使用TAP裝置
dev tap
;dev tun
#OpenVPN使用的ROOT CA,使用build-ca產生的,用於驗證客戶是認證是否合法
ca ca.crt
#Server使用的認證檔案
cert server.crt
#Server使用的認證對應的key,注意檔案的許可權,防止被盜
key server.key # This file should be kept secret
#CRL檔案的申明,被撤銷憑證鏈,這些認證將無法登入
crl-verify vpncrl.pem
#上面提到的產生的Diffie-Hellman檔案
dh dh1024.pem
#這是一條命令的合集,如果你是OpenVPN的老使用者,就知道這條命令的來由
#這條命令等效於:
# mode server #OpenVPN工作在Server模式,可以支援多client同時動態接入
# tls-server #使用TLS加密傳輸,本端為Server,Client端為tls-client
#
# if dev tun: #如果使用tun裝置,等效於以下配置
# ifconfig 10.8.0.1 10.8.0.2 #設定本地tun裝置的地址
# ifconfig-pool 10.8.0.4 10.8.0.251 #說明OpenVPN使用的位址集區(用於分配給客戶),分別是起始地址、結束位址
# route 10.8.0.0 255.255.255.0 #增加一條靜態路由,省略下一跳地址,下一跳為對端地址,這裡是: 10.8.0.2
# if client-to-client: #如果使用client-to-client這個選項
# push "route 10.8.0.0 255.255.255.0" #把這條路由發送給用戶端,客戶串連成功後自動加入路由表,省略了下一跳地址: 10.8.0.1
# else
# push "route 10.8.0.1" #否則發送本條路由,這是一個主機路由,省略了子網路遮罩和下一跳地址,分別為: 255.255.255.255 10.8.0.1
#
# if dev tap: #如果使用tap裝置,則等效於以下命令
# ifconfig 10.8.0.1 255.255.255.0 #配置tap裝置的地址
# ifconfig-pool 10.8.0.2 10.8.0.254 255.255.255.0 #用戶端使用的位址集區,分別是起始地址、結束位址、子網路遮罩
# push "route-gateway 10.8.0.1" #把環境變數route-gateway傳遞給客戶機
#
server 10.8.0.0 255.255.255.0 #等效於以上命令
#用於記錄某個Client獲得的IP地址,類似於dhcpd.lease檔案,
#防止openvpn重新啟動後“忘記”Client曾經使用過的IP地址
ifconfig-pool-persist ipp.txt
#Bridge狀態下類似DHCPD的配置,為客戶分配地址,由於這裡工作在路由模式,所以不使用
;server-bridge 10.8.0.4 255.255.255.0 10.8.0.50 10.8.0.100
#通過VPN Server往Client push路由,client通過pull指令獲得Server push的所有選項並應用
;push "route 192.168.10.0 255.255.255.0"
;push "route 192.168.20.0 255.255.255.0"
#VPN啟動後,在VPN Server上增加的路由,VPN停止後自動刪除
;route 10.9.0.0 255.255.255.252
#Run script or shell command cmd to validate client
#virtual addresses or routes. 具體查看manual
;learn-address ./script
#其他的一些需要PUSH給Client的選項
#
#使Client的預設閘道指向VPN,讓Client的所有Traffic都通過VPN走
;push "redirect-gateway"
#DHCP的一些選項,具體查看Manual
;push "dhcp-option DNS 10.8.0.1"
;push "dhcp-option WINS 10.8.0.1"
#如果可以讓VPN Client之間相互訪問直接通過openvpn程式轉寄,
#不用發送到tun或者tap裝置後重新轉寄,最佳化Client to Client的訪問效率
client-to-client
#如果Client使用的CA的Common Name有重複了,或者說客戶都使用相同的CA
#和keys串連VPN,一定要開啟這個選項,否則只允許一個人串連VPN
;duplicate-cn
#NAT後面使用VPN,如果VPN長時間不通訊,NAT Session可能會失效,
#導致VPN串連丟失,為防止之類事情的發生,keepalive提供一個類似於ping的機制,
#下面表示每10秒通過VPN的Control通道ping對方,如果連續120秒無法ping通,
#認為串連丟失,並重新啟動VPN,重新串連
#(對於mode server模式下的openvpn不會重新串連)。
keepalive 10 120
#上面提到的HMAC防火牆,防止DOS攻擊,對於所有的控制資訊,都使用HMAC signature,
#沒有HMAC signature的控制資訊不予處理,注意server端後面的數字肯定使用0,client使用1
tls-auth ta.key 0 # This file is secret
#對資料進行壓縮,注意Server和Client一致
comp-lzo
#定義最大串連數
;max-clients 100
#定義運行openvpn的使用者
user nobody
group nobody
#通過keepalive檢測逾時後,重新啟動VPN,不重新讀取keys,保留第一次使用的keys
persist-key
#通過keepalive檢測逾時後,重新啟動VPN,一直保持tun或者tap裝置是linkup的,
#否則網路連接會先linkdown然後linkup
persist-tun
#定期把openvpn的一些狀態資訊寫到檔案中,以便自己寫程式計費或者進行其他動作
status openvpn-status.log
#記錄日誌,每次重新啟動openvpn後刪除原有的log資訊
log /var/log/openvpn.log
#和log一致,每次重新啟動openvpn後保留原有的log資訊,新資訊追加到檔案最後
;log-append openvpn.log
#相當於debug level,具體查看manual
verb 3
--------------Cut Here-----------------
把server.conf檔案儲存到/etc/opennvpn目錄中,並把使用easy-rsa下的指令碼什成的key都複製到/etc/openvpn目錄下,命令如下:
#cd /etc/openvpn
#cp easy-rsa/keys/ca.crt .
#cp easy-rsa/keys/server.crt .
#cp easy-rsa/keys/server.key .
#cp easy-rsa/keys/dh1024.pem .
#cp easy-rsa/keys/ta.key .
#cp easy-rsa/keys/vpncrl.pem .
建立OpenVPN啟動指令碼,可以在原始碼目錄中找到,在sample-scripts目錄下的openvpn.init檔案,將其複製到/etc/init.d/目錄中,改名為openvpn
然後運行:
#chkconfig --add openvpn
#chkconfig openvpn on
立即啟動openenvpn
#/etc/init.d/openvpn start
接下來配置用戶端的設定檔client.conf:
Linux或Unix下使用副檔名為.conf Windows下使用的是.ovpn,並把需要使用的keys複製到設定檔所在目錄ca.crt elm.crt elm.key ta.key
-------------Cut Here---------------------
# 申明我們是一個client,配置從server端pull過來,如IP地址,路由資訊之類“Server使用push指令push過來的”
client
#指定介面的類型,嚴格和Server端一致
dev tap
;dev tun
# Windows needs the TAP-Win32 adapter name
# from the Network Connections panel
# if you have more than one. On XP SP2,
# you may need to disable the firewall
# for the TAP adapter.
;dev-node MyTap
# 使用的協議,與Server嚴格一致
;proto tcp
proto udp
#設定Server的IP地址和連接埠,如果有多台機器做負載平衡,可以多次出現remote關鍵字
remote 61.1.1.2 1194
;remote my-server-2 1194
# 隨機播放一個Server串連,否則按照順序從上到下依次串連
;remote-random
# 始終重新解析Server的IP地址(如果remote後面跟的是網域名稱),
# 保證Server IP地址是動態使用DDNS動態更新DNS後,Client在自動重新串連時重新解析Server的IP地址
# 這樣無需人為重新啟動,即可重新接入VPN
resolv-retry infinite
# 在本機不邦定任何連接埠監聽incoming資料,Client無需此操作,除非一對一的VPN有必要
nobind
# 運行openvpn使用者的身份,舊版本在win下需要把這兩行注釋掉,新版本無需此操作
user nobody
group nobody
#在Client端增加路由,使得所有訪問內網的流量都經過VPN出去
#當然也可以在Server的設定檔裡頭設定,Server配置裡頭使用的命令是
# push "route 192.168.0.0 255.255.255.0"
route 192.168.0.0 255.255.0.0
# 和Server配置上的功能一樣 如果使用了chroot或者su功能,最好開啟下面2個選項,防止重新啟動後找不到keys檔案,或者nobody使用者沒有許可權啟動tun裝置
persist-key
persist-tun
# 如果你使用HTTP代理串連VPN Server,把Proxy的IP地址和連接埠寫到下面
# 如果代理需要驗證,使用http-proxy server port [authfile] [auth-method]
# 其中authfile是一個2行的文字檔,使用者名稱和密碼各佔一行,auth-method可以省略,詳細資料查看Manual
;http-proxy-retry # retry on connection failures
;http-proxy [proxy server] [proxy port #]
# 對於無線裝置使用VPN的配置,看看就明白了
# Wireless networks often produce a lot
# of duplicate packets. Set this flag
# to silence duplicate packet warnings.
;mute-replay-warnings
# Root CA 檔案的檔案名稱,用於驗證Server CA認證合法性,通過easy-rsa/build-ca產生的ca.crt,和Server配置裡的ca.crt是同一個檔案
ca ca.crt
# easy-rsa/build-key產生的key pair檔案,上面產生key部分中有提到,不同客戶使用不同的keys修改以下兩行配置並使用他們的keys即可。
cert elm.crt
key elm.key
# Server使用build-key-server指令碼什成的,在x509 v3擴充中加入了ns-cert-type選項
# 防止VPN client使用他們的keys + DNS hack欺騙vpn client串連他們假冒的VPN Server
# 因為他們的CA裡沒有這個擴充
ns-cert-type server
# 和Server配置裡一致,ta.key也一致,注意最後參數使用的是1
tls-auth ta.key 1
# 壓縮選項,和Server嚴格一致
comp-lzo
# Set log file verbosity.
verb 4
--------------Cut Here---------------------
Linux下Client的OpenVPN的安裝方法一樣,只需要配置client.conf檔案,然後把client.conf ca.crt elm.crt elm.key ta.key複製到/etc/openvpn目錄即可啟動VPN。 (命令:service openvpn start)
Win下OpenVPN的安裝,WIN下有圖形介面的OpenVPN-GUI程式,http://openvpn.se
下載安裝,預設下一步就OK了,安裝完事後在托盤上出現一個新的表徵圖,把client.ovpn ca.crt elm.crt elm.key ta.key檔案拷貝到C:/Program Files/openvpn/config目錄下,然後點右鍵connect就OK了。
win下的openvpn在安裝時,預設已經加到系統的"服務"裡了,是"手動"的,可以把它改為"自動",系統啟動後就自動連接了。如果安裝沒安裝到系統的"服務"裡,也可以用下面的方法添加到"服務"裡去:
如果要將openvpn做成服務,運行:
openvpnsev.exe -install
這樣就可以在服務中找到openvpn服務了。
對於LinuxServer使用NAT的一些說明:
首先要把系統的Forward開啟,可以通過如下命令實現
echo 1 >; /proc/sys/net/ipv4/ip_forward
或者使用
sysctl -w net.ipv4.ip_forward=1
或者修改/etc/sysctl.conf檔案,增加
net.ipv4.ip_forward = 1
設定SNAT的規則,使用iptables命令
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth1 -j SNAT --to-source 192.168.1.2
OK,OpenVPN Client可以訪問內網了。