|
| 1 | +#pragma once |
| 2 | +#ifndef HEADERS_H |
| 3 | +#define HEADERS_H |
| 4 | + |
| 5 | +#include <Windows.h> |
| 6 | +#include <shlwapi.h> |
| 7 | +#include <strsafe.h> |
| 8 | +#include <stdio.h> |
| 9 | + |
| 10 | +#pragma comment(lib, "Shlwapi.lib") |
| 11 | +#pragma comment(lib, "Advapi32.lib") |
| 12 | +#pragma comment(lib, "ntdll.lib") |
| 13 | + |
| 14 | + |
| 15 | +// ==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-== |
| 16 | +// Constants |
| 17 | + |
| 18 | +#define BUFFER_SIZE_16 16 |
| 19 | +#define BUFFER_SIZE_32 32 |
| 20 | +#define BUFFER_SIZE_64 64 |
| 21 | +#define BUFFER_SIZE_128 128 |
| 22 | +#define BUFFER_SIZE_256 256 |
| 23 | +#define BUFFER_SIZE_512 512 |
| 24 | +#define BUFFER_SIZE_1024 1024 |
| 25 | +#define BUFFER_SIZE_2048 2048 |
| 26 | +#define BUFFER_SIZE_4096 4096 |
| 27 | +#define BUFFER_SIZE_8192 8192 |
| 28 | + |
| 29 | +#define DRIVE_LETTER_COUNT 26 |
| 30 | +#define ARRAY_INITIAL_CAPACITY 64 |
| 31 | + |
| 32 | +// ==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-== |
| 33 | + |
| 34 | +#define PREFETCH_SIGNATURE 0x41434353 // "SCCA" |
| 35 | +#define PREFETCH_COMPRESSED_SIGNATURE 0x044D414D // "MAM\x04" |
| 36 | + |
| 37 | + |
| 38 | +// https://github.com/libyal/libscca/blob/main/documentation/Windows%20Prefetch%20File%20(PF)%20format.asciidoc#411-format-version |
| 39 | +/* |
| 40 | + 17 - Used in: Windows XP, Windows 2003 [NOT SUPPORTED] |
| 41 | + 23 - Used in: Windows Vista, Windows 7 [NOT SUPPORTED] |
| 42 | + 26 - Used in: Windows 8.1 [NOT SUPPORTED] |
| 43 | + 30 - Used in: Windows 10 [SUPPORTED] |
| 44 | + 31 - Used in: Windows 11 [SUPPORTED] |
| 45 | +*/ |
| 46 | + |
| 47 | +#define PREFETCH_VERSION_WIN10 30 |
| 48 | +#define PREFETCH_VERSION_WIN11 31 |
| 49 | + |
| 50 | +// https://github.com/libyal/libscca/blob/main/documentation/Windows%20Prefetch%20File%20(PF)%20format.asciidoc?plain=1#L387 |
| 51 | +// https://github.com/libyal/libscca/blob/main/documentation/Windows%20Prefetch%20File%20(PF)%20format.asciidoc?plain=1#L423 |
| 52 | +#define PREFETCH_MAX_LAST_RUN_TIMES 8 |
| 53 | + |
| 54 | +// ==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-== |
| 55 | + |
| 56 | +#define PREFETCH_PATH L"C:\\Windows\\Prefetch" |
| 57 | + |
| 58 | +#define DEFAULT_OUTPUT_FILENAME L"PrefetchData.json" |
| 59 | + |
| 60 | +#define MAX_BINARY_FILTERS 64 |
| 61 | + |
| 62 | +// ==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-== |
| 63 | +// Windows Defintions |
| 64 | + |
| 65 | +#ifndef NT_SUCCESS |
| 66 | +#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0) |
| 67 | +#endif |
| 68 | + |
| 69 | +// https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/nf-ntifs-rtldecompressbufferex |
| 70 | +EXTERN_C NTSTATUS NTAPI RtlDecompressBufferEx( |
| 71 | + IN USHORT CompressionFormat, |
| 72 | + OUT PUCHAR UncompressedBuffer, |
| 73 | + IN ULONG UncompressedBufferSize, |
| 74 | + IN PUCHAR CompressedBuffer, |
| 75 | + IN ULONG CompressedBufferSize, |
| 76 | + OUT PULONG FinalUncompressedSize, |
| 77 | + IN PVOID WorkSpace |
| 78 | +); |
| 79 | + |
| 80 | +// https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/nf-ntifs-rtlgetcompressionworkspacesize |
| 81 | +EXTERN_C NTSTATUS NTAPI RtlGetCompressionWorkSpaceSize( |
| 82 | + IN USHORT CompressionFormatAndEngine, |
| 83 | + OUT PULONG CompressBufferWorkSpaceSize, |
| 84 | + OUT PULONG CompressFragmentWorkSpaceSize |
| 85 | +); |
| 86 | + |
| 87 | +#pragma pack(push, 1) |
| 88 | + |
| 89 | +// https://github.com/libyal/libscca/blob/main/documentation/Windows%20Prefetch%20File%20(PF)%20format.asciidoc#31-file-header |
| 90 | +typedef struct _PREFETCH_MAM_HEADER |
| 91 | +{ |
| 92 | + DWORD dwSignature; // 0x00 - Signature ("MAM\x04" - 0x04 indicates XPRESS Huffman compression) |
| 93 | + DWORD dwUncompressedSize; // 0x04 - Uncompressed data size |
| 94 | +} PREFETCH_MAM_HEADER, *PPREFETCH_MAM_HEADER; |
| 95 | + |
| 96 | +// https://github.com/libyal/libscca/blob/main/documentation/Windows%20Prefetch%20File%20(PF)%20format.asciidoc#41-file-header |
| 97 | +typedef struct _PREFETCH_HEADER |
| 98 | +{ |
| 99 | + DWORD dwVersion; // 0x00 - Format version (17=XP, 23=Vista/7, 26=8.1, 30=10, 31=11) |
| 100 | + DWORD dwSignature; // 0x04 - Signature ("SCCA" - 0x41434353) |
| 101 | + DWORD dwUnknown1; // 0x08 - Unknown (flags? 0x01 = boot prefetch) |
| 102 | + DWORD dwFileSize; // 0x0C - File size (uncompressed) |
| 103 | + WCHAR wszExecutableName[30]; // 0x10 - Executable filename (UTF-16, max 29 chars + null terminator) |
| 104 | + DWORD dwHash; // 0x4C - Prefetch hash (path hash) |
| 105 | + DWORD dwUnknown2; // 0x50 - Unknown (possibly padding) |
| 106 | +} PREFETCH_HEADER, *PPREFETCH_HEADER; |
| 107 | + |
| 108 | +// https://github.com/libyal/libscca/blob/main/documentation/Windows%20Prefetch%20File%20(PF)%20format.asciidoc#442-file-metrics-array-entry---version-23 |
| 109 | +// Versions 23/26/30/31 are all the same |
| 110 | +typedef struct _PREFETCH_FILE_METRICS_ENTRY { |
| 111 | + DWORD dwStartTime; |
| 112 | + DWORD dwDuration; |
| 113 | + DWORD dwAverageDuration; |
| 114 | + DWORD dwFilenameOffset; // Offset into filename strings section |
| 115 | + DWORD dwFilenameLength; // Length in characters |
| 116 | + DWORD dwFlags; |
| 117 | + ULONGLONG ullMftReference; |
| 118 | +} PREFETCH_FILE_METRICS_ENTRY, * PPREFETCH_FILE_METRICS_ENTRY; |
| 119 | + |
| 120 | + |
| 121 | +// https://github.com/libyal/libscca/blob/main/documentation/Windows%20Prefetch%20File%20(PF)%20format.asciidoc#volume-information-entry---version-30 |
| 122 | +// Versions 30/31 are the same |
| 123 | +typedef struct _PREFETCH_VOLUME_INFO { |
| 124 | + DWORD dwDevicePathOffset; // 0x00 - Volume device path offset (relative to start of volume info) |
| 125 | + DWORD dwDevicePathLength; // 0x04 - Volume device path number of characters (without end-of-string) |
| 126 | + FILETIME ftCreationTime; // 0x08 - Volume creation time (FILETIME) |
| 127 | + DWORD dwSerialNumber; // 0x10 - Volume serial number |
| 128 | + DWORD dwFileReferencesOffset; // 0x14 - File references offset (relative to start of volume info) |
| 129 | + DWORD dwFileReferencesSize; // 0x18 - File references data size |
| 130 | + DWORD dwDirectoryStringsOffset; // 0x1C - Directory strings offset (relative to start of volume info) |
| 131 | + DWORD dwDirectoryStringsCount; // 0x20 - Number of directory strings |
| 132 | + BYTE padding[64]; // 0x24 - Unknown/padding (to reach 96 bytes) |
| 133 | +} PREFETCH_VOLUME_INFO, * PPREFETCH_VOLUME_INFO; |
| 134 | + |
| 135 | + |
| 136 | +// https://github.com/libyal/libscca/blob/main/documentation/Windows%20Prefetch%20File%20(PF)%20format.asciidoc#425-file-information---version-30---variant-1 |
| 137 | +// File information - version 30 variant 1 - 220 bytes (Windows 10 pre-1903) |
| 138 | +typedef struct _PREFETCH_FILE_INFO_V30_1 |
| 139 | +{ |
| 140 | + DWORD dwMetricsArrayOffset; // 0x00 - File metrics array offset |
| 141 | + DWORD dwMetricsArrayCount; // 0x04 - Number of file metrics array entries |
| 142 | + DWORD dwTraceChainsOffset; // 0x08 - Trace chains array offset |
| 143 | + DWORD dwTraceChainsCount; // 0x0C - Number of trace chains array entries |
| 144 | + DWORD dwFilenameStringsOffset; // 0x10 - Filename strings offset |
| 145 | + DWORD dwFilenameStringsSize; // 0x14 - Filename strings data size |
| 146 | + DWORD dwVolumesInfoOffset; // 0x18 - Volumes information offset |
| 147 | + DWORD dwVolumesInfoCount; // 0x1C - Number of volumes |
| 148 | + DWORD dwVolumesInfoSize; // 0x20 - Volumes information data size |
| 149 | + DWORD dwTotalDirectoryCount; // 0x24 - Total number of directory strings (across all volumes) |
| 150 | + DWORD dwUnknown1; // 0x28 - Unknown |
| 151 | + FILETIME ftLastRunTime[8]; // 0x2C - Last run times (FILETIME array, 8 entries, 64 bytes) |
| 152 | + DWORD dwUnknown2; // 0x6C - Unknown (remnant data after 8 run times filled) |
| 153 | + DWORD dwUnknown3; // 0x70 - Unknown |
| 154 | + DWORD dwRunCount; // 0x74 - Run count |
| 155 | + BYTE padding[100]; // 0x78 - Unknown/padding (to reach 220 bytes) |
| 156 | +} PREFETCH_FILE_INFO_V30_1, *PPREFETCH_FILE_INFO_V30_1; |
| 157 | + |
| 158 | +// https://github.com/libyal/libscca/blob/main/documentation/Windows%20Prefetch%20File%20(PF)%20format.asciidoc#426-file-information---version-30---variant-2 |
| 159 | +// File information - version 30 variant 2 / version 31 - 212 bytes (Windows 10 1903+ / Windows 11) |
| 160 | +typedef struct _PREFETCH_FILE_INFO_V30_2 |
| 161 | +{ |
| 162 | + DWORD dwMetricsArrayOffset; // 0x00 - File metrics array offset |
| 163 | + DWORD dwMetricsArrayCount; // 0x04 - Number of file metrics array entries |
| 164 | + DWORD dwTraceChainsOffset; // 0x08 - Trace chains array offset |
| 165 | + DWORD dwTraceChainsCount; // 0x0C - Number of trace chains array entries |
| 166 | + DWORD dwFilenameStringsOffset; // 0x10 - Filename strings offset |
| 167 | + DWORD dwFilenameStringsSize; // 0x14 - Filename strings data size |
| 168 | + DWORD dwVolumesInfoOffset; // 0x18 - Volumes information offset |
| 169 | + DWORD dwVolumesInfoCount; // 0x1C - Number of volumes |
| 170 | + DWORD dwVolumesInfoSize; // 0x20 - Volumes information data size |
| 171 | + DWORD dwTotalDirectoryCount; // 0x24 - Total number of directory strings (across all volumes) |
| 172 | + DWORD dwUnknown1; // 0x28 - Unknown |
| 173 | + FILETIME ftLastRunTime[8]; // 0x2C - Last run times (FILETIME array, 8 entries, 64 bytes) |
| 174 | + DWORD dwUnknown2; // 0x6C - Unknown (remnant data after 8 run times filled) |
| 175 | + DWORD dwRunCount; // 0x70 - Run count |
| 176 | + BYTE padding[96]; // 0x74 - Unknown/padding (to reach 212 bytes) |
| 177 | +} PREFETCH_FILE_INFO_V30_2, *PPREFETCH_FILE_INFO_V30_2; |
| 178 | + |
| 179 | +// Common file information structure (shared fields up to offset 0x6C) |
| 180 | +typedef struct _PREFETCH_FILE_INFO |
| 181 | +{ |
| 182 | + DWORD dwMetricsArrayOffset; // 0x00 - File metrics array offset |
| 183 | + DWORD dwMetricsArrayCount; // 0x04 - Number of file metrics array entries |
| 184 | + DWORD dwTraceChainsOffset; // 0x08 - Trace chains array offset |
| 185 | + DWORD dwTraceChainsCount; // 0x0C - Number of trace chains array entries |
| 186 | + DWORD dwFilenameStringsOffset; // 0x10 - Filename strings offset |
| 187 | + DWORD dwFilenameStringsSize; // 0x14 - Filename strings data size |
| 188 | + DWORD dwVolumesInfoOffset; // 0x18 - Volumes information offset |
| 189 | + DWORD dwVolumesInfoCount; // 0x1C - Number of volumes |
| 190 | + DWORD dwVolumesInfoSize; // 0x20 - Volumes information data size |
| 191 | + DWORD dwTotalDirectoryCount; // 0x24 - Total number of directory strings (across all volumes) |
| 192 | + DWORD dwUnknown1; // 0x28 - Unknown |
| 193 | + FILETIME ftLastRunTime[8]; // 0x2C - Last run times (FILETIME array, 8 entries, 64 bytes) |
| 194 | +} PREFETCH_FILE_INFO, *PPREFETCH_FILE_INFO; |
| 195 | +#pragma pack(pop) |
| 196 | + |
| 197 | +#define PREFETCH_FILE_INFO_V30_1_SIZE 220 |
| 198 | +#define PREFETCH_FILE_INFO_V30_2_SIZE 212 |
| 199 | +#define PREFETCH_RUN_COUNT_OFFSET_V30_1 0x74 |
| 200 | +#define PREFETCH_RUN_COUNT_OFFSET_V30_2 0x70 |
| 201 | + |
| 202 | +// ==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-== |
| 203 | +// Output Structures |
| 204 | + |
| 205 | +// Parsed prefetch entry containing extracted information |
| 206 | +typedef struct _PREFETCH_ENTRY |
| 207 | +{ |
| 208 | + WCHAR wszExecutableName[BUFFER_SIZE_64]; // Executable filename (from header) |
| 209 | + WCHAR wszPrefetchFile[MAX_PATH]; // Prefetch filename |
| 210 | + DWORD dwRunCount; // Number of times the executable has been run |
| 211 | + FILETIME ftLastRunTimes[PREFETCH_MAX_LAST_RUN_TIMES]; // Last 8 execution timestamps |
| 212 | + DWORD dwLastRunTimeCount; // Number of valid entries in ftLastRunTimes |
| 213 | + DWORD dwVersion; // Prefetch format version |
| 214 | + DWORD dwHash; // Prefetch path hash |
| 215 | + WCHAR wszExecutablePath[MAX_PATH]; // Full path of executable |
| 216 | + LPWSTR* ppszLoadedFiles; // Array of loaded file paths (DLLs, etc.) |
| 217 | + DWORD dwLoadedFileCount; // Number of entries in ppszLoadedFiles |
| 218 | + LPWSTR* ppszDirectories; // Array of accessed directories |
| 219 | + DWORD dwDirectoryCount; // Number of entries in ppszDirectories |
| 220 | + WCHAR wszVolumeDevicePath[MAX_PATH]; // Volume device path |
| 221 | + DWORD dwVolumeSerialNumber; // Volume serial number |
| 222 | + FILETIME ftVolumeCreationTime; // Volume creation time |
| 223 | + FILETIME ftPrefetchCreated; // Prefetch file creation time (from file system) |
| 224 | + FILETIME ftPrefetchModified; // Prefetch file last modified time (from file system) |
| 225 | +} PREFETCH_ENTRY, *PPREFETCH_ENTRY; |
| 226 | + |
| 227 | +// Dynamic array of prefetch entries |
| 228 | +typedef struct _PREFETCH_LIST |
| 229 | +{ |
| 230 | + DWORD dwCount; |
| 231 | + DWORD dwCapacity; |
| 232 | + PPREFETCH_ENTRY pEntries; |
| 233 | +} PREFETCH_LIST, *PPREFETCH_LIST; |
| 234 | + |
| 235 | + |
| 236 | +// ==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-== |
| 237 | +// Functions |
| 238 | + |
| 239 | +BOOL WritePrefetchJson(IN PPREFETCH_LIST pList, IN PCWSTR pszOutputPath); |
| 240 | + |
| 241 | + |
| 242 | +#endif // !HEADERS_H |
0 commit comments