2009年8月17日 星期一

VPN solution 2 – openvpn

之前提到中小企安裝 vpn 的問題, 這篇文章將會講解如何安裝一種 SSL VPN – OpenVPN 

OpenVPN 是一種在 SSL 上建立的 OpenVPN, 在網絡工程學上說, 是最高層級的 ( OSI MODEL) 所以擁有極強的防火趥穿透力, 在大部份地方都可以建立 VPN 連線. 最重要的是它多平台支援 - Solaris, Linux, OpenBSD, FreeBSD, NetBSD, Mac OS X, and Windows 2000/XP/Vista , 而且是免費的.

以下介紹它的設置方法

網絡示意圖 (切記把 192.168.1.1 / 192.168.1.102 換成閣下的設置)


先安裝 UBUNTU LINUX
這裡有它的安裝方法 http://yubis.net/blog/yubis/ubuntu-ubuntu-install-method-367

在 Ubuntu 上輸入以安裝所需軟件
sudo apt-get install openvpn bridge-utils

把所需要的 OPENSSL 軟件拷貝
cp -R /usr/share/doc/openvpn/examples/ /etc/openvpn/

修改 SSL 加密設定
cd /etc/openvpn/examples/easy-rsa/2.0
vi vars
檔案vars 裹的都是一些制作 ssl 鑰匙時用作認證的資料, 特別留心的是 

export KEY_SIZE=2048

單位愈大, 加密時間愈長, 而駭客需要的破解時間都會增加

現在轉作 root 身份

sudo su

輸入以下指令, 以制作鑰匙

./vars
./clean-all
./build-ca #系統會詢問之前在 vars 設下的問題並提供在 vars 中的答案, 按下 ENTER 便可
制作服務器端的鑰匙
./build-key-server server
制作使用者和密碼

#制作密碼 (username = 你自己的使用者名稱)
./build-key-pass username
#不使用密碼 (username = 你自己的使用者名稱)
./build-key username
建立 Diffie Hellman key
./build-dh
#建立服務器 id key
openvpn --genkey --secret ta.key
建立 /etc/openvpn/server.conf 用作 openvpn 服務器的設定
# OpenVPN 服務器地址
local 192.168.1.102
port 1194
# TCP or UDP 服務器
proto udp
#所使用的網橋名稱
dev tap0
#之前制作出來的鑰匙位置
ca /etc/openvpn/examples/easy-rsa/2.0/keys/ca.crt
cert /etc/openvpn/examples/easy-rsa/2.0/keys/server.crt
key /etc/openvpn/examples/easy-rsa/2.0/keys/server.key
dh /etc/openvpn/examples/easy-rsa/2.0/keys/dh1024.pem
ifconfig-pool-persist ipp.txt
#服務器網橋的設定 (切記最後那兩個地址是沒被任何機器 / dhcp使用)
server-bridge 192.168.1.102 255.255.255.0 192.168.1.200 192.168.1.201
#needed to allow communication to internal network
client-to-client
keepalive 10 120
#所選擇的認證方法
#更快的選擇有 blowfish: "BF-CB"
cipher AES-128-CBC
#在這裡提供其他內網的地址 (如果亦想使用 openvpn)
push "route 192.168.2.0 255.255.255.0"
#服務器 id 的鑰匙
tls-auth ta.key 0
#compression for network speed
comp-lzo
# if packets are too large fragment them (only really useful if you have an old router)
#fragment 1400
#最大連接數目
max-clients 5
# do not use if running server on Windows
user nobody
group nogroup
persist-key
persist-tun
#log file settings
status openvpn-status.log
verb 3
# authentication plugin
#forces client to have a linux account in order to connect
plugin /usr/lib/openvpn/openvpn-auth-pam.so login

制作 /etc/init.d/bridge 用作開機時自動打開網橋服務


#!/bin/bash
# Create global variables
# 網橋的名稱 (自訂)
br="br0"
# 虛擬網卡的名稱 (自訂, 但需跟 /etc/openvpn/server.conf 相應)
# for example tap="tap0 tap1 tap2".
tap="tap0"
# Define physical ethernet interface to be bridged
# with TAP interface(s) above.
eth="eth0"
eth_ip="192.168.1.102"
eth_netmask="255.255.255.0"
eth_broadcast="192.168.1.255"
gw="192.168.1.1"
start_bridge () {
#################################
# Set up Ethernet bridge on Linux
# Requires: bridge-utils
#################################
for t in $tap; do
openvpn --mktun --dev $t
done
for t in $tap; do
ifconfig $t 0.0.0.0 promisc up
done
ifconfig $eth 0.0.0.0 promisc up
brctl addbr $br
brctl addif $br $eth
for t in $tap; do
brctl addif $br $t
done
ifconfig $br $eth_ip netmask $eth_netmask broadcast $eth_broadcast up
route add default gw $gw $br
}
stop_bridge () {
####################################
# Tear Down Ethernet bridge on Linux
####################################
ifconfig $br down
brctl delbr $br
for t in $tap; do
openvpn --rmtun --dev $t
done
ifconfig $eth $eth_ip netmask $eth_netmask broadcast $eth_broadcast up
route add default gw $gw $eth
}
case "$1" in
start)
echo -n "Starting Bridge"
start_bridge
;;
stop)
echo -n "Stopping Bridge"
stop_bridge
;;
restart)
stop_bridge
sleep 2
start_bridge
;;
*)
echo "Usage: $0 {start|stop|restart}" >&2
exit 1
;;
esac 
>
設定權限
chmod 755 /etc/init.d/bridge
設定開機時自動打開網橋

update-rc.d bridge defaults 15

打開 openvpn 服務

sudo /etc/init.d/bridge start
sudo openvpn /etc/openvpn/server.conf
這樣, openvpn 的服務器設定就完成了

如果覺得還是太煩的話, 可以參考 http://pfsense.org pfsense 是一種基於 freebsd 的系統, 有預載 openvpn

下一文章, 將會講解如何設定 openvpn 的客戶端

沒有留言:

張貼留言