Einzelnen Beitrag anzeigen

Robert_G
(Gast)

n/a Beiträge
 
#7

Re: File Owner mittels GetNamedSecurityInfo - Hilfe!

  Alt 23. Mai 2005, 12:02
Kennst du den Reflector? Sau geiles Teil.
Du nimmst deinen C#/Delphi.Net/Chrome/Eifel/whatsoever - Samplecode und kompostierst ihn auch in dem Compiler.
Die entstandene Assembly jagst du durch den Reflector und wählst deine Sprache ala Darstellung aus.
*Fump* Schon findest du den Cod ein deiner Sprache...

Der Beispielcode bei P/Invoke ist übrigens falsch, da ihre Version von xxx mit einem anderen Typen für den pSid-Parameter arbeitet. Dieses hier geht.

Ich habe es einfach in eine neue C#-ClassLib kopiert, ein wenig rum-ReSharpert (damit man es auch lesen kann ) und kompiliert.
Code:
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace FileOwnerDings
{
    public class SecurityDescriptor
    {
        [DllImport("advapi32.dll", SetLastError=true)]
        static extern int GetNamedSecurityInfo(string pObjectName,
                                               SeObjectType ObjectType,
                                               SecurityInformation SecurityInfo,
                                               out IntPtr ppsidOwner,
                                               out IntPtr ppsidGroup,
                                               out IntPtr ppDacl,
                                               out IntPtr ppSacl,
                                               out IntPtr ppSecurityDescriptor);

        [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
        static extern int LookupAccountSid(string systemName,
                                           IntPtr psid,
                                           StringBuilder accountName,
                                           ref int cbAccount,
                                           [Out] StringBuilder domainName,
                                           ref int cbDomainName,
                                           out int use);

        public static string GetFileObjectOwner(string objectName)
        {
            IntPtr pZero = IntPtr.Zero;
            IntPtr pSid = pZero;
            IntPtr psd = pZero; // Not used here
            int errorReturn = GetNamedSecurityInfo(objectName,
                                                   SeObjectType.FileObject,
                                                   SecurityInformation.Owner,
                                                   out pSid,
                                                   out pZero,
                                                   out pZero,
                                                   out pZero,
                                                   out psd);
            if (errorReturn != 0)
            {
                throw new Exception(string.Format("GetNamedSecurityInfo has exited with code {0}. (last error is {0})",
                                                  errorReturn,
                                                  Marshal.GetLastWin32Error()));
            }
            int bufferSize = 64;
            StringBuilder buffer = new StringBuilder();
            int accounLength = bufferSize;
            int domainLength = bufferSize;
            int sidNameUse = 0;
            StringBuilder account = new StringBuilder(bufferSize);
            StringBuilder domain = new StringBuilder(bufferSize);
            errorReturn = LookupAccountSid(null,
                                           pSid,
                                           account,
                                           ref accounLength,
                                           domain,
                                           ref domainLength,
                                           out sidNameUse);
            if (errorReturn == 0)
            {
                throw new Exception(string.Format("LookupAccountSid has exited with code 0. (last error is {0})",
                                                  Marshal.GetLastWin32Error()));
            }
            buffer.Append(domain);
            buffer.Append(@"\");
            buffer.Append(account);
            return buffer.ToString();
        }
    }
}
Die Enums:
Code:
    [Flags]
    public enum SecurityInformation
    {
        Owner = 1,
        Group = 2,
        DACL = 4,
        SACL = 8
    }
Code:
   public enum SeObjectType
    {
        UnknownObjectType = 0,
        FileObject,
        Service,
        Printer,
        RegistryKey,
        LMShare,
        KernelObject,
        WindowObject,
        DsObject,
        DsObjectAll,
        ProviderDefinedObject,
        WMIGUIDObject,
        RegistryWOW64Key
    }
Mangels eines Delphi.Net Komposters nahm ich das Chrome PlugIn für den Reflector und ieß mir Chrome Code erzeugen. (Ist ja sehr ähnlich zu Delphi.Net)
Nachdem ich die lokalen Variablen umbenannt habe (DIe werden von der IL nicht wirklich benannt ) und auch hier einen Formatter drüberrennen ließ, sah es so aus:

Delphi-Quellcode:
namespace FileOwnerDings;

interface
uses
   System,
   System.Runtime.InteropServices,
   System.Text;

type
  SecurityDescriptor = public class
  private
    [DllImport('advapi32.dll', SetLastError := True)]
    class method GetNamedSecurityInfo(pObjectName: string;
                                      ObjectType: SeObjectType;
                                      SecurityInfo: SecurityInformation;
                                      out ppsidOwner,
                                          ppsidGroup,
                                          ppDacl,
                                          ppSacl,
                                          ppSecurityDescriptor: IntPtr): Integer; external;
    [DllImport('advapi32.dll', CharSet := CharSet.Auto, SetLastError := True)]
    class method LookupAccountSid(systemName: string;
                                   psid: IntPtr;
                                   accountName: StringBuilder;
                                   var cbAccount: Integer;
                                   [&Out] domainName: StringBuilder;
                                   var cbDomainName: Integer;
                                   out use: Integer): Integer; external;
  public
    class method GetFileObjectOwner(objectName: string): string;
  end;


implementation

class method SecurityDescriptor.GetFileObjectOwner(objectName: string): string;
var
  pZero, pSid, psd : IntPtr;
  errorReturn : Integer;
  bufferSize : Integer;
  accountLength : Integer;
  domainLength : Integer;
  sidNameUse : Integer;
  buffer : StringBuilder;
  account : StringBuilder;
  domain : StringBuilder;
begin
  pZero := IntPtr.Zero;
  pSid := IntPtr.Zero;
  psd := pZero;
  errorReturn := SecurityDescriptor.GetNamedSecurityInfo(objectName,
                                                         SeObjectType.FileObject,
                                                         SecurityInformation.Owner,
                                                         pSid,
                                                         pZero,
                                                         pZero,
                                                         pZero,
                                                         psd);
  if (errorReturn <> 0) then
    raise new Exception(string.Format('GetNamedSecurityInfo excited with code {0}. (last error is {0})',
                                      errorReturn,
                                      Marshal.GetLastWin32Error));

  bufferSize := 64;
  accountLength := bufferSize;
  domainLength := bufferSize;
  sidNameUse := 0;
  
  buffer := new StringBuilder;
  account := new StringBuilder(bufferSize);
  domain := new StringBuilder(bufferSize);
  
  errorReturn := SecurityDescriptor.LookupAccountSid(nil,
                                                     pSid,
                                                     account,
                                                     accountLength,
                                                     domain,
                                                     domainLength,
                                                     sidNameUse);
  if (errorReturn = 0) then
    raise new Exception(string.Format('LookupAccountSid excited with code 0. (last error is {0})',
                                      Marshal.GetLastWin32Error));

  buffer.Append(domain);
  buffer.Append('\');
  buffer.Append(account);
  Result := buffer.ToString;
end;

end.
Ab hier sollte eine Konvertierung zu D.Net easy sein.

Der zeitaufwendigste Part an allem war das Schreiben dieses Beitrages. Also -> Schau dir den Reflector mal an.
Es macht auch Spass die Microsoft Assemblies damit auseinanderzunehmen.
  Mit Zitat antworten Zitat