OpenVPN使用User/Pass驗證登錄
在之前的OpenVPN+CA中已經介紹了使用CA驗證登錄的方式,詳見:
http://bbs.chinaunix.net/forum/viewtopic.php?t=503434&show_type=new
本文主要介紹使用Username/Password方式驗證登錄VPN的方法,雖然使用的是User/Pass
方式登錄,但是在Server端仍然需要證書,這樣的VPN和web的HTTPs方式有點類似(不能等同),
只需Server端有證書,Client可以不提供自己的證書,Client只需驗證Server的合法性即可,
所以Client端只需ca.crt(根證書)即可。當然,由於Client不是使用證書驗證的,所以安全
性方面必然有所下降,但是省去了煩瑣的CA管理,我們可以通過用戶名和密碼來登錄VPN,
這樣使得VPN可以很容易和論壇、郵件系統或者其他統一驗證系統結合,使用現成的管理界面。
關於VPN的一些初步認識,可以從下面這個URL獲得:(E文的)
http://blog.chinaunix.net/resserver.php?blogId=2389&resource=OpenVPN%20and%20the%20SSL%20VPN%20Revolution.pdf
原文出自:
http://www.giac.org/certified_professionals/practicals/gsec/3985.php
文中覺得很有意義的語句是: A VPN is a site-to-site tunnel. Let me say that one more time, a VPN is a site-to-site tunnel.
這篇文章介紹了OpenVPN1.x,也簡單介紹了OpenVPN 2.x的一些新特性,同時也簡單的介紹了
其他VPN以及不同方式實現的VPN產品、軟體,有時間看看是很有必要的。
關於VPN比較通俗的理解,在OpenVPN的FAQ中找到的:
Imagine you had a direct physical wire (i.e. a long cable) connecting two computers (A and B) at different locations. On each computer there would be a /dev/longcable which would be a network device. You could route IP traffic over it, and do everything you could normally do with a network device.
下面開始介紹VPN的安裝和配置:
環境:
OS: FC2 (在公司網路出口處,作路由或者是NAT設備使用)
eth0: 61.1.1.2 (外網地址,直接與Internet相連)
eth1: 192.168.0.1 (內網地址,連接公司內部,假設公司內部使用192.168.0.0/22這4個C地址)
Client端硬體及網路環境配置:
OS: Windown 2000 XP 為主,部分Linux (配置文件通用)
單網卡,IP地址不固定
需要達到的目的:
VPN Client可以隨處通過User/Pass登錄VPN,訪問內網資源。
Server端配置
首先檢查pam-devel包是否安裝,否則從系統盤安裝改軟體包
# rpm -qa | grep pam
pam_smb-1.1.7-3.1
pam-0.77-40
pam_krb5-2.0.10-1
pam-devel-0.77-40
#
檢查Mysql是否安裝,確認mysql-devel包已經安裝,否則從系統盤安裝改軟體包
# rpm -qa | grep mysql
mysql-3.23.58-9
mysql-server-3.23.58-9
mysql-devel-3.23.58-9
#
檢查lzo包是否有安裝,如果沒有,可以到http://rpmfind.net去找
# rpm -qa | grep lzo
# wget ftp://rpmfind.net/linux/dag/fedora/2/en/i386/dag/RPMS/lzo-1.08-3.1.fc2.dag.i386.rpm
# rpm -ivh lzo-1.08-3.1.fc2.dag.i386.rpm
# wget ftp://rpmfind.net/linux/dag/fedora/2/en/i386/dag/RPMS/lzo-devel-1.08-3.1.fc2.dag.i386.rpm
# rpm -ivh lzo-devel-1.08-3.1.fc2.dag.i386.rpm
# rpm -qa | grep lzo
lzo-devel-1.08-3.1.fc2.dag
lzo-1.08-3.1.fc2.dag
#
下面開始編譯安裝OpenVPN
# wget http://mesh.dl.sourceforge.net/sourceforge/openvpn/openvpn-2.0_rc16.tar.gz
# rpmbuild -tb openvpn-2.0_rc16.tar.gz
# cd /usr/src/redhat/RPMS/i386/
# rpm -ivh openvpn-2.0_rc6-1.i386.rpm
為了能使用OpenVPN的PAM驗證插件,我們安裝pam_mysql使用MySQL資料庫存儲用戶數據,其它資料庫可以找相應的PAM驗證模塊
# wget http://internap.dl.sourceforge.net/sourceforge/pam-mysql/pam_mysql-0.5.tar.gz
# tar -zxvf pam_mysql-0.5.tar.gz
# cd pam_mysql
# make
# cp pam_mysql.so /lib/security/
配置資料庫
以管理員身份登錄資料庫:
mysql>; create database vpn;
mysql>; GRANT ALL ON vpn.* TO vpn@localhost IDENTIFIED BY 'vpn123';
mysql>; flush privileges;
mysql>; use vpn;
mysql>; CREATE TABLE vpnuser (
->; name char(20) NOT NULL,
->; password char(128) default NULL,
->; active int(10) NOT NULL DEFAULT 1,
->; PRIMARY KEY (name)
->; );
mysql>; insert into vpnuser (name,password) values('elm',password('elm'));
#創建vpn用戶,對vpn這個database有所有操作許可權,密碼為vpn123
#active不為1,無權使用VPN
#增加用戶 用戶名:elm 密碼:elm
配置pam_mysql模塊
創建/etc/pam.d/openvpn文件,文件內容如下:
===================CUT Here================
auth sufficient pam_mysql.so user=vpn passwd=vpn123 host=localhost db=vpn \
table=vpnuser usercolumn=name passwdcolumn=password \
where=active=1 sqllog=0 crypt=2
account required pam_mysql.so user=vpn passwd=vpn123 host=localhost db=vpn \
table=vpnuser usercolumn=name passwdcolumn=password \
where=active=1 sqllog=0 crypt=2
==================Cut Here=================
crypt(0) -- Used to decide to use MySQL's PASSWORD() function or crypt()
0 = No encryption. Passwords in database in plaintext. NOT recommended!
1 = Use crypt
2 = Use MySQL PASSWORD() function
下面可以測試pam_mysql是否工作正常,先檢查saslauthd是否安裝:
# rpm -qa | grep sasl
cyrus-sasl-plain-2.1.18-2
cyrus-sasl-md5-2.1.18-2
cyrus-sasl-devel-2.1.18-2
cyrus-sasl-2.1.18-2
#
有cyrus-sasl-2.1.18-2應該就可以了,如果沒有請安裝相應的軟體包,不安裝也行,可以通過其它方法測試
# saslauthd -a pam
# testsaslauthd -u elm -p elm -s openvpn
0: OK "Success."
#
恭喜,pam_mysql工作正常了,下面可以開始配置OpenVPN伺服器了。
配置VPN Server:
# cd
# cp -r /usr/share/openvpn/easy-rsa/ /etc/openvpn/
# cd /etc/openvpn/easy-rsa/
# vi vars
修改vars 文件
-----------------------------------------
# 定義你所在的國家,2個字元
export KEY_COUNTRY=CN
# 你所在的省份
export KEY_PROVINCE=Liaoning
# 你所在的城市
export KEY_CITY=Shenyang
# 你所在的組織
export KEY_ORG="ELM OpenVPN ORG"
# 你的郵件地址
export KEY_EMAIL="[email protected]"
-----------------------------------------
#使修改的環境變數生效
# . vars
NOTE: when you run ./clean-all, I will be doing a rm -rf on /etc/openvpn/easy-rsa/keys
#初始化keys目錄
# ./clean-all
#生成Root CA證書,用於簽發Server和Client證書,請保護好keys/ca.key文件。
# ./build-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) : #如果無需修改,直接回車
State or Province Name (full name) :
Locality Name (eg, city) :
Organization Name (eg, company) :
Organizational Unit Name (eg, section) []: OpenVPN Service
Common Name (eg, your name or your server's hostname) []:OpenVPN Root CA
Email Address [[email protected]]:
#查看生成的keys
# ls keys
ca.crt ca.key index.txt serial
#我們可以看到ca.crt ca.key文件已經生成了。
#面我們為伺服器生成 Diffie-Hellman 文件
#TLS server 需要使用的一個文件
# ./build-dh
Generating DH parameters, 1024 bit long safe prime, generator 2
This is going to take a long time
..+..............................................................+.................
...................................................+....+........+.........+.......
.............................................+.+...................................
...................................................................................
............................................+......................................
.+.................................+.............+.................................
................................................+..................................
.....................+.............................++*++*++*
#創建並簽發VPN Server使用的CA
# `server' 為創建后的文件名,分別為server.crt server.key
# ./build-key-server server
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) :
State or Province Name (full name) :
Locality Name (eg, city) :
Organization Name (eg, company) :
Organizational Unit Name (eg, section) []:OpenVPN Service
Common Name (eg, your name or your server's hostname) []:Server No.1
Email Address [[email protected]]:
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:'[email protected]'
Certificate is to be certified until Feb 26 14:43:44 2015 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
#為防止惡意攻擊(如DOS、UDP port flooding),我們生成一個"HMAC firewall"
# openvpn --genkey --secret keys/ta.key
#Server使用的配置文件server.conf
----------------CUT Here-------------
port 1194
;proto tcp
proto udp
;dev tap
dev tun
ca ca.crt
cert server.crt
key server.key
dh dh1024.pem
server 10.8.0.0 255.255.0.0
ifconfig-pool-persist ipp.txt
;client-to-client
;duplicate-cn
keepalive 10 120
tls-auth ta.key 0
plugin ./openvpn-auth-pam.so openvpn
client-cert-not-required
username-as-common-name
comp-lzo
;max-clients 100
user nobody
group nobody
persist-key
persist-tun
status openvpn-status.log
;log /var/log/openvpn.log
;log-append openvpn.log
verb 4
;mute 20
--------------Cut Here-----------------
;client-to-client #如果讓Client之間可以相互看見,去掉本行的註釋掉,否則Client之間無法相互訪問
;duplicate-cn #是否允許一個User同時登錄多次,去掉本行註釋后可以使用同一個用戶名登錄多次
plugin ./openvpn-auth-pam.so openvpn #說明使用的插件,openvpn為插件的參數,使用pam的servicesname
client-cert-not-required #不請求客戶的CA證書,使用User/Pass驗證
username-as-common-name #使用客戶提供的UserName作為Common Name
把server.conf文件保存到/etc/opennvpn目錄中,並把使用easy-rsa下的腳本什成的key都複製到/etc/openvpn目錄下,命令如下:
# cp keys/ca.crt ../
# cp keys/server.crt ../
# cp keys/server.key ../
# cp keys/dh1024.pem ../
# cp keys/ta.key ../
# cp /usr/share/openvpn/plugin/lib/openvpn-auth-pam.so ../
#立即啟動openenvpn
# /etc/init.d/openvpn start
#接下來配置客戶端的配置文件client.conf:
#Linux或Unix下使用擴展名為.conf Windows下使用的是.ovpn,並把需要使用的文件複製到配置文件所在目錄ca.crt ta.key
-------------Cut Here---------------------
client
;dev tap
dev tun
;proto tcp
proto udp
remote 61.1.1.2 1194
;remote my-server-2 1194
remote-random
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
auth-user-pass
ns-cert-type server
tls-auth ta.key 1
route 192.168.0.0 255.255.252.0
comp-lzo
verb 4
;mute 20
------------Cut Here-----------------------
auth-user-pass #詢問用戶名和密碼
Linux下Client的OpenVPN的安裝方法一樣,只是配置文件和keys上的不同,只要把client.conf ca.crt ta.key複製到/etc/openvpn目錄即可啟動VPN。
Win下OpenVPN的安裝,WIN下有圖形界面的OpenVPN-GUI程序,下載地址http://openvpn.se
這裡使用的是TUN設備,主要考慮到Client客戶多,VPN的效率和廣播的問題,選用TUN設備,因為客戶端可能是
Windows系統,Win系統TUN設備獲得的IP地址將會是/30的地址,所以有3*Client個地址浪費,所以地址池設置得比較大。
這樣你每次使用VPN登錄的時候,程序會自動詢問你得用戶名和密碼,輸入正確后就可以連接上VPN了,
連接VPN后所有訪問內網(192.168.0.0/22)的數據都從VPN經過。
如果Win的Client比較多,可以試著把ca.crt ta.key client.ovpn打包到安裝包程序里,具體操作方法參見:
http://openvpn.se/files/howto/openvpn-howto_roll_your_own_installation_package-Rev1.1.html
然後發布改軟體包即可,最好小心保管ta.key文件(防止Dos攻擊)。
#首先要把系統的Forward打開
# vi sysctl.conf
修改
# Controls IP packet forwarding
net.ipv4.ip_forward = 1
#IPTABLES的配置文件
# cat iptables
# Generated by iptables-save v1.2.1a on Tue Nov 6 19:50:51 2001
*nat
:PREROUTING ACCEPT
:POSTROUTING ACCEPT
:OUTPUT ACCEPT
-A POSTROUTING -s 192.168.0.0/255.255.252.0 -o eth0 -j SNAT --to-source 61.1.1.2
COMMIT
*filter
:INPUT DROP
:FORWARD ACCEPT
:OUTPUT ACCEPT
:BLOCK -
:ANTIVIRUS -
# block internal ip address
-A INPUT -i lo -j ACCEPT
-A INPUT -j BLOCK
-A INPUT -j ANTIVIRUS
-A BLOCK -s 192.168.0.0/16 -d 0/0 -j RETURN
-A BLOCK -s 172.16.0.0/12 -d 0/0 -j REJECT
-A BLOCK -s 10.0.0.0/8 -d 0/0 -j RETURN
-A BLOCK -s 127.0.0.0/8 -d 0/0 -j REJECT
-A BLOCK -s 0.0.0.0/8 -d 0/0 -j REJECT
-A BLOCK -s 169.254.0.0/16 -d 0/0 -j REJECT
-A BLOCK -s 192.0.2.0/24 -d 0/0 -j REJECT
-A BLOCK -s 204.152.64.0/23 -d 0/0 -j REJECT
-A BLOCK -s 224.0.0.0/3 -d 0/0 -j REJECT
-A INPUT -p icmp -j ACCEPT
# OSPFD
-A INPUT -d 224.0.0.0/24 -j ACCEPT
# sync time
-A INPUT -p udp -m udp --sport 123 -j ACCEPT
# accept dns
-A INPUT -p udp -m udp --sport 53 -j ACCEPT
# accept ssh from any
-A INPUT -p tcp -m tcp --dport 22 --syn -j ACCEPT
# accept dhcp request
-A INPUT -p udp -m udp --dport 67 -j ACCEPT
# OpenVPN 1194_UDP
-A INPUT -p udp -m udp --dport 1194 -j ACCEPT
# www
-A INPUT -p tcp -m tcp --dport 80 --syn -j ACCEPT
# keep stats
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -m state --state INVALID -j DROP
# Reject all packet to me
-A INPUT -p tcp -m tcp --syn -j REJECT --reject-with tcp-reset
-A INPUT -p udp -m udp -j REJECT
-A FORWARD -j ANTIVIRUS
-A ANTIVIRUS -p tcp -m tcp --dport 135:139 -j DROP
-A ANTIVIRUS -p tcp -m tcp --dport 445 -j DROP
-A ANTIVIRUS -p udp -m udp --dport 69 -j DROP
-A ANTIVIRUS -p udp -m udp --dport 135:139 -j DROP
-A ANTIVIRUS -p udp -m udp --dport 1434 -j DROP
COMMIT
#
Blog: http://elm.blog.edu.cn
http://blog.chinaunix.net/index.php?blogId=2389
[火星人 ] OpenVPN使用User/Pass驗證登錄已經有1399次圍觀