Page 1 of 1 1
Topic Options
#85603 - 2002-05-02 08:00 PM Get SID
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
I posted this question to an existing thread Hacking...er...Editing - HKEY_CURRENT_USER (remotely)... but then decided this is a better place for it.

Has anyone pulled a user SID with ADSI?

This is what I've been able to glean from the help file:
"The value specified in a filter is the encoded byte string representation of the SID. See the discussion of encoded byte strings in the preceding discussion of the OctetString syntax."

Once you've pulled the SID, how would would you convert it to the format that is used in HKU of the registry?
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#85604 - 2002-05-02 08:34 PM Re: Get SID
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
The byte array encoding of ADSI SID`s is a real scripting language killer. That`s why someone came up with the script friendly ADsSecurity.dll resource kit component. Its not included with NT5, you can get it from here:

Reskit with ADsSecurity

Once you extract ADsSecurity.dll out of the reskit folder into system32, register it like this:

regsvr32 adssecurity.dll

Then you can use it to give you an ASCII string rep. of the SID:

break on

$adsid = createobject("ADsSid")

if $adsid
 $user = getobject("WinNT://development/shawn.tassie,user")
 if $user
  $= $adsid.setas(5,$user.adspath)
  $sid = $adsid.getas(1)
  ?"sid=" $sid
 endif
endif

exit 1


-Shawn

Top
#85605 - 2002-05-02 08:37 PM Re: Get SID
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
you can use the ADSID interface to pull the sid of a given user...

code:
$sidobj = createobject("adssid")
$sidobj.setas(5,"WinNT://@ldomain/@userid,user")
$sid = $sidobj.getas(1)

$sid will look like this.

010500000000000515000000FD77B15654FA5551235F636BE8030000

but for easy registry editing... you want a sid value like the one returned by the @sid macro.

S-1-5-21-1454471165-1364589140-1801674531-1000

Oh the math! [Smile]

[ 02 May 2002, 20:39: Message edited by: Bryce ]

Top
#85606 - 2002-05-02 08:39 PM Re: Get SID
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
I can give a Perl program that suits your need, but have yet to venture down that path in KiXtart. Shawn had a HexSid UDF but that isn't the right format.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#85607 - 2002-05-02 08:49 PM Re: Get SID
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Right you are, but you could use the liner notes of that UDF to write your own FormatSID() UDF [Wink]

