Author Topic: openvpn มีประโยชน์อย่างไร ไม่ต้องลงทุนเยอะ ถ้าเข้าใจการทำงาน  (Read 4671 times)

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
openvpn มีประโยชน์อย่างไร ไม่ต้องลงทุนเยอะ ถ้าเข้าใจการทำงานของ openvpn

มาในยุคที่ข้าวของเริ่มแพง หลายๆบริษัทอยากจะคุมงบประมาณการใช้จ่ายในเรื่องของ IT
หันมาใช้งาน opensource กันเยอะขึ้น ซึ่งทางบริษัทแพ็คเกตเลิฟฯ เองก็ได้มีโอกาสช่วยเหลือให้คำแนะนำ เรื่องการใช้งาน opensource แก่หลายๆบริษัทในประเทศไทย เลยอยากจะมาแบ่งปัน อีก1 tools ที่คิดว่ามีประโยชน์ครับ ตัว openvpn

ยิ่งถ้าใช้งานบน AWS หรือ GCP (Google cloud platform ) ก็ไม่ได้ยากมากมายครับ
วันนี้มีโจทย์อยู่ว่า ต้องการให้ client สามารถ vpn เข้าไปใน network เดียวกับ Private network บน GCP ได้
เพื่อที่จะได้ connect ผ่าน db client เข้าไปจัดการ database ได้ง่ายๆ
MacOS ใช้ตัว https://sequelpro.com/ ต่อเข้า DB ได้เลย
MacOS ใช้ตัว https://tunnelblick.net/ ต่อเป็น vpn client ได้เลยครับ

ขั้นแรกก็เริ่มด้วยการเลือก spec ของ instance ที่จะต้องการติดตั้ง openvpn server เลยครับ
ผมใช้เป็น ubuntu 16.04 และเลือก IP Forwarding เป็น on ด้วยนะครับ
เพราะถ้ามาเลือกทีหลังจะไม่สามารถเลือกได้ครับ บน GCP

ต่อไปทำการตั้งค่า Firewall policy ให้อนุญาตใช้งาน port 1194 บน protocol udp ได้ (default port ของ openvpn-server)
หรือจะทำการเปลี่ยน port ไปเป็น port อื่นก็แล้วแต่ความต้องการของผู้ดูแลระบบครับ

หลังจากนั้นเริ่มลง packet เลย
###Beginning install openvpn on ubuntu 16.04
sudo apt-get update
sudo apt-get install openvpn easy-rsa

Step 2: Configure the CA Variables

#make-cadir ~/openvpn-ca
#cd ~/openvpn-ca
#nano vars

###Edit value
. . .

export KEY_COUNTRY="US"
export KEY_PROVINCE="CA"
export KEY_CITY="SanFrancisco"
export KEY_ORG="Fort-Funston"
export KEY_EMAIL="me@myhost.mydomain"
export KEY_OU="MyOrganizationalUnit"

. . .

Change to
. . .

export KEY_COUNTRY=“TH”
export KEY_PROVINCE=“UBON”
export KEY_CITY=“Ubonratchatani City"
export KEY_ORG=“Packetlove”
export KEY_EMAIL="admin@ubon.town”
export KEY_OU="Community"
export KEY_NAME="server"
. . .

####Build the Certificate Authority
cd ~/openvpn-ca
source vars
./clean-all
./build-ca

###Create the Server Certificate, Key, and Encryption Files
./build-key-server server
./build-dh
openvpn --genkey --secret keys/ta.key

##Generate a Client Certificate and Key Pair สร้างให้กับ client ที่จะ connect เข้ามาเป็น .ovpn
cd ~/openvpn-ca
source vars
./build-key client1
        •
##Configure the OpenVPN Service as port 1194(udp) fake
cd ~/openvpn-ca/keys
sudo cp ca.crt ca.key server.crt server.key ta.key dh2048.pem /etc/openvpn
gunzip -c /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz | sudo tee /etc/openvpn/server.conf

#vi /etc/openvpn/server.conf

tls-auth ta.key 0 # This file is secret
key-direction 0
cipher AES-128-CBC
auth SHA256

user nobody
group nogroup

push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 208.67.222.222"
push "dhcp-option DNS 208.67.220.220"

# Optional!
port 1194
proto udp
cert server.crt
key server.key
####### End of configure server.conf #########

ทำการปรับให้ ip forwarding packet ใน OS
#vi /etc/sysctl.conf
net.ipv4.ip_forward=1

อัฟเดท configure sysctl
#sysctl -p

###Check interface for do nat postrouting
ip route | grep default
###default via 10.10.0.1 dev ens4

###ทำการเพิ่ม nat rule for postrouting with card interface name
vi /etc/ufw/before.rules

