Nmap for Pentesting: Port Scanning, Network Recon and NSE

Nmap for penetration testing and network recon: commands, host discovery, TCP/UDP port scanning, service detection, OS fingerprinting, NSE and enumeration.
- Posted on 2026-02-20
- Reading time: 12 min
Nmap: Port Scanning, Enumeration, and NSE #
Nmap is a leading open-source pentesting tool for network discovery, port scanning, and security auditing . It is used to identify reachable hosts, enumerate exposed ports, services, software versions, operating systems, and additional information using the Nmap Scripting Engine (NSE).
In penetration testing, however, Nmap shouldn't be considered simply a port scanner. Its value lies primarily in its ability to transform a scan of an unknown network surface into a sequence of useful information for subsequent phases:
Target
↓
Host Discovery
↓
Port Scanning
↓
Service / Version Detection
↓
OS Detection
↓
NSE
↓
Interpretazione
↓
Enumeration mirata
↓
ValidationThis guide is a practical Nmap tutorial that covers installation, host discovery, TCP and UDP scanning, service detection, OS fingerprinting, NSE, per-service enumeration, output, performance, troubleshooting, evasion techniques, and automation with myNmap . A typical Nmap scan begins with host discovery and continues with port scanning, service detection, OS fingerprinting, and NSE, in the order shown above.
Only run scans on systems, networks, and infrastructure for which you have authorization.
Essential Nmap Commands: Which One to Use and When #
| Command | What does he do | When to use it |
|---|---|---|
nmap target | Basic scan on the most common ports | First quick reconnaissance |
nmap -p- target | All 65535 TCP ports | Full enumeration, don't miss services on non-standard ports |
nmap -sV target | Detect service and version | After finding open doors, before choosing the next tool |
nmap -sC target | NSE script from the default category | Automatic initial enumeration |
nmap -sS target | SYN scan (requires privileges) | Standard TCP scan with root/sudo available |
nmap -sT target | Connect scan | Unprivileged raw-packet |
nmap -sU target | UDP scan | UDP services (DNS, SNMP, NTP…) |
nmap -Pn target | Skip host discovery | The host appears to be down but I suspect ICMP is being filtered. |
nmap -O target | OS fingerprinting | Need to estimate remote operating system |
nmap -A target | OS + version + NSE default + traceroute | Deep enumeration in a single command |
nmap -oA scan target | Save in all major formats | Always, when the results need to be reused or reported |
What is Nmap and what is it used for? #
Nmap can be used for several phases of reconnaissance:
| Function | Target |
|---|---|
| Host Discovery | Identify reachable hosts |
| port scanning | Locate accessible TCP and UDP ports |
| Service Detection | Identify the service present on a port |
| Version Detection | Determine product and version when possible |
| OS Detection | Estimate the remote operating system |
| NSE | Automate discovery, enumeration and security checks |
| output | Save and process the results |
| Timing | Check scan speed and load |
| Scan Evasion | Modify probe characteristics in specific scenarios |
An Nmap result isn't necessarily the final finding. It's often the starting point of a workflow—the Main Ports section below is the complete port-to-next-tool map.
Nmap Installation and Setup #
The installed version of Nmap can be checked with nmap --versionTo download the current release, it is recommended to use the official download page, avoiding hardcoding a specific version in the guide content.
Nmap on Linux — Debian and Ubuntu #
sudo apt update
sudo apt install nmap -y
nmap --versionThe version in your distribution's repository may be different from the most recent version released by the Nmap project.
Nmap on Kali Linux #
nmap --version
sudo apt update && sudo apt install --only-upgrade nmapNmap on Windows #
Nmap has an official installer for Windows and uses Npcap for several packet capture and packet manipulation features.
nmap --version
nmap -sV 192.168.1.100Nmap on macOS #
nmap --versionCompiling from source #
tar xvf nmap-<version>.tar.bz2
cd nmap-<version>
./configure
make
sudo make installRelease tarballs: nmap.org/download.html
Necessary privileges #
Techniques that send and receive raw packets, such as SYN scan, typically require appropriate privileges on Linux and Unix. TCP connect scan, however, can be performed without equivalent raw-packet privileges.
sudo nmap -sS target # richiede privilegi
nmap -sT target # alternativa senza raw packetHow an Nmap scan works #
1. Host Discovery
2. Port Scanning
3. Service Detection
4. Version Detection
5. OS Detection
6. NSE
7. Interpretation
8. Targeted EnumerationThe sequence can be shortened or extended depending on the goal — each step answers a specific question: which hosts are reachable, which ports respond, which service uses them, which version, which OS, what NSE adds, and finally which specific tool to use next.
Nmap Syntax #
nmap [options] targetnmap -sV -p 22,80,443 192.168.1.100-sV enable service/version detection, -p select the doors, 192.168.1.100 is the target.
Basic Nmap parameters #
| Option | Function | Example |
|---|---|---|
-sS | TCP SYN scan | sudo nmap -sS target |
-sT | TCP connect scan | nmap -sT target |
-sU | UDP scan | sudo nmap -sU target |
-sV | Service and version detection | nmap -sV target |
-O | OS detection | sudo nmap -O target |
-p | Specify the ports | nmap -p 22,80,443 target |
-p- | Scan TCP ports 1–65535 | nmap -p- target |
-sC | Runs NSE scripts of the category default | nmap -sC target |
-A | OS detection, version detection, default NSE and traceroute | sudo nmap -A target |
-Pn | Skip host discovery | nmap -Pn target |
-n | Disable DNS resolution | nmap -n target |
-sn | Host discovery without port scan | nmap -sn target |
-sL | List targets without performing port scan | nmap -sL 192.168.1.0/24 |
-T0 -T5 | timing template | nmap -T4 target |
-oA | Save major output formats | nmap -oA scan target |
Basic Nmap Usage #
nmap 192.168.1.100 # singolo host
nmap 192.168.1.1-50 # range
nmap 192.168.1.0/24 # subnet
nmap -iL targets.txt # da file
nmap -sL 192.168.1.0/24 # elenca senza scansionarePORT STATE SERVICE
22/tcp open ssh
80/tcp open http
443/tcp open https-sL It is useful when you want to check which addresses belong to a range and how the names are resolved, without performing the normal port scan.
Host Discovery #
nmap -sn 192.168.1.0/24 # ping scan
sudo nmap -sn -PR 192.168.1.0/24 # ARP discovery, efficace in LAN
sudo nmap -sn -PE -PS443 192.168.1.0/24 # probe combinate ICMP+TCP-Pn: Skip Host Discovery
#
nmap -Pn targetNmap skips the host discovery phase and treats the target as active. -Pn This isn't a universal firewall bypass; it means Nmap doesn't rely on the previous discovery phase to decide whether to continue scanning. This is useful when ICMP or other discovery probes are filtered, but the target still exposes reachable services.
nmap -n target # disabilita risoluzione DNS, riduce traffico e accelera alcuni workflowNmap Port Scanning: Port Scanning #
sudo nmap -sS target # SYN scan, richiede privilegi raw-packet
nmap -sT target # connect scan, senza privilegi
sudo nmap -sU target # UDP, più lento e con semantica di risposta diversa
nmap -p 22,80,443 target # porte specifiche
nmap -p 1-1024 target # range
nmap -p- target # tutte le porte TCP
nmap --top-ports 100 target
nmap -F targetAn effective workflow is to separate port discovery from subsequent enumeration:
nmap -p- target
nmap -sC -sV -p 22,80,443,445 targetOther TCP Scan Types: ACK, FIN, Null, Xmas #
In addition to SYN/Connect/UDP, Nmap supports less commonly used but useful scans in specific scenarios:
sudo nmap -sA target # ACK scan
sudo nmap -sF target # FIN scan
sudo nmap -sN target # Null scan (nessun flag)
sudo nmap -sX target # Xmas scan (FIN+PSH+URG)-sA It does not distinguish between open and closed: it only says whether a door is open or closed. unfiltered o filtered, useful for mapping stateless firewall rules without determining which ports are actually listening.
-sF, -sN e -sX They exploit the behavior provided by RFC 793 (closed port responds to RST, open port does not respond) to pass unnoticed by some stateless filters. They are not reliable against Windows and many other modern TCP stacks, which do not follow that behavior and show all ports as closed regardless of their actual state — useful mostly against Unix-like targets with simple firewalls, not as a primary technique.
Nmap Port States #
The states recognized by Nmap describe how the port scanner sees a port from the current vantage point and scan type used , not an absolute property of the port itself.
| Status | Meaning |
|---|---|
open | An application is accepting TCP connections, UDP datagrams, or SCTP associations |
closed | The port is reachable but no application is listening |
filtered | A filter prevents Nmap from determining whether the port is open or closed. |
unfiltered | The port responds to probes, but Nmap cannot determine whether it is open or closed with that scan type. |
open|filtered | Nmap can't distinguish between open and filtered |
closed|filtered | Nmap cannot distinguish between closed and filtered under certain conditions |
A door open This does not automatically mean “vulnerable.” It means that Nmap has observed a reachable service and that there is a surface to identify and possibly enumerate.
Nmap Service and Version Detection #
nmap -sV targetPORT STATE SERVICE VERSION
22/tcp open ssh OpenSSH 9.x
80/tcp open http Apache httpd
443/tcp open https nginx
445/tcp open microsoft-dsPort → Protocol → Service → Product → Versionnmap -sV --version-intensity 9 target--version-intensity accepts values from 0 to 9; the default is -sV is 7. Higher values try more probes and may identify services on non-standard ports or unusual configurations, but they increase scan time — this is not an automatic guarantee of a better result in every environment.
A result like Apache httpd 2.4.x This does not automatically mean that Apache is vulnerable: it is an element of reconnaissance that must be correlated with vendor documentation and advisories, then validated.
Nmap OS Detection and OS Fingerprinting #
sudo nmap -O target
sudo nmap -O -sV target # combinazione comuneAccuracy depends on the responses received, the quality of the fingerprint, and the presence of firewalls or middleboxes that alter the probes.
Aggressive Scan #
sudo nmap -A target-A Enable OS detection, version detection, default NSE scripts, and traceroute together. This isn't simply a "more powerful" mode: when you want to precisely control what gets executed, it's often better to explicitly choose the features with nmap -sC -sV target.
How to read Nmap output #
PORT STATE SERVICE VERSION
22/tcp open ssh OpenSSH 9.x
80/tcp open http Apache httpd
443/tcp open https nginx
445/tcp open microsoft-ds22 → SSH enumeration
80 → HTTP enumeration
443 → HTTPS enumeration
445 → SMB enumerationMain Doors: What to Do Next #
This is the door-to-next map tool I use myself during an assessment. It's not exhaustive, but it covers most real-world cases:
| Door(s) | Service | Initial Nmap command | Next step |
|---|---|---|---|
| 21 | FTP | nmap -p21 -sV --script ftp-anon target | FTP Port 21 |
| 22 | SSH | nmap -p22 -sV --script ssh-auth-methods target | SSH |
| 25 | SMTP | nmap -p25 -sV --script smtp-commands target | SMTP Port 25 |
| 53 | DNS | nmap -p53 --script dns-zone-transfer target | DNS |
| 80/443 | HTTP / HTTPS | nmap -p80,443 -sV --script http-title,http-headers target | Burp Suite , fuf , Gobuster |
| 88 | Kerberos | nmap -p88 -sV target | Kerberos , Kerber-roasting , AS-REP Roasting |
| 135/445 | PRC/SMB | nmap -p135,445 --script smb-enum-shares target | SMB , smbclient , rpcclient , enum4linux-ng , NetExec |
| 389/636 | LDAP/LDAPS | nmap -p389,636 --script ldap-rootdse target | LDAP Port 389 , ldapsearch |
| 1433 | MSSQL | nmap -p1433 --script ms-sql-info target | Port 1433 MSSQL |
| 3306 | MySQL | nmap -p3306 --script mysql-info target | MySQL Port 3306 |
| 3389 | RDP | nmap -p3389 -sV target | Port 3389 RDP |
| 5985/5986 | WinRM | nmap -p5985 -sV target | Port 5985 WinRM , Evil-WinRM |
Nmap NSE: The Scripting Engine #
The Nmap Scripting Engine (NSE) extends Nmap via Lua scripts to automate discovery, enumeration, service/version detection, vulnerability detection, and, in some cases, exploitation.
ls /usr/share/nmap/scripts/ # script installati localmente
sudo nmap --script-updatedb # aggiorna il database locale
ls /usr/share/nmap/scripts/ | grep -i smb
nmap --script-help smb-enum-sharesnmap -sC target # script della categoria default
nmap -sC -sV target # combinazione comuneNSE Categories #
| Categories | Function |
|---|---|
auth | Authentication-related activities |
broadcast | Discovery via broadcast |
brute | Credential guessing |
default | Scripts included in the default category |
discovery | Information gathering |
dos | Denial of service test |
exploit | Exploitation script |
external | Interaction with external services |
fuzzer | Fuzzing |
info | Additional information |
intrusive | Potentially impactful scripts |
malware | Malware detection |
safe | Scripts classified as generally safe |
version | Version detection |
vuln | Vulnerability detection |
NSE scripts are not sandboxed. Before running a broad category or unknown script, it's important to understand its behavior and potential impact.
Vulnerability checks with NSE #
nmap --script vuln targetThis is not the same as a full vulnerability management platform: Nmap can perform targeted security checks via NSE, but dedicated tools provide inventory, correlation, and management on a larger scale.
SMB Enumeration #
nmap -p 445 --script smb-enum-shares target
nmap -p 445 --script smb-enum-users target
nmap -p 445 --script smb-enum-sessions target
nmap -p 445 --script "smb-vuln*" target445/tcp → SMB detection → NSE enumeration → strumenti dedicatiNmap provides discovery and early enumeration; for more in-depth analysis, switch to rpcclient , smbclient , enum4linux-ng , or NetExec — see the Major Ports table above for the full picture.
LDAP and Active Directory Enumeration #
nmap -p 389,636 --script ldap-rootdse target
nmap -p 389 --script ldap-search target389/636 → LDAP/LDAPS detection → RootDSE → Naming Context → enumerazioneNmap helps with initial discovery; for deeper enumeration, it turns to ldapsearch . Tasks like Kerberoasting and AS-REP Roasting then require dedicated tools, not Nmap itself.
Web Server Enumeration #
nmap -p 80,443,8080,8443 --open -sV target
nmap -p 80,443 --script http-title,http-headers,http-methods target
nmap -p 80,443 --script http-enum target
nmap -p 80,443 --script http-waf-detect,http-waf-fingerprint target80/443/8080/8443 → service detection → HTTP NSE → technology identification → enumerazione mirataAfter the initial fingerprinting, switch to Burp Suite , ffuf , or Gobuster for the actual web enumeration.
SSH Enumeration #
nmap -p 22 -sV target
nmap -p 22 --script ssh-auth-methods targetFor detailed SSH enumeration and hardening see the dedicated article on SSH.
DNS Enumeration #
nmap -p 53 --script dns-service-discovery target
nmap -p 53 --script dns-zone-transfer --script-args dns-zone-transfer.domain=example.com ns.example.comFor complete DNS reconnaissance see the DNS article.
Database Discovery #
nmap -p 3306 --script mysql-info target # MySQL
nmap -p 1433 --script ms-sql-info target # MSSQL
nmap -p 5432 -sV target # PostgreSQL, poi script dedicatils /usr/share/nmap/scripts/ | grep -Ei 'postgres|pgsql'
nmap --script-help <script>For targeted attack on individual DBMSs see MySQL Port 3306 and MSSQL Port 1433.
IoT and OT Discovery #
nmap -p 502 --script modbus-discover target # Modbus
nmap -p 102 --script s7-info target # Siemens S7
nmap -p 1883,8883 --open target # MQTTIn OT/ICS environments, always consider the possible operational impact: appropriate techniques in an IT network can have different consequences on industrial systems and embedded devices.
Firewall, IDS and Scan Evasion #
Evasion options modify traffic, packet structure, or probe timing. They do not provide universal firewall/IDS/IPS bypasses and do not guarantee invisibility.
sudo nmap -f target # fragmentation
sudo nmap -f -f target # doppia frammentazione
sudo nmap --mtu 24 target # MTU custom
sudo nmap -D RND:5 target # decoy random
sudo nmap -D 192.168.1.50,ME,192.168.1.52 target # decoy specifici
sudo nmap --source-port 53 target # source port spoofing
nmap --badsum target # checksum errato (studio comportamento firewall)
nmap --data-length 25 target # dati random aggiuntiviFor a defensive perspective on how to configure a firewall to resist these techniques, see corelab.tech's guide on OPNsense and security through obscurity.
Timing and Throttling #
| template | Profile |
|---|---|
T0 | Very slow |
T1 | Slow |
T2 | Reduced impact |
T3 | Default |
T4 | Most aggressive |
T5 | Very aggressive |
nmap -T4 target
nmap --scan-delay 5s target
nmap --max-rate 100 target
nmap --min-rate 50 targetMore aggressive timing doesn't automatically mean better quality: excessively increasing speed can cause packet loss, timeouts, and increased visibility in monitoring systems. Values should be tailored to the network and the type of engagement, not applied as universal defaults.
Idle Scan #
nmap --script ipidseq 192.168.1.0/24 # cerca host zombie candidati
nmap -sI zombie_ip:80 target_ipIdle Scan is not a “completely anonymous scan”: it can separate the apparent origin of the probes from the target in certain scenarios, but it does not guarantee complete anonymity.
Nmap and Detection #
A scan can be detected by firewalls, IDS/IPS, NDRs, EDRs with network visibility, service logs, and network telemetry. Timing, decoy, and fragmentation can modify the traffic profile, but there is no Nmap option that guarantees "zero detection."
Nmap Output and Reporting #
nmap -sV target -oN scan.nmap # normal output
nmap -sV target -oX scan.xml # XML, ideale per automazione/parsing
nmap -sV target -oG scan.gnmap # grepable, comodo per grep/awk veloci
nmap -sV target -oA scan # tutti e tre insiemeWhere Does Nmap Save Results? #
Nmap does not automatically save each scan to a file. You must specify this explicitly. -oN, -oX, -oG o -oA:
nmap -sV target -oA ./results/web_scanResume an Interrupted Scan #
nmap --resume scan.nmapNmap Workflow for a Penetration Test #
External Reconnaissance #
nmap -F target
nmap -p- target
nmap -sC -sV -p <porte> targetInternal Network Recon #
sudo nmap -sn 10.0.0.0/24 -oG hosts.gnmap
nmap -p- -iL targets.txt -oA tcp_scanActive Directory Recon #
nmap -p 53,88,135,139,389,445,464,636,3268,3269 --open 10.0.0.0/24
nmap -p 389 --script ldap-rootdse 10.0.0.0/24A combination of these ports suggests the presence of a Domain Controller — use the Primary Ports table for the correct tool on each.
Web Reconnaissance #
Network discovery → 80/443/8080/8443 → service detection → HTTP NSE → enumerazione webPerformance and Massive Scanning #
nmap -sn 10.0.0.0/16 -oG hosts.gnmap
grep "Up" hosts.gnmap | awk '{print $2}' > targets.txt
nmap --top-ports 1000 -iL targets.txt -oA service_scan
nmap -sC -sV -p <porte> -iL priority_targets.txt -oA detailed_scanSeparating discovery and enumeration reduces repetitive work and allocates more expensive scans only to truly interesting hosts.
Nmap and myNmap #
During CTFs, labs, and authorized penetration tests, the full scan → service detection → OS detection → NSE → UDP → vulnerability check sequence becomes repetitive. myNmap is Hackita's open source wrapper that automates this workflow while keeping Nmap as the engine:
git clone https://github.com/hack-ita/mynmap.git
cd mynmap
chmod +x mynmap
sudo cp mynmap /usr/local/bin/mynmapsudo mynmap 10.10.10.10 # con privilegi: usa SYN scan (-sS)
mynmap 10.10.10.10 # senza sudo: usa connect scan (-sT)In a single run it covers full TCP discovery, service/version detection with default NSE scripts, OS detection, vulnerability checks on the most common critical ports, and a targeted UDP scan on the most relevant ports before generic ones — designed for the speed/coverage tradeoff of CTFs, HTBs, and Proving Grounds.
| Approach | Control | Automation | When |
|---|---|---|---|
| Manual Nmap | Massimo | Bassa | Fully customized scan |
| Script + Nmap | High | Media | Repeatable, in-house workflows |
| myNmap | High | High | First quick overview target→ports→services→NSE |
Repository: github.com/hack-ita/mynmap
Troubleshooting #
nmap -Pn target # host down ma attivo -> ICMP filtrato
nmap -PS22,80,443 target # probe su porte note
nmap -sT target # SYN scan non disponibile senza privilegi
sudo nmap -sU -p 53,67,68,123,161 target # UDP lento: parti dalle porte rilevanti
sudo nmap -O -sV target # OS detection poco precisaDoors filtered — does not mean the service is shut down, it means that Nmap has not received enough information to determine its status with certainty.
Operation not permitted — the technique requires raw-packet privileges: use sudo or switch to -sT.
FAQ #
What is Nmap? An open source network discovery and security auditing tool: it identifies hosts, ports, services, software versions, operating systems, and additional information via NSE.
What is Nmap for? Host discovery, port scanning, service/version detection, OS fingerprinting, enumeration and vulnerability detection via NSE.
How to scan all ports with Nmap?
nmap -p- target, then nmap -sC -sV -p <porte> target on the doors found.
What is the difference between -sS e -sT?
-sS sends SYN and normally does not complete the three-way handshake; -sT use the connect() system and completes the connection. The difference isn't just a matter of "stealth vs. non-stealth": traffic remains observable via logs and monitoring.
Can I use Nmap without being detected? No. Timing, decoy, and fragmentation modify the traffic generated, but they don't guarantee invisibility.
Can Nmap do vulnerability scanning?
Yes with nmap --script vuln target, but it is not equivalent to a complete vulnerability management platform.
Does Nmap work on Windows? Yes, with the official installer and Npcap for networking.
Where does Nmap save its results?
It does not save them automatically: you need to specify -oN, -oX, -oG o -oA.
Which format is best for automation?
XML (-oX), for dedicated pipelines and parsers.
What language is Nmap written in? The core engine is C/C++; the Nmap Scripting Engine (NSE) uses Lua for scripting; Zenmap, the original GUI, was written in Python.
Nmap Cheat Sheet #
nmap -sn 192.168.1.0/24 # discovery
nmap -Pn target # skip host discovery
nmap -sL 192.168.1.0/24 # lista target
sudo nmap -sS target # SYN scan
nmap -sT target # connect scan
sudo nmap -sU target # UDP
sudo nmap -sA target # ACK scan (mappa regole firewall)
sudo nmap -sF -sN -sX target # FIN / Null / Xmas (evasion su target Unix-like)
nmap -p- target # tutte le porte TCP
nmap -p 22,80,443 target # porte specifiche
nmap --top-ports 100 target
nmap -sV target # service detection
sudo nmap -O target # OS detection
nmap -sC target # default NSE
nmap -sC -sV target
sudo nmap -A target # aggressive
nmap --script vuln target
nmap -sV target -oA scan # tutti i formati
nmap -sV target -oX scan.xml
nmap --resume scan.nmap
nmap -T4 targetResources #
- Nmap Official: nmap.org
- Nmap Download: nmap.org/download.html
- Nmap Reference Guide: nmap.org/book/man.html
- NSE Documentation: nmap.org/nsedoc
- myNmap — Hackita: github.com/hack-ita/mynmap
Use Nmap only on systems and networks for which you have authorization. Scanning, enumeration, and evasion techniques may generate traffic, alerts, or impact the scanned systems.








