TT Lab
Blog Get started
Learn Learning paths Courses

SSH and File Transfer

Digging Tunnels in Three Directions

Continue in TT Lab

Goal

You build all three forwardings, -L, -R, and -D, and check for yourself where each tunnel opens its port. At the end, you take three tunnels whose processes are up but do not work, tell them apart with the tunnels' records and the server log, and fix them.

Why it matters

Forwarding is all easy as long as you do not mix up the direction. -L opens a port on my side, -R opens a port on the other side, and -D sets up a SOCKS proxy on my side. And the host name in the middle of -L is resolved from the SSH server's point of view — it does not matter if the name does not resolve on my side. This one fact is nearly all of connecting through a bastion. However, a tunnel being "up" and "working" are different. It works only when all three are right (where to listen, the server-side destination, and the server's permission), and if even one is off, the ssh process stays alive and well while only the connection is cut.

In this lab the SSH server is 127.0.0.1:2222. The server and client are the same host, so you can check the -R result right away. The incident step starts its own server (gate, 127.0.0.1:2245).

Steps

  1. Start /opt/fixtures/nt-http/server.py on port 9200, bound to 127.0.0.1. This is the tunnel's destination.
  2. With local forwarding, open a 19200 → 127.0.0.1:9200 tunnel in the background, and save the response to a request for /ok through that tunnel to /root/tun/local.txt.
  3. Check who opened 19200 and save it to /root/tun/proof.txt. The output must show even the name of the process holding that port.
  4. With remote forwarding, open 19300 on the SSH server side and send it to 127.0.0.1:9200, and save the response to a request for /ok on that port to /root/tun/remote.txt.
  5. With dynamic forwarding (SOCKS), open 11080, and save the result of requesting http://127.0.0.1:9200/ok through that proxy to /root/tun/socks.txt.
  6. Open one more local forwarding, but this time bind to all addresses, send 19201 → 127.0.0.1:9200, and save the result of connecting through your own interface IP to /root/tun/gateway.txt.
  7. Save the PIDs and command lines of the ssh tunnel processes currently running to /root/tun/inventory.txt. All four tunnels from the earlier steps (two -L, one -R, and one -D) must be there.
  8. Incident: incident ss-tunnel-refused. Three tunnels (report, admin, metrics) started by the tunnel manager are up but do not work. Leave the server configuration, service configuration, and key restrictions as they are, get all three working again, then for each tunnel write a cause code and one line of evidence to /root/incident/ss-tunnel-refused/report.txt.

Notes

Starting the backend service

Start /opt/fixtures/nt-http/server.py on port 9200, bound to 127.0.0.1. This is the tunnel's destination.

You need a service to be the tunnel's destination. Even if you start it only on loopback, it is reachable through the tunnel.

Local forwarding

With local forwarding, open a 19200 → 127.0.0.1:9200 tunnel in the background, and save the response to a request for /ok through that tunnel to /root/tun/local.txt.

After -L you write three values joined by colons. The host in the middle is resolved from the SSH server's point of view.

Checking the port the tunnel opened

Check who opened 19200 and save it to /root/tun/proof.txt. The output must show even the name of the process holding that port.

There is an option that adds processes to the list of listening sockets. With local forwarding, it is not the server but my side's client process that opens the port directly.

Remote forwarding

With remote forwarding, open 19300 on the SSH server side and send it to 127.0.0.1:9200, and save the response to a request for /ok on that port to /root/tun/remote.txt.

-R opens the port on the SSH server side. In this lab the server is the same host, so you can check the result right away.

SOCKS proxy

With dynamic forwarding (SOCKS), open 11080, and save the result of requesting http://127.0.0.1:9200/ok through that proxy to /root/tun/socks.txt.

-D takes just one port. curl has an option to specify a SOCKS5 proxy.

Binding to an outside address

Open one more local forwarding, but this time bind to all addresses, send 19201 → 127.0.0.1:9200, and save the result of connecting through your own interface IP to /root/tun/gateway.txt.

You can additionally write a bind address in front of -L. There is also a way using the -g option.

Organizing the tunnel list

Save the PIDs and command lines of the ssh tunnel processes currently running to /root/tun/inventory.txt. All four tunnels from the earlier steps (two -L, one -R, and one -D) must be there, and the grader checks those PIDs against the actual processes.

If you look at the command lines of the ssh processes currently running, you can tell the direction of each tunnel.

Incident: three tunnels that are up but do not work

Running incident ss-tunnel-refused shows a ticket. Three tunnels (report, admin, metrics) that the tunnel manager (ss-tunnels) started through the gate server (127.0.0.1:2245) have their processes up, but none of the three accepts connections. Fix them so all three work again. The tunnel definitions are in /etc/ss-tun/tunnels.conf, and after fixing you must restart them with the manager. You must not change gate's sshd configuration or the service configuration (/etc/ss-tun/services/), the restrictions tying each key to the places it may reach must remain, and they must be ssh tunnels that go through gate. In /root/incident/ss-tunnel-refused/report.txt, write one line per tunnel, such as report: <코드> (code), plus evidence: <관찰한 출력 한 줄> (one line of output you observed). Candidate codes: bind-loopback (the local forwarding opened only on loopback, so other hosts cannot connect), forwarding-disabled (the server configuration blocks TCP forwarding), key-permitopen (the key line's permitopen does not allow that destination), target-down (nothing is listening at the server-side destination), wrong-direction (-L and -R were swapped).

Direction: separate "up" from "working". First see whether the tunnel accepts the connection and then cuts it (the server side could not open the destination) or does not accept it at all (the listening place is different). Tools: ss-tunnel-check (what the user sees), each tunnel's ssh record /var/log/ss-tun/.log, the gate log /var/log/ss-lab/gate.log, ss -ltnp, sshd -T. Concretely: look at the reason after "open failed" in the record (connect failed and administratively prohibited are different); if gate refused, look at the forwarding permission in the server configuration and that key line's permitopen; and check the port the server-side service actually listens on and which address the -L opened on.