Port scanning and host discovery (only on your own network)
Host discovery finds which devices are online, and port scanning finds which services are open on them. Every open port is an entry point, so admins scan their own networks before an attacker does. Scan only networks you own or have written permission for.
73.1 Why is it used?
- Inventory: know which devices are on the network.
- Every open port is an entry point; a port you do not know about will be found by the attacker first.
- Scanning is also an attacker's first step, so seeing your network the same way helps you defend it.
- Version info (-sV) feeds patching and vulnerability checks.
73.2 Host discovery: who is online
- On the same LAN nmap uses ARP: an ARP who-has broadcast for each IP.
- Every live device must answer ARP, so this is fast and reliable on a LAN.
- nmap -sn does only host discovery, no port scan.
- Step 1
1.Scanner 192.168.93.5 sends ARP who-has as a broadcast to the switch
- Step 2
2.The switch floods it to every device on n073-lan
- Step 3
3.Server .10, DB .20 and PC .30 send ARP replies with their MACs
- Step 4
4.No device at .40: no reply (nmap asks twice)
- Step 5
5.nmap lists .10, .20, .30 as hosts up
nmap -sn 192.168.93.0/24nmap -sn -n --packet-trace 192.168.93.10,40$ nmap -sn 192.168.93.0/24Nmap scan report for 192.168.93.1Nmap scan report for n073-srv.n073-lan (192.168.93.10)MAC Address: 5A:B4:3A:49:16:EB (Unknown)Nmap scan report for n073-db.n073-lan (192.168.93.20)MAC Address: 3A:97:FA:89:2F:3F (Unknown)Nmap scan report for n073-pc.n073-lan (192.168.93.30)MAC Address: F2:3B:3A:24:AC:C5 (Unknown)Nmap scan report for scan (192.168.93.5)Nmap done: 256 IP addresses (5 hosts up) scanned in 2.42 seconds$ nmap -sn -n --packet-trace 192.168.93.10,40SENT (0.0261s) ARP who-has 192.168.93.40 tell 192.168.93.5SENT (0.0262s) ARP who-has 192.168.93.10 tell 192.168.93.5RCVD (0.0262s) ARP reply 192.168.93.10 is-at 5A:B4:3A:49:16:EBSENT (1.1277s) ARP who-has 192.168.93.40 tell 192.168.93.5Nmap scan report for 192.168.93.10Host is up (0.000026s latency).Nmap done: 2 IP addresses (1 host up) scanned in 1.49 seconds- The 5 hosts include the Docker gateway (.1) and the scanner itself (.5).
73.3 TCP connect scan (-sT)
- Uses the OS's normal connect() call, so it needs no root.
- SYN -> SYN-ACK (port open) -> ACK completes the handshake -> RST closes it at once.
- The full connection shows up in server logs; slower than a SYN scan.
tcpdump -nn -t -i eth0 'host 192.168.93.10 and tcp port 22'nmap -n -sT -p 22,80,443 192.168.93.10$ nmap -n -sT -p 22,80,443 192.168.93.10PORT STATE SERVICE22/tcp open ssh80/tcp open http443/tcp closed https$ tcpdump (port 22, trimmed)IP 192.168.93.5.52818 > 192.168.93.10.22: Flags [S], seq 4139464643IP 192.168.93.10.22 > 192.168.93.5.52818: Flags [S.], seq 3381548738, ack 4139464644IP 192.168.93.5.52818 > 192.168.93.10.22: Flags [.], ack 1IP 192.168.93.5.52818 > 192.168.93.10.22: Flags [R.], seq 1, ack 173.4 SYN scan (-sS): half open
- nmap crafts a raw SYN; on SYN-ACK it sends RST instead of ACK, so the connection never completes.
- Needs root/admin (raw packets) but is fast.
- Only 3 packets in the capture; the SYN has win 1024 and only the MSS option because nmap built it, not the OS.
nmap -n -sS -p 22,80,443 192.168.93.10$ nmap -n -sS -p 22,80,443 192.168.93.10PORT STATE SERVICE22/tcp open ssh80/tcp open http443/tcp closed https$ tcpdump (port 22)IP 192.168.93.5.56873 > 192.168.93.10.22: Flags [S], seq 399738790, win 1024, options [mss 1460], length 0IP 192.168.93.10.22 > 192.168.93.5.56873: Flags [S.], seq 2194417328, ack 399738791, win 64240, options [mss 1460], length 0IP 192.168.93.5.56873 > 192.168.93.10.22: Flags [R], seq 399738791, win 0, length 0- Step 1
1.Scanner sends a raw SYN to server port 22
- Step 2
2.Server answers SYN-ACK: port open
- Step 3
3.Scanner sends RST instead of ACK: handshake never completes
- Step 4
4.SYN to closed port 443: the server itself replies RST -> closed
- Step 5
5.SYN to DB 3306: the firewall silently drops it, no reply -> filtered
73.5 Port states: open, closed, filtered
| State | TCP reply | UDP reply | Meaning |
|---|---|---|---|
| open | SYN-ACK | A UDP response (e.g. NTP reply) | A service is listening |
| closed | RST | ICMP port unreachable | Host is up, nothing listens |
| filtered | No reply | - | A firewall drops the probe |
| open|filtered | - | No reply | Silence: could be a service or a firewall |
nmap -n -sS -p 3306,5432 --reason 192.168.93.20$ nmap -n -sS -p 3306,5432 --reason 192.168.93.20Host is up, received arp-response (0.000088s latency).PORT STATE SERVICE REASON3306/tcp filtered mysql no-response5432/tcp closed postgresql reset ttl 64# on n073-db: ss -tlpnLISTEN 0 5 0.0.0.0:3306 0.0.0.0:* users:(("socat",pid=7,fd=5))- --reason prints why nmap chose each state.
- Filtered does not mean no service: an app really listens on 3306, nftables just hides it.
73.6 UDP scan (-sU)
- UDP has no handshake: open = a reply, closed = ICMP port unreachable, silence = open|filtered.
- Slow on real networks: Linux rate-limits ICMP errors and nmap retries every silent port.
nmap -n -sU -p 53,123 --reason 192.168.93.10nmap -n -sU -p 161 --reason 192.168.93.20$ nmap -n -sU -p 53,123 --reason 192.168.93.10PORT STATE SERVICE REASON53/udp closed domain port-unreach ttl 64123/udp open ntp udp-response ttl 64$ nmap -n -sU -p 161 --reason 192.168.93.20PORT STATE SERVICE REASON161/udp open|filtered snmp no-response73.7 Version detection (-sV)
- The port number alone does not prove the service; any app can run on any port.
- -sV connects, reads banners and sends probes to identify the software and version.
nmap -n -sV -p 22,80 192.168.93.10nmap -n -sU -sV -p 123 192.168.93.10$ nmap -n -sV -p 22,80 192.168.93.10PORT STATE SERVICE VERSION22/tcp open ssh OpenSSH 9.7 (protocol 2.0)80/tcp open http SimpleHTTPServer 0.6 (Python 3.12.3)$ nmap -n -sU -sV -p 123 192.168.93.10PORT STATE SERVICE VERSION123/udp open ntp NTP v4 (primary server)73.8 Comparison: connect scan vs SYN scan
| Point | Connect scan (-sT) | SYN scan (-sS) |
|---|---|---|
| Handshake | Full 3-way, then RST | Half open: SYN, SYN-ACK, RST |
| Root needed | No (OS connect call) | Yes (raw packets) |
| Speed | Slower | Faster |
| Server logs | Connection is logged | Often not logged by the app, but firewalls/IDS see every SYN |
| Packets per open port | 4 | 3 |
73.9 Legal and ethical limits
- Scan only your own network or one with written permission that states scope and time.
- Office or college network: get IT team approval first; otherwise it is treated as preparation for an attack.
- In India, accessing a computer system without permission is an offence under the IT Act.
- Practise in your own Docker lab; scanme.nmap.org (Nmap project) allows light scans.
73.10 Common mistakes
- Believing a SYN scan is invisible: modern firewalls/IDS log every SYN and alert on 100 ports from one IP in seconds, and can block the scanner.
- Reading filtered as 'no service': the port can be open behind a firewall.
- Reading UDP open|filtered as open: silence is ambiguous.
- Trusting the port number for the service name instead of running -sV.
73.11 Interview tips
- Connect scan: full handshake, no root, slower, visible in logs. SYN scan: half open, fast, needs root.
- SYN answered by RST = closed (host refused); no reply at all = filtered (firewall drops).
- UDP: reply = open, ICMP unreachable = closed, silence = open|filtered.
- On a LAN, host discovery uses ARP; always mention scanning only with permission.