> For the complete documentation index, see [llms.txt](https://hacking-3.gitbook.io/barre/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-3.gitbook.io/barre/apuntes/general/redes/protocolos/ssh/port-forwarding/example.md).

# Example

## Scenery

To access port 80 on Machine 4 from your machine (Kali) using only SSH, you can set up an SSH tunnel. Since the machines are on different subnets and you can't directly access Machine 4 from your machine, you'll need to use an SSH hop through the intermediate machines.

<figure><img src="/files/qWXIU51Hh1Bhkfu409Q8" alt=""><figcaption></figcaption></figure>

* **My machine (Kali):** <kbd>10.10.10.1</kbd>
* **Machine 2 (Accessible from your machine):** `10.10.10.2` and `20.20.20.2`
* **Machine 3 (Accessible from Machine 2):** `20.20.20.3` and `30.30.30.2`
* **Machine 4 (Accessible from Machine 3):** `30.30.30.3`

## **Solution A**

You will use an SSH tunnel from your Kali machine that hops through Machine 2, then Machine 3, and finally reaches Machine 4 to access port 80.

**Local SSH Tunnel (-L)**

You will forward a local port from your machine through the intermediate machines all the way to port 80 on Machine 4.

### **Command**

```bash
ssh -L 8080:30.30.30.3:80 user@10.10.10.2 -J user@20.20.20.2
```

### **Explanation**

* `-L 8080:30.30.30.3:80`: This means that on your local machine (Kali), port 8080 will be forwarded to port 80 on Machine 4 (IP 30.30.30.3).
* `user@10.10.10.2`: You initiate the SSH connection to Machine 2 (accessible from your machine).
* `-J user@20.20.20.2`: This jump host option tells SSH to first connect to Machine 2 (IP 10.10.10.2), then use an SSH connection to Machine 3 (IP 20.20.20.2), and eventually reach Machine 4 through the chain of hops.

### **What happens**

You will connect to port 8080 on your local machine (Kali), and the traffic will be forwarded through Machine 2 and Machine 3 all the way to Machine 4, specifically to port 80.

To access the web service on Machine 4, simply open `http://localhost:8080` in your browser, and you’ll be accessing the service running on port 80 of Machine 4.

***

## **Solution B**

### **Connect from your Kali machine (10.10.10.1) to Machine 2 (10.10.10.2)**

First, create an SSH tunnel from your machine to Machine 2 on port 8081. This tunnel will forward traffic to Machine 3.

```bash
ssh -L 8081:20.20.20.2:8082 user@10.10.10.2
```

* `-L 8081:20.20.20.2:8082`: This creates a tunnel on your Kali machine that forwards port 8081 to port 8082 on Machine 3 (IP 20.20.20.2).
* `user@10.10.10.2`: You connect to Machine 2 (IP 10.10.10.2).

Now, anything connecting to `localhost:8081` on your Kali machine will be forwarded to Machine 3 via port 8082.

### **Connect from Machine 2 to Machine 3 (20.20.20.2 to 20.20.20.3)**

Now, from Machine 2, create another tunnel to forward traffic to port 8083 on Machine 4.

```bash
ssh -L 8082:30.30.30.3:8083 user@20.20.20.2
```

* `-L 8082:30.30.30.3:8083`: This command on Machine 2 forwards port 8082 to port 8083 on Machine 4 (IP 30.30.30.3).
* `user@20.20.20.2`: You connect to Machine 3 (IP 20.20.20.2) and from there forward the traffic.

### **Connect from Machine 3 to Machine 4 (20.20.20.3 to 30.30.30.3)**

Finally, on Machine 3, create the last tunnel that redirects traffic from port 8083 to port 80 on Machine 4.

```bash
ssh -L 8083:30.30.30.3:80 user@20.20.20.3
```

* `-L 8083:30.30.30.3:80`: On Machine 3, this forwards port 8083 to port 80 on Machine 4 (IP 30.30.30.3).
* `user@20.20.20.3`: You connect to Machine 3 (IP 20.20.20.3).&#x20;

***

### **Process Summary**

* **Kali (10.10.10.1) → Machine 2 (10.10.10.2):**\
  Command: `ssh -L 8081:20.20.20.2:8082 user@10.10.10.2`
* **Machine 2 (10.10.10.2) → Machine 3 (20.20.20.2):**\
  Command: `ssh -L 8082:30.30.30.3:8083 user@20.20.20.2`
* **Machine 3 (20.20.20.3) → Machine 4 (30.30.30.3):**\
  Command: `ssh -L 8083:30.30.30.3:80 user@20.20.20.3`

***

### **What Happens**

* When someone connects to `localhost:8081` on your Kali machine, the traffic is forwarded to Machine 2 via the SSH tunnel on port 8081.
* On Machine 2, the traffic is forwarded to Machine 3 on port 8082.
* On Machine 3, the traffic is finally forwarded to Machine 4 on port 80.
