-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobfusheader.h
428 lines (374 loc) · 12.2 KB
/
obfusheader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/* *
* Made by ac3ss0r @ https://github.com/ac3ss0r/obfusheader.h
* Licensed under Apache-2.0.
* */
#pragma once
// Settings (Just comment out whatever you need)
#define THREAD_LOCAL
// #define CFLOW
// #define FORCE_INLINE
// Without forceinline the compiler will mostly ignore inline methods
#ifdef FORCE_INLINE
#if defined(_MSC_VER)
#define INLINE __forceinline // Visual C++
#else
#define INLINE __attribute__((always_inline)) inline // GCC/G++/CLANG
#endif
#else
#define INLINE inline // Regular inline doesn't always inline
#endif
// __TIME__ && __COUNTER__ both used as a random provider (compile-time)
// 00:XX:XX -> __TIME__[3], __TIME__[4], __TIME__[6], __TIME__[7]
#define CTimeSeed ((__COUNTER__ ^ __TIME__[3]) * (__COUNTER__ ^ __TIME__[4]) +\
(__COUNTER__ ^ __TIME__[6]) * (__COUNTER__ ^ __TIME__[7]))
#define RND(Min, Max) (Min + (CTimeSeed % (Max - Min + 1)))
// Normal & threadlocal modes
#define OBF_KEY_NORMAL(x, type, size, key) []() {\
constexpr static auto data = obf::obfuscator<type, size, key>(x);\
return data;\
}()
#define OBF_KEY_THREADLOCAL(x, type, size, key) []() -> obf::decryptor<type, size, key>& {\
constexpr static auto data = obf::obfuscator<type, size, key>(x);\
thread_local auto decryptor = obf::decryptor<type, size, key>(data);\
return decryptor;\
}()
#define OBF_NORMAL(x) OBF_KEY_NORMAL(x, obf::clean_type<decltype(obf::gettype(x))>, obf::getsize(x), (char)RND(1, 255))
#define OBF_THREADLOCAL(x) OBF_KEY_THREADLOCAL(x, obf::clean_type<decltype(obf::gettype(x))>, obf::getsize(x), (char)RND(1, 255))
#ifdef THREAD_LOCAL
#define OBF(x) (meta::decay_t<decltype(x)>) OBF_THREADLOCAL(x)
#else
#define OBF(x) (meta::decay_t<decltype(x)>) OBF_NORMAL(x)
#endif
// Call hidding is different on windows and linux (symbol-based)
#if defined(__linux__) || defined(__ANDROID__)
#include <dlfcn.h>
#define OBFUSCALL(mtd, def) ((def)(dlsym(RTLD_DEFAULT, OBF(mtd))))
#elif _WIN32
#include <windows.h>
#define OBFUSCALL(lib, mtd, def) ((def)(GetProcAddress(LoadLibrary(OBF(lib)), OBF(mtd))))
#endif
// This was created so the header works without type_traits (on gcc and other compilers)
// It basically replicates type_traits, it might look scary just skip it
namespace meta {
template<class T, T v>
struct integral_constant {
static constexpr T value = v;
using value_type = T;
using type = integral_constant; // using injected-class-name
constexpr operator value_type() const noexcept { return value; }
constexpr value_type operator()() const noexcept { return value; } // since c++14
};
typedef integral_constant<bool, false> false_type;
typedef integral_constant<bool, true> true_type;
// primary template
template<class>
struct is_function : false_type {
};
// specialization for regular functions
template<class Ret, class... Args>
struct is_function<Ret(Args...)> : true_type {
};
// specialization for variadic functions such as std::printf
template<class Ret, class... Args>
struct is_function<Ret(Args..., ...)> : true_type {
};
// specialization for function types that have cv-qualifiers
template<class Ret, class... Args>
struct is_function<Ret(Args...) const> : true_type {
};
template<class Ret, class... Args>
struct is_function<Ret(Args...) volatile> : true_type {
};
template<class Ret, class... Args>
struct is_function<Ret(Args...) const volatile> : true_type {
};
template<class Ret, class... Args>
struct is_function<Ret(Args..., ...) const> : true_type {
};
template<class Ret, class... Args>
struct is_function<Ret(Args..., ...) volatile> : true_type {
};
template<class Ret, class... Args>
struct is_function<Ret(Args..., ...) const volatile> : true_type {
};
// specialization for function types that have ref-qualifiers
template<class Ret, class... Args>
struct is_function<Ret(Args...) &> : true_type {
};
template<class Ret, class... Args>
struct is_function<Ret(Args...) const &> : true_type {
};
template<class Ret, class... Args>
struct is_function<Ret(Args...) volatile &> : true_type {
};
template<class Ret, class... Args>
struct is_function<Ret(Args...) const volatile &> : true_type {
};
template<class Ret, class... Args>
struct is_function<Ret(Args..., ...) &> : true_type {
};
template<class Ret, class... Args>
struct is_function<Ret(Args..., ...) const &> : true_type {
};
template<class Ret, class... Args>
struct is_function<Ret(Args..., ...) volatile &> : true_type {
};
template<class Ret, class... Args>
struct is_function<Ret(Args..., ...) const volatile &> : true_type {
};
template<class Ret, class... Args>
struct is_function<Ret(Args...) &&> : true_type {
};
template<class Ret, class... Args>
struct is_function<Ret(Args...) const &&> : true_type {
};
template<class Ret, class... Args>
struct is_function<Ret(Args...) volatile &&> : true_type {
};
template<class Ret, class... Args>
struct is_function<Ret(Args...) const volatile &&> : true_type {
};
template<class Ret, class... Args>
struct is_function<Ret(Args..., ...) &&> : true_type {
};
template<class Ret, class... Args>
struct is_function<Ret(Args..., ...) const &&> : true_type {
};
template<class Ret, class... Args>
struct is_function<Ret(Args..., ...) volatile &&> : true_type {
};
template<class Ret, class... Args>
struct is_function<Ret(Args..., ...) const volatile &&> : true_type {
};
template<class T>
struct is_array : false_type {
};
template<class T>
struct is_array<T[]> : true_type {
};
template<class T, size_t N>
struct is_array<T[N]> : true_type {
};
template<class T>
struct remove_extent {
using type = T;
};
template<class T>
struct remove_extent<T[]> {
using type = T;
};
template<class T, size_t N>
struct remove_extent<T[N]> {
using type = T;
};
template<class T>
struct remove_reference {
typedef T type;
};
template<class T>
struct remove_reference<T &> {
typedef T type;
};
template<class T>
struct remove_reference<T &&> {
typedef T type;
};
template<class T>
struct remove_cv {
typedef T type;
};
template<class T>
struct remove_cv<const T> {
typedef T type;
};
template<class T>
struct remove_cv<volatile T> {
typedef T type;
};
template<class T>
struct remove_cv<const volatile T> {
typedef T type;
};
template<class T>
struct remove_const {
typedef T type;
};
template<class T>
struct remove_const<const T> {
typedef T type;
};
template<class T>
struct remove_volatile {
typedef T type;
};
template<class T>
struct remove_volatile<volatile T> {
typedef T type;
};
template<class T>
struct remove_all_extents {
typedef T type;
};
template<class T>
struct remove_all_extents<T[]> {
typedef typename remove_all_extents<T>::type type;
};
template<class T, size_t N>
struct remove_all_extents<T[N]> {
typedef typename remove_all_extents<T>::type type;
};
template<bool B, class T, class F>
struct conditional {
using type = T;
};
template<class T, class F>
struct conditional<false, T, F> {
using type = F;
};
template<class T>
struct type_identity {
using type = T;
}; // or use std::type_identity (since C++20)
template<class T>
auto try_add_pointer(int) -> type_identity<typename remove_reference<T>::type *>; // usual case
template<class T>
auto try_add_pointer(...) -> type_identity<T>; // unusual case (cannot form std::remove_reference<T>::type*)
template<class T>
struct add_pointer : decltype(try_add_pointer<T>(0)) {
};
// Helpers from C++14
template<class T>
using remove_cv_t = typename remove_cv<T>::type;
template<class T>
using remove_const_t = typename remove_const<T>::type;
template<class T>
using remove_volatile_t = typename remove_volatile<T>::type;
template<class T>
using remove_reference_t = typename remove_reference<T>::type;
template<class T>
using remove_all_extents_t = typename remove_all_extents<T>::type;
template<class T>
struct decay {
private:
typedef typename remove_reference<T>::type U;
public:
typedef typename conditional<
is_array<U>::value,
typename add_pointer<typename remove_extent<U>::type>::type,
typename conditional<
is_function<U>::value,
typename add_pointer<U>::type,
typename remove_cv<U>::type
>::type
>::type type;
};
template<class T>
using decay_t = typename decay<T>::type;
}
namespace obf {
template<class _Ty>
using clean_type = typename meta::remove_const_t<meta::remove_reference_t<_Ty>>;
template<typename T, T value>
static T ensure_threadlocal() {
thread_local T v = value;
return v;
}
template<typename T, T value>
static constexpr T ensure_constexpr() { return value; }
template<typename T, int size>
constexpr size_t getsize(const T(&)[size]) { return size; }
template<typename T>
constexpr size_t getsize(T) { return 1; }
template<typename T, size_t size>
constexpr static T gettype(const T(&)[size]);
template<typename T>
constexpr static T gettype(T);
// Decryption with control flow to confuse IDA/GHIDRA
template<class T, char key, size_t size>
INLINE void xord(T *data, int *stack, int *value) {
#ifdef CFLOW
for (int i = 0; i < size; i++) {
goto l_1;
l_increase:
*stack += 1; // -Wunused-value
l_1:
if (*stack == *value + 1) {
data[i] = data[i] ^ (*value + 1);
goto l_increase;
}
if (*stack == *value + 2) {
data[i] = data[i] ^ (*value + 2);
goto l_increase;
}
if (*stack == *value + 0) {
data[i] = data[i] ^ (key + i); // real
continue;
}
if (*stack == *value + 4) {
data[i] = data[i] ^ (*value + 3);
goto l_increase;
}
if (*stack == *value + 5) {
data[i] = data[i] ^ (*value + 4);
goto l_increase;
}
}
#else
for (int i = 0; i < size; i++)
data[i] = data[i] ^ (key + i); // no cflow
#endif
}
template<class T, size_t size, char key>
class obfuscator {
public:
INLINE constexpr obfuscator(const T *data) {
for (int i = 0; i < size; i++)
m_data[i] = data[i] ^ (key + i);
}
INLINE constexpr obfuscator(const T data) {
m_data[0] = data ^ key;
}
INLINE T *decrypt() {
if (!decrypted) {
xord<T, key, size>(m_data, &stack, &value);
}
decrypted = true;
return m_data;
}
INLINE operator T *() {
return decrypt();
}
INLINE operator T() {
return decrypt()[0];
}
int stack = 0, value = 0;
T result = NULL;
bool decrypted = false;
T m_data[size]{};
};
template<class T, size_t size, char key>
class decryptor {
public:
INLINE decryptor(const obfuscator<T, size, key> data) {
for (int i = 0; i < size; i++)
m_data[i] = data.m_data[i];
}
INLINE T *decrypt() {
if (!decrypted) {
xord<T, key, size>(m_data, &stack, &value);
}
decrypted = true;
return m_data;
}
INLINE operator T *() {
return decrypt();
}
INLINE operator T() {
return decrypt()[0];
}
int stack = 0, value = 0;
T result = NULL;
bool decrypted = false;
T m_data[size]{};
};
}