-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler_handler.py
249 lines (213 loc) · 7.13 KB
/
crawler_handler.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import logging
from re import (
compile,
IGNORECASE
)
from crawler.sources import (
Source
)
from db import (
create_connection,
insert_data,
find_id_by_params,
update_data_by_id
)
log = logging.getLogger(__name__)
def crawler_handler(
sources: list[Source]
) -> None:
"""
Handles the processing and storage of parsed data from sources.
:param sources: A list of Source objects containing parsed data.
"""
cpe_pattern = compile(
"^cpe\:2\.3\:[aho]\:(\w{0,64})\:(\w{0,64})\:"
)
cwe_pattern = compile(
"^[cwe][A-z, -]{0,2}(\d{1,4})$"
)
conn = create_connection()
for source in sources:
try:
source_title = source['title']
source_priority = source['priority']
source_contents = source['content']
except KeyError as kErr:
log.error(
f"SourceHandleError: {kErr}"
)
continue
for source_content in source_contents:
try:
cve_name = source_content['cve_id']
insert_data(
conn=conn,
table_name="Cve",
query={
'name': cve_name
}
)
cve_id = find_id_by_params(
conn=conn,
table_name="Cve",
search_query={
'name': cve_name
}
)
insert_data(
conn=conn,
table_name="Event",
query={
'source': source_title,
'cve_id': cve_id
}
)
except KeyError as kErr:
log.error(
f"Failed to extract `cve_id` from parsed content ({source_title})."
)
continue
try:
published_date = source_content['published']
if published_date:
update_data_by_id(
conn=conn,
table_name="Cve",
query={
'published_date': published_date
},
row_id=cve_id
)
except KeyError as kErr:
log.error(
f"Failed to extract `published_date` from parsed content ({source_title})."
)
try:
description = source_content['description']
insert_data(
conn=conn,
table_name="Description",
query={
'value': description,
'source': source_title,
'priority': source_priority,
'cve_id': cve_id
}
)
except KeyError as kErr:
log.error(
f"Failed to extract `links` from parsed content ({source_title})."
)
try:
link = source_content['links']
insert_data(
conn=conn,
table_name="Link",
query={
'uri': link,
'cve_id': cve_id
}
)
except KeyError as kErr:
log.error(
f"Failed to extract `links` from parsed content ({source_title})."
)
try:
cvss = float(source_content['cvss'])
if cvss:
update_data_by_id(
conn=conn,
table_name="Cve",
query={
'cvss': cvss
},
row_id=cve_id
)
except KeyError as kErr:
log.error(
f"Failed to extract `cvss` from parsed content ({source_title})."
)
try:
cwe_content = source_content['cwe']
cwe_number = int(
cwe_pattern.search(
cwe_content,
IGNORECASE,
).group(1)
)
cwe_id = find_id_by_params(
conn=conn,
table_name="Cwe",
search_query={
'id': cwe_number
}
)
update_data_by_id(
conn=conn,
table_name="Cve",
query={
'cwe_id': cwe_id
},
row_id=cve_id
)
except KeyError as kErr:
log.error(
f"Failed to extract `cwe_id` from parsed content ({source_title})."
)
except AttributeError as aErr:
log.error(
f"Check format of the `cwe_id` in parsed content ({source_title})."
)
try:
cpe = source_content['cpe']
cpe_regex = cpe_pattern.search(
string=cpe
)
vendor_name = cpe_regex.group(1)
product_name = cpe_regex.group(2)
insert_data(
conn=conn,
table_name="Vendor",
query={
'name': vendor_name
}
)
vendor_id = find_id_by_params(
conn=conn,
table_name="Vendor",
search_query={
'name': vendor_name
}
)
insert_data(
conn=conn,
table_name="Product",
query={
'name': product_name,
'vendor_id': vendor_id
}
)
product_id = find_id_by_params(
conn=conn,
table_name="Product",
search_query={
'name': product_name
}
)
insert_data(
conn=conn,
table_name="Cpe",
query={
'value': cpe,
'vendor_id': vendor_id,
'product_id': product_id
}
)
except KeyError as kErr:
log.error(
f"Failed to extract `cpe` from parsed content ({source_title})."
)
except Exception as exception:
log.error(
f"Unexpected exception ({source_title}) was handled: {exception}."
)