MCPcopy Create free account
hub / github.com/acl-dev/acl / RecordExeRestore

Function RecordExeRestore

lib_fiber/cpp/src/detours/creatwth.cpp:381–458  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

379}
380
381static BOOL RecordExeRestore(HANDLE hProcess, HMODULE hModule, DETOUR_EXE_RESTORE& der)
382{
383 // Save the various headers for DetourRestoreAfterWith.
384 ZeroMemory(&der, sizeof(der));
385 der.cb = sizeof(der);
386
387 der.pidh = (PBYTE)hModule;
388 der.cbidh = sizeof(der.idh);
389 if (!ReadProcessMemory(hProcess, der.pidh, &der.idh, sizeof(der.idh), NULL)) {
390 DETOUR_TRACE(("ReadProcessMemory(idh@%p..%p) failed: %lu\n",
391 der.pidh, der.pidh + der.cbidh, GetLastError()));
392 return FALSE;
393 }
394 DETOUR_TRACE(("IDH: %p..%p\n", der.pidh, der.pidh + der.cbidh));
395
396 // We read the NT header in two passes to get the full size.
397 // First we read just the Signature and FileHeader.
398 der.pinh = der.pidh + der.idh.e_lfanew;
399 der.cbinh = FIELD_OFFSET(IMAGE_NT_HEADERS, OptionalHeader);
400 if (!ReadProcessMemory(hProcess, der.pinh, &der.inh, der.cbinh, NULL)) {
401 DETOUR_TRACE(("ReadProcessMemory(inh@%p..%p) failed: %lu\n",
402 der.pinh, der.pinh + der.cbinh, GetLastError()));
403 return FALSE;
404 }
405
406 // Second we read the OptionalHeader and Section headers.
407 der.cbinh = (FIELD_OFFSET(IMAGE_NT_HEADERS, OptionalHeader) +
408 der.inh.FileHeader.SizeOfOptionalHeader +
409 der.inh.FileHeader.NumberOfSections * sizeof(IMAGE_SECTION_HEADER));
410
411 if (der.cbinh > sizeof(der.raw)) {
412 return FALSE;
413 }
414
415 if (!ReadProcessMemory(hProcess, der.pinh, &der.inh, der.cbinh, NULL)) {
416 DETOUR_TRACE(("ReadProcessMemory(inh@%p..%p) failed: %lu\n",
417 der.pinh, der.pinh + der.cbinh, GetLastError()));
418 return FALSE;
419 }
420 DETOUR_TRACE(("INH: %p..%p\n", der.pinh, der.pinh + der.cbinh));
421
422 // Third, we read the CLR header
423
424 if (der.inh.OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
425 if (der.inh32.CLR_DIRECTORY.VirtualAddress != 0 &&
426 der.inh32.CLR_DIRECTORY.Size != 0) {
427
428 DETOUR_TRACE(("CLR32.VirtAddr=%08lx, CLR.Size=%lu\n",
429 der.inh32.CLR_DIRECTORY.VirtualAddress,
430 der.inh32.CLR_DIRECTORY.Size));
431
432 der.pclr = ((PBYTE)hModule) + der.inh32.CLR_DIRECTORY.VirtualAddress;
433 }
434 }
435 else if (der.inh.OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
436 if (der.inh64.CLR_DIRECTORY.VirtualAddress != 0 &&
437 der.inh64.CLR_DIRECTORY.Size != 0) {
438

Callers 2

UpdateFrom32To64Function · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…