手工解析PE(RVA转FOA)

2023-10-12 02:40
文章标签 解析 pe 手工 foa rva

本文主要是介绍手工解析PE(RVA转FOA),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

RVA是程序加载到内存中的地址

FOA是程序在硬盘中的地址

我们知道 程序运行时是加载到内存中的,比如程序执行了一个函数,此时 我们可以通过调试知道函数所在内存中的地址,但是 如果想要修改这个函数,那就需要知道他对应在物理文件中的哪个位置,就是将exe文件加载到内存的逆过程,为了实现通过RVA找FOA的功能,写出了以下代码

思路就是有了RVA的地址,根据内存的节表,找出RVA所在的节 n 和 节的偏移量 x,然后再根据文件的节表,找出 n 所对应的物理 节位置,再加上偏移量x  就是FOA

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include "mycode.h"int main()
{//用于存放硬盘中 和 内存中的PE结构PE PE_file;PE PE_memory;//找出文件中的PE结构//char* ptr=read_file_to_file_buffer("D:\\T\\zx.exe");char* ptr=read_file_to_file_buffer("D:\\CODE\\DLL\\TestDll.dll");set_pe(ptr,&PE_file);//为ImageBuffer申请空间char* p_imagebuffer;p_imagebuffer = (char*) malloc(*PE_file.p_sizeofimage);//找出内存中的PE结构if( p_imagebuffer == NULL){printf("空间不足\n");return 0;}else{	memset(p_imagebuffer,0,*PE_file.p_sizeofimage);//这里一定要注意 p_imagebuffer里面有数据之后 才能set_pe //FileBuffer内容 复制到 ImageBuffertrans_file_buffer_to_image_buffer(ptr,p_imagebuffer);set_pe(p_imagebuffer,&PE_memory);printf("ImageBuffer范围:%x --> %x \n",p_imagebuffer,p_imagebuffer+(*PE_file.p_sizeofimage));}//节表大小int sectablesize = *PE_file.p_numberofsections*40;//节表结束位置int sectableend = (int)(PE_memory.image_section_header_base + sectablesize);printf("节表范围:%x --> %x \n",PE_memory.image_section_header_base,sectableend);int RVA;int FOA;int section_no;			//RVA属于第几节int section_offset;		//RVA节偏移printf("请输入RVA值\n");scanf("%p",&RVA);printf("RVA = %x \n",RVA);//如果RVA在节表结束之前 那么不用转换 直接减去基址就是FOAif( RVA < sectableend ){FOA = RVA - (int)p_imagebuffer;printf("节表之前 无需转化 FOA = %x \n",FOA);}else{//找出 RVA在内存中节数 和 节偏移量int rva_offset = (char*)RVA - p_imagebuffer;//判断是第几个节int i=1;for(i;i<=*PE_memory.p_numberofsections;i++){// RVA的偏移量  在 两点之间 此时的节数才是我们要的if(     rva_offset >=    *PE_memory.pvirtualaddress &&  rva_offset <   ( *PE_memory.pvirtualaddress + (*PE_memory.pmisc) ) ){ section_no=i;section_offset = rva_offset - *PE_memory.pvirtualaddress ;printf("RVA是第%d个节   节偏移量为%d \n",section_no,section_offset);break;}//image_section_header_base=image_section_header_base+40;//这里注意 pvirtualaddress 要往下走40个字节(一个节表的大小),//因为pvirtualaddress 是int 所以指针需要加10 即可 PE_memory.pvirtualaddress=PE_memory.pvirtualaddress+10;	PE_memory.pmisc=PE_memory.pmisc+10;}//根据 节数  找出文件中节的位置  第n节 需要遍历n-1次for(i=1;i<section_no;i++){PE_file.ppointertorawdata=PE_file.ppointertorawdata+10;	}//加上 节偏移量 得出最终在文件中的位置printf("FOA = %x \n",*PE_file.ppointertorawdata+section_offset);}//释放内存free(p_imagebuffer);p_imagebuffer=NULL;read_file_to_file_buffer_free(ptr);return 0;
}

<mycode.h>

