参考:http://www.mouseos.com/assembly/06.html

参考:http://www.cnblogs.com/tk091/archive/2012/04/18/2456174.html

typedef struct CV_INFO_PDB70
{
    DWORD CvSignature;
    GUID Guid;
    DWORD Age;
    //BYTE PdbFileName[];
    char PdbFilePath[MAX_PATH];
} CV_INFO_PDB70_T;

static const DWORD g_dwTimeStamp = 0x52C652E0;

// 一共有3个地方要replace
int _tmain(int argc, _TCHAR* argv[])
{
    if (argc < 3 || _tcscmp(argv[1], _T("-m")) != 0)
    {
        printf("cmdline format err.\n");
        return 0;
    }

_tprintf(_T("---------------\nprocess file %s.\n"), argv[2]);

FILE *fp;
    fp = _tfopen(argv[2], _T("rb+"));
    if (!fp)
    {
        printf("occur error -1,reason:%d\n", errno);
        return -1;
    }

// IMAGE_NT_HEADER address
    LONG e_lfanew;
    fseek(fp, 0x3c, SEEK_SET);
    fread(&e_lfanew, 4, 1, fp);

// Signature
    DWORD sign;
    fseek(fp, e_lfanew, SEEK_SET);
    fread(&sign, 4, 1, fp);
    if (sign != 0x00004550)
    {
        printf("PE header not matched,sign:%x\n---------------\n", sign);
        fclose(fp);
        return -2;
    }

// IMAGE_FILE_HEADER
    IMAGE_FILE_HEADER ifh;
    fseek(fp, e_lfanew + 4, SEEK_SET);
    fread(&ifh, sizeof(IMAGE_FILE_HEADER), 1, fp);

#ifdef _DEBUG
    printf("IMAGE_FILE_HEADER结构:\n");
    printf("Machine       : %04X\n", ifh.Machine);
    printf("NumberOfSections  : %04X\n", ifh.NumberOfSections);
    printf("TimeDateStamp    : %08X\n", ifh.TimeDateStamp);
    printf("PointerToSymbolTable : %08X\n", ifh.PointerToSymbolTable);
    printf("NumberOfSymbols   : %08X\n", ifh.NumberOfSymbols);
    printf("SizeOfOptionalHeader : %04X\n", ifh.SizeOfOptionalHeader);
    printf("Characteristics   : %04X\n", ifh.Characteristics);
    printf("\n");
#endif

// replace timestamp
    _tprintf(_T("replace %s timestamp,old : %08X, new : %08X.\n"), argv[2], ifh.TimeDateStamp, g_dwTimeStamp);
    ifh.TimeDateStamp = g_dwTimeStamp;
    fseek(fp, e_lfanew + 4, SEEK_SET);
    fwrite((void *)&ifh, sizeof(ifh), 1, fp);

// IMAGE_DIRECTORY_ENTRY_DEBUG
    LONG debugEntryAddr = e_lfanew + 4 + sizeof(IMAGE_FILE_HEADER) + ifh.SizeOfOptionalHeader + (-10) * (long)sizeof(IMAGE_DATA_DIRECTORY);
    fseek(fp, debugEntryAddr, SEEK_SET);

// IMAGE_DATA_DIRECTORY
    IMAGE_DATA_DIRECTORY idd;
    fread(&idd, sizeof(IMAGE_DATA_DIRECTORY), 1, fp);

#ifdef _DEBUG
    printf("IMAGE_DIRECTORY_ENTRY_DEBUG结构:\n");
    printf("VirtualAddress : %08X\n", idd.VirtualAddress);
    printf("Size   : %08X\n", idd.Size);
    printf("IMAGE_DEBUG_DIRECTORY一共有%f个\n", 1.0 * idd.Size / sizeof(IMAGE_DEBUG_DIRECTORY));
    printf("\n");
#endif

// check the address valid or not
    if (idd.VirtualAddress == 0x00 || idd.Size == 0x00)
    {
        _tprintf(_T("Debug information not found in file %s, skip modify debug info.\n---------------\n"), argv[2]);
        fclose(fp);
        return 0;
    }

// IMAGE_DEBUG_DIRECTORY
    IMAGE_DEBUG_DIRECTORY idd2;
    fseek(fp, (WORD)idd.VirtualAddress, SEEK_SET); // need convert virtual address
    fread(&idd2, sizeof(IMAGE_DEBUG_DIRECTORY), 1, fp);

#ifdef _DEBUG
    printf("IMAGE_DEBUG_DIRECTORY结构:\n");
    printf("AddressOfRawData : %08X\n", idd2.AddressOfRawData);
    printf("Characteristics : %08X\n", idd2.Characteristics);
    printf("MajorVersion : %08X\n", idd2.MajorVersion);
    printf("MinorVersion : %08X\n", idd2.MinorVersion);
    printf("PointerToRawData : %08X\n", idd2.PointerToRawData);
    printf("SizeOfData : %08X\n", idd2.SizeOfData);
    printf("TimeDateStamp : %08X\n", idd2.TimeDateStamp);
    printf("Type : %08X\n", idd2.Type);
    printf("\n");
#endif

// replace timestamp
    _tprintf(_T("replace pdb timestamp, old : %08X, new : %08X.\n"), idd2.TimeDateStamp, g_dwTimeStamp);
    idd2.TimeDateStamp = g_dwTimeStamp;
    fseek(fp, (WORD)idd.VirtualAddress, SEEK_SET); // need convert virtual address
    fwrite((void *)&idd2, sizeof(idd2), 1, fp);

// CV_INFO_PDB70
    CV_INFO_PDB70_T cvInfo;
    fseek(fp, idd2.PointerToRawData, SEEK_SET);
    fread(&cvInfo, sizeof(CV_INFO_PDB70_T), 1, fp);

#ifdef _DEBUG
    printf("CV_INFO_PDB70结构:\n");
    printf("Age : %04X\n", cvInfo.Age);
    printf("CvSignature : %04X\n", cvInfo.CvSignature);
    printf("PdbFileName : %s\n", cvInfo.PdbFilePath);
    printf("Guid : %08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
        cvInfo.Guid.Data1, cvInfo.Guid.Data2, cvInfo.Guid.Data3,
        cvInfo.Guid.Data4[0], cvInfo.Guid.Data4[1], cvInfo.Guid.Data4[2],
        cvInfo.Guid.Data4[3], cvInfo.Guid.Data4[4], cvInfo.Guid.Data4[5],
        cvInfo.Guid.Data4[6], cvInfo.Guid.Data4[7]);
    printf("\n");
#endif

if (cvInfo.CvSignature != 0x53445352)   //RSDS
    {
        printf("pdb signature not matched, CvSignature:%x\n---------------\n", cvInfo.CvSignature);
        fclose(fp);
        return -2;
    }

// replace guid
    printf("replace pdb guid,old : %08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
        cvInfo.Guid.Data1, cvInfo.Guid.Data2, cvInfo.Guid.Data3, cvInfo.Guid.Data4[0],
        cvInfo.Guid.Data4[1], cvInfo.Guid.Data4[2], cvInfo.Guid.Data4[3], cvInfo.Guid.Data4[4],
        cvInfo.Guid.Data4[5], cvInfo.Guid.Data4[6], cvInfo.Guid.Data4[7]);
    //_tprintf(_T("replace pdb guid,old : %08X-%04X-%04X-%llX\n"), cvInfo.Guid.Data1, cvInfo.Guid.Data2, cvInfo.Guid.Data3, (__int64)cvInfo.Guid.Data4);
    __int64 tmp = 0xdc38466dca416db1;
    cvInfo.Guid.Data1 = 0xf363bf77;
    cvInfo.Guid.Data2 = 0xb00b;
    cvInfo.Guid.Data3 = 0x4fb0;
    memcpy(&cvInfo.Guid.Data4, &tmp, 8);

//_tprintf(_T("replace pdb guid,new : %08X-%04X-%04X-%llX\n"), cvInfo.Guid.Data1, cvInfo.Guid.Data2, cvInfo.Guid.Data3, tmp);
    printf("replace pdb guid,new : %08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
        cvInfo.Guid.Data1, cvInfo.Guid.Data2, cvInfo.Guid.Data3, cvInfo.Guid.Data4[0],
        cvInfo.Guid.Data4[1], cvInfo.Guid.Data4[2], cvInfo.Guid.Data4[3], cvInfo.Guid.Data4[4],
        cvInfo.Guid.Data4[5], cvInfo.Guid.Data4[6], cvInfo.Guid.Data4[7]);
    fseek(fp, idd2.PointerToRawData, SEEK_SET);
    fwrite((void *)&cvInfo, sizeof(cvInfo), 1, fp);

fclose(fp);

printf("---------------\n");
    //system("pause");
    getchar();

return 0;
}

