My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
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
#ifndef _Protect_h_
#define _Protect_h_

#include <Core/Core.h>
#include <Cypher/Cypher.h>

using namespace Upp;

#define PROTECT_START_MARKER "\xba\xad\xde\xad\xfa\xce"
#define PROTECT_END_MARKER "\xc0\xde\xde\xad\xfe\xed"

#define OBFUSCATE_START_MARKER "\xba\xad\xde\xad\xfa\xde"
#define OBFUSCATE_END_MARKER "\xc0\xde\xde\xad\xfe\xde"

#ifdef flagPROTECT

#ifdef PLATFORM_POSIX

#ifdef flagMT
#define PROTECT_START_FUNC1(decrFunc) \
static volatile bool __decrypted = false; \
INTERLOCKED \
{ \
if(!__decrypted) \
{ \
PROTECT_WRITE_ACCESS((byte *)&&__start, (byte *)&&__end - (byte *)&&__start, true); \
decrFunc((byte *)&&__start, (byte *)&&__end - (byte *)&&__start, (byte *)&&__init + 2, 6 /* sizeof(PROTECT_START_MARK) */); \
PROTECT_WRITE_ACCESS((byte *)&&__start, (byte *)&&__end - (byte *)&&__start, false); \
__decrypted = true; \
asm volatile ( \
"\txor %%eax, %%eax\n" \
"\tcpuid\n" \
: \
: \
: "eax", "ebx", "ecx", "edx" \
); \
} \
}

