package2cve 1.0.0__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.
- package2cve/Package2Cve.py +245 -0
- package2cve/__init__.py +2 -0
- package2cve/cli.py +89 -0
- package2cve/remote.py +120 -0
- package2cve-1.0.0.dist-info/METADATA +126 -0
- package2cve-1.0.0.dist-info/RECORD +10 -0
- package2cve-1.0.0.dist-info/WHEEL +5 -0
- package2cve-1.0.0.dist-info/entry_points.txt +2 -0
- package2cve-1.0.0.dist-info/licenses/LICENSE.txt +17 -0
- package2cve-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
"""Match Debian packages against the Debian Security Tracker.
|
|
3
|
+
|
|
4
|
+
The tracker JSON (https://security-tracker.debian.org/tracker/data/json) is
|
|
5
|
+
keyed by *source* package name, and every fixed version in it is a *source*
|
|
6
|
+
version. Installed binary packages are therefore resolved to their source
|
|
7
|
+
package/version before being compared.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import json
|
|
13
|
+
from typing import Any, Dict, List, Optional, Tuple
|
|
14
|
+
|
|
15
|
+
import requests
|
|
16
|
+
from debian.debian_support import version_compare
|
|
17
|
+
|
|
18
|
+
from package2cve.remote import Executor, executor_for
|
|
19
|
+
|
|
20
|
+
TRACKER_URL = "https://security-tracker.debian.org/tracker/data/json"
|
|
21
|
+
|
|
22
|
+
# Numeric release -> codename. Only used to translate "12" -> "bookworm" or
|
|
23
|
+
# VERSION_ID from /etc/os-release; the set of *valid* releases is taken from
|
|
24
|
+
# the tracker data itself, so new releases work without touching this table.
|
|
25
|
+
DEBIAN_CODENAMES = {
|
|
26
|
+
7: "wheezy",
|
|
27
|
+
8: "jessie",
|
|
28
|
+
9: "stretch",
|
|
29
|
+
10: "buster",
|
|
30
|
+
11: "bullseye",
|
|
31
|
+
12: "bookworm",
|
|
32
|
+
13: "trixie",
|
|
33
|
+
14: "forky",
|
|
34
|
+
15: "duke",
|
|
35
|
+
"unstable": "sid",
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
# dpkg-query is stable across Debian releases (unlike `apt list`, which warns
|
|
39
|
+
# that its CLI is not stable) and gives us the Source field we need.
|
|
40
|
+
DPKG_QUERY_CMD = (
|
|
41
|
+
"dpkg-query -W -f='${Status}\\t${Package}\\t${Version}\\t${Source}\\n'"
|
|
42
|
+
)
|
|
43
|
+
OS_RELEASE_CMD = "cat /etc/os-release"
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class Package2CveError(Exception):
|
|
47
|
+
pass
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class Package2Cve:
|
|
51
|
+
|
|
52
|
+
def __init__(self, data: Optional[Dict[str, Any]] = None,
|
|
53
|
+
tracker_file: Optional[str] = None,
|
|
54
|
+
url: str = TRACKER_URL, timeout: int = 300):
|
|
55
|
+
if data is not None:
|
|
56
|
+
self.data = data
|
|
57
|
+
elif tracker_file:
|
|
58
|
+
with open(tracker_file, encoding="utf-8") as fh:
|
|
59
|
+
self.data = json.load(fh)
|
|
60
|
+
else:
|
|
61
|
+
response = requests.get(url, timeout=timeout)
|
|
62
|
+
response.raise_for_status()
|
|
63
|
+
self.data = response.json()
|
|
64
|
+
self.releases = sorted({
|
|
65
|
+
release
|
|
66
|
+
for cves in self.data.values()
|
|
67
|
+
for cve in cves.values()
|
|
68
|
+
for release in cve.get("releases", {})
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
# ------------------------------------------------------------------ #
|
|
72
|
+
# Release handling
|
|
73
|
+
# ------------------------------------------------------------------ #
|
|
74
|
+
def normalize_release(self, release: Any) -> str:
|
|
75
|
+
"""Accept 'trixie', 13, '13' or 'unstable' and return the codename."""
|
|
76
|
+
value = str(release).strip().lower()
|
|
77
|
+
if value.isdigit():
|
|
78
|
+
value = DEBIAN_CODENAMES.get(int(value), value)
|
|
79
|
+
elif value in DEBIAN_CODENAMES:
|
|
80
|
+
value = DEBIAN_CODENAMES[value]
|
|
81
|
+
if value not in self.releases:
|
|
82
|
+
raise Package2CveError(
|
|
83
|
+
"Debian release '{0}' is not tracked by the security tracker "
|
|
84
|
+
"(tracked: {1})".format(release, ", ".join(self.releases)))
|
|
85
|
+
return value
|
|
86
|
+
|
|
87
|
+
# ------------------------------------------------------------------ #
|
|
88
|
+
# Single package
|
|
89
|
+
# ------------------------------------------------------------------ #
|
|
90
|
+
def package_cve(self, os: Any, package: str, version: str,
|
|
91
|
+
include_unfixed: bool = False) -> Optional[Dict[str, Any]]:
|
|
92
|
+
"""Return the CVEs affecting a *source* package at a given version.
|
|
93
|
+
|
|
94
|
+
:param os: Debian codename or number (``"trixie"``, ``13``...).
|
|
95
|
+
:param package: source package name (``openssl``, not ``libssl3``).
|
|
96
|
+
:param version: installed source version.
|
|
97
|
+
:param include_unfixed: also report CVEs without a fix released yet.
|
|
98
|
+
:return: dict with the findings, or ``None`` if nothing applies.
|
|
99
|
+
"""
|
|
100
|
+
release = self.normalize_release(os)
|
|
101
|
+
entries = self.data.get(package)
|
|
102
|
+
if not entries:
|
|
103
|
+
return None
|
|
104
|
+
|
|
105
|
+
cves = []
|
|
106
|
+
latest_available = None
|
|
107
|
+
fixed_version = None
|
|
108
|
+
for cve_id, cve in entries.items():
|
|
109
|
+
info = cve.get("releases", {}).get(release)
|
|
110
|
+
if not info:
|
|
111
|
+
continue
|
|
112
|
+
for repo_version in info.get("repositories", {}).values():
|
|
113
|
+
latest_available = _max_version(latest_available, repo_version)
|
|
114
|
+
if cve_id.startswith("TEMP-"):
|
|
115
|
+
continue
|
|
116
|
+
|
|
117
|
+
fixed = info.get("fixed_version")
|
|
118
|
+
if info.get("status") == "resolved" and fixed:
|
|
119
|
+
# fixed_version "0" means the release was never affected.
|
|
120
|
+
if fixed == "0" or version_compare(version, fixed) >= 0:
|
|
121
|
+
continue
|
|
122
|
+
fixed_version = _max_version(fixed_version, fixed)
|
|
123
|
+
elif not include_unfixed:
|
|
124
|
+
continue
|
|
125
|
+
else:
|
|
126
|
+
fixed = None
|
|
127
|
+
|
|
128
|
+
cves.append({
|
|
129
|
+
"cve": cve_id,
|
|
130
|
+
"status": info.get("status"),
|
|
131
|
+
"fixed_version": fixed,
|
|
132
|
+
"urgency": info.get("urgency"),
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
if not cves:
|
|
136
|
+
return None
|
|
137
|
+
return {
|
|
138
|
+
"package_name": package,
|
|
139
|
+
"release": release,
|
|
140
|
+
"current_version": version,
|
|
141
|
+
"fixed_version": fixed_version,
|
|
142
|
+
"latest_available": latest_available,
|
|
143
|
+
"cves": sorted(cves, key=lambda c: c["cve"]),
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
# ------------------------------------------------------------------ #
|
|
147
|
+
# Hosts
|
|
148
|
+
# ------------------------------------------------------------------ #
|
|
149
|
+
def host_packages_cve(self, executor: Executor,
|
|
150
|
+
release: Optional[str] = None,
|
|
151
|
+
include_unfixed: bool = False) -> List[Dict[str, Any]]:
|
|
152
|
+
"""Scan the host behind ``executor`` and return its vulnerable packages."""
|
|
153
|
+
if release is None:
|
|
154
|
+
release = detect_release(executor.run(OS_RELEASE_CMD))
|
|
155
|
+
release = self.normalize_release(release)
|
|
156
|
+
|
|
157
|
+
installed = parse_dpkg_query(executor.run(DPKG_QUERY_CMD))
|
|
158
|
+
results = []
|
|
159
|
+
for (source, source_version), binaries in sorted(installed.items()):
|
|
160
|
+
found = self.package_cve(release, source, source_version,
|
|
161
|
+
include_unfixed=include_unfixed)
|
|
162
|
+
if not found:
|
|
163
|
+
continue
|
|
164
|
+
found["binary_packages"] = sorted(binaries)
|
|
165
|
+
results.append(found)
|
|
166
|
+
return results
|
|
167
|
+
|
|
168
|
+
def remote_host_packages_cve(self, hostname: str, user: Optional[str] = None,
|
|
169
|
+
password: Optional[str] = None,
|
|
170
|
+
transport: str = "ssh",
|
|
171
|
+
release: Optional[str] = None,
|
|
172
|
+
include_unfixed: bool = False,
|
|
173
|
+
**transport_options: Any) -> List[Dict[str, Any]]:
|
|
174
|
+
"""Connect to ``hostname`` over SSH or Teleport and scan it.
|
|
175
|
+
|
|
176
|
+
``transport`` is ``"ssh"`` (default), ``"teleport"`` or ``"local"``.
|
|
177
|
+
Extra keyword arguments go to the transport (``port``,
|
|
178
|
+
``key_filename``, ``proxy``, ``cluster``, ``tsh_bin``...).
|
|
179
|
+
"""
|
|
180
|
+
with executor_for(transport, hostname=hostname, user=user,
|
|
181
|
+
password=password, **transport_options) as executor:
|
|
182
|
+
return self.host_packages_cve(executor, release=release,
|
|
183
|
+
include_unfixed=include_unfixed)
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
# ---------------------------------------------------------------------- #
|
|
187
|
+
# Helpers
|
|
188
|
+
# ---------------------------------------------------------------------- #
|
|
189
|
+
def _max_version(current: Optional[str], candidate: str) -> str:
|
|
190
|
+
if current is None or version_compare(candidate, current) > 0:
|
|
191
|
+
return candidate
|
|
192
|
+
return current
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def parse_os_release(text: str) -> Dict[str, str]:
|
|
196
|
+
values = {}
|
|
197
|
+
for line in text.splitlines():
|
|
198
|
+
line = line.strip()
|
|
199
|
+
if not line or line.startswith("#") or "=" not in line:
|
|
200
|
+
continue
|
|
201
|
+
key, value = line.split("=", 1)
|
|
202
|
+
values[key] = value.strip().strip('"').strip("'")
|
|
203
|
+
return values
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def detect_release(os_release_text: str) -> str:
|
|
207
|
+
values = parse_os_release(os_release_text)
|
|
208
|
+
distro = values.get("ID", "")
|
|
209
|
+
if distro != "debian":
|
|
210
|
+
raise Package2CveError(
|
|
211
|
+
"Host is not Debian (ID={0!r}); the Debian security tracker "
|
|
212
|
+
"only covers Debian".format(distro))
|
|
213
|
+
codename = values.get("VERSION_CODENAME")
|
|
214
|
+
if codename:
|
|
215
|
+
return codename
|
|
216
|
+
version_id = values.get("VERSION_ID", "")
|
|
217
|
+
if version_id.isdigit() and int(version_id) in DEBIAN_CODENAMES:
|
|
218
|
+
return DEBIAN_CODENAMES[int(version_id)]
|
|
219
|
+
raise Package2CveError("Could not determine the Debian release of the host")
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def parse_dpkg_query(text: str) -> Dict[Tuple[str, str], List[str]]:
|
|
223
|
+
"""Group installed binary packages by (source package, source version)."""
|
|
224
|
+
installed: Dict[Tuple[str, str], List[str]] = {}
|
|
225
|
+
for line in text.splitlines():
|
|
226
|
+
parts = line.split("\t")
|
|
227
|
+
if len(parts) < 4 or not parts[0].endswith(" installed"):
|
|
228
|
+
continue
|
|
229
|
+
status, package, version, source = parts[:4]
|
|
230
|
+
source = source.strip()
|
|
231
|
+
if not source:
|
|
232
|
+
source_name, source_version = package, version
|
|
233
|
+
elif "(" in source:
|
|
234
|
+
# "openssl (3.0.11-1~deb12u2)" when the source version differs
|
|
235
|
+
source_name, _, rest = source.partition(" (")
|
|
236
|
+
source_version = rest.rstrip(")")
|
|
237
|
+
else:
|
|
238
|
+
source_name, source_version = source, version
|
|
239
|
+
if source_name.startswith("linux-signed-"):
|
|
240
|
+
# Signed kernel images (linux-signed-amd64 "6.1.55+1") are not in
|
|
241
|
+
# the tracker; the binary version is the version of source "linux".
|
|
242
|
+
source_name, source_version = "linux", version
|
|
243
|
+
installed.setdefault((source_name, source_version), []).append(package)
|
|
244
|
+
return installed
|
|
245
|
+
|
package2cve/__init__.py
ADDED
package2cve/cli.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"""Command line interface: ``package2cve host|package|local ...``."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import getpass
|
|
7
|
+
import json
|
|
8
|
+
import sys
|
|
9
|
+
|
|
10
|
+
from package2cve.Package2Cve import Package2Cve
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
14
|
+
parser = argparse.ArgumentParser(
|
|
15
|
+
prog="package2cve",
|
|
16
|
+
description="Find CVEs in Debian packages using the Debian Security Tracker")
|
|
17
|
+
parser.add_argument("--tracker-file",
|
|
18
|
+
help="use a local copy of the tracker JSON instead of downloading it")
|
|
19
|
+
parser.add_argument("--include-unfixed", action="store_true",
|
|
20
|
+
help="also report CVEs that have no fixed version yet")
|
|
21
|
+
sub = parser.add_subparsers(dest="command", required=True)
|
|
22
|
+
|
|
23
|
+
host = sub.add_parser("host", help="scan the packages installed on a remote host")
|
|
24
|
+
host.add_argument("hostname")
|
|
25
|
+
host.add_argument("-u", "--user", help="SSH user / Teleport node login")
|
|
26
|
+
host.add_argument("--teleport", action="store_true",
|
|
27
|
+
help="connect with 'tsh ssh' instead of SSH (needs 'tsh login')")
|
|
28
|
+
host.add_argument("--proxy", help="Teleport proxy (tsh --proxy)")
|
|
29
|
+
host.add_argument("--cluster", help="Teleport cluster (tsh --cluster)")
|
|
30
|
+
host.add_argument("-p", "--port", type=int, default=22, help="SSH port")
|
|
31
|
+
host.add_argument("-i", "--identity", help="SSH private key file")
|
|
32
|
+
host.add_argument("--ask-password", action="store_true",
|
|
33
|
+
help="prompt for the SSH password")
|
|
34
|
+
host.add_argument("--release",
|
|
35
|
+
help="Debian release of the host (autodetected if omitted)")
|
|
36
|
+
|
|
37
|
+
local = sub.add_parser("local", help="scan the packages installed on this machine")
|
|
38
|
+
local.add_argument("--release",
|
|
39
|
+
help="Debian release (autodetected if omitted)")
|
|
40
|
+
|
|
41
|
+
pkg = sub.add_parser("package", help="check a single source package version")
|
|
42
|
+
pkg.add_argument("release", help="codename or number, e.g. trixie or 13")
|
|
43
|
+
pkg.add_argument("package", help="source package name, e.g. openssl")
|
|
44
|
+
pkg.add_argument("version", help="source package version")
|
|
45
|
+
return parser
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def main(argv=None) -> int:
|
|
49
|
+
args = build_parser().parse_args(argv)
|
|
50
|
+
try:
|
|
51
|
+
if args.command == "host" and args.teleport:
|
|
52
|
+
if args.ask_password or args.identity or args.port != 22:
|
|
53
|
+
raise SystemExit("--ask-password, --identity and --port are SSH-only options")
|
|
54
|
+
if args.command == "host" and not args.teleport and (args.proxy or args.cluster):
|
|
55
|
+
raise SystemExit("--proxy and --cluster need --teleport")
|
|
56
|
+
|
|
57
|
+
p2c = Package2Cve(tracker_file=args.tracker_file)
|
|
58
|
+
|
|
59
|
+
if args.command == "package":
|
|
60
|
+
result = p2c.package_cve(args.release, args.package, args.version,
|
|
61
|
+
include_unfixed=args.include_unfixed)
|
|
62
|
+
elif args.command == "local":
|
|
63
|
+
result = p2c.remote_host_packages_cve(
|
|
64
|
+
None, transport="local", release=args.release,
|
|
65
|
+
include_unfixed=args.include_unfixed)
|
|
66
|
+
else:
|
|
67
|
+
if args.teleport:
|
|
68
|
+
options = {"proxy": args.proxy, "cluster": args.cluster}
|
|
69
|
+
transport = "teleport"
|
|
70
|
+
password = None
|
|
71
|
+
else:
|
|
72
|
+
options = {"port": args.port, "key_filename": args.identity}
|
|
73
|
+
transport = "ssh"
|
|
74
|
+
password = getpass.getpass("SSH password: ") if args.ask_password else None
|
|
75
|
+
result = p2c.remote_host_packages_cve(
|
|
76
|
+
args.hostname, user=args.user, password=password,
|
|
77
|
+
transport=transport, release=args.release,
|
|
78
|
+
include_unfixed=args.include_unfixed, **options)
|
|
79
|
+
except Exception as error: # paramiko, requests, tsh... -> clean message
|
|
80
|
+
print("error: {0}".format(error), file=sys.stderr)
|
|
81
|
+
return 2
|
|
82
|
+
|
|
83
|
+
json.dump(result, sys.stdout, indent=2)
|
|
84
|
+
sys.stdout.write("\n")
|
|
85
|
+
return 1 if result else 0
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
if __name__ == "__main__":
|
|
89
|
+
sys.exit(main())
|
package2cve/remote.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"""Ways of running a command on the host being scanned."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import shutil
|
|
6
|
+
import subprocess
|
|
7
|
+
from typing import List, Optional
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class RemoteCommandError(Exception):
|
|
11
|
+
pass
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class Executor:
|
|
15
|
+
"""Runs a shell command on the target host and returns its stdout."""
|
|
16
|
+
|
|
17
|
+
def run(self, command: str) -> str:
|
|
18
|
+
raise NotImplementedError
|
|
19
|
+
|
|
20
|
+
def close(self) -> None:
|
|
21
|
+
pass
|
|
22
|
+
|
|
23
|
+
def __enter__(self) -> "Executor":
|
|
24
|
+
return self
|
|
25
|
+
|
|
26
|
+
def __exit__(self, *exc) -> None:
|
|
27
|
+
self.close()
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _check(cmd_desc: str, returncode: int, stdout: str, stderr: str) -> str:
|
|
31
|
+
if returncode != 0:
|
|
32
|
+
raise RemoteCommandError("{0} failed (exit {1}): {2}".format(
|
|
33
|
+
cmd_desc, returncode, stderr.strip() or stdout.strip()))
|
|
34
|
+
return stdout
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class LocalExecutor(Executor):
|
|
38
|
+
"""Scan the machine package2cve is running on."""
|
|
39
|
+
|
|
40
|
+
def run(self, command: str) -> str:
|
|
41
|
+
proc = subprocess.run(["sh", "-c", command], capture_output=True,
|
|
42
|
+
text=True)
|
|
43
|
+
return _check(command, proc.returncode, proc.stdout, proc.stderr)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class SSHExecutor(Executor):
|
|
47
|
+
"""Plain SSH through paramiko (password, key file or ssh-agent)."""
|
|
48
|
+
|
|
49
|
+
def __init__(self, hostname: str, user: Optional[str] = None,
|
|
50
|
+
password: Optional[str] = None, port: int = 22,
|
|
51
|
+
key_filename: Optional[str] = None, timeout: int = 60):
|
|
52
|
+
import paramiko # only needed for this transport
|
|
53
|
+
|
|
54
|
+
self.client = paramiko.SSHClient()
|
|
55
|
+
self.client.load_system_host_keys()
|
|
56
|
+
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
57
|
+
self.client.connect(hostname, port=port, username=user,
|
|
58
|
+
password=password or None,
|
|
59
|
+
key_filename=key_filename, timeout=timeout)
|
|
60
|
+
|
|
61
|
+
def run(self, command: str) -> str:
|
|
62
|
+
_stdin, stdout, stderr = self.client.exec_command(command)
|
|
63
|
+
out = stdout.read().decode("utf-8", "replace")
|
|
64
|
+
err = stderr.read().decode("utf-8", "replace")
|
|
65
|
+
return _check(command, stdout.channel.recv_exit_status(), out, err)
|
|
66
|
+
|
|
67
|
+
def close(self) -> None:
|
|
68
|
+
self.client.close()
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class TeleportExecutor(Executor):
|
|
72
|
+
"""Run commands through ``tsh ssh``.
|
|
73
|
+
|
|
74
|
+
Requires a valid ``tsh login`` session. ``user`` is the node login
|
|
75
|
+
(e.g. ``root``); ``proxy`` and ``cluster`` map to the tsh flags of the
|
|
76
|
+
same name.
|
|
77
|
+
"""
|
|
78
|
+
|
|
79
|
+
def __init__(self, hostname: str, user: Optional[str] = None,
|
|
80
|
+
proxy: Optional[str] = None, cluster: Optional[str] = None,
|
|
81
|
+
tsh_bin: str = "tsh", timeout: int = 300):
|
|
82
|
+
self.tsh = shutil.which(tsh_bin)
|
|
83
|
+
if not self.tsh:
|
|
84
|
+
raise RemoteCommandError(
|
|
85
|
+
"'{0}' not found in PATH; install Teleport's tsh".format(tsh_bin))
|
|
86
|
+
self.target = "{0}@{1}".format(user, hostname) if user else hostname
|
|
87
|
+
self.flags: List[str] = []
|
|
88
|
+
if proxy:
|
|
89
|
+
self.flags.append("--proxy={0}".format(proxy))
|
|
90
|
+
if cluster:
|
|
91
|
+
self.flags.append("--cluster={0}".format(cluster))
|
|
92
|
+
self.timeout = timeout
|
|
93
|
+
|
|
94
|
+
def run(self, command: str) -> str:
|
|
95
|
+
args = [self.tsh, "ssh", *self.flags, self.target, command]
|
|
96
|
+
try:
|
|
97
|
+
proc = subprocess.run(args, capture_output=True, text=True,
|
|
98
|
+
stdin=subprocess.DEVNULL,
|
|
99
|
+
timeout=self.timeout)
|
|
100
|
+
except subprocess.TimeoutExpired:
|
|
101
|
+
raise RemoteCommandError("tsh ssh {0} timed out".format(self.target))
|
|
102
|
+
return _check("tsh ssh {0}".format(self.target), proc.returncode,
|
|
103
|
+
proc.stdout, proc.stderr)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def executor_for(transport: str, hostname: Optional[str] = None,
|
|
107
|
+
user: Optional[str] = None, password: Optional[str] = None,
|
|
108
|
+
**options) -> Executor:
|
|
109
|
+
transport = transport.lower()
|
|
110
|
+
if transport == "local":
|
|
111
|
+
return LocalExecutor()
|
|
112
|
+
if not hostname:
|
|
113
|
+
raise ValueError("hostname is required for transport '{0}'".format(transport))
|
|
114
|
+
if transport == "ssh":
|
|
115
|
+
return SSHExecutor(hostname, user=user, password=password, **options)
|
|
116
|
+
if transport in ("teleport", "tsh"):
|
|
117
|
+
if password:
|
|
118
|
+
raise ValueError("Teleport does not use passwords; run 'tsh login' first")
|
|
119
|
+
return TeleportExecutor(hostname, user=user, **options)
|
|
120
|
+
raise ValueError("Unknown transport '{0}' (ssh, teleport, local)".format(transport))
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: package2cve
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Give a hostname (SSH or Teleport) to get package CVEs, or pass a Debian package plus version
|
|
5
|
+
Author-email: Fernando Castro <whit3bl0cker@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/fernandocastrovilar/package2cve
|
|
8
|
+
Requires-Python: >=3.9
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE.txt
|
|
11
|
+
Requires-Dist: requests
|
|
12
|
+
Requires-Dist: python-debian
|
|
13
|
+
Requires-Dist: paramiko
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# package2cve
|
|
17
|
+
|
|
18
|
+

|
|
19
|
+
[](LICENSE.txt)
|
|
20
|
+
|
|
21
|
+
## Description
|
|
22
|
+
|
|
23
|
+
`package2cve` checks the packages installed on a Debian host (over SSH, over
|
|
24
|
+
Teleport, or locally) against the [Debian Security Tracker](https://security-tracker.debian.org/tracker/)
|
|
25
|
+
and reports the CVEs that affect them together with the version that fixes them.
|
|
26
|
+
|
|
27
|
+
It works with any release the tracker covers (currently bookworm, trixie,
|
|
28
|
+
forky and sid). New releases are picked up from the tracker data
|
|
29
|
+
automatically.
|
|
30
|
+
|
|
31
|
+
## How it works
|
|
32
|
+
|
|
33
|
+
- Installed packages are read with `dpkg-query` and the release from
|
|
34
|
+
`/etc/os-release` (no `lsb_release` needed).
|
|
35
|
+
- Binary packages are mapped to their **source** package and version
|
|
36
|
+
(`libssl3t64` -> `openssl`), which is what the tracker uses.
|
|
37
|
+
- Versions are compared with Debian semantics (epochs, `~`, `+bN`) via
|
|
38
|
+
`python-debian`.
|
|
39
|
+
- By default only CVEs with a released fix are reported. Use
|
|
40
|
+
`--include-unfixed` / `include_unfixed=True` to also get open ones.
|
|
41
|
+
|
|
42
|
+
## Install
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pip install package2cve
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
For Teleport you need `tsh` in your `PATH` and an active `tsh login`.
|
|
49
|
+
|
|
50
|
+
## CLI
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
# Over SSH (uses ssh-agent / ~/.ssh keys; --ask-password to prompt for one)
|
|
54
|
+
package2cve host myserver.example.com -u admin
|
|
55
|
+
package2cve host myserver.example.com -u admin -i ~/.ssh/id_ed25519 -p 2222
|
|
56
|
+
|
|
57
|
+
# Over Teleport (tsh ssh). -u is the node login
|
|
58
|
+
package2cve host mynode -u root --teleport
|
|
59
|
+
package2cve host mynode -u root --teleport --proxy teleport.example.com:443 --cluster leaf
|
|
60
|
+
|
|
61
|
+
# This machine
|
|
62
|
+
package2cve local
|
|
63
|
+
|
|
64
|
+
# A single source package version
|
|
65
|
+
package2cve package trixie openssl 3.5.7-1~deb13u2
|
|
66
|
+
|
|
67
|
+
# Reuse a downloaded tracker file (it is ~90 MB)
|
|
68
|
+
curl -o tracker.json https://security-tracker.debian.org/tracker/data/json
|
|
69
|
+
package2cve --tracker-file tracker.json host mynode -u root --teleport
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Output is JSON on stdout. Exit code is `0` when nothing is found, `1` when
|
|
73
|
+
vulnerabilities are found and `2` on errors.
|
|
74
|
+
|
|
75
|
+
## Library
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
from package2cve import Package2Cve
|
|
79
|
+
|
|
80
|
+
p2c = Package2Cve() # or Package2Cve(tracker_file="tracker.json")
|
|
81
|
+
|
|
82
|
+
# SSH (default transport)
|
|
83
|
+
p2c.remote_host_packages_cve("myserver", "admin", password=None)
|
|
84
|
+
|
|
85
|
+
# Teleport
|
|
86
|
+
p2c.remote_host_packages_cve("mynode", "root", transport="teleport",
|
|
87
|
+
proxy="teleport.example.com:443")
|
|
88
|
+
|
|
89
|
+
# Single package: release (codename or number), source package, version
|
|
90
|
+
p2c.package_cve("trixie", "openssl", "3.5.7-1~deb13u2")
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Each finding looks like:
|
|
94
|
+
|
|
95
|
+
```json
|
|
96
|
+
{
|
|
97
|
+
"package_name": "openssl",
|
|
98
|
+
"release": "trixie",
|
|
99
|
+
"current_version": "3.5.7-1~deb13u2",
|
|
100
|
+
"fixed_version": "3.5.7-1~deb13u3",
|
|
101
|
+
"latest_available": "3.5.7-1~deb13u3",
|
|
102
|
+
"cves": [
|
|
103
|
+
{"cve": "CVE-2026-35189", "status": "resolved",
|
|
104
|
+
"fixed_version": "3.5.7-1~deb13u3", "urgency": "not yet assigned"}
|
|
105
|
+
],
|
|
106
|
+
"binary_packages": ["libssl3t64", "openssl"]
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
`binary_packages` only appears in host scans. `fixed_version` is the version
|
|
111
|
+
that fixes every reported (fixed) CVE.
|
|
112
|
+
|
|
113
|
+
### Upgrading from 0.x
|
|
114
|
+
|
|
115
|
+
1.0 changes the return values: methods return Python lists/dicts instead of
|
|
116
|
+
JSON strings, errors are raised instead of returned, and the result format
|
|
117
|
+
above replaces the old `cve_list` structure. `package_cve` expects the
|
|
118
|
+
**source** package name.
|
|
119
|
+
|
|
120
|
+
## License
|
|
121
|
+
|
|
122
|
+
MIT. See [LICENSE.txt](LICENSE.txt).
|
|
123
|
+
|
|
124
|
+
## Contact
|
|
125
|
+
|
|
126
|
+
Email: whit3bl0cker@gmail.com · Issues on GitHub.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
package2cve/Package2Cve.py,sha256=A9_goWPqdX8BifFBqgPeY-l02K-PnD5r6gcnUzO928k,9801
|
|
2
|
+
package2cve/__init__.py,sha256=ZyWtnhzs63Su6YxFBr_GXxlNcTgwExPko1RhyQ3yKVo,144
|
|
3
|
+
package2cve/cli.py,sha256=CFyBXJPbiPxiottXdfGvJU6vrIXlcMDJXQLVhUum3JY,4019
|
|
4
|
+
package2cve/remote.py,sha256=Nw2BzzMh8rqXGYQCuL_96QGoS6raPWkbt4jIxOk2ovM,4479
|
|
5
|
+
package2cve-1.0.0.dist-info/licenses/LICENSE.txt,sha256=S1pJL407BwTTXzZK0BQPNM7ZZs57uj1pTSfVZ8Whx30,1061
|
|
6
|
+
package2cve-1.0.0.dist-info/METADATA,sha256=ps2Xa4OKy26zwcWztNNZNaJOQCglJaLuey43KJryJdw,3928
|
|
7
|
+
package2cve-1.0.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
8
|
+
package2cve-1.0.0.dist-info/entry_points.txt,sha256=zuGMRjQkAMmWJnvPC8d0_DDmSHEuVHTsVdaIKxymZIE,53
|
|
9
|
+
package2cve-1.0.0.dist-info/top_level.txt,sha256=ZZ8vJD39Ijl_eTHdaLy8IjpCIujl4P5ileDRHCe3NTs,12
|
|
10
|
+
package2cve-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
Copyright (c) 2018 YOUR NAME
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
The above copyright notice and this permission notice shall be included in all
|
|
10
|
+
copies or substantial portions of the Software.
|
|
11
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
12
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
13
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
14
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
15
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
16
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
17
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
package2cve
|