python类Magic()的实例源码

magic_wrap.py 文件源码 项目:rebus 作者: airbus-seclab 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def from_file(fname, uncompress=False):
    if native:
        return ms.file(fname)
    else:
        m = magic.Magic(uncompress)
        return m.from_file(fname)
magic_wrap.py 文件源码 项目:rebus 作者: airbus-seclab 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def from_buffer(data, uncompress=False):
    if native:
        return ms.buffer(data)
    else:
        m = magic.Magic(uncompress)
        return m.from_buffer(data)
file.py 文件源码 项目:diffoscope 作者: ReproducibleBuilds 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def guess_file_type(self, path):
            if not hasattr(self, '_mimedb'):
                self._mimedb = magic.Magic()
            return maybe_decode(self._mimedb.from_file(path))
file.py 文件源码 项目:diffoscope 作者: ReproducibleBuilds 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def guess_encoding(self, path):
            if not hasattr(self, '_mimedb_encoding'):
                self._mimedb_encoding = magic.Magic(mime_encoding=True)
            return maybe_decode(self._mimedb_encoding.from_file(path))
audiotools.py 文件源码 项目:kur 作者: deepgram 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def get_mime_type(filename):
    """ Returns the MIME type associated with a particular audio file.
    """
    try:
        import magic
    except ImportError:
        if get_mime_type.warn:
            logger.warning('Python package "magic" could not be loaded, '
                'possibly because system library "libmagic" could not be '
                'found. We are falling back on our own heuristics.')
            get_mime_type.warn = False

        ext = os.path.splitext(filename)[1].lower()
        return {
            '.wav' : 'audio/x-wav',
            '.mp3' : 'audio/mpeg',
            '.flac' : 'audio/x-flac'
        }.get(ext, 'unknown')
    else:
        # Read off magic numbers and return MIME types
        mime_magic = magic.Magic(mime=True)
        ftype = mime_magic.from_file(filename)
        if isinstance(ftype, bytes):
            ftype = ftype.decode('utf-8')

        # If we are dealing with a symlink, read the link
        # and try again with the target file.  We do this in 
        # a while loop to cover the case of symlinks which
        # point to other symlinks
        current_filename = filename
        while ftype == 'inode/symlink':
            current_filename = os.readlink(current_filename)
            ftype = mime_magic.from_file(current_filename)
            ftype = ftype.decode('utf-8') if isinstance(ftype, bytes) else ftype

        return ftype
objects.py 文件源码 项目:Snakepit 作者: K4lium 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def get_mime(self):
        try:
            ms = magic.open(magic.MIME)
            ms.load()
            mime_type = ms.file(self.path)
        except:
            try:
                mime = magic.Magic(mime=True)
                mime_type = mime.from_file(self.path)
            except:
                return ''

        return mime_type
new_project.py 文件源码 项目:s2e-env 作者: S2E 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def handle(self, *args, **options):
        # Need an absolute path for the target in order to simplify
        # symlink creation.
        target_path = options['target'][0]
        target_path = os.path.realpath(target_path)

        # Check that the target actually exists
        if not os.path.isfile(target_path):
            raise CommandError('Target %s does not exist' % target_path)

        default_magic = Magic()
        magic_checks = [
            (Magic(magic_file=CGC_MAGIC), CGC_REGEX, CGCProjectConfiguration, 'i386'),
            (default_magic, ELF32_REGEX, LinuxProjectConfiguration, 'i386'),
            (default_magic, ELF64_REGEX, LinuxProjectConfiguration, 'x86_64'),
            (default_magic, DLL32_REGEX, WindowsDLLProjectConfiguration, 'i386'),
            (default_magic, DLL64_REGEX, WindowsDLLProjectConfiguration, 'x86_64'),
            (default_magic, PE32_REGEX, WindowsProjectConfiguration, 'i386'),
            (default_magic, PE64_REGEX, WindowsProjectConfiguration, 'x86_64'),
            (default_magic, MSDOS_REGEX, WindowsProjectConfiguration, 'i386')
        ]

        # Check the target program against the valid file types
        for magic_check, regex, proj_config_class, arch in magic_checks:
            magic = magic_check.from_file(target_path)
            matches = regex.match(magic)

            # If we find a match, create that project. The user instructions
            # are returned
            if matches:
                options['target'] = target_path
                options['target_arch'] = arch

                return call_command(Project(proj_config_class), **options)

        # Otherwise no valid file type was found
        raise CommandError('%s is not a valid target for S2E analysis' %
                           target_path)
file-magic.py 文件源码 项目:Beta 作者: DidierStevens 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def __init__(self):
        self.oMagic=magic.Magic(magic_file=r'C:\Program Files (x86)\GnuWin32\share\misc\magic')

        filename = os.path.join(os.path.dirname(sys.argv[0]), 'file-magic.def')
        if os.path.isfile(filename):
