πŸ”„Remote Port Forwarding

Overview

Remote port forwarding (SSH -R) allows us to forward a local service to a remote port. This is particularly useful when the target host cannot directly reach our attack host, but can communicate through a pivot host.

Based on HTB Academy Page 4: Remote/Reverse Port Forwarding with SSH


Scenario Description

Network Topology

[Attack Host] ←→ [Ubuntu Pivot] ←→ [Windows Target]
10.10.15.x         10.129.202.64      172.16.5.19
                   172.16.5.129       (RDP Service)

The Problem

  • Windows host can only communicate within 172.16.5.0/23 network

  • No direct route from Windows to Attack Host network

  • Need reverse shell but Windows can't reach back to Attack Host

  • Solution: Use Ubuntu server as pivot point


Remote Port Forwarding Concepts

SSH Remote Port Forwarding (-R)

Purpose: Forward remote port back to local service

Syntax:

Traffic Flow:


Practical Implementation (HTB Academy Lab)

Step 1: Create Meterpreter Payload

Generate Windows HTTPS Payload:

Key Points:

  • LHOST = Pivot internal IP (172.16.5.129)

  • LPORT = Port on pivot for forwarding (8080)

  • Format = Windows executable

Step 2: Configure Metasploit Handler

Set up Multi Handler:

Important:

  • LHOST = 0.0.0.0 (listen on all interfaces)

  • LPORT = 8000 (different from payload port)

Step 3: Transfer Payload to Pivot

Copy Payload to Ubuntu Server:

Start Web Server on Pivot:

Step 4: Download Payload on Windows Target

From Windows target (via RDP session):

Step 5: Create SSH Remote Port Forward

Set up Remote Forward Tunnel:

Tunnel Configuration:

  • Remote bind: 172.16.5.129:8080 (pivot internal IP)

  • Local forward: 0.0.0.0:8000 (attack host handler)

  • Direction: Pivot port 8080 β†’ Attack host port 8000

Step 6: Execute Payload and Get Shell

Execute on Windows Target:

Monitor SSH Tunnel Logs:

Receive Meterpreter Session:


Technical Analysis

Why Remote Port Forwarding Works

Network Isolation Problem:

Solution Flow:

  1. Windows connects to Pivot:8080

  2. SSH tunnel forwards to Attack Host:8000

  3. Metasploit receives connection as if local

Connection Source Analysis


Alternative Remote Port Forwarding Examples

Example 1: HTTP Service Exposure

Example 2: Database Access

Example 3: Multiple Service Forwarding


Remote vs Local Port Forwarding Comparison

Aspect

Local Forward (-L)

Remote Forward (-R)

Direction

Remote service β†’ Local access

Local service β†’ Remote access

Use Case

Access remote service locally

Expose local service remotely

Syntax

ssh -L local:remote:port user@host

ssh -R remote:local:port user@host

Traffic Flow

Local β†’ SSH β†’ Remote

Remote β†’ SSH β†’ Local

Example

Access internal web server

Expose reverse shell listener


Security Considerations

Payload Security

  1. Encrypt payloads when transferring

  2. Use HTTPS for meterpreter connections

  3. Clean up payloads after use

  4. Monitor for AV detection

Tunnel Security

  1. Use key authentication for SSH

  2. Limit forwarding ports to necessary only

  3. Monitor tunnel connections for anomalies

  4. Clean up tunnels after assessment

Operational Security

  1. Mimic legitimate traffic patterns

  2. Use standard ports when possible

  3. Avoid suspicious executable names

  4. Document all forwarding configurations


Troubleshooting Common Issues

1. Payload Not Connecting

2. SSH Tunnel Issues

3. Handler Not Receiving Connections

4. Windows Payload Execution Issues


HTB Academy Official Walkthrough

Complete Step-by-Step Guide (HTB Academy)

Objective: Obtain reverse shell from Windows target through Ubuntu pivot using SSH remote port forwarding.

Step 1: Create Meterpreter Payload

Step 2: Configure Metasploit Handler

Step 3: Transfer Payload to Pivot

Step 4: Setup Dynamic Port Forwarding

Step 5: Start Web Server on Pivot

Step 6: RDP to Windows Target

Step 7: Download Payload on Windows

Step 8: Setup SSH Remote Port Forward

Step 9: Execute Payload & Get Shell


HTB Academy Lab Questions

Question 1: Ubuntu Pivot Internal IP

Which IP address assigned to the Ubuntu server Pivot host allows communication with the Windows server target?

Answer: 172.16.5.129

Explanation: The Ubuntu server has two interfaces:

  • ens192: 10.129.202.64 (external network)

  • ens224: 172.16.5.129 (internal network)

