Chuyển đến nội dung

Bài 19 · Service and OS Detection

OS Fingerprinting (-O)

Understand how Nmap -O guesses an operating system from TCP/IP behaviour: TTL, window size, options, accuracy and limits, with runnable scoring examples.

Different operating systems implement the TCP/IP protocols in slightly different ways. The standards leave many details open: the starting value of the time-to-live counter, the initial window size, which TCP options appear and in which order. These small choices form a fingerprint. Nmap's -O option sends a set of carefully built packets, measures the replies and compares the result with a database of known fingerprints.

OS detection is useful for asset inventories ("which of these 300 addresses are Windows servers?") and for spotting devices that should not exist on a network, such as an unexpected printer or camera. As always, use it only on networks you are authorised to scan.

Nmap and raw packet scanning cannot run in a web page. Example scan output is labelled as example output; the Python blocks are stand-ins that show how a fingerprint can be scored.

Running OS detection

OS detection needs raw packets, so on most systems it requires administrator rights (root, or the Windows Npcap driver with an elevated prompt):

Terminal
# Example command (read-only): OS detection against a lab host you own
sudo nmap -O 192.0.2.20

Example output (illustrative):

TEXT
Nmap scan report for 192.0.2.20
Host is up (0.00041s latency).
Not shown: 997 closed tcp ports (reset)
PORT    STATE SERVICE
22/tcp  open  ssh
80/tcp  open  http
443/tcp open  https
Device type: general purpose
Running: Linux 5.X|6.X
OS CPE: cpe:/o:linux:linux_kernel:5 cpe:/o:linux:linux_kernel:6
OS details: Linux 5.4 - 6.2
Network Distance: 1 hop

Notice the range "Linux 5.4 - 6.2". Nmap is honest about uncertainty: when several fingerprints match equally well, it lists them all.

What Nmap measures

Nmap needs at least one open and one closed TCP port to get a good fingerprint, because open and closed ports reply differently. It then sends a sequence of probes:

  • Six TCP SYN probes (called T1 to T7 in the database) with different option sets, to measure the sequence numbers, window size and options of the replies.
  • ICMP echo requests with unusual codes, to see how the stack answers.
  • TCP and UDP probes to closed ports, to see the shape of the reset or "port unreachable" reply.
  • IP ID and timestamp sampling, to check how the target numbers its packets.

From the replies Nmap builds a compact fingerprint, a list of tests such as SEQ, OPS, WIN, ECN, T1 to T7, U1 and IE. Each test has fields (for example W=FAF0 for the window size). The database scores your fingerprint against each known one and reports the best matches.

flowchart LR
    S["Scanner"] -->|"SYN probes, ICMP,<br/>closed-port probes"| T["Target stack"]
    T -->|"replies with TTL, window,<br/>options, IP ID"| S
    S --> F["Build fingerprint"]
    F --> D["Compare with<br/>nmap-os-db"]
    D --> R["Best matches plus<br/>confidence"]

The OS detection loop. More matching fields mean higher confidence.

Simple clues you can compute yourself

Before Nmap sends anything special, two very simple facts already help: the TTL and the window size of a normal reply. Systems start TTL at a round number (commonly 64, 128 or 255) and routers subtract 1 per hop, so the TTL you see is a little lower than the start value.

Python Stand-in: guess the initial TTL from what you see

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

These guesses are weak on their own: an administrator can change the TTL, and a device behind a NAT or proxy shows the proxy's behaviour. Nmap combines dozens of such signals, which is why it is far more reliable than TTL alone.

Scoring a fingerprint

The following stand-in shows the matching idea. Each known system has expected values for several fields. We count how many fields of an observation agree and turn the share into a confidence figure. The data are invented for teaching and are not Nmap's database.

Python Stand-in: score observed fields against known stacks

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Useful options

Option Effect
-O Turn on OS detection.
--osscan-limit Only try OS detection on hosts that have at least one open and one closed port. Saves time on big scans.
--osscan-guess / --fuzzy Print a guess even when the match is not perfect.
--max-os-tries 1 Try only once instead of up to five times. Faster, less reliable.
-A Aggressive bundle: -sV -O -sC --traceroute (see the next lessons).

Limits and good practice

  • Firewalls and NAT can change or hide the replies. If every port is filtered, Nmap cannot build a fingerprint and says No exact OS matches.
  • Virtual machines and containers show the stack of the host kernel or the hypervisor network layer, not of the guest's marketing name.
  • Modern Windows and Linux builds are close to each other in many fields, so versions are given as ranges.
  • Do not use a single OS guess to make decisions on its own. Combine it with service detection and your asset records.
  • Unexpected OS detection results are a good prompt to check your inventory: a "Linux" on an address you recorded as a printer deserves a look.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Why does OS detection work?

    The standards leave details open, so each stack answers specially built probes in a slightly different way. Nmap compares these answers with a fingerprint database.
  2. 2. What does a host with all ports filtered usually produce under -O?

    Nmap needs at least one open and one closed TCP port for a good fingerprint. Without them the result is weak or absent.
  3. 3. You observe a TTL of 117. Which initial TTL is the most likely?

    Initial values are usually 64, 128 or 255. 117 is just below 128, which suggests about 11 hops from a system that starts at 128.
  4. 4. What does --osscan-limit do?

    It saves time on large scans by not attempting OS detection where it is likely to fail.

Summary

  • -O compares measured TCP/IP behaviour with a database of fingerprints and reports best matches with ranges.
  • It needs raw-packet privileges and, ideally, one open and one closed TCP port.
  • TTL and window size are weak clues alone; Nmap combines many signals.
  • Treat results as evidence to check against your inventory, not as proof.

Next: Banner Grabbing and Probes

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.