生活随笔
收集整理的這篇文章主要介紹了
对PE文件进行十六进制代码(机器码)提取并保存到外部文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言與聲明
秉持開源和共享的理念,博主在這里分享自己的代碼。
博客中的代碼可以將PE文件的十六進制代碼(機器碼)提取出來,這些機器碼可用于機器學習、軟件分析等。
聲明: 轉載請標明來處,創作不易!
代碼使用說明
一共有四套代碼:
- create_hex_code_v1: 將一個文件的全部十六進制代碼輸出保存;
- create_hex_code_v2: 將一個文件十六進制代碼從地址00000400h處開始輸出保存;
- create_hex_code_v3: 將一個文件夾中的全部文件的全部十六進制代碼輸出保存;
- create_hex_code_v4: 將一個文件夾中的全部十六進制代碼從地址00000400h處開始輸出保存;
注意事項:
代碼以c++寫成,核心代碼函數未封裝,所以網友們在使用的時候要把填寫文件路徑處的代碼修改為您自己的路徑。路徑分割時切記用雙斜杠。
代碼:
#include <iostream>
#include <iomanip>
#include <windows.h>
#include <fstream>
#include <vector>
using namespace std
;DWORD
FileSize()
{TCHAR szFileName
[MAX_PATH
] = TEXT("D:\\MC\\Minecraft 1.7.10 工業2自定義NPC 光影懶人包\\[啟動器]HMCL-2.2.4.exe");HANDLE hFile
= CreateFile(szFileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);if (INVALID_HANDLE_VALUE
== hFile
){if (0 == GetLastError()){printf("file not exist");}return 0;}DWORD dwFileSize
= 0;dwFileSize
= GetFileSize(hFile
, NULL);CloseHandle(hFile
);return dwFileSize
;
}
int main()
{vector
<int>pe_hex_code
; ifstream
fin("D:\\MC\\Minecraft 1.7.10 工業2自定義NPC 光影懶人包\\[啟動器]HMCL-2.2.4.exe", ios
::binary
); if (!fin
)exit(0);char c
;long i
= 0, j
= 0;cout
.setf(ios
::uppercase
); DWORD leng
= FileSize(); DWORD count
= 0; ofstream
outfile("test.txt"); outfile
.setf(ios
::uppercase
);while ((j
* 16 + i
) < leng
){c
= fin
.get();pe_hex_code
.push_back(((int)c
) & 0x000000ff);vector
<int>::iterator it
;it
= pe_hex_code
.begin() + count
++;if (i
== 0) {cout
<< hex
<<setfill('0')<< setw(7) << j
<< "0h: ";outfile
<< hex
<< setfill('0') << setw(7) << j
<< "0h: ";}cout
<< hex
<< setfill('0') << setw(2) << *it
<< " ";outfile
<< hex
<< setfill('0') << setw(2) << *it
<< " ";if (i
++ == 15){cout
<< endl
;outfile
<< endl
;i
= 0;j
++;}}fin
.close();outfile
.close();return 0;
}
#include <iostream>
#include <iomanip>
#include <windows.h>
#include <fstream>
#include <vector>
using namespace std
;DWORD
FileSize()
{TCHAR szFileName
[MAX_PATH
] = TEXT("D:\\MC\\Minecraft 1.7.10 工業2自定義NPC 光影懶人包\\[啟動器]HMCL-2.2.4.exe");HANDLE hFile
= CreateFile(szFileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);if (INVALID_HANDLE_VALUE
== hFile
){if (0 == GetLastError()){printf("file not exist");}return 0;}DWORD dwFileSize
= 0;dwFileSize
= GetFileSize(hFile
, NULL);CloseHandle(hFile
);return dwFileSize
;
}
int main()
{vector
<int>pe_hex_code
; ifstream
fin("D:\\MC\\Minecraft 1.7.10 工業2自定義NPC 光影懶人包\\[啟動器]HMCL-2.2.4.exe", ios
::binary
); if (!fin
)exit(0);char c
;long i
= 0, j
= 0;cout
.setf(ios
::uppercase
); DWORD leng
= FileSize(); DWORD count
= 0; ofstream
outfile("test0.bytes"); outfile
.setf(ios
::uppercase
);while ((j
* 16 + i
) < leng
){c
= fin
.get();pe_hex_code
.push_back(((int)c
) & 0x000000ff);vector
<int>::iterator it
;it
= pe_hex_code
.begin() + count
++;if ((j
* 16 + i
) >= 0x00000400){if (i
== 0){cout
<< hex
<< setfill('0') << setw(7) << j
<< "0h: ";outfile
<< hex
<< setfill('0') << setw(7) << j
<< "0h: ";}cout
<< hex
<< setfill('0') << setw(2) << *it
<< " ";outfile
<< hex
<< setfill('0') << setw(2) << *it
<< " ";if (i
++ == 15){cout
<< endl
;outfile
<< endl
;i
= 0;j
++;}}else if (i
++ == 15){i
= 0;j
++;} }fin
.close();outfile
.close();return 0;
}
#include <iostream>
#include <iomanip>
#include <windows.h>
#include <fstream>
#include <vector>
#include <io.h>
#include <direct.h>
using namespace std
;vector
<string
> getFiles(string cate_dir
)
{vector
<string
> files
;_finddata_t file
;long lf
;if ((lf
= _findfirst(cate_dir
.c_str(), &file
)) == -1) {cout
<< cate_dir
<< " not found!!!" << endl
;}else {while (_findnext(lf
, &file
) == 0) {if (strcmp(file
.name
, ".") == 0 || strcmp(file
.name
, "..") == 0)continue;files
.push_back(file
.name
);}}_findclose(lf
);return files
;
}
int main()
{vector
<string
> files
= getFiles((string
)"D:\\Dos\\DOSBox-0.74\\Documentation\\*");cout
<< "文件夾中有" << files
.size() << "個文件" << endl
;string filepath_big
= "D:\\Dos\\DOSBox-0.74\\Documentation\\";for (int x
= 0; x
< files
.size(); x
++){string filepath
= filepath_big
+files
[x
]; char* filepath_true
= (char*)filepath
.c_str();cout
<< "文件名:"<<filepath_true
<< endl
;struct _stat info
;_stat(filepath_true
, &info
);DWORD size
= info
.st_size
; cout
<< "文件大小:"<<size
<< endl
;vector
<int>pe_hex_code
; ifstream
fin(filepath_true
, ios
::binary
); if (!fin
) {cout
<< "文件"<<filepath
<<"讀取錯誤."<<endl
;exit(0);}string filepath_x
= files
[x
] + (string
)".bytes";filepath_x
= (char*)filepath_x
.c_str();cout
<< filepath_x
<< endl
;char c
;long i
= 0, j
= 0;cout
.setf(ios
::uppercase
); DWORD leng
= size
; DWORD count
= 0; ofstream
outfile(filepath_x
); outfile
.setf(ios
::uppercase
);while ((j
* 16 + i
) < leng
){c
= fin
.get();pe_hex_code
.push_back(((int)c
) & 0x000000ff);vector
<int>::iterator it
;it
= pe_hex_code
.begin() + count
++;if (i
== 0){cout
<< hex
<< setfill('0') << setw(7) << j
<< "0h: ";outfile
<< hex
<< setfill('0') << setw(7) << j
<< "0h: ";}cout
<< hex
<< setfill('0') << setw(2) << *it
<< " ";outfile
<< hex
<< setfill('0') << setw(2) << *it
<< " ";if (i
++ == 15){cout
<< endl
;outfile
<< endl
;i
= 0;j
++;}}fin
.close();outfile
.close();}return 0;
}
#include <iostream>
#include <iomanip>
#include <windows.h>
#include <fstream>
#include <vector>
#include <io.h>
#include <direct.h>
using namespace std
;
vector
<string
> getFiles(string cate_dir
)
{vector
<string
> files
;_finddata_t file
;long lf
;if ((lf
= _findfirst(cate_dir
.c_str(), &file
)) == -1) {cout
<< cate_dir
<< " not found!!!" << endl
;}else {while (_findnext(lf
, &file
) == 0) {if (strcmp(file
.name
, ".") == 0 || strcmp(file
.name
, "..") == 0)continue;files
.push_back(file
.name
);}}_findclose(lf
);return files
;
}int main()
{vector
<string
> files
= getFiles((string
)"D:\\Dos\\DOSBox-0.74\\Documentation\\*"); string filepath_folder
= "D:\\Dos\\DOSBox-0.74\\Documentation\\"; for (int x
= 0; x
< files
.size(); x
++) {string filepath_string
= filepath_folder
+files
[x
]; char* filepath_char
= (char*)filepath_string
.c_str();cout
<< "文件名:"<<filepath_char
<< endl
;struct _stat info
;_stat(filepath_char
, &info
);DWORD size
= info
.st_size
; cout
<< "文件大小:"<<size
<< endl
;vector
<int>pe_hex_code
; ifstream
fin(filepath_char
, ios
::binary
); if (!fin
) {cout
<< "文件"<<filepath_string
<<"讀取錯誤."<<endl
;exit(0);}string filepath_x
= files
[x
] + (string
)".bytes";filepath_x
= (char*)filepath_x
.c_str();cout
<< filepath_x
<< endl
;char c
;long i
= 0, j
= 0;cout
.setf(ios
::uppercase
); DWORD leng
= size
; DWORD count
= 0; ofstream
outfile(filepath_x
); outfile
.setf(ios
::uppercase
);while ((j
* 16 + i
) < leng
){c
= fin
.get();pe_hex_code
.push_back(((int)c
) & 0x000000ff);vector
<int>::iterator it
;it
= pe_hex_code
.begin() + count
++;if ((j
* 16 + i
) >= 0x00000400) {if (i
== 0){cout
<< hex
<< setfill('0') << setw(7) << j
<< "0h: ";outfile
<< hex
<< setfill('0') << setw(7) << j
<< "0h: ";}cout
<< hex
<< setfill('0') << setw(2) << *it
<< " ";outfile
<< hex
<< setfill('0') << setw(2) << *it
<< " ";if (i
++ == 15){cout
<< endl
;outfile
<< endl
;i
= 0;j
++;}}else if (i
++ == 15){i
= 0;j
++;}}fin
.close();outfile
.close();cout
<< endl
;}return 0;
}
睡眼惺忪的玉子~
總結
以上是生活随笔為你收集整理的对PE文件进行十六进制代码(机器码)提取并保存到外部文件的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。