> For the complete documentation index, see [llms.txt](https://red.infiltr8.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://red.infiltr8.io/redteam/pivoting/ssh-tunneling.md).

# SSH Tunneling

## Theory

SSH tunneling, also known as "SSH port forwarding," is a method that uses the secure shell (SSH) protocol to create encrypted tunnels for network connections. SSH tunneling may be used for covert communication and circumventing network security measures.

## Practice

### SSH Port Forwarding

By using a SSH client with an OpenSSH server, it's possible to create both forward and reverse connections to make SSH tunnels, allowing us to forward ports, and/or create proxies.

{% content-ref url="/pages/DT8wxsRmDlxsm9G8QOJo" %}
[Port Forwarding](/redteam/pivoting/portfwd.md)
{% endcontent-ref %}

### Sshuttle

[**Sshuttle**](https://github.com/sshuttle/sshuttle) uses an SSH connection to create a tunnelled proxy that acts like a new interface. In short, it simulates a VPN, allowing us to route our traffic through the proxy. As it creates a tunnel through SSH, anything we send through the tunnel is also encrypted.

{% tabs %}
{% tab title="Tunnel to Host" %}
We can create our tunnelled proxy by connecting with schuttle to the compromised host's SSH server.

```bash
# Create Tunnel
# SUBNET: specify your subnet (e.g 172.16.0.0/24)
sshuttle -r <USER>@<TARGET_IP> <SUBNET>

# Automatically determine the subnets
sshuttle -r <USER>@<TARGET_IP> -N

# Exclude the specific ip (-x)
sshuttle -r <USER>@<TARGET_IP> <SUBNET> -x <remote-ip>
```

If you don't know the user's password but have an SSH Key, we may use following command

```bash
sshuttle -r <USER>@<TARGET_IP> --ssh-cmd "ssh -i KEYFILE" <SUBNET>
```

{% hint style="danger" %}
If you get the error "Failed to flush caches: Unit dbus-org.freedesktop.resolve1.service not found...", you need to flush DNS cache.

```bash
sudo systemctl enable systemd-resolved.service
sudo resolvectl flush-caches
```

Run sshuttle again.
{% endhint %}
{% endtab %}
{% endtabs %}

## Resources

{% embed url="<https://tryhackme.com/room/wreath>" %}

{% embed url="<https://exploit-notes.hdks.org/exploit/network/protocol/ssh-pentesting/#sshuttle>" %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://red.infiltr8.io/redteam/pivoting/ssh-tunneling.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
