Sub topic 12
DHCP, NTP, SNMP and Syslog
These four protocols run in the background of almost every network. DHCP hands out addresses, NTP keeps clocks in agreement, SNMP lets a management system query devices, and syslog collects log messages. All four default to UDP.
This chapter assumes you know what a port and a socket are, how TCP differs from UDP, and what ephemeral client ports are. You should also be able to read a short Python script and run commands in a terminal.
Summary of the ports
| Protocol | Port | Transport | Role of the port |
|---|---|---|---|
| DHCP (IPv4) | 67 | UDP | Server and relay agent listen |
| DHCP (IPv4) | 68 | UDP | Client listens |
| DHCPv6 | 546 / 547 | UDP | Client / server and relay |
| NTP | 123 | UDP | Servers listen, peers use it on both sides |
| SNMP | 161 | UDP | Agent receives queries |
| SNMP traps | 162 | UDP | Manager receives notifications |
| SNMP over (D)TLS | 10161 / 10162 | TCP or UDP | Secure agent / manager |
| Syslog | 514 | UDP | Traditional log delivery |
| Syslog | 514 | TCP | Reliable delivery, no encryption |
| Syslog over TLS | 6514 | TCP | Encrypted delivery |
DHCP: ports 67 and 68
Dynamic Host Configuration Protocol (DHCP) gives a host an IP address, subnet mask, default gateway, DNS servers and a lease time. The server listens on UDP 67. The client uses UDP 68.
DHCP is unusual because the client has no address yet. UDP is required, since TCP needs both sides to have usable addresses before it can connect. The client sends from 0.0.0.0:68 to the broadcast address 255.255.255.255:67.
The exchange has four steps, often called DORA.
Broadcasts do not cross routers. A router can run a relay agent (also called an IP helper) that receives the broadcast on UDP 67 and forwards it as unicast to a central DHCP server. The relay writes its own interface address into the packet's giaddr field, so the server knows which subnet needs an address.
DHCPv6 uses different ports (client 546, server 547). IPv6 hosts can also configure themselves with router advertisements, so DHCPv6 is not always needed.
Failure modes
- Firewall blocks UDP 67/68. Clients fall back to a link-local address (
169.254.x.x) on IPv4. - Rogue DHCP server. Any device answering on UDP 67 can hand out a wrong gateway. Switches defend against this with DHCP snooping, which allows server replies only on trusted ports.
- Missing relay. A new VLAN without an IP helper gets no leases.
NTP: port 123
Network Time Protocol (NTP) synchronizes clocks over UDP 123. Accurate time matters for log correlation, Kerberos authentication (which rejects tickets when clocks drift beyond about five minutes by default), certificate validity checks and distributed databases.
An NTP client sends a 48-byte packet containing timestamps. The server replies with its own timestamps. The client uses the four timestamps to estimate the network delay and its own clock offset. Time is counted in seconds since 1 January 1900, not 1970, so conversions need an offset of 2208988800 seconds.
Servers listen on 123. Some clients also send from source port 123, while others, such as chrony, use an ephemeral port. A firewall rule that requires source port 123 can break the latter.
The script below sends a minimal NTP request and prints the server's time. It needs outbound UDP 123.
import socket
import struct
import time
NTP_TO_UNIX = 2208988800 # seconds between 1900-01-01 and 1970-01-01
# First byte 0x1b = leap indicator 0, version 3, mode 3 (client)
packet = b"\x1b" + 47 * b"\x00"
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.settimeout(5)
s.sendto(packet, ("pool.ntp.org", 123))
data, server = s.recvfrom(512)
print("local socket:", s.getsockname())
seconds, fraction = struct.unpack("!II", data[40:48]) # transmit timestamp
unix_time = seconds - NTP_TO_UNIX + fraction / 2**32
print("server:", server)
print("time :", time.strftime("%Y-%m-%d %H:%M:%S UTC", time.gmtime(unix_time)))The printed local socket shows an ephemeral source port, which confirms that a client does not need to use 123.
Security gotcha
Old NTP servers answered a monlist control query with a long list of recent clients. Because UDP source addresses can be forged, attackers sent small spoofed requests and the server sent large replies to the victim. This is an amplification attack. Keep NTP servers patched, disable monlist, and do not expose control queries to the internet.
SNMP: ports 161 and 162
Simple Network Management Protocol (SNMP) lets a manager read and change values on devices such as switches, routers, printers and UPS units. Each device runs an agent. Values are identified by object identifiers (OIDs) arranged in a tree called the MIB.
There are two directions:
- Polling. The manager sends a
GETrequest to UDP 161 on the agent and receives the value. - Notifications. The agent sends a trap or inform message to UDP 162 on the manager when something happens, such as a link going down. Informs are acknowledged and traps are not.
Versions differ mainly in security:
| Version | Authentication | Encryption |
|---|---|---|
| v1, v2c | Community string sent in clear text | None |
| v3 | Username with hashed authentication | Optional (privacy mode) |
Community strings such as public are effectively passwords sent in clear text, and many devices ship with defaults. Use v3 with authentication and privacy for anything beyond a lab. Also restrict UDP 161 to the management network, because write access with a guessable community string can reconfigure a device.
Syslog: ports 514 and 6514
Syslog is a standard format and transport for log messages. Devices send messages to a central collector, so logs survive a device failure or compromise.
Each message begins with a priority value in angle brackets. The priority equals facility * 8 + severity. Facility identifies the source type (for example 4 is authentication messages). Severity runs from 0 (emergency) to 7 (debug). So <34> is facility 4, severity 2 (critical).
Transport options:
- UDP 514. The traditional choice. It is cheap, but messages can be lost silently and there is no encryption.
- TCP 514. Detects loss and keeps order. It is still clear text, and framing between messages needs agreement between sender and receiver.
- TLS on TCP 6514. Encrypted and authenticated. This is the choice for logs that cross untrusted networks.
The script below runs a local receiver on a high port, sends one message and decodes the priority. Using port 5514 avoids needing root privileges, because ports below 1024 usually require them.
import socket
PORT = 5514
rx = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
rx.bind(("127.0.0.1", PORT))
rx.settimeout(2)
facility, severity = 4, 2
pri = facility * 8 + severity
message = f"<{pri}>Nov 27 12:06:06 host1 su: 'su root' failed".encode("ascii")
tx = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
tx.sendto(message, ("127.0.0.1", PORT))
tx.close()
data, sender = rx.recvfrom(2048)
rx.close()
text = data.decode("ascii")
value = int(text[1:text.index(">")])
print("from :", sender)
print("facility:", value >> 3)
print("severity:", value & 7)Inspecting these services
Check what is listening on your own host, then capture the traffic to see the real packets.
# Which of these UDP services are listening locally?
ss -ulnp | grep -E ':(67|123|161|514)\b'
# Watch DHCP traffic on an interface (requires root)
sudo tcpdump -ni eth0 'udp port 67 or udp port 68'
# Query a lab device you own with SNMP v2c
snmpget -v2c -c public 192.0.2.10 1.3.6.1.2.1.1.1.0UDP has no handshake, so nc -vz often reports success for UDP ports even when nothing is listening. A reliable test requires a real protocol reply, as the NTP script does.
Firewall reasoning
Allow only what each role needs:
- DHCP server: inbound UDP 67 from client subnets or relays. Clients need inbound UDP 68 for replies.
- NTP client: outbound UDP 123 to specific time servers, and replies to the ephemeral source port. Inbound 123 on a client is not needed.
- SNMP agent: inbound UDP 161 only from the manager's address. The manager needs inbound UDP 162 only from known devices.
- Syslog collector: inbound UDP/TCP 514 or TCP 6514 only from known senders.
A stateful firewall tracks UDP "connections" by address and port pair for a short time, so replies are allowed back automatically. A stateless filter needs an explicit return rule.
Exercise
A new VLAN has a working router but clients show 169.254.x.x addresses. DHCP works in other VLANs. Which port and device feature do you check first, and why?
Show answer
Check that UDP 67 broadcasts from the VLAN reach the DHCP server. Clients broadcast to 255.255.255.255:67, and routers do not forward broadcasts, so the VLAN's router interface needs a DHCP relay (IP helper) pointing at the server. Also confirm that no firewall blocks UDP 67/68 on the path.
Exercise
A syslog message arrives starting with <11>. What are its facility and severity?
Show answer
11 = 1 * 8 + 3, so the facility is 1 (user-level messages) and the severity is 3 (error).
Exercise
Why does nc -vz -u host 123 succeeding not prove that an NTP server is running?
Show answer
UDP is connectionless. With no ICMP "port unreachable" reply, nc treats the probe as successful, so it can report success when nothing is listening or when a firewall silently drops the packet. Send a real NTP request and check for a valid reply instead.