|
1 | 1 | #include "ReflectiveUnloader.h"
|
2 | 2 |
|
3 |
| -static DWORD ImageSizeFromHeaders(PDOS_HEADER pDosHeader) { |
4 |
| - PIMAGE_NT_HEADERS pImgNtHeaders = NULL; |
5 |
| - PIMAGE_SECTION_HEADER pImgSecHeader = NULL; |
6 |
| - PIMAGE_SECTION_HEADER pImgSecHeaderLastRaw = NULL; |
7 |
| - PIMAGE_SECTION_HEADER pImgSecHeaderCursor = NULL; |
8 |
| - DWORD dwCursor = 0; |
9 |
| - |
10 |
| - pImgNtHeaders = (PIMAGE_NT_HEADERS)((ULONG_PTR)pDosHeader + pDosHeader->e_lfanew); |
11 |
| - pImgSecHeader = (PIMAGE_SECTION_HEADER)((ULONG_PTR)pImgNtHeaders + sizeof(IMAGE_NT_HEADERS)); |
12 |
| - pImgSecHeaderLastRaw = pImgSecHeader; |
13 |
| - for (dwCursor = 0; dwCursor < pImgNtHeaders->FileHeader.NumberOfSections; dwCursor++) { |
14 |
| - pImgSecHeaderCursor = &pImgSecHeader[dwCursor]; |
15 |
| - if (pImgSecHeaderLastRaw->PointerToRawData < pImgSecHeaderCursor->PointerToRawData) { |
16 |
| - pImgSecHeaderLastRaw = pImgSecHeaderCursor; |
17 |
| - } |
18 |
| - } |
19 |
| - return (pImgSecHeaderLastRaw->PointerToRawData + pImgSecHeaderLastRaw->SizeOfRawData); |
20 |
| -} |
21 |
| - |
22 | 3 | static BOOL ReflectiveUnloaderUnimport(PDOS_HEADER pDosHeader) {
|
23 | 4 | // PDOS_HEADER pDosHeader: Pointer to the DOS header of the blob to patch.
|
24 | 5 | // Returns: TRUE on success.
|
@@ -64,56 +45,6 @@ static BOOL ReflectiveUnloaderUnrelocate(PDOS_HEADER pDosHeader, ULONG_PTR pBase
|
64 | 45 | return RebaseImage(pDosHeader, pBaseAddress, (ULONG_PTR)(pImgNtHeaders->OptionalHeader.ImageBase));
|
65 | 46 | }
|
66 | 47 |
|
67 |
| -static BOOL ReflectiveUnloaderRestoreWritable(PDOS_HEADER pDosHeader) { |
68 |
| - // Restore the sections that were backed up in the ".restore" section if it |
69 |
| - // is present. If the ".restore" section is not present, this function will |
70 |
| - // return FALSE and the resulting PE image will probably be corrupted due to |
71 |
| - // changes made to writeable sections persisting in the unloaded copy. |
72 |
| - // |
73 |
| - // PDOS_HEADER pDosHeader: Pointer to the DOS header of the blob to patch. |
74 |
| - // Returns: TRUE on success. |
75 |
| - PIMAGE_SECTION_HEADER pImgSecHeaderCopy = NULL; |
76 |
| - PIMAGE_SECTION_HEADER pImgSecHeaderCursor = NULL; |
77 |
| - PIMAGE_SECTION_HEADER pImgSecHeaderDst = NULL; |
78 |
| - PIMAGE_SECTION_HEADER pImgSecHeaderSrc = NULL; |
79 |
| - DWORD dwImageSize = 0; |
80 |
| - |
81 |
| - pImgSecHeaderCopy = SectionHeaderFromName(pDosHeader, ".restore"); |
82 |
| - if (!pImgSecHeaderCopy) { |
83 |
| - return FALSE; |
84 |
| - } |
85 |
| - if (!pImgSecHeaderCopy->SizeOfRawData) { |
86 |
| - return FALSE; |
87 |
| - } |
88 |
| - |
89 |
| - dwImageSize = ImageSizeFromHeaders(pDosHeader); |
90 |
| - pImgSecHeaderCursor = (PIMAGE_SECTION_HEADER)((ULONG_PTR)pDosHeader + pImgSecHeaderCopy->PointerToRawData); |
91 |
| - while (memcmp(pImgSecHeaderCursor->Name, "\x00\x00\x00\x00\x00\x00\x00\x00", 8)) { |
92 |
| - pImgSecHeaderSrc = pImgSecHeaderCursor; |
93 |
| - pImgSecHeaderCursor += 1; |
94 |
| - |
95 |
| - if (!pImgSecHeaderSrc->SizeOfRawData) { |
96 |
| - continue; |
97 |
| - } |
98 |
| - pImgSecHeaderDst = SectionHeaderFromName(pDosHeader, pImgSecHeaderSrc->Name); |
99 |
| - if (!pImgSecHeaderDst) { |
100 |
| - return FALSE; |
101 |
| - } |
102 |
| - if (pImgSecHeaderDst->SizeOfRawData != pImgSecHeaderSrc->SizeOfRawData) { |
103 |
| - return FALSE; |
104 |
| - } |
105 |
| - if (dwImageSize < (pImgSecHeaderCursor->PointerToRawData + pImgSecHeaderCursor->SizeOfRawData)) { |
106 |
| - return FALSE; |
107 |
| - } |
108 |
| - CopyMemory( |
109 |
| - (PVOID)((ULONG_PTR)pDosHeader + pImgSecHeaderDst->PointerToRawData), |
110 |
| - (PVOID)((ULONG_PTR)pDosHeader + pImgSecHeaderSrc->PointerToRawData), |
111 |
| - pImgSecHeaderDst->SizeOfRawData |
112 |
| - ); |
113 |
| - } |
114 |
| - return TRUE; |
115 |
| -} |
116 |
| - |
117 | 48 | VOID ReflectiveUnloaderFree(PVOID pAddress, SIZE_T dwSize) {
|
118 | 49 | // Free memory that was previously allocated by ReflectiveUnloader().
|
119 | 50 | //
|
@@ -198,7 +129,7 @@ PVOID ReflectiveUnloader(HINSTANCE hInstance, PSIZE_T pdwSize) {
|
198 | 129 | ReflectiveUnloaderUnrelocate(pDosHeader, pBaseAddress);
|
199 | 130 | ReflectiveUnloaderUnimport(pDosHeader);
|
200 | 131 | // This step is optional
|
201 |
| - ReflectiveUnloaderRestoreWritable(pDosHeader); |
| 132 | + ShadowSectionRestore(pDosHeader); |
202 | 133 |
|
203 | 134 | if (pdwSize) {
|
204 | 135 | *pdwSize = dwImageSize;
|
|
0 commit comments