Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 37 ยท Auxiliary and Scanning

Port Scanning with Auxiliary Modules

Understand how TCP connect and SYN port scans work, how Metasploit's portscan modules and db_nmap report results, and build a loopback scanner in Node.js.

A port scan asks a host, "which network services are you listening on?" It is the map-making step of an assessment: you cannot judge exposure until you know what is open. Metasploit offers its own scanners under auxiliary/scanner/portscan/ and can also run Nmap and import the results.

You will learn how scanning works at the TCP level, how to read the results, and how to build a safe scanner against your own loopback address.

Scan only systems you own or are allowed to test. The runnable example here connects to 127.0.0.1, your own machine's loopback, and nothing else.

How TCP scanning works

A TCP connection starts with a three-way handshake: SYN, SYN-ACK, ACK. Scanners exploit the different answers a port gives.

Port state What the target does What the scanner sees
Open A service accepts the connection SYN-ACK (or successful connect)
Closed Nothing listens; the OS answers RST (connection refused)
Filtered A firewall drops the packet No answer, timeout
sequenceDiagram
    participant S as Scanner
    participant T as Target
    S->>T: SYN to port 80
    T-->>S: SYN-ACK (open)
    S->>T: ACK (connect scan completes)
    S->>T: SYN to port 81
    T-->>S: RST (closed)

Open ports answer with SYN-ACK; closed ports answer with RST.

A connect scan completes the handshake using the normal operating-system call, so it needs no special rights and is easy to log. A SYN scan sends raw packets and stops after the SYN-ACK, which needs elevated rights and leaves fewer application logs but is still detected by firewalls and intrusion detection.

Metasploit's scanners

TEXT
msf6 > use auxiliary/scanner/portscan/tcp
msf6 auxiliary(scanner/portscan/tcp) > set RHOSTS 192.0.2.5
msf6 auxiliary(scanner/portscan/tcp) > set PORTS 1-1024
msf6 auxiliary(scanner/portscan/tcp) > set THREADS 10
msf6 auxiliary(scanner/portscan/tcp) > run
[+] 192.0.2.5:22 - TCP OPEN
[+] 192.0.2.5:80 - TCP OPEN
[*] Scanned 1 of 1 hosts (100% complete)

Other modules in the same folder include syn (raw SYN scan), ack (maps firewall rules), and xmas. Inside the console you can also call Nmap and have the output stored automatically:

TEXT
msf6 > db_nmap -sV -p 1-1024 192.0.2.5
msf6 > services
Services
========
host       port  proto  name  state  info
----       ----  -----  ----  -----  ----
192.0.2.5  22    tcp    ssh   open   OpenSSH 9.6
192.0.2.5  80    tcp    http  open   Apache httpd 2.4.58

services -p 80 filters, and services -c port,name -o ports.csv exports. These tables feed later steps (Service Version Scanners).

Build a loopback scanner

The block below starts three tiny servers on random loopback ports, then scans a range of ports on 127.0.0.1 with a TCP connect probe and reports what it found. It uses Node's standard net module, so it runs on the site's Node.js runner.

Node.js Connect-scan your own 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.

Port numbers are random each run, which is why the output reports counts and a match test rather than the numbers themselves.

Reading results and avoiding false conclusions

  • "Closed" means the host answered, so it is alive. "Filtered" means something dropped the traffic; the host may or may not exist.
  • A single open port does not prove which service runs there; check the banner (next lesson).
  • Scans are a snapshot. Services start and stop, so repeat them.
  • UDP scanning is slower and less reliable because silence can mean open or filtered.
Python Interpret scan states

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 output shows the point of comparing a scan with an intended list: port 3306 (a database) is open and nobody planned it.

Defence

  • Expose only what the business needs and close everything else.
  • Use host and network firewalls with default deny.
  • Detect scans: many connections from one source to many ports within seconds.
  • Run your own scans regularly and diff the results against an approved list.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a RST reply to a SYN indicate?

    A closed port on a live host answers with RST; silence usually means filtered.
  2. 2. Which scan needs raw packets and elevated rights?

    A SYN scan crafts packets itself; a connect scan uses the operating system's normal connect call.
  3. 3. What does db_nmap do?

    db_nmap wraps Nmap and imports hosts and services into the current workspace.
  4. 4. Why compare a scan with an intended-ports list?

    Differences between what is open and what is approved are findings.

Summary

  • Open, closed and filtered ports answer differently to a TCP probe.
  • Connect scans are simple; SYN scans are quieter but need raw access.
  • Metasploit has native scanners and wraps Nmap with db_nmap.
  • Results go into hosts and services; compare them with an approved list.
  • Scan only what you are authorized to scan.

Next: Service Version Scanners