sys.modules.has_key

Here are the examples of the python api sys.modules.has_key taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

71 Examples 7

Example 1

Project: hellanzb
Source File: HellaReactor.py
View license
    def install(klass):
        """ Install custom reactor """
        if sys.modules.has_key('twisted.internet.reactor'):
            del sys.modules['twisted.internet.reactor']
        Hellanzb.reactor = HellaReactor()
        installReactor(Hellanzb.reactor)

Example 2

Project: babble
Source File: jreload.py
View license
    def do_unload(self,pkg):
        for n in pkg.__dict__.keys():
            e = pkg.__dict__[n]
            if isinstance(e,PyJavaClass):
                if _is_lazy(e): continue
                if e.classLoader is self.loader:
                    del pkg.__dict__[n]
                    if pkg.__name__:
                        n = self.ls_name + '.' + pkg.__name__ + '.' +n
                    else:
                        n = self.ls_name + '.' + n
                    if sys.modules.has_key(n): del sys.modules[n]

            elif isinstance(e,PyJavaPackage):
                self.do_unload(e)

Example 3

Project: babble
Source File: test_pkgimport.py
View license
    def __init__(self, *args, **kw):
        self.package_name = 'PACKAGE_'
        while sys.modules.has_key(self.package_name):
            self.package_name += random.choose(string.letters)
        self.module_name = self.package_name + '.foo'
        unittest.TestCase.__init__(self, *args, **kw)

Example 4

Project: babble
Source File: test_site.py
View license
    def test_sitecustomize_executed(self):
        # If sitecustomize is available, it should have been imported.
        if not sys.modules.has_key("sitecustomize"):
            try:
                import sitecustomize
            except ImportError:
                pass
            else:
                self.fail("sitecustomize not imported automatically")

Example 5

Project: pyff
Source File: test_rollbackimporter.py
View license
    def testSimpleImport(self):
        """Should remove simple import."""
        modname = "lib.test.mod_wo_imports"
        self._del_if_existent(modname)
        rbi = RollbackImporter()
        import lib.test.mod_wo_imports
        self.assertTrue(sys.modules.has_key(modname))
        rbi.uninstall()
        self.assertFalse(sys.modules.has_key(modname))

Example 6

Project: pyff
Source File: test_rollbackimporter.py
View license
    def testComplexImport(self):
        """Should remove import and import(s) of import."""
        modname1 = "lib.test.mod_w_imports"
        modname2 = "lib.test.mod_wo_imports"
        self._del_if_existent(modname1)
        self._del_if_existent(modname2)
        rbi = RollbackImporter()
        import lib.test.mod_w_imports
        self.assertTrue(sys.modules.has_key(modname2))
        rbi.uninstall()
        self.assertFalse(sys.modules.has_key(modname2))

Example 7

Project: pyff
Source File: test_rollbackimporter.py
View license
    def testRelativeImport(self):
        """Should remove relative import."""
        modname = "lib.test.mod_wo_imports"
        self._del_if_existent(modname)
        rbi = RollbackImporter()
        import mod_wo_imports
        self.assertTrue(sys.modules.has_key(modname))
        rbi.uninstall()
        self.assertFalse(sys.modules.has_key(modname))

Example 8

Project: yos-social-python
Source File: http.py
View license
def get_default_urlfetch():
  """Creates the default UrlFetch interface.
  
  If AppEngine environment is detected, then the AppEngineUrlFetch object
  will be created.
  
  TODO: Find a better way to determine if this is an AppEngine environment.

  """
  if sys.modules.has_key('google.appengine.api.urlfetch'):
    return AppEngineUrlFetch()
  return UrlFetch()

Example 9

Project: cgat
Source File: preppy.py
View license
    def rl_get_module(name,dir):
        f, p, desc= imp.find_module(name,[dir])
        if sys.modules.has_key(name):
            om = sys.modules[name]
            del sys.modules[name]
        else:
            om = None
        try:
            try:
                return imp.load_module(name,f,p,desc)
            except:
                raise ImportError('%s[%s]' % (name,dir))
        finally:
            if om: sys.modules[name] = om
            del om
            if f: f.close()

Example 10

Project: codex-backend
Source File: pescanner.py
View license
def get_filetype(data):
    """There are two versions of python-magic floating around, and annoyingly, the interface
        changed between versions, so we try one method and if it fails, then we try the other"""
        if sys.modules.has_key('magic'):
            try:
                ms = magic.open(magic.MAGIC_NONE)
                        ms.load()
                        return ms.buffer(data)
                except:
                    return magic.from_buffer(data)

Example 11

Project: codex-backend
Source File: pescanner.py
View license
    def __init__(self, files, yara_rules=None, peid_sigs=None):
        self.files = files

        # initialize YARA rules if provided
        if yara_rules and sys.modules.has_key('yara'):
            self.rules = yara.compile(yara_rules)
        else:
            self.rules = None

        # initialize PEiD signatures if provided
        if peid_sigs:
            self.sigs = peutils.SignatureDatabase(peid_sigs)
        else:
            self.sigs = None
            print("PEiD no inicializado")

Example 12

Project: pyxer
Source File: routing.py
View license
    def load_module(self, *names):
        " Load module "
        name = ".".join(names)
        if sys.modules.has_key(name):
            return self.init_module(sys.modules[name])
        try:
            __import__(name)
            return self.init_module(sys.modules[name], True)
        except ImportError, msg:
            # Try to filter import errors that are within the loaded module
            if name and (not (str(msg).endswith("." + names[-1]) or str(msg).endswith(" " + names[-1]))):
                log.exception("Error while importing module")
                raise
        return None

Example 13

Project: mythbox
Source File: main.py
View license
def installReactor(reactor):
    # this stuff should be common to all reactors.
    import twisted.internet
    import sys
    assert not sys.modules.has_key('twisted.internet.reactor'), \
           "reactor already installed"
    twisted.internet.reactor = reactor
    sys.modules['twisted.internet.reactor'] = reactor

Example 14

Project: mythbox
Source File: main.py
View license
def installReactor(reactor):
    # this stuff should be common to all reactors.
    import twisted.internet
    import sys
    assert not sys.modules.has_key('twisted.internet.reactor'), \
           "reactor already installed"
    twisted.internet.reactor = reactor
    sys.modules['twisted.internet.reactor'] = reactor

Example 15

Project: delicious2google
Source File: http.py
View license
def get_default_urlfetch():
  """Creates the default UrlFetch interface.
  
  If AppEngine environment is detected, then the AppEngineUrlFetch object
  will be created.
  
  TODO: Find a better way to determine if this is an AppEngine environment.

  """
  if sys.modules.has_key('google.appengine.api.urlfetch'):
    return AppEngineUrlFetch()
  return UrlFetch()

Example 16

Project: delicious2google
Source File: http.py
View license
def get_default_urlfetch():
  """Creates the default UrlFetch interface.
  
  If AppEngine environment is detected, then the AppEngineUrlFetch object
  will be created.
  
  TODO: Find a better way to determine if this is an AppEngine environment.

  """
  if sys.modules.has_key('google.appengine.api.urlfetch'):
    return AppEngineUrlFetch()
  return UrlFetch()

Example 17

Project: medicare-demo
Source File: test_pkgimport.py
View license
    def __init__(self, *args, **kw):
        self.package_name = 'PACKAGE_'
        while sys.modules.has_key(self.package_name):
            self.package_name += random.choose(string.letters)
        self.module_name = self.package_name + '.foo'
        unittest.TestCase.__init__(self, *args, **kw)

Example 18

Project: medicare-demo
Source File: test_site.py
View license
    def test_sitecustomize_executed(self):
        # If sitecustomize is available, it should have been imported.
        if not sys.modules.has_key("sitecustomize"):
            try:
                import sitecustomize
            except ImportError:
                pass
            else:
                self.fail("sitecustomize not imported automatically")

Example 19

Project: CapTipper
Source File: pescanner.py
View license
def get_filetype(data):
    """There are two versions of python-magic floating around, and annoyingly, the interface 
    changed between versions, so we try one method and if it fails, then we try the other.
    NOTE: you may need to alter the magic_file for your system to point to the magic file."""
    if sys.modules.has_key('magic'):
        try:
            ms = magic.open(magic.MAGIC_NONE) 
            ms.load() 
            return ms.buffer(data)
        except:
            try:
                return magic.from_buffer(data)
            except magic.MagicException:
                magic_custom = magic.Magic(magic_file='C:\windows\system32\magic')
                return magic_custom.from_buffer(data)
    return ''

