Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi Gelöst: Vierten Zeitstempel ermitteln (https://www.delphipraxis.net/114451-geloest-vierten-zeitstempel-ermitteln.html)

Garfield 25. Mai 2008 19:26


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?

http://www.forensicswiki.org/index.php?title=NTFS
http://www.forensicswiki.org/wiki/Timestomp


[edit=Matze]Auf Wunsch des Autors ein "Gelöst" in den Titel eingefügt. MfG, Matze[/edit]

Garfield 26. Mai 2008 07:32

Re: Vierten Zeitstempel ermitteln
 
Hab gerade das gefunden, aber noch nicht weiter angesehen.

Garfield 26. Mai 2008 18:46

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 );
http://undocumented.ntinternals.net/

[edit=SirThornberry]quote-tags zu c-tags geändert - Bitte künftig die entsprechenden Tags verwenden - Mfg, SirThornberry[/edit]

Garfield 28. Mai 2008 21:02

Re: Vierten Zeitstempel ermitteln
 
Liste der Anhänge anzeigen (Anzahl: 1)
Nun bin ich soweit:
Delphi-Quellcode:
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;
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.

Garfield 30. Mai 2008 14:15

Re: Vierten Zeitstempel ermitteln
 
Delphi-Quellcode:
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;
Ich denke, der Fehler liegt bei ObjName. Es funktioniert weder mit dem Dateinamen noch dem Devicenamen. Also zum Beispiel:

D:\Garfield\Beispiel.txt
\Device\HarddiskVolume2\Garfield\Beispiel.txt

Wie müsste ein Objectname aussehen?

nicodex 30. Mai 2008 14:22

Re: Vierten Zeitstempel ermitteln
 
Du setzt beim TNtUnicodeString (UNICODE_STRING) nur die Länge der Daten, aber nicht die Maximale Länge.

Garfield 30. Mai 2008 14:45

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:
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
  ...
ergänzt habe, bekomme ich als Fehler:
Zitat:

---------------------------
Filetime
---------------------------
Unzulässiger Zugriff auf einen Speicherbereich.
---------------------------
OK
---------------------------
:gruebel:

nicodex 30. Mai 2008 14:52

Re: Vierten Zeitstempel ermitteln
 
Zitat:

Zitat von Garfield
Wenn ich die maximale Länge nicht setze, steht dort der Wert 17394.

Öhm, ja, schön, aber dein Buffer ist nicht so groß.

nicodex 30. Mai 2008 15:39

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;

Garfield 30. Mai 2008 16:44

Re: Vierten Zeitstempel ermitteln
 
Liste der Anhänge anzeigen (Anzahl: 1)
Zitat:

Zitat von nicodex
Öhm, ja, schön, aber dein Buffer ist nicht so groß.

Stimmt.

Delphi-Quellcode:
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;
Damit sehen die Buffergrößen schon besser aus. Der Fehler ist damit leider nicht behoben.

Zitat:

Zitat von nicodex
Deine Definition von TFileBasicInformation ist falsch (es fehlt die implizite Ausrichtung, die Struktur ist nicht packed!

Die Deklarationen habe ich von dort:
http://www.koders.com/delphi/fid50A5...6A08E3B70.aspx
http://www.koders.com/delphi/fidFFCC...D78A4039C.aspx

Dein Code funktioniert, aber meinem ist die Deklaration egal. :cry:


Alle Zeitangaben in WEZ +1. Es ist jetzt 13:39 Uhr.
Seite 1 von 2  1 2      

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