歡迎您光臨本站 註冊首頁

OpenVPN在Windows下使用User/Pass驗證

←手機掃碼閱讀     火星人 @ 2014-03-04 , reply:0

OpenVPN在Windows下使用User/Pass驗證

OpenVPN在Windows下使用User/Pass驗證

對於Windows下使用User/Pass驗證已經是很久以前的承諾了,因為一開始一直都是在找尋
使用CMD(bat)文件檢查用戶名/密碼的方式,但是一直沒有結果,最後使用C寫了一個小程
序實現用戶名/密碼驗證。

轉載請註明出處,如有疑問訪問: http://wenzk.cublog.cn 反饋。

Windows安裝OpenVPN是很容易的,OpenVPN GUI下載網址:
http://openvpn.se/files/install_packages/openvpn-2.0.7-gui-1.0.3-install.exe

記得選上easy-rsa這部分腳本,安裝完畢后,easy-rsa在C:\Program Files\OpenVPN\目錄下。

把easy-rsa目錄下的vars.bat.sample改名為vars.bat,並且修改其內容:
==================================
set KEY_COUNTRY=CN
set KEY_PROVINCE=Liaoning
set KEY_CITY=Shenyang
set KEY_ORG=OpenVPN
set KEY_EMAIL=elm@elm.freetcp.com
==================================
其它部分就不用修改了,上面部分修改成你自己的配置。

把easy-rsa下的openssl.cnf.sample改成openssl.cnf。

然後進入cmd.exe
=============================================
Microsoft Windows XP [版本 5.1.2600]
(C) 版權所有 1985-2001 Microsoft Corp.

C:\Documents and Settings\Administrator>cd "\Program Files\OpenVPN\easy-rsa"

C:\Program Files\OpenVPN\easy-rsa>vars

C:\Program Files\OpenVPN\easy-rsa>clean-all.bat
系統找不到指定的文件。
已複製         1 個文件。
已複製         1 個文件。

C:\Program Files\OpenVPN\easy-rsa>

