AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Win32/Win64 API (native code) Delphi CreateFile und Security Attributes
Thema durchsuchen
Ansicht
Themen-Optionen

CreateFile und Security Attributes

Ein Thema von Dezipaitor · begonnen am 27. Jun 2007 · letzter Beitrag vom 10. Dez 2007
Antwort Antwort
Dezipaitor

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

CreateFile und Security Attributes

  Alt 27. Jun 2007, 21:54
hi

das folgende Problem habe ich schon vor längerer Zeit festgestellt. Jedoch bis jetzt nichts dazu finden können. Vielleicht kennt ein einsamer Wanderer, der hier zufällig vorbeikommt die Lösung oder weitere Materialien.

Das Problem habe ich bei CreateFile mit dem Parameter SecurityAttributes. Wenn ich eine eigene DACL dort hineinlege, dann wird diese ignoriert und stattdessen, werden die Sicherheitseinstellungen vom Elternordner übernommen.

Hier gebe ich mal den Quelltext meines Unit-Testings an, die meine SecurityLibrary testen soll.
Es sollte in der Datei SecurityXXXX.pas kompilierbar sein

Delphi-Quellcode:
function TSecureFileObjectTests.GetSecurityFileNameTemp(
  out HasSACL: Boolean): String;

var
  pTempName : Array[0..MAX_PATH+1] of char;
  bI : Boolean;
  pSecAttr : jwaWinBase.PSecurityAttributes;
  handle : THANDLE;
  SDesc : TSecurityDescriptor;
  pDACL : jwaWinNT.PACL;
  b : Boolean;
  c : Cardinal;


begin
  CheckNotEquals(0,
        GetTempFileName('.', // LPCTSTR lpPathName,
                        'SMLTest', // LPCTSTR lpPrefixString,
                        0, // UINT uUnique,
                        @pTempName // LPTSTR lpTempFileName
                        ));
  //File does not exist!!

  SDesc := TSecurityDescriptor.Create;
  SDesc.Owner := SecurityThreadUserSID; //current process user
  SDesc.OwnOwner := false;
 // SDesc.DACL.Add(TDiscretionaryAccessControlEntryAllow.Create(nil,[],FILE_ALL_ACCESS,SecurityThreadUserSID, false));
 // SDesc.DACL.Add(TDiscretionaryAccessControlEntryAllow.Create(nil,[],FILE_ALL_ACCESS,LocalSystemSID, false));
 // SDesc.DACL.Add(TDiscretionaryAccessControlEntryAllow.Create(nil,[af_NO_PROPAGATE_INHERIT_ACE],FILE_ALL_ACCESS,WorldSID, false));
  SDesc.DACL.Add(TDiscretionaryAccessControlEntryAllow.Create(nil,[],FILE_ALL_ACCESS,GuestsSID, false));


  if IsPrivilegeSet(SE_SECURITY_NAME) then
  begin
    EnablePrivilege(SE_SECURITY_NAME, pst_Enable);

    SDesc.SACL.Add(TAuditAccessControlEntry.Create(nil,true, false, FILE_ALL_ACCESS,SDesc.Owner,false));
    SDesc.SACL.Add(TAuditAccessControlEntry.Create(nil,true, false, FILE_ALL_ACCESS,WorldSID,false));

    HasSACL := true;
  end
  else
    HasSACL := false;

  result := '';
  try
   //create SA strcture with no inheritance (false))
    pSecAttr := SDesc.Create_SA(false);


    handle := jwaWindows.CreateFile(pTempName,
               FILE_ALL_ACCESS,
               0,
               Pointer(pSecAttr),
               CREATE_ALWAYS,
               FILE_ATTRIBUTE_NORMAL,
               0);
    CheckNotEquals(Integer(INVALID_HANDLE_VALUE),Integer(handle), ESMSecurityException.GetLastErrorMessage());

    CloseHandle(handle);
