2121import Evtx .Views as e_views
2222import os
2323import xmltodict
24- from typing import Dict
24+ import json
2525
2626
2727def main ():
@@ -31,39 +31,37 @@ def main():
3131 description = "Dump a binary EVTX file into XML." )
3232 parser .add_argument ("evtx" , type = str ,
3333 help = "Path to the Windows EVTX event log file" )
34- parser .add_argument ("-o" ,"--output" ,type = str ,help = "Path to the output file" )
34+ parser .add_argument ("-o" ,"--output" ,type = str ,help = "Path to output JSON file" )
3535 args = parser .parse_args ()
3636
3737 with evtx .Evtx (args .evtx ) as log :
3838
3939 if (args .output ):
40- if (os .path .splitext (args .output )[1 ]== ".json" ):
41- for record in log .records ():
42- data_dict = xmltodict .parse (record .xml ()) #convert the xml to a dictionary
43- '''
44- for event_system_key,event_system_value in data_dict['Event']['System'].items(): #loop through each key and value pair
45- if isinstance(data_dict['Event']['System'][str(event_system_key)],Dict): #if the dictionary is nested, enter the dictionary
46- sublist=[]
47- for event_system_subkey,event_system_subvalue in data_dict['Event']['System'][str(event_system_key)].items(): #loop through the nested dictionary
48- print(event_system_key+"_"+event_system_subkey[1:] + ":" + str(event_system_subvalue))
49- else:
50- print(event_system_key + ":" + str(event_system_value))
51- '''
52- for event_system_key , event_system_value in data_dict ['Event' ]['System' ].items (): # loop through each key and value pair
53- if (event_system_key == "EventRecordID" ):
54- json_subline = {}
55- firstline = {event_system_key :event_system_value }
56- json_subline .update (firstline )
57- for event_data_key , event_data_value in data_dict ['Event' ]['EventData' ].items (): # loop through each key and value pair
58- for values in event_data_value :
59- for event_data_subkey ,event_data_subvalue in values .items ():
60- if event_data_subkey == "@Name" :
61- data_name = event_data_subvalue
62- else :
63- data_value = event_data_subvalue
64- json_subline .update ({data_name :data_value })
65- else :
66- print ("Invalid File Type" )
40+ final_json = []
41+ for record in log .records ():
42+ data_dict = xmltodict .parse (record .xml ()) #convert the xml to a dictionary
43+ for event_system_key , event_system_value in data_dict ['Event' ]['System' ].items (): # loop through each key and value pair
44+ if (event_system_key == "EventRecordID" ):
45+ json_subline = {}
46+ firstline = {event_system_key :event_system_value }
47+ json_subline .update (firstline ) #add the event ID to JSON subline
48+ for event_data_key , event_data_value in data_dict ['Event' ]['EventData' ].items (): # loop through each key and value pair
49+ for values in event_data_value :
50+ for event_data_subkey ,event_data_subvalue in values .items (): #loop through each
51+ if event_data_subkey == "@Name" : #extract the name from the value
52+ data_name = event_data_subvalue
53+ else :
54+ data_value = event_data_subvalue #extract the true value
55+ json_subline .update ({data_name :data_value }) #update the JSON sub line
56+ final_json .append (json_subline ) #update the final
57+
58+ # Output the JSON data
59+ if (os .path .splitext (args .output )[1 ] == ".json" ): #if the file extension is correct
60+ json_file = args .output
61+ else : # if the file extension is incorrect
62+ json_file = args .output + ".json"
63+ with open (json_file ,"w" ) as outfile : #write to an output file
64+ json .dump (final_json ,outfile )
6765 else :
6866 print (e_views .XML_HEADER )
6967 print ("<Events>" )
0 commit comments