Windows target (172.16.5.19) can only communicate within 172.16.5.0/23 network.

Question 2: Handler Listening Address

What IP address is used on the attack host to ensure the handler is listening on all IP addresses assigned to the host?

Answer: 0.0.0.0

Explanation: Setting lhost 0.0.0.0 in Metasploit makes the handler listen on ALL network interfaces.


Network Diagram

Remote Port Forwarding Flow


Best Practices Summary

  1. Plan payload configuration carefully (pivot internal IP)

  2. Use appropriate ports for forwarding

  3. Test connectivity at each step

  4. Monitor tunnel status during operations

  5. Clean up all artifacts after assessment

  6. Document forwarding configurations for reporting


HTB Academy Official Answer Key

Complete Official Walkthrough with Expected Outputs

Lab Question: "Which IP address assigned to the Ubuntu server Pivot host allows communication with the Windows server target? (Format: x.x.x.x)"

Official Answer: 172.16.5.129

Step 1: Create Windows HTTPS Reverse Shell Payload

Expected Output:

Step 2: Configure and Start Msfconsole Multi-Handler

Expected Output:

Step 3: Transfer Msfvenom Payload to Pivot Host

Expected Output:

Step 4: SSH Dynamic Port Forwarding to Pivot

Expected Output:

Step 5: Start Python Web Server on Ubuntu

Expected Output:

Step 6: Connect to Windows Target via Proxychains

Expected Output:

Step 7: Download Payload on Windows Target

Note: Run PowerShell as Administrator on Windows target

Expected Output:

Step 8: Perform SSH Remote Port Forward

Expected Output:

Step 9: Execute Payload to Get Reverse Shell

From Windows PowerShell (as Administrator):

Expected Result: Meterpreter session established on the Metasploit handler through the SSH remote port forward tunnel.

Lab Success Criteria

βœ… Payload created with correct LHOST (172.16.5.129) βœ… Metasploit handler listening on 0.0.0.0:8000 βœ… SSH dynamic forward established for RDP access βœ… Python web server serving payload from pivot βœ… RDP connection to Windows target via proxychains βœ… Payload downloaded on Windows target βœ… SSH remote forward tunnel active βœ… Reverse shell received via tunnel


🎯 Practical Lab Experience - July 19, 2025

Real-World Implementation Success

Lab Environment:

  • Target Machine: 10.129.202.64 (Ubuntu Pivot)

  • Windows Target: 172.16.5.19 (Internal network)

  • Attack Host: Kali Linux (Local machine)

Problem Encountered: Port Conflict

Issue: Metasploit handler failed to bind to port 8000

Root Cause Analysis:

Solution Applied

Step 1: Port Resolution

Step 2: Updated SSH Command

Lab Execution Results

Network Discovery Verification:

Successful Connection Chain:

  1. βœ… SSH Dynamic Forward: ssh -D 9050 ubuntu@10.129.202.64

  2. βœ… RDP via Proxychains: proxychains xfreerdp /v:172.16.5.19 /u:victor /p:pass@123

  3. βœ… Payload Download: Windows PowerShell as Administrator

  4. βœ… SSH Remote Forward: Port 8080β†’8001 tunnel established

  5. βœ… Payload Execution: C:\backupScript.exe

Final Success Output:

Key Learning Points

  1. Port Conflicts: Always check for existing processes on target ports

  2. Flexible Port Usage: Using alternative ports (8001) works seamlessly

  3. Process Management: Kill old SSH tunnels before starting new ones

  4. Verification Steps: Confirm each tunnel component before proceeding

  5. Documentation: Real-time troubleshooting improves understanding

Troubleshooting Commands Used

Lab Questions - Verified Answers

Q1: "Which IP address assigned to the Ubuntu server Pivot host allows communication with the Windows server target?" Answer: 172.16.5.129 βœ… (Confirmed via ifconfig on pivot)

Q2: "What IP address is used on the attack host to ensure the handler is listening on all IP addresses?" Answer: 0.0.0.0 βœ… (Used in set LHOST 0.0.0.0)

Success Metrics

🎯 100% Lab Completion - All objectives achieved πŸ”§ Troubleshooting Applied - Port conflict resolved πŸ“š Theory to Practice - SSH remote forwarding mastered ⚑ Real Meterpreter Session - Full Windows target compromise

Lab Completion Time: ~45 minutes (including troubleshooting) Total Attempts: 2 (first failed due to port conflict) Final Result: βœ… SUCCESSFUL - Full remote access achieved


References

  • HTB Academy: Pivoting, Tunneling & Port Forwarding - Page 4

  • SSH Manual: man ssh (Remote port forwarding)

  • Metasploit: use exploit/multi/handler

Last updated