typedef struct _PE
{//DOS头short* p_e_magic;	//MZ标记 用于判断是否为可执行文件short* p_e_cblp;short* p_e_cp;short* p_e_crlc;short* p_e_cparhdr;short* p_e_minalloc;short* p_e_maxalloc;short* p_e_ss;short* p_e_sp;short* p_e_csum;short* p_e_ip;short* p_e_cs;short* p_e_lfarlc;short* p_e_ovno;short* p_e_res;short* p_e_oemid;short* p_e_oeminfo;short* p_e_res2;int* p_e_lfanew;  //第60个字节  一行16个字节 第四行的最后四个字节就是 e_lfanew,从文件开始偏移 *p_e_lfanew 就是PE//PE标记char* p_signature;//标准PE头short* p_machine;	//程序运行的CPU型号   0x0任何处理器 0x14C 386及后续处理器short* p_numberofsections;	//节的总数int* p_timedatestamp;int* p_pointertosymboltable;int* p_numberofsymbols;short* p_sizeofoptionalheader;	//可选PE头的大小  32位默认E0 64位默认F0 可以自定义short* p_characteristics;//可选PE头short* p_magic;	//程序运行的CPU型号   0x0任何处理器 0x14C 386及后续处理器char* p_majorlinkerversion;char* p_minorlinkerversion;int* p_sizeofcode; //所有代码节的和 内存对齐字节数的整数倍int* p_sizeofinitializeddata;	//初始化的数据大小int* p_sizeofuninitializeddata;	//未初始化的数据的大小int* p_addressofentrypoint;		//程序入口int* p_baseofcode;int* p_baseofdata;int* p_imagebase;				//内存镜像基址int* p_sectionalignment;	//内存对齐字节数int* p_filealignment;	//文件对齐字节数short* p_majoroperatingsystemversion;short* p_minoroperatingsystemversion;short* p_majorimageversion;short* p_minorimageversion;short* p_majorsubsystemversion;short* p_minorsubsystemversion;int* p_win32versionvalue;int* p_sizeofimage;int* p_sizeofheaders;int* p_checksum;short* p_subsystem;short* p_dllcharacteristics;int* p_sizeofstackreserve;int* p_sizeofstackcommit;int* p_sizeofheapreserve;int* p_sizeofheapcommit;int* p_loaderflags;int* p_numberofrvaandsizes;int* p_datadirectory;//节表char* image_section_header_base;char name[9];int *pmisc;int *pvirtualaddress;int *psizeofrawdata;int *ppointertorawdata;int *ppointertorelocations;int *ppointertolinenumbers;short *pnumberofrelocations;short *pnumberoflinenumbers;int *pcharacteristics;
}PE;//获取文件大小
int get_file_size(char* filename);
//将文件读取到内存中
char* read_file_to_file_buffer(char* filename);
//释放 将文件读取到内存
void  read_file_to_file_buffer_free(char* ptr);
//FileBuffer内容 复制到 ImageBuffer
int trans_file_buffer_to_image_buffer(char* pfilebuffer,char* pimagebuffer);
//设置PE
void set_pe(char* ptr,PE* p_PE );

