简体   繁体   English

如何从SQL Server存储的SID获取Active Directory组名?

[英]How can I obtain an Active Directory Group name from a SQL Server stored SID?

This is a follow-up of a question I asked earlier this morning ( posted here .) Following the instructions provided, I've managed to query my SQL Server 2000 database for a SID associated with an AD Group. 这是我今早早些时候提出的一个问题的后续跟踪( 在这里发布 )。按照提供的说明,我设法查询我的SQL Server 2000数据库以查找与AD组关联的SID。 The SID, however, looks like this: 然而,SID看起来像这样:

0x0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567

What can I do to obtain the name of the AD Group referenced by the SID? 如何获取SID引用的AD组的名称? I've tried googling PowerShell scripts, however, most of their examples of SIDs look like this: 我尝试使用Google搜索PowerShell脚本,但是,他们的大多数SID示例都是这样的:

S-1-5-21-1454471165-1004335555-1606985555-5555

Obviously, that doesn't look like the value I'm getting back from the SQL Server. 显然,这看起来不像我从SQL Server返回的值。 How can I do this? 我怎样才能做到这一点?

If you're using sqlps (SQL Powershell host) which works against SQL 2000 (I've tested this on my 2000 instance) you can use this: 如果您正在使用sqlps(SQL Powershell主机),它可以对抗SQL 2000(我在2000实例上测试了这个),您可以使用:

$query = @"
select sid from syslogins where isntgroup = 1
AND name = 'CONTOSO\mylogin'
"@

invoke-sqlcmd -ServerInstance "myserver" -Database master -Query $query | 
foreach {$SID = new-object security.principal.securityidentifier($_.SID,0); $SID.translate([system.security.principal.NTAccount]) }

For those without sqlps: use this online C# shell do format single sid to text 对于那些没有sqlps的人:使用这个在线C#shell做单格式sid到文本

http://rextester.com/AFAC13570 http://rextester.com/AFAC13570

code backup: 代码备份:

//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text;
using System.Runtime.Remoting.Metadata.W3cXsd2001;

namespace Rextester
{
    public class Program
    {

        public static string ConvertByteToStringSid(Byte[] sidBytes)
        {

            StringBuilder strSid = new StringBuilder();
            strSid.Append("S-");

            // Add SID revision.
            strSid.Append(sidBytes[0].ToString());
            // Next six bytes are SID authority value.
            if (sidBytes[6] != 0 || sidBytes[5] != 0)
            {
                string strAuth = String.Format
                ("0x{0:2x}{1:2x}{2:2x}{3:2x}{4:2x}{5:2x}",
                (Int16)sidBytes[1],
                (Int16)sidBytes[2],
                (Int16)sidBytes[3],
                (Int16)sidBytes[4],
                (Int16)sidBytes[5],
                (Int16)sidBytes[6]);
                strSid.Append("-");
                strSid.Append(strAuth);
            }
            else
            {
                Int64 iVal = (Int32)(sidBytes[1]) +
                (Int32)(sidBytes[2] << 8) +
                (Int32)(sidBytes[3] << 16) +
                (Int32)(sidBytes[4] << 24);
                strSid.Append("-");
                strSid.Append(iVal.ToString());
            }

            // Get sub authority count...
            int iSubCount = Convert.ToInt32(sidBytes[7]);
            int idxAuth = 0;
            for (int i = 0; i < iSubCount; i++)
            {
                idxAuth = 8 + i * 4;

                if (idxAuth >= sidBytes.Length)
                {
                    Console.WriteLine("OK :old NT account");
                    return strSid.ToString();
                }

                UInt32 iSubAuth = BitConverter.ToUInt32(sidBytes, idxAuth);
                strSid.Append("-");
                strSid.Append(iSubAuth.ToString());
            }
            return strSid.ToString();
        } 

        public static void Main(string[] args)
        {
            //Your code goes here
            Console.WriteLine(
                ConvertByteToStringSid(
                    SoapHexBinary.Parse(
                        "0x01050000000000051500000079542007311FAE6D096510145E540300".Substring(2)
                    ).Value
                )
            );
        }
    }
}

credits: 积分:

https://www.sqlservercentral.com/Forums/FindPost1322822.aspx https://www.sqlservercentral.com/Forums/FindPost1322822.aspx

How do you convert Byte Array to Hexadecimal String, and vice versa? 如何将字节数组转换为十六进制字符串,反之亦然?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查SQL Server SID与Active Directory SID - How to check SQL Server SID vs Active Directory SID 如何从.Net文化获取Sql语言名称 - How can i obtain an Sql Language name from a .Net Culture 如何在 SQL 服务器中添加 Active Directory 用户组作为登录名 - How to add Active Directory user group as login in SQL Server 如何为Active Directory域组检索SQL Server数据库角色 - How to retrieve SQL Server database roles for an Active Directory domain group 需要将 Active Directory 用户添加到 SQL Server Active Directory 组 - Need to add Active Directory User to SQL Server Active Directory Group Active Directory组添加到SQL Server - Active Directory group add to SQL Server 如果用户通过 Active Directory 身份验证,如何将 ODBC 连接字符串发送到 SQL 服务器? - How can I send an ODBC connection string to an SQL server if the user is authenticated by Active Directory? 如何在没有Windows身份验证的情况下使用Active Directory用户在SQL SERVER 2008中进行身份验证 - How i can authenticate in SQL SERVER 2008 with Active Directory User but without Windows Authentication 如何在链接到SQL Server的Microsoft Access中使用Active Directory登录? - How can I use an Active Directory login in Microsoft Access linked to SQL Server? 如何查询与SQL Server用户关联的Active Directory帐户? - How can I query an which Active Directory account is associated with a SQL Server user?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM