Description
When an LDAP client authenticates with a password using a simple bind, it sends the user's distinguished name (DN) and password to the server. Anyone who can intercept an unencrypted connection can read those credentials. When connecting over ldap://, the client can protect the bind by completing StartTLS and verifying the server's certificate before sending the password.
Potential impact
- Someone who intercepts the password can sign in to the directory and read or change data with the account's permissions.
- A reused password can expose other internal services. A compromised administrator or service account can expose every system it has permission to access.
- Sending credentials in cleartext can violate policies that require encryption in transit.
Remediation
- Connect using
ldaps://(default port 636), or complete StartTLS overldap://before performing asimplebind. Close the connection if TLS negotiation fails. - Configure the JVM truststore with the CA certificates your organization trusts, and verify that the server certificate matches the hostname you connect to. Configure the LDAP server to reject password binds on unencrypted connections.
- In a Kerberos environment configured with JAAS, you can authenticate with SASL GSSAPI. If SASL must also protect subsequent traffic, require
auth-conf, the quality of protection (QOP) that provides integrity and confidentiality. Avoid the obsolete DIGEST-MD5 mechanism in new configurations. - Give service accounts only the permissions they need. If a password may have been exposed, change it on the directory account and on every other service where it was reused.
Examples
Before
java
import javax.naming.Context;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import java.util.Hashtable;
public class InsecureLdapLogin {
public DirContext login(String user, String pass) throws Exception {
Hashtable<String, String> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://10.0.0.5:389"); // Cleartext connection
env.put(Context.SECURITY_AUTHENTICATION, "simple"); // Simple Bind
env.put(Context.SECURITY_PRINCIPAL, "uid=" + user + ",ou=people,dc=corp,dc=local");
env.put(Context.SECURITY_CREDENTIALS, pass);
return new InitialDirContext(env); // Credentials are sent in cleartext
}
}
After
java
import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.ldap.Rdn;
import java.util.Hashtable;
public class SecureLdapLogin {
public DirContext login(String user, char[] pass) throws Exception {
if (user == null || user.isEmpty() || pass == null || pass.length == 0) {
throw new AuthenticationException("Username and password are required");
}
Hashtable<String, Object> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldaps://ldap.corp.local:636"); // TLS-encrypted connection
env.put(Context.SECURITY_AUTHENTICATION, "simple"); // Performed over TLS
String userDn = "uid=" + Rdn.escapeValue(user)
+ ",ou=people,dc=corp,dc=local";
env.put(Context.SECURITY_PRINCIPAL, userDn);
env.put(Context.SECURITY_CREDENTIALS, pass);
// Use approved CAs in the JVM truststore and keep hostname verification enabled.
return new InitialDirContext(env);
}
}
// Kerberos/JAAS alternative: configure a separate JNDI environment for the logged-in Kerberos subject.
// env.put(Context.PROVIDER_URL, "ldap://ldap.corp.local:389");
// env.put(Context.SECURITY_AUTHENTICATION, "GSSAPI");
// env.put("javax.security.sasl.qop", "auth-conf"); // Integrity and confidentiality protection
Explanation:
- Before: The client performs a
simplebind overldap://without negotiating StartTLS, exposing the password in transit. - After: The client verifies the server certificate over
ldaps://before sending credentials.Rdn.escapeValueescapes the username for use as theuidvalue in the DN. The client rejects the login before connecting if either the username or the password is empty. Even with a nonempty DN, a server can accept a bind with an empty password without authenticating the user.