/* THIS BECAUSE THEIDE HANGS... */
#define PROTECT_START_FUNC2 \
if(__decrypted) \
{ \
if(!__decrypted) \
{ \
__init: \
asm volatile( \
"\tjmp 1f\n" \
"\t.ascii \""PROTECT_START_MARKER"\"\n" \
"1:\n" \
); \
} \
__start:

#define PROTECT_START_FUNC(DecrFunc) PROTECT_START_FUNC1(DecrFunc) PROTECT_START_FUNC2
#else
#define PROTECT_START_FUNC1(decrFunc) \
static volatile bool __decrypted = false; \
if(!__decrypted) \
{ \
PROTECT_WRITE_ACCESS((byte *)&&__start, (byte *)&&__end - (byte *)&&__start, true); \
decrFunc((byte *)&&__start, (byte *)&&__end - (byte *)&&__start, (byte *)&&__init + 2, 6 /* sizeof(PROTECT_START_MARK) */); \
PROTECT_WRITE_ACCESS((byte *)&&__start, (byte *)&&__end - (byte *)&&__start, false); \
__decrypted = true; \
asm volatile ( \
"\txor %%eax, %%eax\n" \
"\tcpuid\n" \
: \
: \
: "eax", "ebx", "ecx", "edx" \
); \
}

/* THIS BECAUSE THEIDE HANGS... */
#define PROTECT_START_FUNC2 \
if(__decrypted) \
{ \
if(!__decrypted) \
{ \
__init: \
asm volatile( \
"\tjmp 1f\n" \
"\t.ascii \""PROTECT_START_MARKER"\"\n" \
"1:\n" \
); \
} \
__start:

#define PROTECT_START_FUNC(DecrFunc) PROTECT_START_FUNC1(DecrFunc) PROTECT_START_FUNC2
#endif

#define PROTECT_END_FUNC \
} \
else \
{ \
__end: \
asm volatile ( \
"\tjmp 2f\n" \
"\t.ascii \""PROTECT_END_MARKER"\"\n" \
"2:\n" \
::: \
); \
goto __start; \
}

#define OBFUSCATE_START_FUNC \
static volatile bool __xxx = false; \
PROTECT_OBFUSCATE((byte *)&&__start, (byte *)&&__end - (byte *)&&__start + 2, (byte *)&&__init + 2, 16 /* sizeof(OBFUSCATE_START_MARKER) + sizeof("0123456789") */); \
asm volatile ( \
"\txor %%eax, %%eax\n" \
"\tcpuid\n" \
: \
: \
: "eax", "ebx", "ecx", "edx" \
); \
__xxx = true; \
if(__xxx) \
{ \
__init: \
asm __volatile__( \
"\tjmp 1f\n" \
"\t.ascii \""OBFUSCATE_START_MARKER"\"\n" \
"\t.ascii \"0123456789\"\n" \
"1:\n" \
); \
} \
__start: \
if(__xxx) \
__xxx++;

#define OBFUSCATE_END_FUNC \
if(__xxx) \
__xxx++; \
__end: \
asm __volatile__( \
"\tjmp 1f\n" \
"\t.ascii \""OBFUSCATE_END_MARKER"\"\n" \
"1:\n" \
); \
PROTECT_OBFUSCATE((byte *)&&__start, (byte *)&&__end - (byte *)&&__start + 2, (byte *)&&__init + 2, 16 /* sizeof(OBFUSCATE_START_MARKER) + sizeof("0123456789") */);

#else

#define _PROTECT_START_MARKER __asm _emit 0xba __asm _emit 0xad __asm _emit 0xde __asm _emit 0xad __asm _emit 0xfa __asm _emit 0xce
#define _PROTECT_END_MARKER __asm _emit 0xc0 __asm _emit 0xde __asm _emit 0xde __asm _emit 0xad __asm _emit 0xfe __asm _emit 0xed

#define _OBFUSCATE_START_MARKER __asm _emit 0xba __asm _emit 0xad __asm _emit 0xde __asm _emit 0xad __asm _emit 0xfa __asm _emit 0xde
#define _OBFUSCATE_END_MARKER __asm _emit 0xc0 __asm _emit 0xde __asm _emit 0xde __asm _emit 0xad __asm _emit 0xfe __asm _emit 0xde
#define _OBFUSCATE_KEYPLACER __asm _emit 0x00 __asm _emit 0x01 __asm _emit 0x02 __asm _emit 0x03 __asm _emit 0x04 __asm _emit 0x05 __asm _emit 0x06 __asm _emit 0x07 __asm _emit 0x08 __asm _emit 0x09

#ifdef flagMT
#define PROTECT_START_FUNC(decrFunc) \
static volatile bool __decrypted = false; \
INTERLOCKED \
{ \
byte *__startPtr, *__endPtr, *__noncePtr; \
if(!__decrypted) \
{ \
__asm \
{ \
__asm push eax \
__asm lea eax, __start \
__asm mov __startPtr, eax \
__asm lea eax, __end \
__asm mov __endPtr, eax \
__asm lea eax, __init \
__asm inc eax \
__asm inc eax \
__asm mov __noncePtr, eax \
__asm pop eax \
}; \
PROTECT_WRITE_ACCESS(__startPtr, __endPtr - __startPtr, true); \
decrFunc(__startPtr, __endPtr - __startPtr, __noncePtr, 6); \
PROTECT_WRITE_ACCESS(__startPtr, __endPtr - __startPtr, false); \
__decrypted = true; \
__asm \
{ \
__asm push eax \
__asm push ebx \
__asm push ecx \
__asm push edx \
__asm xor eax, eax \
__asm cpuid \
__asm pop edx \
__asm pop ecx \
__asm pop ebx \
__asm pop eax \
}; \
} \
} \
if(!__decrypted) \
goto __end; \
__init: \
__asm { \
__asm jmp __next \
_PROTECT_START_MARKER \
__asm __next: \
} \
__start: \
{
#else
#define PROTECT_START_FUNC(decrFunc) \
static bool __decrypted = false; \
byte *__startPtr, *__endPtr, *__noncePtr; \
if(!__decrypted) \
{ \
__asm \
{ \
__asm push eax \
__asm lea eax, __start \
__asm mov __startPtr, eax \
__asm lea eax, __end \
__asm mov __endPtr, eax \
__asm lea eax, __init \
__asm inc eax \
__asm inc eax \
__asm mov __noncePtr, eax \
__asm pop eax \
}; \
PROTECT_WRITE_ACCESS(__startPtr, __endPtr - __startPtr, true); \
decrFunc(__startPtr, __endPtr - __startPtr, __noncePtr, 6); \
PROTECT_WRITE_ACCESS(__startPtr, __endPtr - __startPtr, false); \
__decrypted = true; \
__asm \
{ \
__asm push eax \
__asm push ebx \
__asm push ecx \
__asm push edx \
__asm xor eax, eax \
__asm cpuid \
__asm pop edx \
__asm pop ecx \
__asm pop ebx \
__asm pop eax \
}; \
} \
if(!__decrypted) \
goto __end; \
__init: \
__asm { \
__asm jmp __next \
_PROTECT_START_MARKER \
__asm __next: \
} \
__start: \
{
#endif

#define PROTECT_END_FUNC \
} \
__end: \
__asm { \
__asm jmp __next2 \
_PROTECT_END_MARKER \
__asm __next2: \
};

#define OBFUSCATE_START_FUNC \
byte *__startPtr, *__endPtr, *__keyPtr; \
__asm \
{ \
__asm push eax \
__asm lea eax, __start \
__asm mov __startPtr, eax \
__asm lea eax, __end \
__asm mov __endPtr, eax \
__asm lea eax, __init \
__asm inc eax \
__asm inc eax \
__asm mov __keyPtr, eax \
__asm pop eax \
}; \
PROTECT_OBFUSCATE(__startPtr, __endPtr - __startPtr, __keyPtr, 16); \
__asm \
{ \
__asm push eax \
__asm push ebx \
__asm push ecx \
__asm push edx \
__asm xor eax, eax \
__asm cpuid \
__asm pop edx \
__asm pop ecx \
__asm pop ebx \
__asm pop eax \
}; \
__init: \
__asm { \
__asm jmp __next \
_OBFUSCATE_START_MARKER \
_OBFUSCATE_KEYPLACER \
__asm __next: \
} \
__start: \

#define OBFUSCATE_END_FUNC \
__asm { \
__asm jmp __next2 \
_OBFUSCATE_END_MARKER \
__asm __next2: \
}; \
__end: \
PROTECT_OBFUSCATE(__startPtr, __endPtr - __startPtr, __keyPtr, 16)


#endif

// Check macro for program startup -- executes next statement if invalid key
#define ON_PROTECT_BAD_KEY(decrFunc) \
bool __keyOk; \
{ \
const char *crypted = PROTECT_START_MARKER "abracadabra" PROTECT_END_MARKER; \
const int len = strlen("abracadabra"); \
Buffer<byte>buf(len); \
Buffer<byte>nonce(6); \
/* can't use memcpy, it gets optimized out */ \
for(int i = 0; i < len; i++) buf[i] = crypted[i + 6 /* sizeof(PROTECT_START_MARKER)*/]; \
for(int i = 0; i < 6 /* sizeof(PROTECT_START_MARKER)*/; i++) nonce[i] = crypted[i]; \
decrFunc(buf, len, nonce, 6); \
__keyOk = !memcmp(buf, "abracadabra", len); \
} \
if(!__keyOk)

#else

#define PROTECT_START_FUNC(decrFunc)
#define PROTECT_END_FUNC
#define OBFUSCATE_START_FUNC
#define OBFUSCATE_END_FUNC
#define ON_PROTECT_BAD_KEY(decrFunc) if(false)

#endif

bool PROTECT_WRITE_ACCESS(byte *start, size_t size, bool access);
void PROTECT_DECRYPT(byte *start, size_t size, String const &key, byte const *nonce, size_t nonceLen);
void PROTECT_OBFUSCATE(byte *start, size_t len, byte *key, size_t keyLen);

#endif

Change log

r4205 by micio on Nov 27, 2011   Diff
Bazaar/Protect : removed a debbugging
printout
Go to: 
Project members, sign in to write a code review

Older revisions

r3189 by micio on Feb 10, 2011   Diff
Bazaar/Protect : fixed Protect macros
for windows part
r3180 by micio on Feb 8, 2011   Diff
Bazaar/Protect : made PROTECT_xxx
macros multithread safe
r3156 by micio on Feb 4, 2011   Diff
Bazaar/Protect : finally working
OBFUSCATE in optimized mode
All revisions of this file

File info

Size: 8740 bytes, 339 lines
Powered by Google Project Hosting