tools

Nmap for Pentesting: Port Scanning, Network Recon and NSE

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:

text
Target
  ↓
Host Discovery
  ↓
Port Scanning
  ↓
Service / Version Detection
  ↓
OS Detection
  ↓
NSE
  ↓
Interpretazione
  ↓
Enumeration mirata
  ↓
Validation

This 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 #

CommandWhat does he doWhen to use it
nmap targetBasic scan on the most common portsFirst quick reconnaissance
nmap -p- targetAll 65535 TCP portsFull enumeration, don't miss services on non-standard ports
nmap -sV targetDetect service and versionAfter finding open doors, before choosing the next tool
nmap -sC targetNSE script from the default categoryAutomatic initial enumeration
nmap -sS targetSYN scan (requires privileges)Standard TCP scan with root/sudo available
nmap -sT targetConnect scanUnprivileged raw-packet
nmap -sU targetUDP scanUDP services (DNS, SNMP, NTP…)
nmap -Pn targetSkip host discoveryThe host appears to be down but I suspect ICMP is being filtered.
nmap -O targetOS fingerprintingNeed to estimate remote operating system
nmap -A targetOS + version + NSE default + tracerouteDeep enumeration in a single command
nmap -oA scan targetSave in all major formatsAlways, 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:

FunctionTarget
Host DiscoveryIdentify reachable hosts
port scanningLocate accessible TCP and UDP ports
Service DetectionIdentify the service present on a port
Version DetectionDetermine product and version when possible
OS DetectionEstimate the remote operating system
NSEAutomate discovery, enumeration and security checks
outputSave and process the results
TimingCheck scan speed and load
Scan EvasionModify 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 #

bash
sudo apt update
sudo apt install nmap -y
nmap --version

The version in your distribution's repository may be different from the most recent version released by the Nmap project.

Nmap on Kali Linux #

bash
nmap --version
sudo apt update && sudo apt install --only-upgrade nmap

Nmap on Windows #

Nmap has an official installer for Windows and uses Npcap for several packet capture and packet manipulation features.

powershell
nmap --version
nmap -sV 192.168.1.100

Nmap on macOS #

bash
nmap --version

Compiling from source #

bash
tar xvf nmap-<version>.tar.bz2
cd nmap-<version>
./configure
make
sudo make install

Release 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.

bash
sudo nmap -sS target   # richiede privilegi
nmap -sT target        # alternativa senza raw packet

How an Nmap scan works #

text
1. Host Discovery
2. Port Scanning
3. Service Detection
4. Version Detection
5. OS Detection
6. NSE
7. Interpretation
8. Targeted Enumeration

The 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 #

bash
nmap [options] target
bash
nmap -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 #

OptionFunctionExample
-sSTCP SYN scansudo nmap -sS target
-sTTCP connect scannmap -sT target
-sUUDP scansudo nmap -sU target
-sVService and version detectionnmap -sV target
-OOS detectionsudo nmap -O target
-pSpecify the portsnmap -p 22,80,443 target
-p-Scan TCP ports 1–65535nmap -p- target
-sCRuns NSE scripts of the category defaultnmap -sC target
-AOS detection, version detection, default NSE and traceroutesudo nmap -A target
-PnSkip host discoverynmap -Pn target
-nDisable DNS resolutionnmap -n target
-snHost discovery without port scannmap -sn target
-sLList targets without performing port scannmap -sL 192.168.1.0/24
-T0 -T5timing templatenmap -T4 target
-oASave major output formatsnmap -oA scan target

Basic Nmap Usage #

bash
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 scansionare
text
PORT     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 #

bash
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 #

bash
nmap -Pn target

Nmap 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.

bash
nmap -n target   # disabilita risoluzione DNS, riduce traffico e accelera alcuni workflow

Nmap Port Scanning: Port Scanning #

bash
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 target

An effective workflow is to separate port discovery from subsequent enumeration:

bash
nmap -p- target
nmap -sC -sV -p 22,80,443,445 target

Other TCP Scan Types: ACK, FIN, Null, Xmas #

In addition to SYN/Connect/UDP, Nmap supports less commonly used but useful scans in specific scenarios:

bash
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.

StatusMeaning
openAn application is accepting TCP connections, UDP datagrams, or SCTP associations
closedThe port is reachable but no application is listening
filteredA filter prevents Nmap from determining whether the port is open or closed.
unfilteredThe port responds to probes, but Nmap cannot determine whether it is open or closed with that scan type.
open|filteredNmap can't distinguish between open and filtered
closed|filteredNmap 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 #

bash
nmap -sV target
text
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-ds
text
Port → Protocol → Service → Product → Version
bash
nmap -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 #

bash
sudo nmap -O target
sudo nmap -O -sV target   # combinazione comune

Accuracy depends on the responses received, the quality of the fingerprint, and the presence of firewalls or middleboxes that alter the probes.

Aggressive Scan #

bash
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 #

text
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-ds
text
22 → SSH enumeration
80 → HTTP enumeration
443 → HTTPS enumeration
445 → SMB enumeration

