forked from armbues/ioc_parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iocp.py
executable file
·394 lines (333 loc) · 13.2 KB
/
iocp.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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#!/usr/bin/env python
###################################################################################################
#
# Copyright (c) 2015, Armin Buescher (armin.buescher@googlemail.com)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
###################################################################################################
#
# File: iocp.py
# Description: IOC Parser is a tool to extract indicators of compromise from security reports
# in PDF format.
# Usage: iocp.py [-h] [-p INI] [-f FORMAT] PDF
# Author: Armin Buescher (@armbues)
# Contributors: Angelo Dell'Aera (@angelodellaera)
# Thanks to: Jose Ramon Palanco
# Koen Van Impe (@cudeso)
#
###################################################################################################
import os
import sys
import fnmatch
import argparse
import re
from StringIO import StringIO
try:
import configparser as ConfigParser
except ImportError:
import ConfigParser
# Import optional third-party libraries
IMPORTS = []
try:
from PyPDF2 import PdfFileReader
IMPORTS.append('pypdf2')
except ImportError:
pass
try:
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfinterp import PDFResourceManager
from pdfminer.converter import TextConverter
from pdfminer.pdfinterp import PDFPageInterpreter
from pdfminer.layout import LAParams
IMPORTS.append('pdfminer')
except ImportError:
pass
try:
from bs4 import BeautifulSoup
IMPORTS.append('beautifulsoup')
except ImportError:
pass
try:
from docx import Document
IMPORTS.append('docx')
except ImportError:
pass
try:
import requests
IMPORTS.append('requests')
except ImportError:
pass
# Import additional project source files
import output
from whitelist import WhiteList
class IOC_Parser(object):
patterns = {}
defang = {}
def __init__(self, patterns_ini=None, input_format='pdf', dedup=False, library='pdfminer', output_format='csv', output_handler=None):
basedir = os.path.dirname(os.path.abspath(__file__))
if patterns_ini is None:
patterns_ini = os.path.join(basedir, 'patterns.ini')
self.load_patterns(patterns_ini)
self.whitelist = WhiteList(basedir)
self.dedup = dedup
if output_handler:
self.handler = output_handler
else:
self.handler = output.getHandler(output_format)
self.ext_filter = "*." + input_format
parser_format = "parse_" + input_format
try:
self.parser_func = getattr(self, parser_format)
except AttributeError:
e = 'Selected parser format is not supported: %s' % (input_format)
raise NotImplementedError(e)
self.library = library
if input_format == 'pdf':
if library not in IMPORTS:
e = 'Selected PDF parser library not found: %s' % (library)
raise ImportError(e)
elif input_format == 'html':
if 'beautifulsoup' not in IMPORTS:
e = 'HTML parser library not found: BeautifulSoup'
raise ImportError(e)
elif input_format == 'docx':
if 'docx' not in IMPORTS:
e = 'DOCX parser library not found: docx.Document'
raise ImportError(e)
def load_patterns(self, fpath):
config = ConfigParser.ConfigParser()
with open(fpath) as f:
config.readfp(f)
for ind_type in config.sections():
try:
ind_pattern = config.get(ind_type, 'pattern')
except:
continue
if ind_pattern:
ind_regex = re.compile(ind_pattern)
self.patterns[ind_type] = ind_regex
try:
ind_defang = config.get(ind_type, 'defang')
except:
continue
if ind_defang:
self.defang[ind_type] = True
def is_whitelisted(self, ind_match, ind_type):
try:
for w in self.whitelist[ind_type]:
if w.findall(ind_match):
return True
except KeyError as e:
pass
return False
def parse_page(self, fpath, data, page_num):
for ind_type, ind_regex in self.patterns.items():
matches = ind_regex.findall(data)
for ind_match in matches:
if isinstance(ind_match, tuple):
ind_match = ind_match[0]
if self.is_whitelisted(ind_match, ind_type):
continue
if ind_type in self.defang:
ind_match = re.sub(r'\[\.\]', '.', ind_match)
if self.dedup:
if (ind_type, ind_match) in self.dedup_store:
continue
self.dedup_store.add((ind_type, ind_match))
self.handler.print_match(fpath, page_num, ind_type, ind_match)
def parse_pdf_pypdf2(self, f, fpath):
try:
pdf = PdfFileReader(f, strict = False)
if self.dedup:
self.dedup_store = set()
self.handler.print_header(fpath)
page_num = 0
for page in pdf.pages:
page_num += 1
data = page.extractText()
self.parse_page(fpath, data, page_num)
self.handler.print_footer(fpath)
except (KeyboardInterrupt, SystemExit):
raise
except Exception as e:
self.handler.print_error(fpath, e)
def parse_pdf_pdfminer(self, f, fpath):
try:
laparams = LAParams()
laparams.all_texts = True
rsrcmgr = PDFResourceManager()
pagenos = set()
if self.dedup:
self.dedup_store = set()
self.handler.print_header(fpath)
page_num = 0
for page in PDFPage.get_pages(f, pagenos, check_extractable=True):
page_num += 1
retstr = StringIO()
device = TextConverter(rsrcmgr, retstr, laparams=laparams)
interpreter = PDFPageInterpreter(rsrcmgr, device)
interpreter.process_page(page)
data = retstr.getvalue()
retstr.close()
self.parse_page(fpath, data, page_num)
self.handler.print_footer(fpath)
except (KeyboardInterrupt, SystemExit):
raise
except Exception as e:
self.handler.print_error(fpath, e)
def parse_pdf(self, f, fpath):
parser_format = "parse_pdf_" + self.library
try:
self.parser_func = getattr(self, parser_format)
except AttributeError:
e = 'Selected PDF parser library is not supported: %s' % (self.library)
raise NotImplementedError(e)
self.parser_func(f, fpath)
def parse_txt(self, f, fpath):
try:
if self.dedup:
self.dedup_store = set()
data = f.read()
self.handler.print_header(fpath)
self.parse_page(fpath, data, 1)
self.handler.print_footer(fpath)
except (KeyboardInterrupt, SystemExit):
raise
except Exception as e:
self.handler.print_error(fpath, e)
def find_hyperlink(self, object):
if hasattr(object, "tag"):
if "hyperlink" in object.tag:
p = self.get_all_text(object)
return p
else:
hyperlinks = []
for child in object.getchildren():
text = self.find_hyperlink(child)
if type(text) == str:
hyperlinks.append(text)
elif type(text) == list:
hyperlinks.extend(text)
if hyperlinks != []:
return ", ".join(hyperlinks)
else:
return None
else:
print "NO TAG"
def get_all_text(self, object):
if object.getchildren() == []:
# print object.text
if object.text is not None:
return [object.text]
else:
return None
else:
text = []
for child in object.getchildren():
t = self.get_all_text(child)
if t is not None:
text.extend(t)
return "".join(text)
def parse_docx(self, f, fpath):
doc = Document(fpath)
all_text = ""
all_text = "\r\n".join([p.text for p in doc.paragraphs])
all_text += " "
for paragraph in doc.paragraphs:
hyperlinks = self.find_hyperlink(paragraph._p)
if hyperlinks is not None:
all_text += hyperlinks
all_text += " "
table_text = ""
for table in doc.tables:
for row in table.rows:
for cell in row.cells:
table_text += "\r\n".join([p.text for p in cell.paragraphs])
table_text += " "
hyperlinks = self.find_hyperlink(table._tbl)
if hyperlinks is not None:
all_text += hyperlinks
all_text += " "
all_text = all_text + table_text
self.handler.print_header(fpath)
self.parse_page(fpath, all_text, 1)
self.handler.print_footer(fpath)
def parse_html(self, f, fpath):
try:
if self.dedup:
self.dedup_store = set()
data = f.read()
soup = BeautifulSoup(data)
html = soup.findAll(text=True)
text = u''
for elem in html:
if elem.parent.name in ['style', 'script', '[document]', 'head', 'title']:
continue
elif re.match('<!--.*-->', unicode(elem)):
continue
else:
text += unicode(elem)
self.handler.print_header(fpath)
self.parse_page(fpath, text, 1)
self.handler.print_footer(fpath)
except (KeyboardInterrupt, SystemExit):
raise
except Exception as e:
self.handler.print_error(fpath, e)
def parse(self, path):
try:
if path.startswith('http://') or path.startswith('https://'):
if 'requests' not in IMPORTS:
e = 'HTTP library not found: requests'
raise ImportError(e)
headers = { 'User-Agent': 'Mozilla/5.0 Gecko Firefox' }
r = requests.get(path, headers=headers)
r.raise_for_status()
f = StringIO(r.content)
self.parser_func(f, path)
return
elif os.path.isfile(path):
with open(path, 'rb') as f:
self.parser_func(f, path)
return
elif os.path.isdir(path):
for walk_root, walk_dirs, walk_files in os.walk(path):
for walk_file in fnmatch.filter(walk_files, self.ext_filter):
fpath = os.path.join(walk_root, walk_file)
with open(fpath, 'rb') as f:
self.parser_func(f, fpath)
return
e = 'File path is not a file, directory or URL: %s' % (path)
raise IOError(e)
except (KeyboardInterrupt, SystemExit):
raise
except Exception as e:
self.handler.print_error(path, e)
if __name__ == "__main__":
argparser = argparse.ArgumentParser()
argparser.add_argument('PATH', action='store', help='File/directory/URL to report(s)')
argparser.add_argument('-p', dest='INI', default=None, help='Pattern file')
argparser.add_argument('-i', dest='INPUT_FORMAT', default='pdf', help='Input format (pdf/txt/html)')
argparser.add_argument('-o', dest='OUTPUT_FORMAT', default='csv', help='Output format (csv/json/yara/netflow)')
argparser.add_argument('-d', dest='DEDUP', action='store_true', default=False, help='Deduplicate matches')
argparser.add_argument('-l', dest='LIB', default='pdfminer', help='PDF parsing library (pypdf2/pdfminer)')
args = argparser.parse_args()
parser = IOC_Parser(args.INI, args.INPUT_FORMAT, args.DEDUP, args.LIB, args.OUTPUT_FORMAT)
parser.parse(args.PATH)