手工解析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)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:https://blog.csdn.net/qq_34479012/article/details/125118265
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/192661

相关文章

C语言中自动与强制转换全解析

《C语言中自动与强制转换全解析》在编写C程序时,类型转换是确保数据正确性和一致性的关键环节,无论是隐式转换还是显式转换,都各有特点和应用场景,本文将详细探讨C语言中的类型转换机制,帮助您更好地理解并在... 目录类型转换的重要性自动类型转换(隐式转换)强制类型转换(显式转换)常见错误与注意事项总结与建议类型

MySQL 缓存机制与架构解析(最新推荐)

《MySQL缓存机制与架构解析(最新推荐)》本文详细介绍了MySQL的缓存机制和整体架构,包括一级缓存(InnoDBBufferPool)和二级缓存(QueryCache),文章还探讨了SQL... 目录一、mysql缓存机制概述二、MySQL整体架构三、SQL查询执行全流程四、MySQL 8.0为何移除查

在Rust中要用Struct和Enum组织数据的原因解析

《在Rust中要用Struct和Enum组织数据的原因解析》在Rust中,Struct和Enum是组织数据的核心工具,Struct用于将相关字段封装为单一实体,便于管理和扩展,Enum用于明确定义所有... 目录为什么在Rust中要用Struct和Enum组织数据?一、使用struct组织数据:将相关字段绑

使用Java实现一个解析CURL脚本小工具

《使用Java实现一个解析CURL脚本小工具》文章介绍了如何使用Java实现一个解析CURL脚本的工具,该工具可以将CURL脚本中的Header解析为KVMap结构,获取URL路径、请求类型,解析UR... 目录使用示例实现原理具体实现CurlParserUtilCurlEntityICurlHandler

深入解析Spring TransactionTemplate 高级用法(示例代码)

《深入解析SpringTransactionTemplate高级用法(示例代码)》TransactionTemplate是Spring框架中一个强大的工具,它允许开发者以编程方式控制事务,通过... 目录1. TransactionTemplate 的核心概念2. 核心接口和类3. TransactionT

数据库使用之union、union all、各种join的用法区别解析

《数据库使用之union、unionall、各种join的用法区别解析》:本文主要介绍SQL中的Union和UnionAll的区别,包括去重与否以及使用时的注意事项,还详细解释了Join关键字,... 目录一、Union 和Union All1、区别:2、注意点:3、具体举例二、Join关键字的区别&php

Spring IOC控制反转的实现解析

《SpringIOC控制反转的实现解析》:本文主要介绍SpringIOC控制反转的实现,IOC是Spring的核心思想之一,它通过将对象的创建、依赖注入和生命周期管理交给容器来实现解耦,使开发者... 目录1. IOC的基本概念1.1 什么是IOC1.2 IOC与DI的关系2. IOC的设计目标3. IOC

java中的HashSet与 == 和 equals的区别示例解析

《java中的HashSet与==和equals的区别示例解析》HashSet是Java中基于哈希表实现的集合类,特点包括:元素唯一、无序和可包含null,本文给大家介绍java中的HashSe... 目录什么是HashSetHashSet 的主要特点是HashSet 的常用方法hasSet存储为啥是无序的

Linux中shell解析脚本的通配符、元字符、转义符说明

《Linux中shell解析脚本的通配符、元字符、转义符说明》:本文主要介绍shell通配符、元字符、转义符以及shell解析脚本的过程,通配符用于路径扩展,元字符用于多命令分割,转义符用于将特殊... 目录一、linux shell通配符(wildcard)二、shell元字符(特殊字符 Meta)三、s

使用Python实现批量访问URL并解析XML响应功能

《使用Python实现批量访问URL并解析XML响应功能》在现代Web开发和数据抓取中,批量访问URL并解析响应内容是一个常见的需求,本文将详细介绍如何使用Python实现批量访问URL并解析XML响... 目录引言1. 背景与需求2. 工具方法实现2.1 单URL访问与解析代码实现代码说明2.2 示例调用