LoginSignup
3
3

More than 1 year has passed since last update.

SSH Reverse Port Forwarding で鍵ごとにlistenできるポートを制限する

Last updated at Posted at 2016-08-26

2023-04-29 追記
OpenSSH 7.8以降ではパッチなどあてなくてもPermitListenを使うとリバースフォワードするクライアントがsshサーバ上でLISTENできるポートを制限できます。

Tunnel Broker をつくるみたいな稀な用途でしか必要にならなさそうですが、(リバースでない)普通のポートフォワーディングをやるときのpermitopenの要領で、リバースポートフォワーディングのセッションがサーバ側でlistenするポートを制限できるpermitremoteopenを有効にするパッチが公開されていたので、試してみた。

Ubuntu 16.04で、既にaptでopensshが導入済みの状態でインストールしてみる。

$ apt-get update && apt-get install -y build-essential zlib1g-dev libssl-dev git
$ git clone https://github.com/antonyantony/openssh.git
$ cd openssh
./configure --prefix=/usr --sysconfdir=/etc/s
sh --with-md5-passwords --with-privsep-path=/var/lib/sshd
$ make install
$ systemctl stop ssh
$ systemctl start ssh

authorized_keysに鍵ごとに許可するポートを記述する。

permitremoteopen="8004" ssh-rsa AAAAB3NzaC1yc2EAAAADAQ.....

クライアント側でsshを実行する。

$ ssh -v -N -R 8004:localhost:1880 miminashi@160.16.xxx.xxx
.
.
.
Authenticated to 160.16.66.104 ([160.16.66.104]:22).
debug1: Remote connections from LOCALHOST:8004 forwarded to local address localhost:1880
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
debug1: remote forward success for: listen 8004, connect localhost:1880
debug1: All remote forwarding requests processed

成功したっぽい。

次は、8005を指定してみる。

$ ssh -v -N -R 8005:localhost:1880 miminashi@160.16.xxx.xxx
.
.
.
Authenticated to 160.16.66.104 ([160.16.66.104]:22).
debug1: Remote connections from LOCALHOST:8005 forwarded to local address localhost:1880
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
debug1: Remote: Server denied remote port forward request.
debug1: remote forward failure for: listen 8005, connect localhost:1880
Warning: remote port forwarding failed for listen port 8005
debug1: All remote forwarding requests processed

無事失敗した。

とりあえず動くかどうか試したかったので行儀の悪い入れ方をしたけど、apt-get sourceでソースコードとってきてパッチあてたほうがいい気がする。

3
3
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
3
3