forked from Hackerl/python-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
153 lines (115 loc) · 4.32 KB
/
main.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
#include <unistd.h>
#include <zero/log.h>
#include <zero/cmdline.h>
#include <zero/proc/process.h>
#include <elfio/elfio.hpp>
#include <sys/user.h>
#include <elf.h>
#include <trap/trap.h>
using EvalFramePtr = void *(*)(void *, void *, void *);
using EvalStringPtr = int (*)(const char *);
constexpr auto UWSGI_IMAGE = "uwsgi";
constexpr auto PYTHON_IMAGE = "bin/python";
constexpr auto PYTHON_LIBRARY_IMAGE = "libpython";
constexpr auto EVAL_STRING_SYMBOL = "PyRun_SimpleString";
constexpr auto EVAL_FRAME_SYMBOL = "PyEval_EvalFrameEx";
constexpr auto EVAL_FRAME_DEFAULT_SYMBOL = "_PyEval_EvalFrameDefault";
static EvalFramePtr origin = nullptr;
static EvalStringPtr eval = nullptr;
static int guard = 0;
static char code[10240] = {};
void *entry(void *x, void *y, void *z) {
if (!guard && __sync_bool_compare_and_swap(&guard, 0, 1))
eval(code);
return origin(x, y, z);
}
int main(int argc, char ** argv) {
INIT_CONSOLE_LOG(zero::ERROR);
zero::CCmdline cmdline;
cmdline.add({"script", "python script", zero::value<std::string>()});
cmdline.addOptional({"file", 'f', "load script from file", zero::value<bool>(), true});
cmdline.parse(argc, argv);
bool file = cmdline.getOptional<bool>("file");
std::string script = cmdline.get<std::string>("script");
if (file) {
std::ifstream stream(script);
if (!stream.is_open()) {
LOG_ERROR("script does not exist");
return -1;
}
script = {std::istreambuf_iterator<char>(stream), std::istreambuf_iterator<char>()};
}
if (script.length() >= sizeof(code)) {
LOG_ERROR("script length limit");
return -1;
}
strcpy(code, script.data());
pid_t pid = getpid();
zero::proc::CProcessMapping processMapping;
if (
!zero::proc::getImageBase(pid, PYTHON_LIBRARY_IMAGE, processMapping) &&
!zero::proc::getImageBase(pid, PYTHON_IMAGE, processMapping) &&
!zero::proc::getImageBase(pid, UWSGI_IMAGE, processMapping)
) {
LOG_ERROR("can't find python image base");
return -1;
}
LOG_INFO("python image base: 0x%lx", processMapping.start);
std::string path = zero::filesystem::path::join("/proc/self/root", processMapping.pathname);
ELFIO::elfio reader;
if (!reader.load(path)) {
LOG_ERROR("open elf failed: %s", path.c_str());
return -1;
}
auto it = std::find_if(
reader.sections.begin(),
reader.sections.end(),
[](const auto& s) {
return s->get_type() == SHT_DYNSYM;
});
if (it == reader.sections.end()) {
LOG_ERROR("can't find symbol section");
return -1;
}
unsigned long base = 0;
if (reader.get_type() != ET_EXEC) {
std::vector<ELFIO::segment *> loads;
std::copy_if(
reader.segments.begin(),
reader.segments.end(),
std::back_inserter(loads),
[](const auto &i){
return i->get_type() == PT_LOAD;
});
auto minElement = std::min_element(
loads.begin(),
loads.end(),
[](const auto &i, const auto &j) {
return i->get_virtual_address() < j->get_virtual_address();
});
base = processMapping.start - ((*minElement)->get_virtual_address() & ~(PAGE_SIZE - 1));
}
ELFIO::symbol_section_accessor symbols(reader, *it);
ELFIO::Elf64_Addr value = 0;
ELFIO::Elf_Xword size = 0;
unsigned char bind = 0;
unsigned char type = 0;
ELFIO::Elf_Half section = 0;
unsigned char other = 0;
if (!symbols.get_symbol(EVAL_STRING_SYMBOL, value, size, bind, type, section, other)) {
LOG_ERROR("find eval string symbol failed");
return -1;
}
eval = (EvalStringPtr)(base + value);
if (!symbols.get_symbol(EVAL_FRAME_DEFAULT_SYMBOL, value, size, bind, type, section, other) &&
!symbols.get_symbol(EVAL_FRAME_SYMBOL, value, size, bind, type, section, other)) {
LOG_ERROR("find eval frame symbol failed");
return -1;
}
LOG_INFO("eval frame address: 0x%lx", base + value);
if (hook((void *)(base + value), (void *)entry, (void **)&origin) < 0) {
LOG_ERROR("hook eval frame failed");
return -1;
}
return 0;
}