Exemplo n.º 1
0
def mod_from_desc(desc, adds_dat):
    "module loader"
    desc['isactive'] = True
    if 'module' not in desc:
        pth, nam = split(splitext(desc['path'])[0])
        try:
            fptr, pth, dsc = find_module(nam, [pth])
        except ImportError:
            desc['isactive'] = False
            print('ImportError: %s' % nam)
            return
        module = load_module(nam, fptr, pth, dsc)
        if fptr:
            fptr.close()
        mdata = dict(adds_dat[' base '])
        mdata['id'] = desc['id']
        adds_dat[desc['id']] = mdata
        if not hasattr(module, 'introduce') or \
                module.introduce(mdata):
            adds_dat.pop(desc['id'])
            desc['isactive'] = False
            print("Error: `%s' can't be introduced" % pth)
            modules.pop(module.__name__)
            return
        desc['module'] = module
        return module
    return desc['module']
Exemplo n.º 2
0
def mod_from_desc(desc, adds_dat):
    "module loader"
    desc['isactive'] = True
    if 'module' not in desc:
        pth, nam = split(splitext(desc['path'])[0])
        try:
            fptr, pth, dsc = find_module(nam, [pth])
        except ImportError:
            desc['isactive'] = False
            print('ImportError: %s' % nam)
            return
        module = load_module(nam, fptr, pth, dsc)
        if fptr:
            fptr.close()
        mdata = dict(adds_dat[' base '])
        mdata['id'] = desc['id']
        adds_dat[desc['id']] = mdata
        if not hasattr(module, 'introduce') or \
                module.introduce(mdata):
            adds_dat.pop(desc['id'])
            desc['isactive'] = False
            print("Error: `%s' can't be introduced" % pth)
            modules.pop(module.__name__)
            return
        desc['module'] = module
        return module
    return desc['module']
Exemplo n.º 3
0
 def introduce(self):
     """modules loader"""
     any_error = False
     for desc in self.descriptions:
         if desc['isactive'] and 'module' not in desc:
             pth, nam = split(splitext(desc["path"])[0])
             try:
                 if isinstance(desc["id"], int) and desc["id"] < 1000:
                     module = import_module("." + desc["path"],
                                            "components")
                 else:
                     module = import_module(desc["path"])
             except ImportError as err:
                 desc['isactive'] = False
                 any_error = True
                 print('ImportError: %s, %s' % (nam, err))
                 continue
             if not hasattr(module, 'introduce') or \
                     module.introduce():
                 desc['isactive'] = False
                 any_error = True
                 print("Error: `%s' can't be introduced" % pth)
                 modules.pop(module.__name__)
                 continue
             desc['module'] = module
     if any_error:
         self.get_active()
     return any_error
Exemplo n.º 4
0
 def introduce(self, adds_dat):
     "modules loader"
     any_error = False
     for desc in self.descriptions:
         if desc['isactive'] and 'module' not in desc:
             pth, nam = split(splitext(desc['path'])[0])
             try:
                 fptr, pth, dsc = find_module(nam, [pth])
                 module = load_module(nam, fptr, pth, dsc)
             except ImportError:
                 desc['isactive'] = False
                 any_error = True
                 print('ImportError: %s' % nam)
                 continue
             if fptr:
                 fptr.close()
             mdata = dict(adds_dat[' base '])
             mdata['id'] = desc['id']
             adds_dat[desc['id']] = mdata
             if not hasattr(module, 'introduce') or \
                     module.introduce(mdata):
                 adds_dat.pop(desc['id'])
                 desc['isactive'] = False
                 any_error = True
                 print("Error: `%s' can't be introduced" % pth)
                 modules.pop(module.__name__)
                 continue
             desc['module'] = module
     if any_error:
         self.get_active()
     return any_error
Exemplo n.º 5
0
 def introduce(self, adds_dat):
     "modules loader"
     any_error = False
     for desc in self.descriptions:
         if desc['isactive'] and 'module' not in desc:
             pth, nam = split(splitext(desc['path'])[0])
             try:
                 fptr, pth, dsc = find_module(nam, [pth])
                 module = load_module(nam, fptr, pth, dsc)
             except ImportError:
                 desc['isactive'] = False
                 any_error = True
                 print('ImportError: %s' % nam)
                 continue
             if fptr:
                 fptr.close()
             mdata = dict(adds_dat[' base '])
             mdata['id'] = desc['id']
             adds_dat[desc['id']] = mdata
             if not hasattr(module, 'introduce') or \
                     module.introduce(mdata):
                 adds_dat.pop(desc['id'])
                 desc['isactive'] = False
                 any_error = True
                 print("Error: `%s' can't be introduced" % pth)
                 modules.pop(module.__name__)
                 continue
             desc['module'] = module
     if any_error:
         self.get_active()
     return any_error
