顯示具有 ipsec 標籤的文章。 顯示所有文章
顯示具有 ipsec 標籤的文章。 顯示所有文章

2016年8月19日 星期五

AWS 筆記 - 透過 VPN/IPSEC 讓 AWS VPC 與 Microsoft Azure VirtualNetwork 互通

對 Azure 來說,預設建置的 VirtualNetwork 就是 10.0.0.0 開頭的網路環境,大多可以繼續延用下去。在這延續上一篇 AWS 筆記 - 建置自己的 VPC 且透過 VPN/IPSec 讓各個 Region 的 VPC 互通 ,先簡略調整 Azure VirtualNetwork 環境,接著建置 Azure VPC 與 AWS VPC 連線機制。

假設 AWS VPC 為 10.100.0.0/16 網域,而 Azure VirtualNetwork 為 10.50.0.0/16 網域。

設置 Azure VPC:

Azure Portal -> New
-> Resource -> Create -> name=StudyVPN, Location=WestUS
-> Networking -> VirtualNetwork
-> Name=US
-> Address Space=10.50.0.0/16
-> Subnet Name=default
-> Subnet address range=10.50.0.0/24
-> Location = WestUS
-> Virtual Machines -> Ubuntu Server 14.04
-> Name=VPN
-> Location = WestUS
-> DS1_V2 Standard

請先將 AWS EC2 Security Group 跟 Azure Network security group 都設置好、開放對方 VPN IP。此例為 AWS EU-West-1 機器跟 Azure WestUS 機器,設置完防火牆後,先 ping 一下彼此吧。

在 EU-West-1 的 VPN Server 機器,其 public ip = 52.209.16.24:

$ ping 13.88.186.107

在 Azure WestUS 的 VPN Server 機器,其 public ip = 13.88.186.107:

$ ping 52.209.16.24

先設置 Azure VPN server:

$ sudo apt-get update && sudo apt-get -y upgrade && sudo apt-get -y dist-upgrade && sudo apt-get autoremove && sudo apt-get autoclean

$ sudo apt-get install openswan
$ ls /etc/ipsec*
/etc/ipsec.conf  /etc/ipsec.secrets

