C/C++ 讀取文件16進制格式


讀取代碼

#include <iostream>
#include <Windows.h>
#include <iomanip>
#include <fstream>
#include<cstdlib>
#include<string>

using namespace std;

char HEX[16] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
void setIndex(int num, char* hexNumber)
{
	// 清空行下標
	for (int i = 0; i < 8; i++){
		hexNumber[i] = '0';
	}

	// 設置新的行下標
	int index = 7;
	while (num != 0 && index >= 0)
	{
		hexNumber[index--] = HEX[num % 16];
		num = num / 16;
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	// 打開文件
	string path_r = "C:\\Windows\\SysNative\\ntoskrnl.exe";
	ifstream in = ifstream(path_r, ios::binary);
	if (!in.is_open()){cout << "Error: File Path is Wrong" << endl;}

	// 獲取文件大小、文件名
	long long Beg = in.tellg();
	in.seekg(0, ios::end);
	long long End = in.tellg();
	long long fileSize = End - Beg;
	in.seekg(0, ios::beg);
	cout << "File Size: " << fileSize / 1024.0 << "KB" << endl;

	// 讀文件(每次循環讀取 1 字節)
	int byteBeenRead = 0;
	char hexNumber[9] = "00000000";
	unsigned char temp;
	while (in.read((char*)&temp, 1))
	{
		// 每讀 16 個字節換行
		if (byteBeenRead % 16 == 0)
		{
			// 設置行下標
			cout << endl;
			setIndex(byteBeenRead, hexNumber);
			cout << hexNumber << ":\t";
		}
		byteBeenRead++;
		
		// 讀 1 字節
		int hex = (unsigned)temp;
		char a = HEX[hex / 16];
		char b = HEX[hex % 16];
		cout << a << b << " ";
	}
	// 關閉文件流
	in.close();
	cout << "Read Successfully" << endl;

	getchar();
	return 0;
}

進階篇 - 找 PE 文件內16進制特征碼,計算對應的內存地址

比如我想要找到內存里 KiProcessExpiredTimerList+0x102 的位置:

征碼:

int codeArr_kipetl_102[] = {
		0xff,0x53,0x08,
		0x41,0x3b,0xb4,0x24,0xc4,0x01,0x00,0x00,
		0x0f,0x85,0x48,0xff,0x09,0x00,
		0x40,0x84,0xff
	};
int codeCtrl_kipetl_102[] = {
		1,1,1,
		1,1,1,1,1,1,1,1,
		1,1,1,1,1,1,
		1,1,1
};

在剛才的函數上稍作修改,找到這一串特征碼在文件內出現的位置:

int get_PE_feature_rof(
	string path_r,			// PE 文件全路徑。我這里是:"C:\\Windows\\SysNative\\ntoskrnl.exe"
	int codeArr[],			// 上面提到的第一個數組
	int codeCtrl[],			// 上面提到的第二個數組
	int len					// 數組的長度
){
	// 打開文件
	ifstream in = ifstream(path_r, ios::binary);
	if (!in.is_open()){
		cout << "文件打開失敗:" << GetLastError() << endl;
		in.close();
		return 0;
	}

	// 獲取文件大小、文件名
	long long Beg = in.tellg();
	in.seekg(0, ios::end);
	long long End = in.tellg();
	long long fileSize = End - Beg;
	in.seekg(0, ios::beg);

	// 讀文件(每次循環讀取 1 字節)
	int byteBeenRead = 0;				// 已經讀取的字節數
	unsigned char temp;					// 存放讀取內容的緩沖區				
	int rof_feature = 0;				// 特征 ROF
	int codeArrSub = 0;					// 要對比的 codeArr[] 下標
	BOOL isFound = FALSE;				// 是否找到特征
	while (in.read((char*)&temp, 1) && isFound == FALSE){
		byteBeenRead++;
		// 讀 1 字節
		int hex = (unsigned)temp;
		
		// 比較特征
		for(int i=0;i<len;i++){
			// 需要匹配
			if(codeCtrl[codeArrSub] == 1){
				// 匹配到特征
				if(hex == codeArr[codeArrSub]){
					codeArrSub++;
					// 匹配完成
					if(codeArrSub == len){
						rof_feature = byteBeenRead - len;
						isFound = TRUE;
						break;
					}else{break;}
				}else{codeArrSub=0;break;}
			}else{codeArrSub++;break;}
		}
	}
	//cout << "rof_feature = " << hex << rof_feature << endl;
	in.close();
	return rof_feature;
}

函數返回后成功拿到這個位置的 ROF:

然后看一下這個 ROF(0x30F42) 屬於 PE 文件的哪一個區段。因為 .text 的 ROffset 小於 0x30F42 ,且 ROffset + RSize 大於 0x30F42 ,所以可知這段代碼處於 .text 區段:

所以最終的偏移 = 我們代碼的 ROF - .text區段的ROF + .text區段的VAddr = 0x30F42 - 0x600 + 0x1000 = 0x31942

用 PCHunter 看下 ntoskrnl.exe 的內核基地址(這里不講怎么獲取了)

試下 FFFFF8000421C000 + 0x31942 = FFFFF8000424D942

計算出的值剛好等於 WinDbg 中的值:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM