-
Notifications
You must be signed in to change notification settings - Fork 21
/
vba_seismograph.py
executable file
·544 lines (448 loc) · 18 KB
/
vba_seismograph.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
#!/usr/bin/env python
# This file is subject to the terms and conditions defined in file 'LICENSE.txt', which is part of this source code package.
# Check to see if an Office doc has had its compressed VBA source code stomped.
import argparse
import sys
import subprocess
import os
import zipfile
import time
import os.path
import shutil
import tempfile
import re
###########################################################################
def _get_pcode_ids(pcode):
"""
Pull out the function and variable names from the given p-code
disassembly.
pcode - (str) The p-code disassembly.
return - (set) The set of function and variable names.
"""
# Look at each line of the disassembly.
ids = set()
in_id_section = False
skip = False
instructions = None
for line in pcode.split("\n"):
# Should we skip this line?
if (skip):
skip = False
continue
# Is this the start of the ID section?
if (line == "Identifiers:"):
in_id_section = True
# Skip the next blank line.
skip = True
continue
# Is this the start of the instruction disassembly?
if (line.startswith("Line #") and (instructions is None)):
# Start saving instructions.
instructions = ""
continue
# Is this an instruction?
if (instructions is not None):
instructions += line + "\n"
continue
# Are we saving IDs?
if (in_id_section):
# Is this an ID line?
if (":" in line):
curr_id = line[line.index(":") + 1:].strip()
ids.add(curr_id)
continue
# Done with ID section?
else:
in_id_section = False
# These IDs seem to appear in the p-code and not necessarily in
# the VBA source code. Filter them out.
common_ids = set(["Word",
"VBA",
"Win16",
"Win32",
"Win64",
"Mac",
"VBA6",
"VBA7",
"Project1",
"stdole",
"VBAProject",
"Excel",
"Project",
"ThisDocument",
"_Evaluate",
"Normal",
"Office",
"Add",
"MSForms",
"UserForm",
"Document"])
# Now filter out the IDs that don't appear in the p-code
# instructions.
tmp = set()
for curr_id in ids:
# Skip IDs that are obviously not used or are common.
if (((instructions is not None) and (curr_id not in instructions)) or
(curr_id in common_ids) or
(curr_id.startswith("_B_var_"))):
continue
# Make sure the ID string is not embedded in some other
# string.
if (instructions is not None):
pat = "." + curr_id + "."
strs = re.findall(pat, instructions)
keep = False
for curr_str in strs:
if ((not curr_str[0].isalnum()) and
(not curr_str[len(curr_str) - 1].isalnum())):
keep = True
break
if (not keep):
continue
# It looks like some IDs in the p-code have underscores added
# to the prefix or suffix of the name. Strip those off so we
# can properly match the IDs in the VBA.
while (curr_id.startswith("_")):
curr_id = curr_id[1:]
while (curr_id.endswith("_")):
curr_id = curr_id[:-1]
# This is a valid ID. Save it.
tmp.add(curr_id)
# Use the filtered IDs.
ids = tmp
# Return the function names and variables.
return ids
###########################################################################
def _missing_ids(vba, pcode_ids, verbose=False):
"""
See if there are any function names or variables that appear in
the p-code that do not appear in the decompressed VBA source code.
vba - (str) The decompressed VBA source code.
pcode_ids - (set) The IDs defined in the p-code.
return - (float) % missing items.
"""
# Check each ID.
num_missing = 0.0
for curr_id in pcode_ids:
if (curr_id not in vba):
if (verbose):
print "P-code ID '" + str(curr_id) + "' is missing."
num_missing += 1
if (len(pcode_ids) == 0):
return 0.0
return (num_missing / len(pcode_ids))
###########################################################################
def _get_pcode_strs(pcode):
"""
Pull out string literals from the given p-code disassembly.
pcode - (str) The p-code disassembly.
return - (set) The set of literal strings.
"""
# Look at each line of the disassembly.
strs = set()
for line in pcode.split("\n"):
# Is this a string literal instruction?
line = line.strip()
if (line.startswith("LitStr ")):
curr_str = line[line.index('"') + 1:-1]
strs.add(curr_str.replace('"', '""'))
# Return the string literals.
return strs
###########################################################################
def _missing_strs(vba, pcode_strs, verbose=False):
"""
See if there are any string literals appear in the p-code that do
not appear in the decompressed VBA source code.
vba - (str) The decompressed VBA source code.
pcode_strs - (set) The string literals defined in the p-code.
return - (float) % missing items.
"""
# Check each string.
num_missing = 0.0
for curr_str in pcode_strs:
if ((('"' + curr_str + '"') not in vba) and
(("'" + curr_str + "'") not in vba)):
if (verbose):
print "P-code string '" + str(curr_str) + "' is missing."
num_missing += 1
if (len(pcode_strs) == 0):
return 0.0
return (num_missing / len(pcode_strs))
###########################################################################
def _get_pcode_comments(pcode):
"""
Pull out comments from the given p-code disassembly.
pcode - (str) The p-code disassembly.
return - (set) The set of comments.
"""
# Look at each line of the disassembly.
comments = set()
for line in pcode.split("\n"):
# Is this a comment instruction?
line = line.strip()
if (line.startswith("QuoteRem ")):
curr_str = line[line.index('"') + 1:-1]
if (curr_str.endswith("_")):
curr_str = curr_str[-1]
comments.add(curr_str)
# Return the comments.
return comments
###########################################################################
def _missing_comments(vba, pcode_comments, verbose=False):
"""
See if there are any comments appear in the p-code that do not
appear in the decompressed VBA source code.
vba - (str) The decompressed VBA source code.
pcode_comments - (set) The comments defined in the p-code.
return - (float) % missing items.
"""
# Check each comment.
num_missing = 0.0
for curr_str in pcode_comments:
# Try the easy case (1 line comments) first.
if (curr_str in vba):
continue
# Need to handle multiline comments. We do this by allowing
# newlines to appear where spaces appear.
pat = curr_str
pat = pat.replace("'", " ")
pat = pat.replace("\\", "\\\\")
pat = pat.replace("(", "\\(")
pat = pat.replace(")", "\\)")
pat = pat.replace("[", "\\[")
pat = pat.replace("]", "\\]")
pat = pat.replace("}", "\\}")
pat = pat.replace("}", "\\}")
pat = pat.replace("+", "\\+")
pat = pat.replace("^", "\\^")
pat = pat.replace(".", "\\.")
pat = pat.replace("$", "\\$")
pat = pat.replace("?", "\\?")
pat = pat.replace("*", "\\*")
pat = pat.replace(",", "\\,")
pat = pat.replace("|", "\\|")
tmp = ""
for i in pat.split(" "):
if (len(i) == 0):
continue
if (len(tmp) > 0):
tmp += "[\\s\\r\\n']{1,50}"
tmp += i
pat = tmp
if (re.search(pat, vba, re.MULTILINE) is None):
if (verbose):
print "P-code comment '" + str(curr_str) + "' is missing."
num_missing += 1
if (len(pcode_comments) == 0):
return 0.0
return (num_missing / len(pcode_comments))
###########################################################################
def _unzip_office_doc(filename):
"""
Extract the vbaProject.bin macro file from Office 2007+ files if
needed.
filename - (str) The Office doc file name.
return - (str) The name of the macro file to analyze. This will be
a temporary copy of vbaProject.bin for Office 2007+ files and will
be the original file name if not given a Office 2007+ file.
"""
# Is this an Office 2007+ file?
try:
file_type = subprocess.check_output(["file", filename])
if (("2007+" not in file_type) or
("Microsoft" not in file_type)):
return filename
except Exception as e:
raise ValueError("Running file on " + filename + \
" failed. " + str(e))
# If we get here we have a Office 2007+ file. Unzip it.
out_dir = None
try:
zip_ref = zipfile.ZipFile(filename, 'r')
millis = int(round(time.time() * 1000))
out_dir = filename
if (os.sep in out_dir):
out_dir = out_dir[out_dir.rindex(os.sep) + 1:]
out_dir = tempfile.gettempdir() + os.sep + out_dir + "_" + str(millis)
zip_ref.extractall(out_dir)
zip_ref.close()
except Exception as e:
raise ValueError("Zip extraction of " + filename + \
" failed. " + str(e))
# Look for word/vbaProject.bin in the unzipped directory.
# TODO: Handle renamed/extra vbaProject.bin files.
unzipped_filename = os.path.join(out_dir, "word", "vbaProject.bin")
if (os.path.isfile(unzipped_filename)):
return unzipped_filename
else:
raise ValueError(str(unzipped_filename) + " not found after " + \
"unzipping Office 2007+ file " + filename)
###########################################################################
def _cleanup_office_doc(orig_filename, filename):
"""
Delete the temporary directory where an Office 2007+ file was
unzipped if needed.
orig_filename - The original Office doc file name.
filename - The file that was analyzed. This could be
vbaProject.bin from the unzipped original file.
"""
# Is there anything to clean up?
if (orig_filename == filename):
return
# Delete the temporary directory of unzipped files.
subdir = os.sep + "word" + os.sep
if (subdir in filename):
tmp_dir = filename[:filename.index(subdir)]
shutil.rmtree(tmp_dir)
###########################################################################
def detect_stomping_via_pcode(filename, verbose=False, sensitivity="medium"):
"""
Detect VBA stomping by comparing variables, function names, and
static strings in the Office doc p-code to the same items in the
decompressed VBA source code.
filename - (str) The name of the Office file to check for VBA
stomping.
verbose - (boolean) If True print out detailed debugging
information.
sensitivity - (string) The sensitivity of the VBA stomping check to
perform ("low", "medium", or "high").
return - (boolean) True if the given Office doc has stomped VBA
source code, False if not.
raises - ValueError, if running sigtool or pcodedmp.py fails.
"""
# Extract the VBA macro file from Office 2007+ files if needed.
orig_filename = filename
filename = _unzip_office_doc(filename)
# Get the p-code disassembly.
pcode = None
try:
pcode = subprocess.check_output(["python", os.path.join(os.environ["PCODEDMP_DIR"], "pcodedmp.py"), filename])
except Exception as e:
raise ValueError("Running pcodedmp.py on " + orig_filename + \
" failed. " + str(e))
if (verbose):
print "----------------------------------------------"
print pcode
# Get the decompressed VBA source code.
vba = None
try:
vba = subprocess.check_output(["olevba", filename])
except Exception as e:
raise ValueError("Running olevba on " + orig_filename + \
" failed. " + str(e))
vba = vba.replace(chr(0x0d), "")
vba = vba.replace("_\n", "\n")
if (verbose):
print "----------------------------------------------"
print vba
print "----------------------------------------------"
# Figure out the threshold of missing items to trigger VBA stomping.
if (sensitivity == "low"):
threshold = .5
elif (sensitivity == "medium"):
threshold = .3
elif (sensitivity == "high"):
threshold = .1
# Get the variable and function names from the p-code.
pcode_ids = _get_pcode_ids(pcode)
# Check to see if all the function names and variables from the
# p-code appear in the decompressed VBA source code.
stomped = False
pct_missing_ids = _missing_ids(vba, pcode_ids, verbose)
if (pct_missing_ids > threshold):
stomped = True
# Get the string literals from the p-code.
pcode_strs = _get_pcode_strs(pcode)
# Check to see if all the string literals from the p-code appear
# in the decompressed VBA source code.
pct_missing_strs = _missing_strs(vba, pcode_strs, verbose)
if (pct_missing_strs > threshold):
stomped = True
# Get the comments from the p-code.
pcode_comments = _get_pcode_comments(pcode)
# Check to see if all the comments from the p-code appear
# in the decompressed VBA source code.
pct_missing_comments = _missing_comments(vba, pcode_comments, verbose)
if (pct_missing_comments > threshold):
stomped = True
# Clean up extracted 2007+ macro file if needed.
_cleanup_office_doc(orig_filename, filename)
# Print more info if needed.
if (verbose):
print "\n---------------------------------"
print "% Missing IDs:\t\t\t" + str(pct_missing_ids)
print "% Missing Strings:\t\t" + str(pct_missing_strs)
print "% Missing Comments:\t\t" + str(pct_missing_comments)
print "---------------------------------\n"
# Return whether the VBA source code was stomped.
return stomped
###########################################################################
def is_vba_stomped(filename, verbose=False, sensitivity="medium"):
"""
Check to see if the given Office doc file has had its VBA source
code stomped.
filename - (str) The name of the Office file to check for VBA
stomping.
verbose - (boolean) If True print out detailed debugging
information.
sensitivity - (string) The sensitivity of the VBA stomping check to
perform ("low", "medium", or "high").
return - (boolean) True if the given Office doc has stomped VBA
source code, False if not.
raises - ValueError, if running olevba or pcodedmp.py fails.
"""
# TODO: For now just detect with 1 method.
return detect_stomping_via_pcode(filename, verbose, sensitivity)
###########################################################################
## Main Program
###########################################################################
if __name__ == '__main__':
# Check to see if prerequisites are installed.
# Check pcodedmp.py
if ("PCODEDMP_DIR" not in os.environ):
print "ERROR: PCODEDMP_DIR environment variable not set. " + \
"This is the install directory for pcodedmp.py (https://github.com/bontchev/pcodedmp)."
sys.exit(1)
try:
subprocess.check_output(["python",
os.path.join(os.environ["PCODEDMP_DIR"], "pcodedmp.py"),
"-h"])
except Exception as e:
print "ERROR: It looks like pcodedmp is not installed. " + str(e) + "\n"
print "To install pcodedmp do the following:\n"
print "git clone https://github.com/bontchev/pcodedmp.git\n"
print "You will also need to set the PCODEDMP_DIR environment " + \
"variable to the pcodedmp install directory."
sys.exit(1)
# Check olevba.
try:
subprocess.check_output(["olevba", "-h"])
except Exception as e:
print "ERROR: It looks like olevba is not installed. " + str(e) + "\n"
print "To install olevba do the following:\n"
print "pip install oletools"
sys.exit(1)
# Get the arguments.
help_msg = "Check to see if a given Office doc file has had its" + \
" VBA source code stomped."
parser = argparse.ArgumentParser(description=help_msg)
parser.add_argument('-s', '--sensitivity', dest="sensitivity", action="store", default="medium",
help="Sensitivity of check (low, medium, high) (default=%default)")
parser.add_argument('-v', "--verbose",
help="Print debug information.",
action='store_true',
required=False)
parser.add_argument("doc",
help="The Office doc to check.")
args = parser.parse_args()
# Check for VBA stomping.
try:
if (is_vba_stomped(args.doc, args.verbose, args.sensitivity)):
print "WARNING: File " + args.doc + " is VBA stomped."
else:
print "File " + args.doc + " is NOT VBA stomped."
sys.exit(0)
except ValueError as e:
print "ERROR: VBA stomping check of " + str(args.doc) + \
" failed. " + str(e)
sys.exit(1)