• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1046
  • Last Modified:

need a script to convert SID from Hex to binary?

can someone please provide a script that will convert a list of SID's from HEX format:
X'010500000000000515000000492e24037d1903770052cf126ea20000'

to decimal format:
S-1-5-21-207860035-1181422338--730112994-1007

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

thanks!

Mike
0
siber1
Asked:
siber1
  • 2
1 Solution
 
Ken FayalCommented:
Here's the exact site you want to visit.  Shows how it's done.

http://froosh.wordpress.com/2005/10/21/hex-sid-to-decimal-sid-translation/
0
 
siber1Author Commented:
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
0
 
oBdACommented:
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.
010500000000000515000000492e24037d1903770052cf126ea20000=S-1-5-21-52702793-1996691837-315576832-41582
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
  Loop
End Function
 
Function EndianReverse(strHex)
  Dim intCounter
  For intCounter = Len(strHex) to 1 Step - 2
    EndianReverse = EndianReverse & Mid(strHex, intCounter - 1, 2)
  Next
End Function
 
' ----- Main
strHex = wscript.Arguments(0)
strSid = ConvertHexStringToSidString(strHex)
wscript.echo strHex & "=" & strSid

Open in new window

0
 
siber1Author Commented:
excellent! just what I was looking for.
thanks so much oBda!
Mike
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now