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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
#!/usr/bin/env python
'''
Metadata anonymisation toolkit library
'''
import os
import subprocess
import logging
import mimetypes
import hachoir_core.cmd_line
import hachoir_parser
import hachoir_editor
import images
import audio
import office
import archive
__version__ = '0.1'
__author__ = 'jvoisin'
LOGGING_LEVEL = logging.DEBUG
logging.basicConfig(level=LOGGING_LEVEL)
strippers = {
hachoir_parser.image.JpegFile: images.JpegStripper,
hachoir_parser.image.PngFile: images.PngStripper,
hachoir_parser.audio.MpegAudioFile: audio.MpegAudioStripper,
hachoir_parser.misc.PDFDocument: office.PdfStripper,
hachoir_parser.archive.TarFile: archive.TarStripper,
hachoir_parser.archive.gzip_parser.GzipParser: archive.GzipStripper,
hachoir_parser.archive.bzip2_parser.Bzip2Parser: archive.Bzip2Stripper,
hachoir_parser.archive.zip.ZipFile: archive.ZipStripper,
}
def secure_remove(filename):
'''
securely remove the file
'''
try:
subprocess.call('shred --remove %s' % filename, shell=True)
except:
logging.error('Unable to remove %s' % filename)
def is_secure(filename):
'''
Prevent shell injection
'''
if not(os.path.isfile(filename)): # check if the file exist
logging.error('Error: %s is not a valid file' % filename)
return False
def create_class_file(name, backup, add2archive):
'''
return a $FILETYPEStripper() class,
corresponding to the filetype of the given file
'''
if is_secure(name):
return
filename = ''
realname = name
try:
filename = hachoir_core.cmd_line.unicodeFilename(name)
except TypeError: # get rid of "decoding Unicode is not supported"
filename = name
parser = hachoir_parser.createParser(filename)
if not parser:
logging.info('Unable to parse %s' % filename)
return
editor = hachoir_editor.createEditor(parser)
try:
'''this part is a little tricky :
stripper_class will receice the name of the class $FILETYPEStripper,
(which herits from the "file" class), based on the editor
of given file (name)
'''
stripper_class = strippers[editor.input.__class__]
except KeyError:
#Place for another lib than hachoir
logging.info('Don\'t have stripper for format %s' % editor.description)
return
if editor.input.__class__ == hachoir_parser.misc.PDFDocument: # pdf
return stripper_class(filename, realname, backup)
elif editor.input.__class__ == hachoir_parser.archive.zip.ZipFile:
#zip based format
mime = mimetypes.guess_type(filename)[0]
try: # ugly workaround, cleaning open document delete mime (wtf?)
if mime.startswith('application/vnd.oasis.opendocument'):
return office.OpenDocumentStripper(realname, filename, parser,
editor, backup, add2archive)
else: # normal zip
return stripper_class(realname, filename, parser, editor,
backup, add2archive)
except: # normal zip
return stripper_class(realname, filename, parser, editor, backup,
add2archive)
else: # normal handling
return stripper_class(realname, filename, parser, editor, backup,
add2archive)
|