C/C++ 讀取檔案16進位制格式
阿新 • • 發佈:2021-07-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 中的值:
文章出處:https://www.cnblogs.com/lyshark許可協議: 文章中的程式碼均為學習時整理的筆記,部落格中除去明確標註有參考文獻的文章,其他文章【均為原創】作品,轉載請務必【添加出處】,您添加出處是我創作的動力!
防惡意轉載:如果發現您在轉載時,沒有新增本人部落格連結,本人將通過爬蟲批量爬取您部落格中所有內容,打上本人原創版權水印,並進行二次發行,請相互尊重,你尊重我的勞動成果,我才會尊重你。