Sub topic 17
Non-standard ports and why port numbers are not proof of protocol
This chapter assumes you know the well-known, registered and dynamic port ranges, the difference between TCP and UDP, and how to list and test ports from the command line. It also assumes you can read a short Python script.
A port number is a convention
A default port is an agreement, not a rule enforced by the network. The operating system only checks that a program has bound to a port. It does not check what the program says once a client connects. Any program can listen on any free port, and any client can connect to any port that is open.
Operators move services off their defaults for ordinary reasons:
- Two web servers run on one host, so one uses 8080 or 8443.
- A developer runs a test service without root privileges. On Linux, binding to ports below 1024 normally needs elevated rights.
- An administrator moves SSH from 22 to something else to reduce the noise from automated login attempts.
- A service sits behind a reverse proxy or a port-forwarding rule that maps one external port to another internal one.
Attackers also use unexpected ports. Malware often tunnels traffic over 443 or 53 because firewalls usually allow those ports.
Same bytes, different port
The code below shows that the port does not change the protocol. It starts a tiny HTTP server on port 8081, then speaks HTTP to it with a raw socket, just as the web example did on port 80.
import socket
import threading
from http.server import BaseHTTPRequestHandler, HTTPServer
class Handler(BaseHTTPRequestHandler):
def do_HEAD(self):
self.send_response(200)
self.end_headers()
def log_message(self, *args):
pass
server = HTTPServer(("127.0.0.1", 8081), Handler)
threading.Thread(target=server.serve_forever, daemon=True).start()
request = (
"HEAD / HTTP/1.1\r\n"
"Host: localhost\r\n"
"Connection: close\r\n\r\n"
).encode("ascii")
with socket.create_connection(("127.0.0.1", 8081), timeout=5) as s:
s.sendall(request)
print(s.recv(1024).decode("latin-1"))
server.shutdown()The reply begins with HTTP/1.0 200 OK, so HTTP works on port 8081. Clients such as browsers and curl just need to be told the port, for example http://localhost:8081/.
Common errors
- Assuming the port names the protocol. An open port 443 may run HTTPS, but it may also run a VPN, a proxy or malware. An open port 2222 is often SSH, but nothing guarantees it.
- Writing firewall rules by protocol name only. A rule that allows "web traffic" by permitting port 80 does not stop other traffic from using port 80.
- Forgetting that both ends must agree. If a server moves to another port, every client, monitoring check and firewall rule must change too.
- Trusting the service name in tools. The name shown by
ssor a scanner comes from the services database, which maps numbers to names. It does not look at the traffic.
Finding out what is really running
To identify a service, look at what it does instead of where it listens. On your own host, ss -tulnp shows the owning process. For a remote host you are authorized to test, nc -v host port shows whether a banner appears, and nmap -sV probes the service to guess the protocol from its replies. Deep packet inspection tools in firewalls do the same thing on live traffic.
Exercise
A scan of your own server shows port 8443 open, and the services database labels it "pcsync-https". How can you confirm what is really listening, and why is the label not enough?
Show answer
Run ss -tulnp on the server to see which process owns the port, and probe it with nmap -sV or by connecting and reading the reply. The label comes from a number-to-name table, so it only records a common use of that number. It does not show what the listening program actually speaks.