openswan(IPSEC) (server :linux
,client:winxp )
入門設定
作者:cschen33.tw@yahoo.com.tw
建立日期:2006/06/02
最後更新:2006/06/04
目的:
以前有設定過兩款VPN,但是主流還是ipsec因此這是必修的,且安全性比較高
server 端:
作業系統
FC3,FC4(均實作成功)
使用軟體:
openswan :ipsec的軟體,由於freeswan停止發展,它是另一個取代的軟體
l2tpd: winxp 使用 ipsec一定要加l2tp協定
ppp:連線軟體必用
ipsec-tools:是key的工具
client 端:
windows xp sp2
環境: winxp -> ip share router -> internet -> linux (ipsec server)
說明:
本文是實作兩種,
1.pre share key:使用固定的密碼來驗證,一般不建議使用在windows上,而是建立使用在linuxt對linux的連線上,並指定IP,以增加安全
2.憑證:建議使用這個方式連線,由於windows 使用的憑證不同於一般linux上使用的,這裡將一併介紹如何轉換
主要參考文件:
http://www.jacco2.dds.nl/networking/freeswan-l2tp.html
http://www.drh-consultancy.demon.co.uk/pkcs12faq.html
http://www.imacat.idv.tw/tech/sslcerts.html
預設連線步驟如下:
client->openswan->l2tpd->pppd
安裝軟體
yum install openswan
yum install ipsec-tools
l2tpd是extra的套件,如果你的yum沒有設定,請到網路上尋找例如:rpmfind.net ,並安裝它
linux server 端設定
openswan
建立rsa key,請選擇下面任選一種方法來設定
1.webmin
設定在網路的IPsec VPN Configure,第一次啟動時會叫你建立host rsa key (webmin 是一個perl寫的網頁管理程式)
2.命令列下設定
ipsec sethostkey --outfile /etc/ipsec.secrets
建立連線的檔案,你只要建立檔案為其副檔名conf放在/etc/ipsec.d下就可以了,請參照/etc/ipsec.d/example,假設如下
/etc/ipsec.d/l2tp-psk.conf
conn L2TP-PSK
authby=secret
auto=add
keyingtries=3
left=%defaultroute
leftprotoport=17/1701
pfs=no
rekey=no
right= %any #如果你的client 端是固定IP ,浮動使用%any
#rightca=%same
rightprotoport=17/%any
#authby=secret 使用pre share key 驗證
#auto=add 自動加入
#left:server端
#right:client端
#%any:任何,
#%defaultroute :預設路由
修改下面檔案
/etc/ipsec.secrets
%any
: PSK "key" #如果你的client 端是固定IP
,浮動使用%any
#其中"key"是你的per share key 你可以設定為你要的密碼
#上述使用格式 client :PSK "key"
#還有另一種server client : PSK "key" ,我測試時有點問題
l2tpd
/etc/l2tpd/l2tpd.conf
[lns default]
ip range =
192.168.0.128-192.168.0.130
local ip =
192.168.0.119
require chap = yes
refuse pap = yes
require
authentication = yes
name = LinuxVPNserver
ppp debug = yes
pppoptfile =
/etc/ppp/options.l2tpd
length bit = yes
# ip range 是指定給client端的 private ip(需要根據你的區網的private ip 設定,是區網內保留沒有用到的)
#local ip 是指定server端閘道用的private ip (它不是server
實體網卡的區網內ip,你要額外準備一個private ip給server 用)
ppp
這個是驗證的密碼
etc/ppp/chap-secrets
#
Secrets for authentication using CHAP
#
client
server
secret
IP addresses
使用者名稱
*
你的密碼
"*"
#任何IP是"*"
由於我的ppp的設定是使用預設值,因此我沒有多作說明,請自行參考其它文件
啟動server
servcie l2tpd start
service ipsec start
檢視你的 ipsec 設定
[root@xxxx]#
ipsec verify
Checking your system to see if IPsec got installed and started
correctly:
Version check and
ipsec
on-path
[OK]
Linux Openswan
U2.4.4/K2.6.12-1.1381_FC3 (netkey)
Checking for IPsec
support in
kernel
[OK]
Checking for RSA
private key
(/etc/ipsec.secrets)
[OK]
Checking that pluto
is
running
[OK]
Two or more
interfaces found, checking IP
forwarding
[OK]
Checking NAT and
MASQUERADEing
Checking for 'ip'
command
[OK]
Checking for
'iptables'
command
[OK]
Checking for
'setkey' command for NETKEY IPsec stack
support [OK]
Opportunistic
Encryption
Support
[DISABLED]
請確定除了最後一個外其它都是ok
並可以查看/var/log/secure 及/var/log/messages的記錄以除錯
client 設定
windows xp sp2 必須手動加入一個]register key(或是使用登錄檔ipsec_psk.reg,下載後點選匯入)
請執行windows 的:開始->執行->regedt32
進入下面的:
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\RasMan\Parameters]
新增DWORD,其名稱為
ProhibitIpSec,其值為1
"ProhibitIpSec"=dword:00000001
然後重新開機
接下來設定撥接
台->網路連線->建立一個新的連線->連線到我工作的地方的網路->虛擬私人網路連線
請依次輸入設定值
要變更的設定如下:
不要選取"要求資料加密"(ipsec已經加密了,因此不影響安全)
另點選"IPSect設定(P)",輸入之前在/etc/ipsec.secrets所輸入的key

