# Port Scanning
> Enumerating which TCP/UDP services an asset exposes, passively from internet-scan databases or actively with SYN/CONNECT/UDP probes, then fingerprinting what answers. It is the step that turns hostnames into reachable attack surface beyond 80/443.
## How it works
In a recon pipeline port scanning sits between resolution and HTTP probing: names/IPs → open ports → `httpx` for web services → fingerprint and vuln-scan ([[Reconnaissance]], [[Asset Discovery]]). There are two modes (YesWeHack Recon #4):
- **Passive**: query Shodan, Censys or SecurityTrails for historically observed ports. This sends no packets and surfaces once-exposed services and forgotten, unmonitored hosts. Example: `shodan search "hostname:example.com"`. naabu has `-passive` built in (Shodan InternetDB).
- **Active**: SYN/CONNECT/UDP probes find services that aren't indexed, ports open only to some source ranges, and short-lived services. It is more complete but trips IDS and rate limits.
Scope gate: port scanning is usually allowed on self-hosted, company-owned ranges and rarely on cloud/SaaS-hosted apps or web-only scopes, so check the program rules first (YesWeHack).
## Techniques / commands
### Fast discovery: naabu, masscan, RustScan
```bash
naabu -host target.com # default: nmap top-100, -s c (CONNECT)
naabu -list hosts.txt -top-ports 1000 # -tp full|100|1000
naabu -p - -exclude-ports 80,443 -host t.com # all ports minus some
naabu -p 80,443,21-23,u:53 -host t.com # u: prefix = UDP; -uP sends nmap UDP probes
echo AS14421 | naabu -p 80,443 # ASN/CIDR input
naabu -l hosts.txt -ec -cdn # -ec: only 80/443 on CDN/WAF IPs; -cdn: show CDN
naabu -host t.com -sa # scan all IPs behind the DNS record
naabu -host t.com -sV # built-in version detection via nmap-service-probes
naabu -l hosts.txt -oA out # json + nmap XML + gnmap
echo t.com | naabu -silent | httpx -silent # ports -> live web services
echo t.com | naabu -nmap-cli 'nmap -sV -oX nmap-output'
```
Notes from the naabu README: `-rate` (default 1000 pps) trades speed for false positives. `-s` picks SYN vs CONNECT. IPv4 and IPv6 are both on by default (`-iv 6` for IPv6 only), and DNS-derived IPs are deduplicated automatically. `-exclude-cdn` matters at scale: full-range scans of Cloudflare/Akamai edges waste time and produce noise.
- **masscan** sends asynchronous SYNs for whole-range sweeps: `masscan -p1-65535 --rate=10000 <target>`. It is less thorough, so confirm hits with nmap (YesWeHack).
- **RustScan** scans all 65k ports in about 3s at its fastest, then pipes open ports straight into nmap automatically. Its scripting engine (Python/Lua/Shell) can trigger follow-ups such as `smb-enum` when SMB is open. Adaptive timing improves over runs.
### Depth: nmap
| Need | Flags |
|---|---|
| SYN (half-open) / full CONNECT | `-sS` / `-sT` (when SYN is filtered) |
| UDP (DNS, SNMP, IKE) | `-sU -p 53,161,500`. Responses are ambiguous and many services answer only protocol-specific probes, so add retries/rate limits |
| All ports / fast / top-N | `-p-` / `-F` / `--top-ports N` |
| Versions | `-sV`, `--version-intensity 0-9`, `--version-all` |
| OS | `-O` |
| Host discovery | `-sn` (ping sweep), `-Pn` (skip discovery), `-PE/-PP/-PM` ICMP echo/timestamp/netmask |
| Timing | `-T0..5`, `--min-rate/--max-rate`, `--min/max-parallelism` |
| Input / output | `-iL targets.txt`, `--excludefile`, `-oN/-oG/-oX` |
| NSE (web) | `--script http-title -p 80,443,8080` and other `http-*` scripts for service and vuln checks |
hakluke's point: a bare `nmap host` covers only the top 1000 TCP ports with no version or script output. Use `-p-` plus `-sV`/NSE.
Banner grab without nmap: `echo -e "\n" | nc -v <ip> 22`.
### IDS / firewall evasion (only where the program permits)
- Decoys: `nmap -sS -D RND:10 -p 80,443 target`
- Slow down: `--scan-delay 1s`, `-T0..2`
- Fragment: `-f`. This beats naive IDS that don't reassemble packets.
- Idle/zombie scan: `-sI ZOMBIE_IP`. Spoofed source: `-S`.
### What to look for once ports are open
Ports that should almost never face the internet: 3389 (RDP), 22 with password auth, 3306/5432 (MySQL/Postgres), 6379 (Redis), 9200 (Elasticsearch), and 8080/8443 (admin panels, dev servers). These are usually a temporary rule that became permanent (dev.to). Self-hosted AI tooling adds 8888 (Jupyter), 5000 (MLflow), 4000 (LiteLLM), 11434 (Ollama), 8188 (ComfyUI), 7860 (Langflow/Gradio) and 8265 (Ray). Over 31 days of honeypot data, 48,688 addresses hit 8888, 4,388 tried to start kernels, and 3,173 dumped environment variables for API keys. Opportunistic scanners already sweep these ports, so finding them first matters. Non-standard HTTP ports feed directly into [[Content Discovery]] and [[Subdomain Takeover]] checks.
## Related
- [[Reconnaissance]] (hub)
- [[Asset Discovery]] · [[Subdomain Enumeration]] · [[OWASP Amass]] · [[Content Discovery]] · [[Subdomain Takeover]]
- [[Bug Bounty Automation]] · [[AI Infrastructure Vulnerabilities]] · [[Domain and Infrastructure OSINT]]
## Sources
- [[raw/yeswehack.com/recon-series-4-port-scanning-methods-yeswehack-4438|Recon series #4: Port scanning methods (YesWeHack)]]
- [[raw/github.com/naabu-fast-go-port-scanner-projectdiscovery-4437|naabu: Fast Go port scanner (ProjectDiscovery)]]
- [[raw/highon.coffee/naabu-cheat-sheet-commands-examples-highon-coffee-4436|Naabu Cheat Sheet (HighOn.Coffee)]]
- [[raw/thecyberaryan.github.io/naabu-zero-to-hero-guide-cyber-aryan-4434|Naabu Zero to Hero Guide]]
- [[raw/github.com/github-rustscan-rustscan-the-modern-port-scanner-6050|RustScan: The Modern Port Scanner]]
- [[raw/hakluke.medium.com/haklukes-guide-to-nmapport-scanning-is-just-the-beginning-6094|Hakluke's Guide to Nmap]]
- [[raw/hacklido.com/the-ultimate-guide-to-port-scanning-using-nmap-nmap-notes-6067|The Ultimate Guide to Port Scanning using Nmap]]
- [[raw/medium.com/a-deep-dive-into-nmap-scripts-for-web-application-testing-by-khaleel-k-6041|A Deep Dive into Nmap Scripts for Web Application Testing]]
- [[raw/dev.to/the-riskiest-server-is-the-one-nobody-remembers-a-checklist-for-forgot-13627|The riskiest server is the one nobody remembers]]
- [[raw/medium.com/who-scans-ai-infrastructure-ollama-jupyter-mlflow-data-15095|Who scans AI infrastructure? Ollama, Jupyter, MLflow data]]