03 / BUILD A TCP PORT SCANNER · LESSON 12

Review service exposure without overclaiming

Compare scanner observations with an expected-listener inventory and preserve uncertainty.

15 min · learn at your pace

A connection result needs context

An open port can be expected, unexpected, or irrelevant to the review. Compare observations with an explicit expected-listener list keyed by computer and port. Joining on a port alone would incorrectly treat every host as having the same approved services.

Use five report categories. ExpectedOpen means a required endpoint accepted a connection. UnexpectedOpen means it was not on the expected list. ExpectedUnavailable means a required endpoint explicitly refused the connection. NoListenerExpected means a non-required endpoint refused it. Unknown preserves TimedOut and Error regardless of whether the endpoint was expected.

Keep the claim narrow

ExpectedOpen is not proof of application health or authenticated access. An open port 5985 does not mean PowerShell Remoting will succeed. Conversely, a timeout does not establish that a service is absent. This distinction supports all three roles: exposure review, service checks, and network troubleshooting.

Report the observations you actually have

This helper returns one finding per supplied observation. It does not manufacture missing probes. A complete audit first constructs the intended plan, ensures each pair receives a result, then joins that result set with expectations. The capstone will use that sequence.

The checks change the host, port, and expected-listener set to catch an overly broad join. They also include empty observation arrays and all four probe statuses. Keep the output as objects so another function can export it or group it by Finding.

Try an example.ps1
Import-Module '/home/user/LabFleet.psm1' -Force
Test-LabTcpPort app01 -Port 5985 | Select-Object ComputerName,Port,Status
# An open TCP port is not an authenticated remoting session.
YOUR TURN

Put it into practice.

Define Compare-ServiceExposure(Observation object[], Expected object[]). Expected rows contain ComputerName and Port. Return one object per observation with ComputerName, Port, and Finding: ExpectedOpen, UnexpectedOpen, ExpectedUnavailable, NoListenerExpected, or Unknown. TimedOut/Error are always Unknown; otherwise compare both computer and port.

See an explained solution
One way to solve it.ps1
function Compare-ServiceExposure {
    param([object[]]$Observation,[object[]]$Expected)
    foreach($row in $Observation) {
        $wanted=@($Expected | Where-Object { $_.ComputerName -eq $row.ComputerName -and $_.Port -eq $row.Port }).Count -gt 0
        $finding=if($row.Status -in @('TimedOut','Error')) { 'Unknown' }
            elseif($row.Status -eq 'Open' -and $wanted) { 'ExpectedOpen' }
            elseif($row.Status -eq 'Open') { 'UnexpectedOpen' }
            elseif($wanted) { 'ExpectedUnavailable' } else { 'NoListenerExpected' }
        [pscustomobject]@{ComputerName=$row.ComputerName;Port=$row.Port;Finding=$finding}
    }
}

A two-field join keeps policy specific to each endpoint. Unknown outcomes stay unknown, and successful TCP connections are described without claiming application or remoting success.