π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/23networkNo 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:
Windows connects to Pivot:8080
SSH tunnel forwards to Attack Host:8000
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
Encrypt payloads when transferring
Use HTTPS for meterpreter connections
Clean up payloads after use
Monitor for AV detection
Tunnel Security
Use key authentication for SSH
Limit forwarding ports to necessary only
Monitor tunnel connections for anomalies
Clean up tunnels after assessment
Operational Security
Mimic legitimate traffic patterns
Use standard ports when possible
Avoid suspicious executable names
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
Plan payload configuration carefully (pivot internal IP)
Use appropriate ports for forwarding
Test connectivity at each step
Monitor tunnel status during operations
Clean up all artifacts after assessment
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:
β SSH Dynamic Forward:
ssh -D 9050 ubuntu@10.129.202.64β RDP via Proxychains:
proxychains xfreerdp /v:172.16.5.19 /u:victor /p:pass@123β Payload Download: Windows PowerShell as Administrator
β SSH Remote Forward: Port 8080β8001 tunnel established
β Payload Execution:
C:\backupScript.exe
Final Success Output:
Key Learning Points
Port Conflicts: Always check for existing processes on target ports
Flexible Port Usage: Using alternative ports (8001) works seamlessly
Process Management: Kill old SSH tunnels before starting new ones
Verification Steps: Confirm each tunnel component before proceeding
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
ired.team: SSH Tunnelling / Port Forwarding
SSH Manual:
man ssh(Remote port forwarding)Metasploit:
use exploit/multi/handler
Last updated