DEV Community

Cover image for LDAP Injection in Laravel: Prevention & Secure Coding
Pentest Testing Corp
Pentest Testing Corp

Posted on

LDAP Injection in Laravel: Prevention & Secure Coding

Introduction

LDAP (Lightweight Directory Access Protocol) is widely used for authentication and user data retrieval. However, if not properly handled, LDAP queries can be manipulated, leading to LDAP Injection attacks. In Laravel applications, poor input validation and direct query concatenation make them vulnerable to such exploits.

LDAP Injection in Laravel: Prevention & Secure Coding
In this guide, we will explore LDAP Injection in Laravel, its risks, and secure coding practices with multiple coding examples.


What is LDAP Injection?

LDAP Injection occurs when an attacker manipulates LDAP queries by injecting malicious input, often bypassing authentication and gaining unauthorized access to sensitive data.

Example of a Vulnerable LDAP Query

Consider a Laravel application using LDAP authentication:

$username = $_GET['username'];
$password = $_GET['password'];

$ldapconn = ldap_connect("ldap://example.com") or die("Could not connect");

$filter = "(uid=$username)";
$search = ldap_search($ldapconn, "dc=example,dc=com", $filter);
$entries = ldap_get_entries($ldapconn, $search);

if ($entries["count"] > 0) {
    echo "User authenticated";
} else {
    echo "Invalid credentials";
}
Enter fullscreen mode Exit fullscreen mode

Why is this Vulnerable?

The input $username is directly used in the LDAP filter, making it susceptible to injection. An attacker could bypass authentication using:

username=*)(&) (password=anything
Enter fullscreen mode Exit fullscreen mode

This results in a manipulated query:

(uid=*)(password=anything)
Enter fullscreen mode Exit fullscreen mode

This always evaluates to true, granting unauthorized access.


How to Prevent LDAP Injection in Laravel?

1. Use Parameterized Queries

Using ldap_escape() prevents special characters from manipulating LDAP queries.

$username = ldap_escape($_GET['username'], "", LDAP_ESCAPE_FILTER);
$password = ldap_escape($_GET['password'], "", LDAP_ESCAPE_FILTER);

$filter = "(uid=$username)";
$search = ldap_search($ldapconn, "dc=example,dc=com", $filter);
Enter fullscreen mode Exit fullscreen mode

2. Validate User Input

Sanitize and validate inputs before using them in LDAP queries.

$username = filter_var($_GET['username'], FILTER_SANITIZE_STRING);
$password = filter_var($_GET['password'], FILTER_SANITIZE_STRING);
Enter fullscreen mode Exit fullscreen mode

3. Implement Role-Based Access Control (RBAC)

Limit LDAP permissions based on user roles.

if ($userRole !== 'admin') {
    die("Access denied.");
}
Enter fullscreen mode Exit fullscreen mode

4. Use Secure Bind Authentication

Instead of filtering directly, authenticate securely.

$ldapbind = ldap_bind($ldapconn, "uid=$username,dc=example,dc=com", $password);
Enter fullscreen mode Exit fullscreen mode

5. Restrict Special Characters

Prevent attackers from injecting wildcard characters like * or |.

if (preg_match('/[\*\(\)\|\&]/', $username)) {
    die("Invalid input detected.");
}
Enter fullscreen mode Exit fullscreen mode

Screenshot 1: Our Free Website Security Checker

To ensure your Laravel application is secure from LDAP Injection, use our free website security scanner.

Screenshot of the free tools webpage where you can access security assessment tools.Screenshot of the free tools webpage where you can access security assessment tools.


Real-World Example of LDAP Injection Exploitation

An attacker can use tools like LDAP Injection Fuzzer to manipulate queries.

Exploitable Payload Example

username=*)(|(objectClass=*))(
Enter fullscreen mode Exit fullscreen mode

If the application is vulnerable, this could expose all user accounts stored in LDAP.


Screenshot 2: Website Vulnerability Assessment Report

We conducted a website vulnerability scan using our free tool to check website vulnerability. Here’s an example of a security report detecting LDAP Injection vulnerabilities.

An Example of a vulnerability assessment report generated with our free tool, providing insights into possible vulnerabilities.An Example of a vulnerability assessment report generated with our free tool, providing insights into possible vulnerabilities.


Best Practices to Secure LDAP in Laravel

Always sanitize and escape user input
Use parameterized queries
Apply role-based authentication
Limit access permissions
Monitor and audit LDAP queries


Conclusion

LDAP Injection is a critical security flaw that can expose sensitive user data and grant unauthorized access. By following secure coding practices in Laravel, you can prevent attacks and keep your application safe.

🔗 Read more about web security vulnerabilities on our blog at https://www.pentesttesting.com/blog/.

💡 Want to check your website’s security? Use our free security scanner now!

Top comments (0)