Exemplo n.º 6
0
 def terminate(self, every=False):
     """modules unloader"""
     id_off = []
     for desc in self.descriptions:
         if 'module' in desc and (every or not desc['isactive']):
             module = desc.pop('module')
             if hasattr(module, 'terminate'):
                 module.terminate()
             modules.pop(module.__name__)
             id_off.append(desc['id'])
     return id_off
Exemplo n.º 7
0
 def terminate(self, adds_dat, all=False):
     "modules unloader"
     id_off = []
     for desc in self.descriptions:
         if 'module' in desc and (all or not desc['isactive']):
             module = desc.pop('module')
             mdata = adds_dat.pop(desc['id'])
             if hasattr(module, 'terminate'):
                 module.terminate(mdata)
             modules.pop(module.__name__)
             id_off.append(desc['id'])
     return id_off
Exemplo n.º 8
0
 def terminate(self, adds_dat, all=False):
     "modules unloader"
     id_off = []
     for desc in self.descriptions:
         if 'module' in desc and (all or not desc['isactive']):
             module = desc.pop('module')
             mdata = adds_dat.pop(desc['id'])
             if hasattr(module, 'terminate'):
                 module.terminate(mdata)
             modules.pop(module.__name__)
             id_off.append(desc['id'])
     return id_off
Exemplo n.º 9
0
    def test_rewrite_pyc_check_code_name(self):
        # This one is adapted from cpython's Lib/test/test_import.py
        from os import chmod
        from os.path import join
        from sys import modules, path
        from shutil import rmtree
        from tempfile import mkdtemp

        code = """if 1:
            import sys
            code_filename = sys._getframe().f_code.co_filename
            module_filename = __file__
            constant = 1
            def func():
                pass
            func_filename = func.func_code.co_filename
            """

        module_name = "unlikely_module_name"
        dir_name = mkdtemp(prefix="pypy_test")
        file_name = join(dir_name, module_name + ".py")
        with open(file_name, "wb") as f:
            f.write(code)
        compiled_name = file_name + ("c" if __debug__ else "o")
        chmod(file_name, 0777)

        # Setup
        sys_path = path[:]
        orig_module = modules.pop(module_name, None)
        assert modules.get(module_name) == None
        path.insert(0, dir_name)

        # Test
        import py_compile

        py_compile.compile(file_name, dfile="another_module.py")
        __import__(module_name, globals(), locals())
        mod = modules.get(module_name)

        try:
            # Ensure proper results
            assert mod != orig_module
            assert mod.module_filename == compiled_name
            assert mod.code_filename == file_name
            assert mod.func_filename == file_name
        finally:
            # TearDown
            path[:] = sys_path
            if orig_module is not None:
                modules[module_name] = orig_module
            else:
                try:
                    del modules[module_name]
                except KeyError:
                    pass
            rmtree(dir_name, True)
Exemplo n.º 10
0
    def test_rewrite_pyc_check_code_name(self):
        # This one is adapted from cpython's Lib/test/test_import.py
        from os import chmod
        from os.path import join
        from sys import modules, path
        from shutil import rmtree
        from tempfile import mkdtemp
        code = b"""if 1:
            import sys
            code_filename = sys._getframe().f_code.co_filename
            module_filename = __file__
            constant = 1
            def func():
                pass
            func_filename = func.__code__.co_filename
            """

        module_name = "unlikely_module_name"
        dir_name = mkdtemp(prefix='pypy_test')
        file_name = join(dir_name, module_name + '.py')
        with open(file_name, "wb") as f:
            f.write(code)
        compiled_name = file_name + ("c" if __debug__ else "o")
        chmod(file_name, 0o777)

        # Setup
        sys_path = path[:]
        orig_module = modules.pop(module_name, None)
        assert modules.get(module_name) == None
        path.insert(0, dir_name)

        # Test
        import py_compile
        py_compile.compile(file_name, dfile="another_module.py")
        __import__(module_name, globals(), locals())
        mod = modules.get(module_name)

        try:
            # Ensure proper results
            assert mod != orig_module
            assert mod.module_filename == file_name
            assert mod.code_filename == file_name
            assert mod.func_filename == file_name
        finally:
            # TearDown
            path[:] = sys_path
            if orig_module is not None:
                modules[module_name] = orig_module
            else:
                try:
                    del modules[module_name]
                except KeyError:
                    pass
            rmtree(dir_name, True)
