-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_static.c
203 lines (180 loc) · 4.68 KB
/
test_static.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
#define _GNU_SOURCE 1 /* REG_RIP */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <sys/mman.h>
static uint8_t* loadfile(const char *fn, size_t *num) {
size_t n, j = 0; uint8_t *buf = 0;
FILE *f = fopen(fn, "rb");
if (f) {
fseek(f, 0, SEEK_END);
n = ftell(f);
fseek(f, 0, SEEK_SET);
if (n) {
buf = (uint8_t*)malloc(n);
if (buf) j = fread(buf, 1, n, f);
}
fclose(f);
}
if (num) *num = j;
return buf;
}
#define TRY_CATCH 0
#if TRY_CATCH
#include <setjmp.h>
#include <signal.h>
static jmp_buf try_return;
static void sighandler(int sig, siginfo_t *si, void *data) {
const ucontext_t *ctx = (ucontext_t*)data;
fprintf(stderr, "!!! %s at address %p\n", sig == SIGSEGV ? "SIGSEGV" : "SIGBUS", (void*)si->si_addr);
#if defined(__x86_64__)
fprintf(stderr, "RIP: %p\n", (void*)ctx->uc_mcontext.gregs[REG_RIP]);
#elif defined(__i386__)
fprintf(stderr, "EIP: %p\n", (void*)ctx->uc_mcontext.gregs[REG_EIP]);
#endif
longjmp(try_return, 1);
}
static void sethandler() {
struct sigaction sa;
sa.sa_flags = SA_SIGINFO;
sigemptyset(&sa.sa_mask);
sa.sa_sigaction = sighandler;
sigaction(SIGSEGV, &sa, NULL);
sigaction(SIGBUS, &sa, NULL);
}
#endif
int main(int argc, char **argv) {
size_t code_size, src_size;
unsigned long long out_size;
uint8_t *code1, *code2, *src, *out, *out2, *temp; char *end;
uint32_t *header; int tsize = 2048, code_extra;
if (argc != 1 + 3) {
fprintf(stderr, "Usage: ./test_static static[32|64].bin src.lzma out_size > output.bin\n");
return 1;
}
code1 = loadfile(argv[1], &code_size);
if (!code1) {
fprintf(stderr, "!!! error loading code\n");
return 1;
}
header = (uint32_t*)code1;
if (code_size < 4 * 6 || code_size - 4 * 6 != header[0]) {
fprintf(stderr, "!!! code header is wrong\n");
return 1;
}
code_size = header[0];
fprintf(stderr, "code size = %u\n", (int)code_size);
src = loadfile(argv[2], &src_size);
if (!src) {
fprintf(stderr, "!!! error loading src data\n");
return 1;
}
out_size = strtoull(argv[3], &end, 0);
if (!!*end) {
fprintf(stderr, "!!! error reading out size\n");
return 1;
}
if (!(out = malloc(out_size))) {
fprintf(stderr, "!!! malloc failed (out)\n");
return 1;
}
#if defined(__x86_64__)
code_extra = 3 + 3 * 10 + 6;
#elif defined(__i386__)
code_extra = 3 + 3 * 5 + 7;
#else
#error
#endif
code2 = mmap(NULL, code_extra + code_size,
PROT_EXEC + PROT_READ + PROT_WRITE,
MAP_PRIVATE + MAP_ANONYMOUS, -1, 0);
if (!code2) {
fprintf(stderr, "!!! mmap failed\n");
return 1;
}
{
uint8_t *p = code2, *c = src + 14;
int pb, lp, lc = src[0];
pb = lc / 9; lc %= 9; lp = pb % 5; pb /= 5;
if (pb >= 5 || c[-1] || *(int64_t*)(src + 5) != -1) {
fprintf(stderr, "!!! wrong lzma stream\n");
return 1;
}
tsize += 768 << (lc + lp);
if (!(temp = malloc(tsize * 2))) {
fprintf(stderr, "!!! malloc failed (temp)\n");
return 1;
}
#if defined(__x86_64__)
*p++ = 0x53; // push rdi
*p++ = 0x56; // push rsi
*p++ = 0x57; // push rbx
p[0] = 0x49; p[1] = 0xb8;
*(uint64_t*)(p + 2) = (uintptr_t)src + 18;
p += 10;
p[0] = 0x49; p[1] = 0xb9;
*(uint64_t*)(p + 2) = (uintptr_t)out;
p += 10;
p[0] = 0x49; p[1] = 0xba;
*(uint64_t*)(p + 2) = (uintptr_t)temp;
p += 10;
#elif defined(__i386__)
*p++ = 0x53; // push edi
*p++ = 0x56; // push esi
*p++ = 0x57; // push ebx
p[0] = 0x68;
*(uint32_t*)(p + 1) = (uintptr_t)src + 18;
p += 5;
p[0] = 0x68;
*(uint32_t*)(p + 1) = (uintptr_t)out;
p += 5;
p[0] = 0x68;
*(uint32_t*)(p + 1) = (uintptr_t)temp;
p += 5;
#endif
memcpy(p, header + 6, code_size);
*(uint32_t*)(p + header[1]) =
c[0] << 24 | c[1] << 16 | c[2] << 8 | c[3];
*(uint32_t*)(p + header[2]) = tsize;
*(uint8_t*)(p + header[3]) = (1 << pb) - 1;
*(uint8_t*)(p + header[4]) = (1 << lp) - 1;
*(uint8_t*)(p + header[5]) = lc;
p += code_size;
#if defined(__x86_64__)
*p++ = 0x49; *p++ = 0x91; // xchg rax, r9
*p++ = 0x5f; // pop rdi
*p++ = 0x5e; // pop rsi
*p++ = 0x5b; // pop rbx
*p++ = 0xc3; // ret
#elif defined(__i386__)
*p++ = 0x59; // pop ecx
*p++ = 0x58; // pop eax
*p++ = 0x59; // pop ecx
*p++ = 0x5f; // pop edi
*p++ = 0x5e; // pop esi
*p++ = 0x5b; // pop ebx
*p++ = 0xc3; // ret
#endif
if (p != code2 + code_size + code_extra) {
fprintf(stderr, "!!! code size mismatch\n");
return 1;
}
}
fprintf(stderr, "src = %p\n", src);
fprintf(stderr, "dest = %p\n", out);
fprintf(stderr, "tmp = %p, %i*2\n", temp, tsize);
fprintf(stderr, "code = %p\n", code2);
#if TRY_CATCH
out2 = 0;
sethandler();
if (setjmp(try_return)) {
} else
#endif
out2 = ((uint8_t*(*)(void))code2)();
if (out2 != out + out_size) {
fprintf(stderr, "!!! wrong result size\n");
}
fwrite(out, 1, out_size, stdout);
return 0;
}