Example 20

Project: CapTipper
Source File: pescanner.py
View license
    def __init__(self, data, yara_rules=None, peid_sigs=None):
        self.pedata = data
        
        # initialize YARA rules if provided 
        if yara_rules and sys.modules.has_key('yara'):
            self.rules = yara.compile(yara_rules)
        else:
            self.rules = None
            
        # initialize PEiD signatures if provided 
        if peid_sigs:
            self.sigs = peutils.SignatureDatabase(peid_sigs)
        else:
            self.sigs = None

Example 21

Project: pymo
Source File: __init__.py
View license
def make_parser(parser_list = []):
    """Creates and returns a SAX parser.

    Creates the first parser it is able to instantiate of the ones
    given in the list created by doing parser_list +
    default_parser_list.  The lists must contain the names of Python
    modules containing both a SAX parser and a create_parser function."""

    for parser_name in parser_list + default_parser_list:
        try:
            return _create_parser(parser_name)
        except ImportError,e:
            import sys
            if sys.modules.has_key(parser_name):
                # The parser module was found, but importing it
                # failed unexpectedly, pass this exception through
                raise
        except SAXReaderNotAvailable:
            # The parser module detected that it won't work properly,
            # so try the next one
            pass

    raise SAXReaderNotAvailable("No parsers found", None)

Example 22

Project: pymo
Source File: __init__.py
View license
def make_parser(parser_list = []):
    """Creates and returns a SAX parser.

    Creates the first parser it is able to instantiate of the ones
    given in the list created by doing parser_list +
    default_parser_list.  The lists must contain the names of Python
    modules containing both a SAX parser and a create_parser function."""

    for parser_name in parser_list + default_parser_list:
        try:
            return _create_parser(parser_name)
        except ImportError,e:
            import sys
            if sys.modules.has_key(parser_name):
                # The parser module was found, but importing it
                # failed unexpectedly, pass this exception through
                raise
        except SAXReaderNotAvailable:
            # The parser module detected that it won't work properly,
            # so try the next one
            pass

    raise SAXReaderNotAvailable("No parsers found", None)

Example 23

Project: imagrium
Source File: test_import_pep328.py
View license
    def top(self):
        if sys.modules.has_key("__main__"):
            return sys.modules["__main__"].__dict__
        return globals()

Example 24

Project: imagrium
Source File: __init__.py
View license
def make_parser(parser_list = []):
    """Creates and returns a SAX parser.

    Creates the first parser it is able to instantiate of the ones
    given in the list created by doing parser_list +
    default_parser_list.  The lists must contain the names of Python
    modules containing both a SAX parser and a create_parser function."""

    for parser_name in parser_list + default_parser_list:
        try:
            return _create_parser(parser_name)
        except ImportError,e:
            import sys
            if sys.modules.has_key(parser_name):
                # The parser module was found, but importing it
                # failed unexpectedly, pass this exception through
                raise
        except SAXReaderNotAvailable:
            # The parser module detected that it won't work properly,
            # so try the next one
            pass

    raise SAXReaderNotAvailable("No parsers found", None)

Example 25

Project: babble
Source File: jreload.py
View license
def makeLoadSet(name,path):
    if sys.modules.has_key(name): return sys.modules[name]
    sys.modules[name] = ls = LoadSet(name,path)
    return ls

Example 26

Project: babble
Source File: jreload.py
View license
def _do_reload(ls_name,mgr,pkg):
    pkg_name = pkg.__name__
    for n in pkg.__dict__.keys():
        e = pkg.__dict__[n]
        if isinstance(e,PyJavaClass):
            if _is_lazy(e): continue
            del pkg.__dict__[n]
            try :
                c = mgr.findClass(pkg_name,n);
                if c:
                    pkg.__dict__[n] = c
                    if pkg_name:
                        n = ls_name + '.' + pkg_name + '.' + n
                    else:
                        n = ls_name + '.' + n
                    if sys.modules.has_key(n): sys.modules[n] = c
            except:
                pass
        elif isinstance(e,PyJavaPackage):
            _do_reload(ls_name,mgr,e)

Example 27

Project: babble
Source File: test_pkgimport.py
View license
    def remove_modules(self):
        for module_name in (self.package_name, self.module_name):
            if sys.modules.has_key(module_name):
                del sys.modules[module_name]

Example 28

Project: babble
Source File: test_pkgimport.py
View license
    def test_package_import__semantics(self):

        # Generate a couple of broken modules to try importing.

        # ...try loading the module when there's a SyntaxError
        self.rewrite_file('for')
        try: __import__(self.module_name)
        except SyntaxError: pass
        else: raise RuntimeError, 'Failed to induce SyntaxError'
        self.assert_(not sys.modules.has_key(self.module_name) and
                     not hasattr(sys.modules[self.package_name], 'foo'))

        # ...make up a variable name that isn't bound in __builtins__
        import __builtin__
        var = 'a'
        while var in dir(__builtin__):
            var += random.choose(string.letters)

        # ...make a module that just contains that
        self.rewrite_file(var)

        try: __import__(self.module_name)
        except NameError: pass
        else: raise RuntimeError, 'Failed to induce NameError.'

        # ...now  change  the module  so  that  the NameError  doesn't
        # happen
        self.rewrite_file('%s = 1' % var)
        module = __import__(self.module_name).foo
        self.assertEqual(getattr(module, var), 1)

Example 29

Project: babble
Source File: __init__.py
View license
def make_parser(parser_list = []):
    """Creates and returns a SAX parser.

    Creates the first parser it is able to instantiate of the ones
    given in the list created by doing parser_list +
    default_parser_list.  The lists must contain the names of Python
    modules containing both a SAX parser and a create_parser function."""

    for parser_name in parser_list + default_parser_list:
        try:
            return _create_parser(parser_name)
        except ImportError,e:
            import sys
            if sys.modules.has_key(parser_name):
                # The parser module was found, but importing it
                # failed unexpectedly, pass this exception through
                raise
        except SAXReaderNotAvailable:
            # The parser module detected that it won't work properly,
            # so try the next one
            pass

    raise SAXReaderNotAvailable("No parsers found", None)

Example 30

Project: fwlite
Source File: zipextimporter.py
View license
    def load_module(self, fullname):
        if sys.modules.has_key(fullname):
            mod = sys.modules[fullname]
            if _memimporter.get_verbose_flag():
                sys.stderr.write("import %s # previously loaded from zipfile %s\n" % (fullname, self.archive))
            return mod
        _memimporter.set_find_proc(self.locate_dll_image)
        try:
            return zipimport.zipimporter.load_module(self, fullname)
        except zipimport.ZipImportError:
            pass
        initname = "init" + fullname.split(".")[-1] # name of initfunction
        filename = fullname.replace(".", "\\")
        if filename in ("pywintypes", "pythoncom"):
            filename = filename + "%d%d" % sys.version_info[:2]
            suffixes = ('.dll',)
        else:
            suffixes = self._suffixes
        for s in suffixes:
            path = filename + s
            if path in self._files:
                if _memimporter.get_verbose_flag():
                    sys.stderr.write("# found %s in zipfile %s\n" % (path, self.archive))
                code = self.get_data(path)
                mod = _memimporter.import_module(code, initname, fullname, path)
                mod.__file__ = "%s\\%s" % (self.archive, path)
                mod.__loader__ = self
                if _memimporter.get_verbose_flag():
                    sys.stderr.write("import %s # loaded from zipfile %s\n" % (fullname, mod.__file__))
                return mod
        raise zipimport.ZipImportError, "can't find module %s" % fullname

Example 31

Project: pyff
Source File: test_rollbackimporter.py
View license
    def _del_if_existent(self, modname):
        if sys.modules.has_key(modname):
            del(sys.modules[modname])

Example 32

