[滴水逆向]03-13 用c++打印出节表结构

#include <iostream>
#include <windows.h>
#include <malloc.h>
using namespace std;
char* ReadFilePe(const char* peFileName)
{
	FILE* peFile = nullptr;
	fopen_s(&peFile, peFileName, "rb");
	int pesize = 0;
	char* peData = nullptr;
	if (peFile != NULL) //说明打开成功
	{
		if (fseek(peFile, 0, SEEK_END) == 0)
		{
			pesize = ftell(peFile);
			fseek(peFile, 0, SEEK_SET);
			peData = (char*)malloc(pesize);
			if (peData != nullptr)
			{
				memset(peData, '\x00', pesize);
				fread_s(peData, pesize, 1,pesize, peFile);
			}
			else
			{
				cout << "申请内存空间失败" << endl;
			}
		}
		else
		{
			cout << "指针移动到末尾失败" << endl;
		}
	}
	else
	{
		cout << "打开文件失败" << endl;
	}

	if (peFile)
	{
		fclose(peFile);
	}
	return peData;

}

void printSectionTable(const char* peFileName)
{
	char* pedata = ReadFilePe(peFileName);
	char* tempdata = pedata;
	if (pedata == nullptr)
	{
		cout << "数据获取失败" << endl;
	}

	WORD(*wp)[1];
	wp = (WORD(*)[1])pedata;
	DWORD(*dp)[1];
	dp = (DWORD(*)[1])pedata;

	size_t docSize = 0x3c;
	size_t fileSize = 0x14;

	size_t lfanew = *(*(dp)+15);
	size_t OptionalSize = *(*(wp)+(lfanew / 2) + 10);

	size_t NumberOfSection = *(*(wp)+(lfanew / 2) + 3);

	//转到FILE结构:
	tempdata = &tempdata[lfanew + 4];
	//转到OPTIONAL结构
	tempdata = &tempdata[20];
	//转到节表
	tempdata = &tempdata[OptionalSize];

	PIMAGE_SECTION_HEADER tempSectionHeader = nullptr;
	for (size_t i = 0; i < NumberOfSection; i++)
	{
		tempSectionHeader = (PIMAGE_SECTION_HEADER)tempdata;
		tempdata = &tempdata[40]; //两个节表之间得距离
		printf(">>>>>>>>>> 节表:%d <<<<<<<<<<<\n", i);
		printf("Name                 = %s\n", tempSectionHeader->Name);
		printf("Misc                 = %x\n", tempSectionHeader->Misc.VirtualSize);
		printf("VirtualAddress       = %x\n", tempSectionHeader->VirtualAddress);
		printf("SizeOfRawData        = %x\n", tempSectionHeader->SizeOfRawData);
		printf("PointerToRawData     = %x\n", tempSectionHeader->PointerToRawData);
		printf("PointerToRelocations = %x\n", tempSectionHeader->PointerToRelocations);
		printf("PointerToLinenumbers = %x\n", tempSectionHeader->PointerToLinenumbers);
		printf("NumberOfRelocations  = %x\n", tempSectionHeader->NumberOfRelocations);
		printf("NumberOfLinenumbers  = %x\n", tempSectionHeader->NumberOfLinenumbers);
		printf("Characteristics      = %x\n", tempSectionHeader->Characteristics);
	}
	free(pedata);
}

int main()
{
	printSectionTable("Afkayas.1.Exe");
	getchar();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值