Main 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)ServiceInitial Nmap commandNext step
21FTPnmap -p21 -sV --script ftp-anon targetFTP Port 21
22SSHnmap -p22 -sV --script ssh-auth-methods targetSSH
25SMTPnmap -p25 -sV --script smtp-commands targetSMTP Port 25
53DNSnmap -p53 --script dns-zone-transfer targetDNS
80/443HTTP / HTTPSnmap -p80,443 -sV --script http-title,http-headers targetBurp Suite , fuf , Gobuster
88Kerberosnmap -p88 -sV targetKerberos , Kerber-roasting , AS-REP Roasting
135/445PRC/SMBnmap -p135,445 --script smb-enum-shares targetSMB , smbclient , rpcclient , enum4linux-ng , NetExec
389/636LDAP/LDAPSnmap -p389,636 --script ldap-rootdse targetLDAP Port 389 , ldapsearch
1433MSSQLnmap -p1433 --script ms-sql-info targetPort 1433 MSSQL
3306MySQLnmap -p3306 --script mysql-info targetMySQL Port 3306
3389RDPnmap -p3389 -sV targetPort 3389 RDP
5985/5986WinRMnmap -p5985 -sV targetPort 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.

bash
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-shares
bash
nmap -sC target          # script della categoria default
nmap -sC -sV target      # combinazione comune

NSE Categories #

Categories Function
authAuthentication-related activities
broadcastDiscovery via broadcast
bruteCredential guessing
defaultScripts included in the default category
discoveryInformation gathering
dosDenial of service test
exploitExploitation script
externalInteraction with external services
fuzzerFuzzing
infoAdditional information
intrusivePotentially impactful scripts
malwareMalware detection
safeScripts classified as generally safe
versionVersion detection
vulnVulnerability 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 #

bash
nmap --script vuln target

This 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 #

bash
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*" target
text
445/tcp → SMB detection → NSE enumeration → strumenti dedicati

Nmap 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 #

bash
nmap -p 389,636 --script ldap-rootdse target
nmap -p 389 --script ldap-search target
text
389/636 → LDAP/LDAPS detection → RootDSE → Naming Context → enumerazione

Nmap 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 #

bash
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 target
text
80/443/8080/8443 → service detection → HTTP NSE → technology identification → enumerazione mirata

After the initial fingerprinting, switch to Burp Suite , ffuf , or Gobuster for the actual web enumeration.

SSH Enumeration #

bash
nmap -p 22 -sV target
nmap -p 22 --script ssh-auth-methods target

For detailed SSH enumeration and hardening see the dedicated article on SSH.

DNS Enumeration #

bash
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.com

For complete DNS reconnaissance see the DNS article.

Database Discovery #

bash
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 dedicati
bash
ls /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 #

bash
nmap -p 502 --script modbus-discover target    # Modbus
nmap -p 102 --script s7-info target            # Siemens S7
nmap -p 1883,8883 --open target                # MQTT

In 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.

bash
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 aggiuntivi

For 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 #

templateProfile
T0Very slow
T1Slow
T2Reduced impact
T3Default
T4Most aggressive
T5Very aggressive
bash
nmap -T4 target
nmap --scan-delay 5s target
nmap --max-rate 100 target
nmap --min-rate 50 target

More 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 #

bash
nmap --script ipidseq 192.168.1.0/24   # cerca host zombie candidati
nmap -sI zombie_ip:80 target_ip

Idle 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 #

bash
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 insieme

Where Does Nmap Save Results? #

Nmap does not automatically save each scan to a file. You must specify this explicitly. -oN, -oX, -oG o -oA:

bash
nmap -sV target -oA ./results/web_scan

Resume an Interrupted Scan #

bash
nmap --resume scan.nmap

Nmap Workflow for a Penetration Test #

External Reconnaissance #

bash
nmap -F target
nmap -p- target
nmap -sC -sV -p <porte> target

Internal Network Recon #

bash
sudo nmap -sn 10.0.0.0/24 -oG hosts.gnmap
nmap -p- -iL targets.txt -oA tcp_scan

Active Directory Recon #

bash
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/24

A combination of these ports suggests the presence of a Domain Controller — use the Primary Ports table for the correct tool on each.

Web Reconnaissance #

text
Network discovery → 80/443/8080/8443 → service detection → HTTP NSE → enumerazione web

Performance and Massive Scanning #

bash
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_scan

Separating 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:

bash
git clone https://github.com/hack-ita/mynmap.git
cd mynmap
chmod +x mynmap
sudo cp mynmap /usr/local/bin/mynmap
bash
sudo 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.

ApproachControlAutomationWhen
Manual NmapMassimoBassaFully customized scan
Script + NmapHighMediaRepeatable, in-house workflows
myNmapHighHighFirst quick overview target→ports→services→NSE

Repository: github.com/hack-ita/mynmap

Troubleshooting #

bash
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 precisa

Doors 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 #

bash
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 target

Resources #

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.

#PortScanning #Nmap #Network Enumeration #NSE #NetworkReconnaissance

leave a message

I'm not a robot