open*** 原理

    ×××直译就是虚拟专用通道,是提供给企业之间或者个人与公司之间安全数据传输的隧道,Open×××无疑是Linux下开源×××的先锋,

提供了良好的性能和友好的用户GUI。该软件最早由James Yonan编写。

    Open×××允许参与建立×××的单点使用预设的私钥,第三方证书,或者用户名/密码来进行身份验证。它大量使用了OpenSSL加密库,以及SSLv3/TLSv1协议。

Open×××能在Linux、xBSD、Mac OS X与Windows 2000/XP上运行。它并不是一个基于Web的×××软件,也不与IPsec及其他×××软件包兼容。

 

Open***的技术核心是虚拟网卡,其次是SSL协议实现,由于SSL协议在其它的词条中介绍的比较清楚了,这里重点对虚拟网卡及其在Open***的中的工作

机理进行介绍:

 虚拟网卡是使用网络底层编程技术实现的一个驱动软件,安装后在主机上多出现一个网卡,可以像其它网卡一样进行配置。服务程序可以在应用层打开虚拟网卡,如果应用软件(如IE)向虚拟网卡发送数据,则服务程序可以读取到该数据,如果服务程序写合适的数据到虚拟网卡,应用软件也可以接收得到。虚拟网卡在很多的操作系统下都有相应的实现,这也是Open***能够跨平台一个很重要的理由。

 在Open***中,如果用户访问一个远程的虚拟地址(属于虚拟网卡配用的地址系列,区别于真实地址),则操作系统会通过路由机制将数据包(TUN模式)或数据帧        (TAP模式)发送到虚拟网卡上,服务程序接收该数据并进行相应的处理后,通过SOCKET从外网上发送出去,远程服务程序通过SOCKET从外网上接收数据,并进行相应的处理后,发送给虚拟网卡,则应用软件可以接收到,完成了一个单向传输的过程,反之亦然。

Open×××所有的通信都基于一个单一的IP端口,默认且推荐使用UDP协议通讯,同时TCP也被支持。Open×××连接能通过大多数的代理服务器,并且能够在NAT的环境中很好地工作。服务端具有向客户端“推送”某些网络配置信息的功能,这些信息包括:IP地址、路由设置等。Open×××提供了两种虚拟网络接口:通用Tun/Tap驱动,通过它们,可以建立三层IP隧道,或者虚拟二层以太网,后者可以传送任何类型的二层以太网络数据。传送的数据可通过LZO算法压缩。IANA(Internet Assigned Numbers Authority)指定给Open×××的官方端口为1194。Open××× 2.0以后版本每个进程可以同时管理数个并发的隧道。

Open×××使用通用网络协议(TCP与UDP)的特点使它成为IPsec等协议的理想替代,尤其是在ISP(Internet service provider)过滤某些特定×××协议的情况下。

在选择协议时候,需要注意2个加密隧道之间的网络状况,如有高延迟或者丢包较多的情况下,请选择TCP协议作为底层协议,UDP协议由于存在无连接和重传机制,导致要隧道上层的协议进行重传,效率非常低下。


open*** 与pptp区别

pptp加密技术使用密码作为密钥,它的数据流载有可获取的混编密码。如果中间有人拦截到了数据流并且破译了密码(尽管可能但很难),那么他就可以破译你的信息。pptp可以应用到几乎所有的操作系统软件,无需安装任何软件。它也兼容许多移动设备,如iphone,ipad和Windows移动,安装简易。相比之下,open×××的安装比pptp要复杂一点,但只要按照正确的指示安装则无太大困难。但如果互联网服务提供商会阻止pptp连接,在此情况下我们便无计可施。

open×××使用非常强大的加密(Blowfish)技术。即使有人拦截你的数据流,他们也无计可施。这使得open×××比pptp安全得多。同时,open***也不受互联网服务提供商的约束。相比之下,我们选择使用open***作为备份,以保证任何时候服务的稳定,不受外界因素的干扰。


open*** 安装配置

特别注意:open*** 安装配置之前切忌关闭selinux

1.安装open***的源码包

wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm –ivh epel-release-6-8.noarch.rpm
yum install lzo-devel.x86_64 lzo.x86_64
yum install pam*
yum install open***.x86_64
yum install openssl.x86_64 openssl-devel.x86_64

2.创建CA相关证书

创建证书前修改主机名,后面可能会涉及


