SECURITY ENGINEERING · PART 2 · NETWORKING FOR SECURITY · SCANNING · 26 OF 229

Nmap Tutorial: Port Scanning and Service Detection

How a port scan works and how to run one on your own lab: open, closed and filtered ports, host discovery, the SYN scan and the connect scan, service and version detection, the scripting engine, reading the output, what a scan looks like in the target logs, and how defenders use the same tool to find what they expose before an attacker does.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 9
  9. 10
  10. 11
  11. 12
  12. 13
  13. 14
  14. 15
  15. 16
  16. 17
  17. 18
  18. 19
  19. 20
  20. 21
  21. 22
  22. 23
  23. 24
  24. 25
  25. 26
  26. 27
  27. 28
  28. 29
  29. 30
  30. 31
  31. 32
  32. 33
  33. 34
  34. 35
  35. 36
  36. 37
  37. 38
  38. 39
  39. 40
  40. 41
  41. 42
  42. 43
  43. 44
  44. 45
  45. 46
  46. 47
  47. 48
  48. 49
  49. 50
  50. 51
  51. 52
  52. 53
  53. 54
  54. 55
  55. 56
  56. 57
  57. 58
  58. 59
  59. 60
  60. 61
  61. 62
  62. 63
  63. 64
  64. 106
  65. 107
  66. 108
  67. 109
  68. 110
  69. 111
  70. 112
  71. 113
  72. 114
  73. 115
  74. 116
  75. 117
  76. 118
  77. 119
  78. 120
  79. 121
  80. 122
  81. 123
  82. 124
  83. 125
  84. 126
  85. 127
  86. 128
  87. 129
  88. 130
  89. 131
  90. 132
  91. 133
  92. 134
  93. 135
  94. 136
  95. 137
  96. 138
  97. 139
  98. 140
  99. 141
  100. 142
  101. 143
  102. 144
1 / 13

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Open, closed, filtered
  • SYN and connect scans
  • versions are the finding
  • scan yourself and diff
  • permission first

Notes

Ports

Every machine has sixty five thousand TCP ports and as many UDP ports. A service listens on one: SSH on twenty two, web servers on eighty and 443, databases like MySQL and Postgres on their own well-known numbers. Each listening port is a door into the machine and therefore attack surface. The question a scan answers is simple: which doors does this machine have, and what is behind each one?

What a port is
  • 65,535 TCP ports and the same number of UDP ports per host
  • 22 SSH, 80 HTTP, 443 HTTPS, 3306 MySQL, 5432 Postgres, 6379 Redis, 3389 RDP
  • a service "listens" on a port
  • every listening port is attack surface

Three answers

Nmap sends a probe to a port and reads the reply. If a SYN-ACK comes back, the port is open: something is listening. If a reset comes back, the port is closed: the host is there but nothing listens. If nothing comes back at all, the port is filtered: a firewall silently dropped the probe. From the outside, a well-configured server shows a few open ports that are meant to be public, and everything else filtered.

Port states
  • open: a SYN-ACK comes back; a service is listening
  • closed: a RST comes back; the host is there, nothing listens
  • filtered: no reply; a firewall dropped the probe
  • filtered is what you want outsiders to see

Two scan types

There are two main scan types. The SYN scan sends a SYN, reads the answer, and never completes the handshake, which makes it fast and a little quieter, because many services only log completed connections. It needs root. The connect scan completes the full handshake through the operating system and needs no special rights, but it is slower and shows up in the service's own logs. Both find the same open ports.

SYN scan vs connect scan
# SYN scan (needs root)
sudo nmap -sS 10.10.10.5
  sends SYN, reads the reply,
  never completes the handshake

# connect scan (no root)
nmap -sT 10.10.10.5
  full handshake; the service
  logs a connection

What is behind it

Knowing a port is open is only the start. With version detection, Nmap talks to each open port and identifies the software and its version. That is the real finding, because a version can be matched against known vulnerabilities. In this example from a lab machine, a database port is open next to the web server. If this were the public face of a real server, that open database port would be the first thing to fix.

Service and version detection
$ nmap -sV -p 22,80,5432 10.10.10.5
PORT     STATE SERVICE  VERSION
22/tcp   open  ssh      OpenSSH 9.6
80/tcp   open  http     nginx 1.24
5432/tcp open  postgres PostgreSQL

(example output from a lab VM)

Useful options

A handful of options covers most real work. Minus s n finds which hosts are up without scanning ports. Minus p minus scans all ports, not just the common thousand. Minus s V detects versions. Minus s C runs the default set of safe scripts. Minus O guesses the operating system. Minus s U scans UDP, which is slow, so limit it to the top ports. And always save the output, because a scan you cannot show is a scan you did not do.

Nmap, the short list
nmap -sn 10.10.10.0/24   who is up
nmap -p- host         all 65535
nmap -sV host         versions
nmap -sC host         safe scripts
nmap -O host          guess the OS
nmap -sU --top-ports 20 host   UDP
nmap -oA scan host    save output
nmap -A host    -sV -sC -O + trace

The scripting engine

