> For the complete documentation index, see [llms.txt](https://rosemary-2.gitbook.io/rosemary-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://rosemary-2.gitbook.io/rosemary-docs/tutorials/reverse-port-forwarding.md). Agents can also search and read this documentation through its [MCP server](https://rosemary-2.gitbook.io/rosemary-docs/~gitbook/mcp).

# Reverse Port Forwarding

***

*Intermediate · 10 minutes*

Learn how to expose a service running on the agent's localhost to your attacking machine through a reverse tunnel.

***

### Scenario

You have a service running on the agent machine bound to `127.0.0.1:3389` (e.g., RDP). It's **not accessible** from the network. You want to access this service from your attacking machine through Rosemary.

```
[Your Attacking Machine] ───connects to───> [Server:13389] ───Tunnel───> [Agent] ───dials───> [127.0.0.1:3389]
   127.0.0.1:13389                                    Rosemary              192.168.3.23           (RDP on agent localhost)
```

**The server listens on a port. When you connect to it, the agent dials the target.**

***

### Step-by-Step

#### Step 1: Verify Service on Agent (Localhost Only)

SSH into the agent machine and verify RDP is running on localhost:

```bash
# On agent machine
sudo netstat -tlnp | grep 3389
```

**Output:**

```
tcp        0      0 127.0.0.1:3389          0.0.0.0:*               LISTEN      1234/xrdp
```

The service is bound to `127.0.0.1` only—inaccessible from the network.

***

#### Step 2: Verify Agent Connection

In Rosemary CLI:

```bash
rosemary> agents
```

**Output:**

```
ID        OS       Hostname      Subnets
agent-2   linux    pivot-host    192.168.3.0/24
```

***

#### Step 3: Create the Reverse Forward

```bash
rosemary> rforward add 13389 agent-2 127.0.0.1 3389
```

**Command breakdown:**

| Part           | Meaning                         |
| -------------- | ------------------------------- |
| `rforward add` | Create a reverse forward        |
| `13389`        | Port the **server** listens on  |
| `agent-2`      | Agent ID                        |
| `127.0.0.1`    | Target host (agent's localhost) |
| `3389`         | Target port (RDP)               |

**Output:**

```
[+] Reverse forward added: agent agent-2 listening on :13389 -> 127.0.0.1:3389 (id: abc123)
```

***

#### Step 4: Access the Service from Your Attacking Machine

From your attacking machine, connect to **the server's listening port**:

```bash
# RDP client
rdesktop 127.0.0.1:13389

# Or via curl if HTTP service
curl http://127.0.0.1:13389
```

The connection flows:

1. You → Server:13389
2. Server → Agent via QUIC (UDP)
3. Agent → 127.0.0.1:3389

***

#### Step 5: List Active Reverse Forwards

```bash
rosemary> rforward list
```

**Output:**

```
ID      Agent     Listen  Target
abc123  agent-2   :13389  127.0.0.1:3389
```

***

#### Step 6: Clean Up

```bash
rosemary> rforward del abc123
```

**Output:**

```
[+] Reverse forward abc123 removed
```

***

### Use Cases

| Scenario          | Description                              |
| ----------------- | ---------------------------------------- |
| RDP access        | Access Windows RDP bound to localhost    |
| Local databases   | MySQL/PostgreSQL on 127.0.0.1            |
| Docker containers | Container ports mapped only to localhost |
| Internal APIs     | Microservices listening on 127.0.0.1     |
| Admin panels      | Web UIs restricted to localhost          |

***

### Forward vs Reverse Forward

| Feature       | Forward                       | Reverse Forward                        |
| ------------- | ----------------------------- | -------------------------------------- |
| **Listener**  | Agent                         | Server                                 |
| **Direction** | Your machine → Agent → Target | Your machine → Server → Agent → Target |
| **Command**   | `forward add`                 | `rforward add`                         |
| **Use case**  | Access remote service locally | Access agent's localhost service       |

***

### Dashboard Equivalent

<figure><img src="https://3327027809-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FXtbgQqKrmN1ymqKZFFPU%2Fuploads%2FERdDuevOXLhfRcwKJe17%2Fimage.png?alt=media&#x26;token=0f78c970-6210-4e86-a271-daefbe976ff7" alt=""><figcaption></figcaption></figure>

In the Rosemary dashboard:

1. Navigate to **Reverse Forwards** tab
2. Select **Agent ID:** `agent-2`
3. **Server Listen Port:** `13389`
4. **Target Host:** `127.0.0.1`
5. **Target Port:** `3389`
6. Click **Add Reverse Forward**

***
