It is hard to protect what you do not even know exists. Nmap is a well-established tool for discovering hosts, open ports, and exposed services, and in defensive work it has a legitimate and very concrete use: taking inventory of your own attack surface. When you know exactly what is exposed and compare it with what should be, the forgotten ports, the services that should not be there, and the configuration drift come to light before an attacker finds them.
The subject here is the defensive, responsible use of Nmap: mapping, with authorization, the assets under your responsibility, for inventory and exposure reduction. We will look at the discovery commands that make sense in this context, how to record the results in a reusable way, and how to turn the inventory into a routine that detects drift. Exploitation and use against third-party systems are out of scope, because they would be illegal and unethical.
Scope, authorization, and responsibility
Everything starts with an authorized scope. Scan only assets that you manage or that you have explicit authorization to assess. Scanning third-party networks without permission is illegal and may be treated as hostile activity. So define clearly which ranges and hosts are in scope, and record the authorization.
In the cloud there is a detail that trips up a lot of people: the network you see may not be yours. Cloud providers' public addresses are shared infrastructure, and scanning a broad range can hit third-party assets and violate the provider's terms of use. Limit the scope to the addresses and resources that belong to your organization. The starting point is an inventory of accounts and assets, not a generic network range.
Impact and communication
Scanning also has an impact. Aggressive scans generate load and noise and, in sensitive environments, can even affect fragile services. Prefer controlled approaches, pick suitable time windows, and let the responsible teams know, so nobody mistakes the scan traffic for an incident.
Record each inventory campaign: who authorized it, what the scope was, when it started and ended, and which source it ran from. This record protects the team and keeps the activity from being read as an attack. It also helps when your own monitoring fires an alert during the scan window, because all you need to do is check the time and the source.
What the inventory reveals
An Nmap inventory shows which ports are open, which services answer on them and, with version detection, which software is exposed. When you compare that result with what should be exposed, the deviations show up: a reachable administrative port, an open database service, a forgotten host answering on the internet. These findings feed directly into fixing Security Groups, firewalls, and configuration.
- Open ports that should not be reachable
- Services exposed with no business need at all
- Software versions that reveal missing updates
- Forgotten hosts that still answer publicly
- Mismatches between what is exposed and what is documented
Running the scan is the easy part. The work lies in interpreting the result in context: every open port needs a justification, and whatever has none should be closed.
Commands for an authorized inventory
With the scope defined and authorized, a few commands cover the inventory work well. The idea is to look closely enough that no port gets left out, while taking care not to overload the services. In the examples, replace the target with your own range or with the file of authorized hosts. The first step is to find out which hosts are up, as in:
# Finds which addresses respond, without scanning ports yet.# Replace the range with your authorized, recorded scope.nmap -sn 10.0.0.0/24 -oA inventory/host-discoveryWith the list of live hosts in hand, the next step is to survey ports and services. From a defensive point of view, version detection is the most useful part, because it reveals which software answers on each port and helps identify outdated components.
# -sV identifies the service and version on each open port.# -T3 keeps a measured pace to avoid overloading anything.# --top-ports covers the most common ports; use -p- for the full range.nmap -sV -T3 --top-ports 1000 -iL authorized-hosts.txt \ -oA inventory/ports-servicesNote the -oA option. It writes the results in three formats at once, and one of them is suited to automated comparison between runs. That detail is what turns one-off scans into a usable history. To check your exposure as seen from the outside, run the survey from a point outside your network, only against the public addresses that are yours, and compare the result with the list of ports that should be open to the world.
Do not treat Nmap scripts as if they were harmless. The ones in the safe discovery category help enrich the inventory, but the more intrusive ones can interfere with fragile services. In a sensitive production environment, stick to the basics of ports and versions and save the deeper checks for agreed-upon windows.
Baseline and drift detection
A single inventory tells you what is exposed today. The bigger gain comes when you compare runs over time and detect what changed. Think of a new port that opened without going through any planned change: that is exactly the kind of drift that comes before an incident. When you compare the current scan with the previous one, that signal jumps out.
# ndiff compares two scans saved as XML and shows the delta:# ports that opened, services that changed, hosts that appeared.ndiff baseline.xml inventory/ports-services.xmlSet an approved baseline of the expected exposure and compare each new scan against it. Every deviation becomes an objective question: should this port be open? If so, update the baseline. If not, open a remediation item with an owner and a deadline. With this discipline, the inventory stops being a snapshot of the moment and starts working as detection.
Combining the external view with the internal one
The scan shows what answers on the network, but it does not explain why the port is open. To close that gap, cross-check the result against the configuration source of truth. On AWS, Security Groups, route tables, and AWS Config tell you which rule allowed that exposure. The external view finds the symptom, and the internal view points to the cause and to where the fix belongs.
From scan to remediation
The last step is to turn the inventory into action. Document the findings, assign owners, and prioritize closing whatever is exposed without need. Repeat the scan periodically, because exposure changes with every new resource that goes live and every rule that gets changed. An Nmap inventory works better as a routine than as a one-time event, and it complements the internal view that configuration tools like AWS Config provide.
Practical checklist
- Define and record the authorized scope of the scan
- Scan only your own assets or assets you are authorized to assess
- Start from an asset inventory, not from generic ranges
- Pick suitable windows and notify the teams
- Avoid aggressive scans and intrusive scripts near fragile services
- Discover live hosts before surveying ports
- Survey ports and services with version detection
- Save the results in a format that allows comparing runs
- Identify open ports that have no justification
- Set a baseline of the expected exposure
- Compare each scan with the baseline and investigate deviations
- Cross-check what is exposed against the network configuration that allowed it
- Document the findings and assign an owner to each one
- Prioritize closing whatever is exposed for no reason
- Repeat the inventory regularly
Best practices
- Use Nmap only within an authorized, recorded scope
- In the cloud, confirm the public addresses are yours before scanning
- Require a justification for every open port
- Save results so you can compare runs and detect changes
- Interpret the results in the context of the environment
- Turn the inventory into a prioritized list of fixes
- Make the inventory a recurring routine
- Combine the external view from the scan with the internal view from the configuration
Common mistakes
- Scanning without authorization or outside the scope
- It is illegal and unethical activity that may be treated as hostile.
- Scanning a provider's public ranges as if they were yours
- The scan hits third-party assets and violates the cloud provider's terms of use.
- Aggressive scans or intrusive scripts against fragile services
- The resulting load and noise can disrupt operations.
- Running the scan without interpreting it
- You are left with a list of ports that never turns into action, and the exposure stays the same.
- Not keeping results for comparison
- Each scan becomes a standalone snapshot, and drift detection is lost.
- A one-time inventory
- Exposure changes, and newly opened services go unnoticed.
- Not notifying the teams
- Someone mistakes the scan traffic for a real incident.
Authorship and review
Cesar Gargiulo
CEO and founder of GUARDIASEC. Technical and editorial lead for the content published in this library.
Published on . Found a technical error? Write to [email protected] and we will correct it and keep a record of the change.