Ports, Services and the Attack Surface
Every listening port is a door. Some doors are meant to be open (443 on a web server), some are open by accident (a database bound to all interfaces, a debug server left running), and an attacker's first job is to find out which is which. A defender's first job is the same, done earlier and from the inside: know every listener on every host, why it is there and who can reach it. Ports and sockets are explained in Ports & Sockets; this lesson is about inventorying them and reducing them.
Port ranges and the ports you should know
| Range | Name | Meaning |
|---|---|---|
| 0–1023 | well-known (system) | assigned by IANA; binding needs privilege on Unix |
| 1024–49151 | registered | assigned to applications (3306 MySQL, 3389 RDP, 5432 PostgreSQL) |
| 49152–65535 | dynamic / ephemeral | client source ports (Linux uses 32768–60999 by default) |
| Port | Service | Security property |
|---|---|---|
| 21 / 23 | FTP / Telnet | cleartext logins; replace (FTP, FTPS and SFTP, SSH versus Telnet) |
| 22 | SSH | encrypted; a brute-force magnet on the Internet |
| 25 / 587 / 465 | SMTP / submission / submissions | STARTTLS or implicit TLS (mail lesson) |
| 53 | DNS | UDP and TCP; reflection risk if open to the world |
| 80 / 443 | HTTP / HTTPS | the web; 80 should only redirect |
| 88 / 389 / 636 | Kerberos / LDAP / LDAPS | directory services, never on the Internet |
| 445 | SMB | file sharing, worm history; never on the Internet |
| 3389 | RDP | remote desktop; behind a VPN or gateway only |
| 3306, 5432, 6379, 9200, 27017 | MySQL, PostgreSQL, Redis, Elasticsearch, MongoDB | databases: frequent "left open" findings |
A port number is a convention, not a guarantee: anything can listen anywhere. That is why scanners identify services by talking to them (Nmap), not by their port numbers.
The address a service binds to
The most important column in a listener list is the local address:
127.0.0.1:portor[::1]:port: reachable only from the same host.- a specific interface address (
10.10.0.10:port): reachable on that network. 0.0.0.0:portor[::]:port(*in some tools): reachable on every interface, including ones added later.
Many "exposed database" incidents are a service that defaulted to 0.0.0.0 on a host that also had a public interface.
Inventory with ss
The session starts four services in the sandbox (a web server bound to all interfaces, an Ncat listener, a TLS server and a UDP socket; the sandbox only has loopback, so none of them is reachable from outside), then lists listeners the way you would on a server. -t TCP, -u UDP, -l listening, -n numeric, -p the owning process (process IDs removed for stable output).
python3 -m http.server 8000 --bind 0.0.0.0 >http.log 2>&1 &
ncat -lk 127.0.0.1 9000 >/dev/null 2>&1 &
openssl s_server -accept 127.0.0.1:8443 -cert $SAMPLES/pki/lab.test.crt -key $SAMPLES/pki/lab.test.key -quiet >/dev/null 2>&1 &
python3 -c "import socket, time; s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM); s.bind(('127.0.0.1', 5514)); time.sleep(5)" &
for i in $(seq 50); do [ "$(ss -Htln | wc -l)" -ge 3 ] && [ "$(ss -Hulni | wc -l)" -ge 1 ] && break; sleep 0.1; done
ss -Htulnp | awk '{printf "%-4s %-16s %s\n", $1, $5, $7}' | sed -E 's/,pid=[0-9]+,fd=[0-9]+//' | sort
echo "--- what Nmap's port table guesses:"
awk '$2 ~ /^(8000|9000|8443)\/tcp$|^5514\/udp$/ {print $2, $1}' /usr/share/nmap/nmap-services
kill %1 %2 %3 %4tcp 0.0.0.0:8000 users:(("python3"))
tcp 127.0.0.1:8443 users:(("openssl"))
tcp 127.0.0.1:9000 users:(("ncat"))
udp 127.0.0.1:5514 users:(("python3"))
--- what Nmap's port table guesses:
8000/tcp http-alt
8443/tcp https-alt
9000/tcp cslistener
The web server is the odd one out: 0.0.0.0:8000 means it would accept connections on every interface the host has. The other three are loopback-only. Notice too that a port table is a poor guide: Nmap's names are guesses based on old registrations and on what is commonly seen on each port, not on what actually runs here.
Detecting changes: a baseline
The useful question is not "what is listening?" but "what is listening that wasn't yesterday?". Store a reviewed baseline and compare. The program does it for two hosts' listener snapshots:
baseline = {
"web1": {("tcp", "0.0.0.0", 443), ("tcp", "0.0.0.0", 80), ("tcp", "127.0.0.1", 5432), ("tcp", "10.10.0.10", 22)},
"db1": {("tcp", "10.10.0.20", 5432), ("tcp", "10.10.0.20", 22)},
}
today = {
"web1": {("tcp", "0.0.0.0", 443), ("tcp", "0.0.0.0", 80), ("tcp", "0.0.0.0", 5432),
("tcp", "10.10.0.10", 22), ("tcp", "0.0.0.0", 9229)},
"db1": {("tcp", "10.10.0.20", 5432), ("tcp", "10.10.0.20", 22)},
}
RISKY = {5432: "PostgreSQL", 9229: "Node.js inspector (remote code execution if reachable)", 6379: "Redis", 3389: "RDP"}
for host in baseline:
new = today[host] - baseline[host]
gone = baseline[host] - today[host]
if not new and not gone:
print(f"{host}: no change")
continue
for proto, addr, port in sorted(new, key=lambda x: x[2]):
scope = "ALL interfaces" if addr in ("0.0.0.0", "::") else addr
note = RISKY.get(port, "")
print(f"{host}: NEW {proto}/{port:<5} on {scope:<15} {note}")
for proto, addr, port in sorted(gone, key=lambda x: x[2]):
print(f"{host}: GONE {proto}/{port:<5} on {addr}")web1: NEW tcp/5432 on ALL interfaces PostgreSQL
web1: NEW tcp/9229 on ALL interfaces Node.js inspector (remote code execution if reachable)
web1: GONE tcp/5432 on 127.0.0.1
db1: no change
Two findings on web1, both serious: the database moved from loopback to all interfaces (probably a configuration change), and a Node.js debugger is listening on every interface, which gives anyone who can reach it code execution on the host.
How exposure is detected
- From inside:
ss -tulnp(Linux),Get-NetTCPConnection -State Listen(Windows) or an agent, compared with a baseline. - From outside: an authorised scan of your own public ranges, regularly, compared with the list of services that are supposed to be public. The inside view tells you what listens, the outside view what is reachable through the firewalls.
- Flow logs showing inbound connections to unexpected ports.
How it is reduced
- Remove services that aren't needed (uninstall or disable, not just stop).
- Bind the rest to the narrowest address: loopback for local-only components, the internal interface for internal services.
- Filter: host firewall and network firewall rules that allow only the clients that need each port (default deny).
- Authenticate and encrypt every service that remains, and keep it patched.
- Review the baseline when changes are deployed, so a new listener is either approved or a finding.
Pitfalls
- Trusting the port number to identify the service, in either direction (SSH on 443, a web shell on 22).
- Checking TCP only. UDP services (DNS, SNMP, NTP, SSDP) are often forgotten and are reflection risks.
- Assuming a firewall protects a
0.0.0.0listener forever. Firewalls get changed; bind addresses are a second, independent layer.
Practice
1. A service shows [::]:6379 in ss. Who can reach it?
Every interface, IPv6 and (on Linux, unless IPV6_V6ONLY is set) IPv4 too, through IPv4-mapped addresses. Redis on all interfaces without authentication is a classic incident.
2. Extend the program so a listener that only changed its bind address is reported as "CHANGED" rather than one NEW and one GONE.
Index both snapshots by (proto, port); for each key present in both with a different address, print "CHANGED old → new"; then report the remaining keys as NEW or GONE. For web1 it reports tcp/5432 CHANGED 127.0.0.1 → 0.0.0.0.
3. Why might the inside view (ss) and the outside view (a scan) disagree?
Firewalls in between (a listener exists but is filtered), NAT and port forwards (the outside reaches a different host than you think), load balancers, or a scan that ran from the wrong place. Both views are needed.
Test yourself
What does a listener on 0.0.0.0:8000 mean?
- It is disabled
- It accepts connections on every IPv4 interface of the host
- It only accepts loopback connections
- It listens on port 0
Which port is SMB, which should never be reachable from the Internet?
- 22
- 445
- 443
- 123
In the baseline comparison, what changed for PostgreSQL on web1?
- It was removed
- It moved from 127.0.0.1 to all interfaces
- It moved to port 9229
- Nothing
Why don't scanners identify services by port number alone?
- Port numbers are encrypted
- Any service can listen on any port
- Ports above 1024 have no services
- IANA forbids it