-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_pdf.py
297 lines (278 loc) · 13 KB
/
module_pdf.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
297
#!/usr/bin/env python
# Main function is handler_<object>
# Return should be structured as JSON, { <module> : { result : <result>, risk : <risk>, indicators : <[indicators]>, additional_info : { <custom> : <custom> } } }
from __future__ import print_function
from subprocess import Popen, PIPE
import re, os, string, sys
# Custom imports
import filescan_config
# Set variables from config for later use
filescanner_logs_dir = filescan_config.scan_logs_directory
filescanner_proc_dir = filescan_config.files_out_directory
pdf_tools = filescan_config.pdf_tools_directory
peepdf_dir = filescan_config.peepdf_directory
peepdf = filescan_config.peepdf
pdfid = filescan_config.pdfid
pdfparser = filescan_config.pdfparser
# Output JSON
module = 'pdf'
output = {}
##
# SUPPORT FUNCTIONS
##
def read_file(file_to_read):
f = open(file_to_read, mode='r')
lines = f.readlines()
f.close()
return lines
def print_to_log(file, string):
outputfile = open(filescanner_logs_dir+file+'.txt', mode='a')
outputfile.write('\n-----------------------------------------\n')
outputfile.write('['+string+']')
outputfile.write('\n-----------------------------------------\n')
outputfile.close()
##
# MAIN
##
def handler_pdf(file, md5, tool, args):
suspicious_indicator = 0 # variable to contain count for suspicious indicator groups
indicators = [] # list to hold all suspicious indicator strings
peepdf_error = '' # variable to identify peepdf errors, either 'Y' or empty
if tool == 'PDFiD':
# PDFiD scan command
pdfid_cmd = pdfid+' "'+filescanner_proc_dir+file+'" >> "'+filescanner_logs_dir+file+'.txt"'
pdfid_cmd_results = Popen(pdfid_cmd, shell=True, stdout=PIPE).communicate()[0]
else:
open(peepdf_dir+'errors.txt', 'w').close() #clear any previous errors for peepdf
# peepdf scan command
peepdf_cmd = peepdf+' -g -f "'+filescanner_proc_dir+file+'" >> "'+filescanner_logs_dir+file+'.txt"'
# print header to TXT results file
print_to_log(file,'PDF Structure')
try:
peepdf_cmd_results = Popen(peepdf_cmd, shell=True, stdout=PIPE, stderr=PIPE).communicate() #communicate() returns a tuple (stdout[0], stderr[1])
# Use PDFiD to scan PDF if peepdf returned an error
except:
#if peepdf_cmd_results[1]:
peepdf_error = 'Y'
pdfid_cmd = pdfid+' "'+filescanner_proc_dir+file+'" >> "'+filescanner_logs_dir+file+'.txt"'
pdfid_cmd_results = Popen(pdfid_cmd, shell=True, stdout=PIPE).communicate()[0]
# Determine obects contained within PDF based on scan results
Page, Producer, Encrypt, AA, OpenAction, JS, JavaScript, RichMedia, EmbeddedFile, EmbeddedFiles, URI, doswf, exe, swf, ftpdown, laaan, U3D, PRC, Launch, AcroForm, XFA, Win, Action, JBIG2Decode, Names, SubmitForm, ImportData, CVE = pdf_objects(file)
##############################################
if Producer:
process_pdf_object(file, Producer, 'Producer', tool, peepdf_error)
if Encrypt:
process_pdf_object(file, Encrypt, 'Encrypt', tool, peepdf_error)
indicators.append('/Encrypt')
if AA:
process_pdf_object(file, AA, 'AA', tool, peepdf_error)
indicators.append('/AA')
if OpenAction:
process_pdf_object(file, OpenAction, 'OpenAction', tool, peepdf_error)
indicators.append('/OpenAction')
if JS:
process_pdf_object(file, JS, 'JS', tool, peepdf_error)
indicators.append('/JS')
if JavaScript:
process_pdf_object(file, JavaScript, 'JavaScript', tool, peepdf_error)
indicators.append('/JavaScript')
if RichMedia:
process_pdf_object(file, RichMedia, 'RichMedia', tool, peepdf_error)
indicators.append('/RichMedia')
if EmbeddedFile:
process_pdf_object(file, EmbeddedFile, 'EmbeddedFile', tool, peepdf_error)
indicators.append('/EmbeddedFile')
if EmbeddedFiles: #peepdf only
process_pdf_object(file, EmbeddedFiles, 'EmbeddedFiles', tool, peepdf_error)
indicators.append('/EmbeddedFiles')
if URI:
process_pdf_object(file, URI, 'URI', tool, peepdf_error)
indicators.append('/URI')
if doswf:
process_pdf_object(file, doswf, 'doswf', tool, peepdf_error)
indicators.append('dos')
if exe:
process_pdf_object(file, exe, 'exe', tool, peepdf_error)
indicators.append('exe')
if swf:
process_pdf_object(file, swf, 'swf', tool, peepdf_error)
indicators.append('swf')
if ftpdown:
process_pdf_object(file, ftpdown, 'ftpdown', tool, peepdf_error)
indicators.append('ftpdown')
if laaan:
process_pdf_object(file, laaan, 'laaan', tool, peepdf_error)
indicators.append('laaan')
if U3D: #peepdf only
process_pdf_object(file, U3D, 'U3D', tool, peepdf_error)
indicators.append('/U3D')
if PRC: #peepdf only
process_pdf_object(file, PRC, 'PRC', tool, peepdf_error)
indicators.append('/PRC')
if Launch:
process_pdf_object(file, Launch, 'Launch', tool, peepdf_error)
indicators.append('/Launch')
if AcroForm:
process_pdf_object(file, AcroForm, 'AcroForm', tool, peepdf_error)
indicators.append('/AcroForm')
if XFA:
process_pdf_object(file, XFA, 'XFA', tool, peepdf_error)
indicators.append('/XFA')
if Win:
process_pdf_object(file, Win, 'Win', tool, peepdf_error)
indicators.append('/Win')
if Action:
process_pdf_object(file, Action, 'Action', tool, peepdf_error)
indicators.append('/Action')
if JBIG2Decode:
process_pdf_object(file, JBIG2Decode, 'JBIG2Decode', tool, peepdf_error)
indicators.append('/JBIG2Decode')
if Names: #peepdf only
process_pdf_object(file, Names, 'Names', tool, peepdf_error)
indicators.append('/Names')
if SubmitForm: #peepdf only
process_pdf_object(file, SubmitForm, 'SubmitForm', tool, peepdf_error)
indicators.append('/SubmitForm')
if ImportData: #peepdf only
process_pdf_object(file, ImportData, 'ImportData', tool, peepdf_error)
indicators.append('/ImportData')
try:
os.remove(pdf_tools+'peepdftemp.txt') # remove peepdf tempfile
except:
pass
###############################################
if CVE:
suspicious_indicator = suspicious_indicator + 1
CVE = CVE.split(':')[0].lstrip()
indicators.append(CVE)
if AA or OpenAction:
suspicious_indicator = suspicious_indicator + 1
if JS or JavaScript or RichMedia or EmbeddedFile or EmbeddedFiles or URI or doswf or exe or swf or ftpdown or laaan or U3D or PRC or Action:
suspicious_indicator = suspicious_indicator + 1
if Encrypt or Launch or AcroForm or XFA or Win or JBIG2Decode or Names or SubmitForm or ImportData:
suspicious_indicator = suspicious_indicator + 1
# Determine risk level based on identified strings
if suspicious_indicator == 0:
risk = 'No Risk'
indicators.append('No suspect strings found.')
elif suspicious_indicator == 1: risk = 'Low Risk'
elif suspicious_indicator == 2: risk = 'Medium Risk'
elif suspicious_indicator > 2: risk = 'High Risk'
# Populate result
# 2 or more suspicious indicator groupings in PDF
if risk == 'High Risk' or risk == 'Medium Risk':
result = 'Suspicious object combination found'
# 1 or less suspicious indicator groupings in PDF
else:
result = 'No suspicious object combinations found'
##
# OUTPUT
##
output[module] = {'result' : result, 'risk' : risk, 'indicators' : indicators }
output[module]['additional_info'] = {'md5' : md5}
return output
############################# PDF OBJECTS TO BE SCANNED ##################################################################################################################################
def pdf_objects(file):
# Reset all pdf objs
Page = ''
Producer = ''
AA = ''
OpenAction = ''
JS = ''
JavaScript = ''
RichMedia = ''
EmbeddedFile = ''
EmbeddedFiles = '' #peepdf only
URI = ''
doswf = ''
exe = ''
swf = ''
ftpdown = ''
laaan = ''
U3D = '' #peepdf only
PRC = '' #peepdf only
Encrypt = ''
Launch = ''
AcroForm = ''
XFA = ''
Win = ''
Action = ''
JBIG2Decode = ''
Names = '' #peepdf only
SubmitForm = '' #peepdf only
ImportData = ''#peepdf only
CVE = ''#peepdf only
# Read results file
data = read_file(filescanner_logs_dir+file+'.txt')
for line in data:
if re.search(r"/Page.*[0-9]{1,9}", line): Page = line
elif re.search(r"/Producer.*[1-9]{1,9}", line): Producer = line
elif re.search(r"/Encrypt.*[1-9]{1,9}", line): Encrypt = line
# May autorun when opened; however, launches can be user enticed manual links in pdf
elif re.search(r"/AA.*[1-9]{1,9}", line): AA = line
elif re.search(r"/OpenAction.*[1-9]{1,9}", line): OpenAction = line
# Embedding: script, code, files, or media
elif re.search(r"/JS.*[1-9]{1,9}", line): JS = line
elif re.search(r"/JavaScript.*[1-9]{1,9}", line): JavaScript = line
elif re.search(r"/RichMedia.*[1-9]{1,9}", line): RichMedia = line
elif re.search(r"/EmbeddedFile.*[1-9]{1,9}", line): EmbeddedFile = line
elif re.search(r"/EmbeddedFiles.*[1-9]{1,9}", line): EmbeddedFiles = line
elif re.search(r"/URI.*[1-9]{1,9}", line): URI = line
# Embedding: non-traditional indicators
elif re.search(r"doswf.*[1-9]{1,9}", line): doswf = line
elif re.search(r"exe.*[1-9]{1,9}", line): exe = line
elif re.search(r"swf.*[1-9]{1,9}", line): swf = line
elif re.search(r"ftpdown.*[1-9]{1,9}", line): ftpdown = line
elif re.search(r"laaan.*[1-9]{1,9}", line): laaan = line
elif re.search(r"/U3D.*[1-9]{1,9}", line): U3D = line
elif re.search(r"/PRC.*[1-9]{1,9}", line): PRC = line
# Supporting object types: facilitating obfuscation, encryption, compression, etc.
elif re.search(r"/Launch.*[1-9]{1,9}", line): Launch = line
elif re.search(r"/AcroForm.*[1-9]{1,9}", line): AcroForm = line
elif re.search(r"/XFA.*[1-9]{1,9}", line): XFA = line
elif re.search(r"/Win.*[1-9]{1,9}", line): Win = line
elif re.search(r"/Action.*[1-9]{1,9}", line): Action = line
elif re.search(r"/JBIG2Decode.*[1-9]{1,9}", line): JBIG2Decode = line
elif re.search(r"/Names.*[1-9]{1,9}", line): Names = line
elif re.search(r"/SubmitForm.*[1-9]{1,9}", line): SubmitForm = line
elif re.search(r"/ImportData.*[1-9]{1,9}", line): ImportData = line
elif re.search(r"CVE-[0-9]{4}.*", line): CVE = line
return Page, Producer, Encrypt, AA, OpenAction, JS, JavaScript, RichMedia, EmbeddedFile, EmbeddedFiles, URI, doswf, exe, swf, ftpdown, laaan, U3D, PRC, Launch, AcroForm, XFA, Win, Action, JBIG2Decode, Names, SubmitForm, ImportData, CVE
############################# PDF PROCESSING (PEEPDF) ####################################################################################################################################
# Process PDF objects
def process_pdf_object(file, object, strobject, tool, peepdf_error):
if tool == 'PDFiD' or peepdf_error == 'Y':
scan_pdfid_object(file,strobject)
else:
objectScan = process_line(object)
# scan each object found in peepdf and pipe results to log, limit to 30 objects to iterate through
objects = len(objectScan)
if objects > 30: objectScan = objectScan[:30]
for num in objectScan:
scan_pdf_object(file,strobject,num)
# Process PDF string lines to pull out object numbers to iterate through
def process_line(string):
stringScan = ''.join(string.split(':')[1:])
stringScan_r1 = stringScan.replace('[','')
stringScan_r2 = stringScan_r1.replace(']','')
stringScan_l = stringScan_r2.split(',')
return stringScan_l
# Scan suspicious PDF ojbects
def scan_pdf_object(file,strobject,num):
open(pdf_tools+'peepdftemp.txt', 'w').close() #ensure peepdf temp file exists and clear contents if so
pdf_object_header = 'Examining '+strobject+' Object:'+num.replace('\n','')+''
print_to_log(file, pdf_object_header)
write_temp_pdf_object(num)
scan_pdf_object = peepdf+' -f -i "'+filescanner_proc_dir+file+'" -s "'+pdf_tools+'peepdftemp.txt" >> "'+filescanner_logs_dir+file+'.txt"'
scan_pdf_object_results = Popen(scan_pdf_object, shell=True, stdout=PIPE).communicate()[0]
# Write PDF objects to temp file for scripting
def write_temp_pdf_object(num):
t = open(pdf_tools+'peepdftemp.txt', 'w')
t.write('object'+num+' ')
t.close()
############################ PDF PROCESSING (PDFID, PDF-PARSER) ##########################################################################################################################
def scan_pdfid_object(file,strobject):
pdf_object_header = 'Searching '+strobject+' Objects'
print_to_log(file, pdf_object_header)
scan_pdfid_object = pdfparser+' --search ' +strobject+' "'+filescanner_proc_dir+file+'" >> "'+filescanner_logs_dir+file+'.txt"'
scan_pdfid_object_results = Popen(scan_pdfid_object, shell=True, stdout=PIPE).communicate()[0]