-
Notifications
You must be signed in to change notification settings - Fork 1
/
merging_16_22_databases.py
241 lines (169 loc) · 6.17 KB
/
merging_16_22_databases.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
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 11 14:36:15 2022
@author: md arman hossain
"""
import pandas as pd
import os
from PyQt5.QtWidgets import (
QApplication)
dddddddddd = {}
def insertKeyValue(key,fromData):
global dddddddddd
if(key in dddddddddd.keys()):
dddddddddd[key] = dddddddddd[key] +"#arman#"+ str(fromData)
else:
dddddddddd[key] = str(fromData)
def aggregate2(indexKey,json):
global dddddddddd
if(type(json) is dict):
# print('hello')
for key in json.keys():
QApplication.processEvents()
# if(key in excludeList):
# continue
if(type(json[key]) is not dict and type(json[key]) is not list):
insertKeyValue(key,json[key])
else:
aggregate2(key,json[key])
elif(type(json) is list):
# print('hello_2')
for item in json:
QApplication.processEvents()
if(type(item) is not list and type(item) is not dict):
insertKeyValue(indexKey,item)
else:
aggregate2(indexKey,item)
def convert_obj_list2df(list_of_obj):
df = pd.DataFrame()
length = len(list_of_obj)
index=0
for item in list_of_obj:
for key in item:
if key not in df.columns:
df[key] = [None]*length
df[key][index] = item[key]
index+=1
return df
def remane_col(df):
df.rename(columns = {'accessVector':'attackVector_v2',
'accessComplexity':'attackComplexity_v2',
'authentication':'privilegesRequired_v2',
'severity':'baseSeverity_v2',
'value':'description'
}, inplace = True)
return df
def split_col(df):
columns = ['description','vectorString','confidentialityImpact','integrityImpact','availabilityImpact','baseScore',
'exploitabilityScore','impactScore']
for col in df.columns:
if col not in columns:
continue
# print(col)
if col == 'description':
newCol = 'weaknessType'
df[newCol] = [None]*len(df[col])
index = 0
for value in df[col]:
if value == None:
df[col][index] = None
index+=1
continue
valueLst = value.split('#arman#')
if len(valueLst)==2:
df[col][index] = valueLst[1]
df[newCol][index] = valueLst[0]
elif len(valueLst)==1:
df[col][index] = valueLst[0]
elif len(valueLst)>2:
df[col][index] = valueLst[-1]
df[newCol][index] = valueLst[0]
index+=1
continue
newCol = col+"_v2"
df[newCol] = [None]*len(df[col])
index = 0
for value in df[col]:
if value == None:
df[col][index] = None
index+=1
continue
valueLst = value.split('#arman#')
if len(valueLst)==2:
df[col][index] = valueLst[0]
df[newCol][index] = valueLst[1]
elif len(valueLst)==1:
df[newCol][index] = valueLst[0]
df[col][index] = None
elif len(valueLst)>2:
df[col][index] = valueLst[0]
df[newCol][index] = valueLst[1]
index+=1
return df
def json_to_datafm(file_path):
list_of_obj = []
global dddddddddd
df = pd.read_json(file_path)
cve = df['CVE_Items']
for index in range(len(cve)):
# print(index)
aggregate2('dump',cve[index])
list_of_obj.append(dddddddddd)
dddddddddd = {}
df = convert_obj_list2df(list_of_obj)
df = remane_col(df)
df = split_col(df)
return df
def merge_all(path='./downloaded',modified_year=['']):
isExist = os.path.exists('./data/2002_22_nvd.csv')
all_year_lst = []
if not isExist:
for year in range(2002,3000):
print('mergering: '+str(year))
c_path = path+'/nvdcve-1.1-'+str(year)+'.json'
isExist = os.path.exists(c_path)
if not isExist:
break
# print(c_path)
year_dat = json_to_datafm(c_path)
all_year_lst.append(year_dat)
lst2 = ['recent','modified']
for i in range(len(lst2)):
print('mergering: '+str(lst2[i]))
c_path = path+'/nvdcve-1.1-'+str(lst2[i])+'.json'
isExist = os.path.exists(c_path)
year_dat = json_to_datafm(c_path)
all_year_lst.append(year_dat)
all_year_df = pd.concat(all_year_lst)
all_year_df.to_csv('./data/2002_22_nvd.csv',index=False)
else:
for i in range(len(modified_year)):
print('mergering: '+str(modified_year[i]))
c_path = path+'/nvdcve-1.1-'+str(modified_year[i])+'.json'
isExist = os.path.exists(c_path)
if not isExist:
break
# print(c_path)
year_dat = json_to_datafm(c_path)
all_year_lst.append(year_dat)
all_year_df = pd.concat(all_year_lst)
all_year_df = all_year_df.reset_index(drop=True)
data = pd.read_csv('./data/2002_22_nvd.csv')
for i in all_year_df.index:
if all_year_df['ID'][i] in data['ID'].tolist():
# print("matched ",i,all_year_df['ID'][i])
for col in data:
data[col][i] = all_year_df[col][i]
else:
data.loc[len(data)] = all_year_df.loc[i]
data.to_csv('./data/2002_22_nvd.csv',index=False)
# return all_year_df
def save_data(df,name,path='../data/output'):
df.to_csv(path+'/'+name+'.csv',index=False)
if __name__ == '__main__':
pass
# dat3 = merge_all('./downloaded',2002,2022)
# df = split_col(df)
# saving file
# save_data(result, 'all')
# dat3.to_csv('2002_22_nvd.csv',index=False)