【问题标题】:How to check if a user account is locked via PHP/LDAP?如何检查用户帐户是否通过 PHP/LDAP 锁定?
【发布时间】:2022-01-22 16:32:21
【问题描述】:

我们创建了一个需要与用户的网络登录密码相同的内网站点,因此我们使用 LDAP 来检查用户名/密码。

这很好,但是如果他们输入错误 3 次,就会锁定他们的帐户,并且一两个用户会感到困惑。

我是否可以使用 LDAP/PHP 检查他们的帐户是否被锁定,所以我可以显示一条提示他们联系 IT 的小消息?

【问题讨论】:

  • 如果他们的帐户被锁定,他们如何登录?
  • 您正在运行什么 LDAP?我不认为“锁定”是用户对象的标准属性。

标签: php ldap


【解决方案1】:

您需要使用 PHP 中的 LDAP 函数连接到 LDAP 并执行搜索/读取以定位和获取信息。你可以在这里阅读:http://us3.php.net/manual/en/book.ldap.php

查找读取条目的示例代码:

if (!($ldap=ldap_connect($ldapip, $ldapport)))  
    {
        die("Error:Unable to connect to the LDAP Server");
        return;
    }
    if (!ldap_bind($ldap, $admindn, $adminpwd))
    {
        die("Error:Unable to bind to '$dn'!");
        return;
    }

    $sr=ldap_search($ldap, $userbasedn, $filter);
    $info = ldap_get_entries($ldap, $sr);

    if($info["count"] > 0)
    {
        $entry = ldap_first_entry($ldap, $sr);
        $return_array = ldap_get_attributes($ldap, $entry);
        if($return_array)
        {
            for ($i=0;$i<$return_array['count'];$i++)
            {
                      print($return_array[$i]);
                      print($return_array[$return_array[$i]][0]);
                    }
        }
    }

您可能需要检查 AD 中的 lockoutTime、LDAP 中的 nsaccountlock 字段并阅读它们

【讨论】:

  • eDirectory 中被入侵者锁定。不过我忘记了重置时间属性名称。
【解决方案2】:

AD 配置文件属性之一useraccountcontrol。 这包含decimal 值,可以在此处转换为可读;

locked一般可以指多个case

  • ACCOUNTDISABLE 2 / 0x0002(十六进制)
  • PASSWORD_EXPIRED8388608 / 0x800000
  • LOCKOUT 16 / 0x0010

【讨论】:

    【解决方案3】:

    如果没有标准的“锁定”字段,我会使用 LDAP 浏览器来比较锁定前后的帐户。您可以使用LBE(LDAP 浏览器/编辑)提取用户对象的 LDIF 文件,然后使用您喜欢的 diff 工具进行比较。

    【讨论】:

    • LBE 何时采用“收费”模式?我想知道它去了哪里,因为我必须链接到它的所有链接都已失效。哎呀。我最喜欢的 LDAP 浏览器仍然...
    • 不知道...我什至没有注意到那部分。 (一年多没用了)
    【解决方案4】:

    这不是否定了共享登录的想法吗?

    如果您的 Intranet 站点允许的试用次数多于网络登录,则可以使用它来查找用户的密码。

    【讨论】:

      【解决方案5】:

      现在是 2022 年,这仍然是一个相关问题。我必须编写类似的逻辑来查询 Active Directory 并确定用户帐户是否被锁定。接受的答案并没有真正帮助我。这是一个对我有用的示例代码:

      function isAccountLocked($ldapconn, $userDn)
      {
          $read = ldap_read($ldapconn, $userDn, "(objectclass=*)", array("msds-user-account-control-computed")) or die("Not found");
          $info = ldap_get_entries($ldapconn, $read);
          $attributeValue = 0;
          if (array_key_exists("0", $info)) {
              if (array_key_exists("msds-user-account-control-computed", $info["0"])) {
                  if (array_key_exists("0", $info["0"]["msds-user-account-control-computed"])) {
                      $attributeValue = $info["0"]["msds-user-account-control-computed"]["0"];
                  }
              }
          }
          return $attributeValue == 16 || $attributeValue == 8388608; 
          //16 - Account locked (by many unsuccessful login attempts)
          //8388608 - Password Expired
          //2 - Account disabled -> Not tested.
          //check the docs here: https://docs.microsoft.com/en-us/windows/win32/adschema/a-msds-user-account-control-computed
      }
      

      另外,您可以在 Linux 终端中使用 ldapsearch 查询 msds-user-account-control-computed 属性。 但是,要在结果中显示它,您必须在过滤器中包含 msds-user-account-control-computed。否则,ldapsearch 默认不会返回。 检查以下示例:

      ldapsearch -x -h activedirectoryhost.example.com -LLL -b "dc=example,dc=com" -D "CN=user,OU=SOME_OU,DC=example,DC=com" "(sAMAccountName=user)" -W cn msDS-User-Account-Control-Computed
      

      令人惊讶的是,powershell 查询将返回一个 LockOut 属性,该属性准确地告诉我们想要什么,但其他 ldap 客户端不会返回它。 powershell 查询示例如下:

      Get-ADUser user -Properties * | Select-Object LockedOut
      

      这里有一些其他有用的链接:

      https://docs.microsoft.com/en-US/troubleshoot/windows-server/identity/useraccountcontrol-manipulate-account-properties#property-flag-descriptions

      https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src/ADSchema/a-msds-user-account-control-computed.md.

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-11-26
        • 1970-01-01
        • 2012-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多