Nmap also has a scripting engine with hundreds of scripts, grouped by category. The safe ones report useful facts: which TLS versions and ciphers a server accepts, the titles and security headers of web pages, the algorithms an SSH server offers. Others test for specific vulnerabilities or try passwords. Stay with the safe and default categories unless your scope explicitly allows more. Intrusive scripts can crash fragile systems, and that is your responsibility.

NSE scripts
  • hundreds of scripts, grouped: default, safe, discovery, vuln, auth, brute, intrusive
  • safe examples: TLS versions and ciphers, HTTP titles and headers, SSH algorithms
  • stay in "safe" and "default" unless the scope says more intrusive ones can crash fragile systems

What the target sees

Now the defender's view. A scan is one source touching many ports in seconds. Firewall logs show a burst of dropped connection attempts, an intrusion detection system raises a port scan alert, and connect scans leave entries in each service's own log. Attackers slow their scans down to hide, but the pattern of one source walking through ports remains. Internet-facing servers are scanned constantly by strangers; what matters is what those scans find.

A scan in the logs
  • one source, many ports, in seconds
  • firewall logs: a burst of dropped SYNs
  • IDS: a "port scan" alert
  • connect scans: the service logs each connection
  • slower scans hide better, but the pattern remains

The defender's scan

The best use of Nmap is on yourself. Scan your own address ranges from the outside, as the internet sees you, and from the inside, as an intruder would. Do it on a schedule and compare each scan with the last; a tool called ndiff shows exactly what changed. Every open port should have an owner and a reason. Close anything that has neither. A new open port that nobody can explain is either a mistake or an incident.

Using Nmap for defense
  • scan your own ranges from outside and from inside
  • compare with the last scan: what is new?
  • ndiff shows the difference
  • every open port needs an owner and a reason
  • close what has neither
  • this is attack surface management

Rules

The rules, because this is the first tool in the track that touches other machines. Scan only your own lab, systems you own, or targets inside a written scope. In the cloud, check the provider's testing policy. At work, tell the network and security operations teams before you scan, or you will become their incident. Go gently on old or fragile devices such as printers and industrial equipment. And keep the scope document where you can find it.

Stay legal
  • scan only: your lab, your own systems, or a written scope
  • cloud: check the provider's testing policy
  • at work: tell the network and SOC teams first
  • be gentle on old or fragile devices
  • keep the scope document

Common mistakes

Mistakes. Scanning without permission. Checking only the default thousand ports, when services hide on high ones. Forgetting UDP entirely. Trusting a single scan from a single location, when a firewall may show a different face to different sources. Running intrusive scripts against production. Saving no output, so you cannot compare next month. And the most common: producing a neat list of open ports, and closing none of them.

Avoid these
  • scanning without permission
  • only the top 1000 ports
  • forgetting UDP
  • trusting one scan from one place
  • intrusive scripts on production
  • no saved output, no diff
  • finding ports, fixing none

Say it like this

In the interview: a port scan sends probes and reads the replies. A SYN-ACK means open, a reset means closed, and silence means filtered by a firewall. A SYN scan never completes the handshake; a connect scan does. Defensively, I scan my own ranges from outside and inside on a schedule with version detection, compare each result with the previous one, and make sure every open port has an owner and a reason, closing the rest.

In the interview
  • "Nmap probes ports and reads
  • the reply: SYN-ACK open, RST
  • closed, silence filtered. A SYN
  • scan never completes the handshake. I scan my own
  • ranges on a schedule with version detection, diff the results, and make sure every open port has an owner."

Interview questions

Q1What is a port scan?

Probing a host’s TCP or UDP ports to learn which are open, closed or filtered, and usually which service and version is behind each open one. Attackers use it for reconnaissance; defenders use it to measure their own attack surface.

Q2What do open, closed and filtered mean in Nmap?

Open: a service responded (SYN-ACK). Closed: the host replied with a reset, so it is reachable but nothing listens. Filtered: no reply or an ICMP error, meaning a firewall dropped the probe and Nmap cannot tell whether a service is there.

Q3What is the difference between a SYN scan and a connect scan?

A SYN scan (-sS) sends a SYN and reads the reply without completing the handshake; it is fast, needs raw-packet privileges and is logged by fewer services. A connect scan (-sT) completes the full handshake through the OS, needs no privileges and is logged by the service.

Q4Why is version detection important?

An open port only shows that something listens. Identifying the software and version (-sV) lets you match it against known vulnerabilities and spot services that should not be exposed, such as a database open to the internet.

Q5Why is UDP scanning harder than TCP scanning?

UDP has no handshake, so an open port often sends no reply at all and closed ports reply with rate-limited ICMP errors. Scans are slow and ambiguous, so they are usually limited to the most common UDP ports with protocol-specific probes.

Q6How would you detect a port scan?

By one source contacting many ports or hosts in a short time: bursts of dropped SYNs in firewall logs, IDS port-scan signatures, and, for connect scans, many short connections in service logs. Slow scans need longer time windows to detect.

Q7How do defenders use Nmap?

They scan their own external and internal ranges on a schedule, save the results, and diff them (for example with ndiff) to find new or unexpected open ports. Each open port should have an owner and a justification; the rest are closed.

Q8Is port scanning legal?

It depends on jurisdiction and authorisation, and scanning systems without permission can breach computer misuse laws and terms of service. Scan only systems you own or that are covered by a written scope, and coordinate with network and SOC teams at work.