如何使用 LD_PRELOAD 覆盖 C++ 字符串类

逆向工程 C++ 函数挂钩 反编译 海湾合作委员会
2021-07-08 05:53:16

我正在尝试覆盖预定义的函数,例如 strcmp、getenv 等。我已经使用 LD_PRELOAD 选项覆盖了其中的一些函数。但我不能覆盖字符串类,我怎样才能覆盖 C++ 字符串类

我已经尝试过这样的事情;

class string{
    char* str;
public:
    string (char* str2){
        str = str2;
        std::cout << str2 << std::endl ;
    }
    bool operator==(char* str2)  {
        std::cout << "c++ => " << str << "==" << str2 << std::endl;
        return false; 
    }
    compare(char* str2)  {
        std::cout << "c++ => " << str << "==" << str2 << std::endl;
        return 0;
    }
};  

但它不起作用我无法打印比较甚至定义的字符串。

编译;

g++ -g -Wall -shared -fPIC -ldl -o bypasscpp.so bypass.cpp

并运行程序;

LD_PRELOAD="./bypasscpp.so" ./app
1个回答

你想覆盖std::string吗?请检查https://en.cppreference.com/w/cpp/string/basic_stringstd::string只是一个typedeffor std::basic_string<char>which 意味着std::string不是动态链接的。换句话说,您尝试覆盖的那些方法是静态链接到您的./app