-
Notifications
You must be signed in to change notification settings - Fork 2
/
one4all.h
526 lines (436 loc) · 11.5 KB
/
one4all.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// one4all.h by Inndy Lin <[email protected]>
// compiled at 2020-03-06 11:58:55 +0800
#ifndef _ONE4ALL_H_
#define _ONE4ALL_H_
/* filename: src/one4all.h */
// version
#define ONE4ALL 2
#include <assert.h>
#include <ctype.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if _WIN32 || _WIN64
#if _WIN64
#define PTR64
#else
#define PTR32
#endif
#ifndef WIN32
#define WIN32
#endif
#elif __GNUC__
#if __x86_64__ || __ppc64__
#define PTR64
#else
#define PTR32
#endif
#ifndef LINUX
#define LINUX
#endif
#elif UINTPTR_MAX > UINT_MAX
#define PTR64
#else
#define PTR32
#endif
/////////////////////////
// //
// OS specific headers //
// //
/////////////////////////
#ifdef WIN32
#define O_OS_EXT
#include <winsock2.h>
#include <windows.h>
#endif
#ifdef LINUX
#define O_OS_EXT
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <unistd.h>
#endif
///////////////////////////
// //
// One4All shared consts //
// //
///////////////////////////
#define O_INVALID (intptr_t)-1
#define O_SUCCESS 1
#define O_FAILED 0
#define O_STATUS(X) (X) ? O_SUCCESS : O_FAILED
#define MUST(X) assert((X) == O_SUCCESS)
/* filename: src/dtypes.h */
typedef void* void_ptr;
typedef const void* const_void_ptr;
typedef void* (*shellcode_t)();
#ifndef WIN32
// Windows data types for non-windows platform
#define CHAR int8_t
#define BYTE uint8_t
#define UCHAR uint8_t
#define SHORT int16_t
#define USHORT uint16_t
#define DWORD uint32_t
#define LONG int32_t
#define ULONG uint32_t
#define QWORD uint64_t
#define SIZE_T size_t
#define PVOID void_ptr
#define LPVOID void_ptr
#define LPCVOID const_void_ptr
#define DWORD_PTR uintptr_t
#endif
// IDA types
#ifndef __int8
#define __int8 char
#endif
#ifndef __int64
#define __int64 int64_t
#endif
#ifndef _DWORD
#define _DWORD uint32_t
#endif
#ifndef _BYTE
#define _BYTE uint8_t
#endif
/* filename: src/file.h */
int fsize(FILE *fp, size_t *out);
int readfile(const char *filename, uint8_t **out_buffer, size_t *out_size);
int writefile(const char *filename, void *buffer, size_t size);
#define READFILE(FN, BUF, SZ) uint8_t *BUF; size_t SZ; MUST(readfile(FN, &BUF, &SZ));
/* filename: src/hashtable.h */
typedef struct _HTBL_ENTRY {
struct _HTBL_ENTRY *next;
void *data;
char key[0];
} HTBL_ENTRY, *PHTBL_ENTRY;
typedef struct {
unsigned int count;
unsigned int key_size;
PHTBL_ENTRY table[0];
} HTBL, *PHTBL;
PHTBL htbl_create(unsigned int count, unsigned int key_size);
void htbl_insert(PHTBL t, void *key, void *data);
void* htbl_search(PHTBL t, void *key);
int htbl_remove(PHTBL t, void *key);
void htbl_destroy(PHTBL t);
/* filename: src/hex.h */
void hexdump_ex(const void *ptr, size_t size, intptr_t addr, void (*cb)(void *, const char *), void *ctx);
void hexdump_file(const void *data, size_t size, FILE *target);
void hexdump(const void *data, size_t size);
char *hexdump_string(const void *data, size_t size, char *buff, size_t buff_size);
size_t hexdecode(const char *encoded, void *buffer);
#define xxd(X) puts(#X); hexdump(X, sizeof(X))
#define zfill(X) memset(X, 0, sizeof(X))
#define HEX_DIGIT_DECODE(H) (('0' <= (H) && (H) <= '9') ? (H) - '0' : (((H) | ' ') - 'a' + 0xa))
/* filename: src/mem.h */
#define ALIGN_TO(V, A) (((V) / (A) + 1) * (A))
#ifdef O_OS_EXT
#define O_MEM_N 0
#define O_MEM_R 1
#define O_MEM_W 2
#define O_MEM_E 4
#define O_MEM_X O_MEM_E
#define O_MEM_RO O_MEM_R
#define O_MEM_RE (O_MEM_R | O_MEM_E)
#define O_MEM_RX O_MEM_RE
#define O_MEM_RW (O_MEM_R | O_MEM_W)
#define O_MEM_RWE (O_MEM_R | O_MEM_W | O_MEM_E)
#define O_MEM_RWX O_MEM_RWE
int memprotect(void *mem, size_t size, uint32_t prot);
void* memmap(void *addr, size_t size, uint32_t prot);
int memunmap(void *addr, size_t size);
#endif
/* filename: src/file.c */
// TODO: handle file size larger than uint32_t under 32bit process
int fsize(FILE *fp, size_t *out)
{
off_t curr = ftello(fp);
if(fseeko(fp, 0, SEEK_END) != 0) return O_FAILED;
*out = ftello(fp);
// it will be a fatal error if we can't restore file pointer
assert(fseeko(fp, curr, SEEK_SET) == 0);
return O_SUCCESS;
}
int readfile(const char *filename, uint8_t **out_buffer, size_t *out_size)
{
FILE *fp = fopen(filename, "rb");
if(fp == NULL) {
return O_FAILED;
}
if(fsize(fp, out_size) != O_SUCCESS) {
goto failed;
}
*out_buffer = (uint8_t*)malloc(*out_size);
if(*out_buffer == NULL) {
goto failed;
}
if(fread(*out_buffer, *out_size, 1, fp) != 1) {
goto failed;
}
fclose(fp);
return O_SUCCESS;
failed:
free(*out_buffer);
*out_size = 0;
*out_buffer = NULL;
fclose(fp);
return O_FAILED;
}
int writefile(const char *filename, void *buffer, size_t size)
{
FILE *fp = fopen(filename, "wb");
if(fp == NULL) {
return O_FAILED;
}
if(fwrite(buffer, size, 1, fp) != 1) {
goto failed;
}
fclose(fp);
return O_SUCCESS;
failed:
fclose(fp);
return O_FAILED;
}
/* filename: src/hashtable.c */
uintptr_t htbl_hash_data(uintptr_t seed, void* data, size_t len)
{
unsigned char *ptr = data;
for(size_t i = 0; i < len; i++) {
seed = seed * 0xdead1337 - ptr[i] * 0x13579bdf + 0xf33d1ee7;
// seed = seed ^ (seed >> 7) ^ (ptr[i] << 19);
}
return seed;
}
PHTBL htbl_create(unsigned int count, unsigned int key_size)
{
PHTBL t = malloc(sizeof(HTBL) + sizeof(PHTBL_ENTRY) * count);
t->count = count;
t->key_size = key_size;
for (int i = 0; i < count; i++)
t->table[i] = NULL;
return t;
}
void htbl_insert(PHTBL t, void *key, void *data)
{
PHTBL_ENTRY node = malloc(sizeof(HTBL_ENTRY) + t->key_size);
memcpy(node->key, key, t->key_size);
node->data = data;
uintptr_t index = htbl_hash_data(0, key, t->key_size) % t->count;
node->next = t->table[index];
t->table[index] = node;
}
void* htbl_search(PHTBL t, void *key)
{
uintptr_t index = htbl_hash_data(0, key, t->key_size) % t->count;
for (PHTBL_ENTRY node = t->table[index];
node != NULL;
node = node->next)
{
if (memcmp(node->data, key, t->key_size) == 0)
return node->data;
}
return NULL;
}
int htbl_remove(PHTBL t, void *key)
{
uintptr_t index = htbl_hash_data(0, key, t->key_size) % t->count;
PHTBL_ENTRY prev = NULL;
for (PHTBL_ENTRY node = t->table[index];
node != NULL;
node = node->next)
{
if (memcmp(node->key, key, t->key_size) == 0) {
if(prev == NULL) {
t->table[index] = node->next;
} else {
prev->next = node->next;
}
free(node);
return 1;
}
prev = node;
}
return 0;
}
void htbl_destroy(PHTBL t)
{
for (int i = 0; i < t->count; i++)
{
PHTBL_ENTRY node = t->table[i], next;
while (node)
{
next = node->next;
free(node);
node = next;
}
}
free(t);
}
/* filename: src/hex.c */
void hexdump_ex(const void *ptr, size_t size, intptr_t addr, void (*cb)(void *, const char *), void *ctx)
{
const uint8_t *buffer = (const uint8_t *)ptr;
while(size > 0) {
uint32_t written;
char line[128];
written = snprintf(line, sizeof(line), "%.8" PRIx64 ": ", (uint64_t)addr);
uint32_t bound = (size >= 16) ? 16 : size;
int i;
for(i = 0; i < bound; i++) {
written += snprintf(line + written, sizeof(line) - written, "%.2x ", buffer[i]);
}
for(; i < 16; i++) {
written += snprintf(line + written, sizeof(line) - written, " ");
}
for(i = 0; i < bound; i++) {
written += snprintf(line + written, sizeof(line) - written, "%c", (0x20 <= buffer[i] && buffer[i] <= 0x7e) ? buffer[i] : '.');
}
cb(ctx, line);
if(size <= 16) {
return;
}
buffer += 16;
size -= 16;
addr += 16;
}
}
void hexdump_cb_file(void *ctx, const char *data)
{
fprintf(ctx, "%s\n", data);
}
void hexdump_file(const void *data, size_t size, FILE *target)
{
hexdump_ex(data, size, (intptr_t)data, hexdump_cb_file, target);
}
void hexdump(const void *data, size_t size)
{
hexdump_file(data, size, stdout);
}
void hexdump_cb_string(void *ctx, const char *data)
{
uintptr_t *x = ctx;
x[1] += snprintf((char*)x[0] + x[1], x[2] - x[1], "%s\n", data);
}
char *hexdump_string(const void *data, size_t size, char *buff, size_t buff_size)
{
uintptr_t ctx[] = { (uintptr_t)buff, 0, buff_size };
hexdump_ex(data, size, (uintptr_t)data, hexdump_cb_string, &ctx);
return buff;
}
size_t hexdecode(const char *encoded, void *buffer)
{
size_t i = 0;
goto skip_space;
while(isxdigit(encoded[0]) && isxdigit(encoded[1])) {
((uint8_t*)buffer)[i++] = HEX_DIGIT_DECODE(encoded[0]) << 4 | HEX_DIGIT_DECODE(encoded[1]);
encoded += 2;
skip_space:
while(*encoded && isspace(*encoded)) encoded++;
}
return i;
}
/* filename: src/mem.c */
#ifdef O_OS_EXT
uint32_t _o4a_native_mem_prot_conv(uint32_t prot)
{
#ifdef WIN32
switch(prot) {
case O_MEM_N: return PAGE_NOACCESS;
case O_MEM_RO: return PAGE_READONLY;
case O_MEM_RE: return PAGE_EXECUTE_READ;
case O_MEM_RW: return PAGE_READWRITE;
case O_MEM_RWE: return PAGE_EXECUTE_READWRITE;
default: return O_INVALID;
}
#endif
#ifdef LINUX
switch(prot) {
case O_MEM_N: return PROT_NONE;
case O_MEM_RO: return PROT_READ;
case O_MEM_RE: return PROT_READ | PROT_EXEC;
case O_MEM_RW: return PROT_READ | PROT_WRITE;
case O_MEM_RWE: return PROT_READ | PROT_WRITE | PROT_EXEC;
default: return O_INVALID;
}
#endif
return O_INVALID;
}
int memprotect(void *mem, size_t size, uint32_t prot)
{
#ifdef WIN32
DWORD old_prot;
return O_STATUS(VirtualProtect(mem, size, _o4a_native_mem_prot_conv(prot), &old_prot) != 0);
#endif
#ifdef LINUX
return O_STATUS(mprotect(mem, size, _o4a_native_mem_prot_conv(prot)) == 0);
#endif
return O_FAILED;
}
void* memmap(void *addr, size_t size, uint32_t prot)
{
#ifdef WIN32
return VirtualAlloc(addr, size, MEM_RESERVE | MEM_COMMIT, _o4a_native_mem_prot_conv(prot));
#endif
#ifdef LINUX
return mmap(addr, size, _o4a_native_mem_prot_conv(prot), /*flags*/ MAP_PRIVATE | MAP_ANONYMOUS, /*fd*/ 0, /*offset*/ 0);
#endif
return NULL;
}
int memunmap(void *addr, size_t size)
{
#ifdef WIN32
return O_STATUS(VirtualFree(addr, 0 /* must be 0 if MEM_RELEASE */, MEM_RELEASE) != 0);
#endif
#ifdef LINUX
return O_STATUS(munmap(addr, size) == 0);
#endif
return O_FAILED;
}
#endif
/* filename: src/test.c */
#ifdef ONE4ALL_TEST
int main()
{
void* data = memmap(NULL, 0x1000, O_MEM_RWE);
memcpy(data, "\x33\xc0\xc3", 3); // zero out eax and return for x86 / amd64
assert(data != NULL);
assert(((shellcode_t)data)() == 0);
char buff[1024];
zfill(buff); // zero fill buffer
char *p = hexdump_string(data, 16, buff, sizeof buff);
strcat(p, "NotBad\n");
puts(buff);
puts("hexdmup_file:");
hexdump_file(buff, 128, stdout);
assert(writefile("test-file.tmp", buff, 64) == O_SUCCESS);
BYTE *ptr;
size_t sz;
assert(readfile("test-file.tmp", &ptr, &sz) == O_SUCCESS);
puts("hexdmup:");
hexdump(ptr, sz);
free(ptr);
assert(memunmap(data, 0x1000) == O_SUCCESS);
memset(buff, 0xcc, sizeof(buff));
size_t n = hexdecode("11 22 33 44 55 66 77 8899aa bb\ncc\tdd\reeff", (void*)&buff);
hexdump(buff, (n | 0xf) + 1);
PHTBL t = htbl_create(16, sizeof(int));
for(int i = 0; i < 16; i++) {
htbl_insert(t, &i, (void*)(uintptr_t)i);
}
for(int i = 0; i < t->count; i++) {
int c = 0;
PHTBL_ENTRY node = t->table[i];
while(node) {
// printf(" - %d\n", (int)node->data);
c++;
node = node->next;
}
printf("table[%d] -> %d\n", i, c);
}
}
#endif
#endif