n1nj4sec / mimipy

port of mimipenguin.sh in python with some additional protection features
BSD 3-Clause "New" or "Revised" License
203 stars 37 forks source link

Can not run in Windows #3

Open kevinalbarton opened 4 years ago

kevinalbarton commented 4 years ago

I have installed Python V.3.7 on Win10 64 bit and ran "python packed/mimipy.py" but I got:

File "packed/mimipy.py", line 12 exec '# -- coding: utf-8 --\n# ---------------------------------------------------------------\n# Copyright (c) 2015, Nicolas VERDIER (contact@n1nj4.eu)\n# All rights reserved.\n#\n# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:\n#\n# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.\n#\n# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.\n#\n# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.\n#\n# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE\n# ---------------------------------------------------------------\n# This module uses the builtins modules pupy and memimporter to load python modules and packages from memory, including .pyd files (windows only)\n# Pupy can dynamically add new modules to the modules dictionary to allow remote importing of python modules from memory !\n#\nimport sys, imp, marshal, gc\n\ndebug = False\ntrace = False\n\ndef dprint(msg):\n global debug\n if debug:\n print msg\n\ndef memtrace(msg):\n global debug\n global trace\n if debug and trace:\n import time\n import os\n import cPickle\n import gc\n\n msg = msg or \'unknown\'\n msg = msg.replace(\'/\', \'\')\n\n gc.collect()\n snapshot = trace.take_snapshot()\n\n if not os.path.isdir(\'/tmp/pupy-traces\'):\n os.makedirs(\'/tmp/pupy-traces\')\n\n with open(\'/tmp/pupy-traces/{}-{}\'.format(time.time(), msg), \'w+b\') as out:\n cPickle.dump(snapshot, out)\n\ntry:\n import _memimporter\n builtin_memimporter = True\n allow_system_packages = False\n\nexcept ImportError:\n builtin_memimporter = False\n allow_system_packages = True\n import ctypes\n import platform\n if sys.platform!="win32":\n libc = ctypes.CDLL(None)\n syscall = libc.syscall\n from tempfile import mkstemp\n from os import chmod, unlink, close, write\n\n class MemImporter(object):\n def init(self):\n self.dir = None\n self.memfd = None\n self.ready = False\n\n if platform.system() == \'Linux\':\n maj, min = platform.release().split(\'.\')[:2]\n if maj >= 3 and min >= 13:\n NR_memfd_create = None\n machine = platform.machine()\n if machine == \'x86_64\':\n NR_memfd_create = 319\n elif machine == \'i386\':\n NR_memfd_create = 356\n\n if NR_memfd_create:\n self.memfd = lambda: syscall(NR_memfd_create, \'heap\', 0x1)\n self.ready = True\n return\n\n for dir in [\'/dev/shm\', \'/tmp\', \'/var/tmp\', \'/run\']:\n try:\n fd, name = mkstemp(dir=dir)\n except:\n continue\n\n try:\n chmod(name, 0777)\n self.dir = dir\n self.ready = True\n break\n\n finally:\n close(fd)\n unlink(name)\n\n def import_module(self, data, initfuncname, fullname, path):\n return self.load_library(data, fullname, dlopen=False, initfuncname=initfuncname)\n\n\n def load_library(self, data, fullname, dlopen=True, initfuncname=None):\n fd = -1\n closefd = True\n\n result = False\n\n if self.memfd:\n fd = self.memfd()\n if fd != -1:\n name = \'/proc/self/fd/{}\'.format(fd)\n closefd = False\n\n if fd == -1:\n fd, name = mkstemp(dir=self.dir)\n\n try:\n write(fd, data)\n if dlopen:\n result = ctypes.CDLL(fullname)\n else:\n if initfuncname:\n result = imp.load_dynamic(initfuncname[4:], name)\n else:\n result = imp.load_dynamic(fullname, name)\n\n except Exception as e:\n self.dir = None\n raise e\n\n finally:\n if closefd:\n close(fd)\n unlink(name)\n\n return result\n\n _memimporter = MemImporter()\n builtin_memimporter = _memimporter.ready\n\nmodules = {}\nremote_load_package = None\nremote_print_error = None\n\ntry:\n import pupy\n if not (hasattr(pupy, \'pseudo\') and pupy.pseudo) and not modules:\n modules = pupy.get_modules()\nexcept ImportError:\n pass\n\ndef get_module_files(fullname):\n """ return the file to load """\n global modules\n path = fullname.replace(\'.\',\'/\')\n\n files = [\n module for module in modules.iterkeys() \\n if module.rsplit(".",1)[0] == path or any([\n path+\'/init\'+ext == module for ext in [\n \'.py\', \'.pyc\', \'.pyo\'\n ]\n ])\n ]\n\n if len(files) > 1:\n # If we have more than one file, than throw away dlls\n files = [ x for x in files if not x.endswith(\'.dll\') ]\n\n return files\n\ndef pupy_add_package(pkdic, compressed=False, name=None):\n """ update the modules dictionary to allow remote imports of new packages """\n import cPickle\n import zlib\n\n global modules\n\n if compressed:\n pkdic = zlib.decompress(pkdic)\n\n module = cPickle.loads(pkdic)\n\n dprint(\'Adding files: {}\'.format(module.keys()))\n\n modules.update(module)\n\n if name:\n try:\n import(name)\n except:\n pass\n\n gc.collect()\n\n memtrace(name)\n\ndef has_module(name):\n return name in sys.modules\n\ndef invalidate_module(name):\n global modules\n global debug\n\n for item in modules.keys():\n if item == name or item.startswith(name+\'.\'):\n dprint(\'Remove {} from pupyimporter.modules\'.format(item))\n del modules[item]\n\n for item in sys.modules.keys():\n if not (item == name or item.startswith(name+\'.\')):\n continue\n\n mid = id(sys.modules[item])\n\n dprint(\'Remove {} from sys.modules\'.format(item))\n del sys.modules[item]\n\n if hasattr(pupy, \'namespace\'):\n dprint(\'Remove {} from rpyc namespace\'.format(item))\n pupy.namespace.invalidate(item)\n\n if debug:\n for obj in gc.get_objects():\n if id(obj) == mid:\n dprint(\'Module {} still referenced by {}\'.format(\n item, [ id(x) for x in gc.get_referrers(obj) ]\n ))\n\n gc.collect()\n\ndef native_import(name):\n import(name)\n\nclass PupyPackageLoader:\n def init(self, fullname, contents, extension, is_pkg, path):\n self.fullname = fullname\n self.contents = contents\n self.extension = extension\n self.is_pkg=is_pkg\n self.path=path\n self.archive="" #need this attribute\n\n def load_module(self, fullname):\n imp.acquire_lock()\n try:\n dprint(\'loading module {}\'.format(fullname))\n if fullname in sys.modules:\n return sys.modules[fullname]\n\n mod=None\n c=None\n if self.extension=="py":\n mod = imp.new_module(fullname)\n mod.name = fullname\n mod.file = \'pupy://{}\'.format(self.path)\n if self.is_pkg:\n mod.path = [mod.file.rsplit(\'/\',1)[0]]\n mod.package = fullname\n else:\n mod.package = fullname.rsplit(\'.\', 1)[0]\n code = compile(self.contents, mod.file, "exec")\n sys.modules[fullname] = mod\n exec (code, mod.dict)\n\n elif self.extension in ["pyc","pyo"]:\n mod = imp.new_module(fullname)\n mod.name = fullname\n mod.file = \'pupy://{}\'.format(self.path)\n if self.is_pkg:\n mod.path = [mod.file.rsplit(\'/\',1)[0]]\n mod.package = fullname\n else:\n mod.package = fullname.rsplit(\'.\', 1)[0]\n sys.modules[fullname] = mod\n exec (marshal.loads(self.contents[8:]), mod.dict)\n\n elif self.extension in ("dll", "pyd", "so"):\n initname = "init" + fullname.rsplit(".",1)[-1]\n path = self.fullname.rsplit(\'.\', 1)[0].replace(".",\'/\') + "." + self.extension\n dprint(\'Loading {} from memory\'.format(fullname))\n dprint(\'init={} fullname={} path={}\'.format(initname, fullname, path))\n mod = _memimporter.import_module(self.contents, initname, fullname, path)\n if mod:\n mod.name=fullname\n mod.file = \'pupy://{}\'.format(self.path)\n mod.package = fullname.rsplit(\'.\',1)[0]\n sys.modules[fullname] = mod\n\n try:\n memtrace(fullname)\n except Exception, e:\n dprint(\'memtrace failed: {}\'.format(e))\n\n\n except Exception as e:\n\n if fullname in sys.modules:\n del sys.modules[fullname]\n\n import traceback\n exc_type, exc_value, exc_traceback = sys.exc_info()\n sys.stderr.write(\'Error importing %s : %s\'%(fullname, traceback.format_exc()))\n dprint(\'PupyPackageLoader: \'\n \'Error while loading package {} ({}) : {}\'.format(\n fullname, self.path, str(e)))\n if remote_print_error:\n try:\n remote_print_error("Error loading package {} ({} pkg={}) : {}".format(\n fullname, self.path, self.is_pkg, str(traceback.format_exc())))\n except:\n pass\n\n raise e\n\n finally:\n self.contents = None\n imp.release_lock()\n gc.collect()\n\n return sys.modules[fullname]\n\nclass PupyPackageFinderImportError(ImportError):\n pass\n\nclass PupyPackageFinder(object):\n def init(self, path=None):\n if path and not path.startswith(\'pupy://\'):\n raise PupyPackageFinderImportError()\n\n def find_module(self, fullname, path=None, second_pass=False):\n global modules\n imp.acquire_lock()\n selected = None\n\n try:\n files=[]\n if fullname in ( \'pywintypes\', \'pythoncom\' ):\n fullname = fullname + \'27.dll\'\n files = [ fullname ]\n else:\n files = get_module_files(fullname)\n\n dprint(\'find_module({},{}) in {})\'.format(fullname, path, files))\n if not builtin_memimporter:\n files = [\n f for f in files if not f.lower().endswith((\'.pyd\',\'.dll\',\'.so\'))\n ]\n\n if not files:\n dprint(\'{} not found in {}: not in {} files\'.format(\n fullname, files, len(files)))\n\n if remote_load_package and not secondpass and not fullname.startswith(\'exposed\'):\n parts = fullname.split(\'.\')[:-1]\n\n for i in xrange(len(parts)):\n part = \'.\'.join(parts[:i+1])\n if part in modules or part in sys.modules:\n return None\n\n try:\n if remote_load_package(fullname):\n return self.find_module(fullname, second_pass=True)\n except Exception as e:\n dprint(\'Exception: {}\'.format(e))\n\n return None\n\n criterias = [\n lambda f: any([\n f.endswith(\'/init\'+ext) for ext in [\n \'.pyo\', \'.pyc\', \'.py\'\n ]\n ]),\n lambda f: any ([\n f.endswith(ext) for ext in [\n \'.pyo\', \'.pyc\'\n ]\n ]),\n lambda f: any ([\n f.endswith(ext) for ext in [\n \'.pyd\', \'.py\', \'.so\', \'.dll\'\n ]\n ]),\n ]\n\n selected = None\n for criteria in criterias:\n for pyfile in files:\n if criteria(pyfile):\n selected = pyfile\n break\n\n if not selected:\n return None\n\n content = modules[selected]\n dprint(\'{} found in "{}" / size = {}\'.format(fullname, selected, len(content)))\n\n extension = selected.rsplit(".",1)[1].strip().lower()\n is_pkg = any([\n selected.endswith(\'/init\'+ext) for ext in [ \'.pyo\', \'.pyc\', \'.py\' ]\n ])\n\n dprint(\'--> Loading {} ({}) package={}\'.format(\n fullname, selected, is_pkg))\n\n return PupyPackageLoader(fullname, content, extension, is_pkg, selected)\n\n except Exception as e:\n dprint(\'--> Loading {} failed: {}\'.format(fullname, e))\n raise e\n\n finally:\n # Don\'t delete network.conf module\n if selected and not selected.startswith(\'network/conf\'):\n dprint(\'XXX {} remove {} from bundle / count = {}\'.format(fullname, selected, len(modules)))\n del modules[selected]\n\n imp.release_lock()\n gc.collect()\n\ndef register_package_request_hook(hook):\n global remote_load_package\n remote_load_package = hook\n\ndef register_package_error_hook(hook):\n global remote_print_error\n import rpyc\n remote_print_error = rpyc.async(hook)\n\ndef unregister_package_error_hook():\n global remote_print_error\n remote_print_error = None\n\ndef unregister_package_request_hook():\n global remote_load_package\n remote_load_package = None\n\ndef install(debug=None, trace=False):\n global debug\n global trace\n global modules\n\n #if debug:\n # debug = True\n\n if trace:\n trace = trace\n\n gc.set_threshold(128)\n\n if allow_system_packages:\n sys.path_hooks.append(PupyPackageFinder)\n sys.path.append(\'pupy://\')\n else:\n sys.meta_path = []\n sys.path = []\n sys.path_hooks = []\n sys.path_hooks = [PupyPackageFinder]\n sys.path.append(\'pupy://\')\n sys.path_importer_cache.clear()\n\n import platform\n platform._syscmd_uname = lambda *args, kwargs: \'\'\n platform.architecture = lambda *args, *kwargs: (\n \'32bit\' if pupy.get_arch() == \'x86\' else \'64bit\', \'\'\n )\n\n try:\n if trace:\n trace = import(\'tracemalloc\')\n\n if debug and trace:\n dprint(\'tracemalloc enabled\')\n trace.start(10)\n\n except Exception, e:\n dprint(\'tracemalloc init failed: {}\'.format(e))\n trace = None\n\n import ctypes\n import ctypes.util\n import os\n\n ctypes._system_dlopen = ctypes._dlopen\n ctypes.util._system_find_library = ctypes.util.find_library\n\n def pupy_make_path(name):\n if not name:\n return\n if \'pupy:\' in name:\n name = name[name.find(\'pupy:\')+5:]\n name = os.path.relpath(name)\n name = \'/\'.join([\n x for x in name.split(os.path.sep) if x and not x in ( \'.\', \'..\' )\n ])\n\n return name\n\n def pupy_find_library(name):\n pupyized = pupy_make_path(name)\n if pupyized in modules:\n dprint("FIND LIBRARY: {} => {}".format(name, pupyized))\n return pupyized\n else:\n return ctypes.util._system_find_library(name)\n\n def pupy_dlopen(name, args, kwargs):\n dprint("ctypes dlopen: {}".format(name))\n from_pupy = False\n name = pupy_make_path(name)\n dprint("ctypes dlopen / pupyized: {}".format(name))\n\n if name in modules:\n if hasattr(_memimporter, \'load_library\'):\n try:\n return _memimporter.load_library(modules[name], name)\n except:\n pass\n elif hasattr(pupy, \'load_dll\'):\n try:\n return pupy.load_dll(name, modules[name])\n except:\n pass\n\n if not from_pupy:\n return ctypes._system_dlopen(name, *args, kwargs)\n\n\n if \'pupy\' in sys.modules and hasattr(pupy, \'find_function_address\'):\n ctypes.CDLL_ORIG = ctypes.CDLL\n\n class PupyCDLL(ctypes.CDLL_ORIG):\n def init(self, name, kwargs):\n super(PupyCDLL, self).init(name, **kwargs)\n self._FuncPtr_orig = self._FuncPtr\n self._FuncPtr = self._find_function_address\n self._name = pupy_make_path(self._name)\n dprint(\'CDLL({})\'.format(self._name))\n\n def _find_function_address(self, search_tuple):\n name, handle = search_tuple\n dprint("PupyCDLL._find_function_address: {}".format(name))\n if not type(name) in (str, unicode):\n return self._FuncPtr_orig(search_tuple)\n else:\n addr = pupy.find_function_address(self._name, name)\n dprint("PupyCDLL._find_function_address: {} = {}".format(name, addr))\n if addr:\n return self._FuncPtr_orig(addr)\n else:\n return self._FuncPtr_orig(search_tuple)\n\n ctypes.CDLL = PupyCDLL\n\n ctypes._dlopen = pupy_dlopen\n ctypes.util.find_library = pupy_find_library\n\n #if \'win\' in sys.platform:\n # import pywintypes\n if debug:\n print \'Bundled modules:\'\n for module in modules.iterkeys():\n print \'+ {}\'.format(module)\n' in mod.dict__ ^ SyntaxError: invalid syntax

tinylama commented 2 years ago

You'll need python 2 to run this