-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathshellcode.cpp
More file actions
74 lines (68 loc) · 2.3 KB
/
shellcode.cpp
File metadata and controls
74 lines (68 loc) · 2.3 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
#include <dlfcn.h>
#include <mach/mach.h>
#include <pthread_spis.h>
#include <stdio.h>
#include <stdlib.h>
extern "C" {
struct rmain_arg { // dealloc
size_t sizeofstruct;
const char* name; // dealloc
thread_t injectThread; // kill this tread
decltype(&dlsym) dlsym;
char entrypoint[256];
void* get_func(const char* func_name) {
return this->dlsym(RTLD_DEFAULT, func_name);
}
};
static void* rmain(void* ptr) {
if (!ptr) {
return 0;
}
rmain_arg& arg = *(rmain_arg*)ptr;
// terminate inject thread
if (arg.injectThread) {
auto _thread_suspend = (decltype(&thread_suspend))arg.get_func("thread_suspend");
auto _thread_terminate = (decltype(&thread_terminate))arg.get_func("thread_terminate");
if (_thread_suspend && _thread_terminate) {
_thread_suspend(arg.injectThread);
_thread_terminate(arg.injectThread);
}
}
void* handler = nullptr;
auto _dlerror = (decltype(&dlerror))arg.get_func("dlerror");
// load lib
{
auto _dlopen = (decltype(&dlopen))arg.get_func("dlopen");
handler = _dlopen((const char*)arg.name, RTLD_LAZY | RTLD_LOCAL);
}
if (!handler) {
auto ec = (const char*)_dlerror();
auto _fprintf = (decltype(&fprintf))arg.get_func("fprintf");
auto _stderr = *(FILE**)arg.get_func("__stderrp");
_fprintf(_stderr, "%s\n", ec);
auto _exit = (decltype(&exit))arg.get_func("exit");
_exit(1);
}
else {
void (*func)();
func = (decltype(func))arg.dlsym(handler, arg.entrypoint);
if (func)
func();
}
// delete memory
{
auto _vm_deallocate = (decltype(&vm_deallocate))arg.get_func("vm_deallocate");
auto task_self_ = *(decltype(mach_task_self_)*)arg.get_func("mach_task_self_");
_vm_deallocate(task_self_, (intptr_t)ptr, arg.sizeofstruct);
}
return 0;
}
void inject [[noreturn]] (rmain_arg* ptr, decltype(&pthread_create_from_mach_thread) pthread_create_from_mach_thread, decltype(&swtch_pri) swtch_pri, decltype(&mach_thread_self) mach_thread_self) {
ptr->injectThread = mach_thread_self();
pthread_t thread;
pthread_create_from_mach_thread(&thread, NULL, rmain, ptr);
while (1) {
swtch_pri(0);
}
}
}