如何通过 JNDI 检索 LDAP 密码

发布于 2024-10-06 22:01:04 字数 2978 浏览 5 评论 0原文

我可以通过 JNDI 读取 LDAP 中存储的密码。但结果是一些乱码字符。那么我该如何解密呢?

下面是我的代码:

public static void main(String[] args)
        {
            String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory";
            String MY_HOST = "ldap://KhooGP-Comp1:1389";
            String MGR_DN = "cn=Directory Manager";
            String MGR_PW = "password";
            String MY_SEARCHBASE = "dc=QuizPortal";
            String MY_FILTER = "uid=yiwei";
            String MY_ATTRS[] = {"cn", "uid", "sn", "userpassword"};

            //Identify service provider to use
            Hashtable env = new Hashtable();
            env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);
            env.put(Context.PROVIDER_URL, MY_HOST);

            env.put(Context.SECURITY_AUTHENTICATION, "simple");
            env.put(Context.SECURITY_PRINCIPAL, MGR_DN);
            env.put(Context.SECURITY_CREDENTIALS, MGR_PW);

            try
            {
                // Create the initial directory context
                InitialDirContext initialContext = new InitialDirContext(env);
                DirContext ctx = (DirContext)initialContext;

                System.out.println("Context Sucessfully Initialized");

                SearchControls constraints = new SearchControls();
                constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);

                NamingEnumeration results = ctx.search(MY_SEARCHBASE, MY_FILTER, constraints);

                while(results != null && results.hasMore())
                {
                    SearchResult sr = (SearchResult) results.next();
                    String dn = sr.getName() + "," + MY_SEARCHBASE;
                    System.out.println("Distinguished Name is " + dn);

                    Attributes ar = ctx.getAttributes(dn, MY_ATTRS);

                    if(ar == null)
                    {
                        System.out.println("Entry " + dn);
                        System.out.println(" has none of the specified attributes\n");
                    }
                    else
                    {
                        for(int i=0; i<MY_ATTRS.length; i++)
                        {
                            Attribute attr = ar.get(MY_ATTRS[i]);
                            System.out.println(MY_ATTRS[i] + ":");

                            for(Enumeration vals=attr.getAll(); vals.hasMoreElements();)
                            {
                                System.out.println("\t" + vals.nextElement());
                            }
                        }
                    }
                }
            }
            catch(Exception e)
            {
                System.err.println(e);
            }
    }

Below is the result:

    Distinguished Name is uid=yiwei,ou=Administrator,o=SID,dc=QuizPortal
    cn:
            yiwei huang
    uid:
            yiwei
    sn:
            huang
    userpassword:
            [B@1cd8669

有什么建议吗?非常感谢

凯文

I am able to read the password stored in LDAP via JNDI. But the result is some gibberish characters. So how do i decrypt it?

Below is my code:

public static void main(String[] args)
        {
            String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory";
            String MY_HOST = "ldap://KhooGP-Comp1:1389";
            String MGR_DN = "cn=Directory Manager";
            String MGR_PW = "password";
            String MY_SEARCHBASE = "dc=QuizPortal";
            String MY_FILTER = "uid=yiwei";
            String MY_ATTRS[] = {"cn", "uid", "sn", "userpassword"};

            //Identify service provider to use
            Hashtable env = new Hashtable();
            env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);
            env.put(Context.PROVIDER_URL, MY_HOST);

            env.put(Context.SECURITY_AUTHENTICATION, "simple");
            env.put(Context.SECURITY_PRINCIPAL, MGR_DN);
            env.put(Context.SECURITY_CREDENTIALS, MGR_PW);

            try
            {
                // Create the initial directory context
                InitialDirContext initialContext = new InitialDirContext(env);
                DirContext ctx = (DirContext)initialContext;

                System.out.println("Context Sucessfully Initialized");

                SearchControls constraints = new SearchControls();
                constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);

                NamingEnumeration results = ctx.search(MY_SEARCHBASE, MY_FILTER, constraints);

                while(results != null && results.hasMore())
                {
                    SearchResult sr = (SearchResult) results.next();
                    String dn = sr.getName() + "," + MY_SEARCHBASE;
                    System.out.println("Distinguished Name is " + dn);

                    Attributes ar = ctx.getAttributes(dn, MY_ATTRS);

                    if(ar == null)
                    {
                        System.out.println("Entry " + dn);
                        System.out.println(" has none of the specified attributes\n");
                    }
                    else
                    {
                        for(int i=0; i<MY_ATTRS.length; i++)
                        {
                            Attribute attr = ar.get(MY_ATTRS[i]);
                            System.out.println(MY_ATTRS[i] + ":");

                            for(Enumeration vals=attr.getAll(); vals.hasMoreElements();)
                            {
                                System.out.println("\t" + vals.nextElement());
                            }
                        }
                    }
                }
            }
            catch(Exception e)
            {
                System.err.println(e);
            }
    }

Below is the result:

    Distinguished Name is uid=yiwei,ou=Administrator,o=SID,dc=QuizPortal
    cn:
            yiwei huang
    uid:
            yiwei
    sn:
            huang
    userpassword:
            [B@1cd8669

Any advice?? Many thanks in advance

Kevin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

电影里的梦 2024-10-13 22:01:04

您所看到的 ([B@1cd8669) 是 Java 表达“这是一个字节数组”的方式。

存储的“密码”很可能是真实密码的哈希值或加密版本。根据定义,加密散列是不可逆的,因此如果 LDAP 存储散列,您将无法看到用户的密码是什么。

如果它是加密的,那么如果您知道算法和密钥,解密就相当简单。 BouncyCastle 是一个很棒的 Java 加密库,可用于解密密码。

基本上,您需要确切地知道您正在查看的内容,这取决于 LDAP 设置。

What you're seeing ([B@1cd8669) is Java's way of saying "this is a byte array".

The stored "password" is most likely either a hash of the real password or an encrypted version. Cryptographic hashes are, by definition, non-reversible so you will not be able to see what the user's password is if LDAP stores the hash.

If it's encrypted then if you know the algorithm and the key it's fairly simple to decrypt. BouncyCastle is a great Java crypto library you can use to decrypt the password.

Basically, you need to know exactly what you're looking at, and that will depend on the LDAP setup.

小巷里的女流氓 2024-10-13 22:01:04

使用 ldap,我们将获取字节数组中的数据。如果您需要获取原始密码文本,请使用
以下代码:

Attribute userPassword = attributes.get("userPassword");
String pwd = new String((byte[]) userPassword.get());

with ldap we will get data in byte array.if you need to get the original password text use the
following code:

Attribute userPassword = attributes.get("userPassword");
String pwd = new String((byte[]) userPassword.get());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文