Project: sftf
Source File: Helper.py
View license
def importModule(name):
	"needed because of a bug in python when dynamically loading a python module"

	if (len(name) == 0):
		return None
	if not name[0].isalpha():
		return None

	# Fast path: see if the module has already been imported.
	if sys.modules.has_key(name):
		return sys.modules[name]

	# If any of the following calls raises an exception,
	# there's a problem we can't handle

	# See if it's a built-in module.
	m = imp.init_builtin(name)
	if m:
		return m

	# See if it's a frozen module.
	m = imp.init_frozen(name)
	if m:
		return m

	try:
		# Search the default path (i.e. sys.path).
		fp, pathname, stuff = imp.find_module(name)
	except ImportError, param:
		Log.logDebug("Helper.importModule(): ImportError: " + str(param), 2)
		Log.logDebug("Helper.importModule(): failed to find module " + str(name), 1)
		return None

	# finally try to load the module
	try:
		m = imp.load_module(name, fp, pathname, stuff)
	finally:
		# Since we may exit via an exception, close fp explicitly.
		if fp:
			fp.close()
	# to please pychecker
	if m:
		return m
	else:
		return None

Example 33

Project: volatility
Source File: volshell.py
View license
    def render_text(self, _outfd, _data):
        self._addrspace = utils.load_as(self._config)

        if not self._config.OFFSET is None:
            self.set_context(offset = self._config.OFFSET)

            self.context_display()

        elif self._config.PID is not None:
            # FIXME: volshell is really not intended to switch into multiple
            # process contexts at once, so it doesn't make sense to use a csv
            # pid list. However, the linux and mac volshell call the respective
            # linux_pslist and mac_pslist which require a csv pidlist. After 
            # the 2.3 release we should close this along with issue 375. 
            pidlist = [int(p) for p in self._config.PID.split(',')]
            for p in pidlist:
                self.set_context(pid = p)
                break
        elif self._config.IMNAME is not None:
            self.set_context(name = self._config.IMNAME)
        else:
            # Just use the first process, whatever it is
            for p in self.getpidlist():
                self.set_context(offset = p.v())
                break

        # Functions inside the shell
        def cc(offset = None, pid = None, name = None, physical = False):
            """Change current shell context.

            This function changes the current shell context to to the process
            specified. The process specification can be given as a virtual address
            (option: offset), PID (option: pid), or process name (option: name).

            If multiple processes match the given PID or name, you will be shown a
            list of matching processes, and will have to specify by offset.
            """
            self.set_context(offset = offset, pid = pid, name = name, physical = physical)

        def db(address, length = 0x80, space = None):
            """Print bytes as canonical hexdump.
            
            This function prints bytes at the given virtual address as a canonical
            hexdump. The address will be translated in the current process context
            (see help on cc for information on how to change contexts).
            
            The length parameter (default: 0x80) specifies how many bytes to print,
            the width parameter (default: 16) allows you to change how many bytes per
            line should be displayed, and the space parameter allows you to
            optionally specify the address space to read the data from.
            """
            if not space:
                space = self._proc.get_process_address_space()
            #if length % 4 != 0:
            #    length = (length+4) - (length%4)
            data = space.read(address, length)
            if not data:
                print "Memory unreadable at {0:08x}".format(address)
                return

            for offset, hexchars, chars in utils.Hexdump(data):
                print "{0:#010x}  {1:<48}  {2}".format(address + offset, hexchars, ''.join(chars))

        def dd(address, length = 0x80, space = None):
            """Print dwords at address.

            This function prints the data at the given address, interpreted as
            a series of dwords (unsigned four-byte integers) in hexadecimal.
            The address will be translated in the current process context
            (see help on cc for information on how to change contexts).
            
            The optional length parameter (default: 0x80) controls how many bytes
            to display, and space allows you to optionally specify the address space
            to read the data from.
            """
            if not space:
                space = self._proc.get_process_address_space()
            # round up to multiple of 4
            if length % 4 != 0:
                length = (length + 4) - (length % 4)
            data = space.read(address, length)
            if not data:
                print "Memory unreadable at {0:08x}".format(address)
                return
            dwords = []
            for i in range(0, length, 4):
                (dw,) = struct.unpack("<L", data[i:i + 4])
                dwords.append(dw)

            if len(dwords) % 4 == 0: lines = len(dwords) / 4
            else: lines = len(dwords) / 4 + 1

            for i in range(lines):
                ad = address + i * 0x10
                lwords = dwords[i * 4:i * 4 + 4]
                print ("{0:08x}  ".format(ad)) + " ".join("{0:08x}".format(l) for l in lwords)

        def dq(address, length = 0x80, space = None):
            """Print qwords at address.

            This function prints the data at the given address, interpreted as
            a series of qwords (unsigned eight-byte integers) in hexadecimal.
            The address will be translated in the current process context
            (see help on cc for information on how to change contexts).
            
            The optional length parameter (default: 0x80) controls how many bytes
            to display, and space allows you to optionally specify the address space
            to read the data from.
            """
            if not space:
                space = self._proc.get_process_address_space()

            # round up 
            if length % 8 != 0:
                length = (length + 8) - (length % 8)

            qwords = obj.Object("Array", targetType = "unsigned long long",
                offset = address, count = length / 8, vm = space)

            if not qwords:
                print "Memory unreadable at {0:08x}".format(address)
                return

            for qword in qwords:
                print "{0:#x} {1:#x}".format(qword.obj_offset, qword.v())

        def ps():
            """Print active processes in a table view.

            Prints a process listing with PID, PPID, image name, and offset.
            """
            self.ps()

        def addrspace():
            """Get the current kernel/virtual address space. 

            This returns the current address space. 
            """
            return self._addrspace

        def proc():
            """Get the current process object.
            
            This returns the current process object. 
            """
            return self._proc 

        def getprocs():
            """Generator of process objects (scripting).

            This returns a list of active process objects.
            """    
            return self.getpidlist()

        def getmods():
            """Generator for kernel modules (scripting).

            This returns a list of loaded kernel module objects.
            """
            return self.getmodules()

        def modules():
            """Print loaded modules in a table view.

            Prints a module listing with base, offset, name etc
            """
            self.modules()

        def sc():
            """Show the current context.
            
            Show the current process information.
            """
            self.context_display()

        def list_entry(head, objname, offset = -1, fieldname = None, forward = True, space = None):
            """Traverse a _LIST_ENTRY.

            Traverses a _LIST_ENTRY starting at virtual address head made up of
            objects of type objname. The value of offset should be set to the
            offset of the _LIST_ENTRY within the desired object."""

            vm = space
            if space == None:
                vm = self._proc.get_process_address_space()
            seen = set()

            if fieldname:
                offset = vm.profile.get_obj_offset(objname, fieldname)
                #if typ != "_LIST_ENTRY":
                #    print ("WARN: given field is not a LIST_ENTRY, attempting to "
                #           "continue anyway.")

            lst = obj.Object("_LIST_ENTRY", head, vm)
            seen.add(lst)
            if not lst.is_valid():
                return
            while True:
                if forward:
                    lst = lst.Flink
                else:
                    lst = lst.Blink

                if not lst.is_valid():
                    return

                if lst in seen:
                    break
                else:
                    seen.add(lst)

                nobj = obj.Object(objname, lst.obj_offset - offset, vm)
                yield nobj

        def dt(objct, address = None, space = None, recursive = False, depth = 0):
            """Describe an object or show type info.

            Show the names and values of a complex object (struct). If the name of a
            structure is passed, show the struct's members and their types.

            You can also pass a type name and an address in order to on-the-fly
            interpret a given address as an instance of a particular structure.

            Examples:
                # Dump the current process object
                dt(self._proc)
                # Show the _EPROCESS structure
                dt('_EPROCESS')
                # Overlay an _EPROCESS structure at 0x81234567
                dt('_EPROCESS', 0x81234567)
            """

            profile = (space or self._proc.obj_vm).profile

            if address is not None:
                objct = obj.Object(objct, address, space or self._proc.get_process_address_space())

            try:
                if isinstance(objct, str):
                        size = profile.get_obj_size(objct)
                        membs = [ (profile.get_obj_offset(objct, m), m, profile.vtypes[objct][1][m][1]) for m in profile.vtypes[objct][1] ]
                        print "{0}".format("..." * depth), repr(objct), "({0} bytes)".format(size)
                        for o, m, t in sorted(membs):
                            print "{0}{1:6}: {2:30} {3}".format("..." * depth, hex(o), m, t)
                            if recursive: 
                                if t[0] in profile.vtypes:
                                    dt(t[0], recursive = recursive, depth = depth + 1)
                elif isinstance(objct, obj.BaseObject):
                    membs = [ (o, m) for m, (o, _c) in objct.members.items() ]
                    if not recursive:
                        print repr(objct)
                    offsets = []
                    for o, m in sorted(membs):
                        val = getattr(objct, m)
                        if isinstance(val, list):
                            val = [ str(v) for v in val ]

                        # Handle a potentially callable offset
                        if callable(o):
                            o = o(objct) - objct.obj_offset

                        offsets.append((o, m, val))

                    # Deal with potentially out of order offsets
                    offsets.sort(key = lambda x: x[0])

                    for o, m, val in offsets:
                        try:
                            print "{0}{1:6}: {2:30} {3}".format("..." * depth, hex(o), m, val)
                        except UnicodeDecodeError:
                            print "{0}{1:6}: {2:30} -".format("..." * depth, hex(o), m)
                        if recursive:
                            if val.obj_type in profile.vtypes:
                                dt(val, recursive = recursive, depth = depth + 1)
                elif isinstance(objct, obj.NoneObject):
                    print "ERROR: could not instantiate object"
                    print
                    print "Reason: ", objct.reason
                else:
                    print "ERROR: first argument not an object or known type"
                    print
                    print "Usage:"
                    print
                    hh(dt)
            except TypeError:
                print "Error: could not instantiate object"
                print
                print "Reason: ", "displaying types with dynamic attributes is currently not supported"

        def dis(address, length = 128, space = None, mode = None):
            """Disassemble code at a given address.

            Disassembles code starting at address for a number of bytes
            given by the length parameter (default: 128).

            Note: This feature requires distorm, available at
                http://www.ragestorm.net/distorm/

            The mode is '16bit', '32bit' or '64bit'. If not supplied, the disasm
            mode is taken from the profile. 
            """
            if not sys.modules.has_key("distorm3"):
                print "ERROR: Disassembly unavailable, distorm not found"
                return
            if not space:
                space = self._proc.get_process_address_space()

            if mode == None:
                mode = space.profile.metadata.get('memory_model', '32bit')
            # we'll actually allow the possiblility that someone passed a correct mode
            if mode not in [distorm3.Decode16Bits, distorm3.Decode32Bits, distorm3.Decode64Bits]:
                if mode == '16bit':
                    mode = distorm3.Decode16Bits
                elif mode == '32bit':
                    mode = distorm3.Decode32Bits
                else:
                    mode = distorm3.Decode64Bits
            distorm_mode = mode

            data = space.read(address, length)
            iterable = distorm3.DecodeGenerator(address, data, distorm_mode)
            for (offset, _size, instruction, hexdump) in iterable:
                print "{0:<#8x} {1:<32} {2}".format(offset, hexdump, instruction)

        def find(needle, max = 1, shift = 0, skip = 0, count = False, length = 0x80):
            """Find bytes in the current process's memory
            needle - string or list/tuple of strings to find
            max    - number of results to return; 0 means find all
            shift  - when outputting bytes, start output this many bytes before/after hit offset
            skip   - ignore this many hits
            count  - if True, displays a message reporting how many hits found; only really useful for max == 0
            length - output this many bytes for each hit
            """
            
            if isinstance(needle, basestring):
                needle = [ needle ]
            elif not isinstance(needle, (list, tuple)) or not all([isinstance(x, basestring) for x in needle]):
                print 'Error: needle must be a string or a list/tuple of strings'
                return

            hit_count = 0
            for hit in self._proc.search_process_memory(needle):
                hit_count += 1
                if hit_count > skip:
                    db(hit + shift, length=length)
                    if hit_count - skip == max:
                        break
                    print '-' * 16
            if count:
                print '-' * 16
                print 'Found {} matches.'.format(hit_count - skip)

        shell_funcs = {'find': find, 'cc': cc, 'dd': dd, 'db': db, 'ps': ps, 'dt': dt, 'list_entry': list_entry, 'dis': dis, 'dq': dq, 'modules': modules, 'sc': sc, 'addrspace': addrspace, 'proc': proc, 'getprocs': getprocs, 'getmods': getmods}
        def hh(cmd = None):
            """Get help on a command."""
            shell_funcs['hh'] = hh
            import pydoc
            from inspect import getargspec, formatargspec
            if not cmd:
                print "\nUse addrspace() for Kernel/Virtual AS"
                print "Use addrspace().base for Physical AS"
                print "Use proc() to get the current process object"
                print "  and proc().get_process_address_space() for the current process AS"
                print "  and proc().get_load_modules() for the current process DLLs\n"
                for f in sorted(shell_funcs):
                    doc = pydoc.getdoc(shell_funcs[f])
                    synop, _full = pydoc.splitdoc(doc)
                    print "{0:40} : {1}".format(f + formatargspec(*getargspec(shell_funcs[f])), synop)
                print "\nFor help on a specific command, type 'hh(<command>)'"
            elif type(cmd) == str:
                try:
                    doc = pydoc.getdoc(shell_funcs[cmd])
                except KeyError:
                    print "No such command: {0}".format(cmd)
                    return
                print doc
            else:
                doc = pydoc.getdoc(cmd)
                print doc

        # Break into shell
        banner = "Welcome to volshell! Current memory image is:\n{0}\n".format(self._config.LOCATION)
        banner += "To get help, type 'hh()'"
        try:
            import IPython
            try:
                # New versions of IPython
                IPython.embed()
            except AttributeError:
                # Old versions of IPythom
                shell = IPython.Shell.IPShellEmbed([], banner = banner)
                shell()
        except (AttributeError, ImportError):
            import code, inspect

            frame = inspect.currentframe()

            # Try to enable tab completion
            try:
                import rlcompleter, readline #pylint: disable-msg=W0612
                readline.parse_and_bind("tab: complete")
            except ImportError:
                pass

            # evaluate commands in current namespace
            namespace = frame.f_globals.copy()
            namespace.update(frame.f_locals)

            code.interact(banner = banner, local = namespace)

