Link to home
Start Free TrialLog in
Avatar of mknoke
mknoke

asked on

SID to group name - Active Directory

I have a SID in the format of S-1-2.... and I need to query an Active Directory to get the group name that corresponds to the respective SID utilizing Visual Basic 6.0.  Any help that anyone can provide will be greatly appreciated.
Avatar of David Lee
David Lee
Flag of United States of America image

A group's SID is stored in the Group object as a property called objectSID.  The first two links are to pages that document the property.  The third link is to a page with a VBScript example that includes code for converting the objectSID property from its native form into the string we are used to seeing.  The VBScript code is easily converted into pure VB.  You can use then use that code in a loop that itterates through the groups in your AD structure looking for a match.  You don't indicate in your question whether you just need code to compare the SIDs or whether you also need code to loop through the groups.  If you need the latter, then I can help with that too.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sds/sds/octet_string__sid__property_type.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ad/ad/group_objects.asp
http://www.rlmueller.net/Programs/IsMember8.txt
Avatar of mknoke
mknoke

ASKER

What I am looking for is to pass VB Code a SID, which will correlate to a group name.  I want that code to return the actual group name that we are used to seeing.  I do not need to determine the users in that group, I just need the group name.  Do you have some code that can do that for me?

Thanks!
Ok, let me see if I can put something together that does that.
I believe I figured out how to do it.  I'll post code tomorrow.
Here's the code.  It really needs to go into a separate module (.bas file).  Do not insert an Option Base 1 command in the file.  Not only would you have to fix all of my array references from 0 based to 1 based, but I discovered during testing that the code just would not work with 1 based arrays.  I tested this code in my domain, searching for several different SIDs.  It correctly found them.  Be sure and include a reference to "Active DS Type Library" in your project.

Usage:  x = GetGroupNameFromSID("Domain","SID")
    Where: Domain is the name of the domain you want to search for the group in
                SID is the SID of the group that you want to search for
    Returns: The name of the group with the searched for SID

Dim arrSID(8) As Long

Public Function GetGroupNameFromSID(ByVal strDomain As String, ByVal strSID As String) As String
    Dim objDomain As IADsDomain, _
        objGroup As IADsGroup, _
        strTemp As String, _
        strMySID As String, _
        intStart As Integer, _
        intCounter As Integer, _
        strRetVal As String
    strRetVal = "Not Found"
    Set objDomain = GetObject("WinNT://" & strDomain)
    objDomain.Filter = Array("Group")
    For Each objGroup In objDomain
        objGroup.GetInfo
        strTemp = OctetToHexStr(objGroup.objectsid)
        arrSID(0) = Mid(strTemp, 1, 2)
        arrSID(1) = CLng(Mid(strTemp, 3, 2)) + 2        'The number of segments in this SID.
        arrSID(2) = BigEndian(Mid(strTemp, 5, 12))
        strMySID = "S-" & arrSID(0) & "-" & arrSID(2)
        intStart = 17
        For intCounter = 3 To arrSID(1)
            arrSID(intCounter) = LittleEndian(Mid(strTemp, intStart, 8))
            strMySID = strMySID & "-" & arrSID(intCounter)
            intStart = intStart + 8
        Next
        If strMySID = strSID Then
            strRetVal = objGroup.Name
            Exit For
        End If
    Next
    Set objGroup = Nothing
    Set objDomain = Nothing
    GetGroupNameFromSID = strRetVal
End Function

Private Function OctetToHexStr(arrbytOctet)
' This function from: http://www.rlmueller.net/Programs/IsMember8.txt
' I've modified it from its original version.
' Function to convert OctetString (byte array) to Hex string
    Dim k
    OctetToHexStr = ""
    For k = 1 To LenB(arrbytOctet)
        OctetToHexStr = OctetToHexStr & _
        Right("0" & Hex(AscB(MidB(arrbytOctet, k, 1))), 2)
    Next
End Function

Private Function BigEndian(strValue As String) As Long
' Function to convert a hex string stored in Big-Endian format
    Dim strBuffer As String
    strBuffer = "&" & strValue
    BigEndian = CLng(strBuffer)
End Function

Private Function LittleEndian(strValue As String) As Long
' Function to convert a hex string stored in Little-Endian format
    Dim strBuffer As String
    strBuffer = "&H" & Mid(strValue, 7, 2) & _
        Mid(strValue, 5, 2) & _
        Mid(strValue, 3, 2) & _
        Mid(strValue, 1, 2)
    LittleEndian = strBuffer
End Function
Avatar of mknoke

ASKER

Thanks for the quick response.  Is there any way that it can search only in a particular OU in the AD?  Or does this code only offer searching by the high level domain.  Thank you for all of your help.
The code above searches the entire domain.  I suspect I can modify it to search a single OU though.  Is that what you need?
Avatar of mknoke

ASKER

Yes, that is what I need.  Thanks again.
ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial