Easily Check Open Ports for Reverse Shells: A Must-Have for OSCP Students

Listen to this Post

Featured Image

Introduction

One of the most frustrating mistakes during the OSCP exam is attempting to establish a reverse shell through a closed port. This oversight can waste precious time and derail your progress. To help, Security Engineer Jose C. has shared a practical bash script that verifies open ports before attempting a reverse shell connection, ensuring a smoother penetration testing process.

Learning Objectives

  • Understand why checking open ports is critical for reverse shells.
  • Learn how to automate port verification using a bash script.
  • Apply this technique to avoid common OSCP exam pitfalls.

You Should Know

1. Verifying Open Ports with Bash

Command:

!/bin/bash 
target_ip="10.10.10.10" 
port=4444 
timeout 1 bash -c "</dev/tcp/$target_ip/$port" && echo "Port $port is open" || echo "Port $port is closed" 

Step-by-Step Guide:

1. Replace `target_ip` with the victim machine’s IP.

2. Set `port` to your desired reverse shell port (e.g., 4444).
3. The script checks if the port is open using a TCP connection attempt with a 1-second timeout.
4. Outputs whether the port is open or closed, saving you from failed reverse shell attempts.

2. Automating Port Checks for Multiple Ports

Command:

!/bin/bash 
target_ip="10.10.10.10" 
for port in {4444..4450}; do 
timeout 1 bash -c "</dev/tcp/$target_ip/$port" 2>/dev/null && echo "Port $port is open" || echo "Port $port is closed" 
done 

Step-by-Step Guide:

1. This script scans ports 4444 to 4450 on the target IP.
2. For each port, it attempts a TCP connection and suppresses error messages (2>/dev/null).
3. Outputs the status of each port, helping identify viable reverse shell candidates.

  1. Integrating with Netcat for Reverse Shell Testing

    Command:

    nc -lvnp 4444 & 
    timeout 1 bash -c "</dev/tcp/127.0.0.1/4444" && echo "Ready for reverse shell" || echo "Port unavailable" 
    

    Step-by-Step Guide:

    1. Start a Netcat listener in the background (&).
    2. The script checks if the port is open locally before proceeding.
    3. Confirms readiness for reverse shell connections, preventing silent failures.

4. Using Nmap for Pre-Exploitation Recon

Command:

nmap -p 4444-4450 --open 10.10.10.10 

Step-by-Step Guide:

1. Nmap scans the target IP for open ports in the specified range.
2. The `–open` flag filters results to show only open ports.
3. Use this to validate outbound connectivity before reverse shell attempts.

5. Windows Alternative: Test-NetConnection

Command (PowerShell):

Test-NetConnection -ComputerName 10.10.10.10 -Port 4444 

Step-by-Step Guide:

1. Run this PowerShell command on a Windows target.
2. Checks if the specified port is open and reachable.

3. Outputs detailed connectivity status, including firewall rules.

6. Exploiting Open Ports with Metasploit

Command:

msfconsole -x "use exploit/multi/handler; set PAYLOAD windows/x64/meterpreter/reverse_tcp; set LHOST 10.10.10.1; set LPORT 4444; exploit" 

Step-by-Step Guide:

1. Launches Metasploit and configures a handler for the reverse shell.
2. Ensure `LPORT` matches an open port verified earlier.
3. Execute the exploit to catch the shell upon successful victim connection.

7. Mitigating Closed Port Issues in Firewalls

Command (Linux):

iptables -A INPUT -p tcp --dport 4444 -j ACCEPT 

Step-by-Step Guide:

1. Adds a firewall rule to allow inbound traffic on port 4444.
2. Useful for lab environments where firewalls block reverse shells.
3. Verify with `iptables -L` to confirm the rule is active.

What Undercode Say

– Key Takeaway 1: Always verify open ports before reverse shell attempts—automate this step to save time.
– Key Takeaway 2: Combine tools like Nmap, Netcat, and custom scripts for reliable pre-exploitation checks.

Analysis:

Jose C.’s script addresses a critical gap in OSCP exam strategies. Many students overlook port verification, assuming outbound connectivity implies inbound availability. This script not only prevents wasted effort but also reinforces disciplined penetration testing habits. As attacks evolve, such small optimizations can mean the difference between success and failure in real-world engagements.

Prediction

Future penetration testing frameworks will likely integrate automated port verification by default, reducing human error. Meanwhile, mastering these manual techniques ensures adaptability in restricted or custom environments. Expect OSCP-like exams to increasingly emphasize such practical validations.

IT/Security Reporter URL:

Reported By: Activity 7341784667789959169 – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 Telegram