生成Root CA
格式: build-ca.bat
輸出: keys/ca.crt keys/ca.key
======================================================================
C:\Program Files\OpenVPN\easy-rsa>build-ca.bat
Loading 'screen' into random state - done
Generating a 1024 bit RSA private key
................................................................................
....++++++
....................................++++++
writing new private key to 'keys\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) :
State or Province Name (full name) :
Locality Name (eg, city) :
Organization Name (eg, company) :
Organizational Unit Name (eg, section) []:OpenVPN ORG
Common Name (eg, your name or your server's hostname) []:OpenVPN ROOTCA
Email Address [elm@elm.freetcp.com]:


生成dh1024.pem文件,Server使用TLS必須使用的一個文件。
格式: build-dh.bat
輸出: keys/dh1024.pem
============================================================================
C:\Program Files\OpenVPN\easy-rsa>build-dh.bat
Loading 'screen' into random state - done
Generating DH parameters, 1024 bit long safe prime, generator 2
This is going to take a long time
............................................................+.................+.
................................................................................
................................................................................
....+..................+...........................+..........................+.
.........................+............................+.+.......................
............................................+......+...+...............+........
..+...........+............+.....................+...+.........................+
.....+..............................................................+...........
...............+....................................+.......................+...
.....................................................+..........................
..................................................+.............................
......................................+..............+.+........................
+..........................................................................+....
................................................................+...............
......................................+...+.............................+.......
............+...........+................+......................................
.........+...........................................+..........................
................................................................................
.+.......+....+..............+..................................................
.........................................................................+......
..........+.....................................................................
................................................................................
...........................+....................................................
........+.......................................................................
...................................................+..............+.........+...
........................................+.........+...................+.........
.............+.......+..........+............+................+.................
................................................................................
................................................................................
.................................+.................................++*++*++*

C:\Program Files\OpenVPN\easy-rsa>

下面開始生成Server使用的證書:
格式: build-key-server.bat <filename>
輸出: keys/<filename>.crt <filename>.csr <filename>.key
================================================================================
C:\Program Files\OpenVPN\easy-rsa>build-key-server.bat server01
Loading 'screen' into random state - done
Generating a 1024 bit RSA private key
...............++++++
...........++++++
writing new private key to 'keys\server01.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) :
State or Province Name (full name) :
Locality Name (eg, city) :
Organization Name (eg, company) :
Organizational Unit Name (eg, section) []:OpenVPN ORG
Common Name (eg, your name or your server's hostname) []:Server01
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 openssl.cnf
Loading 'screen' into random state - done
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:'OpenVPN'
organizationalUnitName:PRINTABLE:'OpenVPN ORG'
commonName            :PRINTABLE:'Server01'
emailAddress          :IA5STRING:'elm@elm.freetcp.com'
Certificate is to be certified until Jul  7 13:33:23 2016 GMT (3650 days)
Sign the certificate? :y


1 out of 1 certificate requests certified, commit? y
Write out database with 1 new entries
Data Base Updated

C:\Program Files\OpenVPN\easy-rsa>

下面生成ta.key文件
格式: openvpn --genkey --secret keys/ta.key
輸出: keys/ta.key
=========================================================================
C:\Program Files\OpenVPN\easy-rsa>openvpn --genkey --secret keys/ta.key

C:\Program Files\OpenVPN\easy-rsa>

OK,那些keys就搞定了,下面開始寫配置文件。
server01.ovpn內容:
----------------CUT Here-------------
port 1194
; proto tcp
proto udp
; dev tap
dev tun
;dev-node MyTap
ca ca.crt
cert server01.crt
key server01.key  # This file should be kept secret
dh dh1024.pem
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
client-to-client
;duplicate-cn
keepalive 10 120
tls-auth ta.key 0 # This file is secret
auth-user-pass-verify checkpsw.exe via-env
client-cert-not-required
username-as-common-name
;cipher BF-CBC        # Blowfish (default)
;cipher AES-128-CBC   # AES
;cipher DES-EDE3-CBC  # Triple-DES
comp-lzo
;max-clients 100
user nobody
group nobody
persist-key
persist-tun
status status.log
;log         /var/log/openvpn.log
;log-append  /var/log/TCP_openvpn.log
verb 4
;mute 20
----------------CUT Here-------------
把配置文件放到C:\Program Files\OpenVPN\config\目錄下。
把easy-rsa\keys\下的 ca.crt server01.crt server01.key ta.key dh1024.pem
複製到server01.ovpn所在目錄。

同時下載本人寫的破爛驗證程序(checkpsw.exe)[不要仍磚塊]放到OpenVPN配置目錄下
程序在Windows XP SP2下測試通過,其他系統如果有問題,可以用源文件進行編譯
程序在壓縮包裡面:

見附件:)

在checkpsw.exe目錄下建立password.txt[用於存放用戶名&密碼]文件:
password.txt文件格式:
用戶名           密碼            是否活動(0/1) 中間用空格隔開
Username     Password    Active
-------------Cut Here---------------------
wzk         wzk         1
-------------Cut Here---------------------



Server的配置已經結束,可以啟動Server了,在右下角OpenVPN-gui上點右鍵,然後選擇connected。
需要伺服器啟動后自動運行,修改 "控制面板" 下面的 "管理工具" 下的 "服務" 把OpenVPN設置成自動啟動。

Client的配置文件:
client.ovpn
-------------Cut Here---------------------
client
dev tun
proto udp

remote 61.1.1.2 1194
;remote my-server-2 1194

;remote-random

resolv-retry infinite
nobind
user nobody
group nobody
route 192.168.0.0 255.255.252.0
persist-key
persist-tun

;http-proxy-retry # retry on connection failures
;http-proxy

ca ca.crt
auth-user-pass

ns-cert-type server
tls-auth ta.key 1
comp-lzo
# Set log file verbosity.
verb 4
--------------Cut Here---------------------
並且把easy-rsa/keys下的ca.crt ta.key一起放到Client的
<OPENVPN_HOME>\config目錄下。

Client的配置已經結束,可以連接Server了,在右下角OpenVPN-gui上點右鍵,然後選擇connected。


OK,整個配置就完成了。
《解決方案》

如果能做成對Radius驗證的就好了。。。。
《解決方案》

自己做啊

看看lz的源代碼,就是用getenv得到環境變數,然後進行驗證。

你還可以做成讀取資料庫(MySQL,SQL Server etc)

auth-user-pass-verify checkpsw.exe via-env就是用來設置環境變數,並且根據checkpsw.exe的返回值來進行判斷登陸是否成功,成功返回0,失敗返回1。
《解決方案》

原帖由 ykzj 於 2006-10-17 08:23 發表
自己做啊

看看lz的源代碼,就是用getenv得到環境變數,然後進行驗證。

你還可以做成讀取資料庫(MySQL,SQL Server etc)

auth-user-pass-verify checkpsw.exe via-env就是用來設置環境變數,並且根據check ...

呵呵,事實就這麼簡單:)
《解決方案》

太牛了
謝謝
《解決方案》

不錯,學習一下,配個出來
《解決方案》

下次 我用 vm 試試
《解決方案》

非常不錯,不過驗證程序在64位伺服器上運行不了
看過樓主的程序后,我寫了個bat文件可以用來驗證
code如下:

@echo off
for /f "tokens=1,2,3 delims= " %%i in (password.txt) do if %%i == %USERNAME% if %%j==%PASSWORD% if %%k == 1 exit /B 0
exit /B 1


2003,xp,2003(64)下測試正常
《解決方案》

上面代碼還有點瑕疵,
如果password.txt多個空行,可能導致空賬號密碼登入
檢查%username%和%password%不為空即可
《解決方案》

多謝,我正需要呢:wink: 正在搞OPEN VPN 謝謝樓主,出的這麼詳細!

[火星人 ] OpenVPN在Windows下使用User/Pass驗證已經有1845次圍觀

http://coctec.com/docs/service/show-post-11688.html