2.1使用yum安装的open***没有安装生成CA和证书的工具,需自行下载。

cat /usr/share/doc/open***-2.3.1/INSTALL|grep easy
To download easy-rsa go to: https://github.com/Open×××/easy-rsa
unzip easy-rsa-master.zip
cp -r easy-rsa-master/easy-rsa/ /etc/open***/
cd /etc/open***/easy-rsa/2.0

2.2.修改生成密钥证书模板文件

vi vars
#指定密钥长度
export KEY_SIZE=1024
#指定设置国家
export KEY_COUNTRY="CN"
#指定省份
export KEY_PROVINCE="SH"
#指定城市
export KEY_CITY="Shanghai"
#指定组织
export KEY_ORG="yx***"
#下面选项根据实际情况变更
export KEY_EMAIL="ckl@me-xx.com"
source ./vars
./clean-all

#创建CA

./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) [CN]:
State or Province Name (full name) [SH]:
Locality Name (eg, city) [Shanghai]:
Organization Name (eg, company) [yx***]:
Organizational Unit Name (eg, section) [MyOrganizationalUnit]:yx***
Common Name (eg, your name or your server's hostname) [yx*** CA]:yx***
Name [EasyRSA]:yx***
Email Address [ckl@me-xx.com]:

#创建dh

./build-dh
Generating DH parameters, 1024 bit long safe prime, generator 2
This is going to take a long time
...........................................+...........................................................+.........+...................................................................................+.........................................+.............+...........+......++*++*++*

#创建服务端证书

./build-key-server yx***
Generating a 1024 bit RSA private key
.................................................................................++++++
........................................................++++++
writing new private key to 'yx***.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) [SH]:
Locality Name (eg, city) [Shanghai]:
Organization Name (eg, company) [yx***]:
Organizational Unit Name (eg, section) [MyOrganizationalUnit]:yx***
Common Name (eg, your name or your server's hostname) [yx***]:yx***
Name [EasyRSA]:yx***
Email Address [ckl@me-xx.com]:
Please enterthe following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456
An optional company name []:yx***
Using configuration from /etc/open***/easy-rsa/2.0/openssl-1.0.0.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName :PRINTABLE:'CN'
stateOrProvinceName :PRINTABLE:'SH'
localityName :PRINTABLE:'Shanghai'
organizationName :PRINTABLE:'yx***'
organizationalUnitName:PRINTABLE:'yx***'
commonName :PRINTABLE:'yx***'
name :PRINTABLE:'yx***'
emailAddress :IA5STRING:'ckl@me-xx.com'
Certificate is to be certified until Jul 27 15:17:20 2023 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

#创建客户端证书

./build-key yxclient
Generating a 1024 bit RSA private key
...........++++++
.........++++++
writing new private key to 'yxclient.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) [SH]:
Locality Name (eg, city) [Shanghai]:
Organization Name (eg, company) [yx***]:
Organizational Unit Name (eg, section) [MyOrganizationalUnit]:yx***
Common Name (eg, your name or your server's hostname) [yxclient]:yxclient
Name [EasyRSA]:yxclient
Email Address [ckl@me-xx.com]:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456
An optional company name []:yx***
Using configuration from /etc/open***/easy-rsa/2.0/openssl-1.0.0.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName :PRINTABLE:'CN'
stateOrProvinceName :PRINTABLE:'SH'
localityName :PRINTABLE:'Shanghai'
:PRINTABLE:'yx***'
organizationalUnitName:PRINTABLE:'yx***'
commonName :PRINTABLE:'yxclient'
name :PRINTABLE:'yxclient'
emailAddress :IA5STRING:'ckl@me-xx.com'
Certificate is to be certified until Jul 27 15:18:34 2023 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

#防止恶意***,生成一个HMAC firewall

open*** --genkey --secret /etc/open***/easy-rsa/2.0/keys/ta.key

#由于系统自带的open***-auth-pam.so会出现认证错误,因此我们使用open***-2.0.9版本编译生成的open***-auth-pam.so

cd open***-2.0.9/plugin/auth-pam/
make
cp open***-auth-pam.so /etc/open***/


2.3配置服务端

服务端配置文件说明

vi server.config
#open***的默认监听端口
port 1194

#默认使用的协议
proto tcp

#采用路由模式
dev tun

#验证客户端证书是否合法
ca /etc/open***/easy-rsa/2.0/keys/ca.crt

#server端使用的证书
cert /etc/open***/easy-rsa/2.0/keys/yx***.crt

#dh文件
dh /etc/open***/easy-rsa/2.0/keys/dh1024.pem

#设定server端虚拟出来的网段
server 10.8.0.0 255.255.255.0

#防止Open*** 重启后忘记client端曾经使用过的IP地址
ifconfig-pool-persist ipp.txt

#给客户端push一条路由。这块的添加很重要,后面添加了谷歌的地址路由信息,此处添加的信息将push到客户端,如果不添加路由,客户端即使连接上,也无法上谷歌,此处非常重要。
push "route 10.8.0.0 255.255.255.0"
push "route 216.58.0.0 255.255.0.0"
push "route 74.125.0.0 255.255.0.0"
push "route 173.194.0.0 255.255.0.0"#给客户端push DNS信息
push "dhcp-option DNS 8.8.8.8"
push "dhcp-option DNS 8.8.8.4"

#可以让***的client之间互相访问,直接通过open***程序转发
client-cert-not-required

#使用普通的认证方式,下面是使用数据库验证
username-as-common-name
plugin /etc/open***/open***-auth-pam.so open***
duplicate-cn#Nat后面使用×××,如果长时间不通信,NAT session 可能会失效,导致***连接丢失。
#所有keepalive提供一个类似ping的机制

每10秒通过***的control通道ping对方,如果120秒无法ping通,则认为丢失,并重启***,重新连接
keepalive 10 120

#最多允许的客户端
max-clients 20 
user nobody
group nobody

#通过keepalive检测超时后,重新启动***,不重新读取keys,保留第一次使用的keys
persist-key

#通过keepalive检测超时后,重新启动***,一直保持tun或tap设备是linkup的,否则网络连接会先linkdown然后linkup
persist-tun

#把open***的状态写入日志中
status open***-status.log

#open***重启后会删除log内容,log-append则是追加log内容,并不删除。
log         open***.log
verb 3
#对数据进行压缩,注意server和client 一致
comp-lzo

3.配置数据库验证

数据库使用源码安装的

3.1.创建数据库及表

给予连接授权

mysql –uroot –proot –e 'GRANT ALL PRIVILEGES ON open***.* TO open***@localhost IDENTIFIED BY '123456';'
mysql –uroot –proot –e 'FLUSH PRIVILEGES;'

创建数据库:

mysql –uroot –proot –e 'create database open***;'

创建表:

mysql -uroot -proot open*** -e 'create table user (name char(20) NOT NULL,password char(128) default NULL,active int(10) NOT NULL DEFAULT 1,PRIMARY KEY (name));’

添加用户:

mysql -uroot -proot open*** -e 'insert into user (name,password) values ('ckl',password('123'));'


3.2.配置pam_mysql

数据库是源码安装的,所以必须要编译生成一个pam_mysql库:

tar zxvf  pam_mysql-0.7RC1.tar.gz 
cd  pam_mysql-0.7RC1
./configure -with-openssl
make
cp .libs/pam_mysql.so /lib/security/

编译pam认证的文件

# vi /etc/pam.d/open*** 
auth sufficient /lib/security/pam_mysql.so user=open*** passwd=ckl893 host=localhost db=open*** table=user usercolumn=name passwdcolumn=password where=active=1 crypt=2 verbose=1 
account required /lib/security/pam_mysql.so user=open*** passwd=ckl893 host=localhost db=open*** table=user usercolumn=name passwdcolumn=password where=active=1 crypt=2 verbose=1

注:crypt=2 是使用mysql 的password 函数加密, 3是MD5

使用saslauthd 测试pam_mysql连接

#saslauthd -a pam
#testsaslauthd -uckl -p123 -s open***
0: OK "Success."

验证成功

如果验证失败,查看/var/log/secure 日志中的相关报错

重启saslauthd

/etc/init.d/saslauthd restart

客户端配置文件说明

client
dev tun
proto tcp
remote 113.196.105.104 1194
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
auth-user-pass
ns-cert-type server
tls-auth ta.key 1
comp-lzo
verb 4

连接测试,如果连接成功,开启转发

iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT --to-source 114.134.22.22

补充,不需要增加谷歌或其他的路由,直接push一条全路由即可

push "0.0.0.0 0.0.0.0"