#        self.oMagicCustom=magic.Magic(magic_file=r'mymagic', keep_going=True)
            self.oMagicCustom = magic.Magic(magic_file=filename)
        else:
            print('Warning: custom magic file not found: %s' % filename)
            self.oMagicCustom = None
validators.py 文件源码 项目:django-cloudinary-storage 作者: klis87 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def validate_video(value):
    if os.name == 'nt':
        magic_object = magic.Magic(magic_file='magic', mime=True)
        mime = magic_object.from_buffer(value.file.read(1024))
    else:
        mime = magic.from_buffer(value.file.read(1024), mime=True)
    value.file.seek(0)
    if not mime.startswith('video/'):
        raise ValidationError(_(app_settings.INVALID_VIDEO_ERROR_MESSAGE))
apk.py 文件源码 项目:ApkParser 作者: yigitozgumus 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def get_files_types(self):
        """
            Return the files inside the APK with their associated types (by using python-magic)

            :rtype: a dictionnary
        """
        try:
            import magic
        except ImportError:
            # no lib magic !
            for i in self.get_files():
                buffer = self.zip.read(i)
                self.files_crc32[i] = crc32(buffer)
                self.files[i] = "Unknown"
            return self.files

        if self.files != {}:
            return self.files

        builtin_magic = 0
        try:
            getattr(magic, "MagicException")
        except AttributeError:
            builtin_magic = 1

        if builtin_magic:
            ms = magic.open(magic.MAGIC_NONE)
            ms.load()

            for i in self.get_files():
                buffer = self.zip.read(i)
                self.files[i] = ms.buffer(buffer)
                self.files[i] = self._patch_magic(buffer, self.files[i])
                self.files_crc32[i] = crc32(buffer)
        else:
            m = magic.Magic(magic_file=self.magic_file)
            for i in self.get_files():
                buffer = self.zip.read(i)
                self.files[i] = m.from_buffer(buffer)
                self.files[i] = self._patch_magic(buffer, self.files[i])
                self.files_crc32[i] = crc32(buffer)

        return self.files
apk.py 文件源码 项目:Tacita 作者: Quantika14 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def get_files_types(self):
        """
            Return the files inside the APK with their associated types (by using python-magic)

            :rtype: a dictionnary
        """
        try:
            import magic
        except ImportError:
            # no lib magic !
            for i in self.get_files():
                buffer = self.zip.read(i)
                self.files_crc32[i] = crc32(buffer)
                self.files[i] = "Unknown"
            return self.files

        if self.files != {}:
            return self.files

        builtin_magic = 0
        try:
            getattr(magic, "MagicException")
        except AttributeError:
            builtin_magic = 1

        if builtin_magic:
            ms = magic.open(magic.MAGIC_NONE)
            ms.load()

            for i in self.get_files():
                buffer = self.zip.read(i)
                self.files[i] = ms.buffer(buffer)
                self.files[i] = self._patch_magic(buffer, self.files[i])
                self.files_crc32[i] = crc32(buffer)
        else:
            m = magic.Magic(magic_file=self.magic_file)
            for i in self.get_files():
                buffer = self.zip.read(i)
                self.files[i] = m.from_buffer(buffer)
                self.files[i] = self._patch_magic(buffer, self.files[i])
                self.files_crc32[i] = crc32(buffer)

        return self.files
apk.py 文件源码 项目:android_malware_detection 作者: congyuandong 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def get_files_types(self):
        """
            Return the files inside the APK with their associated types (by using python-magic)

            :rtype: a dictionnary
        """
        try:
            import magic
        except ImportError:
            # no lib magic !
            for i in self.get_files():
                buffer = self.zip.read(i)
                self.files_crc32[i] = crc32(buffer)
                self.files[i] = "Unknown"
            return self.files

        if self.files != {}:
            return self.files

        builtin_magic = 0
        try:
            getattr(magic, "MagicException")
        except AttributeError:
            builtin_magic = 1

        if builtin_magic:
            ms = magic.open(magic.MAGIC_NONE)
            ms.load()

            for i in self.get_files():
                buffer = self.zip.read(i)
                self.files[i] = ms.buffer(buffer)
                if self.files[i] is None:
                    self.files[i] = "Unknown"
                else:
                    self.files[i] = self._patch_magic(buffer, self.files[i])
                self.files_crc32[i] = crc32(buffer)
        else:
            m = magic.Magic(magic_file=self.magic_file)
            for i in self.get_files():
                buffer = self.zip.read(i)
                self.files[i] = m.from_buffer(buffer)
                if self.files[i] is None:
                    self.files[i] = "Unknown"
                else:
                    self.files[i] = self._patch_magic(buffer, self.files[i])
                self.files_crc32[i] = crc32(buffer)

        return self.files