# START OPENVPN RULES
# NAT table rules
*nat
:POSTROUTING ACCEPT [0:0]
# Allow traffic from OpenVPN client to ens4 (change to the interface you discovered!)
-A POSTROUTING -s 10.99.0.0/8 -o ens4 -j MASQUERADE
COMMIT
# END OPENVPN RULES

vi /etc/default/ufw
DEFAULT_FORWARD_POLICY="ACCEPT"

###Add fw rule into ufw
sudo ufw allow 1194/udp
sudo ufw allow OpenSSH
###Reload ufw rule
sudo ufw disable
sudo ufw enable

###Start openvpn server
systemctl start openvpn@server
systemctl status openvpn@server
systemctl enable openvpn@server

ip addr show tun0
Output
4: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default qlen 100
    link/none
    inet 10.99.0.1 peer 10.99.0.2/32 scope global tun0
       valid_lft forever preferred_lft forever


####Next step
#mkdir -p ~/client-configs/files
#chmod 700 ~/client-configs/files
#cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf ~/client-configs/base.conf

#nano ~/client-configs/base.conf

### Begin of base.conf and change server_IP_address to your global ip of listen interface ####
remote server_IP_address 1194
proto tcp
user nobody
group nogroup
###Comment for ca , cert , key due to added in .ovpn file
#ca ca.crt
#cert client.crt
#key client.key
cipher AES-128-CBC
auth SHA256
key-direction 1
# script-security 2
# up /etc/openvpn/update-resolv-conf
# down /etc/openvpn/update-resolv-conf
###เพิ่ม configure นี้จะทำให้เวลาต่อเข้ามาแล้ว สามารถ connect ไปวงของ google-cloud(10.10.0.0/24) ได้ครับ
push "route 10.10.0.0 255.255.255.0"

##### End of base.conf ####


###Last step create make_config.sh
nano ~/client-configs/make_config.sh
#!/bin/bash

# First argument: Client identifier

KEY_DIR=~/openvpn-ca/keys
OUTPUT_DIR=~/client-configs/files
BASE_CONFIG=~/client-configs/base.conf

cat ${BASE_CONFIG} \
    <(echo -e '<ca>') \
    ${KEY_DIR}/ca.crt \
    <(echo -e '</ca>\n<cert>') \
    ${KEY_DIR}/${1}.crt \
    <(echo -e '</cert>\n<key>') \
    ${KEY_DIR}/${1}.key \
    <(echo -e '</key>\n<tls-auth>') \
    ${KEY_DIR}/ta.key \
    <(echo -e '</tls-auth>') \
    > ${OUTPUT_DIR}/${1}.ovpn

#chmod 700 ~/client-configs/make_config.sh

####Generate config for client1
#cd ~/client-configs
#./make_config.sh client1
#ls ~/client-configs/files
client1.ovpn

ก็ทำการ export file client1.ovpn ไปยัง Client เพื่อให้ connect ผ่าน VPN client program ได้เลยครับผม
หลังจาก connect เครื่อง client ก็จะได้ IP อยู่ในวง 10.99.0.x ครับ ก็จะสามารถคุยกับ วง 10.10.0.x
ได้เพราะมี routing แนบไปด้วยเวลาต่อ vpn ด้วยสำเร็จ
ลองใช้งานกันดูนะครับ ไม่ยากเลย : )
กอล์ฟ [at] Packetlove.com

##Sent this file to client to connect with vpnclient program
##macos = tunnelblick
« Last Edit: กรกฎาคม 01, 2017, 07:10:39 PM by golfreeze »

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
การลบและสร้าง vpn profile ให้กับ client user

ยกตัวอย่างต้องการสร้าง profile ใหม่กับ client1 และลบ profile ของ client3 เมื่อลาออกไปครับ

##Generate a Client Certificate and Key Pair สร้างให้กับ client ที่จะ connect เข้ามาเป็น .ovpn
###change client1 to newprofile_name
###Create key , crt , ca into profile
cd ~/openvpn-ca
source vars
./build-key client1

###make config for profile
cd ~/client-configs
./make_config.sh client1
ls ~/client-configs/files

export client1.ovpn ไปให้กับ client ลอง connect ครับ

####Client resign or need to revoke key for client3
cd ~/openvpn-ca
source vars
./revoke-full client3
###reboot openvpn service 1 times when revoke user key
systemctl restart openvpn@server

##Do at first time only.
cp ~/openvpn-ca/keys/crl.pem /etc/openvpn
nano /etc/openvpn/server.conf
crl-verify crl.pem

เท่านี้ก็เรียบร้อยครับผม : )
Packetlove.com Team.