51 PIMAGE_DOS_HEADER dosHeader;
52 ULONG ntHeadersOffset;
54 MappedImage->ViewBase = ViewBase;
55 MappedImage->Size =
Size;
57 dosHeader = (PIMAGE_DOS_HEADER)ViewBase;
63 __except (EXCEPTION_EXECUTE_HANDLER)
65 return GetExceptionCode();
70 if (dosHeader->e_magic != IMAGE_DOS_SIGNATURE)
71 return STATUS_INVALID_IMAGE_NOT_MZ;
75 ntHeadersOffset = (ULONG)dosHeader->e_lfanew;
77 if (ntHeadersOffset == 0)
78 return STATUS_INVALID_IMAGE_FORMAT;
79 if (ntHeadersOffset >= 0x10000000 || ntHeadersOffset >= Size)
80 return STATUS_INVALID_IMAGE_FORMAT;
82 MappedImage->NtHeaders = (PIMAGE_NT_HEADERS)
PTR_ADD_OFFSET(ViewBase, ntHeadersOffset);
88 MappedImage->NtHeaders,
89 FIELD_OFFSET(IMAGE_NT_HEADERS, OptionalHeader)
93 MappedImage->NtHeaders,
94 FIELD_OFFSET(IMAGE_NT_HEADERS, OptionalHeader) +
95 MappedImage->NtHeaders->FileHeader.SizeOfOptionalHeader +
96 MappedImage->NtHeaders->FileHeader.NumberOfSections *
sizeof(IMAGE_SECTION_HEADER)
99 __except (EXCEPTION_EXECUTE_HANDLER)
101 return GetExceptionCode();
106 if (MappedImage->NtHeaders->Signature != IMAGE_NT_SIGNATURE)
107 return STATUS_INVALID_IMAGE_FORMAT;
109 MappedImage->Magic = MappedImage->NtHeaders->OptionalHeader.Magic;
112 MappedImage->Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC &&
113 MappedImage->Magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC
115 return STATUS_INVALID_IMAGE_FORMAT;
119 MappedImage->NumberOfSections = MappedImage->NtHeaders->FileHeader.NumberOfSections;
121 MappedImage->Sections = (PIMAGE_SECTION_HEADER)(
122 ((PCHAR)&MappedImage->NtHeaders->OptionalHeader) +
123 MappedImage->NtHeaders->FileHeader.SizeOfOptionalHeader
126 return STATUS_SUCCESS;
130 _In_opt_ PWSTR FileName,
131 _In_opt_ HANDLE FileHandle,
132 _In_ BOOLEAN ReadOnly,
142 &MappedImage->ViewBase,
150 MappedImage->ViewBase,
169 MappedImage->ViewBase
174 _In_opt_ PWSTR FileName,
175 _In_opt_ HANDLE FileHandle,
176 _In_ BOOLEAN ReadOnly,
177 _Out_ PVOID *ViewBase,
182 BOOLEAN openedFile =
FALSE;
184 HANDLE sectionHandle = NULL;
188 if (!FileName && !FileHandle)
189 return STATUS_INVALID_PARAMETER_MIX;
197 ((FILE_EXECUTE | FILE_READ_ATTRIBUTES | FILE_READ_DATA) |
198 (!ReadOnly ? (FILE_APPEND_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_DATA) : 0)) | SYNCHRONIZE,
223 ReadOnly ? PAGE_EXECUTE_READ : PAGE_EXECUTE_READWRITE,
233 viewSize = (SIZE_T)size.QuadPart;
246 ReadOnly ? PAGE_EXECUTE_READ : PAGE_EXECUTE_READWRITE
252 *ViewBase = viewBase;
253 *Size = (SIZE_T)size.QuadPart;
257 NtClose(sectionHandle);
270 PhProbeAddress(Address, Length, MappedImage->ViewBase, MappedImage->Size, 1);
280 for (i = 0; i < MappedImage->NumberOfSections; i++)
283 (Rva >= MappedImage->Sections[i].VirtualAddress) &&
284 (Rva < MappedImage->Sections[i].VirtualAddress + MappedImage->Sections[i].SizeOfRawData)
287 return &MappedImage->Sections[i];
297 _Out_opt_ PIMAGE_SECTION_HEADER *Section
300 PIMAGE_SECTION_HEADER section;
311 (ULONG_PTR)MappedImage->ViewBase +
312 (Rva - section->VirtualAddress) +
313 section->PointerToRawData
318 _In_ PIMAGE_SECTION_HEADER Section,
319 _Out_writes_opt_z_(Count) PSTR Buffer,
321 _Out_opt_ PULONG ReturnCount
329 IMAGE_SIZEOF_SHORT_NAME,
336 *ReturnCount = (ULONG)returnCount;
344 _Out_ PIMAGE_DATA_DIRECTORY *Entry
347 if (MappedImage->Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
349 PIMAGE_OPTIONAL_HEADER32 optionalHeader;
351 optionalHeader = (PIMAGE_OPTIONAL_HEADER32)&MappedImage->NtHeaders->OptionalHeader;
353 if (Index >= optionalHeader->NumberOfRvaAndSizes)
354 return STATUS_INVALID_PARAMETER_2;
356 *Entry = &optionalHeader->DataDirectory[Index];
358 else if (MappedImage->Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
360 PIMAGE_OPTIONAL_HEADER64 optionalHeader;
362 optionalHeader = (PIMAGE_OPTIONAL_HEADER64)&MappedImage->NtHeaders->OptionalHeader;
364 if (Index >= optionalHeader->NumberOfRvaAndSizes)
365 return STATUS_INVALID_PARAMETER_2;
367 *Entry = &optionalHeader->DataDirectory[Index];
371 return STATUS_INVALID_PARAMETER;
374 return STATUS_SUCCESS;
380 _In_ ULONG ProbeLength,
381 _Out_ PVOID *LoadConfig
385 PIMAGE_DATA_DIRECTORY entry;
388 if (MappedImage->Magic != Magic)
389 return STATUS_INVALID_PARAMETER;
399 return STATUS_INVALID_PARAMETER;
405 __except (EXCEPTION_EXECUTE_HANDLER)
407 return GetExceptionCode();
410 *LoadConfig = loadConfig;
412 return STATUS_SUCCESS;
417 _Out_ PIMAGE_LOAD_CONFIG_DIRECTORY32 *LoadConfig
422 IMAGE_NT_OPTIONAL_HDR32_MAGIC,
423 sizeof(IMAGE_LOAD_CONFIG_DIRECTORY32),
430 _Out_ PIMAGE_LOAD_CONFIG_DIRECTORY64 *LoadConfig
435 IMAGE_NT_OPTIONAL_HDR64_MAGIC,
436 sizeof(IMAGE_LOAD_CONFIG_DIRECTORY64),
442 _In_ HANDLE ProcessHandle,
448 IMAGE_DOS_HEADER dosHeader;
449 ULONG ntHeadersOffset;
450 IMAGE_NT_HEADERS32 ntHeaders;
453 RemoteMappedImage->ViewBase = ViewBase;
459 sizeof(IMAGE_DOS_HEADER),
468 if (dosHeader.e_magic != IMAGE_DOS_SIGNATURE)
469 return STATUS_INVALID_IMAGE_NOT_MZ;
473 ntHeadersOffset = (ULONG)dosHeader.e_lfanew;
475 if (ntHeadersOffset == 0 || ntHeadersOffset >= 0x10000000)
476 return STATUS_INVALID_IMAGE_FORMAT;
482 sizeof(IMAGE_NT_HEADERS32),
491 if (ntHeaders.Signature != IMAGE_NT_SIGNATURE)
492 return STATUS_INVALID_IMAGE_FORMAT;
494 RemoteMappedImage->Magic = ntHeaders.OptionalHeader.Magic;
497 RemoteMappedImage->Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC &&
498 RemoteMappedImage->Magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC
500 return STATUS_INVALID_IMAGE_FORMAT;
504 RemoteMappedImage->NumberOfSections = ntHeaders.FileHeader.NumberOfSections;
505 ntHeadersSize = FIELD_OFFSET(IMAGE_NT_HEADERS, OptionalHeader) +
506 ntHeaders.FileHeader.SizeOfOptionalHeader +
507 RemoteMappedImage->NumberOfSections *
sizeof(IMAGE_SECTION_HEADER);
509 if (ntHeadersSize > 1024 * 1024)
510 return STATUS_INVALID_IMAGE_FORMAT;
512 RemoteMappedImage->NtHeaders = PhAllocate(ntHeadersSize);
517 RemoteMappedImage->NtHeaders,
524 PhFree(RemoteMappedImage->NtHeaders);
528 RemoteMappedImage->Sections = (PIMAGE_SECTION_HEADER)(
529 (PCHAR)RemoteMappedImage->NtHeaders +
530 FIELD_OFFSET(IMAGE_NT_HEADERS, OptionalHeader) + ntHeaders.FileHeader.SizeOfOptionalHeader
533 return STATUS_SUCCESS;
540 PhFree(RemoteMappedImage->NtHeaders);
542 return STATUS_SUCCESS;
551 PIMAGE_EXPORT_DIRECTORY exportDirectory;
553 Exports->MappedImage = MappedImage;
559 IMAGE_DIRECTORY_ENTRY_EXPORT,
560 &Exports->DataDirectory
568 Exports->DataDirectory->VirtualAddress,
572 if (!exportDirectory)
573 return STATUS_INVALID_PARAMETER;
579 __except (EXCEPTION_EXECUTE_HANDLER)
581 return GetExceptionCode();
584 Exports->ExportDirectory = exportDirectory;
585 Exports->NumberOfEntries = exportDirectory->NumberOfFunctions;
591 exportDirectory->AddressOfFunctions,
596 exportDirectory->AddressOfNames,
601 exportDirectory->AddressOfNameOrdinals,
606 !Exports->AddressTable ||
607 !Exports->NamePointerTable ||
608 !Exports->OrdinalTable
610 return STATUS_INVALID_PARAMETER;
616 Exports->AddressTable,
617 exportDirectory->NumberOfFunctions *
sizeof(ULONG)
621 Exports->NamePointerTable,
622 exportDirectory->NumberOfNames *
sizeof(ULONG)
626 Exports->OrdinalTable,
627 exportDirectory->NumberOfFunctions *
sizeof(USHORT)
630 __except (EXCEPTION_EXECUTE_HANDLER)
632 return GetExceptionCode();
641 return STATUS_SUCCESS;
652 if (Index >= Exports->ExportDirectory->NumberOfFunctions)
653 return STATUS_PROCEDURE_NOT_FOUND;
655 Entry->Ordinal = Exports->OrdinalTable[Index] + (USHORT)Exports->ExportDirectory->Base;
657 if (Index < Exports->ExportDirectory->NumberOfNames)
660 Exports->MappedImage,
661 Exports->NamePointerTable[Index],
666 return STATUS_INVALID_PARAMETER;
677 return STATUS_SUCCESS;
683 _In_opt_ USHORT Ordinal,
696 return STATUS_PROCEDURE_NOT_FOUND;
698 Ordinal = Exports->OrdinalTable[index] + (USHORT)Exports->ExportDirectory->Base;
701 Ordinal -= (USHORT)Exports->ExportDirectory->Base;
703 if (Ordinal >= Exports->ExportDirectory->NumberOfFunctions)
704 return STATUS_PROCEDURE_NOT_FOUND;
706 rva = Exports->AddressTable[Ordinal];
709 (rva >= Exports->DataDirectory->VirtualAddress) &&
710 (rva < Exports->DataDirectory->VirtualAddress + Exports->DataDirectory->Size)
716 Exports->MappedImage,
721 if (!Function->ForwardedName)
722 return STATUS_INVALID_PARAMETER;
726 Function->Function = NULL;
731 Exports->MappedImage,
735 Function->ForwardedName = NULL;
738 return STATUS_SUCCESS;
744 _In_opt_ USHORT Ordinal,
745 _In_ PVOID RemoteBase,
746 _Out_ PVOID *Function
758 return STATUS_PROCEDURE_NOT_FOUND;
760 Ordinal = Exports->OrdinalTable[index] + (USHORT)Exports->ExportDirectory->Base;
763 Ordinal -= (USHORT)Exports->ExportDirectory->Base;
765 if (Ordinal >= Exports->ExportDirectory->NumberOfFunctions)
766 return STATUS_PROCEDURE_NOT_FOUND;
768 rva = Exports->AddressTable[Ordinal];
771 (rva >= Exports->DataDirectory->VirtualAddress) &&
772 (rva < Exports->DataDirectory->VirtualAddress + Exports->DataDirectory->Size)
776 return STATUS_NOT_SUPPORTED;
783 return STATUS_SUCCESS;
795 if (Exports->ExportDirectory->NumberOfNames == 0)
799 high = Exports->ExportDirectory->NumberOfNames - 1;
806 i = (low + high) / 2;
809 Exports->MappedImage,
810 Exports->NamePointerTable[i],
819 comparison = strcmp(Name, name);
823 else if (comparison < 0)
827 }
while (low <= high);
838 PIMAGE_DATA_DIRECTORY dataDirectory;
839 PIMAGE_IMPORT_DESCRIPTOR descriptor;
842 Imports->MappedImage = MappedImage;
847 IMAGE_DIRECTORY_ENTRY_IMPORT,
856 dataDirectory->VirtualAddress,
861 return STATUS_INVALID_PARAMETER;
863 Imports->DescriptorTable = descriptor;
875 if (descriptor->OriginalFirstThunk == 0 && descriptor->FirstThunk == 0)
882 __except (EXCEPTION_EXECUTE_HANDLER)
884 return GetExceptionCode();
887 Imports->NumberOfDlls = i;
889 return STATUS_SUCCESS;
900 if (Index >= Imports->NumberOfDlls)
901 return STATUS_INVALID_PARAMETER_2;
903 ImportDll->MappedImage = Imports->MappedImage;
904 ImportDll->Flags = Imports->Flags;
908 ImportDll->Descriptor = &Imports->DescriptorTable[Index];
911 ImportDll->MappedImage,
912 ImportDll->Descriptor->Name,
916 if (!ImportDll->Name)
917 return STATUS_INVALID_PARAMETER;
921 if (ImportDll->Descriptor->OriginalFirstThunk)
924 ImportDll->MappedImage,
925 ImportDll->Descriptor->OriginalFirstThunk,
932 ImportDll->MappedImage,
933 ImportDll->Descriptor->FirstThunk,
940 ImportDll->DelayDescriptor = &((PImgDelayDescr)Imports->DelayDescriptorTable)[Index];
943 ImportDll->MappedImage,
944 ((PImgDelayDescr)ImportDll->DelayDescriptor)->rvaDLLName,
948 if (!ImportDll->Name)
949 return STATUS_INVALID_PARAMETER;
954 ImportDll->MappedImage,
955 ((PImgDelayDescr)ImportDll->DelayDescriptor)->rvaINT,
960 if (!ImportDll->LookupTable)
961 return STATUS_INVALID_PARAMETER;
967 if (ImportDll->MappedImage->Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
971 entry = (PULONG)ImportDll->LookupTable;
978 ImportDll->MappedImage,
990 __except (EXCEPTION_EXECUTE_HANDLER)
992 return GetExceptionCode();
995 else if (ImportDll->MappedImage->Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
999 entry = (PULONG64)ImportDll->LookupTable;
1006 ImportDll->MappedImage,
1018 __except (EXCEPTION_EXECUTE_HANDLER)
1020 return GetExceptionCode();
1025 return STATUS_INVALID_PARAMETER;
1028 ImportDll->NumberOfEntries = i;
1030 return STATUS_SUCCESS;
1039 PIMAGE_IMPORT_BY_NAME importByName;
1041 if (Index >= ImportDll->NumberOfEntries)
1042 return STATUS_INVALID_PARAMETER_2;
1044 if (ImportDll->MappedImage->Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
1048 entry = ((PULONG)ImportDll->LookupTable)[Index];
1051 if (entry & IMAGE_ORDINAL_FLAG32)
1054 Entry->Ordinal = (USHORT)IMAGE_ORDINAL32(entry);
1056 return STATUS_SUCCESS;
1061 ImportDll->MappedImage,
1067 else if (ImportDll->MappedImage->Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1071 entry = ((PULONG64)ImportDll->LookupTable)[Index];
1074 if (entry & IMAGE_ORDINAL_FLAG64)
1077 Entry->Ordinal = (USHORT)IMAGE_ORDINAL64(entry);
1079 return STATUS_SUCCESS;
1084 ImportDll->MappedImage,
1092 return STATUS_INVALID_PARAMETER;
1096 return STATUS_INVALID_PARAMETER;
1101 ImportDll->MappedImage,
1103 sizeof(IMAGE_IMPORT_BY_NAME)
1106 __except (EXCEPTION_EXECUTE_HANDLER)
1108 return GetExceptionCode();
1111 Entry->Name = (PSTR)importByName->Name;
1112 Entry->NameHint = importByName->Hint;
1116 return STATUS_SUCCESS;
1125 PIMAGE_DATA_DIRECTORY dataDirectory;
1126 PImgDelayDescr descriptor;
1129 Imports->MappedImage = MappedImage;
1134 IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT,
1143 dataDirectory->VirtualAddress,
1148 return STATUS_INVALID_PARAMETER;
1150 Imports->DelayDescriptorTable = descriptor;
1162 if (descriptor->rvaIAT == 0 && descriptor->rvaINT == 0)
1169 __except (EXCEPTION_EXECUTE_HANDLER)
1171 return GetExceptionCode();
1174 Imports->NumberOfDlls = i;
1176 return STATUS_SUCCESS;
1181 _In_reads_(Count) PUSHORT Buffer,
1188 Sum = (Sum >> 16) + (Sum & 0xffff);
1191 Sum = (Sum >> 16) + Sum;
1204 partialSum =
PhCheckSum(0, (PUSHORT)MappedImage->ViewBase, (ULONG)(MappedImage->Size + 1) / 2);
1207 adjust = (PUSHORT)&MappedImage->NtHeaders->OptionalHeader.CheckSum;
1210 partialSum -= partialSum < adjust[0];
1211 partialSum -= adjust[0];
1212 partialSum -= partialSum < adjust[1];
1213 partialSum -= adjust[1];
1215 checkSum = partialSum + (ULONG)MappedImage->Size;