Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 31 ยท Projects

Project: Network Exposure Auditor

Build a network exposure auditor in Python: simulate scans on a fake network model, compare open ports with an allowed-services policy and print a drift report.

Most breaches do not start with a clever exploit. They start with something that was reachable and should not have been: a database port open to the whole network, a forgotten test service, a remote-access daemon nobody remembered to turn off. Defenders therefore keep two lists. One says what should be exposed (the policy). The other says what is exposed (the scan result). The gap between them is the work.

In this project you build a network exposure auditor. It scans a fake network model, compares the result with a per-role policy, and reports violations and drift, meaning changes since the last approved baseline. It never touches a real network. If port states like open, closed and filtered are new to you, read How Port Scanning Works first.

Real versus stand-in. A real audit runs Nmap or a similar scanner from an authorized machine against hosts you own or have written permission to test, for example nmap -sV -p- 10.20.0.0/24, and saves the output with -oX scan.xml. The browser cannot open network connections to arbitrary hosts, so this project replaces the network with a Python dictionary and the scanner with a function that answers from that dictionary. The policy comparison, baseline diffing and reporting code is exactly what you would run on real scan output. The addresses are private example addresses; no packets are sent anywhere.

The plan

From scan to decision

Model Hosts, roles and listening services.
Scan Report open, closed or filtered.
Compare Match results to the policy.
Drift Diff against the last baseline.

The policy is the source of truth, not the scanner.

1 / 2

Stage 1: a fake network and a simulated scan

A model needs just enough detail to behave like the real thing. Each host has a role (web, database, mail), a dictionary of listening ports and the service name behind each, and a set of ports its firewall drops. The scan function applies the same logic a real TCP connect scan reveals from the outside:

  • if a firewall drops the packets, nothing comes back and the port looks filtered;
  • otherwise, if something is listening, the port is open;
  • otherwise the host answers with a reset and the port is closed.
Python Stage 1: model the network and scan it

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.

Notice the filtered line for web01: the database port is listening there, but the firewall hides it. An outside scanner would call it filtered and would not know a MySQL server exists on that host. That is good defense in depth, and also a reminder that a clean scan from one vantage point is not proof of a clean host. A scan from inside the network would see it open.

Stage 2: the policy and the comparison

A policy says what each role may expose. We use two sets per role: allowed ports (may be open) and required ports (must be open for the service to work). Anything open and not allowed is a violation. Anything required but not open is a gap, which means an outage rather than a security problem, and still worth reporting.

Policies keyed by role scale well. When you add web03, you only assign it the role web and the same rules apply.

Python Stage 2: compare scans with the policy

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 comparison uses set arithmetic: open_ports - rules["allowed"] is "open and forbidden", and rules["required"] - open_ports is "needed and missing". Sets make policy checks short and easy to read. Look at what the audit found. Telnet on db01 is the serious one, because Telnet sends everything, including passwords, in clear text. The port on web02 is an undocumented second web server, the typical leftover of a test. And mail01 lost its imaps port, so users cannot read their mail. The audit does not know which problem is worse; the next stage adds that judgment.

Stage 3: drift against a baseline

A scan is a snapshot. Drift is how the snapshot differs from the last one you approved. Storing a baseline and diffing against it catches changes that are individually small but add up, such as a developer who opens a port "just for today". Because a diff is also useful when the policy itself is incomplete, it complements the policy check instead of replacing it.

We represent each baseline as a dictionary from host name to a {port: service} mapping, then compare them with set operations on the keys.

Python Stage 3: baseline and drift

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 CHANGED line for mail01 shows why comparing only port numbers is not enough. The same port now reports a different service string, which could mean an upgrade, a replaced daemon, or something worth a closer look. Also note web03: an unknown new host is a finding all by itself, because every device on your network should be accounted for.

Stage 4: prioritized report with advice

A raw list of changes is useful for engineers. A decision-maker needs to know what to fix first. We add a small risk table: ports that carry clear-text or high-impact protocols get a higher score. The report sorts by score and attaches a recommended action. The scores are a teaching choice, not an industry standard, so adjust them to your environment.

Python Stage 4: the final exposure report

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.

You now have a loop that real teams run on a schedule: scan, compare with policy, diff with the baseline, rank, and hand the top items to an owner. The value is not in any single run but in the trend. A network that shows fewer violations every month is improving; one that shows the same ones every month has a process problem, not a scanning problem.

Ideas to extend the project

  • Add a forbidden_services list (telnet, ftp, rsh) that is critical wherever it appears, whatever the role.
  • Write the baseline to a JSON file and load it on the next run, as a real tool must.
  • Compare the inside view (no firewall drops) with the outside view, and report ports that are only protected by the firewall.
  • Add UDP entries to the model; note that UDP "open" and "filtered" often look the same to a scanner.
  • Only when you own the systems or have written permission: feed real Nmap XML into the same functions after parsing it with xml.etree.ElementTree.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In the simulated model, why does a port with a firewall drop rule show as "filtered" instead of "closed"?

    A closed port actively refuses the connection, which a scanner sees as a reset. A dropped probe gets no reply at all, so the scanner cannot tell whether anything listens there.
  2. 2. What does the expression open_ports - rules["allowed"] produce?

    Set difference keeps the elements of the first set that are not in the second, so it returns exactly the open ports the policy does not permit.
  3. 3. Why keep a baseline and report drift in addition to checking the policy?

    The policy says what should exist, and the baseline says what you last accepted. Together they catch both forbidden services and unexpected changes.
  4. 4. A clean outside scan of a host shows no database port. What can you conclude?

    A scan only shows what is reachable from where it runs. Services hidden by a firewall are still present, which is why inside views and host-level checks matter too.

Summary

  • An exposure audit compares what is reachable (scan) with what should be reachable (policy) and with what was reachable before (baseline).
  • A scanner reports a port as open, closed or filtered; filtered means no answer, often from a firewall.
  • Policies keyed by role scale to many hosts; set arithmetic finds violations (open minus allowed) and gaps (required minus open).
  • Drift covers opened and closed ports, changed service strings, and new or missing hosts.
  • Rank findings by risk so the clear-text and database exposures are fixed first.
  • The model here is a stand-in: only scan real systems you own or have written permission to test.

Next project: Project: Password Policy Checker.