void FixShellReloc(HWND hDlg, PCHAR pFileBuf, PCHAR pShellBuf) {
PIMAGE_DOS_HEADER pFileDosHeader
=
NULL;
PIMAGE_NT_HEADERS32 pFileNtHeader
=
NULL;
PIMAGE_SECTION_HEADER pFileSecHeader
=
NULL;
PIMAGE_SECTION_HEADER pFileLastSecHeader
=
NULL;
PIMAGE_SECTION_HEADER pFileNewSecHeader
=
NULL;
PIMAGE_DOS_HEADER pShellDosHeader
=
NULL;
PIMAGE_NT_HEADERS32 pShellNtHeader
=
NULL;
PIMAGE_SECTION_HEADER pShellSecHeader
=
NULL;
PIMAGE_DATA_DIRECTORY pShellRelocTable
=
NULL;
PIMAGE_BASE_RELOCATION pShellRelocData
=
NULL;
PIMAGE_BASE_RELOCATION pShellRelocItem
=
NULL;
PIMAGE_DATA_DIRECTORY pShellImpTable
=
NULL;
PIMAGE_IMPORT_DESCRIPTOR pShellImpData
=
NULL;
PIMAGE_IMPORT_DESCRIPTOR pShellImpItem
=
NULL;
PIMAGE_THUNK_DATA32 pShellThunkDataItem
=
NULL;
PIMAGE_THUNK_DATA32 pShellFirstThunkItem
=
NULL;
PTYPE_OFFSET pTypeOffset
=
NULL;
UINT iCount
=
0
;
PDWORD pRelocValue
=
0
;
/
/
每个需要重定位的地址
DWORD dwShellHeaderSizeFileAlign
=
0
;
DWORD dwShellHeaderSizeSecAlign
=
0
;
DWORD dwFileNewSecBeginRVA
=
0
;
DWORD dwFIleNewSecBeginRAW
=
0
;
pFileDosHeader
=
(PIMAGE_DOS_HEADER)pFileBuf;
pFileNtHeader
=
(PIMAGE_NT_HEADERS32)((DWORD)pFileDosHeader
+
pFileDosHeader
-
>e_lfanew);
pFileSecHeader
=
(PIMAGE_SECTION_HEADER)(IMAGE_FIRST_SECTION(pFileNtHeader));
pFileLastSecHeader
=
pFileSecHeader
+
pFileNtHeader
-
>FileHeader.NumberOfSections
-
1
;
dwFileNewSecBeginRVA
=
pFileLastSecHeader
-
>VirtualAddress
+
AlignSize(pFileLastSecHeader
-
>Misc.VirtualSize, pFileNtHeader
-
>OptionalHeader.SectionAlignment);
dwFIleNewSecBeginRAW
=
pFileLastSecHeader
-
>PointerToRawData
+
pFileLastSecHeader
-
>SizeOfRawData;
pShellDosHeader
=
(PIMAGE_DOS_HEADER)pShellBuf;
pShellNtHeader
=
(PIMAGE_NT_HEADERS32)((DWORD)pShellDosHeader
+
pShellDosHeader
-
>e_lfanew);
pShellSecHeader
=
(PIMAGE_SECTION_HEADER)(IMAGE_FIRST_SECTION(pShellNtHeader));
dwShellHeaderSizeFileAlign
=
AlignSize(pShellNtHeader
-
>OptionalHeader.SizeOfHeaders, pShellNtHeader
-
>OptionalHeader.FileAlignment);
dwShellHeaderSizeSecAlign
=
AlignSize(pShellNtHeader
-
>OptionalHeader.SizeOfHeaders, pShellNtHeader
-
>OptionalHeader.SectionAlignment);
pShellRelocTable
=
&(pShellNtHeader
-
>OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC]);
pShellRelocData
=
(PIMAGE_BASE_RELOCATION)((DWORD)pShellDosHeader
+
pShellRelocTable
-
>VirtualAddress);
pShellRelocItem
=
pShellRelocData;
pShellImpTable
=
&(pShellNtHeader
-
>OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]);
pShellImpData
=
(PIMAGE_IMPORT_DESCRIPTOR)((DWORD)pShellDosHeader
+
pShellImpTable
-
>VirtualAddress);
pShellImpItem
=
pShellImpData;
while
(pShellRelocItem
-
>VirtualAddress) {
pTypeOffset
=
(PTYPE_OFFSET)(pShellRelocItem
+
1
);
iCount
=
(pShellRelocItem
-
>SizeOfBlock
-
sizeof(IMAGE_BASE_RELOCATION))
/
sizeof(TYPE_OFFSET);
for
(UINT i
=
0
; i < iCount;i
+
+
) {
if
(pTypeOffset[i].
type
=
=
3
) {
/
/
将所有VA地址恢复至LoadLibary前的状态
pRelocValue
=
(PDWORD)((DWORD)pShellDosHeader
+
pShellRelocItem
-
>VirtualAddress
+
(DWORD)(pTypeOffset[i].offset));
*
pRelocValue
=
*
pRelocValue
-
pShellNtHeader
-
>OptionalHeader.ImageBase
-
dwShellHeaderSizeSecAlign
+
pFileNtHeader
-
>OptionalHeader.ImageBase
+
dwFileNewSecBeginRVA;
}
}
/
/
修复重定位表中的RVA
pShellRelocItem
-
>VirtualAddress
=
pShellRelocItem
-
>VirtualAddress
-
dwShellHeaderSizeSecAlign
+
dwFileNewSecBeginRVA;
pShellRelocItem
=
(PIMAGE_BASE_RELOCATION)((DWORD)pShellRelocItem
+
pShellRelocItem
-
>SizeOfBlock);
}
/
/
修复导入表中的RVA
while
(pShellImpItem
-
>Name) {
pShellThunkDataItem
=
(PIMAGE_THUNK_DATA32)(pShellImpItem
-
>OriginalFirstThunk
+
(DWORD)pShellDosHeader);
pShellFirstThunkItem
=
(PIMAGE_THUNK_DATA32)(pShellImpItem
-
>FirstThunk
+
(DWORD)pShellDosHeader);
while
(
*
(PDWORD)pShellThunkDataItem) {
if
(!(
*
(PDWORD)pShellThunkDataItem & IMAGE_ORDINAL_FLAG32)) {
pShellThunkDataItem
-
>u1.AddressOfData
=
pShellThunkDataItem
-
>u1.AddressOfData
-
dwShellHeaderSizeSecAlign
+
dwFileNewSecBeginRVA;
pShellFirstThunkItem
-
>u1.AddressOfData
=
pShellThunkDataItem
-
>u1.AddressOfData;
}
pShellThunkDataItem
+
+
;
pShellFirstThunkItem
+
+
;
}
pShellImpItem
-
>OriginalFirstThunk
=
pShellImpItem
-
>OriginalFirstThunk
-
dwShellHeaderSizeSecAlign
+
dwFileNewSecBeginRVA;
pShellImpItem
-
>FirstThunk
=
pShellImpItem
-
>FirstThunk
-
dwShellHeaderSizeSecAlign
+
dwFileNewSecBeginRVA;
pShellImpItem
-
>Name
=
pShellImpItem
-
>Name
-
dwShellHeaderSizeSecAlign
+
dwFileNewSecBeginRVA;
pShellImpItem
+
+
;
}
pFileNtHeader
-
>OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress
=
pShellRelocTable
-
>VirtualAddress
-
dwShellHeaderSizeSecAlign
+
dwFileNewSecBeginRVA;
pFileNtHeader
-
>OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size
=
pShellRelocTable
-
>Size;
pFileNtHeader
-
>OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress
=
pShellNtHeader
-
>OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress
-
dwShellHeaderSizeSecAlign
+
dwFileNewSecBeginRVA;
pFileNtHeader
-
>OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size
=
pShellNtHeader
-
>OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size;
pFileNtHeader
-
>OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT].VirtualAddress
=
pShellNtHeader
-
>OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT].VirtualAddress
-
dwShellHeaderSizeSecAlign
+
dwFileNewSecBeginRVA;
pFileNtHeader
-
>OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT].Size
=
pShellNtHeader
-
>OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT].Size;
return
;
}