Einzelnen Beitrag anzeigen

Benutzerbild von Zacherl
Zacherl

Registriert seit: 3. Sep 2004
4.629 Beiträge
 
Delphi 10.2 Tokyo Starter
 
#8

Re: Vista: Administratorkonto oder reelle elevated Adminrech

  Alt 2. Sep 2008, 08:30
Hab mal das hier aus verschiedenen Funktionen, die ich hier im Forum gefunden habe gebastelt und mit Hilfe eurer Tips. Vielleicht hilft es ja jemandem:

Delphi-Quellcode:
type
  TAdminPrivileges = (
    apLimited,
    apAdmin,
    apFullAdmin
  );

function HasAdminPrivileges: TAdminPrivileges;

function GetAdminSid: PSID;
const
  SECURITY_NT_AUTHORITY: TSIDIdentifierAuthority =
    (Value: (0, 0, 0, 0, 0, 5));
  SECURITY_BUILTIN_DOMAIN_RID: DWORD = $00000020;
  DOMAIN_ALIAS_RID_ADMINS: DWORD = $00000220;
begin
  Result := nil;
  AllocateAndInitializeSid(SECURITY_NT_AUTHORITY, 2,
    SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
    0, 0, 0, 0, 0, 0, Result);
end;

const
  SE_GROUP_USE_FOR_DENY_ONLY = $00000010;

var
  TokenHandle : THandle;
  ReturnLength : DWORD;
  TokenInformation : PTokenGroups;
  AdminSid : PSID;
  Loop : Integer;
begin
  Result := apLimited;
  TokenHandle := 0;
  if OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, TokenHandle) then
  try
    ReturnLength := 0;
    GetTokenInformation(TokenHandle, TokenGroups, nil, 0, ReturnLength);
    TokenInformation := GetMemory(ReturnLength);
    if Assigned(TokenInformation) then
    try
      if GetTokenInformation(TokenHandle, TokenGroups, TokenInformation,
        ReturnLength, ReturnLength) then
      begin
        AdminSid := GetAdminSid;
        for Loop := 0 to TokenInformation^.GroupCount - 1 do
        begin
          if EqualSid(TokenInformation^.Groups[Loop].Sid, AdminSid) then
          begin
            if (TokenInformation^.Groups[Loop].Attributes and
              SE_GROUP_USE_FOR_DENY_ONLY) = SE_GROUP_USE_FOR_DENY_ONLY then
            begin
              Result := apAdmin;
            end
              else
            begin
              Result := apFullAdmin;
            end;
            Break;
          end;
        end;
        FreeSid(AdminSid);
      end;
    finally
      FreeMemory(TokenInformation);
    end;
  finally
    CloseHandle(TokenHandle);
  end;
end;
Diese Funktion gibt unter XP entweder apLimited für eingeschränkte Rechte oder apFullAdmin für Administratorrechte zurück. Ab Vista wird apAdmin zurückgegeben, wenn es sich um einen Administrator handelt, das aktuelle Programm aber nicht per UAC elevated gestartet wurde. Läuft das Programm mit uneingeschränkten Adminrechten, so wird hier apFullAdmin zurückgegeben.

Gruß Zacherl
  Mit Zitat antworten Zitat