C语言怎么获得进程的PE文件信息

 更新时间:2016年01月02日 12:54:31   投稿:hebedich  
这篇文章主要介绍了C语言怎么获得进程的PE文件信息的相关代码,需要的朋友可以参考下

一、打印Sections信息。下面的程序打印出Windows_Graphics_Programming 1.1中第三个程序“Hello World Version 3:Create a Full-Screen Window"生成的可执行文件的Sections结构字节的信息

#include<stdio.h>
#include<windows.h>

char *strPath="C:/c1_hwv3/Debug/c1_hwv3.exe";

int main()
{
  IMAGE_DOS_HEADER myDosHeader;
  LONG e_lfanew;
  FILE *pFile;
  pFile=fopen(strPath,"rb+");

  fread(&myDosHeader,sizeof(IMAGE_DOS_HEADER),1,pFile);
  e_lfanew=myDosHeader.e_lfanew;

  IMAGE_FILE_HEADER myFileHeader;
  int nSectionCount;

  fseek(pFile,(e_lfanew+sizeof(DWORD)),SEEK_SET);
  fread(&myFileHeader,sizeof(IMAGE_FILE_HEADER),1,pFile);
  nSectionCount=myFileHeader.NumberOfSections;

  IMAGE_SECTION_HEADER *pmySectionHeader=
    (IMAGE_SECTION_HEADER *)calloc(nSectionCount,sizeof(IMAGE_SECTION_HEADER));
  fseek(pFile,(e_lfanew+sizeof(IMAGE_NT_HEADERS)),SEEK_SET);
  fread(pmySectionHeader,sizeof(IMAGE_SECTION_HEADER),nSectionCount,pFile);

  for(int i=0;i<nSectionCount;i++,pmySectionHeader++)
  {
    printf("Name: %s\n", pmySectionHeader->Name);
    printf("union_PhysicalAddress: %08x\n", pmySectionHeader->Misc.PhysicalAddress);
    printf("union_VirtualSize: %04x\n", pmySectionHeader->Misc.VirtualSize);
    printf("VirtualAddress: %08x\n", pmySectionHeader->VirtualAddress);
    printf("SizeOfRawData: %08x\n", pmySectionHeader->SizeOfRawData);
    printf("PointerToRawData: %04x\n", pmySectionHeader->PointerToRawData);
    printf("PointerToRelocations: %04x\n", pmySectionHeader->PointerToRelocations);
    printf("PointerToLinenumbers: %04x\n", pmySectionHeader->PointerToLinenumbers);
    printf("NumberOfRelocations: %04x\n", pmySectionHeader->NumberOfRelocations);
    printf("NumberOfLinenumbers: %04x\n", pmySectionHeader->NumberOfLinenumbers);
    printf("Charateristics: %04x\n", pmySectionHeader->Characteristics);
  }
//  pmySectionHeader-=m_nSectionCount;

  if(pmySectionHeader!=NULL)
  {
    free(pmySectionHeader);
    pmySectionHeader=NULL;
  }

  fclose(pFile);
  return 0;
}

运行程序打印出如下信息

Name: .text

union_PhysicalAddress: 00022350

union_VirtualSize: 22350

VirtualAddress: 00001000

SizeOfRawData: 00023000

PointerToRawData: 1000

PointerToRelocations: 0000

PointerToLinenumbers: 0000

NumberOfRelocations: 0000

NumberOfLinenumbers: 0000

Charateristics: 60000020

Name: .rdata

union_PhysicalAddress: 00001615

union_VirtualSize: 1615

VirtualAddress: 00024000

SizeOfRawData: 00002000

PointerToRawData: 24000

PointerToRelocations: 0000

PointerToLinenumbers: 0000

NumberOfRelocations: 0000

NumberOfLinenumbers: 0000

Charateristics: 40000040

Name: .data

union_PhysicalAddress: 00005650

union_VirtualSize: 5650

VirtualAddress: 00026000

SizeOfRawData: 00004000

PointerToRawData: 26000

PointerToRelocations: 0000

PointerToLinenumbers: 0000

NumberOfRelocations: 0000

NumberOfLinenumbers: 0000

Charateristics: c0000040

Name: .idata

union_PhysicalAddress: 00000b23

union_VirtualSize: 0b23

VirtualAddress: 0002c000

SizeOfRawData: 00001000

PointerToRawData: 2a000

PointerToRelocations: 0000

PointerToLinenumbers: 0000

NumberOfRelocations: 0000

NumberOfLinenumbers: 0000

Charateristics: c0000040

Name: .reloc

union_PhysicalAddress: 00000f00

union_VirtualSize: 0f00

VirtualAddress: 0002d000

SizeOfRawData: 00001000

PointerToRawData: 2b000

PointerToRelocations: 0000

PointerToLinenumbers: 0000

NumberOfRelocations: 0000

NumberOfLinenumbers: 0000

Charateristics: 42000040

pe文件结构图:

时间,时间,会给我答案 time will give me the answer

再给大家分享一则

