-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcve5-parse.py
executable file
·134 lines (102 loc) · 3.49 KB
/
cve5-parse.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
#!/usr/bin/env python3
import sys
import json
import os
import urllib.request
import elasticsearch
import elasticsearch.helpers
from elasticsearch import Elasticsearch
if 'ESURL' not in os.environ:
es_url = "http://localhost:9200"
else:
es_url = os.environ['ESURL']
if 'ESCERT' in os.environ:
cert = os.environ['ESCERT']
else:
cert = None
if cert:
es = Elasticsearch([es_url], ca_certs=cert)
else:
es = Elasticsearch([es_url])
class CVE:
def __init__(self, update_rate = 1000):
self.ids = []
self.current = -1
self.rh_data = None
self.update_rate = update_rate
def add(self, i):
# some of these don't exist, just give up if it fails
i['id'] = i["cveMetadata"]['cveId']
i['year'] = int(i['id'].split('-')[1])
i['just_id'] = int(i['id'].split('-')[2])
print(i['id'])
if 'x_legacyV4Record' in i["containers"]["cna"]:
del(i["containers"]["cna"]["x_legacyV4Record"])
if 'x_generator' in i["containers"]["cna"]:
del(i["containers"]["cna"]["x_generator"])
try:
i['description_len'] = len(i["containers"]["cna"]["descriptions"][0]["value"])
except:
pass
cve_bulk = {
"_op_type": "update",
"_index": "cve5-index",
"_id": i["id"],
"doc_as_upsert": True,
"doc": i
}
self.ids.append(cve_bulk)
self.__check_update()
def __check_update(self, force = False):
if self.update_rate == 1:
resp = es.index(index="cve5-index", id=self.ids[0]["_id"], document=self.ids[0]["doc"])
self.ids = []
self.current = -1
elif force or len(self) > self.update_rate:
for ok, item in elasticsearch.helpers.streaming_bulk(es, self, max_retries=2):
if not ok:
print("ERROR:")
print(item)
self.ids = []
self.current = -1
def done(self):
self.__check_update(True)
def __next__(self):
"Handle a call to next()"
self.current = self.current + 1
if self.current >= len(self.ids):
raise StopIteration
return self.ids[self.current]
def __iter__(self):
return self
def __len__(self):
return len(self.ids)
def main():
if len(sys.argv) > 1:
input_dir = sys.argv[1]
else:
print("Usage: %s <cve5 dir>" % (sys.argv[0]))
sys.exit(1)
# First let's see if the index exists
if es.indices.exists(index='cve5-index') is False:
# We have to create it and add a mapping
# Mapping is busted
#fh = open('cve-index-json-mapping.json')
#mapping = json.load(fh)
#es.indices.create(index='cve-index', mappings=mapping["mappings"], settings=mapping["settings"])
es.indices.create(index='cve5-index')
#the_cves = CVE(update_rate = 1)
the_cves = CVE()
for root, dirs, files in os.walk(input_dir):
for name in files:
if name.endswith(".json") and name.startswith("CVE"):
full_path = os.path.join(root, name)
print(full_path)
fh = open(full_path)
line = " ".join(fh.readlines())
if not line: break
json_data = json.loads(line)
the_cves.add(json_data)
the_cves.done()
if __name__ == "__main__":
main()