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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
'''
Care about office's formats
'''
import os
import logging
import zipfile
import fileinput
import subprocess
import xml.dom.minidom as minidom
try:
import cairo
import poppler
except ImportError:
pass
import mat
import parser
import archive
class OpenDocumentStripper(archive.GenericArchiveStripper):
'''
An open document file is a zip, with xml file into.
The one that interest us is meta.xml
'''
def get_meta(self):
'''
Return a dict with all the meta of the file by
trying to read the meta.xml file.
'''
zipin = zipfile.ZipFile(self.filename, 'r')
metadata = {}
try:
content = zipin.read('meta.xml')
dom1 = minidom.parseString(content)
elements = dom1.getElementsByTagName('office:meta')
for i in elements[0].childNodes:
if i.tagName != 'meta:document-statistic':
nodename = ''.join([k for k in i.nodeName.split(':')[1:]])
metadata[nodename] = ''.join([j.data for j in i.childNodes])
else:
# thank you w3c for not providing a nice
# method to get all attributes from a node
pass
zipin.close()
except KeyError: # no meta.xml file found
logging.debug('%s has no opendocument metadata' % self.filename)
return metadata
def _remove_all(self, method):
'''
FIXME ?
There is a patch implementing the Zipfile.remove()
method here : http://bugs.python.org/issue6818
'''
zipin = zipfile.ZipFile(self.filename, 'r')
zipout = zipfile.ZipFile(self.output, 'w', allowZip64=True)
for item in zipin.namelist():
name = os.path.join(self.tempdir, item)
_, ext = os.path.splitext(name)
if item.endswith('manifest.xml'):
# contain the list of all files present in the archive
zipin.extract(item, self.tempdir)
for line in fileinput.input(name, inplace=1):
#remove the line which contains "meta.xml"
line = line.strip()
if not 'meta.xml' in line:
print line
zipout.write(name, item)
elif ext in parser.NOMETA or item == 'mimetype':
#keep NOMETA files, and the "manifest" file
if item != 'meta.xml': # contains the metadata
zipin.extract(item, self.tempdir)
zipout.write(name, item)
else:
zipin.extract(item, self.tempdir)
if os.path.isfile(name):
try:
cfile = mat.create_class_file(name, False,
self.add2archive)
if method == 'normal':
cfile.remove_all()
else:
cfile.remove_all_strict()
logging.debug('Processing %s from %s' % (item,
self.filename))
zipout.write(name, item)
except:
logging.info('%s\' fileformat is not supported' % item)
if self.add2archive:
zipout.write(name, item)
zipout.comment = ''
logging.info('%s treated' % self.filename)
zipin.close()
zipout.close()
self.do_backup()
return True
def is_clean(self):
'''
Check if the file is clean from harmful metadatas
'''
zipin = zipfile.ZipFile(self.filename, 'r')
try:
zipin.getinfo('meta.xml')
except KeyError: # no meta.xml in the file
czf = archive.ZipStripper(self.filename, self.parser,
'application/zip', self.backup, self.add2archive)
if czf.is_clean():
zipin.close()
return True
zipin.close()
return False
class PdfStripper(parser.GenericParser):
'''
Represent a PDF file
'''
def __init__(self, filename, parser, mime, backup, add2archive):
super(PdfStripper, self).__init__(filename, parser, mime, backup,
add2archive)
uri = 'file://' + os.path.abspath(self.filename)
self.password = None
self.document = poppler.document_new_from_file(uri, self.password)
self.meta_list = ('title', 'author', 'subject', 'keywords', 'creator',
'producer', 'metadata')
def is_clean(self):
'''
Check if the file is clean from harmful metadatas
'''
for key in self.meta_list:
if self.document.get_property(key) is not None and \
self.document.get_property(key) != '':
return False
return True
def remove_all(self):
'''
Remove supperficial
'''
return self._remove_meta()
def remove_all_strict(self):
'''
Opening the PDF with poppler, then doing a render
on a cairo pdfsurface for each pages.
Thanks to Lunar^for the idea.
http://cairographics.org/documentation/pycairo/2/
python-poppler is not documented at all : have fun ;)
'''
page = self.document.get_page(0)
page_width, page_height = page.get_size()
surface = cairo.PDFSurface(self.output, page_width, page_height)
context = cairo.Context(surface) # context draws on the surface
logging.debug('PDF rendering of %s' % self.filename)
for pagenum in xrange(self.document.get_n_pages()):
page = self.document.get_page(pagenum)
context.translate(0, 0)
page.render(context) # render the page on context
context.show_page() # draw context on surface
surface.finish()
return self._remove_meta()
def _remove_meta(self):
'''
Remove superficial/external metadata
from a PDF file, using exiftool,
of pdfrw if exiftool is not installed
'''
processed = False
try:# try with pdfrw
import pdfrw
#For now, poppler cannot write meta, so we must use pdfrw
logging.debug('Removing %s\'s superficial metadata' % self.filename)
trailer = pdfrw.PdfReader(self.output)
trailer.Info.Producer = trailer.Author = trailer.Info.Creator = None
writer = pdfrw.PdfWriter()
writer.trailer = trailer
writer.write(self.output)
self.do_backup()
processed = True
except:
pass
try: # try with exiftool
subprocess.Popen('exiftool', stdout=open('/dev/null'))
import exiftool
# Note: '-All=' must be followed by a known exiftool option.
if self.backup:
process = subprocess.Popen(['exiftool', '-m', '-All=',
'-out', self.output, self.filename], stdout=open('/dev/null'))
process.wait()
else:
# Note: '-All=' must be followed by a known exiftool option.
process = subprocess.Popen(
['exiftool', '-All=', '-overwrite_original', self.filename],
stdout=open('/dev/null'))
process.wait()
processed = True
except:
pass
if processed is False:
logging.error('Please install either pdfrw, or exiftool to\
fully handle PDF files')
return processed
def get_meta(self):
'''
Return a dict with all the meta of the file
'''
metadata = {}
for key in self.meta_list:
if self.document.get_property(key) is not None and \
self.document.get_property(key) != '':
metadata[key] = self.document.get_property(key)
return metadata
class OpenXmlStripper(archive.GenericArchiveStripper):
'''
Represent an office openxml document, which is like
an opendocument format, with some tricky stuff added.
It contains mostly xml, but can have media blobs, crap, ...
(I don't like this format.)
'''
def _remove_all(self, method):
'''
FIXME ?
There is a patch implementing the Zipfile.remove()
method here : http://bugs.python.org/issue6818
'''
zipin = zipfile.ZipFile(self.filename, 'r')
zipout = zipfile.ZipFile(self.output, 'w',
allowZip64=True)
for item in zipin.namelist():
name = os.path.join(self.tempdir, item)
_, ext = os.path.splitext(name)
if item.startswith('docProps/'): # metadatas
pass
elif ext in parser.NOMETA or item == '.rels':
#keep parser.NOMETA files, and the file named ".rels"
zipin.extract(item, self.tempdir)
zipout.write(name, item)
else:
zipin.extract(item, self.tempdir)
if os.path.isfile(name): # don't care about folders
try:
cfile = mat.create_class_file(name, False,
self.add2archive)
if method == 'normal':
cfile.remove_all()
else:
cfile.remove_all_strict()
logging.debug('Processing %s from %s' % (item,
self.filename))
zipout.write(name, item)
except:
logging.info('%s\' fileformat is not supported' % item)
if self.add2archive:
zipout.write(name, item)
zipout.comment = ''
logging.info('%s treated' % self.filename)
zipin.close()
zipout.close()
self.do_backup()
return True
def is_clean(self):
'''
Check if the file is clean from harmful metadatas
'''
zipin = zipfile.ZipFile(self.filename, 'r')
for item in zipin.namelist():
if item.startswith('docProps/'):
return False
zipin.close()
czf = archive.ZipStripper(self.filename, self.parser,
'application/zip', self.backup, self.add2archive)
if not czf.is_clean():
return False
else:
return True
def get_meta(self):
'''
Return a dict with all the meta of the file
'''
zipin = zipfile.ZipFile(self.filename, 'r')
metadata = {}
for item in zipin.namelist():
if item.startswith('docProps/'):
metadata[item] = 'harmful content'
zipin.close()
return metadata
|