Make the PE file consistent when code not changed的更多相关文章

  1. PE File.

    Figure 1 - PE File The CLR header stores information to indicate that the PE file is a .NET executab ...

  2. Delphi : Analyze PE file headers?

    Analyze PE file headers? { You'll need a OpenDialog to open a Exe-File and a Memo to show the file i ...

  3. Inject shellcode into PE file

    先声明这是不免杀的,只是演示. 哔哩哔哩视频 新增节 一般能实现特定功能的shellcode的长度都比较长,可以分到几个节上的空白区,但是这样麻烦啊,或者把最后一个节扩大,但是最后一个节一般没有执行的 ...

  4. 《Peering Inside the PE: A Tour of the Win32 Portable Executable File Format》阅读笔记二

    Common Sections The .text section is where all general-purpose code emitted by the compiler or assem ...

  5. dnSpy PE format ( Portable Executable File Format)

    Portable Executable File Format PE Format  微软官方的 What is a .PE file in the .NET framework? [closed] ...

  6. PE Header and Export Table for Delphi

    Malware Analysis Tutorial 8: PE Header and Export Table 2. Background Information of PE HeaderAny bi ...

  7. Reverse Core 第二部分 - 13章 - PE文件格式

    @date: 2016/11/24 @author: dlive ​ PE (portable executable) ,它是微软在Unix平台的COFF(Common Object File For ...

  8. 利用PE数据目录的导入表获取函数名及其地址

    PE文件是以64字节的DOS文件头开始的(IMAGE_DOS_HEADER),接着是一段小DOS程序,然后是248字节的 NT文件头(IMAGE_NT_HEADERS),NT的文件头位置由IMAGE_ ...

  9. Load PE from memory(反取证)(未完)

      Article 1:Loading Win32/64 DLLs "manually" without LoadLibrary() The most important step ...

随机推荐

  1. 使用pandas读取excel

    使用pandas读取excel Excel是微软的经典之作,在这里我们介绍使用Python的pandas数据分析包来解决此问题. pd.read_excel(io, sheet_name = 0, h ...

  2. Java基础 -3.4

    反码(~) 在计算机中,负数以其正值的补码形式表达. 什么叫补码呢?这得从原码,反码说起. 原码:一个整数,按照绝对值大小转换成的二进制数,称为原码. 比如 00000000 00000000 000 ...

  3. Python学习第十五课——类的基本思想(实例化对象,类对象)

    类的基本思想 类:把一类事物的相同的特征和动作整合到一起就是类类是一个抽象的概念 对象:就是基于类而创建的一个具体的事物(具体存在的)也是特征和动作整合到一块 对象写法 # 对象写法 def scho ...

  4. tcp连接建立和断开

    TCP协议作为传输层主要协议之一,具有面向连接,端到端,可靠的全双工通信,面向字节流的数据传输协议. 1.TCP报文段 虽然TCP面试字节流,但TCP传输的数据单元却是报文段.TCP报文段分为TCP首 ...

  5. 如何往gitlab/github上游贡献代码

    Git 是一个开源的分布式版本控制系统,它能够记录每一次改动. 一些概念 仓库:git 中以仓库为单位:每个项目对应一个仓库,如 /eayuntest/Rally./eayuntest/stack 是 ...

  6. iOS 开发之 开发一款自己的美颜相机

    以前在公司做项目时很少遇到对相机.图片进行处理的(非公司业务),只是偶尔上传,裁剪,预览下.近期自己准备写个相机应用,把图片处理的这些技术细节整理下.包含美颜相机,图片美化,简单拼图,艺术拼图等主要模 ...

  7. How to add VTL tapes on DD6300 & label them into "Oracle_VTL" pool

    Dear all , This is  liulei ,   I was back so , How to add VTL tapes on DD6300 & label them into ...

  8. Codeforces1304F.Animal Observation

    分析一下得知是DP问题,时间复杂度符合,设dp[i][j]为从第i天开始,第j个位置能得到的最大值,其有三种转移状态 1.与上一天的选择有重合 2.与上一天的选择没有重合,且上一天的选择在左边 3.与 ...

  9. C语言结构体指针(指向结构体的指针)详解

    C语言结构体指针详解 一.前言 一个指向结构体的变量的指针表示的是这个结构体变量占内存中的起始位置,同样它也可以指向结构体变量数组. *a).b 等价于 a->b. "."一 ...

  10. python面向对象之练习题2

    练习题 需求: 士兵 可以 花钱买一个AK47 士兵 可以 用开开火 士兵 可以 买弹夹 士兵 可以 上子弹 士兵 可以 给 枪 添加子弹 枪 需要弹夹和有子弹的情况下,借助士兵扣动扳机 才能开火 枪 ...