-
Notifications
You must be signed in to change notification settings - Fork 2
/
external_process.cpp
826 lines (703 loc) · 28.6 KB
/
external_process.cpp
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
/**
* @file external_process.cpp
*
* @brief ExternalProcess class implementation.
*
*/
#include "external_process.hpp"
#include <windows.h>
#include <tlhelp32.h>
// #define SHOW_ERRORS
#ifdef SHOW_ERRORS
#include <iostream>
#include <string>
static std::string get_winapi_error_text(BOOL error_id);
#define SHOW_WINAPI_ERROR(error_code) \
std::cerr << std::endl << "Error: " << error_code << std::endl; \
std::cerr << " File: " << __FILE__ << std::endl; \
std::cerr << " Line: " << std::dec << __LINE__ << std::endl; \
std::cerr << " Function: " << __func__ << std::endl; \
std::cerr << " Message: " << get_winapi_error_text(error_code) \
<< std::endl \
<< std::endl;
#define WINAPI_CALL(call) \
call; \
do \
{ \
DWORD ________last_error = GetLastError(); \
SetLastError(0); \
if (________last_error) \
{ \
SHOW_WINAPI_ERROR(________last_error); \
} \
} while (0)
#else
#define WINAPI_CALL(call) call
#endif /* SHOW_ERRORS */
using namespace P1ExternalProcess;
typedef NTSTATUS(NTAPI *tNtReadVirtualMemory)(
IN HANDLE ProcessHandle, IN PVOID BaseAddress, OUT PVOID buffer,
IN ULONG NumberOfBytesRead, OUT PULONG NumberOfBytesReaded OPTIONAL);
typedef NTSTATUS(NTAPI *tNtWriteVirtualMemory)(
IN HANDLE ProcessHandle, IN PVOID BaseAddress, IN PVOID buffer,
IN ULONG NumberOfBytesToWrite, OUT PULONG NumberOfBytesWritten OPTIONAL);
typedef NTSTATUS(NTAPI *tNtAllocateVirtualMemory)(
IN HANDLE ProcessHandle, IN OUT PVOID *BaseAddress, IN ULONG_PTR ZeroBits,
IN OUT PSIZE_T RegionSize, IN ULONG AllocationType, IN ULONG Protect);
typedef NTSTATUS(NTAPI *tNtFreeVirtualMemory)(IN HANDLE ProcessHandle,
IN OUT PVOID *BaseAddress,
IN OUT PSIZE_T RegionSize,
IN ULONG FreeType);
typedef NTSTATUS(NTAPI *tNtProtectVirtualMemory)(
IN HANDLE ProcessHandle, IN OUT PVOID *BaseAddress,
IN OUT PULONG NumberOfBytesToProtect, IN ULONG NewAccessProtection,
OUT PULONG OldAccessProtection);
static tNtReadVirtualMemory NtReadVirtualMemory =
(tNtReadVirtualMemory)(void *)GetProcAddress(LoadLibrary("ntdll.dll"),
"NtReadVirtualMemory");
static tNtWriteVirtualMemory NtWriteVirtualMemory =
(tNtWriteVirtualMemory)(void *)GetProcAddress(LoadLibrary("ntdll.dll"),
"NtWriteVirtualMemory");
static tNtAllocateVirtualMemory NtAllocateVirtualMemory =
(tNtAllocateVirtualMemory)(void *)GetProcAddress(LoadLibrary("ntdll.dll"),
"NtAllocateVirtualMemory");
static tNtFreeVirtualMemory NtFreeVirtualMemory =
(tNtFreeVirtualMemory)(void *)GetProcAddress(LoadLibrary("ntdll.dll"),
"NtFreeVirtualMemory");
static tNtProtectVirtualMemory NtProtectVirtualMemory =
(tNtProtectVirtualMemory)(void *)GetProcAddress(LoadLibrary("ntdll.dll"),
"NtProtectVirtualMemory");
#ifdef SHOW_ERRORS
static std::string get_winapi_error_text(BOOL error_id)
{
if (!error_id)
return "";
LPSTR error_msg_buffer = nullptr;
size_t size = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, error_id, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&error_msg_buffer, 0, NULL);
std::string error_msg(error_msg_buffer, size);
LocalFree(error_msg_buffer);
return error_msg;
}
#endif /* SHOW_ERRORS */
ExternalProcess::ExternalProcess(uint32_t process_id) : _process_id(process_id)
{
_handle = WINAPI_CALL(OpenProcess(PROCESS_ALL_ACCESS, FALSE, process_id));
}
ExternalProcess::ExternalProcess(const char *process_name)
: ExternalProcess(get_process_id_by_process_name(process_name))
{
}
ExternalProcess::~ExternalProcess()
{
for (auto &i : _callers)
{
free(i.second.caller_address); // TODO: Implement ~caller_destroyer.
}
for (auto &i : _injected_code)
{
uninject_code(i.first);
}
for (auto i = _allocated_memory.cbegin(), n = i;
i != _allocated_memory.cend(); i = n)
{
++n;
free(i->first);
}
for (auto &i : _virtual_protect)
{
restore_virtual_protect(i.first);
}
WINAPI_CALL(CloseHandle((HANDLE)_handle));
}
void ExternalProcess::read_buf(uintptr_t address, size_t size,
void *out_result) const
{
// ReadProcessMemory(static_cast<HANDLE>(_handle),
// reinterpret_cast<LPCVOID>(address), out_result, size,
// 0);
NtReadVirtualMemory(static_cast<HANDLE>(_handle),
reinterpret_cast<PVOID>(address), out_result, size, 0);
}
void ExternalProcess::write_buf(uintptr_t address, size_t size,
const void *data) const
{
// WriteProcessMemory(static_cast<HANDLE>(_handle),
// reinterpret_cast<LPVOID>(address), data, size, 0);
NtWriteVirtualMemory(static_cast<HANDLE>(_handle),
reinterpret_cast<PVOID>(address),
const_cast<PVOID>(data), size, 0);
}
uintptr_t ExternalProcess::alloc(const size_t size)
{
// TODO: Add rights as function argument.
// void *address =
// VirtualAllocEx(static_cast<HANDLE>(_handle), NULL, size,
// MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
void *address = NULL;
SIZE_T sz = size;
NtAllocateVirtualMemory(static_cast<HANDLE>(_handle), &address, 0, &sz,
MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (address != NULL)
{
_allocated_memory[reinterpret_cast<uintptr_t>(address)] = size;
}
return reinterpret_cast<uintptr_t>(address);
}
void ExternalProcess::free(uintptr_t address)
{
auto result = _allocated_memory.find(address);
if (result != _allocated_memory.end())
{
// VirtualFreeEx((HANDLE)_handle, reinterpret_cast<void *>(address),
// result->second, MEM_RELEASE);
PVOID addr = reinterpret_cast<PVOID>(address);
SIZE_T sz = 0;
NtFreeVirtualMemory(static_cast<HANDLE>(_handle), &addr, &sz,
MEM_RELEASE);
_allocated_memory.erase(address);
_virtual_protect.erase(address);
}
}
void ExternalProcess::set_virtual_protect(uintptr_t address, size_t size,
enVirtualProtect type)
{
ULONG old_protect = 0;
PVOID addr = reinterpret_cast<PVOID>(address);
ULONG sz = size;
ULONG protect = 0;
if (type == (enVirtualProtect::READ | enVirtualProtect::WRITE |
enVirtualProtect::EXECUTE))
{
protect = PAGE_READWRITE;
}
else if (type == (enVirtualProtect::READ & enVirtualProtect::WRITE))
{
protect = PAGE_READWRITE; // TODO: Not PAGE_READWRITE
}
else if (type == (enVirtualProtect::READ & enVirtualProtect::EXECUTE))
{
protect = PAGE_EXECUTE_READ;
}
else if (type == enVirtualProtect::READ)
{
protect = PAGE_READONLY;
}
else if (type == enVirtualProtect::NOACCESS)
{
protect = PAGE_NOACCESS;
}
NtProtectVirtualMemory(static_cast<HANDLE>(_handle), &addr, &sz, protect,
&old_protect);
auto original_vp = _virtual_protect.find(address);
if (original_vp == _virtual_protect.end())
{
_virtual_protect[address] = {size, old_protect};
}
else if (original_vp->second.size < size)
{
original_vp->second.size = size;
}
}
void ExternalProcess::restore_virtual_protect(uintptr_t address)
{
auto vp = _virtual_protect.find(address);
if (vp != _virtual_protect.end())
{
ULONG old_protect;
PVOID addr = reinterpret_cast<PVOID>(address);
ULONG sz;
NtProtectVirtualMemory(static_cast<HANDLE>(_handle), &addr, &sz,
vp->second.original_protect, &old_protect);
_virtual_protect.erase(address);
}
}
uintptr_t ExternalProcess::get_module_address(const char *module_name)
{
// TODO: Store modules addresses in map.
uintptr_t result = 0;
HANDLE snapshot_handle = WINAPI_CALL(CreateToolhelp32Snapshot(
TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, _process_id));
if (snapshot_handle != INVALID_HANDLE_VALUE)
{
MODULEENTRY32 module_entry;
module_entry.dwSize = sizeof(module_entry);
if (Module32First(snapshot_handle, &module_entry))
{
do
{
if (!_strcmpi(module_entry.szModule, module_name))
{
result =
reinterpret_cast<uintptr_t>(module_entry.modBaseAddr);
break;
}
} while (Module32Next(snapshot_handle, &module_entry));
}
}
WINAPI_CALL(CloseHandle(snapshot_handle));
return result;
}
uintptr_t ExternalProcess::call_cdecl_function(uintptr_t address, size_t argc,
...)
{
if (_callers.find(address) == _callers.end())
{
auto &caller = _callers[address];
/* Fill in the caller info structure */
caller.function_address = address;
caller.caller_address = build_cdecl_caller(address, argc);
caller.argc = argc;
caller.cc = enCallConvention::ECC_CDECL;
}
// TODO: Optimize: store last args and send new only if there are diffs.
send_external_caller_arguments(_callers[address],
reinterpret_cast<uintptr_t>(&argc) + 4);
return call_external_function(_callers[address]);
}
uintptr_t ExternalProcess::call_stdcall_function(uintptr_t address, size_t argc,
...)
{
if (_callers.find(address) == _callers.end())
{
auto &caller = _callers[address];
/* Fill in the caller info structure */
caller.function_address = address;
caller.caller_address = build_stdcall_caller(address, argc);
caller.argc = argc;
caller.cc = enCallConvention::ECC_STDCALL;
}
// TODO: Optimize: store last args and send new only if there are diffs.
send_external_caller_arguments(_callers[address],
reinterpret_cast<uintptr_t>(&argc) + 4);
return call_external_function(_callers[address]);
}
uintptr_t ExternalProcess::call_thiscall_function(uintptr_t address,
uintptr_t this_ptr,
size_t argc, ...)
{
if (_callers.find(address) == _callers.end())
{
auto &caller = _callers[address];
/* Fill in the caller info structure */
caller.function_address = address;
caller.caller_address = build_thiscall_caller(address, argc);
caller.argc = argc;
caller.cc = enCallConvention::ECC_THISCALL;
}
// TODO: Optimize: store last args and send new only if there are diffs.
send_external_caller_arguments(_callers[address],
reinterpret_cast<uintptr_t>(&argc) + 4);
send_thiscall_this_ptr(_callers[address], this_ptr);
return call_external_function(_callers[address]);
}
void ExternalProcess::inject_code(uintptr_t address, const uint8_t *bytes,
size_t bytes_size,
size_t overwrite_bytes_size,
enInjectionType it)
{
// TODO: Implement call-based injector.
// TODO: Support injecting and uninjecting multiple code-injections in the
// same address.
/* If colde has not already injected on @address address */
if (_injected_code.find(address) == _injected_code.end())
{
uintptr_t result = 0;
switch (it)
{
case enInjectionType::EIT_JMP:
result = inject_code_using_jmp(address, bytes, bytes_size,
overwrite_bytes_size);
break;
case enInjectionType::EIT_PUSHRET:
result = inject_code_using_push_ret(address, bytes, bytes_size,
overwrite_bytes_size);
break;
}
if (result)
{
InjectedCodeInfo ici;
ici.injected_bytes_number = bytes_size;
ici.overwritten_bytes_number = overwrite_bytes_size;
ici.allocated_buffer = result;
_injected_code[address] = ici;
}
}
}
void ExternalProcess::uninject_code(uintptr_t address)
{
auto ici = _injected_code.find(address);
if (ici != _injected_code.end())
{
/* Set vp */
set_virtual_protect(address, ici->second.overwritten_bytes_number,
enVirtualProtect::READ_WRITE_EXECUTE);
/* Create buffer to store original bytes */
uint8_t *original_bytes =
new uint8_t[ici->second.overwritten_bytes_number];
/* Read original bytes to this buffer */
read_buf(ici->second.allocated_buffer +
ici->second.injected_bytes_number,
ici->second.overwritten_bytes_number, original_bytes);
/* Restore original bytes */
write_buf(ici->first, ici->second.overwritten_bytes_number,
original_bytes);
/* Restore vp */
restore_virtual_protect(address);
delete[] original_bytes;
free(ici->second.allocated_buffer);
_injected_code.erase(address);
}
}
void ExternalProcess::patch(uintptr_t address, const uint8_t *bytes,
size_t size)
{
// TODO: Store patched bytes to be able to unpatch later.
/* Set vp */
set_virtual_protect(address, size, enVirtualProtect::READ_WRITE_EXECUTE);
/* Write bytes */
write_buf(address, size, bytes);
/* Restore vp */
restore_virtual_protect(address);
}
uintptr_t ExternalProcess::find_signature(uintptr_t address, size_t size,
const uint8_t *signature,
const char *mask) const
{
if (!signature)
{
return 0;
}
if (!mask)
{
return 0;
}
if (strlen(mask) > size)
{
return 0;
}
uint8_t *buffer = new uint8_t[size];
read_buf(address, size, buffer);
uintptr_t result = 0;
for (size_t i = 0; i <= size - strlen(mask); i++)
{
uintptr_t mask_offset = 0;
while (mask[mask_offset])
{
if (mask[mask_offset] == 'x' &&
buffer[i + mask_offset] != signature[mask_offset])
{
mask_offset = 0;
break;
}
++mask_offset;
}
if (mask_offset != 0)
{
result = address + i;
break;
}
}
delete[] buffer;
return result;
}
uint32_t ExternalProcess::get_process_id_by_process_name(
const char *process_name) const
{
uint32_t result = 0;
HANDLE snapshot_handle =
WINAPI_CALL(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
if (snapshot_handle != INVALID_HANDLE_VALUE)
{
PROCESSENTRY32 process_entry;
process_entry.dwSize = sizeof(process_entry);
if (Process32First(snapshot_handle, &process_entry))
{
do
{
if (!_strcmpi(process_entry.szExeFile, process_name))
{
result = process_entry.th32ProcessID;
break;
}
} while (Process32Next(snapshot_handle, &process_entry));
}
}
WINAPI_CALL(CloseHandle(snapshot_handle));
return result;
}
void ExternalProcess::provide_debug_access(void)
{
WINAPI_CALL(OpenProcessToken(static_cast<HANDLE>(_handle),
TOKEN_ADJUST_PRIVILEGES, &_dbg_handle));
TOKEN_PRIVILEGES tp;
LUID luid;
WINAPI_CALL(LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid));
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
WINAPI_CALL(AdjustTokenPrivileges(_dbg_handle, FALSE, &tp,
sizeof(TOKEN_PRIVILEGES),
(PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL));
}
uintptr_t ExternalProcess::build_cdecl_caller(uintptr_t address, size_t argc)
{
/* Calculate caller size */
size_t caller_size =
(1 + 4) * argc + /* (push-instruction size + argument size) * argc */
1 + 4 + /* call-instruction size + address size */
3 + /* restore-stack-instructions size */
1; /* ret-instruction size */
/* Allocate space for the caller in the remote process's address space */
uintptr_t caller_address = alloc(caller_size);
/* Create local buffer to be sent in remote process */
uint8_t *local_caller_buffer = new uint8_t[caller_size];
/* Write push-args instructions in the caller. */
for (size_t i = 0; i < argc; i++)
{
local_caller_buffer[i * 5] = 0x68; /* push */
/* argument */
*reinterpret_cast<uintptr_t *>(local_caller_buffer + i * 5 + 1) = 0;
}
/* Write call-instruction to the caller bytes */
local_caller_buffer[argc * 5] = 0xe8; /* call */
/* Calculate and write an address for the near call instruction */
*reinterpret_cast<uintptr_t *>(local_caller_buffer + argc * 5 + 1) =
address - (caller_address + argc * 5) - 5;
/* Write restore-stack-instructions to the caller bytes */
local_caller_buffer[argc * 5 + 5] = 0x83; /* add */
local_caller_buffer[argc * 5 + 6] = 0xc4; /* esp */
local_caller_buffer[argc * 5 + 7] = 4 * argc; /* args size */
/* Write ret-instruction to the caller bytes */
local_caller_buffer[argc * 5 + 8] = 0xc3; /* ret */
/* Write caller bytes to the remote process's memory */
write_buf(caller_address, caller_size, local_caller_buffer);
delete[] local_caller_buffer;
return caller_address;
}
uintptr_t ExternalProcess::build_stdcall_caller(uintptr_t address, size_t argc)
{
/* Calculate caller size */
size_t caller_size =
(1 + 4) * argc + /* (push-instruction size + argument size) * argc */
1 + 4 + /* call-instruction size + address size */
1; /* ret-instruction size */
/* Allocate space for the caller in the remote process's address space */
uintptr_t caller_address = alloc(caller_size);
/* Create local buffer to be sent in remote process */
uint8_t *local_caller_buffer = new uint8_t[caller_size];
/* Write push-args instructions in the caller. */
for (size_t i = 0; i < argc; i++)
{
local_caller_buffer[i * 5] = 0x68; /* push */
/* argument */
*reinterpret_cast<uintptr_t *>(local_caller_buffer + i * 5 + 1) = 0;
}
/* Write call-instruction to the caller bytes */
local_caller_buffer[argc * 5] = 0xe8; /* call */
/* Calculate and write an address for the near call instruction */
*reinterpret_cast<uintptr_t *>(local_caller_buffer + argc * 5 + 1) =
address - (caller_address + argc * 5) - 5;
/* Write ret-instruction to the caller bytes */
local_caller_buffer[argc * 5 + 5] = 0xc3; /* ret */
/* Write caller bytes to the remote process's memory */
write_buf(caller_address, caller_size, local_caller_buffer);
delete[] local_caller_buffer;
return caller_address;
}
uintptr_t ExternalProcess::build_thiscall_caller(uintptr_t address, size_t argc)
{
/* Calculate caller size */
size_t caller_size =
(1 + 4) * argc + /* (push-instruction size + argument size) * argc */
5 + /* mov _this to ecx */
1 + 4 + /* call-instruction size + address size */
1; /* ret-instruction size */
/* Allocate space for the caller in the remote process's address space */
uintptr_t caller_address = alloc(caller_size);
/* Create local buffer to be sent in remote process */
uint8_t *local_caller_buffer = new uint8_t[caller_size];
/* Write push-args instructions in the caller. */
for (size_t i = 0; i < argc; i++)
{
local_caller_buffer[i * 5] = 0x68; /* push */
/* argument */
*reinterpret_cast<uintptr_t *>(local_caller_buffer + i * 5 + 1) = 0;
}
/* Write [mov ecx, _this] to the caller bytes */
local_caller_buffer[argc * 5] = 0xB9; /* mov ecx, */
*reinterpret_cast<uintptr_t *>(local_caller_buffer + argc * 5 + 1) = 0;
/* Write call-instruction to the caller bytes */
local_caller_buffer[argc * 5 + 5] = 0xe8; /* call */
/* Calculate and write an address for the near call instruction */
*reinterpret_cast<uintptr_t *>(local_caller_buffer + argc * 5 + 5 + 1) =
address - (caller_address + argc * 5 + 5) - 5;
/* Write ret-instruction to the caller bytes */
local_caller_buffer[argc * 5 + 5 + 5] = 0xc3; /* ret */
/* Write caller bytes to the remote process's memory */
write_buf(caller_address, caller_size, local_caller_buffer);
delete[] local_caller_buffer;
return caller_address;
}
void ExternalProcess::send_external_caller_arguments(ExternalCaller const &ec,
uintptr_t args, ...)
{
// for (uint32_t i = 0; i < ec.argc; i++)
//{
// write<uint32_t>(ec.caller_address + i * 5 + 1,
// *(((uint32_t *)args) + ec.argc - i - 1));
//}
/* Local buffer to build code for pushing arguments onto stack */
uint8_t *push_args_block = new uint8_t[ec.argc * 5];
/* Write push-args instructions in the local buffer */
for (size_t i = 0; i < ec.argc; i++)
{
push_args_block[i * 5] = 0x68; /* push */
/* argument */
*reinterpret_cast<uintptr_t *>(push_args_block + i * 5 + 1) =
*(((uintptr_t *)args) + ec.argc - i - 1);
}
/* Write the local buffer in caller @ec in the external process */
write_buf(ec.caller_address, ec.argc * 5, push_args_block);
delete[] push_args_block;
}
void ExternalProcess::send_thiscall_this_ptr(const ExternalCaller &ec,
uintptr_t this_ptr)
{
if (ec.cc == enCallConvention::ECC_THISCALL)
{
write<uintptr_t>(ec.caller_address + ec.argc * 5 + 1, this_ptr);
}
return;
}
uintptr_t ExternalProcess::call_external_function(
const ExternalProcess::ExternalCaller &ec) const
{
/* Create a thread in the remote process */
HANDLE thread_handle = WINAPI_CALL(CreateRemoteThread(
static_cast<HANDLE>(_handle), NULL, 0,
reinterpret_cast<LPTHREAD_START_ROUTINE>(ec.caller_address), NULL, 0,
NULL));
/* Wait for a return from the function */
while (WaitForSingleObject(thread_handle, 0) != 0)
{
}
uintptr_t result = 0;
/* Get the returned value */
WINAPI_CALL(
GetExitCodeThread(thread_handle, reinterpret_cast<LPDWORD>(&result)));
WINAPI_CALL(CloseHandle(thread_handle));
return reinterpret_cast<uintptr_t>(result);
}
uintptr_t ExternalProcess::inject_code_using_jmp(uintptr_t address,
const uint8_t *bytes,
size_t size,
size_t overwrite_bytes_size)
{
if (overwrite_bytes_size < 5) /* should be at least 5 bytes for jmp */
{
return 0;
}
/* Set vp */
set_virtual_protect(address, overwrite_bytes_size,
enVirtualProtect::READ_WRITE_EXECUTE);
/* Store original bytes to be overwritten */
uint8_t *original_bytes = new uint8_t[overwrite_bytes_size];
read_buf(address, overwrite_bytes_size, original_bytes);
/* Calculate whole remote process buffer size */
size_t allocated_buf_size = size + overwrite_bytes_size + 5;
/* Allocate memory for remote buffer */
uintptr_t allocated_buf = alloc(allocated_buf_size);
/* Create local buffer to be sent in remote process' buf */
uint8_t *local_buf = new uint8_t[allocated_buf_size];
/* Fill local buffer with data */
/* Put injected bytes in local buffer */
memcpy(local_buf, bytes, size);
/* Put original bytes in local buffer */
memcpy(local_buf + size, original_bytes, overwrite_bytes_size);
/* Put jmp instruction in local buffer */
local_buf[size + overwrite_bytes_size] = 0xE9;
/* Put jmp address in local buffer */
*reinterpret_cast<uintptr_t *>(
&(local_buf[size + overwrite_bytes_size + 1])) =
(address + overwrite_bytes_size) -
(allocated_buf + size + overwrite_bytes_size) - 5;
/* Write local buffer in remote process' allocated buffer */
write_buf(allocated_buf, allocated_buf_size, local_buf);
/* Overwrite original bytes */
/* Don't need local_buf anymore, so it can be used here */
size_t nops_number = overwrite_bytes_size - 5;
if (nops_number > 0)
{
memset(local_buf, 0x90, nops_number); /* Put nops in local buffer */
}
local_buf[nops_number] = 0xE9; /* Put jmp instruction in local buffer */
/* Put jmp address in local buffer */
*reinterpret_cast<uintptr_t *>(&(local_buf[nops_number + 1])) =
allocated_buf - (address + nops_number) - 5;
/* Write local buffer in remote process */
write_buf(address, overwrite_bytes_size, local_buf);
/* Restore vp */
restore_virtual_protect(address);
delete[] original_bytes;
delete[] local_buf;
return allocated_buf;
}
uintptr_t ExternalProcess::inject_code_using_push_ret(
uintptr_t address, const uint8_t *bytes, size_t bytes_size,
size_t overwrite_bytes_size)
{
if (overwrite_bytes_size < 6) /* should be at least 6 bytes for push-ret */
{
return 0;
}
/* Set vp */
set_virtual_protect(address, overwrite_bytes_size,
enVirtualProtect::READ_WRITE_EXECUTE);
/* Store original bytes to be overwritten */
uint8_t *original_bytes = new uint8_t[overwrite_bytes_size];
read_buf(address, overwrite_bytes_size, original_bytes);
/* Calculate whole remote process buffer size */
size_t allocated_buf_size = bytes_size + overwrite_bytes_size + 5;
/* Allocate memory for remote buffer */
uintptr_t allocated_buf = alloc(allocated_buf_size);
/* Create local buffer to be sent in remote process' buf */
uint8_t *local_buf = new uint8_t[allocated_buf_size];
/* Fill local buffer with data */
/* Put injected bytes in local buffer */
memcpy(local_buf, bytes, bytes_size);
/* Put original bytes in local buffer */
memcpy(local_buf + bytes_size, original_bytes, overwrite_bytes_size);
/* Put jmp instruction in local buffer */
local_buf[bytes_size + overwrite_bytes_size] = 0xE9;
/* Put jmp address in local buffer */
*reinterpret_cast<uintptr_t *>(
&(local_buf[bytes_size + overwrite_bytes_size + 1])) =
(address + overwrite_bytes_size) -
(allocated_buf + bytes_size + overwrite_bytes_size) - 5;
/* Write local buffer in remote process' allocated buffer */
write_buf(allocated_buf, allocated_buf_size, local_buf);
/* Overwrite original bytes */
/* Don't need local_buf anymore, so it can be used here */
size_t nops_number = overwrite_bytes_size - 6;
if (nops_number > 0)
{
memset(local_buf, 0x90, nops_number); /* Put nops in local buffer */
}
local_buf[nops_number] = 0x68; /* Put push instruction in local buffer */
/* Put return address in local buffer */
*reinterpret_cast<uintptr_t *>(&(local_buf[nops_number + 1])) =
allocated_buf;
/* Put ret instruction in local buffer */
local_buf[nops_number + 5] = 0xC3;
/* Write local buffer in remote process */
write_buf(address, overwrite_bytes_size, local_buf);
/* Restore vp */
restore_virtual_protect(address);
delete[] original_bytes;
delete[] local_buf;
return allocated_buf;
}