Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 36 ยท Evasion, Timing and Ethics

Detecting Port Scans in Logs

Learn what port scans look like in firewall and server logs, then build a Python detector for horizontal and vertical scans with thresholds, and learn how to respond.

Every scan leaves traces. A firewall that logs dropped connections records each probe, a web server logs each odd request and a system log shows authentication attempts. As a defender you want to answer: who is scanning us, how widely, and is it expected? As a scanner you should know how visible your own activity is, which is one more reason to announce authorised scans.

In this lesson you learn the patterns that identify scans, build a small detector in Python that runs on sample log lines and learn how to tune thresholds, avoid false alarms and respond.

All log lines in this lesson are invented documentation examples, using reserved addresses (192.0.2.x, 198.51.100.x, 203.0.113.x). The detector is a teaching tool, not a replacement for a real IDS or SIEM.

Two shapes of scan

  • Vertical scan: one source probes many ports on one host. It looks for any service that answers.
  • Horizontal scan (sweep): one source probes one port on many hosts. It looks for a particular service, such as SSH, across a network.
  • A block scan combines both. A distributed scan spreads probes over many sources to stay below simple thresholds.
flowchart LR
    subgraph V["Vertical scan"]
      A["1 source"] --> B["1 host,<br/>ports 1..1000"]
    end
    subgraph H["Horizontal scan"]
      C["1 source"] --> D["hosts .1 .. .254,<br/>port 22"]
    end

The same source address shows up with many distinct (host, port) pairs.

What the logs look like

A firewall log line usually has a time, action, source, destination and ports. A common simple form:

TEXT
2026-11-12T10:00:01 DROP SRC=203.0.113.50 DST=192.0.2.10 PROTO=TCP DPT=21
2026-11-12T10:00:01 DROP SRC=203.0.113.50 DST=192.0.2.10 PROTO=TCP DPT=22
2026-11-12T10:00:02 DROP SRC=203.0.113.50 DST=192.0.2.10 PROTO=TCP DPT=23

Three drops from one source to three ports within two seconds is not normal behaviour for a user, who opens one or two connections. A threshold on distinct destination ports per source in a time window finds vertical scans; a threshold on distinct destination hosts finds sweeps.

A detector in Python

The program parses sample lines, groups them by source and flags sources that exceed thresholds. Pyodide runs it directly in your browser.

Python Detect vertical and horizontal scans

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.

The legitimate internal client 192.0.2.200 produced only ALLOW lines, so it is ignored, and the one-off attempt from 203.0.113.99 stays below the threshold.

Using a time window

Real scans have a time dimension: five probes in a minute is a scan, five probes in a month may not be. A sliding window counts distinct ports only within the last N seconds. The next stand-in flags a slow scan only when the window is long enough.

Python Sliding window: fast and slow scans

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.

The slow scan only triggers with a long window. That is the trade-off: longer windows catch slow, patient reconnaissance but remember more state and produce more false positives (a busy NAT gateway looks like a scanner).

Reducing false positives

  • Allow-list known scanners: your own vulnerability scanner, monitoring and uptime checks should be recognised, not alerted on.
  • Ignore normal behaviour: web crawlers and load balancers touch many hosts on 80 and 443.
  • Count only denied traffic, as above, or connections that never completed.
  • Use ranks, not only thresholds: alert on the top sources by distinct targets.
  • Correlate: a scan followed by a login attempt from the same source is far more interesting than a scan alone.

Other places scans appear

Source What to look for
Firewall / router logs Many drops from one source, odd flag combinations
Web server access logs Requests for paths such as /admin, /.env, user agents with tool names
Authentication logs Rapid failures from one source address
IDS (Suricata, Snort, Zeek) Built-in port-scan and sweep rules and notices
Cloud flow logs Rejected flows grouped by source
Honeypots Any contact is suspicious by definition

NSE scripts and Nmap itself identify themselves in some requests, for example in the HTTP User-Agent (Mozilla/5.0 (compatible; Nmap Scripting Engine; ...)), which is easy to flag. Responsible testers change it to something that names their team and purpose (--script-args http.useragent=...).

Responding

  1. Check the source. Is it your own scanner, a partner, or an unknown address?
  2. Do not retaliate. Scanning the scanner back can be illegal and is rarely useful.
  3. Block or rate-limit repeat offenders, keeping in mind that a source address may be spoofed or shared.
  4. Review exposure. A scan shows what an attacker sees first. Close or firewall services that need not be public.
  5. Record it. Keep the evidence for trend analysis and, if needed, an abuse report to the network owner.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a horizontal scan (sweep) look like in logs?

    A sweep looks for one service across many hosts. A vertical scan checks many ports on a single host.
  2. 2. Why use a time window in detection?

    Without a window, long-running normal traffic would eventually reach any threshold. The window ties the count to how fast probing happens.
  3. 3. Which is the best way to reduce false alarms from your own security scanner?

    Known, authorised scanners should be recognised explicitly so that alerts remain meaningful.
  4. 4. Why is a very long detection window a trade-off?

    Longer memory finds patient scanners, but a NAT gateway with many users can look like a scanner over a long time.

Summary

  • Scans show up as many distinct (host, port) pairs from one source in a short time: vertical, horizontal or both.
  • A window plus thresholds on distinct ports and hosts finds most simple scans; long windows catch slow ones.
  • Allow-list your own scanners and correlate with other events to reduce noise.
  • Respond with verification, blocking, exposure review and records, not retaliation.

Next: Legal and Ethical Scanning