/
xml_creator.py
executable file
·296 lines (265 loc) · 9.45 KB
/
xml_creator.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
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
import os
import re
import sys
import lxml.etree as ET
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfparser import PDFParser
from pdfminer.pdftypes import PDFStream, PDFObjRef, PDFNotImplementedError
from pdfminer.pdftypes import PDFObjectNotFound
from pdfminer.psparser import PSKeyword, PSLiteral
from pdfminer.utils import isnumber
#from JSAnalysis import isJavascript
from util.str_utils import isFlash
from scandir import scandir
'''
Parse a pdf and build an xml tree based on the object structure
'''
class FrankenParser(object):
def __init__(self, pdf, debug=False):
self.errors = ''
self.debug = debug
self.pdf = pdf
self.xml = ''
self.javascript = []
self.deobfuscated = []
self.swf = []
self.found_eof = False
self.bin_blob = ''
self.malformed = {}
self.parse()
self.tree = self.tree_from_xml(self.xml)
def e(self, s):
ESC_PAT = re.compile(r'[\000-\037&<>()"\042\047\134\177-\377]')
return ESC_PAT.sub(lambda m:'&#%d;' % ord(m.group(0)), s)
'''
Convert a pdf object into xml
'''
def dump(self, obj):
res = ""
if obj is None:
res += '<null />'
return res
if isinstance(obj, dict):
res += '<dict size="%' + str(len(obj)) + '">\n'
for (k,v) in obj.iteritems():
k = re.sub(r'\W+', '', k)
if k.isdigit() or not k:
k = 'xml_creator_' + k
res += '<' + k + '>'
res += self.dump(v)
res += '</' + k + '>\n'
res += '</dict>'
return res
if isinstance(obj, list):
res += '<list size="' + str(len(obj)) + '">\n'
for v in obj:
res += self.dump(v)
res += '\n'
res += '</list>'
return res
if isinstance(obj, str):
self.check_js(obj)
#encode base64 to avoid illegal xml characters
res += '<string>' + self.e(obj).encode('base64') + '</string>'
return res
if isinstance(obj, PDFStream):
res += '<stream>\n'
try:
res += '<props>\n'
res += self.dump(obj.attrs)
res += '\n</props>\n'
data = obj.get_data()
self.check_js(str(data))
self.check_swf(str(data))
res += '<data size="' + str(len(data)) + '">' + self.e(data).encode('base64') + '</data>\n'
#Throws an exception if the filter is unsupported, etc
except Exception as e:
#print e.message
res += '<StreamException>%s</StreamException>' % str(e)
#make sure the tag is closed appropriately
res += '</stream>'
return res
if isinstance(obj, PDFObjRef):
res += '<ref id="' + str(obj.objid) + '" />'
return res
if isinstance(obj, PSKeyword):
self.check_js(obj.name)
res += '<keyword>' + obj.name + '</keyword>'
return res
if isinstance(obj, PSLiteral):
self.check_js(obj.name)
res += '<literal>' + obj.name + '</literal>'
return res
if isnumber(obj):
self.check_js(str(obj))
res += '<number>' + str(obj) + '</number>'
return res
raise TypeError(obj)
'''
Add the PDF trailers to the xml
'''
def dumptrailers(self, doc):
res = ""
for xref in doc.xrefs:
res += '<trailer>\n'
res += self.dump(xref.trailer)
res += '\n</trailer>\n\n'
return res
'''
Records information into a dictionary.
All key values are lists, and the paramter is appended.
'''
def takenote(self, dic, key, val):
try:
dic[key].append(val)
except KeyError:
dic[key] = []
dic[key].append(val)
def getmalformed(self, key=''):
if not key:
return self.malformed
else:
return self.malformed.get(key)
'''
Parse the pdf and build the xml
'''
def parse (self):
fp = file(self.pdf, 'rb')
parser = PDFParser(fp, dbg=self.debug)
doc = PDFDocument(parser, dbg=self.debug)
#extract blob of data after EOF (if it exists)
if doc.found_eof and doc.eof_distance > 3:
self.bin_blob = parser.read_from_end(doc.eof_distance)
res = '<pdf>'
visited = set() #keep track of the objects already visited
for xref in doc.xrefs:
for objid in xref.get_objids():
if objid in visited:
continue
if objid == 21 or objid == 67:
print objid
visited.add(objid)
try:
obj = doc.getobj(objid)
res += '<object id="' + str(objid) + '">\n'
res += self.dump(obj)
res += '\n</object>\n\n'
except PDFObjectNotFound as e:
mal_obj = parser.read_n_from(xref.get_pos(objid)[1], 4096)
mal_obj = mal_obj.replace('<', '0x3C')
res += '<object id="%d" type="malformed">\n%s\n</object>\n\n' % (objid, mal_obj)
self.takenote(self.malformed, 'objects', objid)
except Exception as e:
res += '<object id="%d" type="exception">\n%s\n</object>\n\n' % (objid, e.message)
fp.close()
res += self.dumptrailers(doc)
res += '</pdf>'
self.xml=res
self.errors = doc.errors
self.bytes_read = parser.BYTES
return
'''
Check string for javascript content
'''
def check_js (self, content):
'''
if isJavascript(content):
#pull out js between script tags
reJSscript = '<script[^>]*?contentType\s*?=\s*?[\'"]application/x-javascript[\'"][^>]*?>(.*?)</script>'
res = re.findall(reJSscript, content, re.DOTALL | re.IGNORECASE)
if res != []:
self.javascript.append('\n'.join(res))
else:
self.javascript.append(content)
'''
return
'''
Check string for flash content
'''
def check_swf(self, content):
if isFlash(content):
self.swf.append(content)
return
'''
Create an lxml tree from the xml string
'''
def tree_from_xml (self, xml):
try:
tree = ET.fromstring(xml)
return tree
except Exception as e:
sys.stderr.write("xml_creator cannot create tree: %s\n" % e)
return 'TREE_ERROR: %s' % str(e)
'''
Calls edges to recursively create the graph string
'''
def make_graph(self, tree):
res = []
if tree:
self.edges(tree, res, 0)
return res
'''
creates string showing connections between objects
'''
def edges(self, parent, output, id):
for child in list(parent):
if isinstance(child, str):
return
elif child.get("id") != None:
cid = child.get("id")
output.append(str(id) + ' ' + cid + '\n')
self.edges(child, output, cid)
else:
res = self.edges(child, output, id)
return
if __name__ == "__main__":
try:
dirin = sys.argv[1]
dirout = sys.argv[2]
except IndexError:
sys.exit(0)
else:
if not os.path.isdir(dirin) or not os.path.isdir(dirout):
sys.exit(0)
sys.stdout.write("%s/*.pdf --> %s/*.swf\n\n" % (dirin, dirout))
try:
fdone = open(os.path.join(dirout, "done.txt"), 'a+')
ferr = open(os.path.join(dirout, "error.txt"), 'a')
except IOError as e:
sys.stderr.write("parser done file error: %s\n" % e)
else:
completed = set()
fdone.seek(0)
for line in fdone:
completed.add(line.rstrip())
pdfs = scandir(dirin)
for pdf in pdfs:
if pdf.name in completed:
sys.stdout.write("skipping: %s\n" % pdf.name)
continue
sys.stdout.write("%s\n" % pdf.name)
try:
parsed = FrankenParser(pdf.path)
except Exception as e:
try:
ferr.write("%s:%s\n" % (pdf.name, str(e)))
except Exception:
ferr.write("%s: ferr write() BIG-TIME ERROR\n" % pdf.name)
sys.stderr.write("ferr write error pdf: %s := %s\n" % (pdf.name, e))
else:
if parsed.swf:
try:
fout = open(os.path.join(dirout, "%s.swf" % pdf.name), 'wb')
except IOError as e:
sys.stderr.write("parser output file error: %s\n" % e)
else:
fout.write(''.join(parsed.swf))
fout.close()
finally:
try:
fdone.write("%s\n" % pdf.name)
except Exception as e:
sys.stderr.write("fdone write error pdf: %s := %s\n" % (pdf.name, e))
sys.stdout.write("\n")
fdone.close()
ferr.close()