apk.py 文件源码 项目:android_malware_detection 作者: congyuandong 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def get_files_types(self):
        """
            Return the files inside the APK with their associated types (by using python-magic)

            :rtype: a dictionnary
        """
        try:
            import magic
        except ImportError:
            # no lib magic !
            for i in self.get_files():
                buffer = self.zip.read(i)
                self.files_crc32[i] = crc32(buffer)
                self.files[i] = "Unknown"
            return self.files

        if self.files != {}:
            return self.files

        builtin_magic = 0
        try:
            getattr(magic, "MagicException")
        except AttributeError:
            builtin_magic = 1

        if builtin_magic:
            ms = magic.open(magic.MAGIC_NONE)
            ms.load()

            for i in self.get_files():
                buffer = self.zip.read(i)
                self.files[i] = ms.buffer(buffer)
                if self.files[i] is None:
                    self.files[i] = "Unknown"
                else:
                    self.files[i] = self._patch_magic(buffer, self.files[i])
                self.files_crc32[i] = crc32(buffer)
        else:
            m = magic.Magic(magic_file=self.magic_file)
            for i in self.get_files():
                buffer = self.zip.read(i)
                self.files[i] = m.from_buffer(buffer)
                if self.files[i] is None:
                    self.files[i] = "Unknown"
                else:
                    self.files[i] = self._patch_magic(buffer, self.files[i])
                self.files_crc32[i] = crc32(buffer)

        return self.files
views.py 文件源码 项目:docsbox 作者: dveselov 项目源码 文件源码 阅读 62 收藏 0 点赞 0 评论 0
def post(self):
        """
        Recieves file and options, checks file mimetype,
        validates options and creates converting task
        """
        if "file" not in request.files:
            return abort(400, message="file field is required")
        else:
            with NamedTemporaryFile(delete=False, prefix=app.config["MEDIA_PATH"]) as tmp_file:
                request.files["file"].save(tmp_file)
                remove_file.schedule(
                    datetime.timedelta(seconds=app.config["ORIGINAL_FILE_TTL"])
                , tmp_file.name)
                with Magic() as magic: # detect mimetype
                    mimetype = magic.from_file(tmp_file.name)
                    if mimetype not in app.config["SUPPORTED_MIMETYPES"]:
                        return abort(400, message="Not supported mimetype: '{0}'".format(mimetype))
                options = request.form.get("options", None)
                if options: # options validation
                    options = ujson.loads(options)
                    formats = options.get("formats", None)
                    if not isinstance(formats, list) or not formats:
                        return abort(400, message="Invalid 'formats' value")
                    else:
                        for fmt in formats:
                            supported = (fmt in app.config["SUPPORTED_MIMETYPES"][mimetype]["formats"])
                            if not supported:
                                message = "'{0}' mimetype can't be converted to '{1}'"
                                return abort(400, message=message.format(mimetype, fmt))
                    thumbnails = options.get("thumbnails", None)
                    if thumbnails:
                        if not isinstance(thumbnails, dict):
                            return abort(400, message="Invalid 'thumbnails' value")
                        else:
                            thumbnails_size = thumbnails.get("size", None)
                            if not isinstance(thumbnails_size, str) or not thumbnails_size:
                                return abort(400, message="Invalid 'size' value")
                            else:
                                try:
                                    (width, height) = map(int, thumbnails_size.split("x"))
                                except ValueError:
                                    return abort(400, message="Invalid 'size' value")
                                else:
                                    options["thumbnails"]["size"] = (width, height)
                else:
                    if mimetype == "application/pdf":
                        options = {
                            "formats": ["html"]
                        }
                    else:
                        options = app.config["DEFAULT_OPTIONS"]
                task = process_document.queue(tmp_file.name, options, {
                    "mimetype": mimetype,
                })
        return {
            "id": task.id,
            "status": task.status,
        }
views.py 文件源码 项目:aeon-ztps 作者: Apstra 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def browse(root):
    """ Lists root, files, and file names in root folder

    Args:
        directory (str): Directory to list files for

    """
    path = os.path.join(_AEON_TOPDIR, root)

    if not os.access(path, os.R_OK):
        flash('Access Denied reading {path}'.format(path=path), 'warning')
        root = '/'

    if root == '/':
        dirnames = []
        for path in valid_paths():
            dirnames.append(path.strip('/'))
        parent = '/'
        filenames = []
        dirpath = '/'
        return render_template('browse.html', dirpath=dirpath, dirnames=dirnames, filenames=filenames, parent=parent)

    folder = os.walk(path).next()
    parent = os.path.join(path, os.pardir).split(_AEON_TOPDIR).pop().strip('/')
    dirpath = folder[0].split(_AEON_TOPDIR).pop().strip('/')
    dirnames = [x.split(_AEON_TOPDIR).pop() for x in folder[1]]
    mime = magic.Magic(mime=True)

    files = []
    for filename in folder[2]:
        f = os.path.join(_AEON_TOPDIR, root, filename)
        # If it's a symlink we want to follow it for accurate stats
        # if os.path.islink(f):
        #     # works for absolute paths
        #     # TODO: fix for relative symlink
        #     f = os.readlink(f)

        stat = os.stat(f)
        mimetype = mime.from_file(f)
        # icon = get_icon(mimetype)
        size = stat.st_size
        mtime = stat.st_mtime
        ctime = stat.st_ctime
        name = filename
        files.append({'name': name, 'size': size, 'mtime': mtime, 'ctime': ctime, 'mimetype': mimetype, 'link': f})

    return render_template('browse.html', dirpath=dirpath, dirnames=dirnames, files=files, parent=parent)


