Port scanning

Nmap

The Network Mapper - Free Security Scanner Nmap

Common options

option
description
-p
Port ranges (default: most common 1000 ports), -p- all ports (1-65536)
-sV
Version scanning, enforces full TCP connection
-sC
Script scanning
-O
OS detection
-A
Aggressive scan options, combines: -O, -sV, -sC, --traceroute
-T<0-5>
Set timing template (higher is faster), aka paranoid|sneaky|polite|normal|aggressive|insane (default: 3, aka normal)
-oA
Basename (Output to all formats)
-sU
UDP scan
-Pn
Skip host discovery, use this option if the host is blocking ping probes.
-v -vv
Increase verbosity
-d -dd
Debug, even more verbose
--max-retries
set to 1 or 0 to considerably speed up scan (but gets a bit less accurate)
Note: There are a ton of other options, like more exotic scan types, spoofing IP or MAC addresses, etc.

TCP

Note: Nmap can only perform TCP SYN scans (-sS), when running as privileged user (root or sudoer), otherwise it falls back to full TCP 3-way handshakes (-sT) per default.

Examples

Light scan
1
sudo nmap --top-ports 10 --open -oA nmap_light <ip>
Copied!
Default scan
1
sudo nmap -sV -oA nmap_default <ip>
Copied!
Heavy scan
1
sudo nmap -p- -sV -sC --reason -oA nmap_heavy <ip>
Copied!
Scan for "safe" vulns
1
sudo nmap --script "vuln and safe" -oA nmap_vuln <ip>
Copied!
Scan for SMB vulns
1
sudo nmap -p 139,445 --script smb-vuln* -oA nmap_vuln_smb <ip>
Copied!

UDP

1
sudo nmap -sU -oA nmap_udp <ip>
Copied!

Useful stuff

Get open ports comma separated
1
grep -oP '\d{1,5}/open' nmap.gnmap | cut -d "/" -f 1 | paste -s -d ','
Copied!

Nmap scripts

Quicker scanning and smarter identification

vulners

NSE script based on Vulners.com API

nmapAutomator

A script that you can run in the background!
1
./nmapAutomator <ip> All
Copied!

netcat (nc)

TCP/IP swiss army knife

TCP

Connect scan (only validates if ports are open)
1
nc -nvv -w 1 -z <ip> <port-range>
Copied!

UDP

1
nc -nv -u -z -w 1 <ip> <port-range>
Copied!
No response means port is open, otherwise a ICMP packet port unreachable is sent back. BUT: Response may get dropped (firewalls, routers, ...) -> false positive

Port knocking

In computer networking, port knocking is a method of externally opening ports on a firewall by generating a connection attempt on a set of prespecified closed ports.
-- Wikipedia
1
knockd
2
/etc/knockd.conf
Copied!
Actually knock to a port
1
nmap -Pn -sT --host_timeout 201 --max-retries 0 -p <port> <ip>
Copied!
or
1
echo "" > /dev/tcp/<ip>/<port>
Copied!

Simple bash port scanner

1
for i in {1..65535}; do (echo > /dev/tcp/<ip>/$i) >/dev/null 2>&1 && echo $i is open; done
Copied!