logo

Database

C Sharp Hardcoded Credentials In Directory

Description

Detects hardcoded credentials in C# DirectoryEntry constructor calls which could expose sensitive authentication information. This presents a security risk since hardcoded credentials can be extracted from source code or compiled binaries, potentially leading to unauthorized LDAP/Active Directory access.

Weakness:

359 - Sensitive information in source code - Credentials

Category: Information Collection

Detection Strategy

    Code imports the System.DirectoryServices namespace

    DirectoryEntry constructor is called with 3 or more parameters

    Password parameter contains a hardcoded string value instead of being retrieved from secure configuration

    Identifies instances where LDAP/AD credentials are embedded directly in the code

Vulnerable code example

using System.DirectoryServices;

public class LdapAuth {
    public bool Authenticate(string username) {
        var entry = new DirectoryEntry(
            "LDAP://dc=example,dc=com",
            "admin",
            "secretPass123",  // Vulnerable: Hardcoded credential exposed in source code...

✅ Secure code example

using System.DirectoryServices;
using System.Configuration;

public class LdapAuth {
    public bool Authenticate(string username) {
        var entry = new DirectoryEntry(
            ConfigurationManager.AppSettings["LdapPath"],
            ConfigurationManager.AppSettings["LdapUsername"], ...