Exemplo n.º 11
0
def mod_from_desc(desc):
    """module loader"""
    desc['isactive'] = True
    if 'module' not in desc:
        pth, nam = split(splitext(desc['path'])[0])
        try:
            fptr, pth, dsc = find_module(nam, [pth])
        except ImportError:
            desc['isactive'] = False
            print('ImportError: %s' % nam)
            return
        module = load_module(nam, fptr, pth, dsc)
        if fptr:
            fptr.close()
        if not hasattr(module, 'introduce') or \
                module.introduce():
            desc['isactive'] = False
            print("Error: `%s' can't be introduced" % pth)
            modules.pop(module.__name__)
            return
        desc['module'] = module
        return module
    return desc['module']
Exemplo n.º 12
0
    def importExploits(self, files, route, refresh=False):
        """
        Import all exploits that we found and want to import.
        :param files: the files that will import
        :param route: generic or specific
        :param refresh: True if we want to re-import, False otherwise
        :return: exploits imported
        """
        exploits = []
        try:
            for exploit in files:
                name = 'rexploit.modules.{0}.{1}'.format(route, exploit)

                if refresh:
                    try:
                        modules.pop(name)
                    except KeyError:
                        break

                # Reload all modules
                if name in modules.keys():
                    # Get a module
                    e = modules.get(name)
                else:
                    # Import a new module
                    e = import_module(
                        '.modules.{0}.{1}'.format(route, exploit), 'rexploit')

                # Create an exploit object and we set IP and MAC
                e = e.Exploit()
                e.ip = self.__IP
                e.mac = self.__MAC

                exploits.append(e)
            return exploits
        except ImportError:
            return exploits
Exemplo n.º 13
0
    def importExploits(self, files, route, refresh=False):
        """
        Import all exploits that we found and want to import.
        :param files: the files that will import
        :param route: generic or specific
        :param refresh: True if we want to re-import, False otherwise
        :return: exploits imported
        """
        exploits = []
        try:
            for exploit in files:
                name = 'rexploit.modules.{0}.{1}'.format(route, exploit)

                if refresh:
                    try:
                        modules.pop(name)
                    except KeyError:
                        break

                # Reload all modules
                if name in modules.keys():
                    # Get a module
                    e = modules.get(name)
                else:
                    # Import a new module
                    e = import_module('.modules.{0}.{1}'.format(route, exploit), 'rexploit')

                # Create an exploit object and we set IP and MAC
                e = e.Exploit()
                e.ip = self.__IP
                e.mac = self.__MAC

                exploits.append(e)
            return exploits
        except ImportError:
            return exploits
Exemplo n.º 14
0
 def cleanup(self):
     for m in self.modules:
         modules.pop(m)
Exemplo n.º 15
0
def terminate(data):
    iset = APP_SETT.set
    for i in _DEFAULTS:
        iset(i, data[i], "LIQAM")
    modules.pop("sqcalc")
Exemplo n.º 16
0
def terminate(data):
    iset = APP_SETT.set
    for i in _DEFAULTS:
        iset(i, data[i], "LIQAM")
    modules.pop("sqcalc")
