Table of Contents
Tunneling
Creates a secure or encrypted path for data to pass through restricted networks. It is used to bypass firewalls and use a plethora of protocols to hide traffic to providing secure communication between two endpoints.
Pivoting
Uses a compromised system as a gateway to access other parts of a network. By routing traffic through the compromised machine, an attacker can reach isolated systems and move laterally within the network.
Port Forwarding
Port forwarding encompasses local, remote, and dynamic forwarding.
Local port forwarding redirects traffic from a local machine’s port to an internal machine, typically used to access services behind a firewall.
Remote port forwarding allows remote machines to access services on a local network by forwarding a remote port to an internal system, typically used to allow access to our host (for example a webserver) to others.
Dynamic port forwarding acts like a proxy server, forwarding traffic dynamically to multiple destinations, often used for secure tunneling or SOCKS proxies, enabling access to various internal resources through a single tunnel.
Here are also some good GPT examples to get the idea across:
Local Port Forwarding
Use Case: Accessing a remote database from your local machine.
- Scenario: You have a database running on a remote server (e.g., MySQL on port 3306) that is not directly accessible from your local network due to firewall restrictions.
- Solution: Use SSH local port forwarding to map a local port (e.g., 3307) to the remote server’s database port.
- Command:
ssh -L 3307:localhost:3306 user@remote-server - Result: You can now connect to the remote database by connecting to
localhost:3307on your local machine, which tunnels through the SSH connection tolocalhost:3306on the remote server.
Remote Port Forwarding
Use Case: Allowing access to a local web server from a remote machine.
- Scenario: You are developing a web application on your local machine (e.g., running on port 8080) and need to share it with a remote colleague for testing.
- Solution: Use SSH remote port forwarding to make your local web server accessible from a remote server.
- Command:
ssh -R 8081:localhost:8080 user@remote-server - Result: Your colleague can now access your local web server by navigating to
remote-server:8081.
Dynamic Port Forwarding
Use Case: Creating a secure browsing tunnel through an SSH server.
- Scenario: You want to securely browse the internet through an SSH server to protect your traffic on an unsecured network (e.g., public Wi-Fi).
- Solution: Use SSH dynamic port forwarding to set up a SOCKS proxy on your local machine.
- Command:
ssh -D 1080 user@ssh-server - Result: Configure your browser to use
localhost:1080as a SOCKS proxy. All your browser traffic will now be routed through the SSH server, encrypting your data and masking your IP address.
Also, regarding the connection to the port forwarding:
- With local port forwarding, the access is started and limited to your local machine. Meaning we initiate a connection to the external host.
Attack host -> Target host - With remote port forwarding, typically, access is from the remote host where the port is forwarded, though others might also access it based on network configurations.
Attack host <- Target host(s) - With dynamic port forwarding, the tunnel is accessible from both your local machine and the remote host. It enables bidirectional access.
Attack host <-> Target host
This means, that when you create a LPF you need to initiate the connection, remote host can’t do it by itself. With RPF only remote host can do it (or anybody that can use it). and DPF (or SOCKS) is bidirectional so anything goes.
Also, regarding the role of each tools used:
- SSH: SSH is like a magical ward that shields your party as they traverse a dangerous, enchanted forest. It ensures no enemy or trap can intercept or harm your precious cargo (data) during the journey.
- SOCKS: SOCKS is the trusted guide who knows hidden paths in the forest, leading you swiftly through the safest routes. It ensures your group always reaches the right destination, regardless of what they’re carrying.
- Proxychains: Proxychains is the translator between two languages - one of your party members speaks Elvish (the app), but the guide (SOCKS) speaks Dwarvish. Proxychains helps both understand each other, ensuring the journey goes smoothly.
References
Good read regarding local, remote and dynamic port forwarding - also ssh tunneling in general.
https://blog.jakuba.net/ssh-tunnel---local-remote-and-dynamic-port-forwarding/
SOCKS
SOCKS stands for “Socket Secure”, a protocol that helps communicate with servers where you have firewall restrictions in place.
A typical attack using SOCKS for pivoting, the attacker uses a compromised machine as a SOCKS server (pivot) to route their traffic to the target host, which would otherwise be inaccessible due to firewalls. The SOCKS client is the tool (or the attacker’s machine) that connects to the SOCKS server to reach the final target.
Reverse pivot reverses where the client and the server are located (as with the chisel reverse proxy).
SOCKS proxies are currently of two types: “SOCKS4” and “SOCKS5”. SOCKS4 doesn’t provide any authentication and UDP support, whereas SOCKS5 does provide that.