StrongswanでIKEv2のVPNサーバを構築する
経緯
仕事でVPNサーバ使ったシステムがあるのだが 環境が碌にない状況でテストとかどうすんの?って感じの状況
とりあえず、VPNサーバないと話にならんので調査がてら構築する
作成するVPNサーバの環境
- AWSのEC2
- IKEv2
- ユーザ・パスワードの簡易的なものを作成(あとで削除)
イメージはこんな感じ
やること
- 対象ドメインを準備
- セキュリティグループを作成し、EC2に追加
- EC2にセキュリティグループを追加し、ソースの宛先チェックを無効にする
- EC2にStrongswan、Certbotをインストール
- EC2にCertbotで証明書を作成し、証明書をStrongswanの場所へリンクを貼る
- Strongswanの設定ファイルを編集
- StrongswanのVPNの仮想IPをホストへルーティング
- 接続確認
作業
1. 対象ドメインを準備
vpn.test-ryuouen.link
として過程 ※EC2のIPアドレスをAレコードに設定しておくこと
2. セキュリティグループを作成
タイプ | プロトコル | ポート範囲 | ソース |
---|---|---|---|
カスタムUDP | UDP | 500 | 0.0.0.0/0 |
カスタムUDP | UDP | 4500 | 0.0.0.0/0 |
カスタムプロトコル | ESP(50) | すべて | 0.0.0.0/0 |
カスタムプロトコル | AH(51) | すべて | 0.0.0.0/0 |
3. EC2にセキュリティグループを追加し、ソースの宛先チェックを無効にする
セキュリティグループを付与
ソース/宛先チェックを変更を無効とする
↓
4. EC2にStrongswan、Certbotをインストール
sudo amazon-linux-extras install epel -y sudo yum install strongswan -y sudo yum install certbot -y
5. EC2にCertbotで証明書を作成し、証明書をStrongswanの場所へリンクを貼る
セキュリティグループに80番ポートを許可
※あとで外します
証明書発行
sudo certbot certonly \ --standalone \ -d vpn.test-ryuouen.link \ -m mshige1979@ryuouen.net \ --agree-tos -n
↓
[ec2-user@ip-172-16-10-100 ~]$ sudo tree /etc/letsencrypt/live /etc/letsencrypt/live ├── README └── vpn.test-ryuouen.link ├── README ├── cert.pem -> ../../archive/vpn.test-ryuouen.link/cert1.pem ├── chain.pem -> ../../archive/vpn.test-ryuouen.link/chain1.pem ├── fullchain.pem -> ../../archive/vpn.test-ryuouen.link/fullchain1.pem └── privkey.pem -> ../../archive/vpn.test-ryuouen.link/privkey1.pem 1 directory, 6 files [ec2-user@ip-172-16-10-100 ~]$
※ここで80番ポートは閉じておくこと
sudo ln -s /etc/letsencrypt/live/vpn.test-ryuouen.link/cert.pem /etc/strongswan/ipsec.d/certs/ sudo ln -s /etc/letsencrypt/live/vpn.test-ryuouen.link/chain.pem /etc/strongswan/ipsec.d/cacerts/ sudo ln -s /etc/letsencrypt/live/vpn.test-ryuouen.link/privkey.pem /etc/strongswan/ipsec.d/private/
6. Strongswanの設定ファイルを編集
設定ファイルバックアップ
sudo cp -p /etc/strongswan/ipsec.conf /etc/strongswan/ipsec.conf.org sudo cp -p /etc/strongswan/ipsec.secrets /etc/strongswan/ipsec.secrets.org
/etc/strongswan/ipsec.conf
# basic configuration config setup # strictcrlpolicy=yes # uniqueids = no # これは共通の設定っぽい conn %default keyexchange=ikev2 ike=aes128-sha256-ecp256,aes256-sha384-ecp384,aes128-sha256-modp2048,aes128-sha1-modp2048,aes256-sha384-modp4096,aes256-sha256-modp4096,aes256-sha1-modp4096,aes128-sha256-modp1536,aes128-sha1-modp1536,aes256-sha384-modp2048,aes256-sha256-modp2048,aes256-sha1-modp2048,aes128-sha256-modp1024,aes128-sha1-modp1024,aes256-sha384-modp1536,aes256-sha256-modp1536,aes256-sha1-modp1536,aes256-sha384-modp1024,aes256-sha256-modp1024,aes256-sha1-modp1024! esp=aes128gcm16-ecp256,aes256gcm16-ecp384,aes128-sha256-ecp256,aes256-sha384-ecp384,aes128-sha256-modp2048,aes128-sha1-modp2048,aes256-sha384-modp4096,aes256-sha256-modp4096,aes256-sha1-modp4096,aes128-sha256-modp1536,aes128-sha1-modp1536,aes256-sha384-modp2048,aes256-sha256-modp2048,aes256-sha1-modp2048,aes128-sha256-modp1024,aes128-sha1-modp1024,aes256-sha384-modp1536,aes256-sha256-modp1536,aes256-sha1-modp1536,aes256-sha384-modp1024,aes256-sha256-modp1024,aes256-sha1-modp1024,aes128gcm16,aes256gcm16,aes128-sha256,aes128-sha1,aes256-sha384,aes256-sha256,aes256-sha1! dpdaction=clear dpddelay=300s rekey=no # ユーザーとパスワードによる認証 conn ikev2-vpn-user auto=add compress=no type=tunnel fragmentation=yes forceencaps=yes left=%any leftid=@vpn.test-ryuouen.link # VPNのドメイン leftcert=cert.pem # letsencryptの証明書 leftsendcert=always leftsubnet=0.0.0.0/0 right=%any rightid=%any rightauth=eap-mschapv2 # EAPのユーザー認証らしい rightsourceip=192.168.100.0/24 # VPNクライアントのIP範囲らしい rightdns=8.8.8.8,8.8.4.4 rightsendcert=never eap_identity=%identity
/etc/strongswan/ipsec.secrets
# ipsec.secrets - strongSwan IPsec secrets file : RSA "privkey.pem" # ユーザーパスワード vpn_username : EAP "vpn_password"
※今回はユーザー・パスワードによる認証のみ
起動
sudo systemctl enable strongswan sudo systemctl start strongswan
7. StrongswanのVPNの仮想IPをホストへルーティング
sudo iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o eth0 -m policy --dir out --pol ipsec -j ACCEPT sudo iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o eth0 -j MASQUERADE
↓
なんか他のサイトに繋がらなくなったけどまあひとまずはここまで(多分、DNSサーバがらみ) 次は証明書とかの接続できるか試す予定
おまけ
Apple configurator2で作成した構成プロファイル
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>PayloadContent</key> <array> <dict> <key>DNS</key> <dict> <key>ServerAddresses</key> <array> <string>8.8.8.8</string> <string>8.8.4.4</string> </array> <key>SupplementalMatchDomainsNoSearch</key> <integer>0</integer> </dict> <key>IKEv2</key> <dict> <key>AuthName</key> <string>サーバに設定したユーザー</string> <key>AuthPassword</key> <string>サーバに設定したパスワード</string> <key>AuthenticationMethod</key> <string>None</string> <key>ChildSecurityAssociationParameters</key> <dict> <key>DiffieHellmanGroup</key> <integer>14</integer> <key>EncryptionAlgorithm</key> <string>AES-256</string> <key>IntegrityAlgorithm</key> <string>SHA2-256</string> <key>LifeTimeInMinutes</key> <integer>1440</integer> </dict> <key>DeadPeerDetectionRate</key> <string>Medium</string> <key>DisableMOBIKE</key> <integer>0</integer> <key>DisableRedirect</key> <integer>0</integer> <key>EnableCertificateRevocationCheck</key> <integer>0</integer> <key>EnableFallback</key> <integer>0</integer> <key>EnablePFS</key> <integer>0</integer> <key>ExtendedAuthEnabled</key> <true/> <key>IKESecurityAssociationParameters</key> <dict> <key>DiffieHellmanGroup</key> <integer>14</integer> <key>EncryptionAlgorithm</key> <string>AES-256</string> <key>IntegrityAlgorithm</key> <string>SHA2-256</string> <key>LifeTimeInMinutes</key> <integer>1440</integer> </dict> <key>LocalIdentifier</key> <string>vpn.test-ryuouen.link</string> <key>RemoteAddress</key> <string>vpn.test-ryuouen.link</string> <key>RemoteIdentifier</key> <string>vpn.test-ryuouen.link</string> <key>UseConfigurationAttributeInternalIPSubnet</key> <integer>0</integer> </dict> <key>PayloadDescription</key> <string>VPN設定を構成します</string> <key>PayloadDisplayName</key> <string>VPN</string> <key>PayloadIdentifier</key> <string>com.apple.vpn.managed.ここはなんかシステムで動的に変わる</string> <key>PayloadType</key> <string>com.apple.vpn.managed</string> <key>PayloadUUID</key> <string>ここはなんかシステムで動的に変わる</string> <key>PayloadVersion</key> <integer>1</integer> <key>Proxies</key> <dict> <key>HTTPEnable</key> <integer>0</integer> <key>HTTPSEnable</key> <integer>0</integer> </dict> <key>UserDefinedName</key> <string>vpn.test-ryuouen.link</string> <key>VPNType</key> <string>IKEv2</string> </dict> </array> <key>PayloadDisplayName</key> <string>vpnテスト1</string> <key>PayloadIdentifier</key> <string>ここはなんかシステムで動的に変わる</string> <key>PayloadRemovalDisallowed</key> <false/> <key>PayloadType</key> <string>Configuration</string> <key>PayloadUUID</key> <string>ここはなんかシステムで動的に変わる</string> <key>PayloadVersion</key> <integer>1</integer> </dict> </plist>
【補足】iptablesの設定が再起動したら消えてしまう・・・
設定を外付け保存
sudo iptables-save > /etc/sysconfig/iptables
起動スクリプトに追加
sudo vi /etc/rc.d/rc.local ---- # 追記 if [ -f /etc/sysconfig/iptables ]; then /sbin/iptables-restore < /etc/sysconfig/iptables fi ----
一応実行権限付与
sudo chmod +x /etc/rc.d/rc.local
参考
strongSwanでLet’s Encrypt証明書を使ったIKEv2 VPNサーバーを作る – World203
特集記事 : 時代は変遷するもの… [Gentoo Linuxな生活/IKEv2 VPNで悩む] - Nest Of Hawk
Strongswan で IKEv2 VPN サーバを構築する 前編 | seinolab
Strongswan で IKEv2 VPN サーバを構築する 後編 | seinolab
strongSwanにiPhoneからオレオレ証明書認証でIPsec IKEv2を繋げる - Qiita
感謝