滴水PE作业添加节并在节中添加代码

#include<iostream>
#include<malloc.h>
#include<Windows.h>
#pragma warning(disable:4996)
using namespace std;
char insertcode[] = {
0x6A,0x00,
0x6A,0x00,
0x6A,0x00,
0x6A,0x00,
0xE8,0x00,0x00,0x00,0x00,
0xE9,0x00,0x00,0x00,0x00
};
int filelen(FILE* pfile)
{
    fseek(pfile, 0, SEEK_END);
    int result = ftell(pfile);
    rewind(pfile);
    return result;
}
int main()
{
    FILE* pfile = fopen("C://Users//52511//Desktop//fg.exe", "rb");
    FILE* newpfile = fopen("C://Users//52511//Desktop//newfg.exe", "wb");
    int length = filelen(pfile);
    char* buffer = NULL;
    buffer = (char*)malloc(sizeof(char) * length+0x1000);
    memset(buffer, 0, length + 0x1000);
    fread(buffer, 1, length, pfile);
    PIMAGE_DOS_HEADER pDosH = (PIMAGE_DOS_HEADER)(buffer);
    PIMAGE_NT_HEADERS pNTH = (PIMAGE_NT_HEADERS)(buffer + pDosH->e_lfanew);
    PIMAGE_FILE_HEADER pFH = (PIMAGE_FILE_HEADER)((char*)pNTH + 0x4);
    PIMAGE_OPTIONAL_HEADER pOH = (PIMAGE_OPTIONAL_HEADER)((char*)pFH + 0x14);
    int opHeaderLen = int(pFH->SizeOfOptionalHeader);
    PIMAGE_SECTION_HEADER pSecH = (PIMAGE_SECTION_HEADER)((char*)pOH + opHeaderLen);
    int numberOfSection = pFH->NumberOfSections;
    //printf("%p\n", MessageBox);
    int MessageBoxAddress = 0x7542B000;
    int agoSizeOfImage = pOH->SizeOfImage;
    pOH->SizeOfImage = pOH->SizeOfImage + 0x1000;
    pFH->NumberOfSections = pFH->NumberOfSections + 1;
    PVOID pSecName = &pSecH[numberOfSection].Name;
    PDWORD pSecMisc = &pSecH[numberOfSection].Misc.VirtualSize;
    PDWORD pSecVirtualAddress = &pSecH[numberOfSection].VirtualAddress;
    PDWORD pSecSizeOfRawData = &pSecH[numberOfSection].SizeOfRawData;
    PDWORD pSecPointToRawData = &pSecH[numberOfSection].PointerToRawData;
    PDWORD pSecCharacteristics = &pSecH[numberOfSection].Characteristics;
    memcpy(pSecName, ".NewSec", 8);
    *pSecMisc = 0x1000;
    *pSecVirtualAddress = agoSizeOfImage;
    *pSecSizeOfRawData = 0x1000;
    *pSecPointToRawData = agoSizeOfImage;
    *pSecCharacteristics = pSecH[0].Characteristics;
    char* codeBegin = buffer + agoSizeOfImage;
    memcpy(codeBegin, insertcode, sizeof(insertcode) / sizeof(char));
    //call
    DWORD calladder = MessageBoxAddress - (pOH->ImageBase + (DWORD)(codeBegin + 8 + 5) - (DWORD)buffer);
    *(PDWORD)(codeBegin + 0x09) = calladder;
    //jmp
    DWORD jmpadder = pOH->ImageBase + pOH->AddressOfEntryPoint - (pOH->ImageBase + (DWORD)codeBegin + 18 - (DWORD)buffer);
    *(PDWORD)(codeBegin + 0x0E) = jmpadder;
    //修改oep
    pOH->AddressOfEntryPoint = codeBegin - buffer;
    fwrite(buffer, 1, length + 0x1000, newpfile);
    fclose(pfile);
    fclose(newpfile);
    free(buffer);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力不当无业游民

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值