1- # MAEC 4.0.1 to MAEC 4.1 Converter Script
2- # Translates a MAEC 4.0.1 Package or Bundle into a valid MAEC 4.1 Package or Bundle
3-
4- import sys
5- import os
6- import shutil
7- import maec
8- from maec . bundle . bundle import Bundle
9- from maec .package . package import Package
10-
11- # Update the MAEC v4.0.1 file to MAEC v4.1
12- def update_maec ( infilename , outfilename ):
13- # Parse the input document using the parse_xml_instance() method
14- maec_objects = maec . parse_xml_instance ( infilename , check_version = False )
15-
16- # Get the API Object from the parsed input
17- api_object = maec_objects [ 'api' ]
18-
19- # Determine if we're dealing with a Package or Bundle
20- if isinstance ( api_object , Package ):
21- # Update the Package schema_version
22- api_object . schema_version = "2.1"
23- for malware_subject in api_object . malware_subjects :
24- for analysis in malware_subject . analyses :
25- # Replace the Analysis type value of "manual" with "in-depth"
26- if analysis . type and analysis . type == "manual" :
27- analysis .type = "in-depth"
28- # Update the schema_versions on the Bundles
29- for bundle in malware_subject . findings_bundles . bundles :
30- bundle . schema_version = "4.1"
31- elif isinstance ( api_object , Bundle ):
32- # Update the Bundle schema_version
33- api_object . schema_version = "4.1"
34-
35- # Output the updated MAEC object to XML
36- api_object . to_xml_file ( outfilename )
37-
38- # Print the usage text
39- def usage ():
40- print USAGE_TEXT
41- sys . exit ( 1 )
42-
43- USAGE_TEXT = """
44- MAEC 4.0.1 --> MAEC 4.1 XML Converter Utility
45-
46- Usage: python maec_4.0.1_to_4.1.py -i <input maec 4.0.1 xml file> -o <output maec 4.1 xml file>
47- """
48-
49- def main ():
50- infilename = None
51- outfilename = None
52- directoryname = ' '
53- filepath = ''
54-
55- #Get the command-line arguments
56- args = sys . argv [ 1 :]
57-
58- if len ( args ) < 2 :
59- usage ( )
60- sys . exit ( 1 )
61-
62- for i in range ( 0 , len ( args )):
63- if args [ i ] == '-i' :
64- infilename = args [ i + 1 ]
65- elif args [ i ] == '-o' :
66- outfilename = args [ i + 1 ]
67- elif args [ i ] == '-d' :
68- directoryname = args [ i + 1 ]
69-
70- if directoryname != '' :
71- for filename in os . listdir ( directoryname ) :
72- print filename
73- if '.xml' not in filename :
74- pass
75- elif '_report.maec-4.0.1' not in filename :
76- update_maec ( os . path . join ( directoryname , filename ), filename . rstrip ( '.xml' ) + '_cuckoobox_maec.xml' )
77- else :
78- new_filepath = os . path . join ( directoryname , filename . replace ( '_report.maec-4.0.1' , '' ))
79- shutil . move ( os .path .join (directoryname , filename ), new_filepath )
80- update_maec ( new_filepath , new_filepath . rstrip ( '.xml' ) + '_cuckoobox_maec.xml' )
81-
82- # Basic parameter checking
83- elif infilename and outfilename :
84- update_maec ( infilename , outfilename )
85-
86- if __name__ == "__main__" :
87- main ()
88-
1+ # MAEC 4.0.1 to MAEC 4.1 Converter Script
2+ # Translates a MAEC 4.0.1 Package or Bundle into a valid MAEC 4.1 Package or Bundle
3+
4+ import sys
5+ import os
6+ import shutil
7+ import argparse
8+ import maec
9+ from maec .bundle . bundle import Bundle
10+ from maec . package . package import Package
11+
12+ # Update the MAEC v4.0.1 file to MAEC v4.1
13+ def update_maec ( infilename , outfilename ):
14+ # Parse the input document using the parse_xml_instance() method
15+ maec_objects = maec . parse_xml_instance ( infilename , check_version = False )
16+
17+ # Get the API Object from the parsed input
18+ api_object = maec_objects [ 'api' ]
19+
20+ # Determine if we're dealing with a Package or Bundle
21+ if isinstance ( api_object , Package ):
22+ # Update the Package schema_version
23+ api_object . schema_version = "2.1"
24+ for malware_subject in api_object . malware_subjects :
25+ for analysis in malware_subject . analyses :
26+ # Replace the Analysis type value of "manual" with "in-depth"
27+ if analysis . type and analysis .type == "manual" :
28+ analysis . type = "in-depth"
29+ # Update the schema_versions on the Bundles
30+ for bundle in malware_subject . findings_bundles . bundles :
31+ bundle . schema_version = "4.1"
32+ elif isinstance ( api_object , Bundle ):
33+ # Update the Bundle schema_version
34+ api_object . schema_version = "4.1"
35+
36+ # Output the updated MAEC object to XML
37+ api_object . to_xml_file ( outfilename )
38+
39+ # Print the usage text
40+ def usage ():
41+ print USAGE_TEXT
42+ sys . exit ( 1 )
43+
44+ USAGE_TEXT = """
45+ MAEC 4.0.1 --> MAEC 4.1 XML Converter Utility
46+ Usage: python maec_4.0.1_to_4.1.py -i <input maec 4.0.1 xml file> -o <output maec 4.1 xml file>
47+ """
48+
49+ def main ():
50+ # Setup the argument parser
51+ parser = argparse . ArgumentParser (
52+ description = 'MAEC 4.0.1 --> MAEC 4.1 XML Converter Utility '
53+ )
54+ mutex_group = parser . add_mutually_exclusive_group ( required = True )
55+ required_name = parser . add_argument_group ( 'required arguments' )
56+ mutex_group . add_argument (
57+ '--input' , '-i' ,
58+ help = 'input maec 4.0.1 xml file'
59+ )
60+ mutex_group . add_argument (
61+ '--directory' , '-d' ,
62+ help = 'directory containing maec 4.0.1 xml files to convert to 4.1 xml files'
63+ )
64+ required_name . add_argument (
65+ '--output' , '-o' , required = True ,
66+ help = 'output maec 4.1 xml file'
67+ )
68+
69+ args = parser . parse_args ()
70+
71+ if args . directory :
72+ for filename in os . listdir ( args . directory ):
73+ print filename
74+ if '.xml' not in filename :
75+ pass
76+ elif '_report.maec-4.0.1' not in filename :
77+ update_maec ( os . path . join ( args . directory , filename ), filename . rstrip ( '.xml' ) + '_cuckoobox_maec.xml' )
78+ else :
79+ new_filepath = os .path .join (args . directory , filename . replace ( '_report.maec-4.0.1' , '' ) )
80+ shutil . move ( os . path . join ( args . directory , filename ), new_filepath )
81+ update_maec ( new_filepath , new_filepath . rstrip ( '.xml' ) + '_cuckoobox_maec.xml' )
82+
83+ # Basic parameter checking
84+ elif args . input and args . output :
85+ update_maec ( args . input , args . output )
86+
87+ if __name__ == "__main__" :
88+ main ()
0 commit comments