首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP LDAP获取用户SID

PHP LDAP获取用户SID
EN

Stack Overflow用户
提问于 2016-09-16 14:07:39
回答 4查看 5K关注 0票数 3

我不知道如何在AD中获得用户唯一标识符(SID)。代码片段:

代码语言:javascript
复制
...    
$filter="(&(samaccountname=".$this->username.")(memberOf:1.2.840.113556.1.4.1941:=CN=GROUP_NAME,OU=Security,DC=something,DC=something))";
    $attribute = array("cn","objectsid","description", "group", "member", "samaccountname");
    $sr=ldap_search($this->conn_ldap, $this->ldap_dn, $filter, $attribute);

    if ($sr) 
    {

    $this->info = ldap_get_entries($this->conn_ldap, $sr);
    if ($this->info["count"] == 1){

    ldap_close($this->conn_ldap);
    return true;
    }
    ... 

我可以通过以下方式获取信息:

代码语言:javascript
复制
echo $this->info[0]["cn"][0];

代码语言:javascript
复制
echo $this->info[0]["objectsid"][0];

在第一个输出中,我可以看到0�@�d^�WL7�U中的用户名称,我认为sid应该像S-......一样

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-09-16 14:34:38

我在另一个网站上找到了一个解决方案(见下文)。基本上,这个函数是转换器,使SID可见:

代码语言:javascript
复制
public static function SIDtoString($ADsid)
{
   $sid = "S-";
   //$ADguid = $info[0]['objectguid'][0];
   $sidinhex = str_split(bin2hex($ADsid), 2);
   // Byte 0 = Revision Level
   $sid = $sid.hexdec($sidinhex[0])."-";
   // Byte 1-7 = 48 Bit Authority
   $sid = $sid.hexdec($sidinhex[6].$sidinhex[5].$sidinhex[4].$sidinhex[3].$sidinhex[2].$sidinhex[1]);
   // Byte 8 count of sub authorities - Get number of sub-authorities
   $subauths = hexdec($sidinhex[7]);
   //Loop through Sub Authorities
   for($i = 0; $i < $subauths; $i++) {
      $start = 8 + (4 * $i);
      // X amount of 32Bit (4 Byte) Sub Authorities
      $sid = $sid."-".hexdec($sidinhex[$start+3].$sidinhex[$start+2].$sidinhex[$start+1].$sidinhex[$start]);
   }
   return $sid;
}

https://www.null-byte.org/development/php-active-directory-ldap-authentication/

票数 4
EN

Stack Overflow用户

发布于 2019-07-24 06:29:07

这是一篇老文章,但我做了一些我认为有用的组合,这个函数是二进制到SID翻译的最终功能:

代码语言:javascript
复制
// Binary to SID
function bin_to_str_sid($binary_sid) {

    $sid = NULL;
    /* 64bt PHP */
    if(strlen(decbin(~0)) == 64)
    {
        // Get revision, indentifier, authority 
        $parts = unpack('Crev/x/nidhigh/Nidlow', $binary_sid);
        // Set revision, indentifier, authority 
        $sid = sprintf('S-%u-%d',  $parts['rev'], ($parts['idhigh']<<32) + $parts['idlow']);
        // Translate domain
        $parts = unpack('x8/V*', $binary_sid);
        // Append if parts exists
        if ($parts) $sid .= '-';
        // Join all
        $sid.= join('-', $parts);
    }
    /* 32bit PHP */
    else
    {   
        $sid = 'S-';
        $sidinhex = str_split(bin2hex($binary_sid), 2);
        // Byte 0 = Revision Level
        $sid = $sid.hexdec($sidinhex[0]).'-';
        // Byte 1-7 = 48 Bit Authority
        $sid = $sid.hexdec($sidinhex[6].$sidinhex[5].$sidinhex[4].$sidinhex[3].$sidinhex[2].$sidinhex[1]);
        // Byte 8 count of sub authorities - Get number of sub-authorities
        $subauths = hexdec($sidinhex[7]);
        //Loop through Sub Authorities
        for($i = 0; $i < $subauths; $i++) {
            $start = 8 + (4 * $i);
            // X amount of 32Bit (4 Byte) Sub Authorities
            $sid = $sid.'-'.hexdec($sidinhex[$start+3].$sidinhex[$start+2].$sidinhex[$start+1].$sidinhex[$start]);
        }
    }
    return $sid;
}

现在,您可以在任何PHP版本中使用此函数。

票数 2
EN

Stack Overflow用户

发布于 2017-02-11 16:51:31

作为另一个例子,这可以完全使用PHP的unpack函数来完成。objectSid二进制结构最好记录在这个MSDN文档上。

修订(1字节):一个8位无符号整数,指定SID的修订级别。此值必须设置为0x01。 SubAuthorityCount (1字节):指定SubAuthority数组中元素数的8位无符号整数。允许的最大元素数为15个。 IdentifierAuthority (6字节):指示创建SID的权限的SID_IDENTIFIER_AUTHORITY结构。它描述了创建SID的实体。标识符授权值{0,0,0,0,0,5}表示由NT SID授权机构创建的SID。 SubAuthority (变量):由32位无符号整数组成的可变长度数组,唯一标识相对于IdentifierAuthority的主体。其长度由SubAuthorityCount决定。

代码语言:javascript
复制
/**
 * Decode the binary SID into its readable form.
 *
 * @param string $value
 * @return string
 */
function decodeSID($value)
{
    # revision - 8bit unsigned int (C1)
    # count - 8bit unsigned int (C1)
    # 2 null bytes
    # ID - 32bit unsigned long, big-endian order
    $sid = @unpack('C1rev/C1count/x2/N1id', $value);
    $subAuthorities = [];

    if (!isset($sid['id']) || !isset($sid['rev'])) {
        throw new \UnexpectedValueException(
            'The revision level or identifier authority was not found when decoding the SID.'
        );
    }

    $revisionLevel = $sid['rev'];
    $identifierAuthority = $sid['id'];
    $subs = isset($sid['count']) ? $sid['count'] : 0;

    // The sub-authorities depend on the count, so only get as many as the count, regardless of data beyond it
    for ($i = 0; $i < $subs; $i++) {
        # Each sub-auth is a 32bit unsigned long, little-endian order
        $subAuthorities[] = unpack('V1sub', hex2bin(substr(bin2hex($value), 16 + ($i * 8), 8)))['sub'];
    }

    # Tack on the 'S-' and glue it all together...
    return 'S-'.$revisionLevel.'-'.$identifierAuthority.implode(
        preg_filter('/^/', '-', $subAuthorities)
    );
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39533560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档