编译时字符串加密

Lis*_*ing 13 c++ encryption macros reverse-engineering

我不希望逆向工程师在我的应用程序中读取硬编码字符串的纯文本.对此的简单解决方案是使用简单的XOR加密.问题是我需要一个转换器,在我的应用程序中它将如下所示:

//Before (unsecure)
char * cString = "Helllo Stackoverflow!";
//After (secure)
char * cString = XStr( 0x06, 0x15, 0x9D, 0xD5FBF3CC, 0xCDCD83F7, 0xD1C7C4C3, 0xC6DCCEDE, 0xCBC2C0C7, 0x90000000 ).c();
Run Code Online (Sandbox Code Playgroud)

是否有可能通过使用某些结构来维护干净的代码

//Before (unsecure)
char * cString = "Helllo Stackoverflow!";
//After (secure)
char * cString = CRYPT("Helllo Stackoverflow!");
Run Code Online (Sandbox Code Playgroud)

它也适用于很长的字符串(1000个字符?:-)).先感谢您

SSp*_*oke 10

这里确实存在完美的解决方案.

我也认为这是不可能的,尽管它非常简单,人们编写解决方案,你需要一个自定义工具来扫描构建的文件然后扫描字符串并加密字符串,这是不错但我想要一个从Visual Studio编译的包,现在可能!

您需要的是C++ 11(Visual Studio 2015 Update 1开箱即用)

这个新命令会发生魔力 constexpr

通过魔法发生在这 #define

#define XorString( String ) ( CXorString<ConstructIndexList<sizeof( String ) - 1>::Result>( String ).decrypt() )
Run Code Online (Sandbox Code Playgroud)

它不会在编译时解密XorString,仅在运行时解密,但它只会在编译时加密字符串,因此字符串不会出现在可执行文件中

printf(XorString( "this string is hidden!" ));
Run Code Online (Sandbox Code Playgroud)

它将打印出来,"this string is hidden!"但你不会在可执行文件中找到它作为字符串!,请使用Microsoft Sysinternals Strings程序下载链接自行检查:https://technet.microsoft.com/en-us/sysinternals/strings.aspx

完整的源代码非常大,但可以很容易地包含在一个头文件中.但也非常随机,因此加密的字符串输出将始终改变每个新的编译,种子根据编译时间而改变,非常可靠,完美的解决方案.

创建一个名为的文件 XorString.h

#pragma once

//-------------------------------------------------------------//
// "Malware related compile-time hacks with C++11" by LeFF   //
// You can use this code however you like, I just don't really //
// give a shit, but if you feel some respect for me, please //
// don't cut off this comment when copy-pasting... ;-)       //
//-------------------------------------------------------------//

////////////////////////////////////////////////////////////////////
template <int X> struct EnsureCompileTime {
    enum : int {
        Value = X
    };
};
////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////
//Use Compile-Time as seed
#define Seed ((__TIME__[7] - '0') * 1  + (__TIME__[6] - '0') * 10  + \
              (__TIME__[4] - '0') * 60   + (__TIME__[3] - '0') * 600 + \
              (__TIME__[1] - '0') * 3600 + (__TIME__[0] - '0') * 36000)
////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////
constexpr int LinearCongruentGenerator(int Rounds) {
    return 1013904223 + 1664525 * ((Rounds> 0) ? LinearCongruentGenerator(Rounds - 1) : Seed & 0xFFFFFFFF);
}
#define Random() EnsureCompileTime<LinearCongruentGenerator(10)>::Value //10 Rounds
#define RandomNumber(Min, Max) (Min + (Random() % (Max - Min + 1)))
////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////
template <int... Pack> struct IndexList {};
////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////
template <typename IndexList, int Right> struct Append;
template <int... Left, int Right> struct Append<IndexList<Left...>, Right> {
    typedef IndexList<Left..., Right> Result;
};
////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////
template <int N> struct ConstructIndexList {
    typedef typename Append<typename ConstructIndexList<N - 1>::Result, N - 1>::Result Result;
};
template <> struct ConstructIndexList<0> {
    typedef IndexList<> Result;
};
////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////
const char XORKEY = static_cast<char>(RandomNumber(0, 0xFF));
constexpr char EncryptCharacter(const char Character, int Index) {
    return Character ^ (XORKEY + Index);
}

template <typename IndexList> class CXorString;
template <int... Index> class CXorString<IndexList<Index...> > {
private:
    char Value[sizeof...(Index) + 1];
public:
    constexpr CXorString(const char* const String)
    : Value{ EncryptCharacter(String[Index], Index)... } {}

    char* decrypt() {
        for(int t = 0; t < sizeof...(Index); t++) {
            Value[t] = Value[t] ^ (XORKEY + t);
        }
        Value[sizeof...(Index)] = '\0';
        return Value;
    }

    char* get() {
        return Value;
    }
};
#define XorS(X, String) CXorString<ConstructIndexList<sizeof(String)-1>::Result> X(String)
#define XorString( String ) ( CXorString<ConstructIndexList<sizeof( String ) - 1>::Result>( String ).decrypt() )
////////////////////////////////////////////////////////////////////
Run Code Online (Sandbox Code Playgroud)

  • 不能保证该字符串将被“加密”。编译器可能会在运行时加密它,或者删除这个 encrypt().decrypt() 因为它实际上是一个空操作。 (2认同)

Con*_*ius 6

这个博客提供了一个在 C++ 中编译时字符串散列的解决方案。我想原理是一样的。不幸的是,您必须为每个字符串长度创建一个 Makro。

  • @Listing:在 C++11 和 C++14 中,使用 `constexpr` 方法将允许您使用常规方法来实现此目的;他们可以做的事情有一些限制,但是加密(没有随机值)应该在他们的能力范围内。 (2认同)

Mat*_* M. 6

我的首选解决方案

// some header
extern char const* const MyString;

// some generated source
char const* const MyString = "aioghaiogeubeisbnuvs";
Run Code Online (Sandbox Code Playgroud)

然后使用您喜欢的脚本语言生成这个存储"加密"资源的源文件.

  • +1:这也允许比 XOR 更复杂的加密方法。 (3认同)

kar*_*son 5

这是一个迟到的答案,但我确信有更好的解决方案。

请参阅此处已接受的答案。

基本上,它展示了如何使用ADVobfuscator库来混淆字符串,简单如下:

#include "MetaString.h"

using namespace std;
using namespace andrivet::ADVobfuscator;

void Example()
{
    /* Example 1 */

    // here, the string is compiled in an obfuscated form, and
    // it's only deobfuscated at runtime, at the very moment of its use
    cout << OBFUSCATED("Now you see me") << endl;

    /* Example 2 */

    // here, we store the obfuscated string into an object to
    // deobfuscate whenever we need to
    auto narrator = DEF_OBFUSCATED("Tyler Durden");

    // note: although the function is named `decrypt()`, it's still deobfuscation
    cout << narrator.decrypt() << endl;
}
Run Code Online (Sandbox Code Playgroud)