Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 37 ยท Projects and Practice

Project: Scanning Your Own Loopback Ports

Learn how port scanning works by building a small TCP connect scanner in Node.js that only targets the local loopback address, grabs banners and flags services missing from an allow-list.

Defenders scan their own systems to answer a simple question: what is actually listening? Anything you did not expect to see open is a candidate for closing, firewalling or investigating. Scanning is also a foundation of attacker reconnaissance, which is exactly why scanning without permission is illegal or against policy in many places. In this project you build the technique in the safest possible setting: a TCP connect scanner that is hard-wired to the loopback address 127.0.0.1, scanning servers that the same program starts itself.

Never point scanning code at systems you do not own or do not have written authorization to test. This lesson's scanner refuses any address other than 127.0.0.1 by design. Keep that guard if you extend it. For real assessments of your own networks use established tools, agree the scope in writing and schedule a window.

You will learn what a TCP connect actually tells you, how to scan many ports concurrently with timeouts, how to grab service banners, and how to compare results with an allow-list as in vulnerability management. The code runs on the site's Node.js runner, which supports loopback networking.

How a TCP connect scan works

A connect scan asks the operating system to open a TCP connection to each port and watches what happens:

Result Meaning
Connection succeeds The port is open: some process is accepting connections
Connection refused (RST) The port is closed: the host answered, nobody listens
No answer before the timeout The port is filtered: a firewall is probably dropping packets
sequenceDiagram
    participant Sc as Scanner
    participant T as Target port
    Sc->>T: SYN
    alt open
        T->>Sc: SYN-ACK
        Sc->>T: ACK (connection established)
        Sc->>T: close
    else closed
        T->>Sc: RST (refused)
    else filtered
        Note over Sc,T: no reply, scanner times out
    end

Half-open "SYN" scans stop before the final ACK; they need raw sockets and privileges, so connect scans (what ordinary programs can do) are simplest.

Scans are noisy: every probe is a log line on the target and a firewall DENY in many designs. That noise is the reason a port-scan detector works, and the reason to scan your own estate with the security team's knowledge.

Step 1: a single-port check with a timeout

Node.js Check one port on loopback

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.

The first port is open because we just started a server on it; port 1 is almost certainly closed. Port numbers for the first one differ on each run because we let the OS choose a free port.

Step 2: scan a range concurrently, grab banners, apply a guard

Scanning ports one by one would be slow. We use a small concurrency limit so we do not flood anything (politeness matters even on loopback). We start three tiny "services" on fixed ports with banners, scan a short range around them and read the banner of every open port.

Node.js A guarded loopback scanner with banner grabbing

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.

The scanner finds the three services and reads their banners, then the guard refuses a non-loopback target. Banner grabbing is how scanners guess the product and version (and thereby which vulnerabilities might apply). It is also why servers should avoid advertising exact version numbers where they can.

Step 3: turn results into an exposure report

A list of open ports means little without a policy. The next program compares scan results with what this host is supposed to expose and prints findings. It reuses simplified results so it runs without networking.

Node.js Compare findings with an allow-list

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.

The database listening on port 45103 is not on the allow-list, which is the sort of finding that matters most: forgotten test services and development databases left reachable are common causes of breaches.

What real scanners add

  • Scale and speed control: rate limits and parallelism tuned per network.
  • UDP scanning, which is harder (no handshake; closed ports return ICMP errors, open ones often stay silent).
  • Service and version detection using fingerprints beyond banners, and TLS inspection.
  • OS detection from TCP stack behavior.
  • Scripted checks for known weaknesses (only on authorized targets).
  • Authenticated scanning for accuracy (patching lesson).

How a defender spots scanning

Port scanning is easy to see from the receiving side: one source touching many ports in a short time. The log analyzer project counts distinct denied ports per source, and an IDS threshold rule does the same at line speed. Slow scans, spread over hours or many source addresses, require longer windows and correlation.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does a 'connection refused' response tell a scanner?

    A refusal (TCP RST) means the host answered and no service accepts connections on that port.
  2. 2. What does a timeout with no response usually suggest?

    Dropped packets produce silence; the scanner reports the port as filtered after its timeout.
  3. 3. Why does the project's scanner contain a guard that rejects other addresses?

    Unauthorized scanning is a legal and ethical problem; the guard keeps the exercise safe.
  4. 4. Why limit the scanner's concurrency?

    Controlled parallelism keeps scans polite and reliable.
  5. 5. A scan finds an open database port that is not in the allow-list. What is the best next step?

    Unexpected exposure must be explained and either removed, restricted or formally accepted.

Summary

  • A connect scan classifies ports as open, closed or filtered by how the TCP connection attempt ends.
  • Concurrency limits and timeouts keep scans fast and polite; banners help identify services.
  • Comparing results with an allow-list turns raw output into findings.
  • Scanning is noisy and detectable, and requires authorization on anything you do not own; this project is limited to loopback.
  • Defenders use scans to find unexpected exposure and logs to detect scans against them.

Next: Practice: Network Security Questions and Scenarios