Thứ Bảy, 26 tháng 3, 2011

INSTALL AND CONFIGURE VPN ON LINUX


(Cài đặt và cấu hình VPN trên Linux)



INDEX
  1. GROUP 1: CONFIGURE VPN SERVER
  2. GROUP 2: VPN SECURITY ON LINUX
  3. GROUP 3: PERFORM OPENVPN CLIENT ON WINDOWS MS
  4. GROUP 4: USING OpenVPN ON UBUNTU CLIENT
  5. GROUP 5: INTRODUCTION ABOUT Ethernet Bridging


The environment: 

I
P VPN-SERVER_INTERNAL: 192.168.2.2
IP VPN_SERVER_EXTERNAL: 192.168.0.74
IP CLIEN-External: 192.168.0.3

INSTALL VPN FROM DAG RESPO

# yum --enablerepo=dag -y install openvpn bridge-utils

CONFIGURE VPN SERVER

#cp /usr/share/doc/openvpn-2.0.9/sample-config-files/server.conf /etc/openvpn/
#vim /etc/openvpn/server.conf
Change contens the flowing:

# line 53, change
dev tap0
# line 78: change
ca /etc/openvpn/easy-rsa/keys/ca.crt
cert /etc/openvpn/easy-rsa/keys/server.crt
key /etc/openvpn/easy-rsa/keys/server.key
# line 87: change
dh /etc/openvpn/easy-rsa/keys/dh1024.pem
# line 96: make it comment
#server 10.8.0.0 255.255.255.0
# line 103: make it comment
#ifconfig-pool-persist ipp.txt
# line 115: change ( [VPN server's IP] [subnetmask] [IP range given to client] )
server-bridge 192.168.2.2 255.255.255.0 192.168.2.50 192.168.2.100
# line 125: change ( [VPN server's network] [subnetmask] )
push "route 192.168.2.0 255.255.255.0"
# line 267: change
status /var/log/openvpn-status.log
# line 276: change
log /var/log/openvpn.log
log-append /var/log/openvpn.log

CREATE CA CERTIFICATE AND KEY

#cp -r /usr/share/doc/openvpn-2.0.9/easy-rsa /etc/openvpn/
#cd /etc/openvpn/easy-rsa/
#mkdir keys
#vim vars
Change contens the flowing:
# line 45: change for your environment
export KEY_COUNTRY=VIETNAM
export KEY_PROVINCE=HANOI
export KEY_CITY=HANOI
export KEY_ORG="ait-linux.vn"
export KEY_EMAIL="root@ait-linux.vn"
#chmod 700 vars
#. ./vars
Note: When you run ./clean-all, I will be doing a rm –rf on /etc/openvpn/easy-rsa/keys
#chmod 700 clean-all
#./clean-all or source ./clean-all  ./clean-all
#chmod 700 build-ca
#./build-ca #process to create CA and KEY are be started
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) [JP]: # Enter
State or Province Name (full name) [Hiroshima]: # Enter
Locality Name (eg, city) # Enter
Organization Name (eg, company) # Enter
Organizational Unit Name (eg, section) []: # Enter
Common Name (eg, your name or your server’s hostname) []: server-ca
Email Address [root@server-linux.info]: #Enter
 Export to a file “ca.crt” & ca.key & serial
CREATE SERVER CERTIFICATE AND KEY