#include <windows.h>
#include <stdio.h>
#define MAX_SECTION_NUM  16
#define MAX_IMPDESC_NUM  64
 
HANDLE hHeap;
PIMAGE_DOS_HEADER pDosHeader;
PCHAR  pDosStub;
DWORD  dwDosStubSize;
DWORD  dwDosStubOffset;
PIMAGE_NT_HEADERS      pNtHeaders;
PIMAGE_FILE_HEADER     pFileHeader;
PIMAGE_OPTIONAL_HEADER32  pOptHeader;
PIMAGE_SECTION_HEADER  pSecHeaders;
PIMAGE_SECTION_HEADER  pSecHeader[MAX_SECTION_NUM];
WORD wSecNum;
PBYTE pSecData[MAX_SECTION_NUM];
DWORD dwSecSize[MAX_SECTION_NUM];
DWORD dwFileSize;
 
void OutputPEInMem(HANDLE hd)
{
  // 请在这里填入你的代码
  DWORD             dwBase;
  dwBase = (DWORD)hd;
  pDosHeader = (PIMAGE_DOS_HEADER)dwBase;
  pNtHeaders = (PIMAGE_NT_HEADERS)(dwBase + pDosHeader->e_lfanew);
  pOptHeader = &(pNtHeaders->OptionalHeader);
  pFileHeader = &(pNtHeaders->FileHeader);
  printf("Address Of Entry Point: 0x%08x\n", pOptHeader->AddressOfEntryPoint);
  printf("ImageBase: 0x%08x\n", pOptHeader->ImageBase);
  printf("Number Of Sections: %d\n", pFileHeader->NumberOfSections);
  printf("Size Of Image: 0x%04x\n", pOptHeader->SizeOfImage);
  return;
}
 
int main(int argc, char *argv[])
{
  DWORD pid = 0;
  pid=atoi(argv[1]);
  HANDLE hd=OpenProcess(PROCESS_ALL_ACCESS,FALSE,pid);
   
  LPCSTR lpszFileName = "hello.exe";
  LPCSTR lpszInjFileName = "hello_inj0.exe";
 
   
  OutputPEInMem(hd);
  hHeap = GetProcessHeap();
 
  if (! CopyPEFileToMem(lpszFileName)) {
    return 1;
  }
  return 0;
}

相关文章

  • C++实现LeetCode(140.拆分词句之二)

    C++实现LeetCode(140.拆分词句之二)

    这篇文章主要介绍了C++实现LeetCode(140.拆分词句之二),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • C++容器适配与栈的实现及dequeque和优先级详解

    C++容器适配与栈的实现及dequeque和优先级详解

    这篇文章主要介绍了C++容器适配与栈的实现及dequeque和优先级,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2022-10-10
  • C语言中getopt()函数和select()函数的使用方法

    C语言中getopt()函数和select()函数的使用方法

    这篇文章主要介绍了C语言中getopt()函数和select()函数的使用方法,是C语言入门学习中的基础知识,需要的朋友可以参考下
    2015-09-09
  • C/C++实现高并发http服务器的代码示例

    C/C++实现高并发http服务器的代码示例

    这篇文章简单给大家介绍了C/C++实现高并发http服务器的代码示例,文章通过代码和图文介绍的非常详细,感兴趣的同学可以参考阅读
    2023-07-07
  • C语言广播的使用详解

    C语言广播的使用详解

    顾名思义可以把自己的数据发送给在特定范围内的所有人;我们网络编程中的广播一般是通过特定的广播地址把自己的数据发送给局域网内当前在线的客户端
    2022-05-05
  • 桶排序算法的理解及C语言版代码示例

    桶排序算法的理解及C语言版代码示例

    桶排序算法顾名思义,就是把要排序的元素分桶排序后合并结果,这里我们就来看一下桶排序算法的理解及C语言版代码示例:
    2016-07-07
  • C++ Qt开发之使用QUdpSocket实现UDP网络通信

    C++ Qt开发之使用QUdpSocket实现UDP网络通信

    Qt 是一个跨平台C++图形界面开发库,利用Qt可以快速开发跨平台窗体应用程序,本文主要介绍如何运用QUdpSocket组件实现基于UDP的网络通信功能,需要的可以参考下
    2024-03-03
  • C++ Qt开发之LineEdit单行输入组件的用法详解

    C++ Qt开发之LineEdit单行输入组件的用法详解

    Qt 是一个跨平台C++图形界面开发库,利用Qt可以快速开发跨平台窗体应用程序,在Qt中我们可以通过拖拽的方式将不同组件放到指定的位置,实现图形化开发极大的方便了开发效率,本章将重点介绍LineEdit单行输入框组件的常用方法及灵活运用
    2023-12-12
  • C语言基本概念宏定义中的#和##教程

    C语言基本概念宏定义中的#和##教程

    这篇文章主要为大家介绍了C语言基本概念宏定义中的#和##教程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-04-04
  • C语言练习之扫雷小游戏

    C语言练习之扫雷小游戏

    这篇文章主要为大家详细介绍了C语言练习之扫雷小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-05-05

最新评论