Example 34

Project: codex-backend
Source File: pescanner.py
View license
    def collect(self):
        count = 0

        for file in self.files:
            out = []

            try:
                FILE = open(file, "rb")
                data = FILE.read()
                FILE.close()
            except:
                continue

            if data == None or len(data) == 0:
                out.append("Cannot read %s (maybe empty?)" % file)
                out.append("")
                continue

            try:
                pe = pefile.PE(data=data, fast_load=True)
                pe.parse_data_directories( directories=[
                    pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_IMPORT'],
                    pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_EXPORT'],
                    pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_TLS'],
                    pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_RESOURCE']])
            except:
                out.append("Cannot parse %s (maybe not PE?)" % file)
                out.append("")
                continue

            out.append(("#" * 80) + "\nRecord %d\n" % count + ("#" * 80))
            out.append(self.header("Meta-data"))
            out.append("File:    %s" % file)
            out.append("Size:    %d bytes" % len(data))
            out.append("Type:    %s" % get_filetype(data))
            out.append("MD5:     %s"  % hashlib.md5(data).hexdigest())
            out.append("SHA1:    %s" % hashlib.sha1(data).hexdigest())
            out.append("ssdeep:  %s" % get_ssdeep(file))
            out.append("Date:    %s" % self.get_timestamp(pe))

            # Alert if the EP section is not in a known good section or if its in the last PE section
            (ep, name, pos) = self.check_ep_section(pe)
            s = "EP:      %s %s %d/%d" % (hex(ep+pe.OPTIONAL_HEADER.ImageBase), name, pos, len(pe.sections))
            if (name not in good_ep_sections) or pos == len(pe.sections):
                s += " [SUSPICIOUS]"
            out.append(s)

            crc_claimed = pe.OPTIONAL_HEADER.CheckSum
            crc_actual  = pe.generate_checksum()
            out.append("CRC:     Claimed: 0x%x, Actual: 0x%x %s" % (
                crc_claimed, crc_actual, "[SUSPICIOUS]" if crc_actual != crc_claimed else ""))

            packers = self.check_packers(pe)
            if len(packers):
                out.append("Packers: %s" % ','.join(packers))

            if sys.modules.has_key('yara'):
                yarahits = self.check_yara(data)
            else:
                yarahits = []

            clamhits = self.check_clam(file)

            if len(yarahits) or len(clamhits):
                out.append(self.header("Signature scans"))
                out.append(yarahits)
                out.append(clamhits)

            callbacks = self.check_tls(pe)
            if len(callbacks):
                out.append(self.header("TLS callbacks"))
                for cb in callbacks:
                    out.append("    0x%x" % cb)

            resources = self.check_rsrc(pe)
            if len(resources):
                out.append(self.header("Resource entries"))
                out.append("%-18s %-8s %-8s %-12s %-24s Type" % ("Name", "RVA", "Size", "Lang", "Sublang"))
                out.append("-" * 80)
                for rsrc in resources.keys():
                    (name,rva,size,type,lang,sublang) = resources[rsrc]
                    out.append("%-18s %-8s %-8s %-12s %-24s %s" % (name, hex(rva), hex(size), lang, sublang, type))

            imports = self.check_imports(pe)
            if len(imports):
                out.append(self.header("Suspicious IAT alerts"))
                for imp in imports:
                    out.append(imp)

            out.append(self.header("Sections"))
            out.append("%-10s %-12s %-12s %-12s %-12s" % ("Name", "VirtAddr", "VirtSize", "RawSize", "Entropy"))
            out.append("-" * 80)

            for sec in pe.sections:
                s = "%-10s %-12s %-12s %-12s %-12f" % (
                        ''.join([c for c in sec.Name if c in string.printable]),
                        hex(sec.VirtualAddress),
                        hex(sec.Misc_VirtualSize),
                        hex(sec.SizeOfRawData),
                        sec.get_entropy())
                if sec.SizeOfRawData == 0 or (sec.get_entropy() > 0 and sec.get_entropy() < 1) or sec.get_entropy() > 7:
                    s += "[SUSPICIOUS]"
                out.append(s)

            verinfo = self.check_verinfo(pe)
            if len(verinfo):
                out.append(self.header("Version info"))
                out.append(verinfo)

            out.append("")
            print '\n'.join(out)
            count += 1

Example 35

Project: PipelineConstructionSet
Source File: language.py
View license
    @classmethod
    def source( cls, script, language='mel' ):
        """use this to source mel or python scripts.
        language : 'mel', 'python'
            When set to 'python', the source command will look for the python equivalent of this mel file, if
            it exists, and attempt to import it. This is particularly useful when transitioning from mel to python
            via mel2py, with this simple switch you can change back and forth from sourcing mel to importing python.

        """

        if language == 'mel':
            cls.eval( """source "%s";""" % script )

        elif language == 'python':
            script = util.path( script )
            modulePath = script.namebase
            folder = script.parent
            print modulePath
            if not sys.modules.has_key(modulePath):
                print "importing"
                module = __import__(modulePath, globals(), locals(), [''])
                sys.modules[modulePath] = module

        else:
            raise TypeError, "language keyword expects 'mel' or 'python'. got '%s'" % language

Example 36

Project: PipelineConstructionSet
Source File: startup.py
View license
def mayaInit(forversion=None) :
    """ Try to init Maya standalone module, use when running pymel from an external Python inerpreter,
    it is possible to pass the desired Maya version number to define which Maya to initialize


    Part of the complexity of initializing maya in standalone mode is that maya does not populate os.environ when
    parsing Maya.env.  If we initialize normally, the env's are available via maya (via the shell), but not in python
    via os.environ.

    Note: the following example assumes that MAYA_SCRIPT_PATH is not set in your shell environment prior to launching
    python or mayapy.

    >>> import maya.standalone            #doctest: +SKIP
    >>> maya.standalone.initialize()      #doctest: +SKIP
    >>> import maya.mel as mm             #doctest: +SKIP
    >>> print mm.eval("getenv MAYA_SCRIPT_PATH")    #doctest: +SKIP
    /Network/Servers/sv-user.luma-pictures.com/luma .....
    >>> import os                         #doctest: +SKIP
    >>> 'MAYA_SCRIPT_PATH' in os.environ  #doctest: +SKIP
    False

    The solution lies in `refreshEnviron`, which copies the environment from the shell to os.environ after maya.standalone
    initializes.

    :rtype: bool
    :return: returns True if maya.cmds required initializing ( in other words, we are in a standalone python interpreter )

    """
    setupFormatting()

    global isInitializing

    # test that Maya actually is loaded and that commands have been initialized,for the requested version

    aboutExists = False
    try :
        from maya.cmds import about
        aboutExists = True
    except ImportError:
        pass
    
    if aboutExists and mayaStartupHasStarted():        
        # if this succeeded, we're initialized
        isInitializing = False
        return False

    _logger.debug( "startup.mayaInit running" )
    # for use with pymel compatible maya package
    os.environ['MAYA_SKIP_USERSETUP_PY'] = 'on'

    if not aboutExists and not sys.modules.has_key('maya.standalone'):
        try :
            import maya.standalone #@UnresolvedImport
            maya.standalone.initialize(name="python")

            if versions.current() < versions.v2009:
                refreshEnviron()
                
        except ImportError, e:
            raise e, str(e) + ": pymel was unable to intialize maya.standalone"

    try:
        from maya.cmds import about
    except Exception:
        _logger.error("maya.standalone was successfully initialized, but pymel failed to import maya.cmds (or it was not populated)")
        raise

    if not mayaStartupHasRun():
        _logger.debug( "running maya.app.startup" )
        # If we're in 'maya -prompt' mode, and a plugin loads pymel, then we
        # can have a state where maya.standalone has been initialized, but
        # the python startup code hasn't yet been run...
        if about(batch=True):
            import maya.app.startup.batch
        else:
            import maya.app.startup.gui

    # return True, meaning we had to initialize maya standalone
    isInitializing = True
    return True

Example 37

Project: PipelineConstructionSet
Source File: language.py
View license
    @classmethod
    def source( cls, script, language='mel' ):
        """use this to source mel or python scripts.
        language : 'mel', 'python'
            When set to 'python', the source command will look for the python equivalent of this mel file, if
            it exists, and attempt to import it. This is particularly useful when transitioning from mel to python
            via mel2py, with this simple switch you can change back and forth from sourcing mel to importing python.

        """

        if language == 'mel':
            cls.eval( """source "%s";""" % script )

        elif language == 'python':
            script = util.path( script )
            modulePath = script.namebase
            folder = script.parent
            print modulePath
            if not sys.modules.has_key(modulePath):
                print "importing"
                module = __import__(modulePath, globals(), locals(), [''])
                sys.modules[modulePath] = module

        else:
            raise TypeError, "language keyword expects 'mel' or 'python'. got '%s'" % language

Example 38

Project: PipelineConstructionSet
Source File: startup.py
View license
def mayaInit(forversion=None) :
    """ Try to init Maya standalone module, use when running pymel from an external Python interpreter,
    it is possible to pass the desired Maya version number to define which Maya to initialize


    Part of the complexity of initializing maya in standalone mode is that maya does not populate os.environ when
    parsing Maya.env.  If we initialize normally, the env's are available via maya (via the shell), but not in python
    via os.environ.

    Note: the following example assumes that MAYA_SCRIPT_PATH is not set in your shell environment prior to launching
    python or mayapy.

    >>> import maya.standalone            #doctest: +SKIP
    >>> maya.standalone.initialize()      #doctest: +SKIP
    >>> import maya.mel as mm             #doctest: +SKIP
    >>> print mm.eval("getenv MAYA_SCRIPT_PATH")    #doctest: +SKIP
    /Network/Servers/sv-user.luma-pictures.com/luma .....
    >>> import os                         #doctest: +SKIP
    >>> 'MAYA_SCRIPT_PATH' in os.environ  #doctest: +SKIP
    False

    The solution lies in `refreshEnviron`, which copies the environment from the shell to os.environ after maya.standalone
    initializes.

    :rtype: bool
    :return: returns True if maya.cmds required initializing ( in other words, we are in a standalone python interpreter )

    """
    _logger.debug( "startup.mayaInit: called" )
    setupFormatting()

    global isInitializing

    # test that Maya actually is loaded and that commands have been initialized,for the requested version

    aboutExists = False
    try :
        from maya.cmds import about
        aboutExists = True
    except ImportError:
        pass

    if aboutExists and mayaStartupHasStarted():
        # if this succeeded, we're initialized
        _logger.debug( "startup.mayaInit: maya already started - exiting" )
        isInitializing = False
        return False

    _logger.debug( "startup.mayaInit: running" )
    # for use with pymel compatible maya package
    os.environ['MAYA_SKIP_USERSETUP_PY'] = 'on'

    if not aboutExists and not sys.modules.has_key('maya.standalone'):
        try :
            _logger.debug( "startup.mayaInit: running standalone.initialize" )
            import maya.standalone #@UnresolvedImport
            maya.standalone.initialize(name="python")

            if versions.current() < versions.v2009:
                refreshEnviron()

        except ImportError, e:
            raise e, str(e) + ": pymel was unable to initialize maya.standalone"

    try:
        from maya.cmds import about
    except Exception:
        _logger.error("maya.standalone was successfully initialized, but pymel failed to import maya.cmds (or it was not populated)")
        raise

    if not mayaStartupHasRun():
        _logger.debug( "running maya.app.startup" )
        # If we're in 'maya -prompt' mode, and a plugin loads pymel, then we
        # can have a state where maya.standalone has been initialized, but
        # the python startup code hasn't yet been run...
        if about(batch=True):
            import maya.app.startup.batch
        else:
            import maya.app.startup.gui

    # return True, meaning we had to initialize maya standalone
    isInitializing = True
    return True

Example 39

Project: numscons
Source File: __init__.py
View license
def platform_module(name = platform_default()):
    """Return the imported module for the platform.

    This looks for a module name that matches the specified argument.
    If the name is unspecified, we fetch the appropriate default for
    our execution environment.
    """
    full_name = 'SCons.Platform.' + name
    if not sys.modules.has_key(full_name):
        if os.name == 'java':
            eval(full_name)
        else:
            try:
                file, path, desc = imp.find_module(name,
                                        sys.modules['SCons.Platform'].__path__)
                try:
                    mod = imp.load_module(full_name, file, path, desc)
                finally:
                    if file:
                        file.close()
            except ImportError:
                try:
                    import zipimport
                    importer = zipimport.zipimporter( sys.modules['SCons.Platform'].__path__[0] )
                    mod = importer.load_module(full_name)
                except ImportError:
                    raise SCons.Errors.UserError, "No platform named '%s'" % name
            setattr(SCons.Platform, name, mod)
    return sys.modules[full_name]

Example 40

Project: autopsy
Source File: utils.py
View license
def rl_get_module(name,dir):
    if sys.modules.has_key(name):
        om = sys.modules[name]
        del sys.modules[name]
    else:
        om = None
    try:
        f = None
        try:
            f, p, desc= imp.find_module(name,[dir])
            return imp.load_module(name,f,p,desc)
        except:
            if isCompactDistro():
                #attempt a load from inside the zip archive
                import zipimport
                dir = _startswith_rl(dir)
                dir = (dir=='.' or not dir) and _archive or os.path.join(_archive,dir.replace('/',os.sep))
                zi = zipimport.zipimporter(dir)
                return zi.load_module(name)
            raise ImportError('%s[%s]' % (name,dir))
    finally:
        if om: sys.modules[name] = om
        del om
        if f: f.close()

Example 41

Project: autopsy
Source File: utils.py
View license
def rl_get_module(name,dir):
    if sys.modules.has_key(name):
        om = sys.modules[name]
        del sys.modules[name]
    else:
        om = None
    try:
        f = None
        try:
            f, p, desc= imp.find_module(name,[dir])
            return imp.load_module(name,f,p,desc)
        except:
            if isCompactDistro():
                #attempt a load from inside the zip archive
                import zipimport
                dir = _startswith_rl(dir)
                dir = (dir=='.' or not dir) and _archive or os.path.join(_archive,dir.replace('/',os.sep))
                zi = zipimport.zipimporter(dir)
                return zi.load_module(name)
            raise ImportError('%s[%s]' % (name,dir))
    finally:
        if om: sys.modules[name] = om
        del om
        if f: f.close()

Example 42

Project: databus
Source File: zipextimporter.py
View license
    def load_module(self, fullname):
        if sys.modules.has_key(fullname):
            mod = sys.modules[fullname]
            if _memimporter.get_verbose_flag():
                sys.stderr.write("import %s # previously loaded from zipfile %s\n" % (fullname, self.archive))
            return mod
        _memimporter.set_find_proc(self.locate_dll_image)
        try:
            return zipimport.zipimporter.load_module(self, fullname)
        except zipimport.ZipImportError:
            pass
        initname = "init" + fullname.split(".")[-1] # name of initfunction
        filename = fullname.replace(".", "\\")
        if filename in ("pywintypes", "pythoncom"):
            filename = filename + "%d%d" % sys.version_info[:2]
            suffixes = ('.dll',)
        else:
            suffixes = self._suffixes
        for s in suffixes:
            path = filename + s
            if path in self._files:
                if _memimporter.get_verbose_flag():
                    sys.stderr.write("# found %s in zipfile %s\n" % (path, self.archive))
                code = self.get_data(path)
                mod = _memimporter.import_module(code, initname, fullname, path)
                mod.__file__ = "%s\\%s" % (self.archive, path)
                mod.__loader__ = self
                if _memimporter.get_verbose_flag():
                    sys.stderr.write("import %s # loaded from zipfile %s\n" % (fullname, mod.__file__))
                return mod
        raise zipimport.ZipImportError, "can't find module %s" % fullname

Example 43

Project: PyChecker
Source File: checker.py
View license
    def __import__(name, globals=None, locals=None, fromlist=None, level=None):
        if globals is None:
            globals = {}
        if locals is None:
            locals = {}
        if fromlist is None:
            fromlist = []

        check = not sys.modules.has_key(name) and name[:10] != 'pychecker.'
        if level:
            pymodule = _orig__import__(name, globals, locals, fromlist, level)
        else:
            pymodule = _orig__import__(name, globals, locals, fromlist)
        if check :
            try :
                # FIXME: can we find a good moduleDir ?
                module = PyCheckerModule(pymodule.__name__)
                if module.initModule(pymodule):
                    warnings = warn.find([module], _cfg, _suppressions)
                    _printWarnings(_get_unique_warnings(warnings))
                else :
                    print 'Unable to load module', pymodule.__name__
            except Exception:
                name = getattr(pymodule, '__name__', utils.safestr(pymodule))
                # FIXME: can we use it here ?
                importError(name)

        return pymodule

Example 44

Project: cyberflex-shell
Source File: shell.py
View license
    def __init__(self, basename):
        """Creates a new shell object with the specified basename. The 
        basename will be used when constructing the history and start-up script
        filenames: ~/.basename.history and ~/.basenamerc.
        
        Note that the start-up script will not be called at instantiation time
        but when starting the main-loop. This is so that you can register your
        own commands first."""
        
        if sys.modules.has_key("readline"):
            histfile = os.path.join(self.find_homedir(), ".%s.history" % basename)
            try:
                readline.read_history_file(histfile)
            except IOError:
                pass
            import atexit
            atexit.register(readline.write_history_file, histfile)
            del histfile
            
            readline.parse_and_bind("tab: complete")
            ## FIXME basenamerc
            
            readline.set_completer(self.complete)
            readline.set_completer_delims("")
        else:
            print >>sys.stderr, "Warning: No readline module available. Most functionality will be missing."
        
        self._commandsets = [] ## This contains command sets, it's a list of (object, dictionary) tuples
        self.basename = basename
        self.env = {"print_backtrace": "true"}
        
        self.register_commands(self)
        self.startup_ran = False
        self.fallback = None
        self.pre_hook = []
        self.post_hook = []
        self.prompt = ""

Example 45

Project: cyberflex-shell
Source File: shell.py
View license
    def complete(self, line, state):
        """Try to complete a command line. Known command names first,
        then programmable completion (if applicable)."""
        
        match = self._commandregex.match(line)
        if not match:
            groups = (None, None)
        else:
            groups = match.groups()
        
        found = -1
        if groups[1] is None:
            ## (Possibly incomplete) command
            command_to_match = groups[0] or ""
            retval = False
            
            for cmdset in self._commandsets: # OK
                cmd_dict = cmdset[1] or cmdset[0].COMMANDS
                for (command, function) in cmd_dict.items():
                    if command[:len(command_to_match)] == command_to_match:
                        found = found + 1
                        if found == state:
                            retval = command
                        
                        ## We break when we know there are at least 2 matches
                        ##  and the correct match according to the current state
                        ##  has been reached. Normally it would be enough
                        ##  to break when found == state, but we want to know
                        ##  if this is the only match, so that we can append a
                        ##  space
                        if found > 0 and found >= state:
                            break 
                
                if found > 0 and found >= state:
                    break 
            
            if found == 0 and sys.modules.has_key("readline"):
                ## There is exactly one command that matches. Append a space
                ##  after it
                return retval + " "
            return retval
            
        else:
            return False
        
        return False

Example 46

Project: DAMM
Source File: volshell.py
View license
    def render_text(self, _outfd, _data):
        self._addrspace = utils.load_as(self._config)

        if not self._config.OFFSET is None:
            self.set_context(offset = self._config.OFFSET)

            self.context_display()

        elif self._config.PID is not None:
            # FIXME: volshell is really not intended to switch into multiple
            # process contexts at once, so it doesn't make sense to use a csv
            # pid list. However, the linux and mac volshell call the respective
            # linux_pslist and mac_pslist which require a csv pidlist. After 
            # the 2.3 release we should close this along with issue 375. 
            pidlist = [int(p) for p in self._config.PID.split(',')]
            for p in pidlist:
                self.set_context(pid = p)
                break
        elif self._config.IMNAME is not None:
            self.set_context(name = self._config.IMNAME)
        else:
            # Just use the first process, whatever it is
            for p in self.getpidlist():
                self.set_context(offset = p.v())
                break

        # Functions inside the shell
        def cc(offset = None, pid = None, name = None, physical = False):
            """Change current shell context.

            This function changes the current shell context to to the process
            specified. The process specification can be given as a virtual address
            (option: offset), PID (option: pid), or process name (option: name).

            If multiple processes match the given PID or name, you will be shown a
            list of matching processes, and will have to specify by offset.
            """
            self.set_context(offset = offset, pid = pid, name = name, physical = physical)

        def db(address, length = 0x80, space = None):
            """Print bytes as canonical hexdump.
            
            This function prints bytes at the given virtual address as a canonical
            hexdump. The address will be translated in the current process context
            (see help on cc for information on how to change contexts).
            
            The length parameter (default: 0x80) specifies how many bytes to print,
            the width parameter (default: 16) allows you to change how many bytes per
            line should be displayed, and the space parameter allows you to
            optionally specify the address space to read the data from.
            """
            if not space:
                space = self._proc.get_process_address_space()
            #if length % 4 != 0:
            #    length = (length+4) - (length%4)
            data = space.read(address, length)
            if not data:
                print "Memory unreadable at {0:08x}".format(address)
                return

            for offset, hexchars, chars in utils.Hexdump(data):
                print "{0:#010x}  {1:<48}  {2}".format(address + offset, hexchars, ''.join(chars))

        def dd(address, length = 0x80, space = None):
            """Print dwords at address.

            This function prints the data at the given address, interpreted as
            a series of dwords (unsigned four-byte integers) in hexadecimal.
            The address will be translated in the current process context
            (see help on cc for information on how to change contexts).
            
            The optional length parameter (default: 0x80) controls how many bytes
            to display, and space allows you to optionally specify the address space
            to read the data from.
            """
            if not space:
                space = self._proc.get_process_address_space()
            # round up to multiple of 4
            if length % 4 != 0:
                length = (length + 4) - (length % 4)
            data = space.read(address, length)
            if not data:
                print "Memory unreadable at {0:08x}".format(address)
                return
            dwords = []
            for i in range(0, length, 4):
                (dw,) = struct.unpack("<L", data[i:i + 4])
                dwords.append(dw)

            if len(dwords) % 4 == 0: lines = len(dwords) / 4
            else: lines = len(dwords) / 4 + 1

            for i in range(lines):
                ad = address + i * 0x10
                lwords = dwords[i * 4:i * 4 + 4]
                print ("{0:08x}  ".format(ad)) + " ".join("{0:08x}".format(l) for l in lwords)

        def dq(address, length = 0x80, space = None):
            """Print qwords at address.

            This function prints the data at the given address, interpreted as
            a series of qwords (unsigned eight-byte integers) in hexadecimal.
            The address will be translated in the current process context
            (see help on cc for information on how to change contexts).
            
            The optional length parameter (default: 0x80) controls how many bytes
            to display, and space allows you to optionally specify the address space
            to read the data from.
            """
            if not space:
                space = self._proc.get_process_address_space()

            # round up 
            if length % 8 != 0:
                length = (length + 8) - (length % 8)

            qwords = obj.Object("Array", targetType = "unsigned long long",
                offset = address, count = length / 8, vm = space)

            if not qwords:
                print "Memory unreadable at {0:08x}".format(address)
                return

            for qword in qwords:
                print "{0:#x} {1:#x}".format(qword.obj_offset, qword.v())

        def ps():
            """Print active processes in a table view.

            Prints a process listing with PID, PPID, image name, and offset.
            """
            self.ps()

        def addrspace():
            """Get the current kernel/virtual address space. 

            This returns the current address space. 
            """
            return self._addrspace

        def proc():
            """Get the current process object.
            
            This returns the current process object. 
            """
            return self._proc 

        def getprocs():
            """Generator of process objects (scripting).

            This returns a list of active process objects.
            """    
            return self.getpidlist()

        def getmods():
            """Generator for kernel modules (scripting).

            This returns a list of loaded kernel module objects.
            """
            return self.getmodules()

        def modules():
            """Print loaded modules in a table view.

            Prints a module listing with base, offset, name etc
            """
            self.modules()

        def sc():
            """Show the current context.
            
            Show the current process information.
            """
            self.context_display()

        def list_entry(head, objname, offset = -1, fieldname = None, forward = True):
            """Traverse a _LIST_ENTRY.

            Traverses a _LIST_ENTRY starting at virtual address head made up of
            objects of type objname. The value of offset should be set to the
            offset of the _LIST_ENTRY within the desired object."""

            vm = self._proc.get_process_address_space()
            seen = set()

            if fieldname:
                offset = vm.profile.get_obj_offset(objname, fieldname)
                #if typ != "_LIST_ENTRY":
                #    print ("WARN: given field is not a LIST_ENTRY, attempting to "
                #           "continue anyway.")

            lst = obj.Object("_LIST_ENTRY", head, vm)
            seen.add(lst)
            if not lst.is_valid():
                return
            while True:
                if forward:
                    lst = lst.Flink
                else:
                    lst = lst.Blink

                if not lst.is_valid():
                    return

                if lst in seen:
                    break
                else:
                    seen.add(lst)

                nobj = obj.Object(objname, lst.obj_offset - offset, vm)
                yield nobj

        def dt(objct, address = None, space = None, recursive = False, depth = 0):
            """Describe an object or show type info.

            Show the names and values of a complex object (struct). If the name of a
            structure is passed, show the struct's members and their types.

            You can also pass a type name and an address in order to on-the-fly
            interpret a given address as an instance of a particular structure.

            Examples:
                # Dump the current process object
                dt(self._proc)
                # Show the _EPROCESS structure
                dt('_EPROCESS')
                # Overlay an _EPROCESS structure at 0x81234567
                dt('_EPROCESS', 0x81234567)
            """

            profile = (space or self._proc.obj_vm).profile

            if address is not None:
                objct = obj.Object(objct, address, space or self._proc.get_process_address_space())

            try:
                if isinstance(objct, str):
                        size = profile.get_obj_size(objct)
                        membs = [ (profile.get_obj_offset(objct, m), m, profile.vtypes[objct][1][m][1]) for m in profile.vtypes[objct][1] ]
                        print "{0}".format("..." * depth), repr(objct), "({0} bytes)".format(size)
                        for o, m, t in sorted(membs):
                            print "{0}{1:6}: {2:30} {3}".format("..." * depth, hex(o), m, t)
                            if recursive: 
                                if t[0] in profile.vtypes:
                                    dt(t[0], recursive = recursive, depth = depth + 1)
                elif isinstance(objct, obj.BaseObject):
                    membs = [ (o, m) for m, (o, _c) in objct.members.items() ]
                    if not recursive:
                        print repr(objct)
                    offsets = []
                    for o, m in sorted(membs):
                        val = getattr(objct, m)
                        if isinstance(val, list):
                            val = [ str(v) for v in val ]

                        # Handle a potentially callable offset
                        if callable(o):
                            o = o(objct) - objct.obj_offset

                        offsets.append((o, m, val))

                    # Deal with potentially out of order offsets
                    offsets.sort(key = lambda x: x[0])

                    for o, m, val in offsets:
                        try:
                            print "{0}{1:6}: {2:30} {3}".format("..." * depth, hex(o), m, val)
                        except UnicodeDecodeError:
                            print "{0}{1:6}: {2:30} -".format("..." * depth, hex(o), m)
                        if recursive:
                            if val.obj_type in profile.vtypes:
                                dt(val, recursive = recursive, depth = depth + 1)
                elif isinstance(objct, obj.NoneObject):
                    print "ERROR: could not instantiate object"
                    print
                    print "Reason: ", objct.reason
                else:
                    print "ERROR: first argument not an object or known type"
                    print
                    print "Usage:"
                    print
                    hh(dt)
            except TypeError:
                print "Error: could not instantiate object"
                print
                print "Reason: ", "displaying types with dynamic attributes is currently not supported"

        def dis(address, length = 128, space = None, mode = None):
            """Disassemble code at a given address.

            Disassembles code starting at address for a number of bytes
            given by the length parameter (default: 128).

            Note: This feature requires distorm, available at
                http://www.ragestorm.net/distorm/

            The mode is '16bit', '32bit' or '64bit'. If not supplied, the disasm
            mode is taken from the profile. 
            """
            if not sys.modules.has_key("distorm3"):
                print "ERROR: Disassembly unavailable, distorm not found"
                return
            if not space:
                space = self._proc.get_process_address_space()

            if mode == None:
                mode = space.profile.metadata.get('memory_model', '32bit')
            # we'll actually allow the possiblility that someone passed a correct mode
            if mode not in [distorm3.Decode16Bits, distorm3.Decode32Bits, distorm3.Decode64Bits]:
                if mode == '16bit':
                    mode = distorm3.Decode16Bits
                elif mode == '32bit':
                    mode = distorm3.Decode32Bits
                else:
                    mode = distorm3.Decode64Bits
            distorm_mode = mode

            data = space.read(address, length)
            iterable = distorm3.DecodeGenerator(address, data, distorm_mode)
            for (offset, _size, instruction, hexdump) in iterable:
                print "{0:<#8x} {1:<32} {2}".format(offset, hexdump, instruction)

        shell_funcs = {'cc': cc, 'dd': dd, 'db': db, 'ps': ps, 'dt': dt, 'list_entry': list_entry, 'dis': dis, 'dq': dq, 'modules': modules, 'sc': sc, 'addrspace': addrspace, 'proc': proc, 'getprocs': getprocs, 'getmods': getmods}
        def hh(cmd = None):
            """Get help on a command."""
            shell_funcs['hh'] = hh
            import pydoc
            from inspect import getargspec, formatargspec
            if not cmd:
                print "\nUse addrspace() for Kernel/Virtual AS"
                print "Use addrspace().base for Physical AS"
                print "Use proc() to get the current process object"
                print "  and proc().get_process_address_space() for the current process AS"
                print "  and proc().get_load_modules() for the current process DLLs\n"
                for f in sorted(shell_funcs):
                    doc = pydoc.getdoc(shell_funcs[f])
                    synop, _full = pydoc.splitdoc(doc)
                    print "{0:40} : {1}".format(f + formatargspec(*getargspec(shell_funcs[f])), synop)
                print "\nFor help on a specific command, type 'hh(<command>)'"
            elif type(cmd) == str:
                try:
                    doc = pydoc.getdoc(shell_funcs[cmd])
                except KeyError:
                    print "No such command: {0}".format(cmd)
                    return
                print doc
            else:
                doc = pydoc.getdoc(cmd)
                print doc

        # Break into shell
        banner = "Welcome to volshell! Current memory image is:\n{0}\n".format(self._config.LOCATION)
        banner += "To get help, type 'hh()'"
        try:
            import IPython
            try:
                # New versions of IPython
                IPython.embed()
            except AttributeError:
                # Old versions of IPythom
                shell = IPython.Shell.IPShellEmbed([], banner = banner)
                shell()
        except (AttributeError, ImportError):
            import code, inspect

            frame = inspect.currentframe()

            # Try to enable tab completion
            try:
                import rlcompleter, readline #pylint: disable-msg=W0612
                readline.parse_and_bind("tab: complete")
            except ImportError:
                pass

            # evaluate commands in current namespace
            namespace = frame.f_globals.copy()
            namespace.update(frame.f_locals)

            code.interact(banner = banner, local = namespace)

Example 47

Project: digest
Source File: utils.py
View license
def rl_get_module(name,dir):
    if sys.modules.has_key(name):
        om = sys.modules[name]
        del sys.modules[name]
    else:
        om = None
    try:
        f = None
        try:
            f, p, desc= imp.find_module(name,[dir])
            return imp.load_module(name,f,p,desc)
        except:
            if isCompactDistro():
                #attempt a load from inside the zip archive
                import zipimport
                dir = _startswith_rl(dir)
                dir = (dir=='.' or not dir) and _archive or os.path.join(_archive,dir.replace('/',os.sep))
                zi = zipimport.zipimporter(dir)
                return zi.load_module(name)
            raise ImportError('%s[%s]' % (name,dir))
    finally:
        if om: sys.modules[name] = om
        del om
        if f: f.close()

Example 48

Project: digest
Source File: utils.py
View license
def rl_get_module(name,dir):
    if sys.modules.has_key(name):
        om = sys.modules[name]
        del sys.modules[name]
    else:
        om = None
    try:
        f = None
        try:
            f, p, desc= imp.find_module(name,[dir])
            return imp.load_module(name,f,p,desc)
        except:
            if isCompactDistro():
                #attempt a load from inside the zip archive
                import zipimport
                dir = _startswith_rl(dir)
                dir = (dir=='.' or not dir) and _archive or os.path.join(_archive,dir.replace('/',os.sep))
                zi = zipimport.zipimporter(dir)
                return zi.load_module(name)
            raise ImportError('%s[%s]' % (name,dir))
    finally:
        if om: sys.modules[name] = om
        del om
        if f: f.close()

Example 49

Project: ReproducibleBuilds
Source File: plugin.py
View license
    def load(self, name, path=None, reload=False):
        if self.plugins.has_key(name) and not reload:
            return self.plugins[name]
        mod_name = name
        if self.prefix:
            #mod_name determines how the module is named in sys.modules
            #Using a prefix helps prevent overlap with other modules
            #(no '.' -- it causes problems)
            mod_name = self.prefix + name
        if sys.modules.has_key(mod_name) and not reload:
            raise koji.PluginError, 'module name conflict: %s' % mod_name
        if path is None:
            path = self.searchpath
        if path is None:
            raise koji.PluginError, "empty module search path"
        file, pathname, description = imp.find_module(name, self.pathlist(path))
        try:
            plugin = imp.load_module(mod_name, file, pathname, description)
        finally:
            file.close()
        self.plugins[name] = plugin
        return plugin

Example 50

Project: ReproducibleBuilds
Source File: plugin.py
View license
    def load(self, name, path=None, reload=False):
        if self.plugins.has_key(name) and not reload:
            return self.plugins[name]
        mod_name = name
        if self.prefix:
            #mod_name determines how the module is named in sys.modules
            #Using a prefix helps prevent overlap with other modules
            #(no '.' -- it causes problems)
            mod_name = self.prefix + name
        if sys.modules.has_key(mod_name) and not reload:
            raise koji.PluginError, 'module name conflict: %s' % mod_name
        if path is None:
            path = self.searchpath
        if path is None:
            raise koji.PluginError, "empty module search path"
        file, pathname, description = imp.find_module(name, self.pathlist(path))
        try:
            plugin = imp.load_module(mod_name, file, pathname, description)
        finally:
            file.close()
        self.plugins[name] = plugin
        return plugin