With everyones forgiveness, I`m going to repost it here because I can never find this thing when I go looking for it:

code:

break on

;
; Redirect output to file: usage kix32 script $out=filename
;

if $out
 del "$out"
 $=redirectoutput("$out")
endif

; Get the domain object ...

$domain = getobject("WinNT://TASSIE")

; Filter for just groups ...

$domain.filter="group",""

; For ach group, display group name and getrid() ...

for each $object in $domain

 $rid = getrid($object.adspath)

 ?"object: " $object.name " rid: " $rid
next

exit

function GetRID($adspath)
 ;
 ;
 ; Function GetRID($ADsPath)
 ;
 ; Returns the Relative Identifier (RID) of the AD object specified
 ;
 ; Requires: 1) Windows 2000 or WindowsNT/9x /w ADSI installed
 ; 2) ADsSecurity.DLL (from the ADSI SDK)
 ;
 ; Usage:
 ; $rid = GetRID("WinNT://@LDOMAIN/@USERID,USER")
 ; $rid = GetRID("WinNT://@WKSTA/ADMINISTRATORS,GROUP")
 ;
 dim $adssid,$object,$sid,$sac,$rid
 ;
 ; Set initial function return value to zero
 ;
 $getrid = 0
 ;
 ; Create the ADsSID Security Object. This object (dll) is part of the MS ADSI 2.5 SDK
 ; available for download at Microsoft. ADsSecurity must be manually installed from the
 ; reskit with the command: regsvr32 adssecurity.dll
 ;
 $adssid = createobject("adssid")
 ;
 if $adssid = 0
  return
 endif
 ;
 ; Get a handle to the object specified in the function call ...
 ;
 $object = getobject("$adspath")
 ;
 if $object = 0
  $adssid=0
  return
 endif
 ;
 ; Call ADsSID to convert the objects ADsPath to a string version of the object's SID
 ; ADsSID is a black-box COM object. You put something in (SetAs) in a particular format
 ; (5=ADsPath), then you pull something out (GetAs) under a different format (1=SIDstring).
 ; This "SIDstring" is the elusive conversion of a non-variant byte-array that we are looking
 ; to achieve ...
 ;
 $adssid.setas(5,$adspath) ; put ADsPath in
 ;
 $sid = $adssid.getas(1) ; take SIDstring out
 ;
 ; Check to make sure we got a valid SID string ...
 ;
 if not $sid
  $object=0
  $adssid=0
  return
 endif
 ;
 ; EXTRACT RID FROM SID:
 ;
 ; ADsSID returns the Object's SID as a text string in the following format:
 ;
 ; 010500000000000515000000DCF4DC3B1525AF47A837D665F4010000
 ;
 ; This is actually the SID for the Administrator account on my Win2K workstation. The SID
 ; string can than be broken down into the following component parts:
 ;
 ; 01 - Revision
 ; 05 - SubAuthority Count
 ; 000000000005 - Identifier Authority
 ; 15000000 - SubAuthority 1 (DOMAIN INDENTIFIER)
 ; DCF4DC3B - SubAuthority 2 (DOMAIN INDENTIFIER)
 ; 1525AF47 - SubAuthority 3 (DOMAIN INDENTIFIER)
 ; A837D665 - SubAuthority 4 (DOMAIN INDENTIFIER)
 ; F4010000 - SubAuthority 5 (RID)
 ;
 ; Notice that the SubAuthority count is 5 and there are 5 trailing (matching) SubAuthorities. Most
 ; well known SIDs (Everyone,Users,Power Users) have only 2 SubAuthorities - so you must account
 ; for (n) number of these. The object's RID is always the last SubAuthority in the SID. As well,
 ; The number string is ass-backwards in terms of converting it to a real number (using val() and &)
 ; so we have to normalize this using some binary math ...
 ;
 ; Extract the SID SubAuthority Count (SAC) from the SID.
 ;
 $sac = val(substr($sid,3,2))
 ;
 if not $sac
  $object=0
  $adssid=0
  return
 endif
 ;
 ; Extract RID from SID (last sub-authority)
 ;
 $rid = substr($sid,17+($sac*8)-8,8)
 ;
 if not $rid
  $object=0
  $adssid=0
  return
 endif
 ;
 ; Convert RID little-endian , just re-reverse the bytes and convert to decimal ...
 ; Me smoke'm big peace-pipe when done - thanks for reading this far -
 ; For example this: "DCF4DC3B" becomes this: "3BDCF4DC"
 ;
 $getrid = val("&"+substr($rid,7,2)+substr($rid,5,2)+substr($rid,3,2)+substr($rid,1,2))
 ;
 ; Cleanup
 ;
 $object=0
 $adssid=0
 ;
endfunction


-Shawn

[ 02 May 2002, 20:50: Message edited by: Shawn ]

Top
#85608 - 2002-05-03 02:44 AM Re: Get SID
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Thanks Guys!

I took a bit of this and a bit of that, stirred well, and here's what I ended up with. Maybe someone could Murphy-proof it and make it into a UDF.
code:
break on

$sidobj = createobject("adssid")
$sidobj.setas(5,"WinNT://@ldomain/@userid,user")
$sid = $sidobj.getas(1)

"ADSI SID = " + $sid ?

$Rev = val(substr($sid,1,2))
$SAC = val(substr($sid,3,2))

$Result = "S-" + $Rev + "-" + $SAC
For $X = 1 to $SAC
$Result = $Result + "-"+Val("&"+substr($sid,8*$X+15,2)+substr($sid,8*$X+13,2)+substr($sid,8*$X+11,2)+substr($sid,8*$X+9,2))
Next


"Translated SID = "+ $Result ?


"KiX Macro @@SID = "+@SID ?

get $_

exit 1

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#85609 - 2002-05-03 03:49 AM Re: Get SID
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Les, great work.

I think that adding a line and altering the next makes it more accurate when parsing the hex sid. The Indentity Authority is a 48-bit string.
code:
$IdentAuth = Val("&" + substr($sid,5,12))

$Result = "S-" + $Rev + "-" + $IdentAuth

instead of
code:
$Result = "S-" + $Rev + "-" + $SAC  



[ 03 May 2002, 03:50: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#85610 - 2002-05-03 03:50 AM Re: Get SID
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
OK, wrapped it up as a UDF. If nobody finds fault with it I'll post it in the UDF section.

code:
break on

$SID = GetSID("WinNT://ff/lligetfa,USER")
? $SID

Get $_
Exit 1

function GetSID($adspath)
;Returns the ASCII Security Identifier (SID) of the ADSI object specified
;
; Requires:
; 1) Windows 2000 or WindowsNT/9x /w ADSI installed
; 2) ADsSecurity.DLL (from the ADSI SDK)
;
; Usage:
; $SID = GetSID("WinNT://@LDOMAIN/@USERID,USER")

dim $rev,$sidobj,$sid,$sac,$x
$GetSID=0

$sidobj=createobject("adssid")
$sidobj.setas(5,$adspath)
$sid=$sidobj.getas(1)

if not $sid
$sidobj=0
return
endif

$Rev=val(substr($sid,1,2))
; Extract the SubAuthority Count (SAC) from the SID
$SAC=val(substr($sid,3,2))

$GetSID="S-"+$Rev+"-"+$SAC
; Convert SID little-endian, just re-reverse the bytes and convert to decimal...

For $X=1 to $SAC
$GetSID=$GetSID+"-"+Val("&"+substr($sid,8*$X+15,2)+substr($sid,8*$X+13,2)+substr($sid,8*$X+11,2)+substr($sid,8*$X+9,2))
Next
$sidobj=0

EndFunction

Note: A revised (corrected version) of this code has been posted to GetSID() - Returns SID and converts it to string format

[ 04 May 2002, 20:23: Message edited by: LLigetfa ]
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#85611 - 2002-05-03 04:58 AM Re: Get SID
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Les, try this with your code.

Domain/administrators,group

This will demonstrate the code issue in the post just before your last post.

From MSDN: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/security/Security/sid_components.asp

quote:
The following example uses this notation to display the well-known domain-relative SID of the local Administrators group:

S-1–5-32-544



[ 03 May 2002, 05:00: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#85612 - 2002-05-03 05:07 AM Re: Get SID
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Howard,
I was going by Shawn's definition, taking the third and fourth character as the SubAuthority count. The fifth through sixteenth, listed as Identifier Authority does not appear in the formatted SID.
quote:

; 01 - Revision
; 05 - SubAuthority Count
; 000000000005 - Identifier Authority
; 15000000 - SubAuthority 1 (DOMAIN INDENTIFIER)
; DCF4DC3B - SubAuthority 2 (DOMAIN INDENTIFIER)
; 1525AF47 - SubAuthority 3 (DOMAIN INDENTIFIER)
; A837D665 - SubAuthority 4 (DOMAIN INDENTIFIER)
; F4010000 - SubAuthority 5 (RID)

If you have it on good authority, please share it with me.

I was aware that the value is stored as hex, but decided that it would be unlikely to exceed the value of 10 so no conversion would be necessary.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#85613 - 2002-05-03 05:14 AM Re: Get SID
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
I believe that Shawn's format is correct. My guess is that there is a little confusion. [Wink]
quote:
01 05 000000000005 15000000 BC2E7001 633DBD3D 0E6D055C F4010000
The second item is only a count that represents the number of items that follow the "Identifier Authority". Five in this case.

This is the hex sid of the domain/Administrators, group

01 02 000000000005 20000000 20020000

Two items follow the "Identifier Authority" -> 000000000005

[ 03 May 2002, 05:25: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#85614 - 2002-05-03 05:48 AM Re: Get SID
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Thanks for your input, Howard. In the end, I really didn't have a need. It was more of a challenge spurred on by that other thread.

BRRR! I just got the heebee jeebees... me programming for the fun of it... goes against my very lazy nature. I've always held myself to the principle that the work a program saves has to be greater than the effort to program it. What was I thinking?

Actually, it was Shawn's fault. He goaded me into it.

My entire logon script is only around 500 lines and most of that is to automate software installs.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#85615 - 2002-05-03 06:19 AM Re: Get SID
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
I know exactly what you're saying. Unfortunately, I am one of those sick pups that programs for fun. If you didn't post that code, I would have had to write it tomorrow.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
Page 1 of 1 1


Moderator:  Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 756 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.067 seconds in which 0.028 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org