简体   繁体   English

LDAP 查询 c# 中的 LDAP 注入

[英]LDAP injection in LDAP query c#

this is my bool connection for validating whether an user is in AD group or not.这是我用于验证用户是否在 AD 组中的 bool 连接。 I got a security flag in my code.我的代码中有一个安全标志。

    private bool testconnection(string user)
    {
        bool isInGroup = false;
        if (user.Length <= 7 && user.All(char.IsLetterOrDigit))
        {
            string groupName = "GroupName";
            DirectoryEntry de = new DirectoryEntry("LDAP://DC=mycompany,DC=com");
            DirectorySearcher searcher = new DirectorySearcher(de);
            searcher.Filter = "(&(objectClass=user)(|(cn=" + user + ")(sAMAccountName=" + user + ")))"; //When I'm concatenating the user name, here I got the security flag which is below.

            SearchResult result = searcher.FindOne();

            if (result != null)
            {

                DirectoryEntry person = result.GetDirectoryEntry();
                PropertyValueCollection groups = person.Properties["memberof"];

                foreach (string g in groups)
                {
                    if (g.Contains(groupName))
                    {
                        isInGroup = true;
                        break;
                    }
                }

            }
        }
        return isInGroup;
    }

I would like to know, how to pass the user name as a parameter in the searcher.filter rather than "+user+"我想知道,如何将用户名作为参数传递给 searcher.filter 而不是“+user+”

Security Flag:安全标志:

Description描述

The software does not sufficiently sanitize special elements that are used in LDAP queries or responses, allowing attackers to modify the syntax, contents, or commands of the LDAP query before it is executed.该软件没有充分清理 LDAP 查询或响应中使用的特殊元素,允许攻击者在执行 LDAP 查询之前修改其语法、内容或命令。 Recommendations Validate all user-supplied input to ensure that it conforms to the expected format, using centralized data validation routines when possible.建议 验证所有用户提供的输入以确保其符合预期格式,尽可能使用集中数据验证例程。 When using black lists, be sure that the sanitizing routine performs a sufficient number of iterations to remove all instances of disallowed characters.使用黑名单时,请确保清理例程执行足够多的迭代以删除所有不允许使用的字符实例。 Thank you, Krishna谢谢你,克里希纳

根据LDAP Injection Prevention Cheat Sheet ,在某种名为 .Net AntiXSS 的独立库中提供了一些类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM