Inventorying and Auditing Your Own Network with Nmap
Exit

Lesson 1 of 9 · Framework and fundamentals

Scope, authorization and method of an internal audit

35 min Debian 12

Objective

Understand why an administrator must know what their own machines expose, within which legal framework they are allowed to check it, and follow a simple internal audit method. You will finish with a first scan… of your own VM.

Key points

  • Nmap is an inventory tool: it tells you which ports answer on a machine and which services run there. For an administrator, it is the equivalent of a property inspection.
  • You only scan your own machines or a network for which you hold a written authorization from the organization. Without it, it is an offense (Moroccan Penal Code, articles 607-3 and following).
  • An audit is scoped before it starts: exact scope (addresses), sponsor, authorization document, time window, emergency contact.
  • You log every command you run, with the date and time: it is your proof that you stayed within the scope.
  • In this course, all exercises target 127.0.0.1, that is, your VM itself.

Why inventory your own machines?

Take Samira, a technician in a logistics SME in Tangier. Her file server was installed three years ago by a contractor. Nobody knows exactly what runs on it. One day, an old, forgotten remote administration service lets ransomware in.

Attackers inventory your machines in a few seconds. The only reasonable defense is to do it before them and close whatever is useless. That is exactly the role of an internal audit:

  1. Inventory: which ports are open, which services, which versions?
  2. Compare with what should exist (the "allowlist": the website, the administration SSH…).
  3. Fix the gap: stop, restrict, filter, update.
  4. Verify with a new scan that the fix works.
  5. Repeat regularly, and compare with the previous inventory.

These five steps are the common thread of the whole course.

What Moroccan law says

Law No. 07-03 added articles 607-3 to 607-11 to the Penal Code, on offenses against automated data processing systems. In short, and without replacing a lawyer's advice:

Article What is punished (summary)
607-3 Fraudulently accessing all or part of a system, or remaining in it
607-5 Deliberately hindering or distorting the operation of a system
607-6 Fraudulently introducing, modifying or deleting data
607-10 Making, holding or transferring equipment or programs designed to commit these offenses
607-11 Confiscation of the equipment used

Remember the logic: what separates an audit from an attack is the authorization. The same scan, run against a neighbor's server, can be seen as an access attempt or as hindrance (especially if it slows down a fragile device). Run against your own server, with the written consent of your management, it is an administrator's work.

Two other texts matter in an internal audit:

  • Law 09-08 on the protection of personal data (CNDP): a hospital audit report must not contain patient data;
  • Law 05-20 on cybersecurity and the DNSSI (national information systems security directive) for public administrations and critical infrastructures, which precisely require you to know and control your systems.

The written authorization

Even internally, even if you are "the in-house IT person", ask for an authorization signed by someone who has the power to give it (director, IT director, CISO). It must specify:

Item Example (university in Rabat)
Sponsor Information Systems Department
Auditor Youssef A., network technician
Scope 10.20.30.0/24 (server room), excluding 10.20.30.50 (student records server during the exam period)
Window Saturday 25/10, from 8 a.m. to 12 p.m.
Allowed techniques TCP port inventory, version detection, "safe" NSE scripts
Forbidden denial of service tests, password guessing, exploitation
Emergency contact IT on-call, extension 1234

For a hospital, be even more careful: some biomedical devices or PLCs react badly to an aggressive scan. Exclude them from the scope, or scan them at low speed, with the biomedical department present.

The method, tool by tool

Nmap is installed from your distribution's repositories:

sudo apt install nmap            # Debian, Ubuntu
sudo dnf install nmap            # Rocky, Alma, RHEL
nmap --version                   # Nmap version 7.93 ( https://nmap.org ) on Debian 12

Step 1: check the target list without sending anything

The list scan (-sL) displays the addresses Nmap would target, without sending a single packet to them. It is the best way to review your scope before launching anything.

nmap -sL -n 10.20.30.0/29
# Nmap scan report for 10.20.30.0
# Nmap scan report for 10.20.30.1
# ...
# Nmap done: 8 IP addresses (0 hosts up) scanned in 0.01 seconds

-n avoids DNS queries. "0 hosts up" is normal: Nmap did not query anything.

To exclude a machine from the scope:

nmap -sL -n 10.20.30.0/24 --exclude 10.20.30.50

Step 2: the first scan, on your own machine

nmap -sT -p 8080 127.0.0.1
# PORT     STATE SERVICE
# 8080/tcp open  http-proxy
  • -sT: Nmap opens a real TCP connection, as a browser would. It is the most "polite" scan and it works without root privileges.
  • -p 8080: a single port.
  • 127.0.0.1: the machine itself (the loopback address).

The SERVICE column (http-proxy) is only the usual name of this port, taken from a table: Nmap has not yet looked at what really answers. That is the topic of lesson 2.1.

Step 3: keep a record

nmap -sT -p 8080 127.0.0.1 -oN /root/audit/premier.txt     # saved output
echo "$(date '+%F %T') nmap -sT -p 8080 127.0.0.1" >> /root/audit/journal.txt

The first line of an -oN file already records the command and the time: # Nmap 7.93 scan initiated … as: nmap -sT -p 8080 …. The log, for its part, gathers all the audit commands in a single file that your manager can read.

