paloma blog

NWエンジニアやってます。主に自宅環境のお遊びを書きます。Pythonもちょっと。タイトルは好きなカクテルから。

strongswanとAndroidのネイティブVPNでIPsecをする

前回の記事でsshポートフォワードで無理やりVPNを作りましたが、本当はstrongswanでのIPsecを考えていたので今回ちょっと紹介します。

UbuntuへのVPN

Androidからpfsense向けは経験ありなので出来るのですが、今回の対象はubuntuのLXD環境です。

pfsenseはisoしか提供されてないのでvirtualbox等で無理やり作るよりはおとなしくLXDホストにVPNサーバを立てることにしました。

候補としてはSoftEther、WireGuard、strongswanがありましたが、strongswanにしました。
設定的に専用クライアントを入れなくてもネイティブのツールで出来そうだったからです。

ネイティブのツールだとアルゴリズムがちょっと弱いですが、インストール無しで出来るのが楽でいいですね。

strongswanの設定

インストールは割愛して設定内容。

Version
  • VPNサーバとなるLXDホスト機
masashi@PC-ubuntu:~$ uname -a
Linux PC-ubuntu 5.4.0-81-generic #91-Ubuntu SMP Thu Jul 15 19:09:17 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

masashi@PC-ubuntu:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.3 LTS
Release:        20.04
Codename:       focal
  • strongswan

aptで入れたらswanctlが無かったのでipsecコマンドを使います。

masashi@PC-ubuntu:~$ ipsec --version
Linux strongSwan U5.8.2/K5.4.0-81-generic
University of Applied Sciences Rapperswil, Switzerland
See 'ipsec --copyright' for copyright information.
ipsec.conf

アルゴリズムはpfsenseのドキュメントを参考に同パラメータにしています。 認証方法はPre Shared Key + Xauthです。

# ipsec.conf - strongSwan IPsec configuration file

# basic configuration

config setup
        # strictcrlpolicy=yes
        # uniqueids = no

# Add connections here.
conn %default
        ikelifetime=60m
        keylife=20m
        rekeymargin=3m
        keyingtries=1
        keyexchange=ikev1

conn android
        ikelifetime=24h
        keylife=8h
        aggressive=yes
        ike=aes128-sha1-modp1024
        esp=aes128-sha1
        left=192.168.0.10
        leftsubnet=192.168.0.0/24
        leftauth=psk
        right=%any
        rightauth=psk
        rightauth2=xauth
        rightsourceip=10.255.0.0/24
        auto=add
  • left: サーバ側
  • right: クライアント側

です。

サイト間ではなくリモートアクセスなのでアグレッシブモード。

アグレッシブモードにするにはドキュメントにcharon.confも編集しろとあったのでいじってます。

  • charon.conf
masashi@PC-ubuntu:~$ grep dont_care /etc/strongswan.d/charon.conf
    i_dont_care_about_security_and_use_aggressive_mode_psk = yes

送信元Anyからikeを受け付けて認証後にトンネル用の10.255.0.Xを払いだすという設定です。
rightsourceipを設定してあげないと認証でこけました。

ipsec.secret

PSKとXauthのパスワードです。

# This file holds shared secrets or RSA private keys for authentication.

# RSA private key for this host, authenticating it to any other host
# which knows the public part.

192.168.0.10 %any : PSK <pre shared key>

masashi : XAUTH <password>

接続してみる

同じLANですがAndroidから無事繋がりました。

f:id:paloma69:20210828143858p:plain

接続後ステータス

Androidのはターミナル等入れてないので確認できませんが、サーバ側はこんな感じ。
IPsecは張れましたが、LXD内のサーバにアクセスしていないので実際のデータ転送は未検証です。 ルーティングインストールとか上手くできているんでしょうか?

masashi@PC-ubuntu:~$ sudo ipsec statusall
Status of IKE charon daemon (weakSwan 5.8.2, Linux 5.4.0-81-generic, x86_64):
  uptime: 2 days, since Aug 25 23:43:02 2021
  malloc: sbrk 2703360, mmap 0, used 804256, free 1899104
  worker threads: 11 of 16 idle, 5/0/0/0 working, job queue: 0/0/0/0, scheduled: 14
  loaded plugins: charon aesni aes rc2 sha2 sha1 md5 mgf1 random nonce x509 revocation constraints pubkey pkcs1 pkcs7 pkcs8 pkcs12 pgp dnskey sshkey pem openssl fips-prf gmp agent xcbc hmac gcm drbg attr kernel-netlink resolve socket-default connmark stroke updown eap-mschapv2 xauth-generic counters
