TExcelTAD
Home › Network Security: Discovery, Segmentation, Firewalls, VPN & IDS › Bastion Hosts, Jump Boxes and SSH Tunnels

Bastion Hosts, Jump Boxes and SSH Tunnels

The management zone of Zones, DMZs and Trust Boundaries should have as few entrances as possible. A bastion host (or jump box, jump host) is that entrance: one hardened machine that administrators connect to first, with strong authentication and full logging, and from which they reach the systems they manage. Everything else in the management zone refuses connections from anywhere but the bastion. This lesson designs a bastion, and then looks at the feature that most often undermines one: SSH tunnelling, which can turn an admin path into a hole through every firewall rule.

The SSH mechanics (keys, certificates, ProxyJump) are in Linux Administration: SSH Keys, Hardening the SSH Server and SSH Certificates & Bastion Hosts.

A bastion design

Property How
One way in the only host in the management zone reachable from outside it; from the VPN or access proxy only, never from the Internet directly
Strong authentication individual accounts, SSH certificates or hardware-backed keys, MFA
Minimal no other services, no development tools, no browser, no stored credentials for the targets; patched first
Logged every login, every session (session recording for interactive admin work), shipped off the host at once
Controlled onward access the bastion may reach targets only on admin ports; targets accept admin connections only from the bastion
Short-lived access certificates valid for hours; accounts disabled when people leave

With OpenSSH the user's side is one line, and the private key never leaves the admin's workstation (the bastion only relays the encrypted stream):

text
# ~/.ssh/config on admin01
Host bastion
    HostName bastion.lab.test
    User alice
Host *.srv.lab.test
    ProxyJump bastion

ssh db.srv.lab.test then connects to the bastion, which opens a TCP connection onward to the database server's port 22; the SSH session to the database runs end to end through it. Prefer ProxyJump to agent forwarding (ssh -A): with a forwarded agent, anyone with root on the bastion can use your keys while you are connected.

Why tunnels matter

SSH can carry arbitrary TCP connections:

Option Name What it creates
-L 8080:intranet:80 local forward a listener on the client; connections go through the SSH server to intranet:80
-R 9000:localhost:22 remote (reverse) forward a listener on the server; connections come back through the tunnel to the client
-D 1080 dynamic forward a SOCKS proxy on the client: any destination the SSH server can reach

Each is useful for administration, and each lets traffic cross a boundary the firewall thinks it controls: the firewall sees one SSH connection to the bastion, not the database session or the web browsing inside it. A reverse forward from an internal host to an outside server is a classic way to give an outsider a way in that never appears as an inbound connection.

The mechanism on loopback

The sandbox has no SSH, but a relay shows the same effect with Ncat: a service that only listens on 127.0.0.1 is "safe" from the network, until something forwards a port to it. The relay below listens on all interfaces and passes every connection to the local-only service, which is what an ssh -L 0.0.0.0:9000:127.0.0.1:8000 or a reverse tunnel does.

bash · try it yourself
mkdir -p site && echo "internal admin page" > site/index.html
python3 -m http.server 8000 --bind 127.0.0.1 --directory site >http.log 2>&1 &
for i in $(seq 50); do ss -Htln | grep -q '127.0.0.1:8000' && break; sleep 0.1; done
echo "== before: listeners"; ss -Htln | awk '{print $4}'
ncat -lk 0.0.0.0 9000 -c 'ncat 127.0.0.1 8000' >/dev/null 2>&1 &
for i in $(seq 50); do ss -Htln | grep -q ':9000' && break; sleep 0.1; done
echo "== after starting a relay: listeners"; ss -Htln | awk '{print $4}' | sort
echo "== the local-only page, fetched through the relay port:"
curl -s http://127.0.0.1:9000/
kill %1 %2
== before: listeners
127.0.0.1:8000
== after starting a relay: listeners
0.0.0.0:9000
127.0.0.1:8000
== the local-only page, fetched through the relay port:
internal admin page

The admin page was bound to loopback on purpose; a single forwarding process exposed it on every interface, and nothing about the page changed. On a real host the second listener is exactly what detection looks for.

