Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Enumeration: ports, services and versions

After this lesson: I can list the open ports and services on the hosts in scope with Nmap.

55 min lesson8 commandsPenetration testing lab: from scope to retest

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

Knock on every door in the building

The site named one other host. But a building has more doors than its signs show.

Enumeration knocks on every door of every host in scope, notes which open, and asks what is behind each one.

The one rule

Warning

Scan only the hosts in scope, in your own lab copy. Scanning a network you do not have written permission to test can be a crime: in the UK under the Computer Misuse Act 1990. A scan of all 65,535 ports sends tens of thousands of connections.

Quick check: the rule

Have a go

Quick check: the rule

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

By the end of this lesson

Note

I can list the open ports and services on the hosts in scope with Nmap.

Key words

WordWhat it meansExample
Enumerationlisting what each host offers
Connect scana scan that completes each connection-sT
Open porta port where a service answers
Version detectionasking each service what it is-sV
Privilegeswhat an account is allowed to do

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Who the scanner runs as decides the scan

A SYN scan builds its own packets, which needs root. The lab's scanner runs as an unprivileged account, as a cautious team would set it up.

So the lessons use a connect scan, -sT. It completes every connection, which is slower and shows in more logs.

Ask who the scanner runs as

Command
docker exec scanner id

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Try a SYN scan

Command
docker exec scanner nmap -sS -Pn -p 8080 www

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

The default scan is not every port

With no -p, Nmap tries the 1,000 ports it finds open most often. -Pn skips the check that a host is up, and -iL - reads the scope file again.

Run the default scan first, then all 65,535 ports with -p-. --open hides the closed ones.

Scan the 1,000 common ports

Command
docker exec -i scanner nmap -sT -Pn -iL - < scope.txt

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Scan every port

Command
docker exec -i scanner nmap -sT -Pn -p- --open -iL - < scope.txt

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A common mistake

Warning

Many people think a default scan checks every port. It checks 1,000, and the cache's port is not one of them. When the scope allows it, scan every port of every host, or say in the report which ports you did not scan.

A version is what the service says

-sV sends each open port a few probes and matches the answers. It names the software and a version.

Those names come from banners, so treat them as claims. The cache, for one, runs Valkey, which answers as Redis.

Ask each service what it is

Command
docker exec scanner nmap -sT -Pn -sV --open -p 8080,8000,6379 www intranet cache

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Try it: open or expected?

Code: python
# The ports a full scan found, and the ones the college expected.
found = {"www": [8080], "intranet": [8000], "cache": [6379]}
expected = {"www": [8080], "intranet": [8000], "cache": []}

for host, ports in found.items():
    for port in ports:
        if port not in expected[host]:
            print(host, port, "is open and nobody expected it")

# Add 6379 to the cache in expected. What changes?

Make a free account to save your work and track your progress.

How a defender sees a scan

A connect scan leaves a connection in every log it touches. A version scan also asks the web site for odd paths, such as /nmaplowercheck followed by a number.

The fix for enumeration is fewer doors: close what is not needed, and let only the portal reach the cache.

Find the version scan in the site's log

Command
docker logs www 2>&1 | grep nmap

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Save the scan for a program

Command
docker exec -i scanner nmap -sT -Pn -p- --open -oG - -iL - < scope.txt

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check nothing reached the supplier

Command
docker logs hr

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Assignment: read a scan into a table

Nmap's grepable output (-oG -) has a line per host such as Host: 172.19.0.6 (www.lab)\tPorts: 8080/open/tcp//http-proxy///\tIgnored State: closed (65534). Several ports are separated by , . Write two functions.

open_ports(text) returns a dictionary from each host to a list of (port, service) tuples for its open ports, port a whole number, in the order given. The host is the name in brackets up to its first dot, or the address when the brackets are empty. Lines without Ports: are skipped. unexpected(found, expected) returns a sorted list of (host, port) for every open port in found that is not in expected, a dictionary from host to a list of ports. A host missing from expected expects nothing.

Code: python
def open_ports(text):
    return {}


def unexpected(found, expected):
    return []

Make a free account to save your work and track your progress.

Exit quiz

Have a go

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.