Pivoting, Tunneling, and Port Forwarding
Lateral movement is a technique that adversaries use, after compromising an endpoint, to extend access to other hosts or applications in an organization.
Pivoting's primary use is to defeat segmentation (both physically and virtually) to access an isolated network. Tunneling
, on the other hand, is a subset of pivoting. Tunneling encapsulates network traffic into another protocol and routes traffic through it.
Basic concepts
IP Addressing & NICs: Whether assigned dynamically
or statically
, the IP address is assigned to a Network Interface Controller
(NIC
). Commonly, the NIC is referred to as a Network Interface Card or Network Adapter. A computer can have multiple NICs (physical and virtual), meaning it can have multiple IP addresses assigned, allowing it to communicate on various networks.
Some adapters will have an IPv4 and an IPv6 address assigned in a dual-stack configuration allowing resources to be reached over IPv4 or IPv6.
We will see public IPs on devices that are directly facing the Internet, commonly hosted in DMZs.
Routing: Technically any computer can become a router and participate in routing. A router has a routing table that it uses to forward traffic based on the destination IP address. Sometimes we need to make a pivot host route traffic to another network. One way to do it is through the use of AutoRoute, which allows the attacker machine to have routes
to target networks that are reachable through a pivot host.
Stand-alone appliances designated as routers typically will learn routes using a combination of static route creation, dynamic routing protocols, and directly connected interfaces. Any traffic destined for networks not present in the routing table will be sent to the default route
, which can also be referred to as the default gateway or gateway of last resort.
Footprinting
Check for additional NICs:
Check out the routing table:
Port forwarding
Port forwarding
is a technique that allows us to redirect a communication request from one port to another. Port forwarding uses TCP as the primary communication layer to provide interactive communication for the forwarded port. However, different application layer protocols such as SSH or even SOCKS (non-application layer) can be used to encapsulate the forwarded traffic.
Local port forwarding
Example:
Anothe example. We will use this tunneling as a way to access locally to a remote postgresql service:
1. In the attacking machine:
2. In another terminal in the attacking machine:
3. Confirming Port Forward with Netstat
4. Confirming Port Forward with Nmap
Forward multiple ports
metasploit with proxychains
Now, run metasploit:
Dynamic Port Forwarding
Unlike local port forwarding and remote port forwarding, which use a specific local and remote port (earlier we used 1234 and 5432, for instance), dynamic port forwarding uses a single local port and dynamically assigns remote ports for each connection.
SSH for port forwarding
1. SSH command with the -D option: To use dynamic port forwarding with SSH, you can use the ssh command with the -D option, followed by the local port, the remote host and port, and the remote SSH server.
2. Setting up proxychains: Now we will require a tool that can route any tool's packets over the port 1234
. We can do this using the tool proxychains
, which is capable of redirecting TCP connections through TOR, SOCKS, and HTTP/HTTPS proxy servers.
Ping sweep: nmaps with proxychains
Once you complete the previous steps 1 and 2, you can use nmap with proxychains:
Tip
One more important note to remember here is that we can only perform a full TCP connect scan
over proxychains. The reason for this is that proxychains cannot understand partial packets.
Ping Sweep For Loop on Linux Pivot Hosts
Ping Sweep Using PowerShell
Ping Sweep For Loop Using CMD
RDP with proxychains
With proxychains you can use more services. For instance, xfreerdp:
Set the dynamic proxy using ssh portforwarding:
In the discovered networks range, scan ips. Use proxychain with nmap:
We have found $NewDiscoveredIp with open port 3389. We know the creds. So, with proxychain we can connect:
Module portfwd
in metasploit
Port forwarding can also be accomplished using Meterpreter's portfwd
module. We can enable a listener on our attack host and request Meterpreter to forward all the packets received on this port via our Meterpreter session to a remote host on the 172.16.5.0/23 network
1. Create a payload for Ubuntu Pivot Host (our victim's machine 1):
2. Start a multi/handler, also known as a Generic Payload Handler.
3. Copy the backupjob binary file to the Ubuntu pivot host over SSH and execute it to gain a Meterpreter session.
4. Once the meterpreter Session is established, use Metasploit's post-exploitation portfwd
module:
5. Connect to victim's machine2 from the attacker machine:
Module ping_sweep
in metasploit
Module ping_sweep in metasploit allows us to perform a ping sweep from our meterpreter session to the new discovered subnet (reachable only from that session).
1. Create a payload for Ubuntu Pivot Host (our victim's machine 1):
2. Start a multi/handler, also known as a Generic Payload Handler.
3. Copy the backupjob binary file to the Ubuntu pivot host over SSH and execute it to gain a Meterpreter session.
4. Once the meterpreter Session is established, do the ping sweep with the module ping_sweep:
Module Autoroute
in metasploit
Autoroute module allows you to set routes from a meterpreter session to a discovered subnet in the attacked machine. It allow us pivoting from one subnet to another.
1. Create a payload for Ubuntu Pivot Host (our victim's machine 1):
2. Start a multi/handler, also known as a Generic Payload Handler.
3. Copy the backupjob binary file to the Ubuntu pivot host over SSH and execute it to gain a Meterpreter session.
4. Once the meterpreter Session is established, send the session to the background with CTRL-z and in the same terminal use Metasploit's post-exploitation routing module socks_proxy
to configure a local proxy on our attack host.
5. Add a Line to proxychains.conf if needed (in your kali machine).
6. Finally, we need to tell our socks_proxy module to route all the traffic via our Meterpreter session. 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.
7. Make sure that you will get back the communications with a reverse proxy:
8. Finally, use proxychains to route our Nmap traffic via our Meterpreter session.
Reverse Port Forwarding
For better understanding, we will have the following machines:
- Attacker machine: Kali, with IP 192.64.166.2
- 0.Victim's machine 1: Meta_3, a ubuntu machine with 2 networks interfaces: 192.64.166.3 and 192.182.147.2
- Victim's machine 2: Meta_2, a windows machine with nic 192.182.147.3
SSH Reverse Port Forwarding
Goal: From the kali we will make a RDP connection with Meta_2, the windows machine.
For that we will use the Victim's machine 1 as a reverse port forwarding.
Obtain a reverse shell
Now we need to create a payload that is going to be triggered in the Victim's machine 2 (the windows one). First we create it in our kali:
Now we move this malicious file to the victim's machine 2:
Now, we start a listener in our kali:
Now we execute the payload from the Windows target.
We will receive the reverse shell.
Module portfwd
in metasploit
Reverse port forwarding can also be accomplished using Meterpreter's portfwd
module, where you might want to listen on a specific port on the compromised server and forward all incoming shells from the Ubuntu server to our attack host.
We will start a listener on a new port on our attack host for Windows and request the Ubuntu server to forward all requests received to the Ubuntu server on port 1234
to our listener on port 8081
.
1. Configuring & Starting multi/handler
We can now create a reverse shell payload that will send a connection back to our Ubuntu server on 192.182.147.2
: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.
Finally, if we execute our payload on the Windows host, we should be able to receive a shell from Windows pivoted via the Ubuntu server.
Socat
Socat Redirection with a Reverse Shell
Socat is a bidirectional relay tool that can create pipe sockets between 2
independent network channels without needing to use SSH tunneling.
It acts as a redirector that can listen on one host and port and forward that data to another IP address and port.
Starting Socat Listener in our attacker machine:
Configuring & Starting the multi/handler. Starting a listener on our attacked host:
Create a windows payload
Keep in mind that we must transfer this payload to the Windows host (192.182.147.3) and execute it to obtain the reverse shell.
Socat Redirection with a Bind Shell
Attacker machine: 192.64.166.2 Attacked machine 1: - 192.64.166.3: A listener in port 8080 that forwards all from 192.182.147.3:8443 - 192.182.147.2: Attacked machine 2: - 192.182.147.3: A listener in port 8443
1. Create a bind shell with the windows payload using msfvenom from our attacker machine:
2. Start a socat bind shell listener, which listens on port 8080 and forwards packets to Windows server 8443. We do this from the attacked machine 1.
3. Configuring & Starting the Bind multi/handler. We do this from the attacked machine 1.
SSH for Windows: plink.exe
Plink, short for PuTTY Link, is a Windows command-line SSH tool that comes as a part of the PuTTY package when installed. Similar to SSH, Plink can also be used to create dynamic port forwards and SOCKS proxies
NIC | Attacker machine: windows with putty | Attacked machine 2: ubuntu | Attacked machine 3: Windows |
---|---|---|---|
1 | 10.10.10.2 | 10.10.10.3 | |
2 | 10.10.20.2 | 10.10.20.3 |
Another Windows-based tool called Proxifier can be used to start a SOCKS tunnel via the SSH session we created. 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. It is possible to create a profile where we can provide the configuration for our SOCKS server started by Plink on port 9050.
After configuring the SOCKS server for 127.0.0.1
and port 9050, we can directly start mstsc.exe
to start an RDP session with a Windows target that allows RDP connections.
SSH Pivoting with Sshuttle
NIC | Attacker machine: kali | Attacked machine 2: ubuntu | Attacked machine 3: Windows |
---|---|---|---|
1 | 10.10.10.2/24 | 10.10.10.3/24 | |
2 | 10.10.20.2/24 | 10.10.20.3/24 |
Sshuttle is another tool written in Python which removes the need to configure proxychains. However, this tool only works for pivoting over SSH and does not provide other options for pivoting over TOR or HTTPS proxy servers.
Sshuttle
can be extremely useful for automating the execution of iptables and adding pivot rules for the remote host. We can configure the Ubuntu server as a pivot point and route all of Nmap's network traffic with sshuttle:
1. Installing sshuttle
2. Running sshuttle
3. We can now use any tool directly without using proxychains.
Web Server Pivoting with Rpivot
Pivoting to access a web server on an internal network
Rpivot is a reverse SOCKS proxy tool written in Python for SOCKS tunneling. Rpivot binds a machine inside a corporate network to an external server and exposes the client's local port on the server-side.
NIC | Attacker machine: kali | Attacked machine 1: ubuntu | Attacked machine 2: Windows |
---|---|---|---|
1 | 10.10.10.2/24 | ||
2 | 10.10.50.2/24 | ||
3 | 172.10.20.2/24 | 172.10.20.3/24 |
And an External Network at 10.10.40.0
We will take the scenario above, where we have a web server on our internal network (172.10.20.135
), and we want to access that using the rpivot proxy.
1. Install rpivot in kali attacker machine:
2. Run server.py with python2.7 from the kali attacker machine:
3. Transfer rpivot folder to the target, the attacked machine 1: ubuntu.
4. Run client.py from the pivot target (attacked machine 1: ubuntu):
5. Configure proxychains to pivot over our local server on 127.0.0.1:9050 on our attacker machine, which was initially started by the Python server.
6. Finally, we should be able to access the webserver on our server-side, which is hosted on the internal network of 172.16.5.0/23 at 172.16.5.135:80 using proxychains and Firefox.
Pivoting to access an external server (attack host) on the cloud
here could be scenarios when we cannot directly pivot to an external server (attack host) on the cloud. Some organizations have HTTP-proxy with NTLM authentication configured with the Domain Controller. In such cases, we can provide an additional NTLM authentication option to rpivot to authenticate via the NTLM proxy by providing a username and password. In these cases, we could use rpivot's client.py in the following way.
Client NTLM authentication example
1. Connect to a Web Server using HTTP-Proxy & NTLM Auth
Port Forwarding with Windows Netsh
Netsh is a Windows command-line tool that can help with the network configuration of a particular Windows system. Here are just some of the networking related tasks we can use Netsh
for:
Finding routes
Viewing the firewall configuration
Adding proxies
Creating port forwarding rules
We can use netsh.exe
to forward all data received on a specific port (say 8080) to a remote host on a remote port.
This can be performed entering in the Windows machine (Windows-based pivot host) and using the below command.
Now we can connect to the 8080 port of the Windows machine (Windows-based pivot host) from our kali attacking machine using xfreerdp:
Tunneling
DNS tunneling with Dnscat2
Dnscat2 is a tunneling tool that uses DNS protocol to send data between two hosts. It uses an encrypted Command-&-Control
(C&C
or C2
) channel and sends data inside TXT records within the DNS protocol.
There is a powershell version for the dnscat2 client: https://github.com/lukebaggett/dnscat2-powershell
dnscat2 comes in two parts: the client and the server.
- The client is designed to be run on a compromised machine. When you run the client, you typically specify a domain name. All requests will be sent to the local DNS server, which are then redirected to the authoritative DNS server for that domain (which you, presumably, have control of).
- The server is designed to be run on an authoritative DNS server.
dnscat2 strives to be different from other DNS tunneling protocols by being designed for a special purpose: command and control. It can tunnel any data, with no protocol attached. Which means it can upload and download files, it can run a shell, and it can do those things well. It can also potentially tunnel TCP, but that's only going to be added in the context of a pen-testing tool (that is, tunneling TCP into a network), not as a general purpose tunneling tool.
1. Install it in the attacker machine kali:
2. Start the dnscat2 server by executing the dnscat2 file.
After running the server, it will provide us the secret key, which we will have to provide to our dnscat2 client on the Windows host so that it can authenticate and encrypt the data that is sent to our external dnscat2 server:
3. Depending on if we are attacking a windows or a linux machine, we need to use a dnscat2 version for linux or for windows.
- Linux: the original dnscat2
- Windows: https://github.com/lukebaggett/dnscat2-powershell
In this case our attacked machine 1 is a windows machine so we will install dnscat 2 powershell version for clients.
Installation:
4. After dnscat2.ps1 is imported, we can use it to establish a tunnel with the server running on our attack host. We can send back a CMD shell session to our server.
5. In the server launched from the kali machine you will see:
6. Dnscat2 cheat sheet commands:
SOCKS5 Tunneling with Chisel
Chisel pivot
Chisel is a TCP/UDP-based tunneling tool written in Go that uses HTTP to transport data that is secured using SSH. Chisel
can create a client-server tunnel connection in a firewall restricted environment.
This can allow us to pivot from a reachable network segment to a different network segment. For that, we will use the compromised machine (Ubuntu) as our Chisel server, listing on port 1234:
NIC | Attacker machine: kali | Compromised machine Ubuntu | Target: A webserver |
---|---|---|---|
1 | 10.10.10.2/24 | 10.10.10.3/24 | |
2 | 172.16.5.10/23 | Webserver located at the network range 172.16.5.0/23 with a Domain Controller at 172.16.5.19 |
1. Install chisel:
2. Once the binary is built, we can use SCP to transfer it to the target pivot host.
3. Run the Chisel Server on the Pivot Host, the ubuntu Attacked machine:
The Chisel listener will listen for incoming connections on port 1234 using SOCKS5 (--socks5) and forward it to all the networks that are accessible from the pivot host. In our case, the pivot host has an interface on the 172.16.5.0/23 network, which will allow us to reach hosts on that network.
4. Connect to the chisel server
Results:
The Chisel client has created a TCP/UDP tunnel via HTTP secured using SSH between the Chisel server and the client and has started listening on port 1080.
5. Now we can modify our proxychains.conf file located at /etc/proxychains4.conf and add 1080 port at the end so we can use proxychains to pivot using the created tunnel between the 1080 port and the SSH tunnel.
6. Now if we use proxychains with RDP, we can connect to the DC on the internal network through the tunnel we have created to the Pivot host.
Chisel Reverse Pivot
In the previous example, we used the compromised machine (Ubuntu) as our Chisel server, listing on port 1234. Still, there may be scenarios where firewall rules restrict inbound connections to our compromised target. In such cases, we can use Chisel with the reverse option.
NIC | Attacker machine: kali | Compromised machine Ubuntu | Target: A webserver |
---|---|---|---|
1 | 10.10.10.2/24 | 10.10.10.3/24 | |
2 | 172.16.5.10/23 | Webserver located at the network range 172.16.5.0/23 with a Domain Controller at 172.16.5.19 |
1. Start the Chisel Server on our attacker machine or attacker host:
2. Then we connect from the Ubuntu (pivot host) to our attacker machine (or attack host), using the option R:socks
Results:
3. Now we can modify our proxychains.conf file located at /etc/proxychains4.conf and add 1080 port at the end so we can use proxychains to pivot using the created tunnel between the 1080 port and the SSH tunnel.
4. Now if we use proxychains with RDP, we can connect to the DC on the internal network through the tunnel we have created to the Pivot host.
ICMP tunneling with SOCKS
ICMP tunneling encapsulates your traffic within ICMP packets containing echo requests and responses.
Important: ICMP tunneling would only work when ping responses are permitted within a firewalled network.
We will use the ptunnel-ng tool to create a tunnel between our Ubuntu server and our attack host. Once a tunnel is created, we will be able to proxy our traffic through the ptunnel-ng client
. We can start the ptunnel-ng server
on the target pivot host.
1. Install ptunnel-ng in the attacker machine:
2. Start the ptunnel-ng Server on the Target Host
3. With the ptunnel-ng ICMP tunnel successfully established, we can attempt to connect to the target using SSH through local port 2222 (-p2222).
Now we can:
- OR Tunnel an SSH connection through an ICMP Tunnel
- OR use proxychains with nmap to scan targets on the internal network.
- OR use proxychains with RDP to connect to the machine in the other network range:
RDP and SOCKS Tunneling with SocksOverRDP
There are often times during an assessment when we may be limited to a Windows network and may not be able to use SSH for pivoting. We would have to use tools available for Windows operating systems in these cases. SocksOverRDP is an example of a tool that uses Dynamic Virtual Channels
(DVC
) from the Remote Desktop Service feature of Windows. DVC is responsible for tunneling packets over the RDP connection. Some examples of usage of this feature would be clipboard data transfer and audio sharing. However, this feature can also be used to tunnel arbitrary packets over the network. We can use SocksOverRDP
to tunnel our custom packets and then proxy through it. We will use the tool Proxifier as our proxy server.
NIC | Attacker machine: kali | Attacked machine 1: Windows | Attacked machine 2: Windows | Attacked machine 3: Windows |
---|---|---|---|---|
1 | 10.10.10.2 | 10.10.10.3 | ||
2 | 172.16.5.150 | 172.16.5.19 | ||
3 | 172.16.6.150 | 172.16.6.155 |
Our goal will be reaching the attacked machine 3.
1. First, we download appropriate binaries to our kali attack host:
2. Attacking machine 1
Connect to the target using xfreerdp and transfer SocksOverRDPx64.zip to the Windows target machine. Once done, unzip it.
From the Windows machine 1, we will then need to load the SocksOverRDP.dll (located within the SocksOverRDP-x64 folder) using regsvr32.exe. Do it as Admin
3. Attacking machine 2
Now, from the machine 1, we can connect to machine 2 over RDP using mstsc.exe
. Windows-R and enter mstsc.exe. Enter the machine 2 IP 172.16.5.19, username and password. When doing so, we should receive a prompt that the SocksOverRDP plugin is enabled. And we will have access to the machine 2 from the RDP connection done from machine 1.
4. Attacking machine 3
Now we need to transfer SocksOverRDPx64.zip or just the SocksOverRDP-Server.exe to machine 2, 172.16.5.19. You can just try to copy paste it.
When done, start SocksOverRDP-Server.exe with Admin privileges in machine 2.
When we go back to our foothold target (machine 1) and check with Netstat, we should see our SOCKS listener started on 127.0.0.1:1080.
Also, we transfer from our kali machine to the machine 1, the proxifier portable. Once done we configure it in machine 1 to forward all our packets to 127.0.0.1:1080. Use SOCKS5 as protocol.
With Proxifier configured and running, we can start mstsc.exe, and it will use Proxifier to pivot all our traffic via 127.0.0.1:1080, which will tunnel it over RDP to 172.16.5.19, which will then route it to 172.16.6.155 using SocksOverRDP-server.exe.
So, now Windows-R and mstsc.exe to connect to machine 3 with username and password. If there are some problems with the connection, play in RDP program with the tab EXPERIENCE.
Last update: 2025-01-19 Created: November 5, 2024 21:44:57