Post

SSH tunneling

1
2
$ ssh user\_name@remote\_host\_ip -p port

client와 server가 SSH tunneling으로 연결되어 있는 상태에서 client가 server와 연결된 SSH tunneling 포트를 이용해 최종 호스트로 데이터를 전송하면 client는 SSH tunneling으로 연결되어 있는 포트로 들어오는 패킷을 SSL로 encryption 해서 server로 전송하고, server에서는 이를 받아 decryption한 다음 최종 호스트/포트로 전송한다. 따라서 tunneling 구간은 client - server이다.

보통 tunneling 기능은 로컬포트포워딩을 사용하며, reverse SSH가 필요할 때 원격포트포워딩을 사용한다.

Local port forwarding

local의 특정 포트로 오는 패킷을 SSL로 encryption해서 remotehost로 보낸 후 remotehost에서 decryption해서 최종 목적지의 특정 포트로 포워딩해준다. 즉 local이 client이고, remotehost가 server다.

1
2
3
4
local~ $ ssh -L 1234:destination:80 username@remotehost
Password:
\* local:1234 ==> remotehost:22 --> destination:80

[local]에 포트 [1234]를 열고, 이 포트로 오는 모든 트래픽은 SSH에 의해 암호화되어 [remotehost]로 전송되며, 다시 [remotehost]입장에서 [destination]의 포트 [80]번으로 포워딩된다.

destination에 localhost가 들어가는 경우 이 localhost는 remotehost를 의미한다. remotehost 입장에서의 localhost이기 때문.

그래서 destination에 localhost를 적으면 remotehost와 1:1 암호 통신이 되어 local:1234 ==> remotehost:22->80대로 흐른다.

Remote port forwarding

remotehost의 특정 포트로 오는 트래픽을, local 전송한 후 decryption해서 destination의 특정 포트로 포워딩한다. 즉, local이 server고 remotehost가 client가 된다.

1
2
3
4
local~ $ ssh -R 1234:destination:23 username@remotehost
Password:
\*remotehost:1234 ==> local:22 --> destination:23

[remotehost]에 포트 [1234]를 열고, 이 포트로 오는 모든 트래픽은 SSH에 의해 암호화되어 [local]로 전송되며, 다시 [local] 입장에서 [destination]의 [23]번 포트로 포워딩 된다.

reverse SSH

destination에 localhost를 적으면, reverse SSH가 된다. 패킷은 remotehost:1234 ==> local:22->23 대로 흐른다.

  • -L이나 -R이나 [username]과 [Password]는 [remotehost]의 것을 입력해야 한다.
  • f는 fork 인 듯. N없이 fork만 쓰면 오류난다.
  • -L이든 -R이든 remotehost의 shell이 열리는데, 이를 background에서 실행하고 싶으면
  • -fN을 지정한다

putty를 이용한 SSH tunneling

Dynamic으로 놓고 data를 보낼 localport를 정해서 Add 한 다음, forwarding 할 remote host에 SSH 연결한다. 인터넷 속성의 프록시 설정에 localhost:위에서 정한 localport를 입력하여 설정하면, 이제부터의 데이터 흐름은 다음과 같다. * 인터넷 속성을 사용하지 않고 Chrome의 바로가기 - 대상에 Command-line option을 주면 별도로 설정 가능하다.

1
2
localhost:localport -> remotehost:22 -> remotehost:random\_port -> destination

virtual machine을 사용하여 구성한 경우는 다음과 같은 그림이 된다. * 포트를 모두 변경해 놓았음을 참고.

This post is licensed under CC BY 4.0 by the author.