Sub topic 13
LDAP and SMB
This chapter assumes you know what a port is, the difference between TCP and UDP, and how a client connects to a server socket. It covers two protocols used in corporate networks: LDAP for directory lookups and SMB for file and printer sharing.
LDAP: directory access
LDAP (Lightweight Directory Access Protocol) lets a client search and modify a directory. A directory is a hierarchical database of users, groups, computers and other objects. Services such as Active Directory and OpenLDAP use it for logins and lookups.
| Protocol | Port | Transport | Notes |
|---|---|---|---|
| LDAP | 389 | TCP (UDP for some Active Directory queries) | Plain text, or upgraded with StartTLS |
| LDAPS | 636 | TCP | TLS from the first byte |
| Global Catalog | 3268 | TCP | Active Directory forest-wide search |
| Global Catalog over TLS | 3269 | TCP | TLS version of 3268 |
LDAP on port 389 sends data unencrypted unless the client issues a StartTLS command, which upgrades the same connection to TLS. LDAPS on 636 starts TLS immediately, as HTTPS does. Both are in common use. Without one of them, a simple bind (a login with a user name and password) sends the password in readable form.
SMB: file and printer sharing
SMB (Server Message Block) lets a client open, read and write files on a remote server as if they were local. It also carries printer access and named pipes. Windows file shares use it, and Samba provides it on Linux.
Modern SMB runs directly over TCP port 445. Older setups ran SMB over NetBIOS, which used these ports:
- UDP 137: NetBIOS name service
- UDP 138: NetBIOS datagram service
- TCP 139: NetBIOS session service
Port 445 is all you need for current clients. Ports 137 to 139 matter only for legacy systems.
Checking the ports
You can test whether these services answer, on hosts you own, with the same tools used for other ports. This script reports which of the ports in this chapter accept a TCP connection:
import socket
HOST = "192.0.2.10" # replace with a server you own
PORTS = {389: "LDAP", 636: "LDAPS", 445: "SMB", 139: "NetBIOS session"}
for port, name in PORTS.items():
try:
with socket.create_connection((HOST, port), timeout=2):
print(f"{port:>4} {name}: open")
except OSError:
print(f"{port:>4} {name}: closed or filtered")An open port shows only that something is listening. It does not prove the service is LDAP or SMB, and it says nothing about whether encryption is in use.
Common errors
- Allowing 389 but not 636 (or the reverse), so clients using the other variant time out.
- Opening only 445 when a legacy client needs 139 and the UDP NetBIOS ports.
- Forgetting the Global Catalog ports 3268 and 3269 when applications search an Active Directory forest.
- Assuming a service on 389 is encrypted. Check whether StartTLS is required.
Exercise
A firewall rule allows only TCP 389 from an application server to a domain controller. The application is configured to use LDAPS and cannot connect. What is wrong and what is the fix?
Show answer
LDAPS uses TCP 636, which the rule does not allow. Add TCP 636 from the application server to the domain controller. Alternatively, configure the application for LDAP with StartTLS on port 389.
Exercise
Which single port do current SMB clients need to reach a file server, and why should it be blocked from the internet?
Show answer
TCP 445. SMB has a history of severe vulnerabilities that attackers have used to spread malware and take over hosts, so only systems that need file sharing should reach it.