-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathrun_deduplicator.py
More file actions
71 lines (63 loc) · 2.4 KB
/
run_deduplicator.py
File metadata and controls
71 lines (63 loc) · 2.4 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
# run_deduplicator script
# v0.10 BETA
# Runs the MAEC Deduplicator against a list or folder of MAEC files
import sys
import os
import argparse
import timeit
import maec
from maec.bundle.bundle import Bundle
from maec.package.package import Package
USAGE_TEXT = """
MAEC Run Deduplicator Script v0.10 BETA
*Performs Object-based Deduplication on one or more input MAEC Documents
*Saves deduplicated documents as <original_document_name>_deduplicated.xml
Usage: python run_deduplicator.py -l <single whitespace separated list of MAEC files> OR -d <directory name>
"""
# Process a set of MAEC binding objects and peform the deduplication as appropriate
def process_maec_file(filename):
fn, ext = os.path.splitext(filename)
new_filename = "%s_deduplicated.xml" % fn
start_time = timeit.default_timer()
parsed_objects = maec.parse_xml_instance(filename)
print "Parsing: " + str(timeit.default_timer() - start_time)
start_time = timeit.default_timer()
if parsed_objects and isinstance(parsed_objects['api'], Package):
parsed_objects['api'].deduplicate_malware_subjects()
parsed_objects['api'].to_xml_file(new_filename)
elif parsed_objects and isinstance(parsed_objects['api'], Bundle):
parsed_objects['api'].deduplicate()
parsed_objects['api'].to_xml_file(new_filename)
elapsed = timeit.default_timer() - start_time
print "Deduplicating: %s" % elapsed
def main():
parser = argparse.ArgumentParser(description=USAGE_TEXT)
mutex_group = parser.add_mutually_exclusive_group(required=True)
mutex_group.add_argument(
'-l', '--list', nargs='+',
help='single whitespace separated list of MAEC files'
)
mutex_group.add_argument(
'-d', '--directory',
help='directory name'
)
args = parser.parse_args()
#sys.stdout.write("Deduplicating.")
infilenames = []
list_mode = False
directoryname = ''
# Parse the input files and get the MAEC Bundles from each
if args.list:
for file in args.list:
#sys.stdout.write(".")
process_maec_file(file)
elif args.directory:
for filename in os.listdir(args.directory):
sys.stdout.write(".")
if '.xml' not in filename:
pass
else:
process_maec_file(os.path.join(args.directory, filename))
#sys.stdout.write("Done.")
if __name__ == "__main__":
main()