#chmod 700 build-key-server
#./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): # Enter
State or Province Name (full name): # Enter
Locality Name (eg, city): # Enter
Organization Name (eg, company) [server-linux.info]: # Enter
Organizational Unit Name (eg, section) []: # Enter
Common Name (eg, your name or your server's hostname) []: server # input
Email Address [root@server-linux.info]: # Enter

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: # Enter
An optional company name []: # Enter

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:'VN'
stateOrProvinceName :PRINTABLE:'HN'
localityName :PRINTABLE:'Hiroshima'
organizationName:PRINTABLE:ait-linux.vn
commonName:PRINTABLE:'server'
emailAddress:IA5STRING:'root@server-linux.info'
Certificate is to be certified until Sep 4 14:11:20 2018 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


--> Export: server.crt & server.key & server.csr

CREATE DIFFIE HELLMAN (DH)

#chmod 700 build-dh
#./build-dh
export the file: dh1024.pem
CREATE CLIENT CERTIFICATE AND KEY
(This key and cer used to all user)
#chmod 700 build-key-pass
#./build-key-pass client
Generating a 1024 bit RSA private key
......++++++
.......................++++++
writing new private key to 'client.key'
Enter PEM pass phrase: # set pass-phrase
Verifying - Enter PEM pass phrase: # verify
----- 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) [VN]: # Enter
State or Province Name (full name): Enter
Locality Name (eg, city): # Enter
Organization Name (eg, company) [server-linux.info]: # Enter
Organizational Unit Name (eg, section) []: # Enter
Common Name (eg, your name or your server's hostname) []: client # input
Email Address [root@server-linux.info]: # Enter

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: # Enter
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:'HN'
stateOrProvinceName:PRINTABLE:'VIETNAM'
localityName :PRINTABLE:'HANOI'
organizationName:PRINTABLE:'ait-linux.vn’
commonName:PRINTABLE:'client'
emailAddress:IA5STRING:'root@ait-linux.vn’

Certificate is to be certified until Sep 4 14:20:52 2018 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 entriesData Base Updated
--> Export to three files are “client.crt and client.key” & client.csr

GENERATE CERTIFICATES FOR ONE CLIENT ONLY

chmod 700 build-key
./build-key client1 -> export the file: client1.crt, .csr, .key
./build-key client2



CREATE BRIDGE NETWORKING AND STARTING SCRIPT FOR VPN SERVER 

[root@ns ~]#
cp /usr/share/doc/openvpn-2.0.9/sample-scripts/openvpn.init /etc/openvpn/
cp /usr/share/doc/openvpn-2.0.9/sample-scripts/bridge-stop /etc/openvpn/
cp /usr/share/doc/openvpn-2.0.9/sample-scripts/bridge-start /etc/openvpn/

#chmod 755 /etc/openvpn/bridge-start
#chmod 755 /etc/openvpn/bride-stop
#chmod 755 /etc/openvpn/openvpn.init
#vim /etc/openvpn/bridge-start

Change contents the fllowing:
# 18,19,20: change ( IP for bridge : subnetmask : broadcast address )

eth_ip="192.168.2.2"
eh_netmask="255.255.255.0"
eth_broadcast="192.168.2.255"

#vim /etc/sysctl.conf
Change contents the fllowing:
# line 7: change ( enable ip forward )
net.ipv4.ip_forward = 1

#cp /etc/rc.d/init.d/network /etc/rc.d/init.d/vpn
#vim /etc/rc.d/init.d/vpn
#add 2 line in line 168

/etc/openvpn/bridge-start
/etc/openvpn/openvpn.init start

# line 174: add these 2 lines stop)

/etc/openvpn/openvpn.init stop
/etc/openvpn/bridge-stop

#service vpn restart
Bringing up loopback interface: [  OK  ]
Bringing up interface eth0: [  OK  ]
Bringing up interface eth1: [  OK  ]
Wed Sep 3 23:05:57 2008 TUN/TAP device tap0 opened
Wed Sep 3 23:05:57 2008 Persist state set to: ON
Starting openvpn: [  OK  ]

#chkconfig - -add vpn
#chkconfig vpn on

Kết luận: Đến đây các bạn đã hoàn thành cấu hình cơ bản trên Server rồi đấy. Bây giờ công việc là: Copy 2 file cer (ca.crt & client.crt) và 1 file client.key lên máy Client. Sau đó chạy thử đi nhé. Còn copy thế nào và cấu hình Client ra sau cũng là bứoc hơi phức tạp chút thôi (Là dân IT đã đương đầu với nghề này thì sợ gì phải không.. càng khó càng lằng nhằng càng thích :). Ở dưới mình cũng đã nói qua rồi. Chỗ OpenVPN-Client đấy. Mà các bạn lưu ý nhé, bên trên tên file tạo ra cho guets là "client" --> Tức nhiên là cần nên tạo tên khác ví dụ như tuaninfo_netadmin chẳng hạn. Khi tạo ra sẽ không y/c password nhưng khi chạy trên Client thì nên change pass --> Mục đích là Security mà.

*Note:
- Phải mở cổng 1194 trên Firewall Or Modem, Nếu không mở là ko vô được đâu.
- Cần sử dụng mạng khác (3g, hay mạng nào đó để test).


GROUP 2: VPN SECURITY ON LINUX (AN NINH VPN TRÊN LINUX)

REVOKE THE CLIENT CERTIFICATE (Lấy lại Account của toàn bộ User hay một User nào đó chỉ định)

Reasons for wanting to revoke a certificate include (common):
Private key of user is compromised or stolen.
User forgets the password on the key.
Want to terminate a VPN user’access
The revoke-full script will generate a CRL (Certificate revocation list) file called is crl.pem in keys subdirectory. When the revoke-full script runs again, it will update to crl.pem file.
#cd easy-rsa
#. ./vars
#./revoke-full client2 #revoke the client2 certificate for using vpn.

To finish config, edit /etc/openvpn/config/server.conf file
add line : crl-verify /etc/openvpn/easy-rsa/2.0/keys/crl.pem
 Restart openvpn
Now, all connecting clients are verified in crl.pem file will result in connection being dropped.

HARDENING OPENVPN SECURITY
OpenVPN provides several mechanisms to add additional security layer (OpenVPN cung cấp một số cơ chế thêm lớp bảo mật bổ sung).
tls-auth
The tls adds a additional HMAC (Hash-based Message Authentication Code) signature to all SSL/TLS handshare packets for integiry verification (nhan dang tinh toan ven). Any UDP packet not beadring the correct HMAC signature can be dropped.
It can protect against:
DoS attacks or port flooding on the OpenVPN UDP port (tấn công hàng loạt các ports OpenVPN UDP).
Port scanning to dertermine which server UDP ports are listening.

Lỗ hổng tràn bộ đệm trong việc triển khai SSL/TLS

Khởi tạo quá trình bắt tay SSL/TLS (SS/TLS handshake) từ một máy trái phép.
Sử dụng tls-auth yêu cầu tạo khóa bảo vệ chi sẻ (share-secret key), chúng được sử dụng để bổ sung tới RSA certificate/key:
#openvpn - -genkey - -secret ta.key

Câu lệnh này sẽ tạo một key OpenVPN tĩnh và viết nó vào file ta.key. Key này sẽ được copy qua một kênh bảo mật tới máy server và client. nó có thể được đặt trong cùng thư mục với RSA.key và .crt file.
Trong file cấu hình server, thêm dòng sau:
tls-auth ta.key 0
Trong file cấu hình client, thêm dòng sau:
tls-auth ta.key 1

proto udp
Trong OpenVPN cho phép sử dụng 2 giao thức UDP & TCP, giao thức UDP sẽ bảo vệ khỏi cuộc tấn công DoS và port scanning tốt hơn hơn TCP.
proto udp

Unprivileged mode- Phương thức không đặc quyền (Linux only)

-  Trên Linux, có thể chạy hoàn toàn không đặc quyền, cấu hình có một chút phức tạp nhưng cung   cấp mức độ bảo mật tốt nhất.
Để làm được việc này, OpenVPN phải được cấu hình sử dụng giao diện iproute, điều này được thực hiện qua việc chỉ định - -enable-iproute2. Gói sudo nên được sử dụng trong hệ thống.
-  Cấu hình này sử dụng Linux để thay đổi quyền sử dụng thiết bị tun. Do đó user không có đặc quyền có thể truy cập nó. Ngoài ra nó sử dụng sudo để thực thi iproute, do đó các thuộc tính giao diện và bảng định tuyến có thể bị thay đổi.
Cấu hình OpenVPN:
-   Viết script sau và đặt nó vào /usr/local/sbin/unpriv-ip :
#!/bin/sh
sudo /sbin/ip $*
-  Thực hiện visudo, và thêm dòng sau để cho phép user ‘user1’ thực hiện /sbin/ip :
user ALL=(ALL) NOPASSWD: /sbin/ip
Ngoài ra có thể sử dụng cho nhóm, ví dụ cho nhóm users:
%user ALL=(ALL) NOPASSWD: /sbin/ip
Thêm dòng sau đây vào file cấu hình OPENVPN
dev tunX/tapX
iproute /usr/local/sbin/unpriv-ip
(Note: Phải chọn hằng X và chỉ định tun hoặc tap, không được cả 2)
Sau đây tạo tunX và cho phép ‘user1’ và nhóm ‘users’ truy cập nó
openvpn - -mktu -- dev tunX --type tun --user user1 --group users
Chạy OpenVPN trong khi người dùng không có đặc quyền. Hơn nữa chế độ bảo mật có thể được thêm bằng việc kiểm tra các thông số ở script /usr/local/sbin/unpriv-ip

Larger RSA keys

(Note: RSA- là thuật toán mã hóa khóa công khai, là thuật toán đầu tiên phù hợp với việc tạo ra chữ ký điện tử và mã hóa. RSA sử dụng phổ biến trong thương mại điện tử và đảm bảo an toàn với điều kiện độ dài khóa đủ lớn)
Kích cỡ khóa RSA được điểu khiển bằng KEY_SIZE có thể thay đổi trong file easy-rsa/vars. Nó phải được cài đặt trước khi bất kỳ khóa nào được khởi tạo. Cài đặt 1024 (default), giá trị này có thể chấp nhận được và có thể tăng lên 2048 mà không ảnh hưởng tới hiệu năng VPN.

Larger symmetric keys (Các khóa đối xứng lớn hơn)

Theo mặc định OpenVPN sử dụng Blowfish- một lọai 128 bits mã hóa cân đối (symmetrical cipher)
OpenVPN tự động hỗ trợ bất kỳ kiểu mã hóa nào được hỗ trợ bởi thư viện OpenSSL  có thể sử dụng các cỡ khóa lớn hơn.
Example: 256-bit-version of AES (Advanced Encryption Standard) có thể được sử dụng bằng cách thêm dòng sau tới file cấu hình cả server và client.
cipher AES-256-CBC

Keep the root key (ca.key) on a standalone machine without a network connection (Giữ khóa root trên một máy standalone thông qua kết nối mạng.

Một trong những lợi ích an ninh sử dụng X509 PKI là ‘ca.key’ không nằm trên máy chủ OpenVPN. Trong một môi trường bảo mật cao, ta có thể chỉ định một máy đặc biệt cho các mục đích ký kết quan trọng, giữ cho máy này được bảo vệ vật lý và ngắt kết nối nó từ tất cả các mạng. Ổ đĩa rời có thể được sử đụng để di chuyển các file quan trọng khi cần thiết. Các biện pháp như vậy sẽ rất khó khăn cho kẻ tấn công thực hiện việc đánh cắp key root và hành vi trộm cắp vật lý của máy ký kết quan trọng.

GROUP 3: Introduction about Ethernet Bridging


        Ehernet Bridging chure yếu là kết hợp một interface Ethernet với một hoặc nhiều interface TAP ảo và nối chúng với nhau dưới sự hỗ trợ của một interface bridge. Các Ethernet bridge tương tự như một switch vật lý, có thể được sử dụng để kết nối nhiều interface Ethernet (vật lý or ảo) trên một máy trong khi chia sẻ một IP subnet
- Bằng cách chuyển tiếp một NIC vật lý với một OpenVPN-driven (định hướng). interface TAP tại 2 vị trí riêng biệt. Nó kết hợp 2 mạng Ethernet như một subnet Ethernet riêng biệt.
- Có 2 cách phân bổ IP address client:

  • Chỉ định dài IP client sử dụng server-bridge
  • Cấu hình DHCP server on LAN để cấp IP cho các VPN Client.
-  Virtual Bridge Interface

  • br
  • br0
-  Virtual TAP Interface

  • tap
  • tap0
 Áp dụng: Sau khi tìm hiểu xong Ethernet Bridge --> Ta đi thực hiện các bước cần thiết cài đặt interface cho đúng:

- Đảm báo packet bridge-utils được cài đặt.
- Sửa file script: bridge-start. Cài đặt các thông số br, tap, eth, eth_ip, eth_netmask và eth_broadcast tuân theo interface vật lý mà bạn muốn chuyển.
- Sử dụng command ifconfig để đưa ra các thông tin liên quan về mạng rồi điền vào các thông số bridge-start.
- Khi script bridge-start đã chạy --> Nó sẽ tạo một interface tap0 và chuyển nó với một interfacl đang hoạt động.

--> Next


Sửa file cấu hình,
Comment dòng dev tun và thay nó với dev tap0
Comment dòng bắt đầu với server và thay nó với
server-bridge 192.168.8.4 255.255.255.0 192.168.8.128 192.168.8.254

Bây giờ, cài đặt cho Linux Firewall chấp nhận các packet lưu thông từ các interface được tạo: tap0 và br0.
iptables -A INPUT -i tap0 -j ACCEPT
iptables -A INPUT -i br0 -j ACCEPT
iptables -A FORWARD -i br0 -j ACCEPT

Và giờ, OpenVPN bridge có thể started và stoped.

Reference: http://openvpn.net/index.php/open-so...-bridging.html

GROUP 4: PERFORM OPENVPN CLIENT ON WINDOWS MS

- Copy 3 file from VPN Server (ca.crt, client.crt, client.key) to C:/Program Files/OpenVPN/Config
- Copy the file "client.ovpn" from "C:/Program Files/OpenVPN/sample-config" to C:/Program Files/OpenVPN/Config and edit it like following example.

# the name specified when you made certificate
client

# protocol
proto udp

# device name specified in config on OpenVPN server
dev tap0

# specify OpenVPN server' global IP and port
# remote (OpenVPN server's grobal IP) (port)
remote 172.16.2.1 1194

# path for certificate and key
ca ca.crt
cert client1.crt
key client1.key

# keep name resolution
resolv-retry infinite

# no bind local port
nobind

# enable LZO compress
comp-lzo

# for re-connection
persist-key
persist-tun

# log level
verb 3

# the setting for getting parameter from server
pull

# the setting for gettingIP address from server
float

[3]
Copy "ca.crt", "client.crt", "client.key" made on the section of server's settigs under "C:Program FilesOpenVPN¤nfig".
[4]
Click the VPN icon and select "Connect".

References:
http://adityo.blog.binusian.org/?tag...tory-on-centos

ERRORS COMMON (Các lỗi thường gặp)


Khi Firewall chặn, lỗi khi kết nối Client có dạng như sau:

un Jan 02 16:31:36 2011 NOTE: OpenVPN 2.1 requires '--script-security 2' or higher to call user-defined scripts or executables
Sun Jan 02 16:31:36 2011 Re-using SSL/TLS context
Sun Jan 02 16:31:36 2011 LZO compression initialized
Sun Jan 02 16:31:36 2011 Control Channel MTU parms [ L:1574 D:138 EF:38 EB:0 ET:0 EL:0 ]
Sun Jan 02 16:31:36 2011 Socket Buffers: R=[8192->8192] S=[8192->8192]
Sun Jan 02 16:31:36 2011 Data Channel MTU parms [ L:1574 D:1450 EF:42 EB:135 ET:32 EL:0 AF:3/1 ]
Sun Jan 02 16:31:36 2011 Local Options hash (VER=V4): 'd79ca330'
Sun Jan 02 16:31:36 2011 Expected Remote Options hash (VER=V4): 'f7df56b8'
Sun Jan 02 16:31:36 2011 UDPv4 link local: [undef]
Sun Jan 02 16:31:36 2011 UDPv4 link remote: 192.168.5.1:1194
Sun Jan 02 16:32:36 2011 TLS Error: TLS key negotiation failed to occur within 60 seconds (check your network connectivity)
Sun Jan 02 16:32:36 2011 TLS Error: TLS handshake failed
Sun Jan 02 16:32:36 2011 TCP/UDP: Closing socket
Sun Jan 02 16:32:36 2011 SIGUSR1[soft,tls-error] received, process restarting
Sun Jan 02 16:32:36 2011 Restart pause, 2 second(s)

Qúa trình lỗi trên lặp đi lặp lại nhiều lần nếu không thao tác Disconect.



Lỗi chưa cài interface

Còn một lỗi trên Windows ta hay bắt gặp, đó là phần mềm OpenVPN-CLient không cài đặt được device tun or tap0 gì đó (Vì các bạn phải hiểu dev này phải được cài để thực hiện kết nối VPN qua Interface này...nó giống như New Connection trong Windows đấy)
Lỗi: Windows route add command failed [adaptive]: returned error code 1

Chạy openvpn program với Administrator account --> Vào chỗ Program ở cửa sổ Windows --> remove cái dev ở OpenVPn đi, sau đó add lại --> Nó sẽ tự động add dev.

Vậy là Finish rồi đấy, mong với kiến thức hiện tại. Có thể giúp cộng đồng. Các bạn cũng vậy nhé.


Using OpenVPN on Ubuntu Client (Sử dụng OpenVPN trên Ubuntu Linux)

Bước 1: Cài đặt

#apt-get install openvpn

Bước 2: Cấu hình
#sudo cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf /etc/openvpn
#vim client.conf

Sửa và đảm bảo các Option sau được thiết lập:

dev tap0 //Vì trên Server mình đã chủ động sử dụng tap0
proto udp
remote 117.6.3.223 1194

ca /etc/openvpn/key/ca.crt
cert /etc/openvpn/key/tuantb_client.crt
key /etc/openvpn/key/tuantb_client.key

;tls-auth ta.key 1

Bước 3: Restart OpenVPN client

→ # /etc/init.d/openvpn restart
& # /etc/init.d/networking restart //Nếu không restart lại mạng thì không được đâu nhé, vì dev tap0 cũng cần được update mà.

#chkconfig openvpn off //Nên chọn cái này nhé.

--> Finish

 
--

TABATECH
Cung cấp giải pháp lưu trữ cho doanh nghiệp. 


Giải pháp lưu trữ mềm dẻo, hỗ trợ đa nền tảng mạnh mẽ, kết nối dữ liệu đạt hiệu năng cao. Đảm bảo hệ thống được vận hành thông suốt trong quá trình sử dụng.

1. Giải pháp lưu trữ cho doanh nghiệp vừa và nhỏ
2. Giải pháp lưu trữ cho khối chính phủ và doanh nghiệp lớn
3. Giải pháp lưu trữ cho nhà cung cấp điện toán đám mây (Cloud Computing Strorage)

Chúng tôi sẵn sàng đồng hành cùng quý doanh nghiệp để mang lại những hỗ trợ hiệu quả nhất trong suốt quá trình vận hành hệ thống!

Mọi chi tiết xin liên hệ: 


TA BA TUAN
Email:    tuantb@tabatech.vn
Mobile:  0949.242.832 hoặc 01638.369.743

Website: http://tabatech.vn
Địa chỉ: Số 56, Ngõ 126, Hào Nam, Phường Ô Chợ Dừa, Quận Đống Đa, Thành phố Hà Nội, Việt Nam.