Chuyển đến nội dung

Bài 35 · Projects and Practice

Project: A Safe Loopback Port Scanner

Build a small TCP scanner in Node.js that refuses out-of-scope targets, grabs banners from local test services, and prints a findings report you can compare with a policy.

In this project you build a miniature of what Metasploit's scanner modules do: connect to ports, read banners, and report. You make it safe by design: the scanner refuses to touch anything except loopback addresses, and the services it finds are ones the program itself starts. By the end you have a scanner, a banner parser and a policy check, in about a hundred lines.

You will use ideas from Port Scanning with Auxiliary Modules, Service Version Scanners and Law, Ethics and Authorization.

Requirements

  1. Accept a target address and refuse anything that is not 127.0.0.1 (the scope rule).
  2. Start three fake services on random loopback ports: an SSH-like, an FTP-like and a plain HTTP-like server, each with a banner.
  3. Scan a port window, with a concurrency limit and timeout.
  4. For each open port, read the banner and identify the product and version.
  5. Compare the findings with a policy ("only ports in this allow list may be open; versions must be at least X") and print violations.

Step 1: the scope guard

Put the guard in one function and call it before every network action.

JavaScript
function assertInScope(host) {
  if (host !== "127.0.0.1") {
    throw new Error(`Refusing to scan ${host}: out of scope`);
  }
}

Step 2: the whole program

Read it once, then run it. The services use port 0, so the operating system picks free ports; the report therefore uses service names, not port numbers, to stay the same on every run.

Node.js The complete scanner

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 node:readline or process.stdin), it gets the next line from this box.

Expected output (the lines are sorted, so they are stable):

TEXT
open services: 3
OpenSSH  8.2     OUTDATED (min 9.3)
nginx    1.26.1  ok
vsFTPd   2.3.4   OUTDATED (min 3.0.5)
Refusing to scan 203.0.113.9: out of scope

One thing to notice: 8.2p1 is parsed by the regex [\d.]+ as 8.2, because p stops the match, so the version comparison works.

Step 3: extend it

Try these upgrades, each a real skill:

  • Add a --json output option and a severity field per finding.
  • Add a rate limit (for example 50 probes per second) so it never floods a network.
  • Read an allow list of expected ports from a file and report unexpected opens (the comparison idea).
  • Store results with a timestamp and diff two runs to report newly opened ports.
Python Diff two scan results

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.

What this project shows

A scanner is not magic: it is connections, timeouts and parsing. The scope guard shows that safety can be a design property, not a promise. Real tools like Nmap and the Metasploit scanners add speed, raw packets and fingerprints, but the structure is the same.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Where does the scanner enforce scope?

    A single guard function called first makes out-of-scope scans impossible by construction.
  2. 2. Why use port 0 when starting the test servers?

    Port 0 asks the OS to assign any free port.
  3. 3. Why limit concurrency in a scanner?

    A cap and rate limit keep scans polite and safe.
  4. 4. What does diffing two scan runs reveal?

    Changes between runs are often the most interesting findings.

Summary

  • You built a scanner with a hard-coded scope guard, banner grabbing, version parsing and a policy check.
  • Fake local services make a safe, repeatable test target.
  • Concurrency limits, timeouts and rate limits keep a scanner polite.
  • Diffing runs turns snapshots into change detection.

Next: Project: A Log Analyzer for Scan and Brute-Force Detection

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ị.