//XXXXXXXXXXXXXXx
    //another way to set security without using TSecureFileObject
    //remove old dac
    TSecureGeneralObject.SetNamedSecurityInfo(pTempName,
                SE_FILE_OBJECT,
                [sif_OWNER_SECURITY_INFORMATION],
                SDesc.Owner,
                nil,
                nil,
                nil);

    TSecureGeneralObject.SetNamedSecurityInfo(pTempName,
                SE_FILE_OBJECT,
                [sif_DACL_SECURITY_INFORMATION],
                SDesc.Owner,
                nil,
                SDesc.DACL,
                nil);



  finally
    SDesc.Free_SA(pSecAttr);
    if IsPrivilegeSet(SE_SECURITY_NAME) then
    begin
      EnablePrivilege(SE_SECURITY_NAME, pst_Disable);
    end;
  end;



  result := pTempName;
end;

Wie man hier sehen kann, wird eine neuer SecurityDescriptor gesetzt.
Delphi-Quellcode:
 handle := jwaWindows.CreateFile(pTempName,
               FILE_ALL_ACCESS,
               0,
               Pointer(pSecAttr),
               CREATE_ALWAYS,
               FILE_ATTRIBUTE_NORMAL,
               0);
Nur wird der SD ignoriert.

Was jedoch funktioniert ist folgendes:
Delphi-Quellcode:
TSecureGeneralObject.SetNamedSecurityInfo(pTempName,
                SE_FILE_OBJECT,
                [sif_OWNER_SECURITY_INFORMATION],
                SDesc.Owner,
                nil,
                nil,
                nil);

    TSecureGeneralObject.SetNamedSecurityInfo(pTempName,
                SE_FILE_OBJECT,
                [sif_DACL_SECURITY_INFORMATION],
                SDesc.Owner,
                nil,
                SDesc.DACL,
                nil);
Man muss den SD zweimal setzen. Das erste Mal löscht man ihn,
und das zweite mal setzt man ihn.

Dies hab ich jedoch nur aus Tests erfahren. Dokumentiert hab ich es nicht gefunden.
Christian
Windows, Tokens, Access Control List, Dateisicherheit, Desktop, Vista Elevation?
Goto: JEDI API LIB & Windows Security Code Library (JWSCL)
  Mit Zitat antworten Zitat
Dezipaitor

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

Re: CreateFile und Security Attributes

  Alt 28. Jun 2007, 20:29
hi

kennt jemand noch Foren oder ähnliches, wo man auf solche Threads auch noch Posts bekommt?
Muss auch nicht Delphi sein

THX
Christian
Windows, Tokens, Access Control List, Dateisicherheit, Desktop, Vista Elevation?
Goto: JEDI API LIB & Windows Security Code Library (JWSCL)
  Mit Zitat antworten Zitat
Christian Seehase
(Co-Admin)

Registriert seit: 29. Mai 2002
Ort: Hamburg
11.105 Beiträge
 
Delphi 11 Alexandria
 
#3

Re: CreateFile und Security Attributes

  Alt 28. Jun 2007, 20:41
Moin Dezipaitor,

Zitat von Dezipaitor:
kennt jemand noch Foren oder ähnliches, wo man auf solche Threads auch noch Posts bekommt?
Kann es sein, dass Du durch das meist in der DP vorhandene Antwortverhalten etwas verwöhnt bist...


Zitat von PSDK - CreateFile - CREATE_ALWAYS:
If a file exists, the function overwrites the file, clears the existing attributes, combines the specified file attributes, and flags with FILE_ATTRIBUTE_ARCHIVE, but does not set the security descriptor that the SECURITY_ATTRIBUTES structure specifies.
Könnte es daran liegen.
Tschüss Chris
Die drei Feinde des Programmierers: Sonne, Frischluft und dieses unerträgliche Gebrüll der Vögel.
Der Klügere gibt solange nach bis er der Dumme ist
  Mit Zitat antworten Zitat
Dezipaitor

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

Re: CreateFile und Security Attributes

  Alt 28. Jun 2007, 21:02
