-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy paththunk_code_x64.cpp
More file actions
409 lines (372 loc) · 8.99 KB
/
thunk_code_x64.cpp
File metadata and controls
409 lines (372 loc) · 8.99 KB
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
struct thunk_code
{
struct backup_pc
{
// pop qword ptr [_rip]
short save_rip; //pop qword ptr [_rip]
int32_t _rip;
backup_pc(intptr_t& rip)
{
save_rip = 0x058F;
_rip = offset(&rip, _rip);
}
};
struct restore_pc
{
// push _rip
short restore_rip;
int32_t _rip;
restore_pc(intptr_t& rip)
{
restore_rip = (short)0x35FF;
_rip = offset(&rip, _rip);
}
};
//Move the parameters in the stack to make it to the top of the stack
struct alignment_stack
{
/*
push rcx
push rdi
push rsi
mov rcx, count
lea rdi, [rsp+(offset+4)*8]
lea rsi, [rsp+(offset+3)*8]
rep movsq
pop rsi
pop rdi
pop rcx
*/
char backup_reg[3];
char set_rcx[3];
uint32_t _count;
char copy_src[4];
char _src;
char copy_dest[4];
char _dest;
char move[3];
char restore_reg[3];
alignment_stack(char offset, uint32_t count)
{
memcpy(backup_reg, "\x51\x57\x56", _countof(backup_reg));
memcpy(set_rcx, "\x48\xC7\xC1", _countof(set_rcx));
_count = count;
memcpy(copy_src, "\x48\x8D\x74\x24", sizeof(copy_src));
memcpy(copy_dest, "\x48\x8D\x7C\x24", sizeof(copy_dest));
_src = (offset + 4) * 0x8;
_dest = (offset + 3) * 0x8;
memcpy(move, "\xF3\x48\xA5", _countof(move));
memcpy(restore_reg, "\x5E\x5F\x59", _countof(restore_reg));
}
};
struct alignment_stack1
{
/*
mov rax, qword ptr [rsp+30h]
mov qword ptr [rsp+28h], rax
*/
char copy_src[4];
char _src;
char copy_dest[4];
char _dest;
alignment_stack1(char offset)
{
memcpy(copy_src, "\x48\x8B\x44\x24", sizeof(copy_src));
memcpy(copy_dest, "\x48\x89\x44\x24", sizeof(copy_dest));
_src = (offset + 1) * 0x8;
_dest = offset * 0x8;
}
};
struct alignment_stack2
{
/*
MOVDQA xmm0, xmmword ptr [rsp+30h]
MOVDQU xmmword ptr [rsp+28h], xmm0
*/
char copy_src[5];
char _src;
char copy_dest[5];
char _dest;
alignment_stack2(char offset)
{
memcpy(copy_src, "\x66\x0F\x6F\x44\x24", sizeof(copy_src));
memcpy(copy_dest, "\xF3\x0F\x7F\x44\x24", sizeof(copy_dest));
_src = (offset + 1) * 0x8;
_dest = offset * 0x8;
}
};
struct adjust_params
{
/* MSVC
sub rsp, 16
mov [rsp+20h], r9 (or movsd mmword ptr [rsp+20h],xmm3)
mov r9, r8 (or movsd xmm3, xmm2)
mov r8, rdx (or movsd xmm2, xmm1)
mov rdx, rcx (or movsd xmm1, xmm0)
mov rcx, _this
*/
/* GCC
push 0
push r9
mov r9, r8
mov r8, rcx
mov rcx, rdx
mov rdx, rsi
mov rsi, rdi
mov rdi, _this
*/
char adjust_code[1];
template<size_t N>
inline char* copy_code(char* dest, const char(&code)[N])
{
memcpy(dest, code, N - 1);
return dest + N - 1;
}
adjust_params(size_t argc, const argument_info* arginfos)
{
char* p = adjust_code;
#if defined(_WIN32)
switch (argc)
{
default:
p = copy_code(p, "\x48\x83\xEC\x10");
if (arginfos && arginfos[3].as_floating())
p = copy_code(p, "\xF2\x0F\x11\x5C\x24\x20");
else
p = copy_code(p, "\x4C\x89\x4C\x24\x20");
case 3:
if (arginfos && arginfos[2].as_floating())
p = copy_code(p, "\xF2\x0F\x10\xDA");
else
p = copy_code(p, "\x4D\x8B\xC8");
case 2:
if (arginfos && arginfos[1].as_floating())
p = copy_code(p, "\xF2\x0F\x10\xD1");
else
p = copy_code(p, "\x4C\x8B\xC2");
case 1:
if (arginfos && arginfos[0].as_floating())
p = copy_code(p, "\xF2\x0F\x10\xC8");
else
p = copy_code(p, "\x48\x8B\xD1");
case 0:
if (argc == 5)
{
alignment_stack1* _alignment_stack = reinterpret_cast<alignment_stack1*>(p);
new(_alignment_stack) alignment_stack1(5);
p += sizeof(alignment_stack1);
}
else if (argc == 6)
{
alignment_stack2* _alignment_stack = reinterpret_cast<alignment_stack2*>(p);
new(_alignment_stack) alignment_stack2(5);
p += sizeof(alignment_stack2);
}
else if (argc>6)
{
alignment_stack* _alignment_stack = reinterpret_cast<alignment_stack*>(p);
new(_alignment_stack) alignment_stack(5, argc - 4);
p += sizeof(alignment_stack);
}
p = copy_code(p, "\x48\xB9");
}
#else
size_t iargc = 0, fargc = 0, sargc = 0;
if (arginfos)
{
for (size_t i = 0; i != argc; i++)
{
if (arginfos[i].as_floating()) ++fargc;
else ++iargc;
}
}
else
{
iargc = argc;
}
switch (iargc)
{
default:
p = copy_code(p, "\x6A\x00\x41\x51");
case 5:
p = copy_code(p, "\x4D\x8B\xC8");
case 4:
p = copy_code(p, "\x4C\x8B\xC1");
case 3:
p = copy_code(p, "\x48\x8B\xCA");
case 2:
p = copy_code(p, "\x48\x8B\xD6");
case 1:
p = copy_code(p, "\x48\x8B\xF7");
case 0:
if (iargc > 6)
{
sargc = (iargc - 6) + std::max(int(fargc - 8), 0);
if (sargc == 1)
{
alignment_stack1* _alignment_stack = reinterpret_cast<alignment_stack1*>(p);
new(_alignment_stack) alignment_stack1(1);
p += sizeof(alignment_stack1);
}
else if (sargc > 1)
{
alignment_stack* _alignment_stack = reinterpret_cast<alignment_stack*>(p);
new(_alignment_stack) alignment_stack(1, sargc);
p += sizeof(alignment_stack);
}
}
p = copy_code(p, "\x48\xBF");
}
#endif
}
static size_t calc_size(size_t argc, const argument_info* arginfos, bool* push_param_to_stack)
{
size_t n = 0;
*push_param_to_stack = false;
#if defined(_WIN32)
if (argc > 3)
{
n += 4;
if (argc > 4) *push_param_to_stack = true;
if (argc == 5) n += sizeof(alignment_stack1);
else if (argc == 6) n += sizeof(alignment_stack2);
else if (argc > 6) n += sizeof(alignment_stack);
n += (arginfos && arginfos[3].as_floating()) ? 6 : 5;
argc = 3;
}
for (size_t i = 0; i != argc; i++)
n += (arginfos && arginfos[i].as_floating()) ? 4 : 3;
#else
size_t iargc = 0, fargc = 0;
if (arginfos)
{
for (size_t i = 0; i != argc; i++)
if (arginfos[i].as_floating()) ++fargc;
else ++iargc;
}
else
{
iargc = argc;
}
if (iargc > 5)
{
n += 4;
if (argc > 6) *push_param_to_stack = true;
size_t sargc = (iargc - 6) + std::max(int(fargc - 8), 0);
if (sargc == 1) n += sizeof(alignment_stack1);
else if (argc > 1) n += sizeof(alignment_stack);
iargc = 5;
}
n += iargc * 3;
#endif
n += 2 + sizeof(intptr_t);
return n;
}
};
struct jump_function
{
/*
mov rax, _proc
jmp rax
*/
short mov_proc; //mov rax, _proc
intptr_t _proc;
short call_proc; //call rax
jump_function()
{
mov_proc = (short)0xB848;
call_proc = (short)0xE0FF;
}
};
struct call_function
{
/*
mov rax, _proc
call rax
*/
short mov_proc; //mov rax, _proc
intptr_t _proc;
short call_proc; //call rax
call_function()
{
mov_proc = (short)0xB848;
call_proc = (short)0xD0FF;
}
};
struct return_caller
{
/*
ret n
*/
char ret;
short _n;
return_caller(short n)
{
ret = 0xC2;
_n = n * 8;
}
};
adjust_params* _adjust_params;
jump_function* _jump_function;
call_function* _call_function;
restore_pc* _restore_pc;
intptr_t _rip;
thunk_code(call_declare, call_declare, size_t argc, const argument_info* arginfos)
{
memset(this, 0, sizeof(thunk_code));
char* code = reinterpret_cast<char*>(this + 1);
bool push_param_to_stack = false;
backup_pc* _backup_pc = reinterpret_cast<backup_pc*>(code);
new(_backup_pc) backup_pc(_rip);
_adjust_params = reinterpret_cast<adjust_params*>(_backup_pc + 1);
new(_adjust_params) adjust_params(argc, arginfos);
code = reinterpret_cast<char*>(_adjust_params) + adjust_params::calc_size(argc, arginfos, &push_param_to_stack);
if (push_param_to_stack)
{
_call_function = reinterpret_cast<call_function*>(code);
new(_call_function) call_function();
_restore_pc = reinterpret_cast<restore_pc*>(_call_function + 1);
new(_restore_pc) restore_pc(_rip);
return_caller* _return_caller = reinterpret_cast<return_caller*>(_restore_pc + 1);
new(_return_caller) return_caller(2);
}
else
{
_restore_pc = reinterpret_cast<restore_pc*>(code);
new(_restore_pc) restore_pc(_rip);
_jump_function = reinterpret_cast<jump_function*>(_restore_pc + 1);
new(_jump_function) jump_function();
}
}
void bind(void* object, void* proc)
{
if (_jump_function)
{
intptr_t* _this = reinterpret_cast<intptr_t*>(_restore_pc) - 1;
*_this = reinterpret_cast<intptr_t>(object);
_jump_function->_proc = (intptr_t)proc;
}
else if (_call_function)
{
intptr_t* _this = reinterpret_cast<intptr_t*>(_call_function) - 1;
*_this = reinterpret_cast<intptr_t>(object);
_call_function->_proc = (intptr_t)proc;
}
}
static int32_t offset(const void* var, int32_t& code)
{
return static_cast<int32_t>((const char*)var - (char*)(&code + 1));
}
static size_t calc_size(call_declare, call_declare, size_t argc, const argument_info* arginfos)
{
bool push_param_to_stack = false;
size_t n = adjust_params::calc_size(argc, arginfos, &push_param_to_stack);
n = sizeof(thunk_code) +
sizeof(backup_pc) + n + sizeof(restore_pc);
if (push_param_to_stack)
n += sizeof(call_function) + sizeof(return_caller);
else
n += sizeof(jump_function);
return n;
}
};