PHP LDAP获取用户SID

PHP LDAP获取用户SID,php,active-directory,ldap,Php,Active Directory,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", "m

我不知道如何在广告中获取用户唯一标识符(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];

在第一个输出中,我可以在secound中看到用户名,类似于
0�@�d^�WL7�U

我相信sid应该像
S-…

我在另一个网站上找到了解决方案(见下文)。 基本上,此函数是转换器,使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;
}
公共静态函数SIDtoString($ADsid)
{
$sid=“S-”;
//$ADguid=$info[0]['objectguid'][0];
$sidinhex=str_split(bin2hex($ADsid),2);
//字节0=修订级别
$sid=$sid.hexdec($sidinhex[0])。“-”;
//字节1-7=48位权限
$sid=$sid.hexdec($sidinhex[6]。$sidinhex[5]。$sidinhex[4]。$sidinhex[3]。$sidinhex[2]。$sidinhex[1]);
//字节8子权限计数-获取子权限数
$subauths=hexdec($sidinhex[7]);
//循环通过次级机构
对于($i=0;$i<$subauths;$i++){
$start=8+(4*$i);
//X 32位(4字节)子权限的数量
$sid=$sid.“-”.hexdec($sidinhex[$start+3]。$sidinhex[$start+2]。$sidinhex[$start+1]。$sidinhex[$start]);
}
返回$sid;
}

作为一个替代示例,完全可以使用PHP的unpack函数来完成。objectSid二进制结构最好记录在:

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

子权限计数(1字节):一个8位无符号整数,用于指定 子权限数组中的元素数。最大数量 允许的元素数为15

标识符权限(6字节):SID\u标识符\u权限结构 表示创建SID所依据的权限。信息技术 描述创建SID的实体。标识机构 值{0,0,0,0,0,5}表示NT SID机构创建的SID

子权限(变量):无符号32位的可变长度数组 唯一标识主体相对于对象的整数 识别权限。其长度由SubAuthorityCount确定

/**
*将二进制SID解码为其可读形式。
*
*@param字符串$value
*@返回字符串
*/
函数decodeSID($value)
{
#修订版-8位无符号整数(C1)
#计数-8位无符号整数(C1)
#2个空字节
#ID-32位无符号长、大端顺序
$sid=@unpack('C1rev/C1count/x2/N1id',$value);
$subAuthorities=[];
如果(!isset($sid['id'])| |!isset($sid['rev'])){
抛出新的\UnexpectedValueException(
'解码SID时未找到修订级别或标识符权限。'
);
}
$revisionLevel=$sid['rev'];
$identifierAuthority=$sid['id'];
$subs=isset($sid['count'])?$sid['count']:0;
//子权限取决于计数,因此只获取与计数相同的数量,而不考虑计数之外的数据
对于($i=0;$i<$subs;$i++){
#每个子身份验证都是32位无符号长、小尾端顺序
$subAuthorities[]=unpack('V1sub',hex2bin(substr(bin2hex($value),16+($i*8),8))['sub'];
}
#用大头钉在“S-”上,然后把它粘在一起。。。
返回'S-'.$revisionLevel.-'.$identifierAuthority.Inbode(
preg_过滤器(“/^/”、“-”、$subAuthorities)
);
}

这适用于64位系统,我认为更简洁

function bin_to_str_sid($binsid) {
    $parts = unpack('Crev/x/nidhigh/Nidlow', $binsid);
    $ssid = sprintf('S-%u-%d',  $parts['rev'], ($parts['idhigh']<<32) + $parts['idlow']);
    $parts = unpack('x8/V*', $binsid);
    if ($parts) $ssid .= '-';
    $ssid .= join('-', $parts);
    return $ssid;
}
函数bin\u到\u str\u sid($binsid){
$parts=unpack('Crev/x/nidhigh/Nidlow',$binsid);

$ssid=sprintf('S-%u-%d',$parts['rev'],($parts['idhigh']是一篇老文章,但我做了一些我觉得有用的组合,这个函数是二进制到SID转换的最终函数:

// 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;
}
//二进制到SID
函数bin_to_str_sid($binary_sid){
$sid=NULL;
/*64bt PHP*/
if(strlen(decbin(~0))==64)
{
//获取修订、标识、权限
$parts=unpack('Crev/x/nidhigh/Nidlow',$binary\u sid);
//设置修订、标识、权限

$sid=sprintf('S-%u-%d',$parts['rev'],($parts['idhigh'])谷歌搜索'PHP ldap get sid'的前两个链接提供了一些值得尝试的代码:(请参阅derek dot ethier的评论)&在
$attributes
-数组中添加一个“+”,然后查看结果中的内容。这可能会揭示一些其他信息。
function bin_to_str_sid($binsid) {
    $parts = unpack('Crev/x/nidhigh/Nidlow', $binsid);
    $ssid = sprintf('S-%u-%d',  $parts['rev'], ($parts['idhigh']<<32) + $parts['idlow']);
    $parts = unpack('x8/V*', $binsid);
    if ($parts) $ssid .= '-';
    $ssid .= join('-', $parts);
    return $ssid;
}
// 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;
}