试图在exe文件中输出所有内容

Trying to output everything inside an exe file

本文关键字:输出 exe 文件      更新时间:2023-10-16

我试图输出这个。exe文件的明文内容。它有明文的东西,比如"以这种方式改变代码不会影响最终优化代码的质量",所有这些东西都被微软放在。exe文件中。当我运行下面的代码时,我得到M Z E的输出,后面跟着一个红心和一个菱形。我做错了什么?

ifstream file;
char inputCharacter;    
file.open("test.exe", ios::binary);
while ((inputCharacter = file.get()) != EOF)
{   
    cout << inputCharacter << "n";     
}

file.close();

我会使用std::isprint之类的东西来确保字符是可打印的,而不是在打印之前使用一些奇怪的控制代码。

像这样:

#include <cctype>
#include <fstream>
#include <iostream>
int main()
{
    std::ifstream file("test.exe", std::ios::binary);
    char c;
    while(file.get(c)) // don't loop on EOF
    {
        if(std::isprint(c)) // check if is printable
            std::cout << c;
    }
}

您已经以二进制格式打开了流,这对于预期的目的是很好的。但是,您可以按原样打印每个二进制数据:其中一些字符不可打印,从而产生奇怪的输出。

可能的解决方案:

如果你想打印一个exe文件的内容,你会得到更多的不可打印字符而不是可打印字符。因此,一种方法是打印十六进制值:

while ( file.get(inputCharacter ) )
{   
    cout << setw(2) << setfill('0') << hex << (int)(inputCharacter&0xff) << "n";     
}

或者您可以使用调试器方法来显示十六进制值,然后在可打印或'时显示char。

while (file.get(inputCharacter)) {
    cout << setw(2) << setfill('0') << hex << (int)(inputCharacter&0xff)<<" ";
    if (isprint(inputCharacter & 0xff))
        cout << inputCharacter << "n";
    else cout << ".n";
}

嗯,为了人机工程学的缘故,如果exe文件包含任何真正的exe,你最好选择在每行显示几个字符;-)

二进制文件是字节的集合。字节的取值范围是0..255。可以安全地"打印"的可打印字符的范围要窄得多。假设最基本的ASCII编码

    32 . . 63
  • 64 . . 95
  • 96 . . 126
  • 加上,如果你的代码页有
  • ,可能会比128高一些

参见ASCII表

超出该范围的每个字符至少可以:

  • 打印为不可见
  • 打印成一些奇怪的垃圾
  • 实际上是一个控制字符,它将改变终端的设置

一些终端支持"end of text"字符,并在此之后停止打印任何文本。也许你击中了。

我想说,如果你只对文本感兴趣,那么只打印可打印的,忽略其他的。或者,如果你想要所有的东西,那就把它们写成十六进制吧?

成功了:

ifstream file;
char inputCharacter;
string Result;
file.open("test.exe", ios::binary);
while (file.get(inputCharacter))
{       
    if ((inputCharacter > 31) && (inputCharacter < 127))
        Result += inputCharacter;       
}
cout << Result << endl;
cout << "These are the ascii characters in the exe file" << endl;
file.close();