AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Win32/Win64 API (native code) Delphi Prüfen, ob ein Prozess ein SYSTEM Prozess ist
Thema durchsuchen
Ansicht
Themen-Optionen

Prüfen, ob ein Prozess ein SYSTEM Prozess ist

Ein Thema von Zacherl · begonnen am 28. Aug 2008 · letzter Beitrag vom 28. Aug 2008
Antwort Antwort
Benutzerbild von Zacherl
Zacherl

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

Prüfen, ob ein Prozess ein SYSTEM Prozess ist

  Alt 28. Aug 2008, 16:02
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
  Mit Zitat antworten Zitat
Benutzerbild von Zacherl
Zacherl

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

Re: Prüfen, ob ein Prozess ein SYSTEM Prozess ist

  Alt 28. Aug 2008, 16:27
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 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
  Mit Zitat antworten Zitat
Dezipaitor

Registriert seit: 14. Apr 2003
Ort: Stuttgart
1.701 Beiträge
 
Delphi 7 Professional
 
#3

Re: Prüfen, ob ein Prozess ein SYSTEM Prozess ist

  Alt 28. Aug 2008, 19:13
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.
Christian
Windows, Tokens, Access Control List, Dateisicherheit, Desktop, Vista Elevation?
Goto: JEDI API LIB & Windows Security Code Library (JWSCL)
  Mit Zitat antworten Zitat
Benutzerbild von Zacherl
Zacherl

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

Re: Prüfen, ob ein Prozess ein SYSTEM Prozess ist

  Alt 28. Aug 2008, 20:24
Ah cool danke dir, die JWSCL sehe ich mir gleich mal an. Mit den angepassen RIDs klapt es jetzt allerdings auch
  Mit Zitat antworten Zitat
Dezipaitor

Registriert seit: 14. Apr 2003
Ort: Stuttgart
1.701 Beiträge
 
Delphi 7 Professional
 
#5

Re: Prüfen, ob ein Prozess ein SYSTEM Prozess ist

  Alt 28. Aug 2008, 20:36
Übersicht gibt es auch hier:
http://jwscldoc.delphi-jedi.net
Christian
Windows, Tokens, Access Control List, Dateisicherheit, Desktop, Vista Elevation?
Goto: JEDI API LIB & Windows Security Code Library (JWSCL)
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 21:13 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