andrivet / ADVobfuscator

Obfuscation library based on C++11/14 and metaprogramming
1.39k stars 238 forks source link

MetaString<1, K, Indexes<I...>> doesn't obfuscate string at compile-time but runtime #23

Open macvip opened 6 years ago

macvip commented 6 years ago

Using MetaString<1, K, Indexes<I...>>, compile with VS 2017 ver 15.7.1, disassemble and you'll find constexpr not working as supposed. As a result, the strings are obfuscated at runtime. For some unknown reason, the compiler regards the position I as a variant not a const value though it can be and indeed is produced at compile-time.

After several tries, an ugly template function fools the compiler to obfuscate string at compile-time.

template<char K, int... I>
  struct MetaString<1, K, Indexes<I...>>
  {
    // Constructor. Evaluated at compile time. Instantiate template function **encrypt**.
    constexpr ALWAYS_INLINE MetaString(const char* str)
    : key_(K), buffer_ {encrypt<K+I>(str[I])...} { }
    /* untouched  inline const char* decrypt() */
    private:
        // ......
        // decrypt and encrypt2 are evaluated at runtime.
        constexpr char  encrypt2(char c, size_t position) const { return c ^ key(position); }
    constexpr char decrypt(char c, size_t position) const { return encrypt2(c, position); }
        // **encrypt** will be instantiated at compile time in the constructor of this MetaString.
    template<char _k>
    constexpr char encrypt(char c) const { return c ^ _k; }
        /* key_ and buffer_ */
};
andrivet commented 6 years ago

My code definitely needs improvements for the latest versions of compilers

MeroZemory commented 4 years ago

I had the same symptoms, so I thought this library actually does not support compile time obfuscation. Thanks for letting me know that this is a compiler dependent issue.

andrivet commented 4 years ago

@MeroZemory It is dependent of the compiler you are using, its version and the compilation flags. (It was written more than 3 years...)

MeroZemory commented 4 years ago

@andrivet Thank you for writing such a library. I was exploring instruction-level obfuscation techniques so this isn't related to what I was looking for, but it was great to get the first idea of running FSM based on predicates :)

AlexanderQueen commented 4 years ago

@MeroZemory It is dependent of the compiler you are using, its version and the compilation flags. (It was written more than 3 years...)

Can you addapt it for VS 2019?

andrivet commented 4 years ago

I will update this code once we have good support of C++20 in the major compilers (VS, Clang and GCC). This is currently not the case. More specifically, what I am waiting for is "string literal operator template" support.

AlexanderQueen commented 4 years ago

I will update this code once we have good support of C++20 in the major compilers (VS, Clang and GCC). This is currently not the case. More specifically, what I am waiting for is "string literal operator template" support.

Thx for faster than light answer. So, VS 20 would got major updates?

andrivet commented 4 years ago

VS 20 does not exist so I can't answer.

codecat commented 3 years ago

Visual Studio 2022 came out today, which claims to have proper C++20 support. Is a MetaString rewrite still planned? 👼

andrivet commented 3 years ago

Yes (when ? when I can find some time)