再來選取L2tp IPSec VPN

建立完畢之後就可以正常連線測試
改為憑證作法
首先你要建立主機的憑證,有許多不同的作法,這裡是使用x509的作法,我暫時不詳細說明其作法只以最簡單的作法來說明,其它請你參考其它網路上的說明:
主機憑證
1.建立私key及憑證要求
openssl req -nodes
-new -keyout privkey.pem -out server.req
# 將主機憑證的指令合併為一個步驟 openssl req -new -x509 -nodes -out server.pem,直接就簽證不產生req
#請不要將主機憑證我個人憑證弄混了,主機憑證是自己簽,而個人憑證是使用主機憑證來簽,因此個人憑證最好不要使用上述用法
2.自簽x509
openssl x509 -req
-days 365 -sha1 -extensions v3_ca -signkey
privkey.pem -in server.req -out server.pem
-days :有效天數,你可以延長
-signkey:簽證使用的私key ,
將private.key 及server.pem放到合適位置
如:
privkey.pem
放到/etc/ipsec.d/private
server.pem
放到/etc/ipsec.d/certs
建立使用者憑證(重複本步驟建立你所需要的使用者憑證)
1.同上1
openssl
req -nodes -new -keyout user1.key -out
user1.req
2.自簽x509
openssl
x509 -req -days 365 -sha1
-extfile openssl.cnf -extensions v3_req -CAkey privkey.pem
-CA server.pem
-in user1.req -out
user1.pem
-CAserial server.srl
-CAcreateserial
#我省略了檔案所在目錄,請根據自己的目錄變更,使用者我使用user1也請改為你的使用者名稱,可以變更的以斜體展示
簽完後產生user1.pem
3.轉換為windows使用的pkcs#12 (使用者的私key+使用者憑證)
openssl pkcs12
-export -inkey user1.key
-in user1.pem
-certfile
server.pem
-out user1.p12
-name "VPN for
user1"
你必須輸入一個密碼,等一下會使用匯入用
產生user1.p12要送給使用者用
server端設定
在/etc/ipsec.d/新增一個檔案其副檔名為.conf,參考/etc/ipsec.d/examples/
l2tp-cert-orgWIN2KXP.conf
內容:
conn
l2tp-cert-orgWIN2KXP
authby=rsasig
auto=add
left=%defaultroute
leftcert=/etc/ipsec.d/certs/server.pem #這是我們先前建立主機憑證所在,你可以依據你的設定變更,
leftprotoport=17/0
leftrsasigkey=%cert
pfs=no
rekey=no
right=%any
rightca=%same
rightprotoport=17/1701
rightrsasigkey=%cert
clinet 端設定
1.匯入憑證
將user1.p12複製到windows下,對它點選兩下,可以匯入這個憑證,提示的密碼是先前的產生pkcs#12是輸入的
2.
接下來設定撥接
台->網路連線->建立一個新的連線->連線到我工作的地方的網路->虛擬私人網路連線
請依次輸入設定值
使用預設值,不必如pre share key 變更,不用輸入IPSec設定

如果你要使用網路芳鄰 wins
server 要設,而預設閘道器可能也要修改