PE合并节【内存对齐和文件对其大小不一样版本】

该代码实现了一个将PE文件的各个节进行压缩并合并到一个节的过程。首先,计算新文件的大小,然后分配内存并拷贝头部信息。接着,遍历所有节,逐个拷贝到新的缓冲区,并调整偏移量。最后,更新PE头信息,将所有节的特性合并到第一个节中,删除原有节信息,创建一个单一节的PE文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PVOID PE_Headers::CompressSection(IN PVOID File_Buffer)

{

        PVOID newbuff = NULL;

        PBYTE read = NULL, write = NULL;

        DWORD i;

       

        if (File_Buffer == NULL)

        {

                Error_Flag |= EMPTY_IMAGEBUFFER;

                return NULL;

        }

       

        //新生成的文件大小 = 最后节的RVA + 最后节的FOA - 头的RVA + 头的FOA

        File_Size = pSection_Header[pPE_Header->NumberOfSections - 1].VirtualAddress

                + pSection_Header[pPE_Header->NumberOfSections - 1].SizeOfRawData

                - pSection_Header->VirtualAddress + pSection_Header->PointerToRawData;               

        newbuff = malloc(File_Size);

        memset(newbuff, 0, File_Size);

       

        memcpy (newbuff, pDos_Header, pOptional_Header->SizeOfHeaders);                        //拷贝头部

        for (i = 0; i < pPE_Header->NumberOfSections; i++)

        {

                read = (PBYTE) pDos_Header + pSection_Header[i].PointerToRawData;

                write = (PBYTE) newbuff + pSection_Header[i].VirtualAddress

                        - pSection_Header->VirtualAddress + pSection_Header->PointerToRawData;        //因头部的RVA和FOA的不同产生的偏移量

                memcpy(write, read, pSection_Header[i].SizeOfRawData);                       

        }

       

        Release(File_Buffer);

        GetPEHeaders(newbuff);

       

        //合并后,virtualsize = 文件大小 - 头的文件大小

        pSection_Header->Misc.VirtualSize = File_Size - pSection_Header->PointerToRawData;

        pSection_Header->SizeOfRawData = File_Size - pSection_Header->PointerToRawData;

        for (i = 1; i < pPE_Header->NumberOfSections; i++)

        {

                pSection_Header->Characteristics |= pSection_Header[i].Characteristics;                //把每个节的节属性合并到第一个节里

                memset(&pSection_Header[i], 0, sizeof(IMAGE_SECTION_HEADER));                        //把原来节的数据置0,以便新增一个节

        }

        pPE_Header->NumberOfSections = 1;

        return newbuff;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值