吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1521|回复: 6
收起左侧

[原创] 新手学习pe文件二进制dump

[复制链接]
52_pojie_52 发表于 2023-9-25 19:40
本帖最后由 52_pojie_52 于 2024-4-27 10:47 编辑

最近学习pe文件格式,感觉比较难记忆,用C++写几个小程序帮助理解

pe dump程序的代码如下:

#include <iostream>
#include <bitset>
#include <cstddef>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;

int main(int argc, char* argv[]) {
        /*--------------- read args --------------------*/
        if (2!=argc) {
                cout<<"please input one file name"<<endl;
                exit(1);
        }
        /*--------------- open file --------------------*/
        char* filename=argv[1];
        ifstream infile(filename, ios::binary | ios::in);
        if (! infile) {
                cerr << "open failed" << endl;
                exit(1);
        }

        /*--------------- read file --------------------*/
        // get length of file
        infile.seekg(0,infile.end); // seek to end of stream
        int fileLen=infile.tellg(); // length of stream
        infile.seekg(0,infile.beg); // need go back the begin of stream or read error

        unsigned char * bin = new unsigned char[fileLen];
        infile.read((char*)bin, fileLen);
        infile.close();
        if (!infile.good()) {
                cout<<"error occurred at read time"<<endl;
                exit(1);
        }

        /*--------------- dump file --------------------*/
        // open file
        ofstream outfile;
        outfile.open("pedump.txt", ios::out | ios.trunc);

        // print title
        outfile << hex << setiosflags(ios::uppercase); // set hex base,just need set only once at the begin
        outfile << setfill('0'); // fill with 0
        outfile << "Address   ";
        int i=0;
        for (i=0; i<0x10; i++) {
                outfile << setfill('0') << setw(2) << i << " ";
        }
        outfile << "   " << "ASCII"<<endl;

        // dump data
        int row = 0, rowEnd=ceil((float)fileLen/0x10);
        for (row=0; row < rowEnd; row++) {
                // 1. print addr
                int col=0;
                outfile << setw(8) << row * 0x10 << "  ";

                // 2. dump byte data
                for (col=0; col<0x10; col++) {
                        if ((row * 0x10 + col)<fileLen) {
                                outfile << setw(2) << (int)bin[row * 0x10 + col];
                                if (7==col) {
                                        outfile << '-';
                                } else {
                                        outfile << ' ';
                                }
                        } else {
                                outfile << "   ";
                        }
                }
                outfile << "   ";

                // 3. print ascii
                char c;
                for (col=0; col<0x10; col++) {
                        if ((row * 0x10 + col)<fileLen) {
                                c=bin[row*0x10+col];
                                // dont print 0x00-0x1f
                                if ((int)c>=0x20) {
                                        outfile << c;
                                } else {
                                        outfile<<'.';
                                }
                        } else {
                                outfile << ' ';
                        }

                        if (7==col) {
                                outfile<<' ';
                        }
                }
                outfile<<endl;
        }

        outfile.close();
        return 0;
}

主要代码说明都在注释里了,主要思路就是:

  1. 输入参数(文件名)
  2. 打开pe文件
  3. 读取文件内容到文件流
  4. 打开要输出的文件
  5. 第一行打印“Address”、00-0F、”ASCII“几个title
  6. 后面按一行16个字节来打印:左侧一栏是address、中间是16个字节(转成了16进制大写),右侧是对应的ASCII码(0x00-0x1F的ASCII码是控制字符,所以都改成了'.')

免费评分

参与人数 4吾爱币 +10 热心值 +4 收起 理由
笙若 + 1 + 1 谢谢@Thanks!
Hmily + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
yx69 + 1 + 1 我很赞同!
helian147 + 1 + 1 热心回复!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

yangfan567 发表于 2023-10-1 11:24
学习了,谢谢分享
Andy8633 发表于 2023-10-10 11:59
我看bilibili视频里他们用的吾爱破解的软件在哪里
XMHANGO 发表于 2023-10-11 22:52
 楼主| 52_pojie_52 发表于 2023-10-12 20:21
Andy8633 发表于 2023-10-10 11:59
我看bilibili视频里他们用的吾爱破解的软件在哪里

你说的是什么视频?论坛里有一整套工具包,你可以在这里找需要的软件https://down.52pojie.cn/Tools/
ok378 发表于 2023-10-16 21:45
谢谢分享
hua111 发表于 2023-11-5 12:02
谢谢分享谢谢分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-28 04:26

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表