GetSid() - Security Identifier (SID) fetching [Win_Func]

Share your advanced PureBasic knowledge/code with the community.
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

GetSid() - Security Identifier (SID) fetching [Win_Func]

Post by Thunder93 »

Code: Select all

Prototype ConvertSidToStringSid(Sid, *StringSid)

Procedure Advapi32_Init()
  Shared Lib_Advapi32, cExt.s
  Protected Retr.b
  
  CompilerIf #PB_Compiler_Unicode : cExt = "W" : CompilerElse : cExt = "A" : CompilerEndIf  
  
  Lib_Advapi32 = OpenLibrary(#PB_Any,"Advapi32.dll")
  If Lib_Advapi32
    Global ConvertSidToStringSid.ConvertSidToStringSid = GetFunction(Lib_Advapi32,"ConvertSidToStringSid"+cExt)
    Retr = 1    
  EndIf
  
  ProcedureReturn Retr
EndProcedure

Procedure Advapi32_End()
  Shared Lib_Advapi32
  CloseLibrary(Lib_Advapi32)
EndProcedure

Procedure.s GetSid(AccountName.s = "")
  Protected cbSID.l, lDomainName.s, cbDomainName.l, SIDType.i, SID.s
  
  If Advapi32_Init() = 0 : Debug "Advapi32_Init failed" : ProcedureReturn "" : EndIf
  
  If AccountName = ""
    lpBuffer.s = Space(#UNLEN+1)
    lpnSize.l = #UNLEN+1
    
    If GetUserName_(@lpBuffer, @lpnSize)
      AccountName = lpBuffer
    EndIf    
  EndIf  
  
  
  If Not LookupAccountName_(0, @AccountName, #Null, @cbSID, #Null, @cbDomainName, @SIDType)
    If GetLastError_() = #ERROR_INSUFFICIENT_BUFFER
      
      *ptrSid = AllocateMemory(cbSid)
      If Not *ptrSid : Debug "*ptrSid memory allocation failed" : ProcedureReturn "" : EndIf
      
      lDomainName = Space(cbDomainName)
      
      If LookupAccountName_(0, @AccountName, *ptrSid, @cbSID, @lDomainName, @cbDomainName, @SIDType)        
        
        If ConvertSidToStringSid(*ptrSid, @StringSid)
          FreeMemory(*ptrSid)
          
          SID = PeekS(StringSid)
          LocalFree_(StringSid)
        EndIf
        
        ProcedureReturn SID        
      EndIf
    EndIf
  EndIf
  
  Advapi32_End()
EndProcedure

Procedure.s GetComputerName()
  Protected buffer.s=Space(64), bufsize.l=64
  GetComputerName_(@buffer, @bufsize)
  
  ProcedureReturn buffer
EndProcedure

Debug "Domain name: "+GetSid(GetComputerName())
Debug "Current User: "+GetSid()
Debug ""
Debug "Administrator: "+GetSid("Administrator")
Debug "Guest: "+GetSid("Guest")
Debug "DefaultAccount: "+GetSid("DefaultAccount")
Debug ""
Debug "SYSTEM: "+GetSid("SYSTEM")
Debug "LocalService: "+GetSid("LocalService")
Debug "NetworkService: "+GetSid("NetworkService")
Debug ""
Debug "Administrators: "+GetSid("Administrators")
Debug "Users: "+GetSid("Users")
Debug "Guests: "+GetSid("Guests")



Debug ""
Debug "- Built-In Local Groups"
Debug GetSid("BUILTIN\ADMINISTRATORS")
Debug GetSid("BUILTIN\USERS")
Debug GetSid("BUILTIN\GUESTS")
Debug ""
Debug "- Special Groups"
Debug GetSid("EVERYONE")
Debug GetSid("Local")
Debug GetSid("CREATOR OWNER")
Debug GetSid("Creator Group")
Debug GetSid("NT AUTHORITY\NETWORK")
Debug GetSid("NT AUTHORITY\INTERACTIVE")
Debug GetSid("NT AUTHORITY\SYSTEM")
Debug GetSid("NT AUTHORITY\Authenticated Users")

Edited: Revised #1, 2016-08-17 4:56PM UTC/GMT
Last edited by Thunder93 on Wed Aug 17, 2016 5:56 pm, edited 1 time in total.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
RSBasic
Moderator
Moderator
Posts: 1228
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: GetSid() - Security Identifier (SID) fetching [Win_Func]

Post by RSBasic »

Image
Image
Image
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: GetSid() - Security Identifier (SID) fetching [Win_Func]

Post by Thunder93 »

Thanks RSBasic!

I have revised it. :wink:
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Post Reply