<mycode.cpp>

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include "mycode.h"//获取文件大小
int get_file_size(char* filename)
{FILE* fp = fopen(filename,"r");int size;if( fp == NULL ){printf("open fail \n");return -1;}fseek(fp,0,SEEK_END);size=ftell(fp);fclose(fp);return size;
}//将文件读取到内存中 返回读取到哪个内存地址
char* read_file_to_file_buffer(char* filename)
{//char* filename="D:\\T\\zx.exe"; int file_size=get_file_size(filename);//申请空间char* ptr;ptr = (char*) malloc(file_size);if( ptr == NULL){printf("空间不足\n");return 0;}memset(ptr,0,file_size);printf("文件读取到:%x \n",ptr);//将文件内容 读取到内存  //注意 这里读取文件 一定要 rb+  否则 读取的文件会缺少字节FILE* fp = fopen(filename,"rb+");fread(ptr,file_size,1,fp);fclose(fp);return ptr;
}
//释放内存
void read_file_to_file_buffer_free(char* ptr)
{free(ptr);ptr=NULL;
}//FileBuffer 转化到 ImageBuffer
int trans_file_buffer_to_image_buffer(char* pfilebuffer,char* pimagebuffer)
{//所有节的范围char* section_begin=NULL;char* section_end=NULL;//找到PE偏移int* p_e_lfanew=(int*)pfilebuffer+15; //PE标志char* p_signature = pfilebuffer+(*p_e_lfanew);	//节数short* p_numberofsections=(short*)(p_signature+6);	//DOS头-节表  这段可以直接复制过去 不用做内存转换  大小在sizeofheaders 记录着int* p_sizeofheaders=(int*)(p_signature+84);memcpy(pimagebuffer,pfilebuffer,*p_sizeofheaders);//节区 复制到 ImageBuffer  每节大小为 sizeofrawdata//遍历节表  复制每一节short* p_sizeofoptionalheader=(short*)(p_signature+20);	char* image_section_header_base=p_signature+4+20+(*p_sizeofoptionalheader);int i=1;for(i;i<=*p_numberofsections;i++){int *pmisc=(int*)(image_section_header_base+8);					//节内存中的大小int *pvirtualaddress=(int*)(image_section_header_base+12);		//节内存中的偏移int *psizeofrawdata=(int*)(image_section_header_base+16);		//节文件中的大小int *ppointertorawdata=(int*)(image_section_header_base+20);	//节文件中的偏移printf("复制第 %d 个节	起始地址:%x \t 结束地址:%x \n",i,pfilebuffer + (*ppointertorawdata),pfilebuffer + (*ppointertorawdata)+ *psizeofrawdata);printf("到第 %d 个节	起始地址:%x \t 结束地址:%x \n",i,pimagebuffer + (*pvirtualaddress),pimagebuffer + (*pvirtualaddress)+ *pmisc);//								    偏移	                                memcpy( pimagebuffer  +  (*pvirtualaddress)      //Image中 节的位置,pfilebuffer   +  (*ppointertorawdata)    //文件中  节的位置 ,*psizeofrawdata);						 //节大小//获取所有节的范围if( i == 1 ){section_begin=pimagebuffer + (*pvirtualaddress);}if( i == *p_numberofsections ){section_end=pimagebuffer + (*pvirtualaddress) + *pmisc;}image_section_header_base=image_section_header_base+40;}printf("RVA值 范围: %x - %x \n",section_begin,section_end);return 0;
}void set_pe(char* ptr,PE* p_PE )
{//DOS头p_PE->p_e_magic=(short*)ptr;	//MZ标记 用于判断是否为可执行文件p_PE->p_e_cblp=(short*)ptr+1;p_PE->p_e_cp=(short*)ptr+2;p_PE->p_e_crlc=(short*)ptr+3;p_PE->p_e_cparhdr=(short*)ptr+4;p_PE->p_e_minalloc=(short*)ptr+5;p_PE->p_e_maxalloc=(short*)ptr+6;p_PE->p_e_ss=(short*)ptr+7;p_PE->p_e_sp=(short*)ptr+8;p_PE->p_e_csum=(short*)ptr+9;p_PE->p_e_ip=(short*)ptr+10;p_PE->p_e_cs=(short*)ptr+11;p_PE->p_e_lfarlc=(short*)ptr+12;p_PE->p_e_ovno=(short*)ptr+13;p_PE->p_e_res=(short*)ptr+17;p_PE->p_e_oemid=(short*)ptr+18;p_PE->p_e_oeminfo=(short*)ptr+19;p_PE->p_e_res2=(short*)ptr+20;p_PE->p_e_lfanew=(int*)ptr+15;  //第60个字节  一行16个字节 第四行的最后四个字节就是 e_lfanew,从文件开始偏移 *p_e_lfanew 就是PE//PE标记p_PE->p_signature = ptr+(*(p_PE->p_e_lfanew));//标准PE头p_PE->p_machine=(short*)(p_PE->p_signature+4);	//程序运行的CPU型号   0x0任何处理器 0x14C 386及后续处理器p_PE->p_numberofsections=(short*)(p_PE->p_signature+6);	//节的总数p_PE->p_timedatestamp=(int*)(p_PE->p_signature+8);p_PE->p_pointertosymboltable=(int*)(p_PE->p_signature+12);p_PE->p_numberofsymbols=(int*)(p_PE->p_signature+16);p_PE->p_sizeofoptionalheader=(short*)(p_PE->p_signature+20);	//可选PE头的大小  32位默认E0 64位默认F0 可以自定义p_PE->p_characteristics=(short*)(p_PE->p_signature+22);//可选PE头p_PE->p_magic=(short*)(p_PE->p_signature+24);	//程序运行的CPU型号   0x0任何处理器 0x14C 386及后续处理器p_PE->p_majorlinkerversion=(char*)(p_PE->p_signature+26);p_PE->p_minorlinkerversion=(char*)(p_PE->p_signature+27);p_PE->p_sizeofcode=(int*)(p_PE->p_signature+28); //所有代码节的和 内存对齐字节数的整数倍p_PE->p_sizeofinitializeddata=(int*)(p_PE->p_signature+32);	//初始化的数据大小p_PE->p_sizeofuninitializeddata=(int*)(p_PE->p_signature+36);	//未初始化的数据的大小p_PE->p_addressofentrypoint=(int*)(p_PE->p_signature+40);		//程序入口p_PE->p_baseofcode=(int*)(p_PE->p_signature+44);p_PE->p_baseofdata=(int*)(p_PE->p_signature+48);p_PE->p_imagebase=(int*)(p_PE->p_signature+52);				//内存镜像基址p_PE->p_sectionalignment=(int*)(p_PE->p_signature+56);	//内存对齐字节数p_PE->p_filealignment=(int*)(p_PE->p_signature+60);	//文件对齐字节数p_PE->p_majoroperatingsystemversion=(short*)(p_PE->p_signature+64);p_PE->p_minoroperatingsystemversion=(short*)(p_PE->p_signature+66);p_PE->p_majorimageversion=(short*)(p_PE->p_signature+68);p_PE->p_minorimageversion=(short*)(p_PE->p_signature+70);p_PE->p_majorsubsystemversion=(short*)(p_PE->p_signature+72);p_PE->p_minorsubsystemversion=(short*)(p_PE->p_signature+74);p_PE->p_win32versionvalue=(int*)(p_PE->p_signature+76);p_PE->p_sizeofimage=(int*)(p_PE->p_signature+80);p_PE->p_sizeofheaders=(int*)(p_PE->p_signature+84);p_PE->p_checksum=(int*)(p_PE->p_signature+88);p_PE->p_subsystem=(short*)(p_PE->p_signature+92);p_PE->p_dllcharacteristics=(short*)(p_PE->p_signature+94);p_PE->p_sizeofstackreserve=(int*)(p_PE->p_signature+96);p_PE->p_sizeofstackcommit=(int*)(p_PE->p_signature+100);p_PE->p_sizeofheapreserve=(int*)(p_PE->p_signature+104);p_PE->p_sizeofheapcommit=(int*)(p_PE->p_signature+108);p_PE->p_loaderflags=(int*)(p_PE->p_signature+112);p_PE->p_numberofrvaandsizes=(int*)(p_PE->p_signature+116);p_PE->p_datadirectory=(int*)(p_PE->p_signature+120);//节表p_PE->image_section_header_base=(p_PE->p_signature)+4+20+ *(p_PE->p_sizeofoptionalheader);memcpy(p_PE->name,p_PE->image_section_header_base,8);p_PE->name[8]='\0';p_PE->pmisc=(int*)(p_PE->image_section_header_base+8);p_PE->pvirtualaddress=(int*)(p_PE->image_section_header_base+12);p_PE->psizeofrawdata=(int*)(p_PE->image_section_header_base+16);p_PE->ppointertorawdata=(int*)(p_PE->image_section_header_base+20);p_PE->ppointertorelocations=(int*)(p_PE->image_section_header_base+24);p_PE->ppointertolinenumbers=(int*)(p_PE->image_section_header_base+28);p_PE->pnumberofrelocations=(short*)(p_PE->image_section_header_base+32);p_PE->pnumberoflinenumbers=(short*)(p_PE->image_section_header_base+34);p_PE->pcharacteristics=(int*)(p_PE->image_section_header_base+36);
}

