Port Scanning

The process of inspecting TCP or UDP ports on a remote machine with the intention of detecting what services are running on the target and what potential attack vectors may exist.

The simplest TCP port scanning technique, usually called CONNECT scanning, relies on the three-way TCP handshake mechanism. In basic terms, a host sends a TCP SYN packet to a server on a destination port. If the destination port is open, the server responds with a SYN-ACK packet and the client host sends an ACK packet to complete the handshake. If the handshake completes successfully, the port is considered open.

We can demonstrate that simply by using Netcat, although it is not considered to be a scanning tool but because of its simplicity and provides an option to check only if the port is opened using -z , we will use it against scanme.nmap.org

With Netcat

TCP

nc -vvz scanme.nmap.org 20-30 # We can use the options also in -vv -z
scanme.nmap.org [45.33.32.156] 30 (?) : Connection refused
scanme.nmap.org [45.33.32.156] 29 (?) : Connection refused
scanme.nmap.org [45.33.32.156] 28 (?) : Connection refused
scanme.nmap.org [45.33.32.156] 27 (?) : Connection refused
scanme.nmap.org [45.33.32.156] 26 (?) : Connection refused
scanme.nmap.org [45.33.32.156] 25 (smtp) : Connection refused
scanme.nmap.org [45.33.32.156] 24 (?) : Connection refused
scanme.nmap.org [45.33.32.156] 23 (telnet) : Connection refused
scanme.nmap.org [45.33.32.156] 22 (ssh) open
scanme.nmap.org [45.33.32.156] 21 (ftp) : Connection refused
scanme.nmap.org [45.33.32.156] 20 (ftp-data) : Connection refused
 sent 0, rcvd 0

The default is to connect on the ports using TCP, we can see also that it resolves the service name depending on the port.

UDP


With NMAP

Nmap is one of the most popular, versatile, and robust port scanners available. It has been actively developed for over two decades and offers numerous features beyond port scanning. Some of the Nmap example scans we’ll cover in this Module are run using sudo. This is because quite a few Nmap scanning options require access to raw sockets, which in turn require root privileges. Raw sockets allow for surgical manipulation of TCP and UDP packets. A default Nmap TCP scan will scan the 1000 most popular ports on a given machine.

TCP

  • Default

  • Specified Ports

  • SYN Scan

    • SYN scanning is a TCP port scanning method that involves sending SYN packets to various ports on a target machine without completing a TCP handshake. If a TCP port is open, a SYN-ACK should be sent back from the target machine, informing us that the port is open. At this point, the port scanner does not bother to send the final ACK to complete the three-way handshake.

  • TCP Connect Scan

    • As discussed at the begining of the section, thi scan goes through the three-way handshake

UDP

  • Default

  • With TCP

    • You can run both TCP and UDP scan at the same time but you can't specify ports.

Common Nmap Options

Option / Switch
Function

-sS

TCP SYN Scan

-sT

TCP Connect Scan

-sU

UDP Scan

-sC

Default scripts

-sV

Get service version

-O

OS Scan - OS Guessing

-A

Aggressive mode, it does; default scripts, get service version, traceroute,

OS Guessing


With PowerShell

  • Using Test-NetConnection

  • Using Net.Sockets.TcpClient

    • This scan is slow so try to specify what ports you are looking for

Last updated

Was this helpful?