Scanning with Nmap
Chapters and transcript
Scanning with Nmap In the last lesson, you used s s to see open ports from inside a machine. Attackers and testers do not have that view. They look from the outside, using a port scanner. The most famous one is n map, the network mapper. Today you will run it on our own lab server, and, more importantly, learn to read what it tells you.
Port scanning A port scanner sends small messages, called probes, to many ports on a target. The target answers. From the replies, n map works out which ports are open, and which services are behind them. This is usually the first step of a penetration test, because you cannot attack or defend what you do not know is there.
Permission first Before you type a single command, one rule. Permission first. Only scan systems you own, or have written permission to test. Scanning other people's systems without permission can break the law in many countries, and can disrupt their services. To practise, use your own lab machines, or platforms built for practice, like the lab in this course.
Basic commands Here are the basic commands. N map and an address scans the one thousand most common TCP ports. Dash p with a list scans only the ports you name. Dash p dash scans all sixty-five thousand five hundred and thirty-five TCP ports. Slower, but complete. And dash s V also tries to detect which software and version runs on each open port.
Reading nmap output Now I scan our own lab server, with n map dash s V. The host is up. Nine hundred and ninety-three closed ports are hidden, to keep the output short. Each line shows the port, its state, the service, and the version. F T P, S S H, Telnet, a web server and a database are open. Port thirty-three eighty-nine is filtered.
Open, closed, filtered What do the states mean? Open: a program is listening and accepted the connection. That is what testers care about most. Closed: the host replied, but nothing is listening there. Filtered: no clear reply came back. Usually a firewall dropped the probe, so n map cannot tell. On our lab server, port thirty-three eighty-nine is filtered, which is a good sign for the defender.
Quick check Quick check. You scan a client web server, with their written permission. N map shows port thirty-three oh six open, running my s q l, reachable from the internet. What should you do? Take a moment, then choose.
Risky services Not every open port is a problem. But some services should almost never face the internet. F T P and Telnet send passwords without encryption. S M B, Windows file sharing, has a long history of serious flaws. R D P is a frequent target for password guessing. And databases, like my s q l, Redis and Mongo D B, are meant for internal use only.
Old versions Old software versions are another key finding. I scan just two ports on the lab server, with dash s V. Open S S H seven point two is very old. And v s f t p d two point three point four is famous: one release of it was shipped with a hidden backdoor. A tester looks up each version in public vulnerability databases, to see which known flaws apply.
Writing the report Finding open ports is only half the job. The client needs a clear report. For each finding, write a title, the affected host and port, and a risk level. Then a short description, the evidence, like the n map line, and a recommendation. Explain the risk in simple words and give a clear fix, so the owners can act.
Defending And defenders? First, shrink what is exposed. Scan your own public addresses first, so you see what attackers see. Close and disable the services you do not need. And bind internal services, like databases, to one two seven dot zero dot zero dot one, so only the machine itself can reach them.
Defending Then protect what must stay public. Use a firewall to allow only the ports that must be public. Replace insecure services: S S H instead of Telnet, S F T P instead of F T P, and a V P N in front of R D P and S M B. And keep software updated, so a version scan finds nothing old.
Putting it together Let us put it together. Only scan systems you have permission to scan. N map finds open ports from the outside, and dash s V adds versions. Open means listening, closed means nothing there, filtered usually means a firewall. Exposed databases, Telnet, F T P and old versions are key findings. Report each one with evidence and a clear fix. Now read the lesson below, then try the lab.
In the last lesson you used ss to see which ports are open from inside a machine. Attackers and testers do not have that view. They look from the outside, using a port scanner. The most famous port scanner is nmap (network mapper). This lesson teaches you to run it and, more importantly, to read what it tells you.
What port scanning is
A port scanner sends small network messages, called probes, to many ports on a target. From the replies, it works out which ports are open and which services are behind them. This is usually the first step of a penetration test, because you cannot attack or defend what you do not know is there.
Permission first: only scan systems you own or have written permission to test. Scanning other people's systems without permission can break the law in many countries and can disrupt their services. To practise, use your own lab machines or platforms built for practice.
Basic nmap commands
| Command | What it does |
|---|---|
nmap 192.0.2.10 |
Scans the 1,000 most common TCP ports |
nmap -p 22,80,443 192.0.2.10 |
Scans only the ports you list |
nmap -p- 192.0.2.10 |
Scans all 65,535 TCP ports. Slower but more complete |
nmap -sV 192.0.2.10 |
Also tries to detect which software and version runs on each open port |
You can combine options, for example nmap -sV -p- 192.0.2.10. Some scan types need root rights, so you may see testers run nmap with sudo.
Reading nmap output
Here is a sample scan of a test server:
$ nmap -sV 192.0.2.10
Starting Nmap ( https://nmap.org )
Nmap scan report for 192.0.2.10
Host is up (0.021s latency).
Not shown: 993 closed tcp ports (reset)
PORT STATE SERVICE VERSION
21/tcp open ftp vsftpd 2.3.4
22/tcp open ssh OpenSSH 7.2p2 Ubuntu 4ubuntu2.10
23/tcp open telnet Linux telnetd
80/tcp open http Apache httpd 2.4.18 ((Ubuntu))
443/tcp open ssl/http Apache httpd 2.4.18 ((Ubuntu))
3306/tcp open mysql MySQL 5.5.62
3389/tcp filtered ms-wbt-server
Service detection performed.
Nmap done: 1 IP address (1 host up) scanned in 14.32 seconds
| Column | Meaning |
|---|---|
| PORT | The port number and protocol, such as 22/tcp |
| STATE | What nmap found: open, closed or filtered |
| SERVICE | The service nmap thinks is running. Without -sV, this is only a guess from the port number |
| VERSION | The software and version, shown only when you use -sV |
The line Not shown: 993 closed tcp ports means nmap hid the closed ports to keep the output short.
Open, closed and filtered
| State | What it means |
|---|---|
| open | A program is listening and accepted the connection. This is what testers care about most |
| closed | The host replied, but nothing is listening on that port |
| filtered | No clear reply came back. Usually a firewall dropped the probes, so nmap cannot tell if the port is open |
In the sample, port 3389 is filtered. A firewall is likely protecting it, which is a good sign for the defender.
Which exposed services are risky
Not every open port is a problem. A web server is supposed to have ports 80 and 443 open. But some services should almost never be reachable from the internet.
| Port | Service | Why it is risky when exposed |
|---|---|---|
| 21 | FTP | Sends usernames, passwords and files without encryption. Sometimes allows anonymous login |
| 23 | Telnet | Remote login with no encryption. Anyone on the path can read the password |
| 445 | SMB | Windows file sharing. Has a long history of serious vulnerabilities and is meant for internal networks |
| 3389 | RDP | Windows remote desktop. A frequent target for password guessing |
| 3306 | MySQL | A database. Should only be reachable by the application that uses it |
| 6379 | Redis | A fast data store, often run without a password in internal setups |
| 27017 | MongoDB | A database. Older or careless setups may allow access without login |
Old software versions are another key finding. With -sV, the sample shows OpenSSH 7.2, Apache 2.4.18 and MySQL 5.5, all of which are very old. The FTP version shown, vsftpd 2.3.4, is famous because one release of it was distributed with a hidden backdoor. A tester would look up each version in public vulnerability databases to see which known flaws apply.
What a tester writes in a report
Finding open ports is only half the job. The client needs a clear report. For each important finding, a tester usually writes:
| Section | Example |
|---|---|
| Title | Telnet service exposed to the internet |
| Affected host | 192.0.2.10, port 23/tcp |
| Risk level | High |
| Description | Telnet sends login details in plain text |
| Evidence | The nmap output line showing 23/tcp open telnet |
| Recommendation | Disable Telnet and use SSH instead. Block port 23 at the firewall |
Good reports explain the risk in simple words and give a clear fix, so the people who own the system can act on it.
How defenders reduce exposure
- Scan yourself first. Run nmap against your own public addresses so you see what attackers see.
- Close what you do not need. Stop and disable unused services.
- Bind internal services to 127.0.0.1 or a private address, so databases are not reachable from outside.
- Use a firewall to allow only the ports that must be public.
- Replace insecure services: SSH instead of Telnet, SFTP instead of FTP, and a VPN in front of RDP and SMB.
- Keep software updated, so version scans do not reveal known flaws.
In the lab, you will read a scan result for a test server and answer the questions a real tester would ask about it.
HANDS-ON LAB
Read the Scan
An authorised Nmap scan of a company server is in. What should worry you?
Check your understanding
Answer every question correctly to complete this lesson. If you miss one, try the quiz again.
1. Nmap reports a port as filtered. What does that usually mean?
2. Which option makes nmap try to detect service versions?
3. Why is Redis on port 6379 open to the internet a serious finding?
Locked: answer every quiz question above correctly to complete this lesson.
Spotted a mistake or something unclear? Report a mistake