-
Notifications
You must be signed in to change notification settings - Fork 0
/
metahachoir.py
91 lines (80 loc) · 3.04 KB
/
metahachoir.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# DFF -- An Open Source Digital Forensics Framework
# Copyright (C) 2009-2013 ArxSys
#
# This program is free software, distributed under the terms of
# the GNU General Public License Version 2. See the LICENSE file
# at the top of the source tree.
#
# See http://www.digital-forensic.org for more information about this
# project. Please do not directly contact any of the maintainers of
# DFF for assistance; the project provides a web site, mailing lists
# and IRC channels for your use.
#
# Author(s):
# Johannes Binder <j.binder.x@gmail.com>
__dff_module_metahachoir_version__ = "1.0.1"
from dff.api.module.script import Script
from dff.api.module.module import Module
from dff.api.module.manager import ModuleProcessusHandler
from dff.api.types.libtypes import Variant, VMap, Argument, typeId, vtime
from dff.api.vfs.libvfs import AttributesHandler
from hachoir_core.error import HachoirError
from hachoir_metadata import extractMetadata
from hachoir_parser import guessParser
from hachoir_parser import QueryParser
from hachoir_core.stream.input import StringInputStream
class HachoirHandler(AttributesHandler):
def __init__(self):
AttributesHandler.__init__(self, "hachoir")
self.__disown__()
def attributes(self, node):
attr = VMap()
attr.thisown = False
file = node.open()
parser = guessParser(StringInputStream(file.read()))
file.close()
if not parser:
attr["info"] = Variant("unable to read metadata")
return attr
try:
metadata = extractMetadata(parser)
for data in metadata:
if not(any(data.values)):
continue
attr[data.key] = Variant("; ".join([str(val.value) for val in data.values]))
except HachoirError, err:
attr["info"] = Variant("error while reading metadata")
return attr
class MetaHachoir(Script):
def __init__(self):
Script.__init__(self, "metahachoir")
self.handler = HachoirHandler()
def start(self, args):
try:
node = args['file'].value()
self.stateinfo = "Registering node: " + str(node.name())
node.registerAttributes(self.handler)
except KeyError:
pass
@staticmethod
def getSupportedFileExtensions():
extensions = list()
for parser in QueryParser([]):
file_ext = parser.getParserTags().get("file_ext")
if not file_ext:
continue
extensions.extend(file_ext)
return list(set(extensions) - set(['']))
class metahachoir(Module):
"""This module shows metadata provided by Hachoir in node attributes"""
def __init__(self):
Module.__init__(self, "metahachoir", MetaHachoir)
self.conf.addArgument({"name": "file",
"description": "file for extracting metadata",
"input": Argument.Required|Argument.Single|typeId.Node})
self.conf.addConstant({"name": "extension-type",
"type": typeId.String,
"description" : "compatible extensions",
"values" : MetaHachoir.getSupportedFileExtensions()})
self.flags = ["single"]
self.tags = "Metadata"