Common pitfalls

  • "It's the company network, I work here, so I'm allowed": no. Without a written authorization that specifies the scope, you are at fault, even with good intentions.
  • Getting the address wrong: one typo (10.20.3.0/24 instead of 10.20.30.0/24) and you scan someone else's network. Always review with -sL first.
  • Scanning a hosting provider or cloud network without reading its terms: many regulate scanning, even of your own machines.
  • Forgetting the log: if an incident happens during the audit window, it is what will prove what you did (and did not do).

Lab

You prepare a mini-audit of your VM: you write the scoping sheet (target, sponsor, authorization, window), you review a scope with the list scan without sending any packet, then you run your first TCP scan of port 8080 on 127.0.0.1 and record it in the audit log. Start with sudo ml demarrer nmap-1-1.

Quiz

  1. What legally distinguishes an audit from an attack?
  2. What does the -sL option do? Does it send packets to the targets?
  3. Name four essential items of an audit authorization.
  4. Why must you be careful in a hospital?
  5. In the output 8080/tcp open http-proxy, did Nmap check that it is a proxy?
Answers
  1. The written authorization of the owning organization, which sets the scope and the allowed techniques (Penal Code, articles 607-3 and following for unauthorized access or hindrance).
  2. It lists the targeted addresses without sending any packet to them: it is a review of the scope.
  3. Sponsor, auditor, scope (and exclusions), time window, allowed and forbidden techniques, emergency contact.
  4. Some biomedical devices or PLCs are fragile; a scan that is too fast can disrupt them. You exclude them or scan them gently, with the department concerned.
  5. No: it is the usual name of port 8080. Only version detection (-sV) really queries the service.

Reference sources: Nmap Reference Guide and the nmap(1) manual page; Moroccan Penal Code (Law No. 07-03). Original MasterLinux text.

Practical exercises

To do on Debian 12

Do this lab on your own machine

The terminal is yours: a Linux VM (VirtualBox, VMware, Hyper-V, WSL…) or Docker/Podman. The ml tool prepares the exercise and checks your work; the lesson is validated here automatically.

Create a free account to get your personal command and have your labs checked automatically.

Create my account

Tasks

  1. Write the scoping sheet /root/audit/cadre/perimetre.txt with four "key=value" lines: cible=127.0.0.1 (the target), commanditaire=… (who requests the audit), autorisation=… (reference of the signed document) and fenetre=… (authorized day and hours). Keep these French key names exactly, they are checked.
    Show a hintOne line per item, no spaces around the "=". Example: commanditaire=Direction PME Tanger.
    Show the solution
    printf "%s\n" "cible=127.0.0.1" "commanditaire=Direction informatique - PME Tanger" "autorisation=AUT-2026-014 signee le 19/10/2026" "fenetre=lundi 20/10 de 20h a 22h" > /root/audit/cadre/perimetre.txt

    Check : f=/root/audit/cadre/perimetre.txt; grep -qx "cible=127.0.0.1" $f && grep -q "^commanditaire=.\{2,\}" $f && grep -q "^autorisation=.\{2,\}" $f && grep -q "^fenetre=.\{2,\}" $f

  2. Without sending a single packet, have Nmap list the addresses of 127.0.0.0/30 (list scan, no DNS resolution) and save the result in normal format to /root/audit/cadre/liste.txt
    Show a hintThe list scan is -sL; -n disables DNS; -oN file saves the normal output.
    Show the solution
    nmap -sL -n 127.0.0.0/30 -oN /root/audit/cadre/liste.txt >/dev/null

    Check : f=/root/audit/cadre/liste.txt; grep -q "^# Nmap .* -sL" $f && grep -q "Nmap scan report for 127.0.0.3" $f && grep -q "(0 hosts up)" $f

  3. A small test web server is listening on your VM. Run your first scan: full TCP connect (-sT) on port 8080 only of 127.0.0.1, normal output in /root/audit/cadre/premier.txt
    Show a hintnmap -sT -p 8080 127.0.0.1 -oN …
    Show the solution
    nmap -sT -p 8080 127.0.0.1 -oN /root/audit/cadre/premier.txt >/dev/null

    Check : f=/root/audit/cadre/premier.txt; grep -q "^# Nmap .*-sT" $f && grep -Eq "^8080/tcp +open" $f

  4. Keep the audit log: append to /root/audit/cadre/journal.txt a line that starts with the date in YYYY-MM-DD format followed by the time, then the exact command of the previous scan (nmap -sT -p 8080 127.0.0.1)
    Show a hintdate "+%F %T" gives 2026-10-19 21:05:12. Combine them: echo "$(date '+%F %T') nmap -sT -p 8080 127.0.0.1" >> …
    Show the solution
    echo "$(date "+%F %T") nmap -sT -p 8080 127.0.0.1" >> /root/audit/cadre/journal.txt

    Check : f=/root/audit/cadre/journal.txt; grep -Eq "^20[0-9]{2}-[01][0-9]-[0-3][0-9].*[0-2][0-9]:[0-5][0-9].*nmap -sT -p 8080 127\.0\.0\.1" $f

Official references consulted: Nmap Reference Guide (Nmap Network Scanning, Gordon Lyon) — https://nmap.org/book/man.html ; Nmap — Legal Issues — https://nmap.org/book/legal-issues.html ; nmap(1) — page de manuel (Debian 12, paquet nmap 7.93) ; Code pénal marocain, articles 607-3 à 607-11 (loi n° 07-03 relative aux atteintes aux systèmes de traitement automatisé des données). Original MasterLinux text.