VOID MergeSection()
{
PIMAGE_DOS_HEADER pDos
=
NULL;
PIMAGE_FILE_HEADER pFile
=
NULL;
PIMAGE_NT_HEADERS32 pNt
=
NULL;
PIMAGE_OPTIONAL_HEADER pOpt
=
NULL;
PIMAGE_SECTION_HEADER pSec
=
NULL;
PVOID pFileBuffer
=
NULL;
PVOID pImageBuffer
=
NULL;
PVOID pNewBuffer
=
NULL;
BOOL
isOk
=
FALSE;
DWORD size
=
0
;
ReadPEFile(file_path,&pFileBuffer);
if
(!pFileBuffer)
{
printf(
"读取文件失败"
);
return
;
}
CopyFileBufferToImageBuffer(pFileBuffer, &pImageBuffer);
if
(!pImageBuffer)
{
printf(
"复制文件到imagebuffer失败"
);
free(pFileBuffer);
return
;
}
pDos
=
(PIMAGE_DOS_HEADER)pImageBuffer;
pNt
=
(PIMAGE_NT_HEADERS32)((DWORD)pDos
+
pDos
-
>e_lfanew);
pFile
=
(PIMAGE_FILE_HEADER)((DWORD)pNt
+
4
);
pOpt
=
(PIMAGE_OPTIONAL_HEADER)((DWORD)pFile
+
IMAGE_SIZEOF_FILE_HEADER);
pSec
=
(PIMAGE_SECTION_HEADER)((DWORD)pOpt
+
pFile
-
>SizeOfOptionalHeader);
/
/
修改节的属性
DWORD dwMax
=
pSec[pFile
-
>NumberOfSections
-
1
].Misc.VirtualSize > pSec[pFile
-
>NumberOfSections
-
1
].SizeOfRawData ? pSec[pFile
-
>NumberOfSections
-
1
].Misc.VirtualSize : pSec[pFile
-
>NumberOfSections
-
1
].SizeOfRawData;
pSec
-
>Misc.VirtualSize
=
pSec
-
>SizeOfRawData
=
pSec[pFile
-
>NumberOfSections
-
1
].VirtualAddress
+
dwMax
-
pOpt
-
>SizeOfHeaders;
for
(size_t i
=
1
; i < pFile
-
>NumberOfSections; i
+
+
)
{
pSec
-
>Characteristics |
=
pSec[i].Characteristics;
}
memset(pSec
+
1
,
0
, IMAGE_SIZEOF_SECTION_HEADER
*
(pFile
-
>NumberOfSections
-
1
));
pFile
-
>NumberOfSections
=
1
;
/
/
TO NewBuffer
size
=
CopyImageBufferToNewFileBuffer(pImageBuffer, &pNewBuffer);
if
(size
=
=
0
|| !pNewBuffer)
{
printf(
"存盘失败"
);
free(pFileBuffer);
free(pImageBuffer);
free(pNewBuffer);
return
;
}
isOk
=
MemoryToFile(pNewBuffer, size, write_file_path);
if
(isOk)
{
printf(
"存盘成功"
);
return
;
}
free(pFileBuffer);
free(pImageBuffer);
free(pNewBuffer);
return
;
}