前言与声明

秉持开源和共享的理念,博主在这里分享自己的代码。
博客中的代码可以将PE文件的十六进制代码(机器码)提取出来,这些机器码可用于机器学习、软件分析等。

声明: 转载请标明来处,创作不易!


代码使用说明

一共有四套代码:

  • create_hex_code_v1: 将一个文件的全部十六进制代码输出保存;
  • create_hex_code_v2: 将一个文件十六进制代码从地址00000400h处开始输出保存;
  • create_hex_code_v3: 将一个文件夹中的全部文件的全部十六进制代码输出保存;
  • create_hex_code_v4: 将一个文件夹中的全部十六进制代码从地址00000400h处开始输出保存;

注意事项:
代码以c++写成,核心代码函数未封装,所以网友们在使用的时候要把填写文件路径处的代码修改为您自己的路径。路径分割时切记用双斜杠

代码:

  • create_hex_code_v1:
#include <iostream>
#include <iomanip>
#include <windows.h>
#include <fstream>
#include <vector>
using namespace std;DWORD FileSize() //获取PE文件的大小
{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; //创建容器pe_hex_code,类似于数组,但其长度是不受限的,可以完整地输出PE文件的十六进制代码ifstream fin("D:\\MC\\Minecraft 1.7.10 工业2自定义NPC 光影懒人包\\[启动器]HMCL-2.2.4.exe", ios::binary); //读PE文件if (!fin)exit(0);char c;long i = 0, j = 0;cout.setf(ios::uppercase); //ios::uppercase 在以科学计数法输出E和16进制数大写输出.DWORD leng = FileSize(); //PE文件大小DWORD count = 0; //用于记录容器pe_hex_code中正在输出的十六进制代码的indexofstream outfile("test.txt"); //将PE文件的十六进制代码输出outfile.setf(ios::uppercase);//ios::uppercase 在以科学计数法输出E和16进制数大写输出.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;
}
  • create_hex_code_v2:
#include <iostream>
#include <iomanip>
#include <windows.h>
#include <fstream>
#include <vector>
using namespace std;DWORD FileSize() //获取PE文件的大小
{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; //创建容器pe_hex_code,类似于数组,但其长度是不受限的,可以完整地输出PE文件的十六进制代码ifstream fin("D:\\MC\\Minecraft 1.7.10 工业2自定义NPC 光影懒人包\\[启动器]HMCL-2.2.4.exe", ios::binary); //读PE文件if (!fin)exit(0);char c;long i = 0, j = 0;cout.setf(ios::uppercase); //ios::uppercase 在以科学计数法输出E和16进制数大写输出.DWORD leng = FileSize(); //PE文件大小DWORD count = 0; //用于记录容器pe_hex_code中正在输出的十六进制代码的indexofstream outfile("test0.bytes"); //将PE文件的十六进制代码输出outfile.setf(ios::uppercase);//ios::uppercase 在以科学计数法输出E和16进制数大写输出.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;
}
  • create_hex_code_v3:
/*#include <iostream>
#include <iomanip>
#include <windows.h>
#include <fstream>
using namespace std;#define MAX 16*10000
int value[MAX];int main()
{ifstream fin("D:\\MC\\Minecraft 1.7.10 工业2自定义NPC 光影懒人包\\[启动器]HMCL-2.2.4.exe" ,ios::binary); //读取文件if (!fin)exit(0);else{fin.seekg(0, ios::end);int size = (int)fin.tellg();cout << "size:" << size << endl;;char c;long i = 0, j = 0;cout.setf(ios::uppercase); //ios::uppercase 在以科学计数法输出E和16进制数大写输出.cout << setfill('0');while ((j * 16 + i) < size ){c = fin.get(); //ifstream的.get()函数用于读取一个字符,字符大小为一个bytevalue[j * 16 + i] = ( ((int)c) & 0x000000FF); //(int)c会将byte的内存空间高位补1(也就是按符号位补位)扩充到32位,所以前16位需要置0if (i == 0)cout << hex << setw(7) << j << "0h: ";cout << hex << setw(2) << value[j * 16 + i] << " ";if (i++ == 15){cout << endl;i = 0;j++;}}fin.close();}return 0;
}
*/#include <iostream>
#include <iomanip>
#include <windows.h>
#include <fstream>
#include <vector>
#include <io.h>
#include <direct.h>
using namespace std;/*DWORD FileSize() //获取PE文件的大小
{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;
}*/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) {//输出文件名//cout<<file.name<<endl;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; //DWORD:4字节无符号整数*/cout << "文件大小:"<<size << endl;///vector<int>pe_hex_code; //创建容器pe_hex_code,类似于数组,但其长度是不受限的,可以完整地输出PE文件的十六进制代码ifstream fin(filepath_true, ios::binary); //读PE文件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); //ios::uppercase 在以科学计数法输出E和16进制数大写输出.DWORD leng = size; //PE文件大小DWORD count = 0; //用于记录容器pe_hex_code中正在输出的十六进制代码的indexofstream outfile(filepath_x); //将PE文件的十六进制代码输出outfile.setf(ios::uppercase);//ios::uppercase 在以科学计数法输出E和16进制数大写输出.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;
}
  • create_hex_code_v4:
