MCPcopy Create free account
hub / github.com/ELMERIKH/PyinMemoryPE / generate_python_exec_shellcode_32

Function generate_python_exec_shellcode_32

windows/injection.py:182–237  ·  view source on GitHub ↗
(target, PyDll)

Source from the content-addressed store, hash-verified

180python_function_32_bits = {}
181
182def generate_python_exec_shellcode_32(target, PyDll):
183 pymodule = [mod for mod in target.peb.modules if mod.name == PyDll][0]
184 base = pymodule.baseaddr
185 if not python_function_32_bits:
186 Py_exports = pymodule.pe.exports
187 python_function_32_bits["PyEval_InitThreads"] = Py_exports["PyEval_InitThreads"] - base
188 python_function_32_bits["Py_IsInitialized"] = Py_exports["Py_IsInitialized"] - base
189 python_function_32_bits["PyGILState_Release"] = Py_exports["PyGILState_Release"] - base
190 python_function_32_bits["PyGILState_Ensure"] = Py_exports["PyGILState_Ensure"] - base
191 python_function_32_bits["PyEval_SaveThread"] = Py_exports["PyEval_SaveThread"] - base
192 python_function_32_bits["Py_Initialize"] = Py_exports["Py_Initialize"] - base
193 python_function_32_bits["PyRun_SimpleString"] = Py_exports["PyRun_SimpleString"] - base
194 Py_exports = python_function_32_bits
195 PyEval_InitThreads = Py_exports["PyEval_InitThreads"] + base
196 Py_IsInitialized = Py_exports["Py_IsInitialized"] + base
197 PyGILState_Release = Py_exports["PyGILState_Release"] + base
198 PyGILState_Ensure = Py_exports["PyGILState_Ensure"] + base
199 PyEval_SaveThread = Py_exports["PyEval_SaveThread"] + base
200 Py_Initialize = Py_exports["Py_Initialize"] + base
201 PyRun_SimpleString = Py_exports["PyRun_SimpleString"] + base
202
203 code = x86.MultipleInstr()
204 code += x86.Mov('EAX', Py_IsInitialized)
205 code += x86.Call('EAX')
206 code += x86.Mov("EDI", "EAX")
207 code += x86.Cmp("EAX", 0)
208 code += x86.Jnz(":DO_ENSURE")
209 code += x86.Mov('EAX', Py_Initialize)
210 code += x86.Call('EAX')
211 # https://docs.python.org/3/c-api/init.html#c.PyEval_InitThreads
212 code += x86.Mov('EAX', PyEval_InitThreads)
213 code += x86.Call('EAX')
214 code += x86.Label(":DO_ENSURE")
215 code += x86.Mov('EAX', PyGILState_Ensure)
216 code += x86.Call('EAX')
217 code += x86.Push('EAX')
218 # Get the string to execute from parameters
219 code += x86.Mov("EAX", x86.mem("[ESP + 0x8]"))
220 code += x86.Push('EAX')
221 code += x86.Mov('EAX', PyRun_SimpleString)
222 code += x86.Call('EAX')
223 code += x86.Mov("ESI", "EAX")
224 code += x86.Mov('EAX', PyGILState_Release)
225 code += x86.Call('EAX')
226 code += x86.Pop('EAX')
227 code += x86.Cmp("EDI", 0)
228 code += x86.Jnz(":RETURN")
229 # If PyEval_InitThreads was called (init done in this thread)
230 # We must release the GIL
231 code += x86.Mov('EAX', PyEval_SaveThread)
232 code += x86.Call('EAX')
233 code += x86.Label(":RETURN")
234 code += x86.Mov("EAX", "ESI")
235 code += x86.Pop("EDI")
236 code += x86.Ret()
237 return code.get_code()
238
239

Callers

nothing calls this directly

Calls 1

get_codeMethod · 0.95

Tested by

no test coverage detected