运行结果如下

用第二节地址验证    结果与PE软件分析一致

 

这篇关于手工解析PE(RVA转FOA)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/192661

相关文章

深度解析Python中递归下降解析器的原理与实现

《深度解析Python中递归下降解析器的原理与实现》在编译器设计、配置文件处理和数据转换领域,递归下降解析器是最常用且最直观的解析技术,本文将详细介绍递归下降解析器的原理与实现,感兴趣的小伙伴可以跟随... 目录引言:解析器的核心价值一、递归下降解析器基础1.1 核心概念解析1.2 基本架构二、简单算术表达

深度解析Java @Serial 注解及常见错误案例

《深度解析Java@Serial注解及常见错误案例》Java14引入@Serial注解,用于编译时校验序列化成员,替代传统方式解决运行时错误,适用于Serializable类的方法/字段,需注意签... 目录Java @Serial 注解深度解析1. 注解本质2. 核心作用(1) 主要用途(2) 适用位置3

Java MCP 的鉴权深度解析

《JavaMCP的鉴权深度解析》文章介绍JavaMCP鉴权的实现方式,指出客户端可通过queryString、header或env传递鉴权信息,服务器端支持工具单独鉴权、过滤器集中鉴权及启动时鉴权... 目录一、MCP Client 侧(负责传递,比较简单)(1)常见的 mcpServers json 配置

