2016-09-16 123 views
1

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

...  
$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; 
    } 
    ... 

我可以拉的信息:

echo $this->info[0]["cn"][0]; 

echo $this->info[0]["objectsid"][0]; 

在第一输出,我可以在谢胜利,像0�@�d^�WL7�U 看到用户的名字,我相信SID应像S-......

+0

从谷歌搜索的第2个链接 'PHP LDAP获得SID' 扔了一些代码值得尝试:http://php.net/manual/en/function.ldap-get-values-len。 php(请参阅derek dot ethier的评论)&http://l3rady.com/index.html%3Fp=435.html –

+0

向“''$ attributes'''数组添加一个”+“,看看结果然后。这可能会揭示一些额外的信息。 – heiglandreas

回答

2

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

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/

1

作为替代示例中,这可以完全使用PHP的解压缩功能来完成。所述的objectSID二进制结构上最好this MSDN doc记载:

修订(1个字节):一个8位的无符号整数,指定SID的 修订级别。该值必须设置为0x01。

SubAuthorityCount(1字节):一个8位无符号整数,指定 SubAuthority数组中的元素数量。允许的元素的最大数量是 15.

IdentifierAuthority(6个字节):甲SID_IDENTIFIER_AUTHORITY结构 ,其指示在其下SID被创建的权限。它 描述了创建SID的实体。标识符权限 值{0,0,0,0,0,5}表示由NT SID权限创建的SID。

SubAuthority(变量):32位无符号整数 的可变长度数组,它唯一地标识一个主要相对于 IdentifierAuthority。它的长度由SubAuthorityCount决定。

/** 
* 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) 
    ); 
}