Ldap Injection in Axum
How Ldap Injection Manifests in Axum
Ldap Injection in Axum applications occurs when user input is directly incorporated into LDAP queries without proper sanitization. This vulnerability allows attackers to manipulate directory service queries, potentially accessing unauthorized data, bypassing authentication, or modifying directory information.
In Axum, this typically manifests in route handlers that process authentication requests or search operations. Consider a common pattern where an application uses LDAP for authentication:
use axum::{routing::get, Router};
use axum_extra::extract::Query;
use ldap3::{LdapConn, Scope, SearchOptions};
#[derive(serde::Deserialize)]
struct LoginQuery {
username: String,
password: String,
}
async fn login(Query(params): Query) -> &'static str {
let ldap = LdapConn::new("ldap://localhost:389").unwrap();
let user_dn = format!("uid={},ou=users,dc=example,dc=com", params.username);
let simple = ldap.simple_bind(user_dn, ¶ms.password).await;
match simple {
Ok(_) => "Login successful",
Err(_) => "Login failed",
}
}
let app = Router::new().route("/login", get(login)); The vulnerability here is in the user_dn construction. An attacker can inject LDAP-specific characters like *, (, ), or
to manipulate the query. For example, submitting username=john)(&(uid=*)) could potentially return true for any user, bypassing authentication entirely.
Another common manifestation is in search operations where user input filters directory queries:
async fn search(Query(params): Query) -> Result {
let ldap = LdapConn::new("ldap://localhost:389").unwrap();
let filter = format!("(&(objectClass=person)(cn={}))", params.search_term);
let (result, _meta) = ldap.search(
"ou=users,dc=example,dc=com",
Scope::Sub,
&filter,
vec!["cn", "sn", "mail"]
).await?;
Ok(serde_json::to_string(&result)?)
} Here, an attacker could submit search_term=*)(cn=* to retrieve all users' information, causing a data exposure vulnerability. The lack of input validation in Axum route handlers makes this particularly dangerous, as the framework doesn't automatically sanitize LDAP inputs.
Axum-Specific Detection
Detecting Ldap Injection in Axum applications requires examining both the code structure and runtime behavior. The most effective approach combines static analysis with dynamic scanning.
Static analysis involves reviewing route handlers that interact with LDAP. Look for patterns where user input is directly interpolated into LDAP queries without validation. In Axum, this often appears in handlers using Query, Json, or Path extractors that pass data directly to LDAP operations.
middleBrick's scanning approach for Axum applications includes:
- Authentication Bypass Testing: The scanner attempts LDAP injection payloads like
)(&(uid=*))andadmin)(userPassword=*))to test if authentication can be bypassed - Directory Traversal Attempts: Testing for payloads that could navigate outside intended search bases, such as
*)orcn=admin - Special Character Injection: Testing with LDAP metacharacters like
*,(,),, and