首页 > 其他分享 >DOS头+NT头+节表代码解析

DOS头+NT头+节表代码解析

时间:2023-03-16 21:45:39浏览次数:32  
标签:printf pFile pfileBuffer 节表 PIMAGE pOptionHeader DOS NULL NT

#include "stdafx.h"
#include <malloc.h>
#include <windows.h>


LPVOID readPEFile(LPSTR peFile) //LPVOID是一个没有类型的指针    LPSTR",其相当于char*针
{
    FILE * pFile = NULL;
    DWORD fileSize = 0;
    LPVOID pfileBuffer = NULL;
    
    pFile = fopen(peFile,"rb");
    if(!pFile)
    {
        printf("da kai shi bai");
        return NULL;
    }

    fseek(pFile,0,SEEK_END);
    fileSize = ftell(pFile);
    fseek(pFile,0,SEEK_SET);

    pfileBuffer = malloc(fileSize);
    if(!pfileBuffer)
    {
        printf("内存分配失败");
        free(pfileBuffer);
        fclose(pFile);
        return NULL;
    }
    
    size_t n = fread(pfileBuffer,fileSize,1,pFile);//写数据到堆栈区
    
    if(!n)
    {
        printf("数据读取失败");
        free(pfileBuffer);
        fclose(pFile);
        return NULL;
    }
    
    fclose(pFile);
    return pfileBuffer;//返回堆栈的指针
} 


VOID printNTHeaders() //遍历PE头函数
{
    //定义PE头结构体指针
    LPVOID pfileBuffer = NULL;
    PIMAGE_DOS_HEADER pDosHeader = NULL;
    PIMAGE_NT_HEADERS pNTHeader = NULL;
    PIMAGE_FILE_HEADER pPEHeader = NULL;
    PIMAGE_OPTIONAL_HEADER32 pOptionHeader = NULL;

    pfileBuffer = readPEFile("C:\\windows\\system32\\notepad.exe"); //返回堆栈的指针
    if(!pfileBuffer)
    {
        printf("da kai shi bai");
        return;
    }

    if(*((PWORD)pfileBuffer) != IMAGE_DOS_SIGNATURE)  //先把pFileBuffer转换成PWORD类型的指针
    {
        printf("不是有效的MZ标志\n");
        free(pfileBuffer);
        return;
    }
    pDosHeader = (PIMAGE_DOS_HEADER)pfileBuffer; //把pFileBuffer转换成DOS头结构体指针类型
    printf("********************DOC头********************\n");
    printf("MZ标志:%X\n",pDosHeader->e_magic);
    printf("PE偏移:%x\n",pDosHeader->e_lfanew);
    if(*(PWORD)((DWORD)pfileBuffer+pDosHeader->e_lfanew) != IMAGE_NT_SIGNATURE)
    {
        printf("不是有效的PE标志\n");
        free(pfileBuffer);
        return;
    }
    pNTHeader = (PIMAGE_NT_HEADERS)((DWORD)pfileBuffer+pDosHeader->e_lfanew);
    
    printf("********************NT头********************\n");
    
    printf("NT:%x\n",pNTHeader->Signature);
    pPEHeader = (PIMAGE_FILE_HEADER)(((DWORD)pNTHeader)+4);

    printf("********************PE头********************\n");

    printf("PE:%x\n",pPEHeader->Machine);
    
    printf("节的数量:%x\n",pPEHeader->NumberOfSections);
    
    printf("SizeOfOptionalHeader:%x\n",pPEHeader->SizeOfOptionalHeader);

    pOptionHeader = (PIMAGE_OPTIONAL_HEADER32)((DWORD)pPEHeader+IMAGE_SIZEOF_FILE_HEADER);//这里的IMAGE_SIZEOF_FILE_HEADER是二十个字节。
    printf("********************OPTIOIN_PE头********************\n");

    printf("OPTION_PE:%x\n",pOptionHeader->Magic);
    
    printf("sizeofcode=%x\n",pOptionHeader->SizeOfCode);
    
    printf("baseofcode=%x\n",pOptionHeader->BaseOfCode);

    printf("baseofdata=%x\n",pOptionHeader->BaseOfData);

    printf("imagebase=%x\n",pOptionHeader->ImageBase);
    
    printf("sectionalignment=%x\n",pOptionHeader->SectionAlignment);
    
    printf("filealignment=%x\n",pOptionHeader->FileAlignment);

    printf("sizeofimage=%x\n",pOptionHeader->SizeOfImage);

    printf("sizeofheader=%x\n",pOptionHeader->SizeOfHeaders);

    printf("checksum=%x\n",pOptionHeader->CheckSum);

    free(pfileBuffer);
}

int main(int argc,char* argv[])
{
    printNTHeaders();
}

 

标签:printf,pFile,pfileBuffer,节表,PIMAGE,pOptionHeader,DOS,NULL,NT
From: https://www.cnblogs.com/cspecialr/p/17224268.html

相关文章

  • 血泪的线上bug,有关Object.fromEntries
    因为这个线上bug引发的反思:1,兼容性测试的重要性2,代码review的重要性3,技术敏感的重要性还有很多……因为出现线上bug,这个链路上每个人都有责任和需要学习的地方,而作为......
  • Swoole\Event::rshutdown(): Event::wait() in shutdown function is deprecated
    1<?php23use\Swoole\Coroutine;4usefunction\Swoole\Coroutine\run;5use\Swoole\Coroutine\Channel;67//参考链接:https://wiki.swoole.com/#/co......
  • AtCoder Regular Contest 158
    Preface这场比赛刚好周日晚上没事就打了,堪堪混过三道题,也算是小上了一波分吧但是由于A题脑抽了一波卡了30min,导致排名不高,也没时间看DE了,属实有点可惜A-+3+5+7显......
  • 安装Centos7初始化操作系统
    设置静态网络1、设置静态网络,一是用来不让地址飘忽不定,二来是为了访问外网。命令如下:[root@localhost~]#vim/etc/sysconfig/network-scripts/ifcfg-ens33修改内容......
  • High-Resolution Image Synthesis with Latent Diffusion Models
    目录概大概流程代码RombachR.,BlattmannA.,LorenzD.,EsserP.andOmmerB.High-resolutionimagesynthesiswithlatentdiffusionmodels.InIEEEComputerV......
  • CountDownLatch的用法
    CountDownLatch的两个重要方法::await()与countDown():await():调用await()方法的线程会被封装成共享节点加入同步队列阻塞等待,直至state=0时才会唤醒同步队列中所有的线程......
  • centos7 安装 postgresql-9.2
    1.添加yum配置yuminstall-yhttp://download.postgresql.org/pub/repos/yum/9.2/redhat/rhel-7-x86_64/pgdg-centos92-9.2-3.noarch.rpm2.安装服务yumins......
  • IntelliJ IDEA集成本地Maven步骤
    IntelliJIDEA集成本地Maven步骤一、前期准备Maven已经在本地环境配置完成,步骤可以参考我的这篇文章:https://www.cnblogs.com/rainbow-1/p/17223811.html二、IDEAmave......
  • 基本的Dos命令
    打开cmd的方式(黑色命令窗口)1.开始+Windows工具+命令提示符2.win键+R键输入cmd打开控制台(推荐使用)3.在任意的文件夹下面,按住shift键+鼠标右键点击,选择在终端中打开4.资......
  • Android代码静态检查(lint、Checkstyle、ktlint、Detekt)
    Android代码静态检查(lint、Checkstyle、ktlint、Detekt)在​​Android​​项目开发过程中,开发团队往往要花费大量的时间和精力发现并修改代码缺陷。静态代码分析工具能够在代......