Virtual IP pools (size/online/offline):
  10.255.0.0/24: 254/1/0
Listening IP addresses:
  192.168.0.10
  10.28.57.1
  fd42:24ab:b835:ae42::1
Connections:
     android:  192.168.0.10...%any  IKEv1 Aggressive
     android:   local:  [192.168.0.10] uses pre-shared key authentication
     android:   remote: uses pre-shared key authentication
     android:   remote: uses XAuth authentication: any
     android:   child:  192.168.0.0/24 === dynamic TUNNEL
Security Associations (1 up, 0 connecting):
     android[7]: ESTABLISHED 57 seconds ago, 192.168.0.10[192.168.0.10]...192.168.0.15[moon.strongswan.org]
     android[7]: Remote XAuth identity: masashi
     android[7]: IKEv1 SPIs: 005e02f5920b8651_i 95777d60fe83c0aa_r*, pre-shared key reauthentication in 23 hours
     android[7]: IKE proposal: AES_CBC_128/HMAC_SHA1_96/PRF_HMAC_SHA1/MODP_1024
     android{8}:  INSTALLED, TUNNEL, reqid 7, ESP SPIs: ca903b04_i 0c287df6_o
     android{8}:  AES_CBC_128/HMAC_SHA1_96, 4024 bytes_i, 0 bytes_o, rekeying in 7 hours
     android{8}:   192.168.0.0/24 === 10.255.0.1/32

Androidの設定

ネイティブのVPNクライアントです。

f:id:paloma69:20210828145040p:plain

割愛しますが接続方法は以前のこちらから。

AndroidからpfSenseにIPsec接続して内部NWにアクセスする - paloma blog

キャプチャ

tcpdumpはこんな感じ。ESPが確認できませんね。
ログ上クイックモードになってるから?
たまに触ると忘れちゃってますね。

masashi@PC-ubuntu:~$ sudo tcpdump -nni enp2s0 port 500 or port 4500
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on enp2s0, link-type EN10MB (Ethernet), capture size 262144 bytes

14:27:09.709073 IP 192.168.0.15.500 > 192.168.0.10.500: isakmp: phase 1 I agg
14:27:09.712766 IP 192.168.0.10.500 > 192.168.0.15.500: isakmp: phase 1 R agg
14:27:09.721536 IP 192.168.0.15.500 > 192.168.0.10.500: isakmp: phase 1 I agg
14:27:09.721629 IP 192.168.0.15.500 > 192.168.0.10.500: isakmp: phase 2/others I inf[E]
14:27:09.722114 IP 192.168.0.10.500 > 192.168.0.15.500: isakmp: phase 2/others R #6[E]
14:27:09.724073 IP 192.168.0.15.500 > 192.168.0.10.500: isakmp: phase 2/others I #6[E]
14:27:09.724586 IP 192.168.0.10.500 > 192.168.0.15.500: isakmp: phase 2/others R #6[E]
14:27:09.726540 IP 192.168.0.15.500 > 192.168.0.10.500: isakmp: phase 2/others I #6[E]
14:27:09.726631 IP 192.168.0.15.500 > 192.168.0.10.500: isakmp: phase 2/others I #6[E]
14:27:09.727315 IP 192.168.0.10.500 > 192.168.0.15.500: isakmp: phase 2/others R #6[E]
14:27:10.347408 IP 192.168.0.15.500 > 192.168.0.10.500: isakmp: phase 2/others I oakley-quick[E]
14:27:10.348159 IP 192.168.0.10.500 > 192.168.0.15.500: isakmp: phase 2/others R oakley-quick[E]
14:27:10.401565 IP 192.168.0.15.500 > 192.168.0.10.500: isakmp: phase 2/others I oakley-quick[E]

まとめ

strongswanとAndroidIPSecの確認でした。
LAN内での接続なのでパラメータのチューニングは余地ありですが、
AndroidのネイティブアプリでとりあえずのVPN接続ができました。
また、今回ログ周りの編集はしていないのでデーモンのログは/var/log/syslogに吐かれます。

これでInternet経由で接続出来たら完璧なのですが、取り急ぎはSSHポートフォワードで凌ぎつつ
レンタルルータをいじってゆくゆくはIPSecに移行できたらと思います。