-
Notifications
You must be signed in to change notification settings - Fork 9
/
wsa
executable file
·540 lines (438 loc) · 19.6 KB
/
wsa
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#! /usr/bin/env python3
from argparse import ArgumentParser
from dataclasses import asdict
from pathlib import Path
from ruamel.yaml import YAML
from time import strftime
import re
import sys
import logging
import textwrap
class Formatter:
text_width: int
item_indent: int
cve_list_indent: int
bug_bullet = ""
t_header: str
t_cve: str
t_footer: str
t_footer_common = textwrap.dedent("""\
We recommend updating to the latest stable versions of WebKitGTK
and WPE WebKit. It is the best way to ensure that you are running
safe versions of WebKit. Please check our websites for information
about the latest stable releases.""")
def __init__(self, wsa_id: str, wsa_data: dict):
self.wsa_id = wsa_id
self.wsa_data = wsa_data
self._data = {
"WSA": wsa_id,
"reportdate": strftime("%B %d, %Y"),
"affectedtext": self._line_for_print(
"Several vulnerabilities were discovered in WebKitGTK"
" and WPE WebKit."),
}
def _line_for_print(self, line, indent=2, width=None, dot=True):
if width is None:
width = self.text_width
if dot and not line.endswith('.'):
line += '.'
line = textwrap.wrap(line, width=(width - indent))
line = "\n".join(line)
line = textwrap.indent(line, " " * indent)
return line.strip()
def _expand_template(self, template: str, **kw) -> str:
tvars = dict(self._data)
tvars.update(kw)
return template % tvars
@staticmethod
def linkify(target: str) -> str:
return target
def title_spaces(self, title: str) -> str:
return " "
def cve_list(self) -> str:
raise NotImplementedError
def generate(self, write, cve_data_path: Path|None = None,
fill_missing_description=False):
cve_cache = None
self.header(write)
for cve_id in sorted(self.wsa_data.keys()):
cve_data = self.wsa_data[cve_id]
affected_text = "Versions affected:"
version = cve_data["version"]
if isinstance(version, str):
affected_text += f" WebKitGTK and WPE WebKit before {version}"
else:
assert isinstance(cve_data, dict)
if "gtk" in version:
affected_text += f" WebKitGTK before {version['gtk']}"
if "wpe" in version:
affected_text += f" and WPE WebKit before {version['wpe']}"
else:
assert "wpe" in version
affected_text += f" WPE WebKit before {version['wpe']}"
affected_text += "."
description = None
if "impact" in cve_data:
if "description" in cve_data:
description = f"Impact: {cve_data['impact']} Description: {cve_data['description']}"
else:
description = f"Impact: {cve_data['impact']}"
elif "description" in cve_data:
description = cve_data["description"]
elif fill_missing_description:
if cve_cache is None:
import cve
assert isinstance(cve_data_path, Path)
cve_cache = cve.LRUMemDiskFetcherCache(cve_data_path)
cve = cve_cache.get(cve_id)
if cve and cve.description:
description = cve.description
if description is None:
description = "No description was provided"
author = cve_data.get("author", "an anonymous researcher")
bug_link = ""
bug_bullet = ""
if "bugzilla" in cve_data:
bug_link = f"WebKit Bugzilla: {cve_data['bugzilla']}"
bug_bullet = self.bug_bullet
write(self._expand_template(self.t_cve,
CVE=self.linkify(cve_id),
affected=self._line_for_print(affected_text, indent=self.item_indent),
credits=self._line_for_print(author, indent=self.item_indent),
description=self._line_for_print(description, indent=self.item_indent),
maybeLinkbug=bug_link,
maybeLinkbugBullet=bug_bullet))
self.footer(write)
def header(self, write):
title = "WebKitGTK and WPE WebKit Security Advisory"
write(self._expand_template(self.t_header, title=title,
titlespaces=self.title_spaces(title),
cvelist=self.cve_list()))
def footer(self, write):
write(self._line_for_print(self.t_footer_common, indent=0))
moreinfo = "Further information about WebKitGTK and WPE WebKit" \
" security advisories can be found at: "
moreinfo += self.linkify("https://webkitgtk.org/security.html")
moreinfo += " or "
moreinfo += self.linkify("https://wpewebkit.org/security")
write(self._expand_template(self.t_footer, moreinfo=self._line_for_print(moreinfo, indent=0)))
class Mail(Formatter):
text_width = 72
item_indent = 4
cve_list_indent = 26
t_header = textwrap.dedent("""\
Subject: %(title)s %(WSA)s
To: webkit-gtk@lists.webkit.org, webkit-wpe@lists.webkit.org
Cc: security@webkit.org, oss-security@lists.openwall.com
------------------------------------------------------------------------
%(title)s%(titlespaces)s%(WSA)s
------------------------------------------------------------------------
Date reported : %(reportdate)s
Advisory ID : %(WSA)s
WebKitGTK Advisory URL : https://webkitgtk.org/security/%(WSA)s.html
WPE WebKit Advisory URL : https://wpewebkit.org/security/%(WSA)s.html
CVE identifiers : %(cvelist)s
%(affectedtext)s
""")
t_cve = textwrap.dedent("""\
%(CVE)s
%(affected)s
Credit to %(credits)s
%(description)s
%(maybeLinkbugBullet)s%(maybeLinkbug)s
""")
t_footer = textwrap.dedent("""\
%(moreinfo)s
The WebKitGTK and WPE WebKit team,
""")
def title_spaces(self, title: str) -> str:
return " " * (72 - len(title) - len(self.wsa_id))
def cve_list(self) -> str:
return self._line_for_print(", ".join(sorted(self.wsa_data.keys())),
indent=self.cve_list_indent)
class Markdown(Formatter):
cve_list_indent = 2
item_indent = 4
text_width = 90
bug_bullet = "* "
t_header = textwrap.dedent("""\
---
layout: post
title: %(title)s%(titlespaces)s%(WSA)s
permalink: /security/%(WSA)s.html
tags: WSA
---
* Date Reported: **%(reportdate)s**\n
* Advisory ID: **%(WSA)s**\n
* CVE identifiers: %(cvelist)s\n
%(affectedtext)s
""")
t_cve = textwrap.dedent("""\
* %(CVE)s
* %(affected)s
* Credit to %(credits)s
* %(description)s
%(maybeLinkbugBullet)s%(maybeLinkbug)s
""")
t_footer = textwrap.dedent("""\
%(moreinfo)s
""")
def cve_list(self) -> str:
return ", ".join(map(lambda x: self.linkify(x, anchor=True),
sorted(self.wsa_data.keys())))
@staticmethod
def linkify(target: str, anchor=False) -> str:
if target.startswith("http://"):
text = target[len("http://"):]
elif target.startswith("https://"):
text = target[len("https://"):]
elif target.startswith("CVE-"):
if anchor:
return f"[{target}](#{target})"
else:
return f"<a name='{target}' href='https://cve.mitre.org/cgi-bin/cvename.cgi?name={target}'>{target}</a>"
else:
text = target
return f"[{text}]({target})"
def get_cve_data_path(p: Path | None) -> Path:
if p is None:
p = Path(__file__).parent / "cvedata" / "cache"
return p.resolve()
wsa_id_re = re.compile(r"(WSA-\d{4}-\d+)")
def determine_wsa_id(s: str) -> str | None:
for m in wsa_id_re.finditer(s):
return m[1]
return None
def cmd_generate(args):
if (not (args.email or args.markdown)) or (args.email and args.markdown):
raise SystemExit("Must use one of --email or --markdown")
if not args.report_yml.is_file():
raise SystemExit(f"File '{args.report_yml!s}' does not exist")
if args.wsa_id is None:
if args.report_yml.suffix.lower() in ("yml", "yaml"):
filename = args.report_yml.stem
else:
filename = args.report_yml.name
args.wsa_id = determine_wsa_id(filename)
if args.wsa_id is None:
raise SystemExit(f"Could not determine WSA identifier from '{filename}'")
if not wsa_id_re.match(args.wsa_id):
raise SystemExit(f"Invalid WSA identifier format '{args.wsa_id}'")
with args.report_yml.open("r") as f:
wsa_data = YAML(typ="safe").load(f)
Fmt = Mail if args.email else Markdown
Fmt(args.wsa_id, wsa_data).generate(sys.stdout.write if args.output is None
else args.output.open("w").write,
cve_data_path=get_cve_data_path(args.cve_data),
fill_missing_description=args.fill)
def cmd_fill(args):
if len(args.report_yml) > 1 and not args.inplace:
raise SystemExit("Option --inplace needs to be used with multiple inputs")
import cve
cve_cache = cve.LRUMemDiskFetcherCache(get_cve_data_path(args.cve_data))
yaml = YAML()
yaml.indent(mapping=2, sequence=4, offset=2)
for report_yml in args.report_yml:
with report_yml.open("r") as f:
wsa_data = YAML(typ="safe").load(f)
modified = False
for cve_id, cve_data in wsa_data.items():
if cve_data is None:
wsa_data[cve_id] = cve_data = {}
if "description" not in cve_data:
cve = cve_cache.get(cve_id)
if cve and cve.description:
cve_data["description"] = cve.description
modified = True
if args.inplace:
if modified:
with report_yml.open("w") as f:
yaml.dump(wsa_data, f)
else:
yaml.dump(wsa_data, sys.stdout)
def cmd_check(args):
from httpcache import HTTPCache
from urllib.error import HTTPError
from lxml.cssselect import CSSSelector
from lxml import html
apple_support_url_re = re.compile(r"^https://support.apple.com/(en-us/|kb/HT)\d+$")
advisory_bugzilla_re = re.compile(r"^WebKit\s+Bugzilla:\s*(\d+)\s*$")
advisory_cve_author_re = re.compile(r"^(CVE-\d+-\d+):\s*(.*)$")
advisory_cve_re = re.compile(r"^(CVE-\d+-\d+)$")
advisory_description_re = re.compile(r"^[Dd]escription:\s*(.*)$")
advisory_impact_re = re.compile(r"^[Ii]mpact:\s*(.*)$")
webkit_boundary_re = re.compile(r"\bWebKit\b", re.IGNORECASE)
select_advisory_headers = CSSSelector("div#sections > h3")
select_advisory_links = CSSSelector("table > tbody > tr > td > p > a")
select_index_links = CSSSelector("ul > li > p > a")
select_paragraphs = CSSSelector("p")
if not args.cache:
args.cache = Path(__file__).parent / "httpcache"
if not args.report_yml:
from itertools import chain
cvedata_path = Path(__file__).parent / "cvedata"
args.report_yml = chain(cvedata_path.glob("*.yml"), cvedata_path.glob("*.yaml"))
if args.cache.exists() and not args.cache.is_dir():
raise SystemExit(f"Path {args.cache!r} is not a directory")
args.cache.mkdir(parents=True, exist_ok=True)
cache = HTTPCache(args.cache)
reported_cves = set()
for wsa_yaml_path in args.report_yml:
with open(wsa_yaml_path, "r") as f:
wsa_data = YAML(typ="safe").load(f)
reported_cves.update(wsa_data.keys())
print("CVEs already in WSAs:", len(reported_cves), file=sys.stderr)
indexes = {"https://support.apple.com/en-us/HT201222"}
visited = set()
cves = {}
def webkit_cve_entries(url):
entries = set()
entry = None
try:
entry, _ = cache.get(url)
except HTTPError as e:
if e.code == 404:
print("\x1b[2KNot found:", url, "-", e, file=sys.stderr)
return entries
print("\x1b[2K*", url, "- visited:", len(visited),
"- pending:", len(indexes), "- CVEs:", len(cves), end="\r",
file=sys.stderr)
assert entry is not None
tree = html.fromstring(cache.read_blob(entry))
for header in select_advisory_headers(tree):
if header.text is None or not webkit_boundary_re.match(header.text):
continue
items = []
current = header.getnext()
while current is not None and current.tag != header.tag:
if current.tag == "p":
items.append(current.text)
else:
items.extend((p.text for p in select_paragraphs(current)))
current = current.getnext()
cve_id = None
author = None
bugzilla = None
impact = None
description = None
for text in items:
if not text:
continue
text = text.strip()
m = advisory_cve_author_re.match(text)
if m:
cve_id, author = m[1], m[2]
continue
m = advisory_cve_re.match(text)
if m:
cve_id = m[1]
continue
m = advisory_description_re.match(text)
if m:
description = m[1]
continue
m = advisory_impact_re.match(text)
if m:
impact = m[1]
continue
m = advisory_bugzilla_re.match(text)
if m:
bugzilla = m[1]
continue
if bugzilla and cve_id:
entries.add((cve_id, bugzilla, author, impact, description))
return entries
def fetch_index(url):
entry = None
try:
entry, _ = cache.get(url)
except HTTPError as e:
if e.code == 404:
print("\x1b[2KNot found:", url, "-", e, file=sys.stderr)
return entry
def get_advisory_links(tree):
for item in select_advisory_links(tree):
url = item.get("href")
if apple_support_url_re.match(url) and url not in visited:
yield url
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor(max_workers=10) as exec:
while indexes:
urls = {url for url in indexes if url not in visited}
indexes.clear()
for url, entry in zip(urls, exec.map(fetch_index, urls)):
print("\x1b[2K*", url, "- visited:", len(visited),
"- pending:", len(indexes), "- CVEs:", len(cves), end="\r",
file=sys.stderr)
visited.add(url)
if entry is None:
continue
tree = html.fromstring(cache.read_blob(entry))
tree.make_links_absolute(url)
advisory_urls = set(get_advisory_links(tree))
visited.update(advisory_urls)
for item in select_index_links(tree):
url = item.get("href")
if apple_support_url_re.match(url) and url not in visited:
indexes.add(url)
for entries in exec.map(webkit_cve_entries, advisory_urls):
for cve_id, bugzilla, author, impact, description in entries:
if cve_id in cves:
continue
cves[cve_id] = {"id": cve_id,
"bug": int(bugzilla),
"author": author,
"impact": impact,
"description": description,
"url": url}
print("\x1b[2KFetched CVEs:", len(cves), file=sys.stderr)
known_cves = set(cves.keys())
missing_cves = known_cves - reported_cves
print("Missing CVEs:", len(missing_cves), file=sys.stderr)
yaml = YAML()
yaml.indent(mapping=2, sequence=4, offset=2)
if args.cache_stats:
yaml.dump(dict(cache_stats=asdict(cache.stats)), sys.stderr)
if not missing_cves:
return
from ruamel.yaml.comments import CommentedMap
cve_data = CommentedMap()
for cve_id in sorted(missing_cves):
item = {"bugzilla": cves[cve_id]["bug"]}
for key in ("author", "impact", "description"):
value = cves[cve_id].get(key, None)
if value:
item[key] = value
cve_data[cve_id] = item
cve_data.yaml_add_eol_comment(cves[cve_id]["url"], cve_id, column=0)
yaml.dump(cve_data, sys.stdout)
log_levels = {logging.getLevelName(level).lower(): level
for level in (logging.INFO, logging.DEBUG, logging.ERROR, logging.WARNING, logging.FATAL)}
log_level_default_name = logging.getLevelName(logging.WARNING).lower()
arg_parser = ArgumentParser()
arg_parser.add_argument("--log", choices=log_levels.keys(), default=log_level_default_name,
help=f"set log level (default: {log_level_default_name})")
subparsers = arg_parser.add_subparsers(dest="subcommand", required=True)
gen_parser = subparsers.add_parser("generate", aliases=("gen",), description="Generate advisory text.")
gen_parser.add_argument("-c", "--cve-data", type=Path, default=None, help="path to the CVE data dump")
gen_parser.add_argument("-f", "--fill", action="store_true", default=False, help="fill missing fields from CVE data")
gen_parser.add_argument("-w", "--wsa-id", type=str, help="manually specify generated WSA identifier")
gen_parser.add_argument("-o", "--output", type=Path, default=None, help="output file path, instead of stdout")
gen_parser.add_argument("-m", "--markdown", action="store_true", default=False, help="generate Markdown")
gen_parser.add_argument("-e", "--email", action="store_true", default=False, help="generate e-mail")
gen_parser.add_argument("report_yml", type=Path, help="path to the WSA report YAML source")
fil_parser = subparsers.add_parser("fill", description="Fill missing advisory fields.")
fil_parser.add_argument("-c", "--cve-data", type=Path, default=None, help="path to the CVE data dump")
fil_parser.add_argument("-i", "--inplace", action="store_true", default=False, help="edit YAML file in-place")
fil_parser.add_argument("report_yml", type=Path, nargs="+", help="path to the WSA report YAML source")
chk_parser = subparsers.add_parser("check", description="Check for CVEs in Apple security advisories")
chk_parser.add_argument("-c", "--cache", type=Path, default=None, help="path to directory used as HTTP cache")
chk_parser.add_argument("--cache-stats", action="store_true", default=False, help="Print HTTP cache statistics")
chk_parser.add_argument("report_yml", type=Path, nargs="*", help="path to WSA report YAML source")
args = arg_parser.parse_args()
logging.basicConfig(level=log_levels[args.log])
({
"generate": cmd_generate, "gen": cmd_generate,
"fill": cmd_fill,
"check": cmd_check,
})[args.subcommand](args)