Exemplo n.º 17
0
    def _reload(self, filename, request):

        re = self._module is not None and "re" or ""

        self._loader.log.message("{0:s}loading module {1:s} from {2:s}".\
                                 format(re, self._name, filename))
        try:

            # managing imports requires holding a global lock

            acquire_imp_lock()
            try:

                if self._name in sys_modules:
                    raise ModuleAlreadyImportedError("module {0:s} has already been "
                                                     "imported".format(self._name))

                with open(filename, "rb") as module:

                    ext = os_path.splitext(filename)[1]
                    assert valid_module_ext(ext)

                    if ext == ".py":

                        # as a simple guard against picking up incomplete files,
                        # being simultaneously written to, we require the modules
                        # to end with # EOF

                        try:
                            next(filter(lambda s: s.rstrip() == b"# EOF", module))
                        except StopIteration:
                            raise ModuleFileIncompleteError("file {0:s} is incomplete, does not "
                                                            "end with # EOF".format(filename))
                        else:
                            module.seek(0) # rewind the file

                    # actually import the module, it can already be broken
                    # again at this point, but we don't care

                    try:
                        load_module(self._name, module, filename, ("", "rb", ext == ".py" and PY_SOURCE or PY_COMPILED))
                    except Exception as e:
                        raise ModuleFileBrokenError("file {0:s} is broken: {1:s}".format(filename, e))

                # the pmnc-accessible modules are invisible in sys.modules

                module = sys_modules.pop(self._name)

                # see if the loaded module has __all__ attribute,
                # and if not provide a default empty list

                all = getattr(module, "__all__", None)
                if all is None:
                    setattr(module, "__all__", [])
                    self._loader.log.warning("module {0:s} has no __all__ attribute and will "
                                             "export no methods or classes".format(self._name))
                else:
                    assert list_of(str)(all), "__all__ attribute must be a list of strings"

                # append self_test method to a list of accessible
                # methods for the module being tested

                if request.self_test == self._name:
                    all.append("self_test")

            finally:
                release_imp_lock()

            # the module has been successfully loaded from the file

            try:

                # the newly imported module should have just one reference to it

                if getrefcount(module) != 2:
                    raise ModuleWithDependenciesError("the newly loaded module {0:s} has "
                                                      "unexpected dependencies".format(self._name))

                # a module containing __reloadable__ = False is assumed
                # to have state and hence be not reloadable

                reloadable = bool(getattr(module, "__reloadable__", True))

                # the imported module is instrumented with pmnc and others

                setattr(module, "pmnc", ModuleLoaderProxy(self._loader, self._name))

                setattr(module, "__node__", self._loader._node_name)
                setattr(module, "__cage__", self._loader._cage_name)
                setattr(module, "__module__", self._name)
                setattr(module, "__cage_dir__", self._loader._cage_directory)

                # success, the methods cache is cleared and the previous version is discarded

                with self._lock:
                    self._attrs.clear()

                self._module, module, self._reloadable = module, self._module, reloadable

            finally:
                del module

        except ModuleLoaderError as e:
            if self._module is not None:
                self._loader.log.message("reloading of module {0:s} failed: {1:s} (the error "
                                         "is ignored)".format(self._name, str(e)))
            else:
                raise
        else:
            self._loader.log.message("module {0:s} has been {1:s}loaded{2:s}".format(self._name, re,
                                     not reloadable and " (not reloadable)" or ""))
Exemplo n.º 18
0
    return pkg_prepare


# =========================================================================

self_discovery_plan = ['__project__']
while True:
    pkg, backup_mod, project = {}, None, self_discovery_plan.pop()
    try:
        # XXX check relative import
        backup_mod = sys_modules.get(project)
        if backup_mod:
            if not hasattr(backup_mod,
                           '__path__'):  # not the case for builtins
                continue
            backup_mod = sys_modules.pop(project)
        backup_path, sys_path[:] = sys_path[:], [here]
        pkg = __import__(project, globals=pkg)
        break
    except ImportError:
        sys_path[:] = backup_path
        if backup_mod:
            sys_modules[project] = backup_mod
        if project == '__project__':
            from glob import iglob
            self_discovery_plan.extend(p[:-len('.egg-info')].split('-', 1)[0]
                                       for p in iglob('*.egg-info'))
        if not self_discovery_plan:
            print "Cannot find myself, please help me with __project__ symlink"
            raise
sys_path[:] = backup_path
Exemplo n.º 19
0
    return pkg_prepare


# =========================================================================

self_discovery_plan = ['__project__']
while True:
    pkg, backup_mod, project = {}, None, self_discovery_plan.pop()
    try:
        # XXX check relative import
        backup_mod = sys_modules.get(project)
        if backup_mod:
            if not hasattr(backup_mod, '__path__'):  # not the case for builtins
                continue
            backup_mod = sys_modules.pop(project)
        backup_path, sys_path[:] = sys_path[:], [here]
        pkg = __import__(project, globals=pkg)
        break
    except ImportError:
        sys_path[:] = backup_path
        if backup_mod:
            sys_modules[project] = backup_mod
        if project == '__project__':
            from glob import iglob
            self_discovery_plan.extend(p[:-len('.egg-info')].split('-', 1)[0]
                                       for p in iglob('*.egg-info'))
        if not self_discovery_plan:
            print("Cannot find myself, please help me with __project__ symlink")
            raise
sys_path[:] = backup_path