> 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/port-forwarding.md). Agents can also search and read this documentation through its [MCP server](https://rosemary-2.gitbook.io/rosemary-docs/~gitbook/mcp).

# Port Forwarding

***

*Beginner · 10 minutes*

Learn how to make an agent listen on a port and forward connections to your attacking machine.

***

### Overview

In this scenario, the Rosemary agent opens a listening port on its local network. Any machine on that network that connects to this port will have its traffic tunneled back to your attacking machine through the encrypted Rosemary connection.

**What you'll learn:**

* Agent listens on a port
* Traffic forwards to your attacking machine
* Useful for catching reverse shells, callbacks, or data exfiltration

***

### Scenario

```
[Target on Agent's Network] ───connects to───> [Agent:9001] ───Rosemary Tunnel───> [Attacker:4444]
     192.168.3.10                                     192.168.3.23                                 10.10.14.72
```

The agent listens on port `9001`. When a target on the internal network connects to `192.168.3.23:9001`, the traffic is forwarded through the tunnel to your attacking machine on port `4444`.

***

### Prerequisites

* Rosemary server running (`sudo rosemary`)
* One agent connected to the server
* Attacking machine IP accessible from the server

***

### Step-by-Step

#### Step 1: Start a Listener on Your Attacking Machine

Open a terminal on your attacking machine and start netcat:

```bash
nc -lvnp 4444
```

**Output:**

```
Listening on 0.0.0.0 4444
```

Leave this running.

***

#### Step 2: Verify Agent Connection

In the Rosemary CLI, check which agent you want to use:

```bash
rosemary> agents
```

**Output:**

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

***

#### Step 3: Create the Forward

Tell the agent to listen on port `9001` and forward to your attacking machine:

```bash
rosemary> forward add 9001 agent-1 10.10.14.72 4444 tcp
```

**Command breakdown:**

| Part          | Meaning                          |
| ------------- | -------------------------------- |
| `forward add` | Create a new forward             |
| `9001`        | Port the agent listens on        |
| `agent-1`     | Agent ID                         |
| `10.10.14.72` | Your attacking machine IP        |
| `4444`        | Port your netcat is listening on |
| `tcp`         | Protocol                         |

**Output:**

```
[+] Forward added: agent agent-1 listening on :9001 -> 10.10.14.72:4444 (tcp)
```

***

#### Step 4: Connect from a Target on the Internal Network

On any machine within the agent's network (`192.168.3.0/24`), connect to the agent's listening port:

```bash
nc 192.168.3.23 9001
```

*Replace `192.168.3.23` with your agent's actual IP on that network.*

***

#### Step 5: Verify the Connection

Type a message in the target's netcat session:

```
Hello from internal network!
```

It appears on your attacking machine's listener:

```
Listening on 0.0.0.0 4444
Connection received on 10.129.244.98 34772
Hello from internal network!
```

The connection is successfully tunneled through Rosemary.

***

#### Step 6: Clean Up

List active forwards:

```bash
rosemary> forwards
```

**Output:**

```
ID  Agent     Listen  Target          Proto
1   agent-1   :9001   10.10.14.72:4444 tcp
```

Delete the forward:

```bash
rosemary> forward del 1
```

**Output:**

```
[+] Forward 1 removed
```

***

### Use Cases

| Scenario          | Description                                     |
| ----------------- | ----------------------------------------------- |
| Reverse shell     | Catch a shell callback from an internal machine |
| Data exfiltration | Receive files sent to the agent's port          |
| Webhook receiver  | Internal service POSTs data to your listener    |
| DNS tunneling     | Forward DNS queries to your attacker DNS server |

***

### Troubleshooting

| Issue                     | Solution                                               |
| ------------------------- | ------------------------------------------------------ |
| Agent port already in use | Choose a different listening port                      |
| Attacker IP unreachable   | Ensure server can reach your IP                        |
| No connection received    | Verify target can reach agent's IP on internal network |
| Firewall blocking         | Agent's port must be accessible on internal network    |

***
