โจทย์

จัดทำ VPN connection สำหรับผู้ใช้ Windows, Android, and iOS รวมถึงการทำ Site-to-Site กับลูกค้า

สภาพแวดล้อม และอุปกรณ์

  • Mikrotik hEX S, RouterOS 7.8
  • Internet connection ที่มี public IP ให้ กรณีในบันทึกนี้จะเป็น NT Max DFiber 300/300
  • IP address ที่ใช้
    • 172.16.0.0/24 เป็นเน็ตหลัก
    • 192.16.0.0/24 เป็นเน็ตที่ลูกค้า
  • บริการ IP Cloud ของ Mikrotik

ปัญหาที่พบเจอ

  • การตั้งค่า VPN client ในแต่ละ platform มีความแตกต่าง ยากง่ายแตกต่างกันไป
  • ใช้ PSK บน Windows เชื่อมต่อไม่มีปัญหา แต่บน Android ไม่ได้
  • ใช้ certificate บน Android ต้องกำหนด IPSec identifier ด้วย แต่พอไปใช้บน Windows กลับเชื่อมต่อไม่ได้

ลงมือทำ

  • ดำเนินการตั้งค่ามาตรฐานต่างๆ ไม่ว่าจะเป็น Interface, Bridge, Address, Pool, DHCP, NAT, ฯลฯ การทำ VPN Server บน Mikrotik นั้นไม่ยาก เพียงแต่ทำให้ถูกต้อง ถูกลำดับขั้นตอน
  • สำหรับ IPSec เริ่มด้วยการสร้าง Profiles (/ip ipsec profile) ซึ่งเป็นการกำหนดคุณลักษณะของปลายทาง โดยมีค่า algorithm ต่างๆ การเข้ารหัส ซึ่งตรงนี้หากทำ Site-to-Site ต้องกำหนดให้ตรงกันทั้ง 2 ฝั่ง ซึ่งทำให้สามารถที่จะสร้าง Profile ได้มากกว่าหนึ่ง เพื่อรองรับปลายทางที่แตกต่างกัน

/ip ipsec profile add name="default" hash-algorithm=sha1 \
enc-algorithm=aes-128,3des dh-group=modp2048,modp1024 \
lifetime=1d proposal-check=obey nat-traversal=yes \
dpd-interval=2m dpd-maximum-failures=5

แนะนำ algorithm ตามนี้
/ip ipsec profile
add dh-group=modp2048 enc-algorithm=aes-256 hash-algorithm=sha256 name=IPSecProfile
 
  • ทำการสร้าง Peers เป็นการกำหนดข้อมูลการเชื่อมต่อกับปลายทาง ตรง address สามารถใส่ได้ทั้ง public IP หรือ DNS ก็ได้ และต้องระบุ profile ที่ต้องการใช้ สามารถที่จะสร้าง Peer ได้มากกว่า 1 สำหรับการรองรับการเชื่อมต่อที่แตกต่าง

/ip ipsec peer
add address=xxx.xxx.xxx.xxx exchange-mode=ike2 name=Site1 profile=IPSecProfile
add address=this.domain.name exchange-mode=ike2 name=Site2 profile=IPSecProfile

add exchange-mode=ike2 name=ClientVPN passive=yes profile=ClientVPN
 
  • Proposals กำหนด algorithm ที่ใช้สำหรับการเข้ารหัสในการสื่อสาร (policy)

/ip ipsec proposal
add name=IPsecProposal auth-algorithms=sha256 enc-algorithms=aes-256-cbc pfs-group=modp2048
 
  • หากมีการทำ Client-to-Site (transport) ด้วย ก็ต้องกำหนด IP range ที่จะให้ client ที่เข้ามาได้รับ IP ใน Mode Configs โดยมีการกำหนด IP pool ไว้ก่อน

/ip ipsec mode-config
add address-pool=dhcp_vpn name=vpn-client-ip
 
  • เมื่อจัดเตรียมความพร้อมแล้ว ต่อไปก็ทำการสร้าง Identities ซึ่ง Identities นี้มีแยกย่อยว่าเป็น PSK หรือ Certificate สำหรับ PSK สามารถกำหนดได้เลยใน Identities ส่วน Certificate จะแยกหัวข้อการสร้างไว้ด้านล่าง

/ip ipsec identity
add auth-method=pre-shared-key peer=Site1 secret="r8&#ym2*pufg" \
generate-policy=port-strict
add auth-method=pre-shared-key peer=Site2 secret="2j5%e5&@9z!7" \
generate-policy=port-strict

# identity for each client certificate
/ip ipsec identity
add peer=ClientVPN auth-method=digital-signature certificate=ServerCert \
generate-policy=port-strict match-by=certificate mode-config=vpn-client-ip  \
policy-template-group=P2SVPN remote-certificate=ClientCert
 
  • และ Policies นั้น สำหรับ Site-to-Site (tunnel) จะเป็นการกำหนดว่า แต่ละ peer ที่เชื่อมต่อนั้น internal IP เป็นอะไร ซึ่งหมายความว่าไม่ควรมี IP range ซ้ำในแต่ละ site ส่วนของ client (transport) นั้น ให้กำหนด Policy ที่เป็น IP ที่เปิด และกำหนดให้เป็น template

/ip ipsec policy
add dst-address=192.168.1.0/24 peer=Site1 proposal=IPsecProposal src-address=172.16.0.0/24 tunnel=yes
add dst-address=192.168.0.0/24 peer=Site2 proposal=IPsecProposal src-address=172.16.0.0/24 tunnel=yes

# client (transport)
add dst-address=0.0.0.0/0 group=P2SVPN proposal=ClientVPN src-address=0.0.0.0/0 template=yes
 

.