/etc/ipsec.d:
aacerts  cacerts  certs  crls  examples  ocspcerts  policies  private
$ sudo ipsec setup version
Linux Openswan U2.6.38/K3.19.0-65-generic (netkey)
See `ipsec --copyright' for copyright information.
$ sudo vim /etc/ipsec.d/AWS-EU-WEST-1.conf
conn Connect-to-AWS-EU-WEST-1
 type=tunnel
 authby=secret

 leftid=13.88.186.107
 left=%defaultroute
 leftsubnet=10.50.0.0/16
 leftnexthop=%defaultroute

 right=52.209.16.24
 rightsubnet=10.100.0.0/16

 pfs=yes
 auto=start
$ sudo vim /etc/ipsec.d/AWS-EU-WEST-1.secrets
13.88.186.107 52.209.16.24 : PSK "IPSEC_PASSWORD"

$ sudo vim /etc/ipsec.conf
#protostack=auto
protostack=netkey

#...
include /etc/ipsec.d/AWS-EU-WEST-1.conf

$ sudo vim /etc/ipsec.secrets
include /etc/ipsec.d/AWS-EU-WEST-1.secrets

$ sudo ipsec setup restart
ipsec_setup: Stopping Openswan IPsec...
ipsec_setup: Starting Openswan IPsec U2.6.38/K3.19.0-65-generic...


更新 AWS VPN server:

$ sudo vim /etc/ipsec.d/AZURE-US-WEST.conf
conn Connect-to-AZURE-EU-WEST
 type=tunnel
 authby=secret

 leftid=52.209.16.24

 left=%defaultroute
 leftsubnet=10.100.0.0/16
 leftnexthop=%defaultroute

 right=13.88.186.107
 rightsubnet=10.50.0.0/16

 pfs=yes
 auto=start
$ sudo vim /etc/ipsec.d/AZURE-US-WEST.secrets
$ sudo ipsec setup restart
ipsec_setup: Stopping Openswan IPsec...
ipsec_setup: Starting Openswan IPsec U2.6.37/K4.4.11-23.53.amzn1.x86_64...
ipsec_setup: /usr/libexec/ipsec/addconn Non-fips mode set in /proc/sys/crypto/fips_enabled


當兩邊的設定好後,並且用 ipsec setup restart 後,另一方才可以用指令連上對方:

Azure VPN server:

$ sudo ipsec auto --up Connect-to-AWS-EU-WEST-1
117 "Connect-to-AWS-EU-WEST-1" #4: STATE_QUICK_I1: initiate
004 "Connect-to-AWS-EU-WEST-1" #4: STATE_QUICK_I2: sent QI2, IPsec SA established tunnel mode {ESP=>0x29ba22d7 <0x68b944dd xfrm=AES_128-HMAC_SHA1 NATOA=none NATD=52.209.16.24:4500 DPD=none}


AWS VPN server:

$ sudo ipsec auto --up Connect-to-AZURE-EU-WEST
117 "Connect-to-AZURE-EU-WEST" #6: STATE_QUICK_I1: initiate
004 "Connect-to-AZURE-EU-WEST" #6: STATE_QUICK_I2: sent QI2, IPsec SA established tunnel mode {ESP=>0x74246b5f <0x7156f72b xfrm=AES_128-HMAC_SHA1 NATOA=none NATD=13.88.186.107:4500 DPD=none}


並且兩邊就可以 ping 彼此的 private ip 囉!

接著還需設定 routing table 以及對 AWS VPN Server機器打開 Change Source/Dest. Check 、對 Azure VPN Server 機器的網卡打開 IP forwarding 後,兩邊機器就可以互相 ping  到啦:

對 AWS 而言,在 VPC Dashboard -> Route 中,將相關的子網域都添加一則 10.50.0.0/16 導向至 AWS VPN Server (別忘了,若第一次設置 EC2 上的 VPN Server,還要從 EC2 Dashboard 修改該機器,取消 Change Source/Dest. Check)

對 Azure 而言,Azure portal -> New -> Networking -> Route table ,建立後,再去設定

Resource -> Route table -> Settings
-> Routes -> Add -> Route name=ToAWS, Address prefix=10.100.0.0/16, Next hop type=Virtual appliance -> Next hop address=10.50.0.4 (Azure VPN Server Private IP)
-> Subnet -> Associate subnet -> 挑選 VirtualNetwork、挑選 Subnet

最後,記得 NAT 相關要設定好,像在 Azure 上的 VPN server 是從一台乾淨的 Ubuntu 14.04 裝後,還必須設定以下:

$ echo "net.ipv4.ip_forward = 1" | sudo tee -a /etc/sysctl.conf
$ echo "net.ipv4.conf.all.accept_redirects = 0" | sudo tee -a /etc/sysctl.conf
$ echo "net.ipv4.conf.all.send_redirects = 0" | sudo tee -a /etc/sysctl.conf
$ for vpn in /proc/sys/net/ipv4/conf/*; do echo 0 | sudo tee $vpn/accept_redirects; echo 0 | sudo tee $vpn/send_redirects; done
$ sudo sysctl -p


其他備註:

$ sudo ipsec setup restart
ipsec_setup: Stopping Openswan IPsec...
ipsec_setup: Starting Openswan IPsec 2.6.38...
ipsec_setup: No KLIPS support found while requested, desperately falling back to netkey
ipsec_setup: NETKEY support found. Use protostack=netkey in /etc/ipsec.conf to avoid attempts to use KLIPS. Attempting to continue with NETKEY


解法 => protostack=netkey

$ sudo ipsec auto --up Connect-to-AWS-EU-WEST-1
022 "Connect-to-AWS-EU-WEST-1": We cannot identify ourselves with either end of this connection.


解法 => 先把一些錯誤訊息修掉,如 sudo ipsec setup restart 看到的問題,接著再看 conn 的設定,當設定錯誤也會看到這段訊息。

2016年8月17日 星期三

AWS 筆記 - 建置自己的 VPC 且透過 VPN/IPSec 讓各個 Region 的 VPC 互通

Amazon VPC 的全名為 Amazon Virtual Private Cloud,就是所謂的內網架構,在此網路架構內的機器可以互通有無。https://aws.amazon.com/tw/vpc/

假設使用情境:
  1. 建立一個 VPC 網段為 10.100.0.0/16
  2. 建立兩個 Subnet 網段 A 跟 B 分別為 10.100.1.0/24, 10.100.2.0/24 且為不同 Availability Zone
  3. 其中 A 網段不給予 Public IP ,而 B 網段會有 Public IP
  4. 其中 A 網段需建立 NAT 運作方式,可以連外,也可以被 B 網段連到
建置方式:

VPC dashboard
-> Your VPCs -> Create VPC
-> 給定 CIDR block,如 10.100.0.0/16
-> Subnets -> Create Subnet
-> 挑選剛剛建立 VPC 以及 Availability Zone (兩個 Subnet 挑不同區),以及給定 CIDR block
-> A 網段 10.100.1.0/24
-> B 網段 10.100.2.0/24 並打開 Auto-Assign Public IP 屬性
-> Internet Gateways -> Create Internet Gateway
-> 勾選後,點擊 Attach to VPC -> 挑剛剛的 10.100.0.0/16
-> Nat Gateways -> Create NAT Gateway
-> 在此只需建立 A 網段的 NAT Gateway,但要建立在 B 網段,才能讓 A 網段的機器連出去 -> 給予 EIP/Create New EIP
-> Route Tables -> Create Route Table
-> 建立 A 網段 10.100.1.0/24 專屬表 -> 並設置 0.0.0.0/0 給予剛剛建立的 NAT   -> 建立 B 網段 10.100.2.0/24 專屬表 -> 並設置 0.0.0.0/0 給予剛剛建立的 Internet Gateway
-> 回到 Subnets 分別挑選 A/B 網段並更新他們的 route table

如此一來,就有一個 VPC = 10.100.0.0/16 ,內有兩個 subnet = A網段 10.100.1.0/24, B網段 10.100.2.0/24,其中 A 網段聯外方式就是透過 NAT 出去(對外會看到NAT IP),而 B 網段則是用 public ip 透過 Internet Gateway 出去。機器們可以用 curl ipino.io 去驗證 IP ,以及使用 ping 來檢驗是不是可以連到彼此。以上就是自建 VPC 且擁有 public ip 跟 private ip 網段的部分。

接著進行不同 Region VPC 溝通方式,不同 Region VPC 要溝通的方式,就只是單純在各個 Region 架設 VPN/IPSEC Server 互連,除了彼此更新 Route Table 外,還需在 VPN 機上打開防火牆、取消封包檢查等,如此一來即可處理封包移動流程。

假設有 10.100.0.0/16 跟 10.200.0.0/16 兩個網段,一處在 EU-West-1 另一處在 SA-EAST-1 ,一樣透過上述先建立完 VPC-EU-West-1 跟 VPC-SA-EAST-1,並且分別建立 Subnet EU-WEST-1 10.100.1.0/24 跟 SA-EAST-1 10.200.1.0/24 且兩者都有 Public IP(在此不做 private subnet),接著分別在這兩各區域,各開一台機器安裝 IPSEC,在此直接挑 Community AMIs -> amzn-ami-vpc-nat -> amzn-ami-vpc-nat-hvm-2016.03.3.x86_64-ebs CentOS ,並且使用 ec2-user 帳號登入。建議先在兩台 VPN server 彼此 ping 一下對方(在 Security Group 可以偷懶開放對方 IP 所有網路流量),接著就設定 IPSEC 的部分:

兩台機器都安裝 openswan 軟體:

$ sudo yum install openswan
$ sudo ls /etc/ipsec*
/etc/ipsec.conf  /etc/ipsec.secrets

/etc/ipsec.d:
policies


在 EU-WEST-1 機器,其 public ip = 52.209.16.24:

$ sudo vim /etc/ipsec.d/SA-EAST-1.conf
conn Connect-to-SA-EAST-1
type=tunnel
authby=secret

leftid=52.209.16.24
left=%defaultroute
leftsubnet=10.100.0.0/16
leftnexthop=%defaultroute

right=52.67.138.147
rightsubnet=10.200.0.0/16

pfs=yes
auto=start
$ sudo vim /etc/ipsec.d/SA-EAST-1.secrets
52.209.16.24 52.67.138.147: PSK "IPSEC_PASSWORD"
$ sudo grep -c "^include /etc/ipsec.d/" /etc/ipsec.conf > /dev/null || (echo "include /etc/ipsec.d/*.conf" | sudo tee --append /etc/ipsec.conf)
$ sudo grep -c "^include /etc/ipsec.d/" /etc/ipsec.secrets > /dev/null || (echo "include /etc/ipsec.d/*.secrets" | sudo tee --append /etc/ipsec.secrets)
$ sudo ipsec setup restart
$ sudo ipsec auto --up Connect-to-SA-EAST-1
117 "Connect-to-SA-EAST-1" #3: STATE_QUICK_I1: initiate
004 "Connect-to-SA-EAST-1" #3: STATE_QUICK_I2: sent QI2, IPsec SA established tunnel mode {ESP=>0xe7553eef <0xa8498182 xfrm=AES_128-HMAC_SHA1 NATOA=none NATD=52.67.138.147:4500 DPD=none}


在 SA-EAST-1 機器上,其 public ip = 52.67.138.147:

$ sudo vim /etc/ipsec.d/EU-WEST-1.conf
conn Connect-to-EU-WEST-1
type=tunnel
authby=secret

leftid =52.67.138.147
left=%defaultroute
leftsubnet=10.200.0.0/16
leftnexthop=%defaultroute

right=52.209.16.24
rightsubnet=10.100.0.0/16

pfs=yes
auto=start
$ sudo vim /etc/ipsec.d/EU-WEST-1.secrets
52.67.138.147 52.209.16.24 : PSK "IPSEC_PASSWORD"
$ sudo grep -c "^include /etc/ipsec.d/" /etc/ipsec.conf > /dev/null || (echo "include /etc/ipsec.d/*.conf" | sudo tee --append /etc/ipsec.conf)
$ sudo grep -c "^include /etc/ipsec.d/" /etc/ipsec.secrets > /dev/null || (echo "include /etc/ipsec.d/*.secrets" | sudo tee --append /etc/ipsec.secrets)
$ sudo ipsec setup restart
$ sudo ipsec auto --up Connect-to-EU-WEST-1
117 "Connect-to-EU-WEST-1" #5: STATE_QUICK_I1: initiate
004 "Connect-to-EU-WEST-1" #5: STATE_QUICK_I2: sent QI2, IPsec SA established tunnel mode {ESP=>0x681a5ce6 <0xcb613471 xfrm=AES_128-HMAC_SHA1 NATOA=none NATD=52.209.16.24:4500 DPD=none}


接著,就可以在兩台機器上,分別 ping 對方的 private ip 來測試,如此一來就算打通一條連線,最後要做的就是設定 VPC 的 route table:

在 EU-WEST-1 VPC (10.100.0.0/16) 的所有 Subnet (例如10.100.1/16),多添加一條 routing rule = 10.200.0.0/16 而 Target 用 EU-WEST-1 上的 VPN Server (可以在 Target 上打 i 關鍵字,就可以看到 EC2 server);在 SA-EAST-1 VPC (10.200.0.0/16) 裡所有的 Subnet (例如 10.200.1.0/16) ,多添加一條 routing rule = 10.100.0.0/16 而 Target 用 SA-EAST-1 上的 VPN Server 。

最後,回到 EC2 Dashboard ,將各區域的 VPN Server 的設定更新一下:Actions -> Networking -> Change Source/Dest. Check -> Disable,如此一來,雙方 VPC 下的機器,就可以互相 ping 和連線了。

在此使用 IPSEC 的做法,是因為後續可以來做跨 ISP 的結合,如 Azure service 等。

2014年1月28日 星期二

[Linux] 架設 VPN Server (PPTP/IPSEC/L2TP) @ Ubuntu 12.04

研究上需要架設一台 VPN,於是架了 PPTP 後,也順便把 IPSEC、L2TP 補齊。流程:

安裝需要的軟體:

$ sudo apt-get install pptpd openswan xl2tpd ppp

PPTP:

$ sudo vim /etc/pptpd.conf
# (Recommended)
localip 192.168.0.1
remoteip 192.168.0.234-238,192.168.0.245

$ sudo vim /etc/ppp/pptpd-options
# …
ms-dns 8.8.8.8
# …
logfile /var/log/pptpd.log

$ sudo touch /var/log/pptpd.log

$ sudo vim /etc/ppp/chap-secrets
# PPTP
username pptpd password *
# L2TP
username l2tpd password *

其中 username, password 都是明碼


IPSEC/L2TP:

$ sudo su
$ echo "net.ipv4.ip_forward = 1" | tee -a /etc/sysctl.conf
$ echo "net.ipv4.conf.all.accept_redirects = 0" | tee -a /etc/sysctl.conf
$ echo "net.ipv4.conf.all.send_redirects = 0" | tee -a /etc/sysctl.conf
$ for vpn in /proc/sys/net/ipv4/conf/*; do echo 0 > $vpn/accept_redirects; echo 0 > $vpn/send_redirects; done
$ sysctl -p


$ sudo vim /etc/rc.local
for vpn in /proc/sys/net/ipv4/conf/*; do echo 0 > $vpn/accept_redirects; echo 0 > $vpn/send_redirects; done
iptables --table nat --append POSTROUTING --jump MASQUERADE


$ sudo vim /etc/ipsec.conf
conn L2TP-PSK-NAT
    rightsubnet=vhost:%priv
    also=L2TP-PSK-noNAT

conn L2TP-PSK-noNAT
    authby=secret
    pfs=no
    auto=add
    keyingtries=3
    rekey=no
    ikelifetime=8h
    keylife=1h
    type=transport
    left=SERVER_IP
    leftprotoport=17/1701
    right=%any
    rightprotoport=17/%any


$ sudo vim /etc/ipsec.secrets
SERVER_IP  %any:  PSK "L2TP_PRIVATE_KEY"


$ sudo vim /etc/xl2tpd/xl2tpd.conf
[global]
ipsec saref = yes

[lns default]
ip range = 10.1.2.2-10.1.2.255
local ip = 10.1.2.1
;require chap = yes
refuse chap = yes
refuse pap = yes
require authentication = yes
ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
length bit = yes


$ sudo vim /etc/ppp/options.xl2tpd
require-mschap-v2
ms-dns 8.8.8.8
asyncmap 0
auth
crtscts
lock
hide-password
modem
debug
name l2tpd
proxyarp
lcp-echo-interval 30
lcp-echo-failure 4


重啟相關服務:

$ sudo service pptpd restart
Restarting PPTP:
Stopping PPTP: pptpd.
Starting PPTP Daemon: pptpd.

$ sudo service ipsec restart
ipsec_setup: Stopping Openswan IPsec...
ipsec_setup: Starting Openswan IPsec 2.6.37...
ipsec_setup: No KLIPS support found while requested, desperately falling back to netkey
ipsec_setup: NETKEY support found. Use protostack=netkey in /etc/ipsec.conf to avoid attempts to use KLIPS. Attempting to continue with NETKEY

$ sudo service xl2tpd restart
Restarting xl2tpd: xl2tpd.


服務驗證:

$ sudo ipsec verify
Checking your system to see if IPsec got installed and started correctly:
Version check and ipsec on-path                                 [OK]
Linux Openswan U2.6.37/K3.2.0-58-generic (netkey)
Checking for IPsec support in kernel                            [OK]
 SAref kernel support                                           [N/A]
 NETKEY:  Testing XFRM related proc values                      [OK]
        [OK]
        [OK]
Checking that pluto is running                                  [OK]
 Pluto listening for IKE on udp 500                             [OK]
 Pluto listening for NAT-T on udp 4500                          [OK]
Two or more interfaces found, checking IP forwarding            [OK]
Checking NAT and MASQUERADEing                                  [OK]
Checking for 'ip' command                                       [OK]
Checking /bin/sh is not /bin/dash                               [WARNING]
Checking for 'iptables' command                                 [OK]
Opportunistic Encryption Support                                [DISABLED]