2011-10-24 8 views
2

私のコードで難読化したい文字列があるとします。 (この例は学習のためのものです)Cマクロプリビルドプリプロセッサを作成するには?

私は文字列リテラルをマクロで囲むことを予定しています。 _MY_ENCRYPTION_MACROのすべての用途に調べて、文字列を難読化するために自分のプリプロセッサを通じて私のソースファイルを実行するための

#define MY_STRING "lol" 
const char *get_string() { return _MY_ENCRYPTION_MACRO(MY_STRING); } 

と、ビルド前の段階として、。

Visual C++でこの前処理を実行するにはどうすればよいですか?

+0

チェックアウトBoost.Waveというファイルを作成します。 –

+1

Plesae参照http://stackoverflow.com/questions/1356896/how-to-hide-a-string-in-binary-code and http://stackoverflow.com/questions/4102320/c-how-to-encrypt -strings-at-compile-time –

+0

@MitchWheat:これらのリンクは本当に答えを提供しません。 – Mehrdad

答えて

0

Linuxで最近GCC(GCC 4.6)を使用した場合、コンパイル時の文字列を「暗号化」する組み込み関数を提供するプラグインを使用することもできます。また、GCC MELT拡張子にすることもできますGCCを拡張するための高水準ドメイン特有の言語)。

他のC++を使用している場合は、マクロを見つけるための独自の前処理スクリプトがあるとします。 たとえば、すべてのC++ソースですべての出現をスキャンして​​のプログラムを作成し、#include "mycrypt.h"というC++コードのmycrypt.hファイルを生成することができます。そして、あなたは

#define ENCRYPTSTRING(S) ENCRPYTSTRING_AT(S,__LINE__) 
#define ENCRYPTSTRING_AT(S,L) cryptstring_#L 

のようなトリックを行う可能性がありますし、あなたの"mycrypt.h"を生成した"mycrypt.h"発電機がawkまたはpythonまたはocaml(など)をスクリプトすることができなど

const char crypstring_123[]="thecryptedstringatline123"; 

のようなものが含まれています。

1

私はこのような問題を抱えている人が多いため、この回答を掲載しました。これは不可能だと思っていましたが、非常に簡単ですが、後でビルドされたファイルを読み込み、文字列をスキャンしてそのような文字列を暗号化しますが、これは悪くはありませんでしたが、Visual Studioからコンパイルされたパッケージが必要でした。

何が必要(箱から出してアップデート1のVisual Studio 2015)C++ 11ある

魔法は魔法でconstexpr

この#define

#define XorString(String) (CXorString<ConstructIndexList<sizeof(String) - 1>::Result>(String).decrypt()) 

で起こるこの新しいコマンドで起こること実行時にのみコンパイル時にXorStringを復号化しませんが、文字列は実行可能ファイルには表示されません。

printf(XorString("this string is hidden!")); 

それは"this string is hidden!"をプリントアウトしますが、文字列として実行可能ファイル内でそれを見つけることができません!Microsoft Sysinternals Stringsプログラムのダウンロードリンクとそれを自分でチェック:https://technet.microsoft.com/en-us/sysinternals/strings.aspx

完全なソースコードは非常に大きいですが、簡単かもしれません1つのヘッダーファイルに含まれています。しかし、非常にランダムなので、暗号化された文字列出力は常に新しいコンパイルごとに変更されます。シードは、コンパイルに要した時間に基づいて変更されています。

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()) 
//////////////////////////////////////////////////////////////////// 
+0

それはvs15アップデート3で動作しません。 私は次のコードを使用しました: printf(XorString( "test")); しかし、文字列app(sysinternals)には "test"文字列が表示されます。 – AK87

+0

最適化モードを試してください。 (-O) – SSpoke

関連する問題