-
Notifications
You must be signed in to change notification settings - Fork 3
/
funccrypt.c
319 lines (271 loc) · 9.01 KB
/
funccrypt.c
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
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <stdint.h>
#include <errno.h>
#include <sys/mman.h> /* PROT_* */
#include <string.h> /* strdup() */
#include "utils.h"
//#define VERBOSE 1
#ifndef __GNUC__
#error "Unknown compiler; Only GCC supports `asm goto'."
#endif
#ifdef __clang__
#error "CLang does not support `asm goto' (yet?)."
#endif
/* Force GCC struct for MingW compilers and pack them,
* which means the struct is 1-byte aligned.
*/
#define GCC_PACKED __attribute__((packed, gcc_struct))
typedef struct crypt_header {
uint64_t key;
uint8_t crypted;
uint32_t marker;
uint64_t func_body[0];
} GCC_PACKED crypt_header;
#define CRYPT_FUNC_MAXSIZ 0x100
#define CRYPT_FUNC(fn) \
crypt_func((void *)fn, 0, NULL, NULL)
#define CRYPT_PROLOGUE(fn) \
crypt_return __cr; \
{ \
__cr = CRYPT_FUNC(fn); \
if (__cr != CRET_OK) \
asm volatile goto("jmp %l0 \n" \
: : : : cr_epilogue); \
asm volatile goto("jmp %l0 \n" \
: : : : cr_prologue); \
asm volatile( \
".byte " \
/* key: */ "0x00, 0x00, 0x00, 0x00," \
"0x00, 0x00, 0x00, 0x00," \
/* crypted: */ "0x00," \
/* marker: */ "0xDE, 0xC0, 0xDE, 0xC0; \n" \
); \
} \
cr_prologue: {
#define CRYPT_EPILOGUE(fn) \
} { \
asm volatile goto("jmp %l0 \n" \
: : : : cr_epilogue); \
asm volatile( \
".byte " \
/* Insert encryption gap, so we can find the end marker,
* while the function body is encrypted.
*/ \
"0x00, 0x00, 0x00, 0x00," \
"0x00, 0x00, 0x00, 0x00," \
/* marker: */ "0xFE, 0xCA, 0xFE, 0xCA; \n" \
); \
} \
cr_epilogue: \
CRYPT_FUNC(fn);
#define CRYPT_FNDEF(name, ...) \
void name( __VA_ARGS__ ) { \
CRYPT_PROLOGUE( name );
#define CRYPT_FNEND(name) \
CRYPT_EPILOGUE(name); \
}
typedef enum crypt_return {
CRET_ERROR, CRET_PROLOGUE, CRET_EPILOGUE, CRET_CHECK, CRET_OK,
CRET_CHECK_ENCRYPTED, CRET_CHECK_PLAIN
} crypt_return;
static const char *crypt_strs[] = {
"ERROR", "PROLOGUE", "EPILOGUE", "CHECK", "OK", "ENCRYPTED", "PLAIN"
};
static crypt_return crypt_func(void *fn_start, int do_check, struct crypt_header ** const func_crypt_header,
size_t * const func_body_size);
static void printHexBuf(uint8_t *buf, size_t siz, size_t chars_per_line);
static int some_fn(int arg0, char *arg1, void *arg2)
{
CRYPT_PROLOGUE(some_fn);
int i;
printf("I'm decrypted .. args: %d, %s, %p\n", arg0, arg1, arg2);
for (i = 0; i < 32; ++i)
printf("%d ", i);
puts("");
CRYPT_EPILOGUE(some_fn);
return 0x66;
}
static void another_fn(void)
{
CRYPT_PROLOGUE(another_fn);
printf("Another decrypted fn..\n");
CRYPT_EPILOGUE(another_fn);
}
CRYPT_FNDEF(fndef_fn, void *arg0, int arg1, const char *arg2)
{
printf("Yet another cryptable fn.. args: %p, %d, %s", arg0, arg1, arg2);
/* generate some nonsense machine instructions */
printf("."); printf("."); printf("."); printf(".");
printf("\n");
}
CRYPT_FNEND(fndef_fn);
static crypt_return crypt_func(void *fn_start, int do_check,
struct crypt_header ** const func_crypt_header,
size_t * const func_body_size)
{
size_t i;
enum crypt_return cret = CRET_ERROR;
uint8_t *fnbuf = (uint8_t *) fn_start;
uint8_t *pro, *epi, *mbuf;
const uint32_t prologue_marker = 0xC0DEC0DE;
const uint32_t epilogue_marker = 0xCAFECAFE;
crypt_header *hdr = NULL;
size_t crypt_size = 0;
#if VERBOSE
printf("Fn: %p\n", fnbuf);
#endif
for (i = 0; i < CRYPT_FUNC_MAXSIZ; ++i) {
if (cret == CRET_ERROR &&
*(uint32_t *) &fnbuf[i] == prologue_marker)
{
#if VERBOSE
printf("Prologue Marker: %p\n", &fnbuf[i]);
#endif
pro = &fnbuf[i];
cret = CRET_PROLOGUE;
} else
if (cret == CRET_PROLOGUE &&
*(uint32_t *) &fnbuf[i] == epilogue_marker)
{
#if VERBOSE
printf("Epilogue Marker: %p\n", &fnbuf[i]);
#endif
epi = &fnbuf[i];
cret = CRET_EPILOGUE;
break;
}
}
if (cret == CRET_EPILOGUE &&
i >= sizeof *hdr)
{
cret = CRET_CHECK;
#if VERBOSE
printf("Prologue: ");
printHexBuf(pro - 9, 13, 13);
printf("Epilogue: ");
printHexBuf(epi, 4, 4);
#endif
hdr = (crypt_header *)(pro + sizeof(prologue_marker) - sizeof *hdr);
crypt_size = epi - (pro + sizeof(prologue_marker));
if (do_check)
{
if (hdr->crypted == 0x00) {
cret = CRET_CHECK_PLAIN;
} else {
cret = CRET_CHECK_ENCRYPTED;
}
} else if (i &&
(hdr->crypted == 0x00 || hdr->crypted == 0xFF) &&
(long int)crypt_size < sysconf(_SC_PAGESIZE))
{
mbuf = (uint8_t *)( (long int)hdr & ~(sysconf(_SC_PAGESIZE) - 1) );
if (!mprotect(mbuf, sysconf(_SC_PAGESIZE), PROT_READ|PROT_WRITE|PROT_EXEC))
{
if (hdr->crypted == 0x00) {
hdr->crypted = 0xFF;
hdr->key = (uint64_t) rand() << 32;
hdr->key |= rand();
} else {
hdr->crypted = 0x00;
}
for (i = 0; i < crypt_size / 0x8; ++i) {
hdr->func_body[i] ^= hdr->key;
}
if (!mprotect(mbuf, sysconf(_SC_PAGESIZE), PROT_READ|PROT_EXEC))
cret = CRET_OK;
}
}
}
if (func_crypt_header) {
*func_crypt_header = hdr;
}
if (func_body_size) {
*func_body_size = crypt_size;
}
return cret;
}
static void printHexBuf(uint8_t *buf, size_t siz, size_t chars_per_line)
{
size_t i;
for (i = 0; i < siz; ++i) {
printf("%02X ", buf[i]);
if ((i+1) % chars_per_line == 0)
printf("\n");
}
if ((i) % chars_per_line != 0)
printf("\n");
}
static void calcAndPrintEntropy(struct crypt_header * const func_crypt_header,
size_t const func_body_size)
{
printf("entropy of %s function: %f\n", (func_crypt_header->crypted == 0xFF ? "encrypted" : "unencrypted"),
entropy((uint8_t *)func_crypt_header->func_body, func_body_size));
}
static void initRandom(void)
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
srand((int)(ts.tv_sec + ts.tv_nsec));
}
int main(int argc, char ** argv)
{
struct crypt_header * hdr = NULL;
size_t func_body_size = 0;
crypt_return cret;
(void)argc;
(void)argv;
initRandom();
cret = crypt_func((void *)some_fn, 1, &hdr, &func_body_size);
printf("some_fn check return val: %s\n", crypt_strs[cret]);
calcAndPrintEntropy(hdr, func_body_size);
printHexBuf((uint8_t *)hdr->func_body, func_body_size, 32);
if (cret == CRET_CHECK_PLAIN) {
cret = crypt_func((void *)some_fn, 0, &hdr, &func_body_size);
assert(cret == CRET_OK);
printf("\nsome_fn encryption return val: %s\n", crypt_strs[cret]);
calcAndPrintEntropy(hdr, func_body_size);
printf("\nsome_fn encrypted:\n");
printHexBuf((uint8_t *)hdr->func_body, func_body_size, 32);
}
puts("\n");
cret = crypt_func((void *)another_fn, 1, &hdr, &func_body_size);
printf("another_fn check return val: %s\n", crypt_strs[cret]);
calcAndPrintEntropy(hdr, func_body_size);
printHexBuf((uint8_t *)hdr->func_body, func_body_size, 32);
if (cret == CRET_CHECK_PLAIN) {
cret = crypt_func((void *)another_fn, 0, &hdr, &func_body_size);
assert(cret == CRET_OK);
printf("\nanother_fn return val: %s\n", crypt_strs[cret]);
calcAndPrintEntropy(hdr, func_body_size);
printf("\nanother_fn encrypted:\n");
printHexBuf((uint8_t *)hdr->func_body, func_body_size, 32);
}
puts("\n");
cret = crypt_func((void *)fndef_fn, 1, &hdr, &func_body_size);
printf("fndef_fn check return val: %s\n", crypt_strs[cret]);
calcAndPrintEntropy(hdr, func_body_size);
printHexBuf((uint8_t *)hdr->func_body, func_body_size, 32);
if (cret == CRET_CHECK_PLAIN) {
cret = crypt_func((void *)fndef_fn, 0, &hdr, &func_body_size);
assert(cret == CRET_OK);
printf("\nfndef_fn return val: %s\n", crypt_strs[cret]);
calcAndPrintEntropy(hdr, func_body_size);
printf("\nfndef_fn encrypted:\n");
printHexBuf((uint8_t *)hdr->func_body, func_body_size, 32);
}
puts("\n");
printf("\n[output]\n");
printf("some_fn:\n");
char * blah = strdup("BLAH");
printf("return value: 0x%X\n", some_fn(0, blah, NULL));
free(blah);
printf("\nanother_fn:\n");
another_fn();
printf("\nfndef_fn:\n");
fndef_fn(NULL, (unsigned int)-1, "TEST");
return 0;
}