-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdfml.py
More file actions
337 lines (302 loc) · 11.8 KB
/
Copy pathpdfml.py
File metadata and controls
337 lines (302 loc) · 11.8 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# Please download and include the following two files in the same directory:
# "https://raw.githubusercontent.com/kartik2309/Malicious_pdf_detection/master/feature_extraction.py"
# "https://raw.githubusercontent.com/kartik2309/Malicious_pdf_detection/master/pdfid.py
# Run ONCE on startup to load PDF ML models into memory. Saves time un-pickling each time model is required for evaluation
import os
import pickle
from constants import *
from subprocess import Popen, PIPE
from constants import PDFML_MODEL_PATH
from remote_logging import do_remote_logging
MODELS_IN_MEMORY =[]
def extract_features(output):
tuples = []
obj_subsection = bytes(output[2])
obj_subsection = obj_subsection.replace(b'obj', b'')
obj_subsection = obj_subsection.replace(b' ', b'')
obj_subsection = obj_subsection.replace(b'\\n', b'')
obj_subsection = obj_subsection.decode('UTF-8')
try:
obj = int(obj_subsection)
tuples.append(obj)
except ValueError:
obj = 10
tuples.append(obj)
pass
endobj_subsection = bytes(output[3])
endobj_subsection = endobj_subsection.replace(b'endobj', b'')
endobj_subsection = endobj_subsection.replace(b' ', b'')
endobj_subsection = endobj_subsection.replace(b'\\n', b'')
endobj_subsection = endobj_subsection.decode('UTF-8')
try:
obj = int(endobj_subsection)
tuples.append(obj)
except ValueError:
obj = 10
tuples.append(obj)
pass
stream_subsection = bytes(output[4])
stream_subsection = stream_subsection.replace(b'stream', b'')
stream_subsection = stream_subsection.replace(b' ', b'')
stream_subsection = stream_subsection.replace(b'\\n', b'')
stream_subsection = stream_subsection.decode('UTF-8')
try:
stream = int(stream_subsection)
tuples.append(stream)
except ValueError:
stream = 3
tuples.append(stream)
pass
endstream_subsection = bytes(output[5])
endstream_subsection = endstream_subsection.replace(b'endstream', b'')
endstream_subsection = endstream_subsection.replace(b' ', b'')
endstream_subsection = endstream_subsection.replace(b'\\n', b'')
endstream_subsection = endstream_subsection.decode('UTF-8')
try:
endstream = int(endstream_subsection)
tuples.append(endstream)
except ValueError:
endstream = 3
tuples.append(endstream)
pass
xref_subsection = bytes(output[6])
xref_subsection = xref_subsection.replace(b'xref', b'')
xref_subsection = xref_subsection.replace(b' ', b'')
xref_subsection = xref_subsection.replace(b'\\n', b'')
xref_subsection = xref_subsection.decode('UTF-8')
try:
xref = int(xref_subsection)
tuples.append(xref)
except ValueError:
xref = 1
tuples.append(xref)
pass
trailer_subsection = bytes(output[7])
trailer_subsection = trailer_subsection.replace(b'trailer', b'')
trailer_subsection = trailer_subsection.replace(b' ', b'')
trailer_subsection = trailer_subsection.replace(b'\\n', b'')
trailer_subsection = trailer_subsection.decode('UTF-8')
try:
trailer = int(trailer_subsection)
tuples.append(trailer)
except ValueError:
trailer = 1
tuples.append(trailer)
pass
startxref_subsection = bytes(output[8])
startxref_subsection = startxref_subsection.replace(b'startxref', b'')
startxref_subsection = startxref_subsection.replace(b' ', b'')
startxref_subsection = startxref_subsection.replace(b'\\n', b'')
startxref_subsection = startxref_subsection.decode('UTF-8')
try:
startxref = int(startxref_subsection)
tuples.append(startxref)
except ValueError:
startxref = 1
tuples.append(startxref)
pass
Page_subsection = bytes(output[9])
Page_subsection = Page_subsection.replace(b'/Page', b'')
Page_subsection = Page_subsection.replace(b' ', b'')
Page_subsection = Page_subsection.replace(b'\\n', b'')
Page_subsection = Page_subsection.decode('UTF-8')
try:
Page = int(Page_subsection)
tuples.append(Page)
except ValueError:
Page = 1
tuples.append(Page)
pass
Encrypt_subsection = bytes(output[10])
Encrypt_subsection = Encrypt_subsection.replace(b'/Encrypt', b'')
Encrypt_subsection = Encrypt_subsection.replace(b' ', b'')
Encrypt_subsection = Encrypt_subsection.replace(b'\\n', b'')
Encrypt_subsection = Encrypt_subsection.decode('UTF-8')
try:
Encrypt = int(Encrypt_subsection)
tuples.append(Encrypt)
except ValueError:
Encrypt = 0
tuples.append(Encrypt)
pass
ObjStm_subsection = bytes(output[11])
ObjStm_subsection = ObjStm_subsection.replace(b'/ObjStm', b'')
ObjStm_subsection = ObjStm_subsection.replace(b' ', b'')
ObjStm_subsection = ObjStm_subsection.replace(b'\\n', b'')
ObjStm_subsection = ObjStm_subsection.decode('UTF-8')
try:
ObjStm = int(ObjStm_subsection)
tuples.append(ObjStm)
except ValueError:
ObjStm = 0
tuples.append(ObjStm)
pass
JS_subsection = bytes(output[12])
JS_subsection = JS_subsection.replace(b'/JS', b'')
JS_subsection = JS_subsection.replace(b' ', b'')
JS_subsection = JS_subsection.replace(b'\\n', b'')
JS_subsection = JS_subsection.decode('UTF-8')
try:
JS = int(JS_subsection)
tuples.append(JS)
except ValueError:
JS = 1
tuples.append(JS)
pass
JavaScript_subsection = bytes(output[13])
JavaScript_subsection = JavaScript_subsection.replace(b'/JavaScript', b'')
JavaScript_subsection = JavaScript_subsection.replace(b' ', b'')
JavaScript_subsection = JavaScript_subsection.replace(b'\\n', b'')
JavaScript_subsection = JavaScript_subsection.decode('UTF-8')
try:
JavaScript = int(JavaScript_subsection)
tuples.append(JavaScript)
except ValueError:
JavaScript = 1
tuples.append(JavaScript)
pass
AA_subsection = bytes(output[14])
AA_subsection = AA_subsection.replace(b'/AA', b'')
AA_subsection = AA_subsection.replace(b' ', b'')
AA_subsection = AA_subsection.replace(b'\\n', b'')
AA_subsection = AA_subsection.decode('UTF-8')
try:
AA = int(AA_subsection)
tuples.append(AA)
except ValueError:
AA = 0
tuples.append(AA)
pass
OpenAction_subsection = bytes(output[15])
OpenAction_subsection = OpenAction_subsection.replace(b'/OpenAction', b'')
OpenAction_subsection = OpenAction_subsection.replace(b' ', b'')
OpenAction_subsection = OpenAction_subsection.replace(b'\\n', b'')
OpenAction_subsection = OpenAction_subsection.decode('UTF-8')
try:
OpenAction = int(OpenAction_subsection)
tuples.append(OpenAction)
except ValueError:
OpenAction = 1
tuples.append(OpenAction)
pass
AcroForm_subsection = bytes(output[16])
AcroForm_subsection = AcroForm_subsection.replace(b'/AcroForm', b'')
AcroForm_subsection = AcroForm_subsection.replace(b' ', b'')
AcroForm_subsection = AcroForm_subsection.replace(b'\\n', b'')
AcroForm_subsection = AcroForm_subsection.decode('UTF-8')
try:
AcroForm = int(AcroForm_subsection)
tuples.append(AcroForm)
except ValueError:
AcroForm = 0
tuples.append(AcroForm)
pass
JBIG2Decode_subsection = bytes(output[17])
JBIG2Decode_subsection = JBIG2Decode_subsection.replace(b'/JBIG2Decode', b'')
JBIG2Decode_subsection = JBIG2Decode_subsection.replace(b' ', b'')
JBIG2Decode_subsection = JBIG2Decode_subsection.replace(b'\\n', b'')
JBIG2Decode_subsection = JBIG2Decode_subsection.decode('UTF-8')
try:
JBIG2Decode = int(JBIG2Decode_subsection)
tuples.append(JBIG2Decode)
except ValueError:
JBIG2Decode = 0
tuples.append(JBIG2Decode)
pass
RichMedia_subsection = bytes(output[18])
RichMedia_subsection = RichMedia_subsection.replace(b'/RichMedia', b'')
RichMedia_subsection = RichMedia_subsection.replace(b' ', b'')
RichMedia_subsection = RichMedia_subsection.replace(b'\\n', b'')
RichMedia_subsection = RichMedia_subsection.decode('UTF-8')
try:
RichMedia = int(RichMedia_subsection)
tuples.append(RichMedia)
except ValueError:
RichMedia = 0
tuples.append(RichMedia)
pass
Launch_subsection = bytes(output[19])
Launch_subsection = Launch_subsection.replace(b'/Launch', b'')
Launch_subsection = Launch_subsection.replace(b' ', b'')
Launch_subsection = Launch_subsection.replace(b'\\n', b'')
Launch_subsection = Launch_subsection.decode('UTF-8')
try:
Launch = int(Launch_subsection)
tuples.append(Launch)
except ValueError:
Launch = 0
tuples.append(Launch)
pass
EmbeddedFile_subsection = bytes(output[20])
EmbeddedFile_subsection = EmbeddedFile_subsection.replace(b'/EmbeddedFile', b'')
EmbeddedFile_subsection = EmbeddedFile_subsection.replace(b' ', b'')
EmbeddedFile_subsection = EmbeddedFile_subsection.replace(b'\\n', b'')
EmbeddedFile_subsection = EmbeddedFile_subsection.decode('UTF-8')
try:
EmbeddedFile = int(EmbeddedFile_subsection)
tuples.append(EmbeddedFile)
except ValueError:
EmbeddedFile = 0
tuples.append(EmbeddedFile)
pass
XFA_subsection = bytes(output[21])
XFA_subsection = XFA_subsection.replace(b'/XFA', b'')
XFA_subsection = XFA_subsection.replace(b' ', b'')
XFA_subsection = XFA_subsection.replace(b'\\n', b'')
XFA_subsection = XFA_subsection.decode('UTF-8')
try:
XFA = int(XFA_subsection)
tuples.append(XFA)
except ValueError:
XFA = 0
tuples.append(XFA)
pass
colors_subsection = bytes(output[22])
colors_subsection = colors_subsection.replace(b'/Colors > 2^24', b'')
colors_subsection = colors_subsection.replace(b' ', b'')
colors_subsection = colors_subsection.replace(b'\\n', b'')
colors_subsection = colors_subsection.decode('UTF-8')
try:
colors = int(colors_subsection)
tuples.append(colors)
except ValueError:
colors = 0
tuples.append(colors)
pass
return tuples
def feature_extraction(filepath):
features = []
command_to_execute = 'python3 ' + str(PDFML_MAIN_PATH) + '/pdfid.py ' + filepath
stdout = Popen(command_to_execute, shell=True, stdout=PIPE).stdout
output = stdout.readlines()
if len(output) == 24:
features.append(extract_features(output))
return features
for x in os.listdir(PDFML_MODEL_PATH):
clf_pickle = open(PDFML_MODEL_PATH+'/'+x, 'rb')
clf = pickle.load(clf_pickle)
MODELS_IN_MEMORY.append(clf)
def predict_file(logger, remote_logger, file_path, file_name):
# filepath is the path to the PDF to test
features = feature_extraction(file_path)
prediction_array=[]
if not os.path.exists(file_path):
logger.log("ERROR", "FilePredict", "Non-Existent File %s ... " % file_name)
do_remote_logging(remote_logger, "ERROR", ["FilePredict", "Non-Existent File %s ... " % file_name])
return {}
logger.log("INFO", "FilePredict", "Extracting features from File %s ... " % file_name)
do_remote_logging(remote_logger, "INFO", ["FilePredict", "Extracting features from File %s ... " % file_name])
try:
for model in MODELS_IN_MEMORY:
result = bool(model.predict(features))
prediction_array.append(result)
if True in prediction_array:
return "DANGEROUS"
else:
return "SAFE"
except Exception as e:
logger.log("ERROR", "FilePredict", e)
do_remote_logging(remote_logger, "ERROR", ["FilePredict", e])
return {}
# returns an array containing the predictions (True/False) from all selected models
# True means malicious, False means benign