-
Notifications
You must be signed in to change notification settings - Fork 22
/
gha.py
executable file
·290 lines (271 loc) · 11.2 KB
/
gha.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
"""
GitHub Security Advisory to NVD CVE converter
This module fetches the recent security advisories from GitHub and stores them in NVD CVE 1.1 json format. Below substitutions are made to properly construct the NVD CVE Json
- versionStartIncluding and versionEndIncluding are calculated from version range. Version End is used to hold any single version number being passed
- vectorString is constructed based on severity. The official calculator [url](https://www.first.org/cvss/calculator/3.1) was used to construct some realistic strings for given severity
- Full description (description) is ignored for now
"""
import logging
import os
import re
import httpx
import orjson
from vdb.lib import config
from vdb.lib.nvd import NvdSource
from vdb.lib.utils import compress_str, get_default_cve_data, get_cvss3_from_vector
logging.basicConfig(
level=logging.INFO, format="%(levelname)s [%(asctime)s] %(message)s"
)
LOG = logging.getLogger(__name__)
api_token = os.environ.get("GITHUB_TOKEN")
headers = {"Authorization": f"token {api_token}"}
vendor_overrides = {
"pip": "pypi",
"go": "golang",
"rust": "cargo",
"rubygems": "gem"
}
def get_query(qtype="recent"):
""""""
if qtype == "recent" or not qtype:
extra_args = "first: 100"
else:
extra_args = 'first: 100, after: "' + str(qtype) + '"'
gqljson = {
"query": """
query {
securityAdvisories(%(extra_args)s) {
nodes {
id
ghsaId
summary
description
identifiers {
type
value
}
origin
publishedAt
updatedAt
references {
url
}
severity
withdrawnAt
vulnerabilities(first: 10) {
nodes {
firstPatchedVersion {
identifier
}
package {
ecosystem
name
}
severity
updatedAt
vulnerableVersionRange
}
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
"""
% dict(extra_args=extra_args)
}
return gqljson
class GitHubSource(NvdSource):
"""GitHub CVE source"""
def download_all(self):
"""Download all historic cve data"""
data_list = []
last_id = None
for y in range(0, int(config.GHA_PAGES_COUNT)):
data, page_info = self.fetch(vtype=last_id)
if data:
self.store(data)
if page_info and page_info["hasNextPage"]:
last_id = page_info["endCursor"]
return data_list
def download_recent(self):
"""Method which downloads the recent CVE"""
data, _ = self.fetch("recent")
if data:
self.store(data)
return data
def fetch(self, vtype):
"""Private method to fetch the advisory data via GraphQL api"""
LOG.debug(
"Download GitHub advisory from %s with cursor %s", config.GHA_URL, vtype
)
client = httpx.Client(http2=True, follow_redirects=True, timeout=180)
r = client.post(
url=config.GHA_URL, json=get_query(qtype=vtype), headers=headers
)
json_data = r.json()
return self.convert(json_data)
@staticmethod
def get_version_range(version_str):
"""
Version range format - https://developer.github.com/v4/object/securityvulnerability/
= 0.2.0 denotes a single vulnerable version.
<= 1.0.8 denotes a version range up to and including the specified version
< 0.1.11 denotes a version range up to, but excluding, the specified version
>= 4.3.0, < 4.3.5 denotes a version range with a known minimum and maximum version.
>= 0.0.1 denotes a version range with a known minimum, but no known maximum
> 2.1.0, < 2.1.8
> 2.0.0, <= 2.0.14
"""
version_start_including = ""
version_end_including = ""
version_start_excluding = ""
version_end_excluding = ""
if version_str.startswith("= "):
version_start_including = version_str.replace("= ", "")
version_end_including = version_start_including
elif version_str.startswith("<= "):
version_end_including = version_str.replace("<= ", "")
elif version_str.startswith("< "):
version_end_excluding = version_str.replace("< ", "")
elif version_str.startswith(">= "):
version_start_including = version_str.replace(">= ", "").split(", ")[0]
elif version_str.startswith("> "):
version_start_excluding = version_str.replace("> ", "").split(", ")[0]
if version_end_excluding == "":
tmp_a = version_str.split(", ")
if tmp_a[len(tmp_a) - 1].startswith("<= "):
version_end_including = tmp_a[len(tmp_a) - 1].replace("<= ", "")
elif tmp_a[len(tmp_a) - 1].startswith("< "):
version_end_excluding = tmp_a[len(tmp_a) - 1].replace("< ", "")
if (
not version_start_including
and not version_end_including
and not version_start_excluding
and not version_end_excluding
):
version_start_including = version_str
return (
version_start_including,
version_end_including,
version_start_excluding,
version_end_excluding,
)
def convert(self, cve_data):
"""Convert the GitHub advisory data into Vulnerability objects"""
ret_data = []
if cve_data.get("errors"):
return ret_data, None
if cve_data.get("message") and cve_data.get("message") == "Bad credentials":
LOG.warning("GITHUB_TOKEN environment variable is invalid!")
return ret_data, None
page_info = cve_data["data"]["securityAdvisories"].get("pageInfo")
for cve in cve_data["data"]["securityAdvisories"]["nodes"]:
# If this CVE is withdrawn continue
if cve.get("withdrawnAt"):
continue
cve_id = None
assigner = "mitre"
references = []
for r in cve["references"]:
references.append({"url": r["url"], "name": r["url"]})
for cid in cve["identifiers"]:
if cid["type"] == "CVE":
cve_id = cid["value"]
if not cve_id:
cve_id = cve["ghsaId"]
assigner = "github_m"
references = orjson.dumps(references, option=orjson.OPT_NAIVE_UTC)
if isinstance(references, bytes):
references = references.decode("utf-8", "ignore")
for p in cve["vulnerabilities"]["nodes"]:
if not p:
continue
vendor = p["package"]["ecosystem"].lower()
product = p["package"]["name"]
vendor = vendor_overrides.get(vendor, vendor)
if vendor not in ("golang", "swift", "composer") and (":" in product or "/" in product):
tmp_a = re.split(r"[/|:]", product)
# This extract's the correct vendor based on the namespace
# Eg: org.springframework:spring-webflux would result in
# product: org.springframework/spring-webflux
product = f"{tmp_a[0]}/{tmp_a[len(tmp_a) - 1]}"
if vendor not in ("nuget",):
product = product.lower()
version = p["vulnerableVersionRange"]
(
version_start_including,
version_end_including,
version_start_excluding,
version_end_excluding,
) = self.get_version_range(version)
top_fix_version = p.get("firstPatchedVersion")
if not top_fix_version or not top_fix_version.get("identifier"):
top_fix_version = {"identifier": ""}
fix_version = top_fix_version.get("identifier", {})
(
fix_version_start_including,
fix_version_end_including,
fix_version_start_excluding,
fix_version_end_excluding,
) = self.get_version_range(fix_version)
severity = p["severity"]
(
score,
severity,
vector_string,
attack_complexity,
) = get_default_cve_data(severity)
exploitability_score = score
cvss3_obj = get_cvss3_from_vector(vector_string)
attack_complexity = ""
user_interaction = "REQUIRED"
if cvss3_obj:
exploitability_score = cvss3_obj.get("temporalScore")
attack_complexity = cvss3_obj.get("attackComplexity")
score = cvss3_obj.get("baseScore")
user_interaction = cvss3_obj.get("userInteraction")
description = """# {}
{}
""".format(
cve.get("summary"), cve.get("description")
)
tdata = config.CVE_TPL % dict(
cve_id=cve_id,
cwe_id="UNKNOWN",
assigner=assigner,
references=references,
description="",
vectorString=vector_string,
vendor=vendor.lower(),
product=product.lower(),
version="*",
edition="*",
version_start_including=version_start_including,
version_end_including=version_end_including,
version_start_excluding=version_start_excluding,
version_end_excluding=version_end_excluding,
fix_version_start_including=fix_version_start_including,
fix_version_end_including=fix_version_end_including,
fix_version_start_excluding=fix_version_start_excluding,
fix_version_end_excluding=fix_version_end_excluding,
severity=severity,
attackComplexity=attack_complexity,
score=score,
userInteraction=user_interaction,
exploitabilityScore=exploitability_score,
publishedDate=cve["publishedAt"],
lastModifiedDate=cve["updatedAt"],
)
try:
tdata_json = orjson.loads(tdata)
vuln = NvdSource.convert_vuln(tdata_json)
if vuln is None:
continue
vuln.description = compress_str(description)
ret_data.append(vuln)
except Exception as e:
LOG.debug(e)
return ret_data, page_info