Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi Prüfen, ob ein Prozess ein SYSTEM Prozess ist (https://www.delphipraxis.net/119580-pruefen-ob-ein-prozess-ein-system-prozess-ist.html)

Zacherl 28. Aug 2008 16:02


Prüfen, ob ein Prozess ein SYSTEM Prozess ist
 
Hey,

ich würde gerne prüfen, ob der eigene Prozess ein SYSTEM Prozess, ein Prozess des LOKALEN DIENSTES oder ein Prozess des NETZWERKDIENSTES ist. Dachte mir das in etwa so:

Delphi-Quellcode:
function IsSystemProcess: Boolean;

type
  PTOKEN_USER = ^TOKEN_USER;
  _TOKEN_USER = record
    User: TSidAndAttributes;
  end;
  TOKEN_USER = _TOKEN_USER;

const
  SECURITY_LOCAL_SYSTEM_RID = $00000012;

var
  hToken: THandle;
  cbBuf: Cardinal;
  ptiUser: PTOKEN_USER;

  bSuccess: Boolean;
  SystemSid: TSIDIdentifierAuthority;
  pSystemSid: PSID;
begin
  Result := false;
 
  if OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, hToken) then
  begin
    try
      ptiUser := nil;
      bSuccess := GetTokenInformation(hToken, TokenUser, nil, 0, cbBuf);
      while (not bSuccess) and (GetLastError = ERROR_INSUFFICIENT_BUFFER) do
      begin
        ReallocMem(ptiUser, cbBuf);
        bSuccess := GetTokenInformation(hToken, TokenUser, ptiUser, cbBuf,
          cbBuf);
      end;
      if bSuccess then
      begin
        AllocateAndInitializeSID(SystemSid, 1, SECURITY_LOCAL_SYSTEM_RID, 0, 0,
          0, 0, 0, 0, 0, pSystemSid);
        Result := EqualSid(ptiUser.User.Sid, pSystemSid);
      end;
    finally
      if Assigned(ptiUser) then
      begin
        FreeMem(ptiUser);
      end;
      CloseHandle(hToken);
    end;
  end;
end;
Gibt allerdings immer false zurück. Mach ich was falsch?

Gruß Zacherl

Zacherl 28. Aug 2008 16:27

Re: Prüfen, ob ein Prozess ein SYSTEM Prozess ist
 
So gehts mit den SYSTEM Prozessen schonmal, das hab ich getestet:

Delphi-Quellcode:
function IsSystemProcess: Boolean;

type
  PTOKEN_USER = ^TOKEN_USER;
  _TOKEN_USER = record
    User: TSidAndAttributes;
  end;
  TOKEN_USER = _TOKEN_USER;

const
  SECURITY_NT_AUTHORITY = 5;
  SECURITY_LOCAL_SYSTEM_RID = $00000012;

var
  hToken: THandle;
  cbBuf: Cardinal;
  ptiUser: PTOKEN_USER;
  bSuccess: Boolean;
  SystemSid: TSIDIdentifierAuthority;
  pSystemSid: PSID;
begin
  Result := false;
 
  if OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, hToken) then
  begin
    try
      ptiUser := nil;
      bSuccess := GetTokenInformation(hToken, TokenUser, nil, 0, cbBuf);
      while (not bSuccess) and (GetLastError = ERROR_INSUFFICIENT_BUFFER) do
      begin
        ReallocMem(ptiUser, cbBuf);
        bSuccess := GetTokenInformation(hToken, TokenUser, ptiUser, cbBuf,
          cbBuf);
      end;
      if bSuccess then
      begin
        SystemSid.Value[0] := 0;
        SystemSid.Value[1] := 0;
        SystemSid.Value[2] := 0;
        SystemSid.Value[3] := 0;
        SystemSid.Value[4] := 0;
        SystemSid.Value[5] := SECURITY_NT_AUTHORITY;
        pSystemSID := nil;
        AllocateAndInitializeSID(SystemSid, 1, SECURITY_LOCAL_SYSTEM_RID, 0, 0,
          0, 0, 0, 0, 0, pSystemSid);
        Result := EqualSid(PSID(ptiUser^.User.Sid), pSystemSid);
      end;
    finally
      if Assigned(ptiUser) then
      begin
        FreeMem(ptiUser);
      end;
      CloseHandle(hToken);
    end;
  end;
end;
Bin aber nicht sicher, ob NETZWERKDIENST und LOKALER DIENST Prozesse da mit eingeschlossen werden. Habe eine AppInit DLL mit dem Code erstellt, welche pro Prozess eine Datei mit der PID und Ja oder Nein dropt, da Systemprozesse ja z.b. auf einem anderen Desktop agieren und somit keine Nachrichten ausgeben können :mrgreen: Bei den NETZWERKDIENST und LOKALER DIENST Prozessen wird allerdings nichts auf die Platte geschrieben von daher kann ich das nicht testen. Gibts da noch ne zweite SID auf die ich prüfen müsste?

Gruß Zacherl

Dezipaitor 28. Aug 2008 19:13

Re: Prüfen, ob ein Prozess ein SYSTEM Prozess ist
 
Du musst schon den relativen Sicherheitsbezeichner (RID) anpassen:

Du verwendest für SYSTEM: SECURITY_LOCAL_SYSTEM_RID
hier gibts die anderen:
http://msdn.microsoft.com/en-us/libr...49(VS.85).aspx

PS.
Weißt du, dass diese ganzen Strukturen (PTOKEN_USER) bereits durch die JEDI API units (JwaWinNT) definiert sind?

Die JWSCL bietet zudem noch eine leichtere Lösung von diese Art von Problem.

Zacherl 28. Aug 2008 20:24

Re: Prüfen, ob ein Prozess ein SYSTEM Prozess ist
 
Ah cool danke dir, die JWSCL sehe ich mir gleich mal an. Mit den angepassen RIDs klapt es jetzt allerdings auch :)

Dezipaitor 28. Aug 2008 20:36

Re: Prüfen, ob ein Prozess ein SYSTEM Prozess ist
 
Übersicht gibt es auch hier:
http://jwscldoc.delphi-jedi.net


Alle Zeitangaben in WEZ +1. Es ist jetzt 11:49 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz