Sub topic 13
LDAP and SMB
This chapter covers two protocols that sit behind most corporate networks: LDAP, which looks up information about users and resources, and SMB, which shares files and printers. It assumes you know what a port is, the difference between TCP and UDP, and how a plain-text protocol relates to its TLS variant from earlier chapters.
Why these two belong together
In a typical office network, LDAP and SMB often work together. A user signs in to a Windows or Linux workstation, and the sign-in is checked against a directory. The user then opens a shared folder on a file server. The first step uses a directory protocol (LDAP, often alongside Kerberos). The second uses a file-sharing protocol (SMB).
| Protocol | Purpose | Default port | Transport |
|---|---|---|---|
| LDAP | Query and update a directory | 389 | TCP (UDP 389 for AD "CLDAP" locator pings) |
| LDAPS | LDAP wrapped in TLS from the first byte | 636 | TCP |
| LDAP Global Catalog | Forest-wide search in Active Directory | 3268 | TCP |
| LDAP Global Catalog over TLS | Same, with TLS | 3269 | TCP |
| SMB (direct hosting) | File, printer and named-pipe access | 445 | TCP |
| NetBIOS Session Service | Legacy transport for SMB | 139 | TCP |
| NetBIOS Name and Datagram | Legacy name lookup and broadcasts | 137, 138 | UDP |
LDAP: the Lightweight Directory Access Protocol
A directory is a database tuned for reading. It holds entries such as users, groups, computers and printers. Each entry has attributes, such as a name, an email address or a group membership. LDAP is the protocol a client uses to search that database, read entries, and (with permission) change them.
How entries are named
Every entry has a distinguished name (DN). It is written from the most specific part to the least specific part:
uid=alice,ou=People,dc=example,dc=comuid=aliceis the entry itself.ou=Peopleis the organizational unit that contains it.dc=example,dc=comare domain components, matching the DNS nameexample.com.
A search names a base DN where it starts, a scope (just the base, one level, or the whole subtree) and a filter such as (uid=alice).
Operations
An LDAP session is a sequence of requests on one TCP connection. The main operations are:
- Bind: authenticate. An anonymous bind sends no credentials. A simple bind sends a DN and password. A SASL bind uses a mechanism such as GSSAPI (Kerberos).
- Search: find entries that match a filter.
- Add, modify, delete: change entries.
- Unbind: close the session.
Port 389, StartTLS and port 636
Plain LDAP on port 389 sends everything unencrypted, including a simple-bind password. There are two ways to protect it:
- StartTLS keeps port 389. The client connects in plain text, asks the server to upgrade, then negotiates TLS on the same connection.
- LDAPS uses port 636. TLS starts immediately, like HTTPS on 443.
Both give equivalent protection when configured correctly. StartTLS is the standardized approach in the LDAP specifications, while LDAPS is older and still widely deployed. A client that skips certificate validation gains nothing from either.
Active Directory ports
Microsoft Active Directory (AD) speaks LDAP but adds more ports. A domain controller commonly listens on:
- 389 and 636 for ordinary LDAP and LDAPS.
- 3268 and 3269 for the Global Catalog, a partial copy of every object in the forest. Use it for searches that must span several domains.
- 88 for Kerberos, 53 for DNS, and 445 for SMB. Domain sign-in needs all of these, so a firewall that allows only 389 will break domain membership.
Querying LDAP from Python
The third-party ldap3 library (pip install ldap3) is a pure-Python client. This script connects to a server you control, upgrades with StartTLS, binds and searches. Replace the host, DNs and password with your own values.
import ssl
from ldap3 import Server, Connection, Tls, SUBTREE
tls = Tls(validate=ssl.CERT_REQUIRED)
server = Server("ldap.example.com", port=389, tls=tls)
conn = Connection(
server,
user="cn=reader,dc=example,dc=com",
password="change-me",
auto_bind=False,
)
conn.open()
conn.start_tls()
conn.bind()
conn.search(
search_base="dc=example,dc=com",
search_filter="(uid=alice)",
search_scope=SUBTREE,
attributes=["cn", "mail"],
)
for entry in conn.entries:
print(entry.entry_dn, entry.cn, entry.mail)
conn.unbind()If you only want to check that the port is open, the standard library is enough:
import socket
for port in (389, 636):
try:
with socket.create_connection(("ldap.example.com", port), timeout=3):
print(port, "open")
except OSError as err:
print(port, "closed or filtered:", err)Common LDAP errors
- Password sent in clear text. A simple bind over plain port 389 exposes the password to anyone on the path. Require StartTLS or LDAPS, and consider refusing binds that are not encrypted.
- Certificate name mismatch. The name in the server certificate must match the host you connect to. Connecting by IP address often fails validation.
- Anonymous access left on. Anonymous searches can leak user and group lists.
- Exposing LDAP to the internet. Directory ports should be reachable only from the networks that need them.
- Wrong port for the scope. A search on 389 sees one domain. A search on 3268 sees the forest, but only returns the attributes replicated to the Global Catalog.
SMB: Server Message Block
SMB lets a client open, read, write and lock files on a remote server as if they were local. It also carries printer access and named pipes, which many Windows management tools use. Linux systems use Samba to serve and mount SMB shares. Shares are written as \\server\share on Windows and //server/share elsewhere.
Port 445 and the NetBIOS legacy
Early SMB ran on top of NetBIOS, an older Windows networking layer, which used:
- UDP 137 for name lookups.
- UDP 138 for datagram messages.
- TCP 139 for the session service that carried SMB.
Starting with Windows 2000, SMB can run directly on TCP port 445 without NetBIOS. Modern systems use 445. Ports 137 to 139 remain mostly for old clients and for network browsing on small networks.
Versions
SMB has several dialects, and the version matters for security.
| Version | Notes |
|---|---|
| SMB 1 (CIFS) | Old, slow and insecure. Disabled by default on current Windows. |
| SMB 2.x | Fewer round trips, better performance. |
| SMB 3.x | Adds encryption, secure dialect negotiation and multichannel. |
The client and server negotiate the highest dialect both support. SMB 1 should be turned off wherever possible, since it was the target of widely used exploits, including the one behind the 2017 WannaCry outbreak.
SMB 3 can encrypt traffic on port 445 itself. Unlike HTTPS or LDAPS, no separate TLS port exists for it. Encryption is a feature enabled per share or per server. SMB over QUIC, available in recent Windows Server releases, instead carries SMB over UDP port 443 inside TLS 1.3, so it can cross the internet without exposing port 445.
SMB is not for the internet
Port 445 on a public address is one of the most scanned and attacked ports. Many internet service providers block it. Reach file servers from outside through a VPN, or use SMB over QUIC, rather than opening 445.
Checking for SMB from the command line
# Is anything listening on 445 or 139 on this host?
ss -tlnp | grep -E ':(445|139)\b'
# Can another machine reach it? (use only on hosts you own)
nc -vz 192.0.2.20 445
# List shares on a Samba or Windows server (needs smbclient)
smbclient -L //192.0.2.20 -U aliceUse nc -vz for LDAP too, for example nc -vz 192.0.2.30 636. A successful connection proves only that something accepts TCP connections on that port. It does not prove that the service is LDAP or that the TLS setup is correct.
Common SMB errors
- Port 445 blocked. A firewall or ISP drops traffic. The client times out instead of showing a permission error.
- Only 139 allowed. Modern clients try 445 first, then may fall back to 139. A rule set that opens 139 alone gives slow or failed connections.
- SMB 1 required by an old device. Fix the device or isolate it. Do not enable SMB 1 across the network.
- Name resolution fails. The port is open but
\\servercannot be resolved. Check DNS, not the firewall. - Authentication errors. The port works, but the credentials, domain or time are wrong. Kerberos fails when client and server clocks differ by more than the allowed skew, which is five minutes by default in Active Directory.
Firewall reasoning for these services
Write rules from the service's needs, and allow only the sources that need them.
- A directory used only by servers inside a data center: allow TCP 636 from those subnets, and block 389 if all clients support TLS.
- A file server for one office: allow TCP 445 from that office's subnet. Do not allow 137 to 139 unless an old client requires them.
- A domain controller: allow the full set (53, 88, 389, 445, 636, and 3268/3269 if used) from internal client networks only, plus the dynamic RPC ports if Windows replication or management crosses the firewall.
Summary
LDAP (TCP 389, with TLS through StartTLS or LDAPS on 636) queries directories, and Active Directory adds the Global Catalog on 3268 and 3269. SMB (TCP 445, with legacy NetBIOS on 137 to 139) shares files and printers, and its encryption is built into SMB 3. Both carry credentials and sensitive data, so protect them with encryption and restrict them to internal networks.
Exercise
A firewall rule set for a new office file server allows only TCP 139 from the office subnet. Windows 11 clients cannot connect reliably. Which port should be allowed, which legacy ports are not needed, and why should the rule not use "any source"?
Show answer
Allow TCP 445, which is where modern SMB runs without NetBIOS. Ports 137 (UDP), 138 (UDP) and 139 (TCP) are only needed by legacy NetBIOS clients. Restricting the source to the office subnet matters because port 445 is heavily scanned and attacked, and a public file server port exposes shares and authentication to the whole internet.
Exercise
An application sends a simple LDAP bind with a user's password to ldap.example.com on port 389. Name two ways to protect the password, and say which port each uses.
Show answer
Use StartTLS on port 389, which upgrades the existing connection to TLS before the bind is sent. Or use LDAPS on port 636, where TLS starts as soon as the connection opens. In both cases the client must validate the server certificate, or an attacker in the path can still intercept the password.
Exercise
A tool must search for a user in any domain of an Active Directory forest. Which port should it use instead of 389, and what is the trade-off?
Show answer
Use the Global Catalog on port 3268, or 3269 for TLS. It covers every domain in the forest, but it holds only a subset of each object's attributes, so some attributes will be missing from the results.