Zitat von Christian Seehase:
Moin Dezipaitor,

Zitat von Dezipaitor:
kennt jemand noch Foren oder ähnliches, wo man auf solche Threads auch noch Posts bekommt?
Kann es sein, dass Du durch das meist in der DP vorhandene Antwortverhalten etwas verwöhnt bist...
Ich finde diese Delphigemeinschaft echt super. Nur leider bewege ich mich wohl
für die allermeisten Leute hier auf einer zu hohen Stufe?!. Ich hoffe jedoch immer wieder, dass jemand doch schon
dieselbe Erfahrung gemacht hat und etwas antworten kann. Desweiteren hoffe ich, dass durch die Niederschrift
jemand irgendwann einmal durch eine Suchmaschine, diesen Post findet und mir eine Mail schreibt, weil er gerade
dasselbe Problem hat (das passierte schon öfters).

Verwöhnt kann ich deshalb schon garnicht sein, weil viele Threads von und für Anfänger geschrieben sind (ich denke da z.B. an den Thread, wie man eine Zahl in Delphi negiert). Das bringt mich jedoch immer wieder zum Lachen - und das hält ja bekanntlich gesund
Aber manchmal wünsche ich mir schon die guten alten Zeiten zurück. Es scheint, als ob Delphi wirklich ausstirbt? Oder wissen die Profis schon alles? Ups, nein, kann garnet sein. Ich bin ein Profi (=verdiene Geld damit) und weiß net alles über Delphi (ich denke da an den Kommabug ab Delphi 6. Erst vor kurzem entdeckt -zumindest von mir.).



Zitat von Christian Seehase:
Zitat von PSDK - CreateFile - CREATE_ALWAYS:
If a file exists, the function overwrites the file, clears the existing attributes, combines the specified file attributes, and flags with FILE_ATTRIBUTE_ARCHIVE, but does not set the security descriptor that the SECURITY_ATTRIBUTES structure specifies.
Könnte es daran liegen.
Hmm ich habe es wohl leider verpasst, konsequent darauf hinzuweißen, dass ich eine neue Datei erstelle. Mit dem Aufruf von GetTempFileName erzeuge ich einen einzigartigen Dateinamen für den Ordner.
Trotzdem Danke für deinen Aufwand
Christian
Windows, Tokens, Access Control List, Dateisicherheit, Desktop, Vista Elevation?
Goto: JEDI API LIB & Windows Security Code Library (JWSCL)
  Mit Zitat antworten Zitat
Alter Mann

Registriert seit: 15. Nov 2003
Ort: Berlin
934 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#5

Re: CreateFile und Security Attributes

  Alt 10. Dez 2007, 18:43
Hallo Dezipaitor,

reicht das als Tipp?

Zitat:
Wenn der DACL Zugriffssteuerungsliste (DACL) einer Datei ein access-allowed ACE hinzugefügt wird, wird der zugelassene Zugriff auf die Datei von dem System dem entsprechenden Benutzer oder dem Gruppenkonto, das dem ACE zugeordnet wird, bereitgestellt. Das DACL der Datei ist in den meisten Fällen genug nicht groß, ein zusätzliches ACE hinzuzufügen. Deshalb sind das Erstellen einer neuen Zugriffssteuerungsliste (ACL) und das Kopieren der ACEs aus dem vorhandenen DACL der Datei in der bevorzugten Reihenfolge notwendig. Das neue DACL kann dann das alte DACL in der Sicherheitsbeschreibung (SD) der Datei ersetzen.
Hier der komplette Text.

Gruß
  Mit Zitat antworten Zitat
Dezipaitor

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

Re: CreateFile und Security Attributes

  Alt 10. Dez 2007, 19:25
Uff, das hab ich ganz vergessen.

Wenn man aber das Flag "Protected" dem Security Descriptor übergibt, dann wird die Vererbund an dieser neuen Datei gestoppt und die übergeben ACL verwendet.
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 08:20 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