CCyberwiki

Port scanning

Port scanning
ClassReconnaissance
Also known asPort scan, network service discovery
MITRE ATT&CKT1046 Network Service Discovery; T1595.001 Scanning IP Blocks
Common toolsNmap, ZMap
AffectsAny server with a public address, and every host inside a network an attacker has entered

A server on the internet has 65,535 numbered TCP ports, and each program that accepts connections, such as a web server, a database or SSH, waits on one of them.1 Port scanning is the sending of a small probe to many ports, on one address or on millions, and recording which ones answer. The result is a list of the services a machine exposes. Each open port on that list is a possible way in for an attacker, and administrators scan their own servers to close or firewall the ones that should not be open.2

How a probe reads a port

A TCP connection begins with a three-way handshake: the client sends a SYN segment, the server replies SYN/ACK, and the client confirms with ACK.3 A TCP stack that has no connection and no listener for a segment answers it with a reset (RST).3 A scanner uses this difference. In a SYN scan, the default in Nmap when it has the rights to send raw packets, the scanner sends the SYN and never completes the handshake, which is why it is also called half-open scanning. A SYN/ACK marks the port open, an RST marks it closed, and silence after several retries, or an ICMP unreachable message, marks it filtered.4 A connect scan instead asks the operating system to open a full connection; it needs more packets and is more likely to appear in the target's logs.4

Table 1. What a TCP SYN probe receives, and how a scanner labels the port
ReplyLabelMeaning
SYN/ACKopenA program accepts connections on this port.
RSTclosedThe host is reachable, but nothing listens on the port.
Nothing, or ICMP unreachablefilteredA firewall or router dropped the probe or its reply.

The labels describe what the scanner saw from where it stood, not a fixed property of the port. A port can look open from inside the same network and filtered from across the internet at the same moment.2 A closed port still shows that a host is alive, and blocking it with a firewall turns it into a filtered one.2 Filtered ports also slow a scan, because the scanner has to retry in case the probe was lost.2

UDP probes are harder to read. An open UDP port usually sends nothing back, so the scanner cannot tell it from a filtered one; a closed port answers with ICMP port unreachable (type 3, code 3). Many hosts, Linux among them, limit how many of those messages they send, and at one per second a scan of every UDP port on one host takes more than 18 hours. Services on UDP, such as DNS on port 53 and SNMP on 161, are common, and attackers scan them even when some auditors skip them.4

From an open port to a version

An open port alone says little. Version detection sends further probes to each open port and compares the replies with a database of known responses, which in Nmap is the file nmap-service-probes. The match names the protocol, the application, often its exact version, and sometimes the device type and operating system family.5 An exact version number shows which published flaws and exploits apply to the server.5 Checking those flaws is the work of Vulnerability scanning.

Scanning the whole internet

In 2013 the ZMap scanner showed that a single machine could probe every IPv4 address in under 45 minutes.6 The team behind it later started Censys, which scans continuously, covers all 65,535 ports across over 99% of the public internet by its own account, and every day rescans each known service whose data is more than 24 hours old. Censys does not try to log in; a server owner who wants out has to drop traffic from its scanning networks, and past results stay in its data.7

In June 2021 NewsBlur put a new server into service as its MongoDB primary at 6:10 pm. The firewall rule Docker inserts for a published port had opened MongoDB's port 27017 to the world, and at 9:35 pm an automated attacker connected to the open database, deleted it and left a ransom note.8

Scanning from inside a network

Port scanning also happens after a break-in. An attacker who controls one machine scans the hosts and network devices around it to find services that can be exploited next, in cloud networks as well as offices. The TeamTNT group used masscan to look for open Docker API ports and Kubernetes clusters, and malware that runs ZMap to find vulnerable services in cloud environments.9 A web app with Server-side request forgery can be made to scan ports on internal hosts for an attacker.10 Scanning a target's address ranges from outside, before any access, is reconnaissance;11 the scan from a machine already taken is discovery, a later stage.9

Checking a server

A server check compares what the server listens on with what the internet can reach. On Linux, ss lists listening TCP and UDP sockets with the program that owns each one:12

sudo ss -tulnp

A scan from a machine outside the server's network shows which of those ports get through. Without options Nmap checks the 1,000 most common ports; -p- covers all 65,535, and -sV adds version detection.15

nmap -p- -sV example.com

Scan only servers you run or have written permission to test. Stop each service that has no reason to be public, and put the rest behind a host or cloud firewall that lets through only the ports they need.9 For login ports that stay open, see SSH brute-force attack and Default credentials.

In SecFlash

On the Security page, SF-SEC-302 reports databases, admin panels and remote logins reachable from the internet, and SF-SEC-303 reports known vulnerabilities in the software answering on those ports. On a connected server, the Server page reports SF-SRV-101 for a risky service open to the internet, SF-SRV-102 for a port that listens on every address and answers from outside, SF-SRV-103 for an unexpected open port, and SF-SRV-104 for a port open on IPv6 but closed on IPv4. SF-DET-301 on the Detections page flags a program that starts listening on a reachable port, and SF-CLD-202 on the Cloud page flags a cloud firewall rule that opens every TCP port.

See also

References

  1. a b Lyon, G. (n.d.). "Port Specification and Scan Order". Nmap Reference Guide.
  2. a b c d Lyon, G. (n.d.). "Port Scanning Basics". Nmap Reference Guide.
  3. a b Eddy, W. (ed.) (2022). "RFC 9293: Transmission Control Protocol (TCP)". IETF.
  4. a b c Lyon, G. (n.d.). "Port Scanning Techniques". Nmap Reference Guide.
  5. a b c Lyon, G. (n.d.). "Service and Version Detection". Nmap Reference Guide.
  6. back Durumeric, Z., Wustrow, E. and Halderman, J. A. (2013). "ZMap: Fast Internet-wide Scanning and Its Security Applications". 22nd USENIX Security Symposium.
  7. back Censys (n.d.). "Scanning FAQs". Censys documentation.
  8. back NewsBlur (2021). "How a Docker footgun led to a vandal deleting NewsBlur's MongoDB database". The NewsBlur Blog.
  9. a b c MITRE (n.d.). "Network Service Discovery, T1046". MITRE ATT&CK.
  10. back MITRE (n.d.). "CWE-918: Server-Side Request Forgery (SSRF)". Common Weakness Enumeration.
  11. back MITRE (2026). "Active Scanning: Scanning IP Blocks, T1595.001". MITRE ATT&CK.
  12. back Linux man-pages project (n.d.). "ss(8)". man7.org.