need a script to convert SID from Hex to binary?

Posted on 2009-02-19
Last Modified: 2012-05-06
can someone please provide a script that will convert a list of SID's from HEX format:

to decimal format:

I have over 400 to convert, so a script that would call an input text or csv file would be ideal.


Question by:siber1
    LVL 9

    Expert Comment

    by:Ken Fayal
    Here's the exact site you want to visit.  Shows how it's done.

    Author Comment

    thanks. but i was already aware of that link before I posted this. That code that is posted doesn't seem to work for me.
    does anyone else have another script i can try.
    again, i have to convert 300+ SID's from hex to decimal.

    thx - Mike
    LVL 82

    Accepted Solution

    Save the condensed version below as Hex2Sid.vbs. The script expects the hex string to to convert as argument, and will echo out the hex string, an equal sign, and the sid string.
    Run this using cscript for several conversions using the command line and a list of input hex string (hex.txt) with the following command:
    for /f %a in ('type hex.txt') do @cscript /nologo Hex2Sid.vbs %a
    You can capture the output by redirecting it to a file (sid.txt):
    for /f %a in ('type hex.txt') do @cscript /nologo Hex2Sid.vbs %a>>"sid.txt"

    Function ConvertHexStringToSidString(strHex)
      Const intSidVersionLength = 2
      Const intSubAuthorityCountLength = 2
      Const intAuthorityIdentifierLength = 12
      Const intSubAuthorityLength = 8
      Dim intStringPosition, bytSidVersion, lngAuthorityIdentifier, bytSubAuthorityCount, lngTempSubAuthority
      intStringPosition = 1
      bytSidVersion = CByte("&h" & Mid(strHex, intStringPosition, intSidVersionLength))
      intStringPosition = intStringPosition + intSidVersionLength
      bytSubAuthorityCount = CByte("&h" & Mid(strHex, intStringPosition, intSubAuthorityCountLength))
      intStringPosition = intStringPosition + intSubAuthorityCountLength
      lngAuthorityIdentifier = CLng("&h" & Mid(strHex, intStringPosition, intAuthorityIdentifierLength))
      intStringPosition = intStringPosition + intAuthorityIdentifierLength
      ConvertHexStringToSidString = "S-" & bytSidVersion & "-" & lngAuthorityIdentifier
      Do Until bytSubAuthorityCount = 0
        lngTempSubAuthority = CLng("&h" & EndianReverse(Mid(strHex, intStringPosition, intSubAuthorityLength)))
        intStringPosition = intStringPosition + intSubAuthorityLength
        If lngTempSubAuthority < 0 Then lngTempSubAuthority = lngTempSubAuthority + 2^32
        ConvertHexStringToSidString = ConvertHexStringToSidString & "-" & lngTempSubAuthority
        bytSubAuthorityCount = bytSubAuthorityCount - 1
    End Function
    Function EndianReverse(strHex)
      Dim intCounter
      For intCounter = Len(strHex) to 1 Step - 2
        EndianReverse = EndianReverse & Mid(strHex, intCounter - 1, 2)
    End Function
    ' ----- Main
    strHex = wscript.Arguments(0)
    strSid = ConvertHexStringToSidString(strHex)
    wscript.echo strHex & "=" & strSid

    Open in new window


    Author Closing Comment

    excellent! just what I was looking for.
    thanks so much oBda!

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Installing a printer using group policy preferences is not that hard let’s take a look at it. First lets open up your group policy console and edit the policy you want to add it to. I recommend creating a new policy for each printer makes it a l…
    Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
    This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
    The viewer will learn how to dynamically set the form action using jQuery.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now