openssl で SNI を指定して接続する

openssl コマンドで SNI を偽装して接続する方法のメモ。

tcpdump を取得。

[root@ip-172-31-41-121 ~]# tcpdump -nn -i eth0 -s 0 -w /tmp/tcpdump1.pcap
dropped privs to tcpdump
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
^C179 packets captured
183 packets received by filter
0 packets dropped by kernel
[root@ip-172-31-41-121 ~]#

この間に接続を行う。

[root@ip-172-31-41-121 ~]# openssl s_client -connect www.sotoiwa.com:443 -msg -quiet 2> /dev/null | egrep "<<<|>>>"
>>> ??? [length 0005]
>>> TLS 1.3, Handshake [length 0134], ClientHello
<<< ??? [length 0005]
<<< TLS 1.3, Handshake [length 007a], ServerHello
<<< ??? [length 0005]
<<< ??? [length 0005]
<<< TLS 1.3 [length 0001]
<<< TLS 1.3, Handshake [length 000a], EncryptedExtensions
<<< ??? [length 0005]
<<< TLS 1.3 [length 0001]
<<< TLS 1.3, Handshake [length 1363], Certificate
<<< ??? [length 0005]
<<< TLS 1.3 [length 0001]
<<< TLS 1.3, Handshake [length 0108], CertificateVerify
<<< ??? [length 0005]
<<< TLS 1.3 [length 0001]
<<< TLS 1.3, Handshake [length 0024], Finished
>>> ??? [length 0005]
>>> TLS 1.3, ChangeCipherSpec [length 0001]
>>> ??? [length 0005]
>>> TLS 1.3 [length 0001]
>>> TLS 1.3, Handshake [length 0024], Finished
<<< ??? [length 0005]
<<< TLS 1.3 [length 0001]
<<< TLS 1.3, Handshake [length 007c], NewSessionTicket
<<< ??? [length 0005]
<<< TLS 1.3 [length 0001]
<<< TLS 1.3, Alert [length 0002], warning close_notify
>>> ??? [length 0005]
>>> TLS 1.3 [length 0001]
>>> TLS 1.3, Alert [length 0002], warning close_notify
[root@ip-172-31-41-121 ~]#

ファイルをダウロードして確認する。セッションマネージャー経由の SCP は以下の記事が参考になる。

パケットを確認する。Client Hello パケットの Server Name が接続先と同じ www.sotoiwa.com になっている。

もう一度 tcpdump を取得。

[root@ip-172-31-41-121 ~]# tcpdump -nn -i eth0 -s 0 -w /tmp/tcpdump2.pcap
dropped privs to tcpdump
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
^C84 packets captured
88 packets received by filter
0 packets dropped by kernel
[root@ip-172-31-41-121 ~]#

今度は SNI を変えて接続。すぐ切断される。

[root@ip-172-31-41-121 ~]# openssl s_client -connect www.sotoiwa.com:443 -servername hogehoge.example.com -msg -quiet 2> /dev/null | egrep "<<<|>>>"
>>> ??? [length 0005]
>>> TLS 1.3, Handshake [length 0139], ClientHello
<<< ??? [length 0005]
<<< TLS 1.3, Alert [length 0002], fatal handshake_failure
[root@ip-172-31-41-121 ~]#

パケットを確認する。Server Name が指定した hogehoge.example.com になっている。