01 / BUILD A TCP PORT SCANNER · LESSON 04

Explain observed exposure

Compare recorded scan outcomes with service policy without inventing certainty.

22 min · learn at your pace

An open port needs context

A successful TCP connection says that an endpoint accepted a connection at the time of observation. It does not identify the application, prove that authentication is safe or establish whether the service should be exposed. Compare the observation with a separate policy instead of labelling every open port a vulnerability.

This exercise joins by both address and port. The policy says whether an open service is expected. A recorded open result can then become expected_open or unexpected_open. A recorded refusal can establish a mismatch for an expected service, but it still does not explain why that service was unavailable.

Unknown is an honest finding

Timeouts, collection errors, missing observations and missing policy all remain unknown. A timeout must never be reported as a successful firewall restriction merely because no connection completed. Similarly, missing policy does not imply that a service is allowed or forbidden.

Keep the observation and assessment separate in a larger report. That allows a reviewer to change policy without pretending that the underlying evidence changed. Deterministic sorting makes two assessments easier to compare. A scan is one source of evidence: use authenticated configuration, application checks and operational context before claiming a confirmed security problem. The following modules strengthen the execution and reporting boundaries around this small scanner.

Try an example.sh
awk -F '\t' 'NR>1 {print $1 ":" $2 " expects open=" $3}' policy.tsv
YOUR TURN

Put it into practice.

policy.tsv has unique address<TAB>port<TAB>expected_open entries, yes/no. scan.tsv has unique address<TAB>port<TAB>state observations. Print address<TAB>port<TAB>finding, sorted by the complete data row under LC_ALL=C. For known policy: open maps to expected_open (yes) or unexpected_open (no); refused maps to expected_service_unavailable (yes) or expected_refusal (no). Every other state and every missing policy maps to unknown. Retain every observed row; do not invent rows for policy-only endpoints.

See an explained solution
One way to solve it.sh
export LC_ALL=C
printf 'address\tport\tfinding\n'
awk -F '\t' 'FILENAME==ARGV[1]{if(FNR>1)expected[$1 SUBSEP $2]=$3;next}
FNR>1 {
 key=$1 SUBSEP $2;finding="unknown"
 if(key in expected){
  if($3=="open")finding=(expected[key]=="yes" ? "expected_open" : "unexpected_open")
  else if($3=="refused")finding=(expected[key]=="yes" ? "expected_service_unavailable" : "expected_refusal")
 }
 printf "%s\t%s\t%s\n",$1,$2,finding
}' policy.tsv scan.tsv | sort

The report keeps uncertainty explicit and interprets observations only when policy exists. A positive connection is evidence of exposure, while a timeout or missing policy cannot establish whether the endpoint meets expectations.