/*************************************************Copyright:000000Author:Enmin ZhuDate:2020-02-18Description:获取文件夹中所有PE文件的十六进制代码并输出保存为.bytes文件*************************************************/#include <iostream>
#include <iomanip>
#include <windows.h>
#include <fstream>
#include <vector>
#include <io.h>
#include <direct.h>
using namespace std;/******************************************
Brief:      //将文件夹中所有文件的文件名存入容器中
Function:   //getFiles
Param:      //cate_dir 文件夹路径
******************************************/
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) {//输出文件名//cout<<file.name<<endl;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_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; //DWORD:4字节无符号整数cout << "文件大小:"<<size << endl;//创建容器pe_hex_code,类似于数组,但其长度是不受限的,可以完整地保存PE文件的十六进制代码vector<int>pe_hex_code; ifstream fin(filepath_char, ios::binary); //读PE文件if (!fin) {cout << "文件"<<filepath_string<<"读取错误."<<endl;exit(0);}//将PE文件输出保存为.bytes文件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); //ios::uppercase 在以科学计数法输出E和16进制数大写输出.DWORD leng = size; //PE文件大小DWORD count = 0; //用于记录容器pe_hex_code中正在输出的十六进制代码的indexofstream outfile(filepath_x); //将PE文件的十六进制代码输出outfile.setf(ios::uppercase);//ios::uppercase 在以科学计数法输出E和16进制数大写输出.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) //从PE文件的00000400h处开始输出打印、保存{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文件进行十六进制代码(机器码)提取并保存到外部文件相关推荐

  1. FFmpeg从视频中提取音频保存为mp3文件

    如何把视频文件中的音频数据提取出来,网络上有不少音频提取工具,但作为技术人员提到音视频处理,肯定会想到FFmpeg.本文介绍了如何基于FFmpeg从视频中提取音频,然后将音频保存为mp3格式的音频文件 ...

  2. 怎样换通达信服务器文件夹,通达信的指标模版保存在那个文件夹,如何迁移

    通达信的指标模版保存在那个文件夹,如何迁移 在T0002文件夹里,PriCS.DAT是软件自带指标,PriGS.DAT是你的自定义指标. 自选股票T0002\blocknew\ZXG.blk 自编公式 ...

  3. 将多个npz文件合成成一个由list形式保存的npz文件

    字典中存在array元组,不能保存成json文件,但可以保存成npz文件. 可以将多个字典合并成一个大字典或者list,然后保存成npz文件. append() import os import nu ...

  4. java io 文件是否存在,代码实例Java IO判断目录和文件是否存在

    我们先来看完整的代码: import java.io.File; public class JudgeFile { public static void main(String[] args) { F ...

  5. php如何写文件缓存,PHP代码篇(八)--php实现文件缓存的读写

    标签:mod   bool   重写   lin   function   防止   value   files   写入文件 说,近期接手的论坛项目,因为对于代码,不是特别熟悉,有个地方需要用到缓存 ...

  6. 提取txt文件某一列数据,并保存为txt文件

    import sys path = "./001.txt" #数据来源 f = open(path) line = f.readline() list = [] while lin ...

  7. MATLAB中批量从txt文件中读取指定行的数据保存为txt文件

    经常遇到,我们想要读取的数据,不是从头开始读取的,我们往往要求从固定的行开始读取,MATLAB代码如下: function readData() %从指定行开始读取数据 readFilePath='C ...

  8. PageOffice既保存Word文件中指定区域的数据又保存整篇文件

    一.首先在word文件中给需要在后台获取数据的区域设置以PO_开头的书签. 二.通过pageoffice在线打开文件并编辑保存.有两种打开文件的模式 1.普通编辑模式(docNormalEdit) 普 ...

  9. Unity3d 提取mesh并保存成asset文件

    Unity3d 提取mesh并保存成asset文件 应用背景 下载链接 使用步骤 贴出脚本 应用背景 有些FBX文件过大,但是只用其中的一个mesh时,我们可以考虑把这个mesh单独提取出来保存问as ...

最新文章

  1. aaynctask控制多个下载进度_AsyncTask用法解析-下载文件动态更新进度条
  2. hbase性能优化2
  3. Spring实战(第3版)
  4. boost::geometry模块Linestring相关的测试程序
  5. androdi 中listview.setTextFilterEnabled(boolean)的作用
  6. 保护IIS服务器的15个技巧
  7. RobotFramework Library
  8. python用户登录a_用Python实现用户登录接口
  9. 右下角出现测试模式 win7 内部版本7601如何去掉
  10. Centos 中如何快速定制二进制的内核 RPM 包
  11. 牛客小白月赛9: div.2 A(线性筛)
  12. 中文编程——函语言概念
  13. 鸿翼上线DMSTMS管理系统,助力智飞龙科马打造数字化质量安全生命线
  14. AD7606调试过程与源码
  15. laragon 之Nginx
  16. 基础30讲 第九讲 一元函数积分学的几何应用
  17. 音视频流媒体————基本概念
  18. 《新型智慧城市总体规划导则》发布
  19. IPv4下,划分子网,构造超网(CIDR)
  20. java全jit编译_Javac编译与JIT编译

热门文章

  1. hdu 1560 DNA sequence(迭代加深搜索)
  2. java 求最长子串
  3. 杭电Color the ball1556
  4. 以KNN为例用sklearn进行数据分析和预测
  5. [转载]国外物联网平台初探(二):微软Azure IoT
  6. Chapter 5. Expressions
  7. 重拾Javascript (四) KnockoutJs使用
  8. PowerDesigner的Table视图同时显示Code和Name的方法[转发]
  9. MOSS点滴(1):如何开发和部署feature
  10. Python学习笔记:常用第三方模块3