Advertisement
Guest User

Template

a guest
Mar 4th, 2014
939
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. //-------------------------------------------------------------//
  5. // "Malware related compile-time hacks with C++11" by LeFF //
  6. // You can use this code however you like, I just don't really //
  7. // give a shit, but if you feel some respect for me, please //
  8. // don't cut off this comment when copy-pasting... ;-) //
  9. //-------------------------------------------------------------//
  10.  
  11. // Usage examples:
  12. void exampleRandom1() __attribute__((noinline));
  13. void exampleRandom2() __attribute__((noinline));
  14. void exampleHashing() __attribute__((noinline));
  15. void exampleEncryption() __attribute__((noinline));
  16.  
  17. #ifndef vxCPLSEED
  18. // If you don't specify the seed for algorithms, the time when compilation
  19. // started will be used, seed actually changes the results of algorithms...
  20. #define vxCPLSEED ((__TIME__[7] - '0') * 1 + (__TIME__[6] - '0') * 10 + \
  21. (__TIME__[4] - '0') * 60 + (__TIME__[3] - '0') * 600 + \
  22. (__TIME__[1] - '0') * 3600 + (__TIME__[0] - '0') * 36000)
  23. #endif
  24.  
  25. // The constantify template is used to make sure that the result of constexpr
  26. // function will be computed at compile-time instead of run-time
  27. template <uint32_t Const> struct vxCplConstantify { enum { Value = Const }; };
  28.  
  29. // Compile-time mod of a linear congruential pseudorandom number generator,
  30. // the actual algorithm was taken from "Numerical Recipes" book
  31. constexpr uint32_t vxCplRandom(uint32_t Id)
  32. { return (1013904223 + 1664525 * ((Id > 0) ? (vxCplRandom(Id - 1)) : (vxCPLSEED))) & 0xFFFFFFFF; }
  33.  
  34. // Compile-time random macros, can be used to randomize execution
  35. // path for separate builds, or compile-time trash code generation
  36. #define vxRANDOM(Min, Max) (Min + (vxRAND() % (Max - Min + 1)))
  37. #define vxRAND() (vxCplConstantify<vxCplRandom(__COUNTER__ + 1)>::Value)
  38.  
  39. // Compile-time recursive mod of string hashing algorithm,
  40. // the actual algorithm was taken from Qt library (this
  41. // function isn't case sensitive due to vxCplTolower)
  42. constexpr char vxCplTolower(char Ch) { return (Ch >= 'A' && Ch <= 'Z') ? (Ch - 'A' + 'a') : (Ch); }
  43. constexpr uint32_t vxCplHashPart3(char Ch, uint32_t Hash) { return ((Hash << 4) + vxCplTolower(Ch)); }
  44. constexpr uint32_t vxCplHashPart2(char Ch, uint32_t Hash) { return (vxCplHashPart3(Ch, Hash) ^ ((vxCplHashPart3(Ch, Hash) & 0xF0000000) >> 23)); }
  45. constexpr uint32_t vxCplHashPart1(char Ch, uint32_t Hash) { return (vxCplHashPart2(Ch, Hash) & 0x0FFFFFFF); }
  46. constexpr uint32_t vxCplHash(const char* Str) { return (*Str) ? (vxCplHashPart1(*Str, vxCplHash(Str + 1))) : (0); }
  47.  
  48. // Compile-time hashing macro, hash values changes using the first pseudorandom number in sequence
  49. #define vxHASH(Str) (uint32_t)(vxCplConstantify<vxCplHash(Str)>::Value ^ vxCplConstantify<vxCplRandom(1)>::Value)
  50.  
  51. // Compile-time generator for list of indexes (0, 1, 2, ...)
  52. template <uint32_t...> struct vxCplIndexList {};
  53. template <typename IndexList, uint32_t Right> struct vxCplAppend;
  54. template <uint32_t... Left, uint32_t Right> struct vxCplAppend<vxCplIndexList<Left...>, Right> { typedef vxCplIndexList<Left..., Right> Result; };
  55. template <uint32_t N> struct vxCplIndexes { typedef typename vxCplAppend<typename vxCplIndexes<N - 1>::Result, N - 1>::Result Result; };
  56. template <> struct vxCplIndexes<0> { typedef vxCplIndexList<> Result; };
  57.  
  58. // Compile-time string encryption of a single character
  59. const char vxCplEncryptCharKey = vxRANDOM(0, 0xFF);
  60. constexpr char vxCplEncryptChar(const char Ch, uint32_t Idx) { return Ch ^ (vxCplEncryptCharKey + Idx); }
  61.  
  62. // Compile-time string encryption class
  63. template <typename IndexList> struct vxCplEncryptedString;
  64. template <uint32_t... Idx> struct vxCplEncryptedString<vxCplIndexList<Idx...> >
  65. {
  66. char Value[sizeof...(Idx) + 1]; // Buffer for a string
  67.  
  68. // Compile-time constructor
  69. constexpr inline vxCplEncryptedString(const char* const Str)
  70. : Value({ vxCplEncryptChar(Str[Idx], Idx)... }) {}
  71.  
  72. // Run-time decryption
  73. char* decrypt()
  74. {
  75. for(uint32_t t = 0; t < sizeof...(Idx); t++)
  76. { this->Value[t] = this->Value[t] ^ (vxCplEncryptCharKey + t); }
  77. this->Value[sizeof...(Idx)] = '\0'; return this->Value;
  78. }
  79. };
  80.  
  81. // Compile-time string encryption macro
  82. #define vxENCRYPT(Str) (vxCplEncryptedString<vxCplIndexes<sizeof(Str) - 1>::Result>(Str).decrypt())
  83.  
  84. // A small random code path example
  85. void exampleRandom1()
  86. {
  87. switch(vxRANDOM(1, 4))
  88. {
  89. case 1: { printf("exampleRandom1: Code path 1!\n"); break; }
  90. case 2: { printf("exampleRandom1: Code path 2!\n"); break; }
  91. case 3: { printf("exampleRandom1: Code path 3!\n"); break; }
  92. case 4: { printf("exampleRandom1: Code path 4!\n"); break; }
  93. default: { printf("Fucking poltergeist!\n"); }
  94. }
  95. }
  96.  
  97. // A small random code generator example
  98. void exampleRandom2()
  99. {
  100. volatile uint32_t RndVal = vxRANDOM(0, 100);
  101. if(vxRAND() % 2) { RndVal += vxRANDOM(0, 100); }
  102. else { RndVal -= vxRANDOM(0, 200); }
  103. printf("exampleRandom2: %d\n", RndVal);
  104. }
  105.  
  106. // A small string hasing example
  107. void exampleHashing()
  108. {
  109. printf("exampleHashing: 0x%08X\n", vxHASH("hello world!"));
  110. printf("exampleHashing: 0x%08X\n", vxHASH("HELLO WORLD!"));
  111. }
  112.  
  113. void exampleEncryption()
  114. {
  115. printf("exampleEncryption: %s\n", vxENCRYPT("Hello world!"));
  116. }
  117.  
  118. extern "C" void Main()
  119. {
  120. exampleRandom1();
  121. exampleRandom2();
  122. exampleHashing();
  123. exampleEncryption();
  124. }
  125.  
  126.  
  127. To build code with GCC/MinGW:
  128. g++ -o main.exe -m32 -std=c++0x -Wall -O3 -Os -fno-exceptions -fno-rtti -flto -masm=intel -e_Main -nostdlib -O3 -Os -flto -s main.cpp -lmsvcrt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement