-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathvmonkey.py
1159 lines (943 loc) · 43.5 KB
/
vmonkey.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env pypy
"""@package vmonkey
The ViperMonkey command line and programatic interface. The top level
function for using ViperMonkey programatically is process_file().
ViperMonkey is a specialized engine to parse, analyze and interpret Microsoft
VBA macros (Visual Basic for Applications), mainly for malware analysis.
Author: Philippe Lagadec - http://www.decalage.info
License: BSD, see source code or documentation
Project Repository:
https://github.com/decalage2/ViperMonkey
"""
from __future__ import print_function
# Do this before any other imports to make sure we have an unlimited
# packrat parsing cache. Do not move or remove this line.
import pyparsing
pyparsing.ParserElement.enablePackrat(cache_size_limit=100000)
import shutil
import logging
import json
import random
import optparse
import sys
import os
import traceback
import colorlog
import re
from datetime import datetime
from datetime import timedelta
import zipfile
import io
import prettytable
from oletools.thirdparty.xglob import xglob
from oletools.olevba import VBA_Parser, filter_vba, FileOpenError
import olefile
from core.meta import get_metadata_exif
# add the vipermonkey folder to sys.path (absolute+normalized path):
_thismodule_dir = os.path.normpath(os.path.abspath(os.path.dirname(__file__)))
if _thismodule_dir not in sys.path:
sys.path.insert(0, _thismodule_dir)
# relative import of core ViperMonkey modules:
import core
import core.excel as excel
import core.read_ole_fields as read_ole_fields
from core.utils import safe_print
from core.utils import safe_str_convert
# for logging
from core.logger import log
from core.logger import CappedFileHandler
from logging import FileHandler
#=== LICENSE ==================================================================
# ViperMonkey is copyright (c) 2015-2021 Philippe Lagadec (http://www.decalage.info)
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#------------------------------------------------------------------------------
# CHANGELOG:
# 2015-02-12 v0.01 PL: - first prototype
# 2015-2016 PL: - many changes
# 2016-10-06 v0.03 PL: - fixed vipermonkey.core import
# 2016-12-11 v0.04 PL: - fixed relative import for core package (issue #17)
# 2018-01-12 v0.05 KS: - lots of bug fixes and additions by Kirk Sayre (PR #23)
# 2018-06-20 v0.06 PL: - fixed issue #28, import prettytable
# 2018-08-17 v0.07 KS: - lots of bug fixes and additions by Kirk Sayre (PR #34)
# PL: - added ASCII art banner
__version__ = '1.0.3'
#------------------------------------------------------------------------------
# TODO:
# TODO: detect subs/functions with same name (in different modules)
# TODO: can several projects call each other?
# TODO: Word XML with several projects?
# - cleanup main, use optionparser
# - option -e to extract and evaluate constant expressions
# - option -t to trace execution
# - option --entrypoint to specify the Sub name to use as entry point
# - use olevba to get all modules from a file
# Environ => VBA object
# vbCRLF, etc => Const (parse to string)
# py2vba: convert python string to VBA string, e.g. \" => "" (for olevba to scan expressions) - same thing for ints, etc?
#TODO: expr_int / expr_str
#TODO: eval(parent) => for statements to set local variables into parent functions/procedures + main VBA module
#TODO: __repr__ for printing
#TODO: Environ('str') => '%str%'
#TODO: determine the order of Auto subs for Word, Excel
# TODO later:
# - add VBS support (two modes?)
#------------------------------------------------------------------------------
# REFERENCES:
# - [MS-VBAL]: VBA Language Specification
# https://msdn.microsoft.com/en-us/library/dd361851.aspx
# - [MS-OVBA]: Microsoft Office VBA File Format Structure
# http://msdn.microsoft.com/en-us/library/office/cc313094%28v=office.12%29.aspx
def get_vb_contents_from_hta(vba_code):
"""Pull out Visual Basic code from .hta file contents.
@param vba_code (str) The HTA file contents from which to extract
the VBScript code.
@return (str) If the given data is HTA that contains VBScript
script elements, the VBScript in the HTA is returned. If the given
data is not VBScript HTA, the original data is returned.
"""
# Fix some obfuscation if needed.
# 'V'
if (re.search(r"&#\d{1,3};", vba_code) is not None):
for i in range(0, 256):
curr_c = chr(i)
vba_code = vba_code.replace("&#" + str(i) + ";", curr_c)
# Try several regexes to pull out HTA script contents.
hta_regexes = [r"<\s*[Ss][Cc][Rr][Ii][Pp][Tt]\s+(?:(?:[Ll][Aa][Nn][Gg][Uu][Aa][Gg][Ee])|(?:[Tt][Yy][Pp][Ee]))\s*=" + \
r"\s*\"?.{0,10}[Vv][Bb][Ss][Cc][Rr][Ii][Pp][Tt]\"?\s*>(.{20,}?)</\s*[Ss][Cc][Rr][Ii][Pp][Tt][^>]*>",
r"<\s*[Ss][Cc][Rr][Ii][Pp][Tt]\s+\%\d{1,10}\s*>(.{20,}?)</\s*[Ss][Cc][Rr][Ii][Pp][Tt][^>]*>",
r"<\s*[Ss][Cc][Rr][Ii][Pp][Tt]\s+(?:(?:[Ll][Aa][Nn][Gg][Uu][Aa][Gg][Ee])|(?:[Tt][Yy][Pp][Ee]))\s*=" + \
r"\s*\"?.{0,10}[Vv][Bb][Ss][Cc][Rr][Ii][Pp][Tt]\"?\s*>(.{20,})$"]
code = []
for pat in hta_regexes:
code = re.findall(pat, vba_code.strip(), re.DOTALL)
if (len(code) > 0):
#for c in code:
# print("\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n")
# print(c)
break
if (len(code) == 0):
return vba_code
# We have script block VB code.
# Return the code.
r = ""
for b in code:
b = b.strip()
if ("</script>" in b.lower()):
b = b[:b.lower().index("</script>")]
if ("<![CDATA[" in b.upper()):
b = b[b.upper().index("<![CDATA[") + len("<![CDATA["):]
if ("]]>" in b[-10:]):
b = b[:b.rindex("]]>")]
# More tag stripping.
pat = r"<!\-\-(.+)/?/?\-\->"
tmp_b = re.findall(pat, b, re.DOTALL)
if (len(tmp_b) > 0):
b = tmp_b[0].strip()
if (b.endswith("//")):
b = b[:-2]
r += b + "\n"
return r
def parse_stream(subfilename,
stream_path=None,
vba_filename=None,
vba_code=None,
strip_useless=False,
local_funcs=None):
"""Parse the macros from a single OLE stream.
@param subfilename (str) The name of the file containing the
macros.
@param stream_path (??) ??
@param vba_filename (??) ??
@param vba_code (str) The macro code to parse.
@param local_funcs (list) A list of the names of already declared
local VBA functions.
@return (Module object) A parsed module object.
"""
# Set local func list if needed.
if (local_funcs is None):
local_funcs = []
# Check for timeouts.
core.vba_object.limits_exceeded(throw_error=True)
# Are the arguments all in a single tuple?
if (stream_path is None):
subfilename, stream_path, vba_filename, vba_code = subfilename
# Skip old-style XLM macros.
if (repr(stream_path).strip() == "'xlm_macro'"):
log.warning("Skipping XLM macro stream...")
return "empty"
# Collapse long lines.
vba_code = core.vba_collapse_long_lines(vba_code)
# Filter cruft from the VBA.
vba_code = filter_vba(vba_code)
# Pull out Visual Basic from .hta contents (if we are looking at a
# .hta file).
vba_code = get_vb_contents_from_hta(vba_code)
# Do not analyze the file if the VBA looks like garbage characters.
if (read_ole_fields.is_garbage_vba(vba_code)):
raise ValueError("VBA looks corrupted. Not analyzing.")
# Skip some XML that olevba gives for some 2007+ streams.
if (vba_code.strip().startswith("<?xml")):
log.warning("Skipping XML stream.")
return "empty"
# Strip out code that does not affect the end result of the program.
if (strip_useless):
vba_code = core.strip_lines.strip_useless_code(vba_code, local_funcs)
safe_print('-'*79)
safe_print('VBA MACRO %s ' % vba_filename)
safe_print('in file: %s - OLE stream: %s' % (subfilename, repr(stream_path)))
safe_print('- '*39)
# Parse the macro.
m = None
if vba_code.strip() == '':
safe_print('(empty macro)')
m = "empty"
else:
safe_print('-'*79)
safe_print('VBA CODE (with long lines collapsed):')
safe_print(vba_code)
safe_print('-'*79)
#sys.exit(0)
safe_print('PARSING VBA CODE:')
try:
m = core.module.parseString(vba_code + "\n", parseAll=True)[0]
pyparsing.ParserElement.resetCache()
m.code = vba_code
except pyparsing.ParseException as err:
safe_print(err.line)
safe_print(" "*(err.column-1) + "^")
safe_print(err)
log.error("Parse Error. Processing Aborted.")
return None
# Check for timeouts.
core.vba_object.limits_exceeded(throw_error=True)
# Return the parsed macro.
return m
def get_all_local_funcs(vba):
"""Get the names of all locally defined functions. Also get the names
of all defined constants. The constant names are saved in
core.strip_lines.defined_constants.
@params vba (VBA_Parser object) The olevba VBA_Parser object for
reading the Office file being analyzed.
"""
# Find the sub/function definitions.
pat = r"(?:Sub |Function )([^\(]+)"
r = []
for (_, _, _, vba_code) in vba.extract_macros():
if (vba_code is None):
continue
# Get local func names.
for line in vba_code.split("\n"):
names = re.findall(pat, line)
r.extend(names)
# Get constant defs. This is saved in strip_lines.defined_constants.
core.strip_lines.find_defined_constants(vba_code)
# Return local function names.
return r
def parse_streams(vba, strip_useless=False):
"""Parse all the VBA streams and return list of parsed module
objects.
@params vba (VBA_Parser object) The olevba VBA_Parser object for
reading the Office file being analyzed.
@param strip_useless (boolean) Flag turning on/off modification of
VB code prior to parsing.
@return (list) A list of parsed Module objects.
"""
# Get the names of all the locally defined functions.
local_funcs = get_all_local_funcs(vba)
# Parse the VBA streams.
r = []
for (subfilename, stream_path, vba_filename, vba_code) in vba.extract_macros():
m = parse_stream(subfilename, stream_path, vba_filename, vba_code, strip_useless, local_funcs)
if (m is None):
return None
r.append(m)
return r
# === Top level utility functions ================================================================================
def read_excel_sheets(fname):
"""Read all the sheets of a given Excel file as CSV and return them
as a ExcelBook object.
@param fname (str) The name of the Excel file to read.
@return (core.excel.ExceBook object) On success return the Excel
sheets as an ExcelBook object. Returns None on error.
"""
# Read the sheets.
try:
f = open(fname, 'rb')
data = f.read()
f.close()
return excel.load_excel_libreoffice(data)
except Exception as e:
if (log.getEffectiveLevel() == logging.DEBUG):
log.debug("Reading Excel sheets failed. " + str(e))
return None
def pull_urls_office97(fname):
"""Pull URLs directly from an Office97 file.
@param fname (str) The name of the file from which to scrape
URLs.
@return (set) The URLs scraped from the file. This will be empty
if there are no URLs.
"""
return read_ole_fields.pull_urls_office97(fname, False, None)
# === Top level Programatic Interface ================================================================================
# pylint: disable=too-many-arguments
def process_file(container,
filename,
data,
strip_useless=False,
entry_points=None,
time_limit=None,
verbose=False,
display_int_iocs=False,
set_log=False,
tee_log=False,
tee_bytes=0,
artifact_dir=None,
out_file_name=None,
do_jit=False):
"""Process an Office file with VBA macros, a VBScript file, or
VBScript HTA file with ViperMonkey. This is the main programatic
interface for ViperMonkey.
@param container (str) Path and filename of container if the file is within
a zip archive, None otherwise.
@param filename (str) str, path and filename of file on disk, or
within the container.
@param data (bytes) content of the file if it is in a container,
None if it is a file on disk.
@param strip_useless (boolean) Flag turning on/off modification of
VB code prior to parsing.
@param entry_points (list) A list of the names (str) of the VB functions
from which to start emulation.
@param time_limit (int) The emulation time limit, in minutes. If
None there is not time limit.
@param verbose (boolean) Flag turning debug logging on/off.
@param display_int_iocs (boolean) Flag turning on/off the
reporting of intermediate IOCs (base64 strings and URLs) found
during the emulation process.
@param set_log (boolean) A flag??
@param tee_log (boolean) A flag turning on/off saving all of
ViperMonkey's output in a text log file. The log file will be
FNAME.log, where FNAME is the name of the file being analyzed.
@param tee_bytes (int) If tee_log is true, this gives the number
of bytes at which to cap the saved log file.
@param artifact_dir (str) The directory in which to save artifacts
dropped by the sample under analysis. If None the artifact
directory will be FNAME_artifacts/ where FNAME is the name of the
file being analyzed.
@param out_file_name (str) The name of the file in which to store
the ViperMonkey analysis results as JSON. If None no JSON results
will be saved.
@param do_jit (str) A flag turning on/off doing VB -> Python
transpiling of loops to speed up loop emulation.
@return (list) A list of actions if actions found, an empty list
if no actions found, and None if there was an error.
"""
# set logging level
if verbose:
colorlog.basicConfig(level=logging.DEBUG, format='%(log_color)s%(levelname)-8s %(message)s')
elif set_log:
colorlog.basicConfig(level=logging.INFO, format='%(log_color)s%(levelname)-8s %(message)s')
# assume they want a tee'd file if they give bytes for it
if tee_bytes > 0:
tee_log = True
# add handler for tee'd log file
if tee_log:
tee_filename = "./" + filename
if ("/" in filename):
tee_filename = "./" + filename[filename.rindex("/") + 1:]
if tee_bytes > 0:
capped_handler = CappedFileHandler(tee_filename + ".log", sizecap=tee_bytes)
capped_handler.setFormatter(logging.Formatter("%(levelname)-8s %(message)s"))
log.addHandler(capped_handler)
else:
file_handler = FileHandler(tee_filename + ".log", mode="w")
file_handler.setFormatter(logging.Formatter("%(levelname)-8s %(message)s"))
log.addHandler(file_handler)
# Check for files that do not exist.
if (isinstance(data, Exception)):
log.error("Cannot open file '" + str(filename) + "'.")
return None
# Read in file contents if we have not already been provided data to analyze.
if not data:
# TODO: replace print by writing to a provided output file (sys.stdout by default)
if container:
display_filename = '%s in %s' % (filename, container)
else:
display_filename = filename
safe_print('='*79)
safe_print('FILE: ' + str(display_filename))
# FIXME: the code below only works if the file is on disk and not in a zip archive
# TODO: merge process_file and _process_file
try:
input_file = open(filename,'rb')
data = input_file.read()
input_file.close()
except IOError as e:
log.error("Cannot open file '" + str(filename) + "'. " + str(e))
return None
r = _process_file(filename,
data,
strip_useless=strip_useless,
entry_points=entry_points,
time_limit=time_limit,
display_int_iocs=display_int_iocs,
artifact_dir=artifact_dir,
out_file_name=out_file_name,
do_jit=do_jit)
# Reset logging.
colorlog.basicConfig(level=logging.ERROR, format='%(log_color)s%(levelname)-8s %(message)s')
# Done.
return r
def _remove_duplicate_iocs(iocs):
"""Remove IOC strings that are substrings of other IOC strings.
@param iocs (list) List of IOCs (str).
@return (set) The original IOC list with duplicate-ish IOC strings
stripped out.
"""
# Track whether to keep an IOC string.
r = set()
skip = set()
log.info("Found " + str(len(iocs)) + " possible IOCs. Stripping duplicates...")
for ioc1 in iocs:
# Does this IOC look like straight up garbage?
if (read_ole_fields.is_garbage_vba(ioc1, test_all=True, bad_pct=.25)):
skip.add(ioc1)
continue
# Looks somewhat sensible. See if it is a duplicate.
keep_curr = True
for ioc2 in iocs:
if (ioc2 in skip):
continue
if ((ioc1 != ioc2) and (ioc1 in ioc2)):
keep_curr = False
break
if ((ioc1 != ioc2) and (ioc2 in ioc1)):
skip.add(ioc2)
if (keep_curr):
r.add(ioc1)
# Return stripped IOC set.
return r
def _get_vba_parser(data):
"""Get an olevba VBA_Parser object for reading an Office file. This
handles regular Office files and HTA files with VBScript script
elements.
@param data (str) The file contents for which to generate a
VBA_Parser.
@return (VBA_Parser object) On success, the olevba VBA_Parser
object for the given file contents. On error, None.
"""
# First just try the most common case where olevba can directly get the VBA.
vba = None
try:
vba = VBA_Parser('', data, relaxed=True)
except Exception as e:
if (log.getEffectiveLevel() == logging.DEBUG):
log.debug("Creating VBA_PArser() Failed. Trying as HTA. " + str(e))
# If that did not work see if we can pull HTA wrapped VB from the data.
extracted_data = get_vb_contents_from_hta(data)
# If this throws an exception it will get passed up.
vba = VBA_Parser('', extracted_data, relaxed=True)
# Return the vba parser.
return vba
def pull_embedded_pe_files(data, out_dir):
"""Directly pull out any PE files embedded in the given data. The PE
files will be saved in a directory and will be named things like
embedded*.exe.
@param data (str) The contents of the file being analyzed.
@param out_dir (str) The directory in which to save extracted PE
files.
"""
# Is this a Office 2007 (zip) file?
if core.filetype.is_office2007_file(data, is_data=True):
# convert data to a BytesIO buffer so that we can use zipfile in memory
# without writing a temp file on disk:
data_io = io.BytesIO(data)
# Pull embedded PE files from each file in the zip.
with zipfile.ZipFile(data_io, "r") as f:
for name in f.namelist():
curr_data = f.read(name)
pull_embedded_pe_files(curr_data, out_dir)
return
# Is a PE file in the data at all?
pe_pat = r"MZ.{70,80}This program (?:(?:cannot be run in DOS mode\.)|(?:must be run under Win32))"
if (re.search(pe_pat, data) is None):
return
# There is an embedded PE. Break them out.
# Get where each PE file starts.
pe_starts = []
for match in re.finditer(pe_pat, data):
pe_starts.append(match.span()[0])
pe_starts.append(len(data))
# Make the 2nd stage output directory if needed.
if not os.path.isdir(out_dir):
os.makedirs(out_dir)
# Break out each PE file. Note that we probably will get extra data,
# but due to the PE file format the file will be a valid PE (with an overlay).
pos = 0
out_index = 0
while (pos < len(pe_starts) - 1):
curr_data = data[pe_starts[pos]:pe_starts[pos+1]]
curr_name = out_dir + "/embedded_pe" + str(out_index) + ".bin"
# Make sure name is unique.
while os.path.isfile(curr_name):
out_index += 1
curr_name = out_dir + "/embedded_pe" + str(out_index) + ".bin"
f = open(curr_name, "wb")
f.write(curr_data)
f.close()
pos += 1
out_index += 1
def _report_analysis_results(vm, data, display_int_iocs, orig_filename, out_file_name):
"""Report analysis results (screen and file) to the user. Results will
be printed to stdout and saved in an output file as JSON if needed.
@param vm (ViperMonkey object) The ViperMonkey emulation engine
object that did the emulation.
@param data (str) The read in Office file (data).
@param display_int_iocs (boolean) Flag turning on/off the
reporting of intermediate IOCs (base64 strings and URLs) found
during the emulation process.
@param orig_filename (str) path and filename of file on disk, or
within the container.
@param out_file_name (str) The name of the file in which to store
the ViperMonkey analysis results as JSON. If None no JSON results
will be saved.
@return (tuple) A 3 element tuple where the 1st element is a list
of reported actions all converted to strings, the 2nd element is a
list of unique intermediate IOCs, and the 3rd element is a list of
shell code bytes injected by the VB (empty list if no shell code).
"""
# Print table of all recorded actions
safe_print('\nRecorded Actions:')
safe_print(vm.dump_actions())
safe_print('')
full_iocs = core.vba_context.intermediate_iocs
raw_b64_iocs = read_ole_fields.pull_base64(data)
for ioc in raw_b64_iocs:
if (core.vba_context.num_b64_iocs > 200):
log.warning("Found too many potential base64 IOCs. Skipping the rest.")
break
full_iocs.add(ioc)
core.vba_context.num_b64_iocs += 1
# Report intermediate IOCs.
tmp_iocs = []
if (len(full_iocs) > 0):
tmp_iocs = _remove_duplicate_iocs(full_iocs)
if (display_int_iocs):
safe_print('Intermediate IOCs:')
safe_print('')
for ioc in tmp_iocs:
safe_print("+---------------------------------------------------------+")
safe_print(ioc)
safe_print("+---------------------------------------------------------+")
safe_print('')
# Display injected shellcode.
shellcode_bytes = core.vba_context.get_shellcode_data()
if (len(shellcode_bytes) > 0):
safe_print("+---------------------------------------------------------+")
safe_print("Shell Code Bytes: " + str(shellcode_bytes))
safe_print("+---------------------------------------------------------+")
safe_print('')
# See if we can directly pull any embedded PE files from the file.
pull_embedded_pe_files(data, core.vba_context.out_dir)
safe_print('VBA Builtins Called: ' + str(vm.external_funcs))
safe_print('')
safe_print('Finished analyzing ' + str(orig_filename) + " .\n")
# Reporting results in JSON file?
if out_file_name:
# Create the results data structure.
actions_data = []
for action in vm.actions:
actions_data.append({
"action": str(action[0]),
"parameters": str(action[1]),
"description": str(action[2])
})
out_data = {
"file_name": orig_filename,
"potential_iocs": list(tmp_iocs),
"shellcode" : shellcode_bytes,
"vba_builtins": vm.external_funcs,
"actions": actions_data
}
# Write out the results as JSON.
try:
with open(out_file_name, 'w') as out_file:
out_file.write("\n" + json.dumps(out_data, indent=4))
except Exception as exc:
log.error("Failed to output results to output file. " + str(exc))
# Make sure all the action fields are strings before returning.
str_actions = []
for action in vm.actions:
str_actions.append((safe_str_convert(action[0]),
safe_str_convert(action[1]),
safe_str_convert(action[2])))
# Done.
return (str_actions, tmp_iocs, shellcode_bytes)
# Wrapper for original function; from here out, only data is a valid variable.
# filename gets passed in _temporarily_ to support dumping to vba_context.out_dir = out_dir.
def _process_file (filename,
data,
strip_useless=False,
entry_points=None,
time_limit=None,
display_int_iocs=False,
artifact_dir=None,
out_file_name=None,
do_jit=False):
"""Process a single file.
@param container (str) Path and filename of container if the file is within
a zip archive, None otherwise.
@param filename (str) path and filename of file on disk, or within
the container.
@param data (bytes) content of the file if it is in a container,
None if it is a file on disk.
@param strip_useless (boolean) Flag turning on/off modification of
VB code prior to parsing.
@param entry_points (list) A list of the names (str) of the VB functions
from which to start emulation.
@param time_limit (int) The emulation time limit, in minutes. If
None there is not time limit.
@param display_int_iocs (boolean) Flag turning on/off the
reporting of intermediate IOCs (base64 strings and URLs) found
during the emulation process.
@param artifact_dir (str) The directory in which to save artifacts
dropped by the sample under analysis. If None the artifact
@param out_file_name (str) The name of the file in which to store
the ViperMonkey analysis results as JSON. If None no JSON results
will be saved.
@param do_jit (str) A flag turning on/off doing VB -> Python
transpiling of loops to speed up loop emulation.
@return (list) A list of actions if actions found, an empty list
if no actions found, and None if there was an error.
"""
# Increase Python call depth.
sys.setrecursionlimit(13000)
# Set the emulation time limit.
if (time_limit is not None):
core.vba_object.max_emulation_time = datetime.now() + timedelta(minutes=time_limit)
# Create the emulator.
log.info("Starting emulation...")
vm = core.ViperMonkey(filename, data, do_jit=do_jit)
orig_filename = filename
if (entry_points is not None):
for entry_point in entry_points:
vm.entry_points.append(entry_point)
try:
#TODO: handle olefile errors, when an OLE file is malformed
if (isinstance(data, Exception)):
data = None
vba = None
try:
vba = _get_vba_parser(data)
except FileOpenError as e:
# Is this an unrecognized format?
if ("Failed to open file is not a supported file type, cannot extract VBA Macros." not in str(e)):
# No, it is some other problem. Pass on the exception.
raise e
# This may be VBScript with some null characters. Remove those and try again.
data = data.replace("\x00", "")
vba = _get_vba_parser(data)
# Do we have analyzable VBA/VBScript? Do the analysis even
# without VBA/VBScript if we are scraping for intermediate
# IOCs.
if (vba.detect_vba_macros() or display_int_iocs):
# Read in document metadata.
try:
log.info("Reading document metadata...")
ole = olefile.OleFileIO(data)
vm.set_metadata(ole.get_metadata())
except Exception as e:
log.warning("Reading in metadata failed. Trying fallback. " + str(e))
vm.set_metadata(get_metadata_exif(orig_filename))
# If this is an Excel spreadsheet, read it in.
vm.loaded_excel = excel.load_excel(data)
# Set where to store directly dropped files if needed.
if (artifact_dir is None):
artifact_dir = "./"
if ((filename is not None) and ("/" in filename)):
artifact_dir = filename[:filename.rindex("/")]
only_filename = filename
if ((filename is not None) and ("/" in filename)):
only_filename = filename[filename.rindex("/")+1:]
# Set the output directory in which to put dumped files generated by
# the macros.
out_dir = None
if (only_filename is not None):
out_dir = artifact_dir + "/" + only_filename + "_artifacts/"
if os.path.exists(out_dir):
shutil.rmtree(out_dir)
else:
out_dir = "/tmp/tmp_file_" + str(random.randrange(0, 10000000000))
log.info("Saving dropped analysis artifacts in " + out_dir)
core.vba_context.out_dir = out_dir
del filename # We already have this in memory, we don't need to read it again.
# Parse the VBA streams.
log.info("Parsing VB...")
comp_modules = parse_streams(vba, strip_useless)
if (comp_modules is None):
return None
got_code = False
for m in comp_modules:
if (m != "empty"):
vm.add_compiled_module(m)
got_code = True
if ((not got_code) and (not display_int_iocs)):
log.info("No VBA or VBScript found. Exiting.")
return ([], [], [], [])
# Get the VBA code.
vba_code = ""
for (_, _, _, macro_code) in vba.extract_macros():
if (macro_code is not None):
vba_code += macro_code
# Do not analyze the file if the VBA looks like garbage.
if (read_ole_fields.is_garbage_vba(vba_code)):
raise ValueError("VBA looks corrupted. Not analyzing.")
# Read in text values from all of the various places in
# Office 97/2000+ that text values can be hidden. So many
# places.
read_ole_fields.read_payload_hiding_places(data, orig_filename, vm, vba_code, vba)
# Do Emulation.
safe_print("")
safe_print('-'*79)
safe_print('TRACING VBA CODE (entrypoint = Auto*):')
if (entry_points is not None):
log.info("Starting emulation from function(s) " + str(entry_points))
pyparsing.ParserElement.resetCache()
vm.vba = vba
vm.trace()
# Done with emulation.
# Report the results.
str_actions, tmp_iocs, shellcode_bytes = _report_analysis_results(vm, data, display_int_iocs, orig_filename, out_file_name)
# Return the results.
return (str_actions, vm.external_funcs, tmp_iocs, shellcode_bytes)
# No VBA/VBScript found?
else:
safe_print('Finished analyzing ' + str(orig_filename) + " .\n")
safe_print('No VBA macros found.')
safe_print('')
return ([], [], [], [])
# Handle uncaught exceptions triggered during analysis.
except Exception as e:
# Print error info.
if (("SystemExit" not in str(e)) and (". Aborting analysis." not in str(e))):
traceback.print_exc()
log.error(str(e))
# If this is an out of memory error terminate the process with an
# error code indicating that there are memory problems. This is so
# that higer level systems using ViperMonkey can see that there is a
# memory issue and handle it accordingly.
if isinstance(e, MemoryError):
log.error("Exiting ViperMonkey with error code 137 (out of memory)")
sys.exit(137)
# Done. Analysis failed.
return None
def process_file_scanexpr (container, filename, data):
"""Process a single file.
@param container (str) Path and filename of container if the file is within
a zip archive, None otherwise.
@param filename (str) path and filename of file on disk, or within
the container.
@param data (bytes) Content of the file if it is in a container,
None if it is a file on disk.
"""
#TODO: replace print by writing to a provided output file (sys.stdout by default)
if container:
display_filename = '%s in %s' % (filename, container)
else:
display_filename = filename
safe_print('='*79)
safe_print('FILE: ' + str(display_filename))
all_code = ''
try:
#TODO: handle olefile errors, when an OLE file is malformed
import oletools
oletools.olevba.enable_logging()
if (log.getEffectiveLevel() == logging.DEBUG):
log.debug('opening %r' % filename)
vba = VBA_Parser(filename, data, relaxed=True)
if vba.detect_vba_macros():
# Read in document metadata.
vm = core.ViperMonkey(filename, data)
ole = olefile.OleFileIO(filename)
try:
vm.set_metadata(ole.get_metadata())
except Exception as e:
log.warning("Reading in metadata failed. Trying fallback. " + str(e))
vm.set_metadata(get_metadata_exif(filename))
#print 'Contains VBA Macros:'
for (subfilename, stream_path, vba_filename, vba_code) in vba.extract_macros():
# hide attribute lines:
#TODO: option to disable attribute filtering
vba_code = filter_vba(vba_code)
safe_print('-'*79)
safe_print('VBA MACRO %s ' % vba_filename)
safe_print('in file: %s - OLE stream: %s' % (subfilename, repr(stream_path)))
safe_print('- '*39)
# detect empty macros:
if vba_code.strip() == '':
safe_print('(empty macro)')
else:
# TODO: option to display code
safe_print(vba_code)
vba_code = core.vba_collapse_long_lines(vba_code)
all_code += '\n' + vba_code
safe_print('-'*79)
safe_print('EVALUATED VBA EXPRESSIONS:')
t = prettytable.PrettyTable(('Obfuscated expression', 'Evaluated value'))
t.align = 'l'
t.max_width['Obfuscated expression'] = 36
t.max_width['Evaluated value'] = 36