# TODO: constraint to POST or DELETE
apk.py 文件源码 项目:DroidWatcher 作者: suemi994 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def get_files_types(self):
        """
            Return the files inside the APK with their associated types (by using python-magic)

            :rtype: a dictionnary
        """
        try:
            import magic
        except ImportError:
            # no lib magic !
            for i in self.get_files():
                buffer = self.zip.read(i)
                self.files_crc32[i] = crc32(buffer)
                self.files[i] = "Unknown"
            return self.files

        if self.files != {}:
            return self.files

        builtin_magic = 0
        try:
            getattr(magic, "MagicException")
        except AttributeError:
            builtin_magic = 1

        if builtin_magic:
            ms = magic.open(magic.MAGIC_NONE)
            ms.load()

            for i in self.get_files():
                buffer = self.zip.read(i)
                self.files[i] = ms.buffer(buffer)
                self.files[i] = self._patch_magic(buffer, self.files[i])
                self.files_crc32[i] = crc32(buffer)
        else:
            m = magic.Magic(magic_file=self.magic_file)
            for i in self.get_files():
                buffer = self.zip.read(i)
                self.files[i] = m.from_buffer(buffer)
                self.files[i] = self._patch_magic(buffer, self.files[i])
                self.files_crc32[i] = crc32(buffer)

        return self.files
apk.py 文件源码 项目:DroidWatcher 作者: suemi994 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def get_files_types(self):
        """
            Return the files inside the APK with their associated types (by using python-magic)

            :rtype: a dictionnary
        """
        try:
            import magic
        except ImportError:
            # no lib magic !
            for i in self.get_files():
                buffer = self.zip.read(i)
                self.files_crc32[i] = crc32(buffer)
                self.files[i] = "Unknown"
            return self.files

        if self.files != {}:
            return self.files

        builtin_magic = 0
        try:
            getattr(magic, "MagicException")
        except AttributeError:
            builtin_magic = 1

        if builtin_magic:
            ms = magic.open(magic.MAGIC_NONE)
            ms.load()

            for i in self.get_files():
                buffer = self.zip.read(i)
                self.files[i] = ms.buffer(buffer)
                self.files[i] = self._patch_magic(buffer, self.files[i])
                self.files_crc32[i] = crc32(buffer)
        else:
            m = magic.Magic(magic_file=self.magic_file)
            for i in self.get_files():
                buffer = self.zip.read(i)
                self.files[i] = m.from_buffer(buffer)
                self.files[i] = self._patch_magic(buffer, self.files[i])
                self.files_crc32[i] = crc32(buffer)

        return self.files
__init__.py 文件源码 项目:symhash 作者: threatstream 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def create_sym_hash(filename=None, data=None):
    # create the sym hash
    if filename:
        with open(filename, 'rb') as f:
            data = f.read()

    if not data:
        return

    with magic.Magic() as m:
        filetype = m.id_buffer(data[0:1000])

    if 'Mach-O' not in filetype:
        print("Data provided is not a valid Mach-O filetype")
        return

    macho_parser = MachOParser(data)

    try:
        macho_parser.parse()
    except MachOParserError as e:
        print("Error {}".format(e))
        return

    sym_dict = {}

    for entity in macho_parser.entities:
        if entity.magic_str != 'Universal':

            entity_string = "{} {} {}".format(entity.cpu_type_str,
                                              entity.filetype_str,
                                              entity.magic_str)

            sym_list = []

            for cmd in entity.cmdlist:
                if cmd['cmd'] == MachOEntity.LC_SYMTAB:
                    for sym in cmd['symbols']:
                        if not sym['is_stab']:
                            if sym['external'] is True:
                                if sym['n_type'] == '0x00':
                                    sym_list.append(sym.get('string', '').decode())

            symhash = md5(','.join(sorted(sym_list)).encode()).hexdigest()
            sym_dict[entity_string] = symhash

    return sym_dict


问题


面经


文章

微信
公众号

扫码关注公众号