![]() |
Gelöst: Vierten Zeitstempel ermitteln
Die Zeitstempel für Erstellung, letzte Änderung und letzten Zugriff kennt wohl jeder. Wie man diese ermittelt, ist mir auch klar. Unter NTFS gibt es noch einen vierten Zeitstempel - "Entry modified" bzw "MFT last modified" -, welcher die letzte Änderung des MFT Eintrags angibt.
Weiß jemand, wie man diesen ermitteln kann? ![]() ![]() [edit=Matze]Auf Wunsch des Autors ein "Gelöst" in den Titel eingefügt. MfG, Matze[/edit] |
Re: Vierten Zeitstempel ermitteln
Hab gerade
![]() |
Re: Vierten Zeitstempel ermitteln
Code:
typedef struct _FILE_BASIC_INFORMATION {
LARGE_INTEGER CreationTime; LARGE_INTEGER LastAccessTime; LARGE_INTEGER LastWriteTime; LARGE_INTEGER ChangeTime; ULONG FileAttributes; } FILE_BASIC_INFORMATION, *PFILE_BASIC_INFORMATION;
Code:
typedef struct _FILE_BOTH_DIR_INFORMATION {
ULONG NextEntryOffset; ULONG FileIndex; LARGE_INTEGER CreationTime; LARGE_INTEGER LastAccessTime; LARGE_INTEGER LastWriteTime; LARGE_INTEGER ChangeTime; LARGE_INTEGER EndOfFile; LARGE_INTEGER AllocationSize; ULONG FileAttributes; ULONG FileNameLength; ULONG EaSize; BYTE ShortNameLength; WCHAR ShortName[12]; WCHAR FileName[1]; } FILE_BOTH_DIR_INFORMATION, *PFILE_BOTH_DIR_INFORMATION;
Code:
NtQueryInformationFile(
IN HANDLE FileHandle, OUT PIO_STATUS_BLOCK IoStatusBlock, OUT PVOID FileInformation, IN ULONG Length, IN FILE_INFORMATION_CLASS FileInformationClass ); ![]() [edit=SirThornberry]quote-tags zu c-tags geändert - Bitte künftig die entsprechenden Tags verwenden - Mfg, SirThornberry[/edit] |
Re: Vierten Zeitstempel ermitteln
Liste der Anhänge anzeigen (Anzahl: 1)
Nun bin ich soweit:
Delphi-Quellcode:
Es gibt keine Fehlermeldungen der Status ist dennoch FALSE. Ich suche seit Stunden und finde im Prinzip immer nur die gleichen Typdeklarationen aber keine Beispiele, die mich weiterbringen. Vielleicht hat hier jemand eine Idee, was falsch sein könnte oder was fehlt.
function NtQueryAttributesFile(ObjectAttributes: POBJECT_ATTRIBUTES;
FileInformation: PFILE_BASIC_INFORMATION): NTSTATUS; stdcall; external ntdll name 'NtQueryAttributesFile'; function NT_SUCCESS(Status: NTSTATUS): WordBool; begin Result := Status >= 0; end; procedure InitObjectAttributes(var p: _OBJECT_ATTRIBUTES; n: PUNICODE_STRING; a: ULONG; r: THandle; s: POINTER); begin p.Length := SizeOf(OBJECT_ATTRIBUTES); p.RootDirectory := r; p.Attributes := a; p.ObjectName := n; p.SecurityDescriptor := s; p.SecurityQualityOfService := nil; end; function GetNTTime(fn: String; var FILE_BASIC_INFORMATION): Boolean; var Status : NTSTATUS; ObjName : UNICODE_STRING; ObjectAttributes : OBJECT_ATTRIBUTES; FileInformation : _FILE_BASIC_INFORMATION; Buffer : array of WideChar; begin Result := False; try SetLength(Buffer, Length(fn)); FillChar(ObjectAttributes, SizeOf(OBJECT_ATTRIBUTES), 0); FillChar(FileInformation, SizeOf(_FILE_BASIC_INFORMATION), 0); MultiByteToWideChar(CP_UTF8, 0, @fn[1], Length(fn), PWideChar(Buffer), Length(Buffer)); ObjName.Buffer := @Buffer[0]; ObjName.Length := Length(fn) * SizeOf(WideChar); InitObjectAttributes(ObjectAttributes, @ObjName, OBJ_CASE_INSENSITIVE, 0, nil); Status := NtQueryAttributesFile(@ObjectAttributes, @FileInformation); Result := NT_SUCCESS(Status); finally // end; end; |
Re: Vierten Zeitstempel ermitteln
Delphi-Quellcode:
Ich denke, der Fehler liegt bei ObjName. Es funktioniert weder mit dem Dateinamen noch dem Devicenamen. Also zum Beispiel:
function GetNTTime(fn: String; var FileInformation: FILE_BASIC_INFORMATION): Boolean;
var dosfn : String; szNtDeviceName : array[0..MAX_PATH] of Char; Status : NTSTATUS; ObjName : UNICODE_STRING; ObjectAttributes : OBJECT_ATTRIBUTES; Buffer : array of WideChar; begin Result := False; QueryDosDevice (PAnsiChar(ExtractFileDrive(fn)), szNtDeviceName, MAX_PATH); dosfn := Format('%s\%s', [szNtDeviceName, copy(fn, 4, Length(fn) - 3)]); try SetLength(Buffer, Length(dosfn)); FillChar(ObjectAttributes, SizeOf(OBJECT_ATTRIBUTES), 0); FillChar(FileInformation, SizeOf(FILE_BASIC_INFORMATION), 0); MultiByteToWideChar(CP_UTF8, 0, @dosfn[1], Length(dosfn), PWideChar(Buffer), Length(Buffer)); ObjName.Buffer := @Buffer[0]; ObjName.Length := Length(dosfn) * SizeOf(WideChar); InitializeObjectAttributes(@ObjectAttributes, @ObjName, OBJ_CASE_INSENSITIVE, 0, nil); Status := NtQueryAttributesFile(@ObjectAttributes, @FileInformation); Result := NT_SUCCESS(Status); finally // end; end; D:\Garfield\Beispiel.txt \Device\HarddiskVolume2\Garfield\Beispiel.txt Wie müsste ein Objectname aussehen? |
Re: Vierten Zeitstempel ermitteln
Du setzt beim TNtUnicodeString (UNICODE_STRING) nur die Länge der Daten, aber nicht die Maximale Länge.
|
Re: Vierten Zeitstempel ermitteln
Wenn ich die maximale Länge nicht setze, steht dort der Wert 17394.
Nachdem ich jetzt meine Function um
Delphi-Quellcode:
ergänzt habe, bekomme ich als Fehler:
var
... doserr : DWORD; Error : PChar; begin ... doserr := RtlNtStatusToDosError(Status); SetLastError (doserr); GetMem (Error, 255); FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_FROM_SYSTEM, nil, GetLastError, 0, @Error, 255, nil); ShowMessage(Trim(Error)); finally ... Zitat:
|
Re: Vierten Zeitstempel ermitteln
Zitat:
|
Re: Vierten Zeitstempel ermitteln
Deine Definition von TFileBasicInformation ist falsch (es fehlt die implizite Ausrichtung, die Struktur ist nicht packed!)
Delphi-Quellcode:
{$ALIGN 8}
{$MINENUMSIZE 4} const NtApiLib = 'ntdll.dll'; type TNtStatus = type LongInt; UIntPtr = Cardinal; type PIoStatusBlock = ^TIoStatusBlock; TIoStatusBlock = record u: record case Integer of 0: ( Status: TNtStatus); 1: ( Pointer: Pointer); end; Information: UIntPtr; end; type PFileInformationClass = ^TFileInformationClass; TFileInformationClass = ( FileInvalidInformation, FileDirectoryInformation, // 1 FileFullDirectoryInformation, // 2 FileBothDirectoryInformation, // 3 FileBasicInformation, // 4 FileStandardInformation, // 5 FileInternalInformation, // 6 FileEaInformation, // 7 FileAccessInformation, // 8 FileNameInformation, // 9 FileRenameInformation, // 10 FileLinkInformation, // 11 FileNamesInformation, // 12 FileDispositionInformation, // 13 FilePositionInformation, // 14 FileFullEaInformation, // 15 FileModeInformation, // 16 FileAlignmentInformation, // 17 FileAllInformation, // 18 FileAllocationInformation, // 19 FileEndOfFileInformation, // 20 FileAlternateNameInformation, // 21 FileStreamInformation, // 22 FilePipeInformation, // 23 FilePipeLocalInformation, // 24 FilePipeRemoteInformation, // 25 FileMailslotQueryInformation, // 26 FileMailslotSetInformation, // 27 FileCompressionInformation, // 28 FileObjectIdInformation, // 29 FileCompletionInformation, // 30 FileMoveClusterInformation, // 31 FileQuotaInformation, // 32 FileReparsePointInformation, // 33 FileNetworkOpenInformation, // 34 FileAttributeTagInformation, // 35 FileTrackingInformation, // 36 FileIdBothDirectoryInformation, // 37 FileIdFullDirectoryInformation, // 38 FileValidDataLengthInformation, // 39 FileShortNameInformation, // 40 FileIoCompletionNotificationInformation, // 41 FileIoStatusBlockRangeInformation, // 42 FileIoPriorityHintInformation, // 43 FileSfioReserveInformation, // 44 FileSfioVolumeInformation, // 45 FileHardLinkInformation, // 46 FileProcessIdsUsingFileInformation, // 47 FileNormalizedNameInformation, // 48 FileNetworkPhysicalNameInformation, // 49 FileIdGlobalTxDirectoryInformation, // 50 FileMaximumInformation ); function NtQueryInformationFile(AFileHandle: THandle; out AIoStatusBlock: TIoStatusBlock; out AFileInformation; ALength: ULONG; AFileInformationClass: TFileInformationClass): TNtStatus; stdcall; external NtApiLib; type PFileBasicInformation = ^TFileBasicInformation; TFileBasicInformation = record CreationTime : TLargeInteger; // 00 LastAccessTime: TLargeInteger; // 08 LastWriteTime : TLargeInteger; // 10 ChangeTime : TLargeInteger; // 18 FileAttributes: ULONG; // 20 //Reserved : ULONG; // 24 end; //(28) procedure Foobar(); const FILE_READ_ATTRIBUTES = $0080; var FileNameLength: DWORD; FileName: array [0..MAX_PATH - 1] of WideChar; FileHandle: THandle; IoStatusBlock: TIoStatusBlock; FileInformation: TFileBasicInformation; Status: TNtStatus; begin FileNameLength := GetModuleFileNameW(0, FileName, Length(FileName)); if (FileNameLength > 0) and (FileNameLength < DWORD(Length(FileName))) then begin FileHandle := CreateFileW(FileName, FILE_READ_ATTRIBUTES, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0); if FileHandle <> INVALID_HANDLE_VALUE then try FillChar(IoStatusBlock, SizeOf(IoStatusBlock), 0); FillChar(FileInformation, SizeOf(FileInformation), 0); Status := NtQueryInformationFile(FileHandle, IoStatusBlock, FileInformation, SizeOf(FileInformation), FileBasicInformation); if (Status >= 0) and (IoStatusBlock.u.Status >= 0) and (IoStatusBlock.Information = SizeOf(FileInformation)) then with FileInformation do ShowMessage( 'CreationTime: $' + IntToHex(CreationTime, 16) + #13#10 + 'LastAccessTime: $' + IntToHex(LastAccessTime, 16) + #13#10 + 'LastWriteTime: $' + IntToHex(LastWriteTime, 16) + #13#10 + 'ChangeTime: $' + IntToHex(ChangeTime, 16) + #13#10 + 'FileAttributes: $' + IntToHex(FileAttributes, 8)) else ShowMessage( 'Status: $' + IntToHex(Status, 8) + #13#10 + 'IoStatus: $' + IntToHex(IoStatusBlock.u.Status, 8) + #13#10 + 'IoInformation: $' + IntToHex(IoStatusBlock.Information, 8)); finally CloseHandle(FileHandle); end; end; end; |
Re: Vierten Zeitstempel ermitteln
Liste der Anhänge anzeigen (Anzahl: 1)
Zitat:
Delphi-Quellcode:
Damit sehen die Buffergrößen schon besser aus. Der Fehler ist damit leider nicht behoben.
function GetNTTime(fn: String; var FileInformation: FILE_BASIC_INFORMATION): Boolean;
var dosfn : ANSI_STRING; szNtDeviceName : array[0..MAX_PATH] of Char; ObjName : UNICODE_STRING; ObjectAttributes : OBJECT_ATTRIBUTES; Buffer : array of WideChar; Status : NTSTATUS; DosErr : DWORD; Error : PChar; begin Result := False; FillChar(dosfn, SizeOf(ANSI_STRING), 0); FillChar(ObjName, SizeOf(UNICODE_STRING), 0); FillChar(ObjectAttributes, SizeOf(OBJECT_ATTRIBUTES), 0); FillChar(FileInformation, SizeOf(FILE_BASIC_INFORMATION), 0); QueryDosDevice(PAnsiChar(ExtractFileDrive(fn)), szNtDeviceName, MAX_PATH); RtlInitAnsiString(@dosfn, PAnsiChar(Format('%s\%s', [szNtDeviceName, copy(fn, 4, Length(fn) - 3)]))); RtlAnsiStringToUnicodeString(@ObjName, @dosfn, True); InitializeObjectAttributes(@ObjectAttributes, @ObjName, OBJ_CASE_INSENSITIVE, 0, nil); Status := NtQueryAttributesFile(@ObjectAttributes, @FileInformation); Result := NT_SUCCESS(Status); if not Result then begin doserr := RtlNtStatusToDosError(Status); SetLastError(DosErr); GetMem(Error, 255); FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_FROM_SYSTEM, nil, GetLastError, 0, @Error, 255, nil); ShowMessage(Trim(Error)); end; end; Zitat:
![]() ![]() Dein Code funktioniert, aber meinem ist die Deklaration egal. :cry: |
Re: Vierten Zeitstempel ermitteln
Zitat:
In meinem Code steht {$ALIGN 8} und {$MINENUMSIZE 4}, um von den Compiler-Einstellungen unabhängig zu sein. Und ich verstehe nicht ganz, warum du unbedingt NtQueryAttributesFile verwenden möchtest (und damit den Win32-Pfad in einen Objektnamen auflösen musst - was fehleranfällig ist, da nicht jeder Dateiname mit einem Laufwerksbuchstaben beginnt (UNC-Präfix, \\?\-Präfix, Volume-Präfix, usw...)). |
Re: Vierten Zeitstempel ermitteln
Wenn man sich ein Handle auf die Datei holt, wird die LastAccessTime geändert, was ich vermeiden möchte.
|
Re: Vierten Zeitstempel ermitteln
Liste der Anhänge anzeigen (Anzahl: 1)
Object_Attributes ist ja so deklariert:
Code:
Da ist ein Handle auf das RootDirectory und der PUNICODE_STRING für den Objektname. Der Dateiname samt Pfad als Object funktioniert nicht. Also müsste man es über das Handle versuchen. Wenn ich den Objektnamen leer lasse und das Handle auf die Datei verwende, funktioniert es:
typedef struct _OBJECT_ATTRIBUTES {
ULONG Length; HANDLE RootDirectory; PUNICODE_STRING ObjectName; ULONG Attributes; PVOID SecurityDescriptor; PVOID SecurityQualityOfService; } OBJECT_ATTRIBUTES, *POBJECT_ATTRIBUTES; typedef CONST OBJECT_ATTRIBUTES *PCOBJECT_ATTRIBUTES;
Delphi-Quellcode:
Jetzt wollte ich den Pfad aufteilen, damit ich kein Handle auf die Datei nehmen muss.
function GetNTTime(fn: String; var FileInformation: FILE_BASIC_INFORMATION): Boolean;
const FILE_READ_ATTRIBUTES = $0080; var strDosDeviceName : ANSI_STRING; RootDirectory : THandle; FileName : ANSI_STRING; szNtDeviceName : array[0..MAX_PATH] of Char; ObjName : UNICODE_STRING; ObjectAttributes : OBJECT_ATTRIBUTES; Buffer : array of WideChar; Status : NTSTATUS; DosErr : DWORD; Error : PChar; begin Result := False; RtlZeroMemory(@strDosDeviceName, SizeOf(ANSI_STRING)); RtlZeroMemory(@ObjName, SizeOf(UNICODE_STRING)); RtlZeroMemory(@ObjectAttributes, SizeOf(OBJECT_ATTRIBUTES)); RtlZeroMemory(@FileInformation, SizeOf(FILE_BASIC_INFORMATION)); RootDirectory := CreateFile(PAnsiChar(fn), FILE_READ_ATTRIBUTES, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0); InitializeObjectAttributes(@ObjectAttributes, @ObjName, OBJ_CASE_INSENSITIVE, RootDirectory, nil); Status := NtQueryAttributesFile(@ObjectAttributes, @FileInformation); Result := NT_SUCCESS(Status); if not Result then begin doserr := RtlNtStatusToDosError(Status); SetLastError(DosErr); GetMem(Error, 255); FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_FROM_SYSTEM, nil, GetLastError, 0, @Error, 255, nil); ShowMessage(Trim(Error)); end; end;
Delphi-Quellcode:
Ergibt ein ungültiges Handle. Deshalb
RootDirectory := CreateFile(PAnsiChar(ExtractFileDrive(fn)), FILE_READ_ATTRIBUTES, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0);
Delphi-Quellcode:
Allerdings scheint es egal zu sein, wie ich den Dateinamen aufteile:
QueryDosDevice(PAnsiChar(ExtractFileDrive(fn)), szNtDeviceName, MAX_PATH);
RootDirectory := CreateFile(szNtDeviceName, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0);
Delphi-Quellcode:
QueryDosDevice(PAnsiChar(ExtractFileDrive(fn)), szNtDeviceName, MAX_PATH);
RootDirectory := CreateFile(PAnsiChar(szNtDeviceName + Copy(ExtractFilePath(fn), 3, Length(ExtractFilePath(fn)) - 2)), FILE_READ_ATTRIBUTES, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0); RtlInitAnsiString(@FileName, PAnsiChar(ExtractFileName(fn))); RtlAnsiStringToUnicodeString(@ObjName, @FileName, True);
Delphi-Quellcode:
Es gibt immer diesen Fehler:
QueryDosDevice(PAnsiChar(ExtractFileDrive(fn)), szNtDeviceName, MAX_PATH);
RootDirectory := CreateFile(szNtDeviceName, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0); RtlInitAnsiString(@FileName, PAnsiChar(Copy(fn, 4, Length(fn) - 3))); RtlAnsiStringToUnicodeString(@ObjName, @FileName, True);
Code:
Nehme ich das Handle auf die Datei und die Datei als Object:
---------------------------
Filetime --------------------------- Unzulässiger Zugriff auf einen Speicherbereich. --------------------------- OK ---------------------------
Delphi-Quellcode:
bekomme ich den Fehler:
RootDirectory := CreateFile(PAnsiChar(fn), FILE_READ_ATTRIBUTES, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0);
RtlInitAnsiString(@FileName, PAnsiChar(ExtractFileName(fn))); RtlAnsiStringToUnicodeString(@ObjName, @FileName, True);
Code:
:wall:
---------------------------
Filetime --------------------------- Das System kann den angegebenen Pfad nicht finden. --------------------------- OK --------------------------- |
Re: Vierten Zeitstempel ermitteln
Liste der Anhänge anzeigen (Anzahl: 1)
Gelöst!
Der Fehler
Code:
lag an
---------------------------
Filetime --------------------------- Unzulässiger Zugriff auf einen Speicherbereich. --------------------------- OK ---------------------------
Delphi-Quellcode:
Das ungültige Handle lag an falschen Parametern. Jetzt sieht es so aus:
QueryDosDevice(PAnsiChar(ExtractFileDrive(fn)), szNtDeviceName, MAX_PATH);
Delphi-Quellcode:
Das scheint auch ein Irrtum zu sein:
function GetNTTime(fn: String; var FileInformation: FILE_BASIC_INFORMATION): Boolean;
const FILE_LIST_DIRECTORY = $0001; var RootDirectory : THandle; FileName : ANSI_STRING; ObjName : UNICODE_STRING; ObjectAttributes : OBJECT_ATTRIBUTES; Status : NTSTATUS; DosErr : DWORD; Error : PChar; begin Result := False; RtlZeroMemory(@ObjName, SizeOf(UNICODE_STRING)); RtlZeroMemory(@ObjectAttributes, SizeOf(OBJECT_ATTRIBUTES)); RtlZeroMemory(@FileInformation, SizeOf(FILE_BASIC_INFORMATION)); RootDirectory := CreateFile(PAnsiChar(ExtractFilePath(fn)), FILE_LIST_DIRECTORY, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0); RtlInitAnsiString(@FileName, PAnsiChar(ExtractFileName(fn))); RtlAnsiStringToUnicodeString(@ObjName, @FileName, True); InitializeObjectAttributes(@ObjectAttributes, @ObjName, OBJ_CASE_INSENSITIVE, RootDirectory, nil); Status := NtQueryAttributesFile(@ObjectAttributes, @FileInformation); Result := NT_SUCCESS(Status); if not Result then begin doserr := RtlNtStatusToDosError(Status); SetLastError(DosErr); GetMem(Error, 255); FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_FROM_SYSTEM, nil, GetLastError, 0, @Error, 255, nil); ShowMessage(Trim(Error)); end; CloseHandle(RootDirectory); end; Zitat:
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 07:16 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz