2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

閉じたVPC内のHTTPSサービスにEC2 SSH踏み台を通してアクセスする

2
Last updated at Posted at 2022-12-15

閉じたVPC内のEC2へのSSH

インターネット接続経路が無いVPC内にあるEC2でも、SSM経由ならSSHアクセスが可能です。

HTTPならSSHトンネルでもアクセス可能

SSHが通るなら、(HTTPSではなく) HTTP のサイトなら簡単にトンネルを貼ってアクセスすることが可能です。

ssh -L 8080:yoursite.local:80 bastion

アクセス先のホスト名は変わりますが、アクセスできます。

curl http://localhost:8080
-> OK

ただ HTTPS の場合は、

ssh -L 8081:yoursite.local:443 bastion

証明書エラーになり上手くいきません。

curl https://localhost:8081/
curl: (35) OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to localhost:8081

Dynamic Port Forwardingを使えばHTTPSもアクセス可能

Dynamic Port Forwardingを使えばHTTPSもアクセス可能になります。

-D オプションで待ち受けポートを指定して踏み台にSSHします。

# Port番号1080は任意
# [-D 0.0.0.0:1080]なども指定可能
ssh -D 1080 bastion

HTTPSやHTTPでなく、 socks5をスキーマに指定して実行すると通ります。ホスト名は本来のアクセス先のホスト名です。

curl -x socks5://localhost:1080 https://yoursite.local

ブラウザと一緒に利用もできます。

> "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --proxy-server="socks5://localhost:1080"

ブラウザの拡張機能と合わせて使うと便利です。

それぞれSOCKS5の設定をすることで繋がります。

image.png

アプリ等がSocks Proxyに対応していない場合

アプリが対応してないなど、どうしてもHTTPのPROXYを通したアクセスが必要な場合は、SOCKS5のPROXYを更に DeleGate でラップしてあげると、HTTPのPROXYとして利用可能です。HTTPS_PROXY 等の環境変数と一緒に普通のPROXYとして利用可能になります。

Dynamic Port Forwardingの準備をして、

ssh -D ${PRIVATE_IP}:1080 bastion

DeleGateDockerコンテナ使ってラップします。

docker run --rm -p 8080:8080  vimagick/delegated -P8080 SERVER=http SOCKS=${PRIVATE_IP}:1080

これでHTTP ProxyとしてもHTTPS通すことができます。

curl -x http://localhost:8080 https://yoursite.local
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?