Centos6.3下利用open***部署遠程***服務

     今天看到羣裏有人提到open***,剛好放假在家,就順帶的研究了下。

     2年前自己還是小白的時候就在老單位連總部OA時用過open***這個客戶端,感覺還挺好用,而且覺得以後項目應該也能用得上,SO google了網上大量零碎資料,折騰了大半天,按照自己的理解就整理了如下文檔,給有興趣的朋友分享一下。

     最近一直想把自己的短板,也就是LINUX編程這塊好好研究下。。

     不扯了,開始幹活。。


-------------------華麗的分割線-----------------------


我的博客新站已經建好,更多新的內容即將在新站更新。。

歡迎訪問     http://www.showerlee.com



     open***是一款在LINUX網關服務器使用的開源的***軟件,顧名思義,其實就是用來打通一條安全的虛擬專用通道,實現用戶遠程辦公,獲取內網資源。

     該軟件可跨平臺在在Linux、xBSD、Mac OS X與Windows間使用,並利用openssl作爲加密庫,使用加密證書或用戶名/密碼來實現身份驗證,是一款不可多得的開源***解決方案。

     我們做這個實驗的目的就是模擬線上常見的公司外出人員在外需要訪問公司內網OA,實現遠程辦公自動化。

解決方案:


系統環境:centos6.3 x64

OPEN***: open***-2.3.0(附件有下載)


*** server:        eth0:192.168.100.90,eth1:172.24.30.1

*** client:        192.168.100.34

intranet server:   172.24.30.10


011256978.jpg


部署環境:


1.清空默認策略並重啓iptables

# iptables -t NAT -F

# iptables -F

# service iptables save

# service iptables restart


2.關閉SELINUX

# setenforce 0

# vi /etc/sysconfig/selinux

---------------

SELINUX=disabled

---------------


server端(路由模式):


一.網絡設置

1.開啓服務器端路由轉發功能

# vi /etc/sysctl.conf

---------------------

net.ipv4.ip_forward = 1

---------------------

# sysctl -p


2.設置nat轉發:

注:保證***地址池可路由出外網

# iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE


3.設置open***端口通過:

# iptables -A INPUT -p TCP --dport 1194 -j ACCEPT

# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

重啓iptables:

注:這裏提一下,INPUT策略是執行後即時生效的,POSTROUTING需要保存並重啓服務才能生效

# service iptables save

# service iptables restart

注:若想讓該服務器只提供open***等基本服務,可參照本文檔附件iptables腳本


3.時間同步(重要)

# ntpdate asia.pool.ntp.org


二.安裝依賴庫

# yum install -y openssl openssl-devel lzo lzo-devel pam pam-devel automake pkgconfig


三.安裝open***:

# wget -c http://swupdate.open***.org/community/releases/open***-2.3.0.tar.gz

# tar zxvf open***-2.3.0.tar.gz

# cd open***-2.3.0

# ./configure --prefix=/usr/local/open***

# make && make install

# mkdir -p /etc/open***

複製模板到open***配置目錄:

# cp -rf sample  /etc/open***/

複製open***配置文件到主目錄:

# cp /etc/open***/sample/sample-config-files/server.conf /etc/open***/

# cd ..


四.下載easy-rsa:

注:該包用來製作ca證書,服務端證書,客戶端證書,open***2.3.0該版本源碼不包含easy-rsa,所以需要單獨下載安裝用來配合open***實現證書生成。

# wget -c http://www.showerlee.com/down/easy-rsa-master.zip

# unzip easy-rsa-master.zip

# mv easy-rsa-master easy-rsa

# cp -rf  easy-rsa /etc/open***

# cd /etc/open***/easy-rsa/easy-rsa/2.0

修改_證書變量

# vi vars

修改如下參數

注:在後面生成服務端ca證書時,這裏的配置會作爲缺省配置

---------------------

export KEY_COUNTRY="CN"

export KEY_PROVINCE="SX"

export KEY_CITY="Xian"

export KEY_ORG="example"

export KEY_EMAIL="[email protected]"

---------------------

做SSL配置文件軟鏈:

