C++实现代码中的字符串不应存在于输出二进制文件中.如何解决
strings in C++ implementation code should not be present in output binary. How to fix
我没有使用 -g 编译器选项,但我仍然在我的可执行文件中很容易看到字符串。 例如,通过使用字符串程序。
这是我的代码:
测试.hpp:
#ifndef TEST_HPP_
#define TEST_HPP_
#include <string>
namespace ns1 {
class Test
{
public:
std::string Get(const std::string& root);
private:
void append_other_stuff(std::string& s);
};
} // namespace ns1
#endif // TEST_HPP_
测试.cpp:
#include "test.hpp"
static const char* privatePart = "_hideme_";
namespace ns1 {
std::string Test::Get(const std::string& root) {
std::string result = root + "_fixed_" + privatePart;
append_other_stuff(result);
return result;
}
void Test::append_other_stuff(std::string& s) {
// all these string must be hidden
static char middle1[] = {'s','e','c','r', 'e','t','1',' '};
static char middle2[] = {'s','e','c','r', 'e','t','2',' '};
static char endbit[] = {'s','e','c','r', 'e','t','3',' '};
s += middle1;
s += middle2;
s += endbit;
}
}
主.cpp:
#include "test.hpp"
#include <iostream>
using namespace std;
int main() {
ns1::Test t1;
cout << t1.Get("123") << endl;
}
制作文件:
CXX = g++
CXXFLAGS = -Wall -std=c++11
main.o: main.cpp
$(CXX) $(CXXFLAGS) -c main.cpp
test.o: test.cpp test.hpp
$(CXX) $(CXXFLAGS) -c test.cpp
prog: main.o test.o
$(CXX) $(CXXFLAGS) main.o test.o -o prog
使用字符串的输出(缩短(:
字符串程序
_hideme_
_fixed_
;*3$"
zPLR
secret1
secret2
secret3
即使我运行 strip 命令:
条带 -s -g 程序
我要隐藏的字符串仍在程序中。
如何隐藏这些字符串?
为了跟进上面的评论,无法使用任何标准编译器或链接器工具隐藏这些字符串。 您必须自己实现这一点。
你没有说你为什么要这样做,所以我犹豫是否提供如何做的建议。 请注意,任何拥有您的程序的人都可以自己解码此字符串,如果他们愿意,无论您经历了多少混淆:您只会隐藏字符串以防止随意调查。 没有办法安全地在程序中保存机密,除非它需要一些外部输入来解码。 因此,您绝对不应该使用这些方法在您的程序中存储密码或任何其他类型的秘密。
有了这个警告,如果这是你唯一的目标,有一些简单的方法可以防止通过strings
显示内容:例如,您可以将字符串存储在一个静态的整数数组中,每个字符一个,然后在运行时将其转换回字符串。
相关文章:
- 读取二进制文件时如何输出?
- C++实现代码中的字符串不应存在于输出二进制文件中.如何解决
- 在C 中输出到二进制文件中的麻烦
- 如何获取与 objdump 输出的标签相对应的 ELF 二进制文件中的文件偏移量
- 如何在 c++ 中使用二进制文件输入/输出读取/写入结构的字符串类型成员
- 将对象(指针)在地图中输出到二进制文件
- 如何将std::映射输出到二进制文件
- 读取并输出二进制文件(C++)中位置的字节
- 在线C++编译器:为我输出二进制文件
- 正在读取C++中的二进制文件,并将结果输出为hexdump
- 如何在 C++ 中使用更改光标输入和输出二进制文件
- C ++如何以二进制格式输出Ply文件
- 如何将整数和双精度输出到二进制文件
- 二进制文件中结构的输出返回垃圾
- 用C++将二进制字符串输出到二进制文件
- 自动使输出二进制文件大于命令行编译的二进制文件
- 字节输出到二进制文件c++
- 输出一个写在二进制文件c++中的double
- 从turbo c++中的二进制文件读取后出现意外输出
- 有没有一种方法可以在输出二进制文件中存储clang编译时标志