从原理到实战解析Java Stream 的并行流性能优化

《从原理到实战解析JavaStream的并行流性能优化》本文给大家介绍JavaStream的并行流性能优化:从原理到实战的全攻略,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的... 目录一、并行流的核心原理与适用场景二、性能优化的核心策略1. 合理设置并行度:打破默认阈值2. 避免装箱

Maven中生命周期深度解析与实战指南

《Maven中生命周期深度解析与实战指南》这篇文章主要为大家详细介绍了Maven生命周期实战指南,包含核心概念、阶段详解、SpringBoot特化场景及企业级实践建议,希望对大家有一定的帮助... 目录一、Maven 生命周期哲学二、default生命周期核心阶段详解(高频使用)三、clean生命周期核心阶

深入解析C++ 中std::map内存管理

《深入解析C++中std::map内存管理》文章详解C++std::map内存管理,指出clear()仅删除元素可能不释放底层内存,建议用swap()与空map交换以彻底释放,针对指针类型需手动de... 目录1️、基本清空std::map2️、使用 swap 彻底释放内存3️、map 中存储指针类型的对象

Java Scanner类解析与实战教程

《JavaScanner类解析与实战教程》JavaScanner类(java.util包)是文本输入解析工具,支持基本类型和字符串读取,基于Readable接口与正则分隔符实现,适用于控制台、文件输... 目录一、核心设计与工作原理1.底层依赖2.解析机制A.核心逻辑基于分隔符(delimiter)和模式匹

Java+AI驱动实现PDF文件数据提取与解析

《Java+AI驱动实现PDF文件数据提取与解析》本文将和大家分享一套基于AI的体检报告智能评估方案,详细介绍从PDF上传、内容提取到AI分析、数据存储的全流程自动化实现方法,感兴趣的可以了解下... 目录一、核心流程:从上传到评估的完整链路二、第一步:解析 PDF,提取体检报告内容1. 引入依赖2. 封装

深度解析Python yfinance的核心功能和高级用法

《深度解析Pythonyfinance的核心功能和高级用法》yfinance是一个功能强大且易于使用的Python库,用于从YahooFinance获取金融数据,本教程将深入探讨yfinance的核... 目录yfinance 深度解析教程 (python)1. 简介与安装1.1 什么是 yfinance?

99%的人都选错了! 路由器WiFi双频合一还是分开好的专业解析与适用场景探讨

《99%的人都选错了!路由器WiFi双频合一还是分开好的专业解析与适用场景探讨》关于双频路由器的“双频合一”与“分开使用”两种模式,用户往往存在诸多疑问,本文将从多个维度深入探讨这两种模式的优缺点,... 在如今“没有WiFi就等于与世隔绝”的时代,越来越多家庭、办公室都开始配置双频无线路由器。但你有没有注