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
| #include <common/log.h> #include <common/cmdline.h> #include <common/utils/process.h> #include <elfio/elfio.hpp>
typedef int (*PFN_RUN)(const char *command); typedef int (*PFN_ENSURE)(); typedef void (*PFN_RELEASE)(int);
constexpr auto PYTHON = "bin/python"; constexpr auto PYTHON_LIBRARY = "libpython"; constexpr auto PYTHON_CALLER = "python_caller";
int main(int argc, char ** argv) { cmdline::parser parse;
parse.add<int>("pid", 'p', "pid", true, 0); parse.add<std::string>("source", 's', "python source file", true, ""); parse.add<std::string>("pangolin", '\0', "pangolin path", true, ""); parse.add("file", '\0', "pass source by file");
parse.parse_check(argc, argv);
int pid = parse.get<int>("pid");
CProcessMap processMap;
if ( !CProcess::getFileMemoryBase(pid, PYTHON_LIBRARY, processMap) && !CProcess::getFileMemoryBase(pid, PYTHON, processMap) ) { LOG_ERROR("find target failed"); return -1; }
LOG_INFO("find target: 0x%lx -> %s", processMap.start, processMap.file.c_str());
ELFIO::elfio reader;
if (!reader.load(processMap.file)) { LOG_ERROR("open elf failed: %s", processMap.file.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 baseAddress = 0;
if (reader.get_type() != ET_EXEC) { auto sit = std::find_if( reader.segments.begin(), reader.segments.end(), [](const auto& s) { return s->get_type() == PT_LOAD; });
if (sit == reader.segments.end()) { LOG_ERROR("can't find load segment"); return -1; }
baseAddress = processMap.start - (*sit)->get_virtual_address(); }
PFN_ENSURE pfnEnsure = nullptr; PFN_RUN pfnRun = nullptr; PFN_RELEASE pfnRelease = nullptr;
ELFIO::symbol_section_accessor symbols(reader, *it);
for (ELFIO::Elf_Xword i = 0; i < symbols.get_symbols_num(); i++) { std::string name; 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(i, name, value, size, bind, type, section, other)) { LOG_ERROR("get symbol %lu failed", i); return -1; }
if (name == "PyGILState_Ensure") pfnEnsure = (PFN_ENSURE)(baseAddress + value); else if (name == "PyRun_SimpleString") pfnRun = (PFN_RUN)(baseAddress + value); else if (name == "PyGILState_Release") pfnRelease = (PFN_RELEASE)(baseAddress + value); }
if (!pfnEnsure || !pfnRun || !pfnRelease) { LOG_ERROR("can't find python symbols"); return -1; }
LOG_INFO("ensure func: %p run func: %p release func: %p", pfnEnsure, pfnRun, pfnRelease);
std::string source = parse.get<std::string>("source"); std::string pangolin = parse.get<std::string>("pangolin"); std::string caller = CPath::join(CPath::getAPPDir(), PYTHON_CALLER);
char callerCommand[1024] = {};
snprintf( callerCommand, sizeof(callerCommand), "%s %s %d %p %p %p", caller.c_str(), source.c_str(), parse.exist("file"), pfnEnsure, pfnRun, pfnRelease );
int err = execl( pangolin.c_str(), pangolin.c_str(), "-c", callerCommand, "-p", std::to_string(pid).c_str(), nullptr );
if (err < 0) { LOG_ERROR("exec pangolin failed: %s", strerror(errno)); return -1; }
return 0; }
|