'''
@author:Dheeraj Kumar Pant , 19111029
'''
import os
import pandas as pd
import pickle
import json
import csv


#These features are taken from summary part of behaviour section
features_behave_summ = ['directory_created','file_created','file_read', 'dll_loaded', 'regkey_opened', 
'file_failed', 'guid',  'file_exists', 'file_written' , 'file_opened', 'directory_enumerated', 'regkey_read',]

#These features are taken from network section
features_network=['udp','icmp','http','tcp','dns']

#INFODURA = Duration inside info section
#BEHGEN = Generic inside behavior section
#BEHPROC = Processes inside behavior section
features = ['INF0DURA','BEHGEN','BEHPROC']

#Features is final list of features that i have taken
features=features+features_network+features_behave_summ

#Hash_of_files used to record hash
hash_of_files=[]
hash_of_files.append('File_Hash')


def openJSON(direc):
	"""
	Function to open json file
	@param  : direc,name of file to open
	@return : data contained in file
	"""
	f = open(direc)
	data=json.load(f)
	f.close()
	return data

def direct_access_features(data,final_features,counter):
	"""
	Function to extract some important features described above in comments
	@param  : data(to extract feature from) , final_features(where all data-set stored) , counter
	"""
	final_features.append([])

	try:
		final_features[counter].append(data['info']['duration'])
	except:
		final_features[counter].append(0)
	try:
		final_features[counter].append(len(data['behavior']['generic']))
	except:
		final_features[counter].append(0)
	try:
		final_features[counter].append(len(data['behavior']['processes']))
	except:
		final_features[counter].append(0)


counter = 0

#Final_features will contain all data points extracted by this code
final_features=[]


work_dir = input('Enter the path: ')
current_directory = os.listdir(work_dir)

for direc in current_directory:


	hash_of_files.append(direc[:-5])

	data = openJSON(os.path.join(work_dir, direc))

	direct_access_features(data,final_features,counter)

	#Iterating over features present inside network and appending them to final_features
	for net in features_network:

		if data['network'].get(net,-1) != -1:
			final_features[counter].append(len(data['network'][net]))
		else:
			final_features[counter].append(0)
	
	#Iterating over summary features present inside behaviour and appending them to final_features
	for summary in features_behave_summ:
		if data['behavior']['summary'].get(summary,-1) != -1 :
			final_features[counter].append(len(data['behavior']['summary'][summary]))
		else:
			final_features[counter].append(0)
	counter +=1

#Convert final_features to dataframe
final_features=pd.DataFrame(final_features)

#Loading my Decision Tree model
dynamic_model = pickle.load(open('DYNAMIC_MODEL','rb'))

#Labels that are predict by above model is inside predicted_labels
predicted_labels=[]
predicted_labels.append('Predicted Label')
predicted_labels[1:] = list(dynamic_model.predict(final_features))

#This portion of code is just converting whole data into csv
with open('Dynamic.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerows(zip(hash_of_files, predicted_labels))