cvescope 0.2.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cvescope/__init__.py +4 -0
- cvescope/__main__.py +3 -0
- cvescope/acks.py +205 -0
- cvescope/cli.py +361 -0
- cvescope/collectors/__init__.py +128 -0
- cvescope/collectors/appliances.py +343 -0
- cvescope/collectors/containers.py +145 -0
- cvescope/collectors/generic.py +125 -0
- cvescope/collectors/nmap.py +178 -0
- cvescope/collectors/packages.py +175 -0
- cvescope/collectors/sbom.py +291 -0
- cvescope/collectors/software.py +327 -0
- cvescope/collectors/webscan.py +123 -0
- cvescope/config.example.toml +199 -0
- cvescope/config.py +117 -0
- cvescope/db.py +232 -0
- cvescope/match.py +602 -0
- cvescope/net.py +206 -0
- cvescope/notify.py +132 -0
- cvescope/query.py +409 -0
- cvescope/report.py +285 -0
- cvescope/server.py +243 -0
- cvescope/sources/__init__.py +63 -0
- cvescope/sources/cvelist.py +283 -0
- cvescope/sources/distro.py +160 -0
- cvescope/sources/epss.py +58 -0
- cvescope/sources/exploits.py +210 -0
- cvescope/sources/freebsd.py +76 -0
- cvescope/sources/kev.py +41 -0
- cvescope/sources/nvd.py +118 -0
- cvescope/sources/osvpkg.py +147 -0
- cvescope/sources/proxmox_psa.py +215 -0
- cvescope/versions.py +424 -0
- cvescope/vulnid.py +38 -0
- cvescope/web/app.js +547 -0
- cvescope/web/favicon.svg +12 -0
- cvescope/web/index.html +96 -0
- cvescope/web/style.css +187 -0
- cvescope-0.2.6.dist-info/METADATA +555 -0
- cvescope-0.2.6.dist-info/RECORD +44 -0
- cvescope-0.2.6.dist-info/WHEEL +5 -0
- cvescope-0.2.6.dist-info/entry_points.txt +2 -0
- cvescope-0.2.6.dist-info/licenses/LICENSE +675 -0
- cvescope-0.2.6.dist-info/top_level.txt +1 -0
cvescope/__init__.py
ADDED
cvescope/__main__.py
ADDED
cvescope/acks.py
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
"""Acknowledgements: "I've dealt with this one" for a CVE on one inventory item.
|
|
2
|
+
|
|
3
|
+
An acknowledged match drops out of act now / watch, the totals and the alerts. It comes back by
|
|
4
|
+
itself when:
|
|
5
|
+
|
|
6
|
+
- its end date passes (the end date is optional), or
|
|
7
|
+
- the evidence gets worse than it was when you acknowledged it: a higher tier, a new kind of
|
|
8
|
+
exploitation evidence (added to KEV, a first Metasploit module, SSVC raised), or a "check"
|
|
9
|
+
that became "affected".
|
|
10
|
+
|
|
11
|
+
More of the same evidence (another GitHub PoC, EPSS moving within its tier) and a new item
|
|
12
|
+
version (monthly Windows builds) don't bring it back.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import datetime as dt
|
|
18
|
+
import json
|
|
19
|
+
|
|
20
|
+
from . import db
|
|
21
|
+
|
|
22
|
+
TIER_RANK = {"act": 0, "watch": 1, "info": 2}
|
|
23
|
+
TIER_LABEL = {"act": "act now", "watch": "watch", "info": "info"}
|
|
24
|
+
KEY_LABEL = {"kev": "added to CISA KEV", "msf": "Metasploit module", "msf:exploit": "Metasploit exploit module",
|
|
25
|
+
"nuclei": "Nuclei template", "edb": "Exploit-DB entry", "poc": "GitHub PoC",
|
|
26
|
+
"local": "in your exploit archive", "ssvc:poc": "CISA SSVC: PoC public",
|
|
27
|
+
"ssvc:active": "CISA SSVC: active exploitation"}
|
|
28
|
+
LIVE = ("affected", "check", "keyword")
|
|
29
|
+
NOTE_MAX = 500
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def evidence_keys(sig: dict, ssvc: str | None) -> list[str]:
|
|
33
|
+
"""The kinds of exploitation evidence a CVE has right now. sig: source -> list of signals."""
|
|
34
|
+
keys = {k for k, v in sig.items() if v}
|
|
35
|
+
if any(m.get("type") == "exploit" for m in sig.get("msf", [])):
|
|
36
|
+
keys.add("msf:exploit")
|
|
37
|
+
if ssvc in ("poc", "active"):
|
|
38
|
+
keys.add(f"ssvc:{ssvc}")
|
|
39
|
+
return sorted(keys)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def load(con) -> dict[tuple[str, str], dict]:
|
|
43
|
+
return {(r["cve_id"], r["item_id"]): dict(r) for r in con.execute("SELECT * FROM ack")}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def state(ack: dict, tier: str, status: str, keys: list[str],
|
|
47
|
+
today: dt.date | None = None) -> tuple[str, str]:
|
|
48
|
+
"""-> ("active" | "expired" | "lapsed", why it's no longer active)."""
|
|
49
|
+
today = today or dt.datetime.now(dt.timezone.utc).date()
|
|
50
|
+
if ack.get("until") and today.isoformat() > ack["until"][:10]:
|
|
51
|
+
return "expired", f"ended {ack['until'][:10]}"
|
|
52
|
+
ev = json.loads(ack.get("evidence") or "{}")
|
|
53
|
+
why = []
|
|
54
|
+
if TIER_RANK.get(tier, 9) < TIER_RANK.get(ev.get("tier"), 9):
|
|
55
|
+
why.append(f"moved up to {TIER_LABEL[tier]}")
|
|
56
|
+
new = [k for k in keys if k not in (ev.get("keys") or [])]
|
|
57
|
+
if "msf:exploit" in new and "msf" in new:
|
|
58
|
+
new.remove("msf")
|
|
59
|
+
why += [KEY_LABEL.get(k, k) for k in new]
|
|
60
|
+
if ev.get("status") == "check" and status == "affected":
|
|
61
|
+
why.append("now confirmed affected")
|
|
62
|
+
return ("lapsed", "; ".join(why)) if why else ("active", "")
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _until(days=None, until=None) -> str | None:
|
|
66
|
+
if until:
|
|
67
|
+
return dt.date.fromisoformat(str(until)[:10]).isoformat() # ValueError on junk
|
|
68
|
+
if days:
|
|
69
|
+
days = int(days)
|
|
70
|
+
if not 1 <= days <= 3650:
|
|
71
|
+
raise ValueError("days must be between 1 and 3650")
|
|
72
|
+
return (dt.datetime.now(dt.timezone.utc).date() + dt.timedelta(days=days)).isoformat()
|
|
73
|
+
return None
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _pick(rows: list[dict], item_ref: str | None, cve_id: str) -> list[dict]:
|
|
77
|
+
"""item_ref: an item id, an item name, or a unique part of a name. None: every row."""
|
|
78
|
+
if not item_ref:
|
|
79
|
+
return rows
|
|
80
|
+
ref = item_ref.strip().lower()
|
|
81
|
+
for test in (lambda r: r["item_id"].lower() == ref, lambda r: (r["item_name"] or "").lower() == ref,
|
|
82
|
+
lambda r: ref in (r["item_name"] or "").lower()):
|
|
83
|
+
hit = [r for r in rows if test(r)]
|
|
84
|
+
if len(hit) == 1:
|
|
85
|
+
return hit
|
|
86
|
+
if len(hit) > 1:
|
|
87
|
+
raise ValueError(f"'{item_ref}' fits several items for {cve_id}: "
|
|
88
|
+
+ ", ".join(r["item_name"] for r in hit))
|
|
89
|
+
raise ValueError(f"no item matching '{item_ref}' for {cve_id}")
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def add(con, cfg, cve_id: str, item_ref: str | None = None, note: str = "",
|
|
93
|
+
days=None, until=None) -> list[dict]:
|
|
94
|
+
"""Acknowledge cve_id on one item (or every item it shows up on). Returns the rows acked."""
|
|
95
|
+
from . import query
|
|
96
|
+
note = (note or "").strip()
|
|
97
|
+
if len(note) > NOTE_MAX:
|
|
98
|
+
raise ValueError(f"the note is over {NOTE_MAX} characters")
|
|
99
|
+
end = _until(days, until)
|
|
100
|
+
rows = query.matches(con, cfg, include_keyword=True, cve_ids=[cve_id], acked="include")
|
|
101
|
+
if not rows:
|
|
102
|
+
raise ValueError(f"{cve_id} doesn't show up for anything in your inventory, so there's nothing to acknowledge")
|
|
103
|
+
rows = _pick(rows, item_ref, cve_id)
|
|
104
|
+
sig = query.signals_for(con, [cve_id]).get(cve_id, {})
|
|
105
|
+
t = db.now()
|
|
106
|
+
for r in rows:
|
|
107
|
+
ev = {"tier": r["tier"], "status": r["status"], "keys": evidence_keys(sig, r.get("ssvc_exploitation")),
|
|
108
|
+
"item_version": r.get("item_version")}
|
|
109
|
+
con.execute("INSERT OR REPLACE INTO ack(cve_id, item_id, created, until, note, evidence) VALUES(?,?,?,?,?,?)",
|
|
110
|
+
(cve_id, r["item_id"], t, end, note, json.dumps(ev)))
|
|
111
|
+
db.add_event(con, cve_id, "ack", {"item": r["item_id"], "note": note, "until": end})
|
|
112
|
+
con.commit()
|
|
113
|
+
return rows
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
BULK_MAX = 2000 # pairs per bulk call
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _pairs(pairs) -> list[tuple[str, str]]:
|
|
120
|
+
from . import vulnid
|
|
121
|
+
out = list(dict.fromkeys((vulnid.canon(c), str(i).strip()) for c, i in pairs))
|
|
122
|
+
if not out:
|
|
123
|
+
raise ValueError("nothing selected")
|
|
124
|
+
if len(out) > BULK_MAX:
|
|
125
|
+
raise ValueError(f"{len(out)} selected; acknowledge at most {BULK_MAX} at a time")
|
|
126
|
+
return out
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def add_many(con, cfg, pairs, note: str = "", days=None, until=None) -> list[dict]:
|
|
130
|
+
"""Acknowledge many (cve_id, item_id) pairs with one note and end date, all or nothing."""
|
|
131
|
+
from . import query
|
|
132
|
+
note = (note or "").strip()
|
|
133
|
+
if len(note) > NOTE_MAX:
|
|
134
|
+
raise ValueError(f"the note is over {NOTE_MAX} characters")
|
|
135
|
+
end = _until(days, until)
|
|
136
|
+
pairs = _pairs(pairs)
|
|
137
|
+
cves = {c for c, _ in pairs}
|
|
138
|
+
live = {(m["cve_id"], m["item_id"]): m for m in query.matches(
|
|
139
|
+
con, cfg, include_keyword=True, cve_ids=cves, acked="include")}
|
|
140
|
+
gone = [f"{c} on {i}" for c, i in pairs if (c, i) not in live]
|
|
141
|
+
if gone:
|
|
142
|
+
raise ValueError(f"{len(gone)} of the {len(pairs)} selected no longer match your inventory "
|
|
143
|
+
f"(first: {gone[0]}). Reload and try again.")
|
|
144
|
+
sigs = query.signals_for(con, cves)
|
|
145
|
+
t = db.now()
|
|
146
|
+
rows = []
|
|
147
|
+
for c, i in pairs:
|
|
148
|
+
r = live[(c, i)]
|
|
149
|
+
ev = {"tier": r["tier"], "status": r["status"], "keys": evidence_keys(sigs.get(c, {}), r.get("ssvc_exploitation")),
|
|
150
|
+
"item_version": r.get("item_version")}
|
|
151
|
+
con.execute("INSERT OR REPLACE INTO ack(cve_id, item_id, created, until, note, evidence) VALUES(?,?,?,?,?,?)",
|
|
152
|
+
(c, i, t, end, note, json.dumps(ev)))
|
|
153
|
+
db.add_event(con, c, "ack", {"item": i, "note": note, "until": end})
|
|
154
|
+
rows.append(r)
|
|
155
|
+
con.commit()
|
|
156
|
+
return rows
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def remove_many(con, pairs) -> int:
|
|
160
|
+
"""Drop the acknowledgements for many (cve_id, item_id) pairs. Returns how many there were."""
|
|
161
|
+
pairs = _pairs(pairs)
|
|
162
|
+
n = 0
|
|
163
|
+
for c, i in pairs:
|
|
164
|
+
if con.execute("DELETE FROM ack WHERE cve_id = ? AND item_id = ?", (c, i)).rowcount:
|
|
165
|
+
db.add_event(con, c, "unack", {"item": i})
|
|
166
|
+
n += 1
|
|
167
|
+
if not n:
|
|
168
|
+
con.rollback()
|
|
169
|
+
raise ValueError("none of the selected are acknowledged")
|
|
170
|
+
con.commit()
|
|
171
|
+
return n
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def remove(con, cve_id: str, item_ref: str | None = None) -> list[str]:
|
|
175
|
+
"""Drop the acknowledgement(s) for cve_id. Returns the item ids it removed."""
|
|
176
|
+
rows = [dict(r) for r in con.execute(
|
|
177
|
+
"SELECT a.item_id, COALESCE(i.name, a.item_id) AS item_name FROM ack a "
|
|
178
|
+
"LEFT JOIN item i ON i.id = a.item_id WHERE a.cve_id = ?", (cve_id,))]
|
|
179
|
+
if not rows:
|
|
180
|
+
raise ValueError(f"{cve_id} isn't acknowledged")
|
|
181
|
+
rows = _pick(rows, item_ref, cve_id)
|
|
182
|
+
for r in rows:
|
|
183
|
+
con.execute("DELETE FROM ack WHERE cve_id = ? AND item_id = ?", (cve_id, r["item_id"]))
|
|
184
|
+
db.add_event(con, cve_id, "unack", {"item": r["item_id"]})
|
|
185
|
+
con.commit()
|
|
186
|
+
return [r["item_id"] for r in rows]
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def list_all(con, cfg) -> list[dict]:
|
|
190
|
+
"""Every acknowledgement with its current state; "resolved" when it no longer matches."""
|
|
191
|
+
from . import query
|
|
192
|
+
acks = load(con)
|
|
193
|
+
if not acks:
|
|
194
|
+
return []
|
|
195
|
+
live = {(m["cve_id"], m["item_id"]): m for m in query.matches(
|
|
196
|
+
con, cfg, include_keyword=True, cve_ids={c for c, _ in acks}, acked="include")}
|
|
197
|
+
names = query.item_names(con)
|
|
198
|
+
out = []
|
|
199
|
+
for (cid, iid), a in sorted(acks.items(), key=lambda kv: kv[1]["created"], reverse=True):
|
|
200
|
+
m = live.get((cid, iid))
|
|
201
|
+
st, why = (m["ack"]["state"], m["ack"]["reason"]) if m else ("resolved", "no longer matches your inventory")
|
|
202
|
+
out.append({"cve_id": cid, "item_id": iid, "item_name": names.get(iid, iid), "created": a["created"],
|
|
203
|
+
"until": a["until"], "note": a["note"], "state": st, "reason": why,
|
|
204
|
+
"tier": m["tier"] if m else None})
|
|
205
|
+
return out
|
cvescope/cli.py
ADDED
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
"""cvescope command line."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import json
|
|
7
|
+
import shutil
|
|
8
|
+
import sys
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
from . import __version__, config, db, vulnid
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _con(cfg):
|
|
15
|
+
return db.connect(cfg.db_path)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def cmd_sync(cfg, a):
|
|
19
|
+
from . import sources
|
|
20
|
+
con = _con(cfg)
|
|
21
|
+
only = a.only.split(",") if a.only else None
|
|
22
|
+
started = db.now()
|
|
23
|
+
# inventory first: the distro source fetches only the releases your hosts run
|
|
24
|
+
if not a.no_inventory and not only:
|
|
25
|
+
cmd_inventory(cfg, a, con=con, quiet=True)
|
|
26
|
+
results = sources.run(con, cfg, only=only, rebaseline=a.rebaseline, verbose=a.verbose)
|
|
27
|
+
if not only or "match" in (only or []):
|
|
28
|
+
from . import match
|
|
29
|
+
print("[match]")
|
|
30
|
+
print(" " + match.run(con, cfg))
|
|
31
|
+
if cfg["nvd"].get("enabled") and not only:
|
|
32
|
+
from .sources import nvd
|
|
33
|
+
print("[nvd]")
|
|
34
|
+
fetched, msg = nvd.enrich(con, cfg)
|
|
35
|
+
print(" " + msg)
|
|
36
|
+
if fetched:
|
|
37
|
+
print(" re-matching with NVD data: " + match.run(con, cfg))
|
|
38
|
+
if not a.no_notify and not only:
|
|
39
|
+
from . import notify
|
|
40
|
+
print("[notify]")
|
|
41
|
+
print(" " + notify.run(con, cfg, dry_run=a.dry_run))
|
|
42
|
+
if not only:
|
|
43
|
+
# the page and `cvescope status` use this to spot a schedule that quietly stopped
|
|
44
|
+
failed_collectors = [r[0] for r in con.execute(
|
|
45
|
+
"SELECT DISTINCT collector FROM item WHERE error IS NOT NULL ORDER BY collector")]
|
|
46
|
+
db.meta_set(con, "last_sync", {
|
|
47
|
+
"started": started, "finished": db.now(), "version": __version__,
|
|
48
|
+
"failed_feeds": sorted(n for n, m in results.items() if m.startswith("ERROR")),
|
|
49
|
+
"failed_collectors": failed_collectors,
|
|
50
|
+
})
|
|
51
|
+
return 0
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def cmd_inventory(cfg, a, con=None, quiet=False):
|
|
55
|
+
from . import collectors
|
|
56
|
+
con = con or _con(cfg)
|
|
57
|
+
if not quiet:
|
|
58
|
+
print("[inventory]")
|
|
59
|
+
else:
|
|
60
|
+
print("[inventory]")
|
|
61
|
+
items = collectors.run(con, cfg, verbose=getattr(a, "verbose", False))
|
|
62
|
+
for it in items:
|
|
63
|
+
ver = it.get("version") or "?"
|
|
64
|
+
err = f" !! {it['error']}" if it.get("error") else ""
|
|
65
|
+
print(f" {it['name']:<40} {ver:<22} {it['collector']}{err}")
|
|
66
|
+
return 0
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def cmd_match(cfg, a):
|
|
70
|
+
from . import match
|
|
71
|
+
con = _con(cfg)
|
|
72
|
+
print(match.run(con, cfg))
|
|
73
|
+
return 0
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def cmd_report(cfg, a):
|
|
77
|
+
from . import query, report
|
|
78
|
+
con = _con(cfg)
|
|
79
|
+
tiers = ["act", "watch", "info"]
|
|
80
|
+
want = tiers[: tiers.index(a.tier) + 1]
|
|
81
|
+
if a.format != "text":
|
|
82
|
+
d = report.build(con, cfg, tiers=want, host=a.host, days=a.days, include_keyword=a.keyword)
|
|
83
|
+
text = report.render(d, a.format)
|
|
84
|
+
if a.out:
|
|
85
|
+
import datetime as dt
|
|
86
|
+
out = Path(dt.datetime.now().strftime(str(Path(a.out).expanduser()))) # ~/reports/cvescope-%Y-%m-%d.html
|
|
87
|
+
out.parent.mkdir(parents=True, exist_ok=True)
|
|
88
|
+
out.write_text(text, encoding="utf-8", newline="")
|
|
89
|
+
c = d["counts"]
|
|
90
|
+
print(f"wrote {out} ({c['act']} act now, {c['watch']} watch, {c['info']} info, {c['acked']} acknowledged)")
|
|
91
|
+
else:
|
|
92
|
+
sys.stdout.buffer.write(text.encode("utf-8"))
|
|
93
|
+
return 0
|
|
94
|
+
rows = query.matches(con, cfg, tiers=want, include_keyword=a.keyword,
|
|
95
|
+
acked="include" if a.acked else "hide")
|
|
96
|
+
if a.host:
|
|
97
|
+
rows = [r for r in rows if (r.get("host") or "").lower() == a.host.strip().lower()]
|
|
98
|
+
if not rows:
|
|
99
|
+
print("nothing at or above tier", a.tier)
|
|
100
|
+
return 0
|
|
101
|
+
cur = None
|
|
102
|
+
for r in rows:
|
|
103
|
+
if r["tier"] != cur:
|
|
104
|
+
cur = r["tier"]
|
|
105
|
+
print(f"\n== {query.TIER_LABEL[cur]} ==")
|
|
106
|
+
flags = " ".join(query.flag_text(r))
|
|
107
|
+
if r.get("ack"):
|
|
108
|
+
flags = ("[acked] " if r["ack"]["state"] == "active" else f"[ack {r['ack']['state']}] ") + flags
|
|
109
|
+
epss = f"epss {r['epss_pct'] * 100:.0f}%" if r.get("epss_pct") is not None else ""
|
|
110
|
+
cvss = f"cvss {r['cvss']}" if r.get("cvss") is not None else ""
|
|
111
|
+
print(f"{r['cve_id']:<18} {r['item_name'][:28]:<28} {r['status']:<9} {cvss:<9} {epss:<9} {flags}")
|
|
112
|
+
if a.long:
|
|
113
|
+
print(" " + (r.get("title") or r.get("description") or "")[:150])
|
|
114
|
+
return 0
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def cmd_ack(cfg, a):
|
|
118
|
+
from . import acks
|
|
119
|
+
con = _con(cfg)
|
|
120
|
+
failed = 0
|
|
121
|
+
for cid in dict.fromkeys(vulnid.canon(c) for c in a.cve):
|
|
122
|
+
try:
|
|
123
|
+
rows = acks.add(con, cfg, cid, a.item, a.note or "", days=a.days, until=a.until)
|
|
124
|
+
except ValueError as e:
|
|
125
|
+
print(f"cvescope ack: {e}", file=sys.stderr)
|
|
126
|
+
failed += 1
|
|
127
|
+
continue
|
|
128
|
+
a_row = acks.load(con)[(cid, rows[0]["item_id"])]
|
|
129
|
+
for r in rows:
|
|
130
|
+
print(f"acknowledged {cid} on {r['item_name']}"
|
|
131
|
+
+ (f" until {a_row['until']}" if a_row["until"] else " (no end date)"))
|
|
132
|
+
if failed < len(set(vulnid.canon(c) for c in a.cve)):
|
|
133
|
+
print("It comes back if the evidence gets worse. `cvescope unack` takes it back.")
|
|
134
|
+
return 1 if failed else 0
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def cmd_unack(cfg, a):
|
|
138
|
+
from . import acks, query
|
|
139
|
+
con = _con(cfg)
|
|
140
|
+
names = query.item_names(con)
|
|
141
|
+
failed = 0
|
|
142
|
+
for cid in dict.fromkeys(vulnid.canon(c) for c in a.cve):
|
|
143
|
+
try:
|
|
144
|
+
removed = acks.remove(con, cid, a.item)
|
|
145
|
+
except ValueError as e:
|
|
146
|
+
print(f"cvescope unack: {e}", file=sys.stderr)
|
|
147
|
+
failed += 1
|
|
148
|
+
continue
|
|
149
|
+
for iid in removed:
|
|
150
|
+
print(f"removed the acknowledgement of {cid} on {names.get(iid, iid)}")
|
|
151
|
+
return 1 if failed else 0
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def cmd_acks(cfg, a):
|
|
155
|
+
from . import acks
|
|
156
|
+
con = _con(cfg)
|
|
157
|
+
rows = acks.list_all(con, cfg)
|
|
158
|
+
if not a.all:
|
|
159
|
+
rows = [r for r in rows if r["state"] != "resolved"]
|
|
160
|
+
if not rows:
|
|
161
|
+
print("nothing acknowledged" + ("" if a.all else " (--all includes resolved ones)"))
|
|
162
|
+
return 0
|
|
163
|
+
for r in rows:
|
|
164
|
+
end = f"until {r['until']}" if r["until"] else "no end date"
|
|
165
|
+
print(f"{r['cve_id']:<18} {r['item_name'][:28]:<28} {r['state']:<8} {r['created'][:10]} {end:<16} {r['note']}")
|
|
166
|
+
if r["reason"]:
|
|
167
|
+
print(f" {'back because' if r['state'] == 'lapsed' else 'note'}: {r['reason']}")
|
|
168
|
+
return 0
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def cmd_show(cfg, a):
|
|
172
|
+
from . import query
|
|
173
|
+
con = _con(cfg)
|
|
174
|
+
d = query.cve_detail(con, cfg, vulnid.canon(a.cve))
|
|
175
|
+
if not d:
|
|
176
|
+
print("not found")
|
|
177
|
+
return 1
|
|
178
|
+
print(json.dumps(d, indent=2, default=str))
|
|
179
|
+
return 0
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def cmd_events(cfg, a):
|
|
183
|
+
from . import query
|
|
184
|
+
con = _con(cfg)
|
|
185
|
+
for e in query.events(con, cfg, days=a.days, matched_only=not a.all):
|
|
186
|
+
print(f"{e['ts'][:16]} {e['kind']:<8} {e['cve_id']:<18} {e.get('summary', '')}")
|
|
187
|
+
return 0
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def cmd_status(cfg, a):
|
|
191
|
+
con = _con(cfg)
|
|
192
|
+
from . import query
|
|
193
|
+
print(f"config: {cfg.path or '(none: defaults)'}")
|
|
194
|
+
print(f"data: {cfg.db_path}")
|
|
195
|
+
h = query.health(con, cfg)
|
|
196
|
+
print(f"last full sync: {h['finished'] or 'never'}"
|
|
197
|
+
+ (f" ({h['age_hours']:.1f} h ago)" if h["age_hours"] is not None else ""))
|
|
198
|
+
for line in h["problems"]:
|
|
199
|
+
print(f" !! {line}")
|
|
200
|
+
for r in con.execute("SELECT name,last_ok,last_try,last_error FROM source ORDER BY name"):
|
|
201
|
+
state = "ok " if not r["last_error"] else "ERR"
|
|
202
|
+
print(f" {state} {r['name']:<22} last ok {r['last_ok'] or 'never':<22} {r['last_error'] or ''}")
|
|
203
|
+
n = con.execute("SELECT count(*) FROM cve").fetchone()[0]
|
|
204
|
+
m = con.execute("SELECT count(*) FROM match").fetchone()[0]
|
|
205
|
+
i = con.execute("SELECT count(*) FROM item").fetchone()[0]
|
|
206
|
+
print(f" {n:,} CVEs, {i} inventory items, {m:,} matches")
|
|
207
|
+
return 0
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def cmd_serve(cfg, a):
|
|
211
|
+
from . import server
|
|
212
|
+
host = cfg["serve"]["host"]
|
|
213
|
+
port = a.port or int(cfg["serve"]["port"])
|
|
214
|
+
server.serve(cfg, host, port)
|
|
215
|
+
return 0
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def cmd_notify(cfg, a):
|
|
219
|
+
from . import notify
|
|
220
|
+
con = _con(cfg)
|
|
221
|
+
if a.test:
|
|
222
|
+
print(notify.test(cfg))
|
|
223
|
+
else:
|
|
224
|
+
print(notify.run(con, cfg, dry_run=a.dry_run))
|
|
225
|
+
return 0
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def cmd_pin(cfg, a):
|
|
229
|
+
from . import net
|
|
230
|
+
host, _, port = a.target.partition(":")
|
|
231
|
+
fp = net.fingerprint(host, int(port or 443))
|
|
232
|
+
print(f'pin_sha256 = "{fp}"')
|
|
233
|
+
print("Compare this with the fingerprint shown in the device's own UI before trusting it.")
|
|
234
|
+
return 0
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def cmd_init(cfg, a):
|
|
238
|
+
src = Path(__file__).resolve().parent / "config.example.toml" # shipped inside the package
|
|
239
|
+
dest = Path(a.path) if a.path else config.user_config_dir() / "config.toml"
|
|
240
|
+
if dest.exists():
|
|
241
|
+
print(f"{dest} already exists; not overwriting")
|
|
242
|
+
return 1
|
|
243
|
+
dest.parent.mkdir(parents=True, exist_ok=True)
|
|
244
|
+
shutil.copy(src, dest)
|
|
245
|
+
print(f"wrote {dest}: add your inventory, then run `cvescope sync`")
|
|
246
|
+
return 0
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def main(argv=None) -> int:
|
|
250
|
+
p = argparse.ArgumentParser(prog="cvescope", description=__doc__)
|
|
251
|
+
p.add_argument("--config", "-c", help="path to config.toml")
|
|
252
|
+
p.add_argument("--log", help="append all output to this file (for cron / Task Scheduler runs)")
|
|
253
|
+
p.add_argument("--version", action="version", version=f"cvescope {__version__}")
|
|
254
|
+
sub = p.add_subparsers(dest="cmd", required=True)
|
|
255
|
+
|
|
256
|
+
s = sub.add_parser("sync", help="pull every feed, refresh inventory, match, notify")
|
|
257
|
+
s.add_argument("--only", help="comma list: cvelist,kev,epss,metasploit,nuclei,exploitdb,github_poc,local_archive,distro,freebsd,osv,proxmox")
|
|
258
|
+
s.add_argument("--rebaseline", action="store_true", help="re-download the full CVE List")
|
|
259
|
+
s.add_argument("--no-inventory", action="store_true")
|
|
260
|
+
s.add_argument("--no-notify", action="store_true")
|
|
261
|
+
s.add_argument("--dry-run", action="store_true", help="print notifications instead of sending")
|
|
262
|
+
s.add_argument("-v", "--verbose", action="store_true")
|
|
263
|
+
s.set_defaults(fn=cmd_sync)
|
|
264
|
+
|
|
265
|
+
s = sub.add_parser("inventory", help="collect versions from your gear")
|
|
266
|
+
s.add_argument("-v", "--verbose", action="store_true")
|
|
267
|
+
s.set_defaults(fn=cmd_inventory)
|
|
268
|
+
sub.add_parser("match", help="re-match CVEs against inventory").set_defaults(fn=cmd_match)
|
|
269
|
+
|
|
270
|
+
s = sub.add_parser("report", help="matched CVEs by tier, or a full report (--format html|md|csv|json)")
|
|
271
|
+
s.add_argument("--tier", choices=["act", "watch", "info"], default="watch")
|
|
272
|
+
s.add_argument("--keyword", action="store_true", help="include keyword-only matches")
|
|
273
|
+
s.add_argument("-l", "--long", action="store_true")
|
|
274
|
+
s.add_argument("--acked", action="store_true", help="include acknowledged matches (text format)")
|
|
275
|
+
s.add_argument("--format", choices=["text", "html", "md", "csv", "json"], default="text",
|
|
276
|
+
help="text (default) lists findings; the others write a full report")
|
|
277
|
+
s.add_argument("--out", help="write the report here; strftime codes work: ~/reports/cvescope-%%Y-%%m-%%d.html")
|
|
278
|
+
s.add_argument("--host", help="only this host (as the page groups it)")
|
|
279
|
+
s.add_argument("--days", type=int, default=7, help="how far back the report's changes go (default 7)")
|
|
280
|
+
s.set_defaults(fn=cmd_report)
|
|
281
|
+
|
|
282
|
+
s = sub.add_parser("ack", help="acknowledge a CVE you've dealt with (hides it until the evidence gets worse)")
|
|
283
|
+
s.add_argument("cve", nargs="+", help="one or more CVE IDs")
|
|
284
|
+
s.add_argument("--item", help="item id, name, or part of a name (default: every item it shows up on)")
|
|
285
|
+
s.add_argument("--note", "-m", help="what you did, e.g. 'applied the opt-in fix'")
|
|
286
|
+
g = s.add_mutually_exclusive_group()
|
|
287
|
+
g.add_argument("--days", type=int, help="bring it back after this many days")
|
|
288
|
+
g.add_argument("--until", help="bring it back after this date (YYYY-MM-DD)")
|
|
289
|
+
s.set_defaults(fn=cmd_ack)
|
|
290
|
+
|
|
291
|
+
s = sub.add_parser("unack", help="take an acknowledgement back")
|
|
292
|
+
s.add_argument("cve", nargs="+", help="one or more CVE IDs")
|
|
293
|
+
s.add_argument("--item", help="item id, name, or part of a name (default: all of them)")
|
|
294
|
+
s.set_defaults(fn=cmd_unack)
|
|
295
|
+
|
|
296
|
+
s = sub.add_parser("acks", help="list acknowledgements and whether they still hold")
|
|
297
|
+
s.add_argument("--all", action="store_true", help="include ones that no longer match anything")
|
|
298
|
+
s.set_defaults(fn=cmd_acks)
|
|
299
|
+
|
|
300
|
+
s = sub.add_parser("show", help="everything known about one CVE")
|
|
301
|
+
s.add_argument("cve")
|
|
302
|
+
s.set_defaults(fn=cmd_show)
|
|
303
|
+
|
|
304
|
+
s = sub.add_parser("events", help="recent changes")
|
|
305
|
+
s.add_argument("--days", type=int, default=7)
|
|
306
|
+
s.add_argument("--all", action="store_true", help="include CVEs you don't run")
|
|
307
|
+
s.set_defaults(fn=cmd_events)
|
|
308
|
+
|
|
309
|
+
sub.add_parser("status", help="feed health").set_defaults(fn=cmd_status)
|
|
310
|
+
|
|
311
|
+
s = sub.add_parser("serve", help="local web page")
|
|
312
|
+
s.add_argument("--port", type=int)
|
|
313
|
+
s.set_defaults(fn=cmd_serve)
|
|
314
|
+
|
|
315
|
+
s = sub.add_parser("notify", help="send pending alerts")
|
|
316
|
+
s.add_argument("--test", action="store_true", help="send one test message to every channel")
|
|
317
|
+
s.add_argument("--dry-run", action="store_true")
|
|
318
|
+
s.set_defaults(fn=cmd_notify)
|
|
319
|
+
|
|
320
|
+
s = sub.add_parser("pin", help="print a host's certificate SHA-256 for pin_sha256")
|
|
321
|
+
s.add_argument("target", help="host[:port]")
|
|
322
|
+
s.set_defaults(fn=cmd_pin)
|
|
323
|
+
|
|
324
|
+
s = sub.add_parser("init", help="write a starter config")
|
|
325
|
+
s.add_argument("path", nargs="?")
|
|
326
|
+
s.set_defaults(fn=cmd_init)
|
|
327
|
+
|
|
328
|
+
a = p.parse_args(argv)
|
|
329
|
+
log = _open_log(a.log) if a.log else None
|
|
330
|
+
if log:
|
|
331
|
+
# background runs (pythonw, cron) have no console: everything goes to the file
|
|
332
|
+
sys.stdout = sys.stderr = log
|
|
333
|
+
print(f"\n===== cvescope {__version__} {a.cmd} {db.now()} =====", flush=True)
|
|
334
|
+
elif hasattr(sys.stdout, "reconfigure"):
|
|
335
|
+
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
336
|
+
try:
|
|
337
|
+
cfg = config.load(a.config)
|
|
338
|
+
return a.fn(cfg, a)
|
|
339
|
+
except KeyboardInterrupt:
|
|
340
|
+
return 130
|
|
341
|
+
except Exception:
|
|
342
|
+
if not log:
|
|
343
|
+
raise
|
|
344
|
+
import traceback
|
|
345
|
+
traceback.print_exc()
|
|
346
|
+
return 1
|
|
347
|
+
finally:
|
|
348
|
+
if log:
|
|
349
|
+
log.flush()
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
def _open_log(path: str):
|
|
353
|
+
"""Append-mode log file; rotates to <name>.1 once it passes 5 MB."""
|
|
354
|
+
p = config.expand(path)
|
|
355
|
+
p.parent.mkdir(parents=True, exist_ok=True)
|
|
356
|
+
if p.exists() and p.stat().st_size > 5_000_000:
|
|
357
|
+
old = p.with_name(p.name + ".1")
|
|
358
|
+
if old.exists():
|
|
359
|
+
old.unlink()
|
|
360
|
+
p.rename(old)
|
|
361
|
+
return open(p, "a", encoding="utf-8", errors="replace", buffering=1)
|