How tunnels are detected

  • New listeners: ss -tlnp on bastions and servers, compared with the baseline (Nmap Inventories for the network view). A sshd process listening on an unexpected port is a remote forward.
  • sshd logs: with LogLevel VERBOSE, OpenSSH logs key fingerprints at login; forwarding requests appear at higher log levels and in session recordings. Some products (bastion proxies, PAM tools) log every channel.
  • Flow shape: long-lived SSH sessions with steady traffic in both directions, SSH to the Internet from servers (servers rarely need outbound SSH), SSH sessions whose volume looks like file transfer or browsing.
  • EDR on hosts: ssh -R/-D command lines, or tunnelling tools such as chisel, socat or ngrok appearing on servers.

How tunnelling is controlled

In sshd_config on the bastion and the targets:

text
# "local" instead of "no" on the bastion if ProxyJump is the only need
AllowTcpForwarding no
AllowStreamLocalForwarding no
# remote forwards may never listen on non-loopback addresses
GatewayPorts no
# no layer-3 tun devices
PermitTunnel no
AllowAgentForwarding no
X11Forwarding no
# when forwarding is allowed, only to these destinations
PermitOpen db.srv.lab.test:22 www.srv.lab.test:22
# no remote forwards at all (OpenSSH 7.8 and later)
PermitListen none

(Comments go on their own lines: sshd_config documents only whole-line # comments.)

ProxyJump uses a local forward on the bastion (direct-tcpip channels), so a bastion that must support it keeps AllowTcpForwarding local and restricts destinations with PermitOpen; targets set AllowTcpForwarding no. Add egress filtering so servers can't open SSH to arbitrary Internet hosts (Egress Filtering), and a firewall rule on targets that accepts port 22 only from the bastion's address.

The checker from Service Configuration Review extends naturally to these keywords: one more baseline table, applied to sshd -T output from every host.

Pitfalls

  • A bastion that is also a file server, a build box and somebody's IRC client.
  • Credentials for the targets stored on the bastion (one compromise exposes everything).
  • Forgetting that AllowTcpForwarding no doesn't stop a user with a shell from running their own relay (as in the session): restrict shells (ForceCommand, restricted accounts) where only forwarding is needed, and rely on detection.
  • Bypass paths: a second "temporary" way into the management zone that never goes away.

Practice

1. A firewall allows only SSH from the VPN pool to the bastion. How can a user still browse the internal wiki, and how do you stop it?

ssh -D 1080 bastion (SOCKS) or ssh -L 8080:wiki:80 bastion relays the browsing through the SSH connection. Stop it with AllowTcpForwarding no (or local with a PermitOpen list of admin destinations only) on the bastion, and a firewall rule that lets the bastion reach only admin ports on targets.

2. In the session, which command would show you the exposure on a real server?

ss -tlnp (listening TCP sockets with process names): it lists 0.0.0.0:9000 owned by ncat, next to the expected 127.0.0.1:8000.

3. Why is ProxyJump safer than copying your key to the bastion or using ssh -A?

With ProxyJump the key stays on your workstation and the bastion only relays an encrypted stream it can't read. A key copied to the bastion, or a forwarded agent, can be used by anyone who controls the bastion.

Test yourself

What creates a listener on the SSH server that leads back to the client?

  1. ssh -L
  2. ssh -R
  3. ssh -D
  4. ssh -A

Which sshd setting stops remote forwards from listening on non-loopback addresses?

  1. PermitRootLogin no
  2. GatewayPorts no
  3. X11Forwarding no
  4. MaxAuthTries 3

What did the relay in the session demonstrate?

  1. That loopback is unreachable
  2. That a forwarding process can expose a loopback-only service on all interfaces
  3. That curl can't fetch pages
  4. That Ncat encrypts traffic

Why should targets accept SSH only from the bastion's address?

  1. To save bandwidth
  2. So the bastion's authentication and logging can't be bypassed
  3. Because SSH needs it
  4. For IPv6