# ln -s openssl-1.0.0.cnf openssl.cnf

修改vars文件可執行並調用

# chmod +x vars

# source ./vars

-----------------

NOTE: If you run ./clean-all, I will be doing a rm -rf on /etc/open***/easy-rsa/easy-rsa/2.0/keys

-----------------

注:也就是如果執行./clean-all,就會清空/etc/open***/easy-rsa/easy-rsa/2.0/keys下所有文件


開始配置證書:

1.清空原有證書:

# ./clean-all

注:下面這個命令在第一次安裝時可以運行,以後在添加完客戶端後慎用,因爲這個命令會清除所有已經生成的證書密鑰,和上面的提示對應


2.生成服務器端ca證書

# ./build-ca

注:由於之前做過缺省配置,這裏一路回車即可


3.生成服務器端密鑰證書, 後面這個open***.example.com就是服務器名,也可以自定義

# ./build-key-server open***.example.com

---------------------------

Generating a 2048 bit RSA private key

...................................................+++

..................................+++

writing new private key to 'open***.example.com.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) [SX]:

Locality Name (eg, city) [Xian]:

Organization Name (eg, company) [example]:

Organizational Unit Name (eg, section) []:

Common Name (eg, your name or your server's hostname)


[open***.example.com]:

Name [EasyRSA]:

Email Address [[email protected]]:


Please enter the following 'extra' attributes

to be sent with your certificate request

A challenge password []:123456

An optional company name []:example

Using configuration from /etc/open***/easy-rsa/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:'SX'

localityName          :PRINTABLE:'Xian'

organizationName      :PRINTABLE:'example'

commonName            :PRINTABLE:'open***.example.com'

name                  :PRINTABLE:'EasyRSA'

emailAddress          :IA5STRING:'[email protected]'

Certificate is to be certified until Jun 10 21:58:49 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

---------------------------


4.生成所需客戶端證書密鑰文件(名字任意,建議寫成你要發給的人的姓名,方便管理):

# ./build-key client1

# ./build-key client2

注:這裏與生成服務端證書配置類似,中間一步提示輸入服務端密碼,其他按照缺省提示一路回車即可。


5.再生成diffie hellman參數,用於增強open***安全性(生成需要漫長等待)

# ./build-dh


6.打包keys

# tar zcvf keys.tar.gz keys/


7.終端發送到客戶端備用

# yum install lrzsz -y

# sz keys.tar.gz


五.配置open*** server:

# vi /etc/open***/server.conf

注:可按照默認模板配置,本例爲自定義配置文件:

--------------------------

# 設置監聽IP,默認是監聽所有IP

;local a.b.c.d


# 設置監聽端口,必須要對應的在防火牆裏面打開

port 1194


# 設置用TCP還是UDP協議?

;proto tcp

proto tcp


# 設置創建tun的路由IP通道,還是創建tap的以太網通道

# 路由IP容易控制,所以推薦使用它;但如果如IPX等必須

# 使用第二層才能通過的通訊,則可以用tap方式,tap也

# 就是以太網橋接

;dev tap

dev tun


# Windows需要給網卡一個名稱,這裏設置,linux不需要

;dev-node MyTap


# 這裏是重點,必須指定SSL/TLS root certificate (ca),

# certificate(cert), and private key (key)

# ca文件是服務端和客戶端都必須使用的,但不需要ca.key

# 服務端和客戶端指定各自的.crt和.key

# 請注意路徑,可以使用以配置文件開始爲根的相對路徑,

# 也可以使用絕對路徑

# 請小心存放.key密鑰文件

ca /etc/open***/easy-rsa/easy-rsa/2.0/keys/ca.crt

cert /etc/open***/easy-rsa/easy-rsa/2.0/keys/open***.example.com.crt

key /etc/open***/easy-rsa/easy-rsa/2.0/keys/open***.example.com.key

# This file should be kept secret


# 指定Diffie hellman parameters.

dh /etc/open***/easy-rsa/easy-rsa/2.0/keys/dh2048.pem


# 配置***使用的網段,Open***會自動提供基於該網段的DHCP

# 服務,但不能和任何一方的局域網段重複,保證唯一

server 10.8.0.0 255.255.255.0


# 維持一個客戶端和virtual IP的對應表,以方便客戶端重新

# 連接可以獲得同樣的IP

ifconfig-pool-persist ipp.txt


# 配置爲以太網橋模式,但需要使用系統的橋接功能

# 這裏不需要使用

;server-bridge 10.8.0.4 255.255.255.0 10.8.0.50 10.8.0.100


# 爲客戶端創建對應的路由,以另其通達公司網內部服務器

# 但記住,公司網內部服務器也需要有可用路由返回到客戶端

;push "route 192.168.20.0 255.255.255.0"

push "route 172.24.30.0 255.255.255.0"


# 爲特定的客戶端指定IP或指定路由,該路由通常是客戶端後面的

# 內網網段,而不是服務端連接的網段

# ccd是/etc/open***下的目錄,其中建有希望限制的客戶端Common

# Name爲文件名的文件,並通過下面的命令寫入固定IP地址

# 例如Common Name爲client1,則在/etc/open***/ccd/client1寫有:

# ifconfig-push 10.9.0.1 10.9.0.2

;client-config-dir ccd

;route 192.168.40.128 255.255.255.248


# 爲可以對不同的客戶端設置防火牆等權限

# 可以讓其自動運行對應腳本,可參考man

;learn-address ./script


# 若客戶端希望所有的流量都通過***傳輸,則可以使用該語句

# 其會自動改變客戶端的網關爲***服務器,推薦關閉

# 一旦設置,請小心服務端的DHCP設置問題

;push "redirect-gateway"


# 用Open***的DHCP功能爲客戶端提供指定的DNS、WINS等

;push "dhcp-option DNS 10.8.0.1"

;push "dhcp-option WINS 10.8.0.1"


# 默認客戶端之間是不能直接通訊的,除非把下面的語句註釋掉

client-to-client


# 如果您希望有相同Common Name的客戶端都可以登陸

# 也可以註釋下面的語句,推薦每個客戶端都使用不用的Common Name

# 常用於測試

;duplicate-cn


# 設置服務端檢測的間隔和超時時間

keepalive 10 120


# 下面是一些對安全性增強的措施

# For extra security beyond that provided

# by SSL/TLS, create an "HMAC firewall"

# to help block DoS attacks and UDP port flooding.

#

# Generate with:

#   open*** --genkey --secret ta.key

#

# The server and each client must have

# a copy of this key.

# The second parameter should be 0

# on the server and 1 on the clients.

;tls-auth ta.key 0 # This file is secret


# Select a cryptographic cipher.

# This config item must be copied to

# the client config file as well.

;cipher BF-CBC        # Blowfish (default)

;cipher AES-128-CBC   # AES

;cipher DES-EDE3-CBC  # Triple-DES


# 使用lzo壓縮的通訊,服務端和客戶端都必須配置

comp-lzo


# 設置最大用戶數

;max-clients 100


# 讓Open***以nobody用戶和組來運行(安全)

;user nobody

;group nobody


# The persist options will try to avoid

# accessing certain resources on restart

# that may no longer be accessible because

# of the privilege downgrade.

persist-key

persist-tun


# 輸出短日誌,每分鐘刷新一次,以顯示當前的客戶端

status /var/log/open***/open***-status.log


# 缺省日誌會記錄在系統日誌中,但也可以導向到其他地方

# 建議調試的使用先不要設置,調試完成後再定義

log         /var/log/open***/open***.log

log-append  /var/log/open***/open***.log


# 設置日誌的級別

#

# 0 is silent, except for fatal errors

# 4 is reasonable for general usage

# 5 and 6 can help to debug connection problems

# 9 is extremely verbose

verb 3


# Silence repeating messages.  At most 20

# sequential messages of the same message

# category will be output to the log.

;mute 20        

--------------------------

創建日誌目錄:

# mkdir -p /var/log/open***/


啓動open*** server

# /usr/local/open***/sbin/open*** --config /etc/open***/server.conf &


設置開機啓動:

# echo "/usr/local/open***/sbin/open*** --config /etc/open***/server.conf > /dev/null 2>&1 &" >> /etc/rc.local


client端:


六.安裝WINDOWS客戶端(WIN7 64bit)

1.下載客戶端,並默認安裝:

http://***tech.googlecode.com/files/open***-2.1.1-gui-1.0.3-install-cn-64bit.zip


2.將服務端打包文件解壓,並將包內ca.crt、client1.crt、client1.key複製到客戶端C:\Program Files\Open***\config下.


3.在C:\Program Files\Open***\config下創建client.o***文件

內容如下:

-----------------------

# 定義是一個客戶端

client


# 定義使用路由IP模式,與服務端一致

;dev tap

dev tun


# 定義Windows下使用的網卡名稱,linux不需要

;dev-node MyTap


# 定義使用的協議,與服務端一致

;proto tcp

proto tcp


# 指定服務端地址和端口,可以用多行指定多臺服務器

# 實現負載均衡(從上往下嘗試)

remote 192.168.100.90 1194

;remote my-server-2 1194


# 若上面配置了多臺服務器,讓客戶端隨機連接

;remote-random


# 解析服務器域名

# Keep trying indefinitely to resolve the

# host name of the Open*** server.  Very useful

# on machines which are not permanently connected

# to the internet such as laptops.

resolv-retry infinite


# 客戶端不需要綁定端口

# Most clients do not need to bind to

# a specific local port number.

nobind


# 也是爲了讓Open***也nobody運行(安全)

# 注意:Windows不能設置

;user nobody

;group nobody


# Try to preserve some state across restarts.

persist-key

persist-tun


# 若客戶端通過HTTP Proxy,在這裏設置

# 要使用Proxy,不能使用UDP爲***的通訊協議

;http-proxy-retry # retry on connection failures

;http-proxy [proxy server] [proxy port #]


# 無線網絡有很多多餘的頭文件,設置忽略它

;mute-replay-warnings


# 重點,就是指定ca和客戶端的證書

ca ca.crt

cert client1.crt

key client1.key


# 如果服務端打開了PAM認證模塊,客戶端需要另其有效

;auth-user-pass


# 一些安全措施

# Verify server certificate by checking

# that the certicate has the nsCertType

# field set to "server".  This is an

# important precaution to protect against

# a potential attack discussed here:

#  http://open***.net/howto.html#mitm

#

# To use this feature, you will need to generate

# your server certificates with the nsCertType

# field set to "server".  The build-key-server

# script in the easy-rsa folder will do this.

;ns-cert-type server


# If a tls-auth key is used on the server

# then every client must also have the key.

;tls-auth ta.key 1


# Select a cryptographic cipher.

# If the cipher option is used on the server

# then you must also specify it here.

;cipher x


# 使用lzo壓縮,與服務端一致

comp-lzo


# Set log file verbosity.

verb 3


# Silence repeating messages

;mute 20

-----------------------


5.連接:

在右下角的open***圖標上右擊,選擇“Connect”,若能正常分配IP,則連接成功。


6.最終測試:

C:\Users\Administrator>ipconfig/all

---------------------------------------

...............



以太網適配器 本地連接* 12:


  連接特定的 DNS 後綴 . . . . . . . :

  描述. . . . . . . . . . . . . . . : TAP-Win32 Adapter V9

  物理地址. . . . . . . . . . . . . : 00-FF-45-FB-F5-E2

  DHCP 已啓用 . . . . . . . . . . . : 是

  自動配置已啓用. . . . . . . . . . : 是

  本地鏈接 IPv6 地址. . . . . . . . : fe80::848d:bd1d:c1f4:fb51%27(首選)

  IPv4 地址 . . . . . . . . . . . . : 10.8.0.6(首選)

  子網掩碼  . . . . . . . . . . . . : 255.255.255.252

  獲得租約的時間  . . . . . . . . . : 2013年6月15日 22:36:59

  租約過期的時間  . . . . . . . . . : 2014年6月15日 22:36:59

  默認網關. . . . . . . . . . . . . :

  DHCP 服務器 . . . . . . . . . . . : 10.8.0.5

  DHCPv6 IAID . . . . . . . . . . . : 453050181

.....................

----------------------------------


在*** client上ping intranet server 主機IP:172.24.30.10

C:\Users\Administrator>ping 172.24.30.10

-------------------------

正在 Ping 172.24.30.10 具有 32 字節的數據:

來自 172.24.30.10 的回覆: 字節=32 時間=2ms TTL=63

來自 172.24.30.10 的回覆: 字節=32 時間<1ms TTL=63

來自 172.24.30.10 的回覆: 字節=32 時間<1ms TTL=63

來自 172.24.30.10 的回覆: 字節=32 時間<1ms TTL=63

--------------------------


大功告成。。。


七.注意事項:

(這裏參考並感謝酒哥的“構建高可用LINUX服務器”一書)

1.公司如果有同事離職,如何註銷該用戶***證書:

注:這裏需保持open***服務正常開啓

# cd /etc/open***/easy-rsa/easy-rsa/2.0

# ./revoke-full client2

如果報錯,則註釋掉該目錄下openssl.cnf文件若干行內容,如下:

(實際情況執行上面的操作,直接可註銷該用戶)

-------------------------

#[pkcs11_section]

#engine_id = pkcs11

#dynamic_path = /usr/lib/engines/engine_pkcs11.so

#MODULE_PATH = $EVN::PKCS11_MODULE_PATH

#PIN = $ EVN::PKCS!!_PIN

#init =0  

-------------------------

重新註銷:

# ./revoke-full client2

若末行返回error23則賬號註銷成功,但需完全註銷掉還需做如下配置:

# vi /etc/open***/server.conf

末行添加如下內容保證每次在重啓加載open***配置文件時都會重新加載crl.pem文件:

-----------------------

crl-verify /etc/open***/easy-rsa/easy-rsa/2.0/keys/crl.pem

-----------------------

注:crl.pem爲註銷的用戶的黑名單,可以理解爲每次啓動open***時,加載一次黑名單操作,保證最新被吊銷的證書無法使用。


重啓open***:

# killall open***

# /usr/local/open***/sbin/open*** --config /etc/open***/server.conf &

在客戶端服務器使用client2證書驗證該證書是否能夠使用

最終確定該證書無法連接open***服務器


2.更改證書有效期,提高證書的安全性:

默認_證書的有效期是3650天,也就是10年

# cd /etc/open***/easy-rsa/easy-rsa/2.0

# vi pkitool

搜索到兩處默認有效期天數"3650",修改該爲你需要設置的天數保存即可

下次執行該腳本製作客戶端證書時,期限就會更改爲新的天數。


進階:

open***作爲內網server提供遠程***服務解決方案:

有充裕預算的公司可以搭建open***的負載均衡,這裏可以把兩臺服務器掛到內網交換機上,利用一臺防火牆隔離內外網,並做分別作到內網這兩臺open***服務器的兩條端口映射,保證兩臺服務器與其他內網服務器同網段,且兩臺open***配置相同。

014412208.jpg


這個方案其實適用於在項目後期,如果已經架設好防火牆,在既保留現有防火牆的情況下又想遠程客戶端訪問內網資源,其實就可以利用原有防火牆做一個到內網open***服務器1194端口的映射,只要能保證open***服務器與其他內網資源在同一網段,或者可路由網段,就能起到遠程***訪問功能。

(open***-server)

1.打開ip_forward(略)

2.做open***地址池網段數據可轉發出eth0口的策略

# iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE

3.保證open***配置文件做了到內網的路由

---------------------

push "route 192.168.100.0 255.255.255.0"

---------------------

其他配置與網關open***模式一致。


最後在客戶端配置文件(C:\Program Files\Open***\config\client.o***)裏添加如下內容:

------------------

remote 172.24.30.40 1194

remote 172.24.30.40 1195

.....

remote-random

------------------

這行客戶端會隨機連接這2臺服務器地址,從而利用其作爲代理訪問網內其他資源。


--------大功告成----------

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章