IMAGE_DOS_HEADER DosHeader;
IMAGE_NT_HEADERS32 ExeHeader;
int NumOfSections;
FILE *fp;
fp=fopen(FileName.c_str(),"rb+");
fseek(fp,0,SEEK_SET);
fread(&DosHeader,sizeof(DosHeader),1,fp);
if (DosHeader.e_magic!=IMAGE_DOS_SIGNATURE)
{
ShowMessage("不是有效的MZ文件");
return ;
}
fseek(fp,DosHeader.e_lfanew,SEEK_SET);
fread(&ExeHeader,sizeof(ExeHeader),1,fp);
if (ExeHeader.Signature!=IMAGE_NT_SIGNATURE)
{
ShowMessage("不是有效的PE文件");
return ;
}
int NumSection = ExeHeader.FileHeader.NumberOfSections;
fseek(fp,(DosHeader.e_lfanew+sizeof(ExeHeader.Signature)+sizeof(ExeHeader.FileHeader)+(ExeHeader.FileHeader.SizeOfOptionalHeader)),SEEK_SET);
IMAGE_SECTION_HEADER OLD_SECTION;
for (int i = 0; i < NumSection; i++)
{
fread(&OLD_SECTION,sizeof(IMAGE_SECTION_HEADER),1,fp);
}
IMAGE_SECTION_HEADER iMageNewSection;
memset(&iMageNewSection,0,sizeof(iMageNewSection));
memcpy((char*)iMageNewSection.Name,".fish",strlen(".fish"));
iMageNewSection.VirtualAddress=ExeHeader.OptionalHeader.SizeOfImage;
iMageNewSection.Misc.VirtualSize=0x1000;
iMageNewSection.PointerToRawData=OLD_SECTION.PointerToRawData+OLD_SECTION.SizeOfRawData;
iMageNewSection.SizeOfRawData=0x200;
iMageNewSection.Characteristics=IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE;
fseek(fp,DosHeader.e_lfanew+sizeof(IMAGE_NT_HEADERS)+NumSection*sizeof(IMAGE_SECTION_HEADER),SEEK_SET);
fwrite(&iMageNewSection,sizeof(iMageNewSection),1,fp);
ExeHeader.FileHeader.NumberOfSections++;
ExeHeader.OptionalHeader.SizeOfCode=ExeHeader.OptionalHeader.SizeOfCode+0x1000;
ExeHeader.OptionalHeader.SizeOfImage=ExeHeader.OptionalHeader.SizeOfImage+0x1000;
ExeHeader.OptionalHeader.AddressOfEntryPoint=iMageNewSection.VirtualAddress;
ExeHeader.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT].VirtualAddress = 0;
ExeHeader.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT].Size = 0;
fseek(fp,DosHeader.e_lfanew,SEEK_SET);
fwrite(&ExeHeader,sizeof(ExeHeader),1,fp);
fseek(fp,iMageNewSection.PointerToRawData,SEEK_SET);//来到新节的文件偏移
fputc('A',fp);//比如就说这里,这里来到新节的文件偏移,写入一个字符,按理说后面的都应该用00填充啊,可是只是在文件末尾加了一个A, 整个pe文件大小也没变.. 区段是添加成功了,在LORDPE里查看添加成功了..
小弟刚学习pe结构2天,麻烦哪位朋友有熟悉pe结构的,帮忙看下问题....小弟先谢谢了
[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)