Repository navigation
Expand file tree
/
Copy pathnode2shell-audit.py
More file actions
executable file
·1596 lines (1426 loc) · 81.1 KB
/
Copy pathnode2shell-audit.py
File metadata and controls
executable file
·1596 lines (1426 loc) · 81.1 KB
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""Read-only artifact audit for the ten CVEs in Node's October 2026 advisory."""
import argparse
import base64
import ctypes
import errno
import gzip
import hashlib
import json
import ntpath
import os
import platform
import re
import secrets
import shlex
import stat
import subprocess
import sys
import zlib
from contextlib import ExitStack
from dataclasses import dataclass
from datetime import datetime, timezone
from pathlib import Path, PureWindowsPath
BROTLI_CVE = "CVE-2026-111497"
# Branch scope is from the October advisory, not inferred from changelog omissions.
CVE_BRANCHES = {
"CVE-2026-105482": (22, 24, 26),
"CVE-2026-105483": (24, 26),
"CVE-2026-106069": (22, 24, 26),
BROTLI_CVE: (22, 24, 26),
"CVE-2026-105481": (22, 24, 26),
"CVE-2026-106072": (22, 24, 26),
"CVE-2026-106074": (22, 24, 26),
"CVE-2026-106070": (26,),
"CVE-2026-106071": (26,),
"CVE-2026-106073": (22, 24, 26),
}
CVES = tuple(CVE_BRANCHES)
BASELINES = {22: (22, 23, 4), 24: (24, 22, 1), 26: (26, 12, 1)}
KINDS = {"node", "libnode", "brotli"}
MODULE_KINDS = {"libnode", "brotli"}
MANAGERS = {"dpkg", "rpm", "apk", "pacman"}
MAX_CONFIG = 1024 * 1024
TOOL_VERSION = "1.0.0"
ADVISORY = "https://nodejs.org/en/blog/vulnerability/october-2026-security-releases"
DEBIAN_BROTLI_ADVISORY = "https://security-tracker.debian.org/tracker/CVE-2026-111497"
GUIDANCE = {
"debian": "https://www.debian.org/doc/manuals/debian-handbook/sect.regular-upgrades.en.html",
"ubuntu": "https://ubuntu.com/server/docs/how-to/software/package-management/",
"rhel": "https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/9/html/managing_and_monitoring_security_updates/index",
"rocky": "https://docs.rockylinux.org/guides/package_management/dnf_package_manager/",
"almalinux": "https://wiki.almalinux.org/documentation/errata.html",
"fedora": "https://dnf5.readthedocs.io/en/latest/commands/upgrade.8.html",
"ol": "https://docs.oracle.com/en/operating-systems/oracle-linux/software-management/sfw-mgmt-UpdateSoftwareonEnterpriseLinux.html",
"amzn": "https://docs.aws.amazon.com/linux/al2023/ug/security-inplace-update.html",
"sles": "https://documentation.suse.com/sles/15-SP7/html/SLES-all/cha-onlineupdate-you.html",
"alpine": "https://docs.alpinelinux.org/user-handbook/0.1a/Working/apk.html#_upgrading_packages",
"arch": "https://wiki.archlinux.org/title/System_maintenance",
"nixos": "https://wiki.nixos.org/wiki/Updating_NixOS",
"gentoo": "https://wiki.gentoo.org/wiki/Upgrading_Gentoo",
"slackware": "https://docs.slackware.com/slackware:slackpkg",
"darwin": "https://github.com/nodejs/node#verifying-binaries",
"win32": "https://github.com/nodejs/node#verifying-binaries",
}
NEXT_STEPS = {
"ACCESS_DENIED": "Obtain read access to the process/file and rerun.",
"FILE_MISSING": "Check inventory and deployment; inspect inactive files separately.",
"FILE_CHANGED": "Recheck artifact identity; restart or replace stale workers, then re-audit.",
"PROCESS_CHANGED": "Rerun against a stable process identity.",
"EVIDENCE_MISSING": "Supply trusted SHA-256 build provenance or exact vendor evidence.",
"EVIDENCE_MISMATCH": "Investigate the mismatch; obtain evidence for the actual artifact.",
"VENDOR_RULE_MISSING": "Supply a rule for this exact OS, release, package and CVE.",
"VERSION_UNSUPPORTED": "Use a supported fixed build or provide verified backport evidence.",
"LOADED_IMAGE_UNVERIFIED": "Use --files for installed files; that cannot clear Windows loaded images.",
"RESTART_REQUIRED": "Restart or replace the deployment using its normal procedure, then re-audit.",
"AFFECTED": "Update the affected Node/Brotli artifact, restart its consumers and re-audit.",
"INVENTORY_UNCONFIRMED": "Review all deployments and supply a confirmed inventory.",
"EXPECTED_RUNTIME_MISSING": "Check the expected service/namespace; inspect inactive files separately.",
"UNEXPECTED_RUNTIME": "Identify the artifact and include it in the reviewed inventory.",
"DISCOVERY_INCOMPLETE": "Resolve process access or namespace restrictions and rerun within the deployment.",
"NO_RUNTIME_FOUND": "Confirm namespace and inventory; use --files for inactive deployments.",
"TRANSPORT_FAILED": "Check SSH/container access, target Python path and transport errors.",
"OBSERVATION_ONLY": "Collect verified artifact and application-exposure evidence.",
"INSPECTION_FAILED": "Resolve the inspection error and rerun; do not clear this deployment.",
"DECODER_UNRESOLVED": "Identify and verify the actual loaded Brotli decoder and libnode artifacts.",
}
class AuditError(Exception):
"""Audit failure with a stable diagnostic code."""
def __init__(self, message, code="INSPECTION_FAILED"):
"""Initialize an audit failure.
Args:
message (str): Human-readable failure detail.
code (str): Key in NEXT_STEPS identifying the unresolved condition.
"""
super().__init__(message)
self.code = code
def command(argv, timeout=15, data=None):
"""Fixed system utilities, or a binary explicitly selected with --probe-node."""
environment = {"PATH": "/usr/sbin:/usr/bin:/sbin:/bin", "LC_ALL": "C"}
if sys.platform == "win32":
directory = windows_system_directory()
environment = {"SystemRoot": str(directory.parent), "PATH": str(directory),
"TEMP": os.environ.get("TEMP", str(directory.parent / "Temp"))}
if Path(argv[0]).name.lower() == "ssh.exe":
for key in ("USERPROFILE", "HOMEDRIVE", "HOMEPATH"):
if key in os.environ:
environment[key] = os.environ[key]
if Path(argv[0]).name == "ssh" and "SSH_AUTH_SOCK" in os.environ:
environment["SSH_AUTH_SOCK"] = os.environ["SSH_AUTH_SOCK"]
if Path(argv[0]).name in ("docker", "podman"):
environment["HOME"] = str(Path.home())
for key in ("DOCKER_HOST", "DOCKER_CONTEXT", "DOCKER_CONFIG", "DOCKER_TLS_VERIFY",
"DOCKER_CERT_PATH", "CONTAINER_HOST", "CONTAINER_CONNECTION", "XDG_RUNTIME_DIR"):
if key in os.environ:
environment[key] = os.environ[key]
try:
return subprocess.run(argv, input=data, text=True, capture_output=True,
timeout=timeout, check=False,
env=environment, encoding="utf-8", errors="replace")
except (OSError, subprocess.TimeoutExpired) as exc:
raise AuditError("Command failed or timed out: " + argv[0]) from exc
def windows_system_directory():
import ctypes
buffer = ctypes.create_unicode_buffer(32768)
size = ctypes.windll.kernel32.GetSystemDirectoryW(buffer, len(buffer))
require(0 < size < len(buffer), "Cannot locate Windows system directory")
return Path(buffer.value)
def system_tool(name):
if sys.platform == "win32":
relative = {"powershell": "WindowsPowerShell/v1.0/powershell.exe",
"ssh": "OpenSSH/ssh.exe"}.get(name)
require(relative is not None, "Unsupported Windows utility: " + name)
path = windows_system_directory() / relative
require(path.is_file(), "Missing Windows system utility: " + name)
return str(path)
directories = ("/usr/bin", "/bin", "/usr/sbin", "/sbin")
if name in ("docker", "podman"):
directories += ("/usr/local/bin", "/opt/homebrew/bin")
owners = {0}
if name in ("docker", "podman") and os.getuid() == os.geteuid():
owners.add(os.getuid())
for directory in directories:
path = Path(directory) / name
try:
info = path.stat()
if (stat.S_ISREG(info.st_mode) and info.st_uid in owners
and not info.st_mode & 0o022 and os.access(path, os.X_OK)):
return str(path)
except OSError:
continue
raise AuditError("Missing trusted system utility: " + name)
def load_json(path):
with open(path, "rb") as stream:
data = stream.read(MAX_CONFIG + 1)
if len(data) > MAX_CONFIG:
raise AuditError("Configuration exceeds 1 MiB")
return json.loads(data)
def require(condition, message, code="INSPECTION_FAILED"):
"""Enforce an audit precondition.
Args:
condition (bool): Whether the required invariant holds.
message (str): Failure detail.
code (str): Stable diagnostic code.
Raises:
AuditError: The condition is false.
"""
if not condition:
raise AuditError(message, code)
def diagnostic(code, detail, distro=None, manager=None):
"""Describe a finding without changing its patch verdict.
Args:
code (str): Stable NEXT_STEPS key.
detail (str): Finding detail; treated as data in text and JSON output.
distro (tuple[str, str] | None): Audited OS ID and release.
manager (str | None): Artifact package manager, when known.
Returns:
dict: Code, detail, next step and applicable update guidance URLs.
"""
guide = GUIDANCE.get(distro[0] if distro else sys.platform)
# Versioned vendor guides apply only to their documented product release.
if distro and ((distro[0] == "rhel" and distro[1].split(".")[0] != "9")
or (distro[0] == "amzn" and distro[1] != "2023")
or (distro[0] == "sles" and distro[1] != "15.7")):
guide = None
return {"code": code, "detail": detail, "next_step": NEXT_STEPS[code],
"guidance": [ADVISORY, guide] if guide else [ADVISORY]}
def error_diagnostic(error, distro=None, manager=None):
"""Convert an inspection error into an actionable diagnostic.
Args:
error (Exception): Inspection or validation error.
distro (tuple[str, str] | None): Audited OS ID and release.
manager (str | None): Package manager already identified for the artifact.
Returns:
dict: Diagnostic retaining the original error detail.
"""
code = error.code if isinstance(error, AuditError) else "INSPECTION_FAILED"
if isinstance(error, PermissionError):
code = "ACCESS_DENIED"
elif isinstance(error, FileNotFoundError):
code = "FILE_MISSING"
return diagnostic(code, str(error), distro, manager)
def artifact_cves(kind):
return (BROTLI_CVE,) if kind == "brotli" else CVES
def unknown_cves(kind, detail="No verified evidence for this CVE"):
return {cve: {"status": "UNKNOWN", "detail": detail, "sources": []} for cve in artifact_cves(kind)}
def combine_statuses(statuses):
statuses = list(statuses)
if "AFFECTED" in statuses:
return "AFFECTED"
if not statuses or any(s not in ("PATCHED", "NOT_AFFECTED") for s in statuses):
return "UNKNOWN"
return "NOT_AFFECTED" if all(s == "NOT_AFFECTED" for s in statuses) else "PATCHED"
def refresh_assessment(record):
results = record.setdefault("cve_results", unknown_cves(record["kind"]))
status = combine_statuses(results.get(cve, {}).get("status", "UNKNOWN") for cve in artifact_cves(record["kind"]))
record["status"] = "PATCHED" if status == "NOT_AFFECTED" else status
def set_cve_result(record, cve, status, detail, sources=()):
record.setdefault("cve_results", unknown_cves(record["kind"]))[cve] = {
"status": status, "detail": detail, "sources": list(sources)}
refresh_assessment(record)
def summarize_cves(records):
summary = {}
for cve in CVES:
statuses = []
for row in records:
if cve not in artifact_cves(row["kind"]):
continue
status = row.get("cve_results", {}).get(cve, {}).get("status", "UNKNOWN")
statuses.append("UNKNOWN" if row["restart_needed"] and status == "PATCHED" else status)
summary[cve] = combine_statuses(statuses)
return summary
def explain_record(record, distro):
"""Attach unresolved conditions and restart guidance to an artifact record.
Args:
record (dict): Artifact assessment, updated in place.
distro (tuple[str, str] | None): Audited OS ID and release.
Returns:
dict: The record with additive diagnostics; status remains unchanged.
"""
refresh_assessment(record)
findings = record.setdefault("diagnostics", [])
if not findings and record["status"] != "PATCHED":
code = "AFFECTED" if record["status"] == "AFFECTED" else (
"VERSION_UNSUPPORTED" if "node_version" in record else "INSPECTION_FAILED")
findings.append(diagnostic(code, record.get("detail", ""), distro, record.get("manager")))
if record.get("restart_needed"):
findings.append(diagnostic("RESTART_REQUIRED", "Running file was removed or replaced", distro, record.get("manager")))
return record
def report_metadata(inventory=None, evidence=None):
"""Record the audited host and fingerprints of the supplied JSON documents.
Args:
inventory (dict | None): Parsed inventory, including its scope assertion.
evidence (dict | None): Parsed evidence, including imported advisory rows.
Returns:
dict: UTC report time, tool identity, target OS/architecture and SHA-256
fingerprints of canonical JSON; raw inputs are not retained.
"""
def fingerprint(value):
return hashlib.sha256(json.dumps(value, sort_keys=True, separators=(",", ":"),
allow_nan=False).encode()).hexdigest() if value is not None else None
try:
distro = os_release()
except (OSError, AuditError):
distro = None
source_hash = globals().get("AUDIT_SOURCE_SHA256")
if source_hash is None and "__file__" in globals():
try:
source_hash = hashlib.sha256(Path(__file__).read_bytes()).hexdigest()
except OSError:
source_hash = None
return {"timestamp_utc": datetime.now(timezone.utc).isoformat(),
"tool_version": TOOL_VERSION, "tool_sha256": source_hash,
"target": {"platform": sys.platform, "os_release": list(distro) if distro else None,
"os_version": platform.mac_ver()[0] if sys.platform == "darwin" else platform.version(),
"architecture": platform.machine()},
"inventory_sha256": fingerprint(inventory), "evidence_sha256": fingerprint(evidence)}
def absolute_path(value):
return bool(isinstance(value, str) and not any(ord(c) < 32 for c in value)
and not value.startswith(("//", "\\\\"))
and (value.startswith("/") or
(PureWindowsPath(value).is_absolute() and re.match(r"^[A-Za-z]:", value)
and ":" not in value[2:])))
def path_key(value):
return ntpath.normcase(ntpath.normpath(value)) if re.match(r"^[A-Za-z]:", value) else value
def os_release():
# Only machine identifiers are needed; display fields have different syntax.
values = {}
path = Path("/etc/os-release")
if not path.is_file():
path = Path("/usr/lib/os-release")
for line in path.read_text().splitlines():
key, _, value = line.partition("=")
if key not in ("ID", "VERSION_ID", "VERSION_CODENAME"):
continue
match = re.fullmatch(r"""(["']?)([A-Za-z0-9._+~^-]+)\1""", value)
require(match is not None and (key != "ID" or re.fullmatch(r"[a-z0-9._-]+", match[2])),
"Invalid os-release field: " + key)
values[key] = match[2]
require("ID" in values, "Cannot determine OS release")
return values["ID"], values.get("VERSION_ID", values.get("VERSION_CODENAME", "rolling"))
def start_time(pid):
return Path(f"/proc/{pid}/stat").read_text().rsplit(")", 1)[1].split()[19]
def same_namespace(pid):
remote = os.stat(f"/proc/{pid}/ns/mnt")
local = os.stat("/proc/self/ns/mnt")
return ((remote.st_dev, remote.st_ino) == (local.st_dev, local.st_ino) and
os.stat(f"/proc/{pid}/root").st_ino == os.stat("/").st_ino and
os.stat(f"/proc/{pid}/root").st_dev == os.stat("/").st_dev)
def proc_path(raw):
deleted = raw.endswith(" (deleted)")
if deleted:
raw = raw[:-10]
return re.sub(r"\\([0-7]{3})", lambda m: chr(int(m[1], 8)), raw), deleted
@dataclass
class Runtime:
pid: int
kind: str
path: str
live_path: str
start: str
deleted: bool = False
device: int = 0
inode: int = 0
def powershell_json(script, data):
encoded = base64.b64encode(script.encode("utf-16le")).decode("ascii")
result = command([system_tool("powershell"), "-NoLogo", "-NoProfile", "-NonInteractive",
"-EncodedCommand", encoded], timeout=120, data=json.dumps(data))
require(result.returncode == 0, "Windows inspection failed: " + result.stderr[:1000])
return json.loads(result.stdout.lstrip("\ufeff"))
class MacOSProcesses:
"""Read process identities and mapped vnodes through libproc."""
class BSDInfo(ctypes.Structure):
_fields_ = [(name, ctypes.c_uint32) for name in (
"flags", "status", "xstatus", "pid", "ppid", "uid", "gid", "ruid",
"rgid", "svuid", "svgid", "reserved")]
_fields_ += [("comm", ctypes.c_char * 16), ("name", ctypes.c_char * 32)]
_fields_ += [(name, ctypes.c_uint32) for name in ("nfiles", "pgid", "jobc", "tdev", "tpgid")]
_fields_ += [("nice", ctypes.c_int32), ("seconds", ctypes.c_uint64), ("microseconds", ctypes.c_uint64)]
class RegionInfo(ctypes.Structure):
_fields_ = [(name, ctypes.c_uint32) for name in ("protection", "max_protection", "inheritance", "flags")]
_fields_ += [("offset", ctypes.c_uint64)]
_fields_ += [(name, ctypes.c_uint32) for name in (
"behavior", "wired", "tag", "resident", "private_shared", "swapped", "dirty",
"references", "shadow_depth", "share_mode", "private", "shared", "object_id", "depth")]
_fields_ += [("address", ctypes.c_uint64), ("size", ctypes.c_uint64)]
class VnodeStat(ctypes.Structure):
_fields_ = [("device", ctypes.c_uint32), ("mode", ctypes.c_uint16), ("nlink", ctypes.c_uint16),
("inode", ctypes.c_uint64), ("uid", ctypes.c_uint32), ("gid", ctypes.c_uint32)]
_fields_ += [(name, ctypes.c_int64) for name in (
"atime", "atime_ns", "mtime", "mtime_ns", "ctime", "ctime_ns",
"birthtime", "birthtime_ns", "size", "blocks")]
_fields_ += [("blocksize", ctypes.c_int32), ("flags", ctypes.c_uint32),
("generation", ctypes.c_uint32), ("rdevice", ctypes.c_uint32),
("reserved", ctypes.c_int64 * 2)]
def __init__(self):
class RegionPath(ctypes.Structure):
_fields_ = [("region", MacOSProcesses.RegionInfo), ("stat", MacOSProcesses.VnodeStat),
("type", ctypes.c_int), ("padding", ctypes.c_int),
("fsid", ctypes.c_int32 * 2), ("path", ctypes.c_char * 1024)]
self.region_type = RegionPath
self.lib = ctypes.CDLL("/usr/lib/libproc.dylib", use_errno=True)
self.lib.proc_listallpids.argtypes = [ctypes.c_void_p, ctypes.c_int]
self.lib.proc_listallpids.restype = ctypes.c_int
self.lib.proc_pidinfo.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_uint64, ctypes.c_void_p, ctypes.c_int]
self.lib.proc_pidinfo.restype = ctypes.c_int
self.lib.proc_pidpath.argtypes = [ctypes.c_int, ctypes.c_void_p, ctypes.c_uint32]
self.lib.proc_pidpath.restype = ctypes.c_int
def pids(self):
for _ in range(3):
count = self.lib.proc_listallpids(None, 0)
require(0 < count < 1000000, "Cannot enumerate macOS processes")
buffer = (ctypes.c_int * (count + 128))()
used = self.lib.proc_listallpids(buffer, ctypes.sizeof(buffer))
require(used > 0, "Cannot enumerate macOS processes")
if used < len(buffer):
return sorted(set(pid for pid in buffer[:used] if pid > 0))
raise AuditError("macOS process list kept growing during enumeration")
def info(self, pid, flavor, data, address=0):
ctypes.set_errno(0)
size = self.lib.proc_pidinfo(pid, flavor, address, ctypes.byref(data), ctypes.sizeof(data))
if size <= 0:
code = ctypes.get_errno() or errno.EIO
raise OSError(code, os.strerror(code))
require(size == ctypes.sizeof(data), "Incomplete macOS process information")
return data
def identity(self, pid):
data = self.info(pid, 3, self.BSDInfo()) # PROC_PIDTBSDINFO
require(data.pid == pid, "macOS process identity mismatch")
if data.status == 5: # SZOMB
raise ProcessLookupError(errno.ESRCH, "Process is a zombie")
return f"{data.seconds}:{data.microseconds}"
def executable(self, pid):
buffer = ctypes.create_string_buffer(4096)
ctypes.set_errno(0)
size = self.lib.proc_pidpath(pid, buffer, len(buffer))
if size <= 0:
code = ctypes.get_errno() or errno.EIO
raise OSError(code, os.strerror(code))
path = os.fsdecode(buffer.value)
require(absolute_path(path), "Invalid macOS executable path")
return path
def mappings(self, pid):
address, mappings = 0, {}
for index in range(100000):
try:
data = self.info(pid, 8, self.region_type(), address) # PROC_PIDREGIONPATHINFO
except OSError as exc:
if exc.errno == errno.EINVAL and index:
return list(mappings.values())
raise
end = data.region.address + data.region.size
require(data.region.address >= address and end > address and end < 2 ** 64,
"Invalid macOS memory region")
address = end
path = os.fsdecode(data.path)
if path:
require(absolute_path(path), "Invalid macOS mapped-file path")
row = {"path": path, "device": data.stat.device, "inode": data.stat.inode,
"nlink": data.stat.nlink}
key = path, row["device"], row["inode"]
mappings[key] = row
raise AuditError("macOS memory-region limit exceeded")
def hash_macos_runtime(runtime):
api = MacOSProcesses()
require(api.identity(runtime.pid) == runtime.start, "Process exited or PID was reused", "PROCESS_CHANGED")
require(runtime.inode > 0 and not runtime.deleted, "Running macOS file has no readable vnode identity")
with open(runtime.live_path, "rb") as stream:
before = os.fstat(stream.fileno())
require((before.st_dev, before.st_ino) == (runtime.device, runtime.inode),
"macOS pathname no longer identifies the running mapping; restart and re-audit", "FILE_CHANGED")
hashes, info = hash_stream(stream)
mappings = api.mappings(runtime.pid)
require(any((row["device"], row["inode"]) == (info.st_dev, info.st_ino) for row in mappings),
"macOS mapping changed during inspection", "PROCESS_CHANGED")
require(api.identity(runtime.pid) == runtime.start, "Process exited or PID was reused", "PROCESS_CHANGED")
after = os.fstat(stream.fileno())
require((info.st_size, info.st_mtime_ns, info.st_ctime_ns) ==
(after.st_size, after.st_mtime_ns, after.st_ctime_ns), "Running file changed during inspection", "FILE_CHANGED")
try:
current = os.stat(runtime.path)
restart = (current.st_dev, current.st_ino) != (info.st_dev, info.st_ino)
except FileNotFoundError:
restart = True
return hashes, restart
def hash_stream(stream):
info = os.fstat(stream.fileno())
require(stat.S_ISREG(info.st_mode), "Image is not a regular file")
hashes = {name: hashlib.new(name, usedforsecurity=False)
for name in ("md5", "sha1", "sha256", "sha384", "sha512")}
for block in iter(lambda: stream.read(1024 * 1024), b""):
for digest in hashes.values():
digest.update(block)
after = os.fstat(stream.fileno())
require((info.st_size, info.st_mtime_ns, info.st_ctime_ns) ==
(after.st_size, after.st_mtime_ns, after.st_ctime_ns), "File changed while hashing", "FILE_CHANGED")
return {name: digest.hexdigest() for name, digest in hashes.items()}, info
def hash_file(path):
"""Hash a stable installed file without executing it.
Args:
path (str): Absolute path on this host.
Returns:
dict[str, str]: File digests keyed by algorithm name.
Raises:
AuditError: The path is invalid, non-regular or changes while hashing.
OSError: The file is missing or cannot be inspected.
"""
require(absolute_path(path) and Path(path).is_absolute(), "File path must be absolute on this host")
require(stat.S_ISREG(os.stat(path).st_mode), "File is not a regular file: " + path)
with open(path, "rb") as stream:
hashes, info = hash_stream(stream)
# Windows stat/fstat can disagree on ctime semantics; compare open handles.
with open(path, "rb") as current_stream:
current = os.fstat(current_stream.fileno())
require((info.st_dev, info.st_ino, info.st_size, info.st_mtime_ns, info.st_ctime_ns) ==
(current.st_dev, current.st_ino, current.st_size, current.st_mtime_ns, current.st_ctime_ns),
"File pathname changed while hashing", "FILE_CHANGED")
return hashes
def hash_runtime(runtime):
if sys.platform == "darwin":
return hash_macos_runtime(runtime)
require(start_time(runtime.pid) == runtime.start, "Process exited or PID was reused", "PROCESS_CHANGED")
require(same_namespace(runtime.pid), "Process changed mount/root namespace", "PROCESS_CHANGED")
with ExitStack() as stack:
try:
stream = stack.enter_context(open(runtime.live_path, "rb"))
except OSError as exc:
# map_files may need extra capabilities. A pathname is acceptable only
# when its device/inode proves it is the file in the inspected mapping.
require(runtime.kind in MODULE_KINDS and not runtime.deleted,
"Cannot read the running file", error_diagnostic(exc)["code"])
stream = stack.enter_context(open(runtime.path, "rb"))
info = os.fstat(stream.fileno())
require(stat.S_ISREG(info.st_mode), "Running image is not a regular file")
require(runtime.inode > 0 and (info.st_dev, info.st_ino) == (runtime.device, runtime.inode),
"File no longer identifies the discovered executable/mapping", "FILE_CHANGED")
hashes, info = hash_stream(stream)
require(start_time(runtime.pid) == runtime.start, "Process exited or PID was reused", "PROCESS_CHANGED")
require(same_namespace(runtime.pid), "Process changed mount/root namespace", "PROCESS_CHANGED")
if runtime.kind in MODULE_KINDS:
mapped = False
for line in Path(f"/proc/{runtime.pid}/maps").read_text().splitlines():
fields = line.split(None, 5)
if len(fields) == 6:
device = os.makedev(*(int(part, 16) for part in fields[3].split(":")))
if (device, int(fields[4])) == (info.st_dev, info.st_ino):
mapped = True
require(mapped, "Node.js module mapping changed during inspection", "PROCESS_CHANGED")
else:
current = os.stat(runtime.live_path)
require((current.st_dev, current.st_ino) == (info.st_dev, info.st_ino),
"Process executable changed during inspection", "PROCESS_CHANGED")
restart = runtime.deleted
try:
disk = os.stat(runtime.path)
restart |= (disk.st_dev, disk.st_ino) != (info.st_dev, info.st_ino)
except FileNotFoundError:
restart = True
return hashes, restart
def dpkg_metadata(path):
tool = system_tool("dpkg-query")
result = command([tool, "-S", "--", path])
owners = set()
for line in result.stdout.splitlines():
owner, sep, owned_path = line.partition(": ")
if sep and owned_path == path:
owners.add(owner)
require(result.returncode == 0 and len(owners) == 1, "No unique dpkg owner for runtime")
owner = owners.pop()
require(re.fullmatch(r"[a-z0-9][a-z0-9+.-]*(?::[a-z0-9]+)?", owner), "Invalid package owner")
result = command([tool, "-W", "-f=${binary:Package}\t${Version}\t${db:Status-Status}\n", "--", owner])
fields = result.stdout.strip().split("\t")
require(result.returncode == 0 and len(fields) == 3 and fields[2] == "installed",
"Package is not fully installed")
package, version, _ = fields
require(re.fullmatch(r"[a-z0-9][a-z0-9+.-]*(?::[a-z0-9]+)?", package), "Invalid package name")
checksum = None
for name in dict.fromkeys((package, owner, package.split(":")[0])):
manifest = Path("/var/lib/dpkg/info") / (name + ".md5sums")
if not manifest.is_file():
continue
for line in manifest.read_text().splitlines():
parts = line.split(None, 1)
if len(parts) == 2 and "/" + parts[1].removeprefix("./") == path:
require(re.fullmatch(r"[0-9a-f]{32}", parts[0]), "Invalid package checksum")
checksum = parts[0]
require(checksum is not None, "Package has no checksum for the running file")
return {"manager": "dpkg", "package": package.split(":")[0], "version": version,
"algorithm": "md5", "digest": checksum}
def rpm_metadata(path):
tool = system_tool("rpm")
result = command([tool, "-qf", "--qf",
"%{NAME}\t%{EPOCHNUM}\t%{VERSION}\t%{RELEASE}\t%{FILEDIGESTALGO}\n", "--", path])
fields = result.stdout.strip().split("\t")
require(result.returncode == 0 and len(fields) == 5, "No unique RPM owner for runtime")
package, epoch, version, release, algorithm = fields
require(re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9+._-]*", package), "Invalid RPM name")
require(epoch.isdigit(), "Cannot determine RPM epoch")
algorithm = {"1": "md5", "2": "sha1", "8": "sha256", "9": "sha384", "10": "sha512"}.get(algorithm)
require(algorithm is not None, "Unsupported RPM file digest algorithm")
result = command([tool, "-qf", "--qf", "[%{FILENAMES}\t%{FILEDIGESTS}\n]", "--", path])
digests = [line.split("\t")[1] for line in result.stdout.splitlines()
if line.startswith(path + "\t") and len(line.split("\t")) == 2]
require(result.returncode == 0 and len(digests) == 1 and
re.fullmatch(r"[0-9a-f]+", digests[0]), "No unique RPM file checksum")
identity = command([tool, "-qf", "--qf", "%{ARCH}\t%{VENDOR}\n", "--", path])
identity_fields = identity.stdout.strip().split("\t")
require(identity.returncode == 0 and len(identity_fields) == 2, "Cannot determine RPM architecture/vendor")
arch, vendor = identity_fields
module_query = command([tool, "-qf", "--qf", "%{MODULARITYLABEL}", "--", path])
module = module_query.stdout.strip() if module_query.returncode == 0 else None
return {"manager": "rpm", "package": package, "version": f"{epoch}:{version}-{release}",
"algorithm": algorithm, "digest": digests[0], "arch": arch,
"module": "" if module == "(none)" else module, "vendor": vendor}
def apk_metadata(path):
matches = []
with Path("/lib/apk/db/installed").open() as stream:
name = version = directory = filename = None
for line in stream:
line = line.rstrip("\r\n")
if not line:
name = version = directory = filename = None
continue
key, sep, value = line.partition(":")
if not sep:
continue
if key == "P":
name = value
elif key == "V":
version = value
elif key == "F":
directory, filename = value, None
elif key == "R":
filename = value
elif key == "Z" and directory is not None and filename is not None:
if "/" + "/".join(filter(None, (directory, filename))) != path:
continue
require(value.startswith("Q1"), "Unsupported APK checksum encoding")
digest = base64.b64decode(value[2:], validate=True)
require(len(digest) == 20 and name and version, "Invalid APK file checksum")
matches.append({"manager": "apk", "package": name, "version": version,
"algorithm": "sha1", "digest": digest.hex()})
require(len(matches) == 1, "No unique APK file checksum; use build evidence")
return matches[0]
def pacman_metadata(path):
result = command([system_tool("pacman"), "-Qoq", "--", path])
owners = result.stdout.splitlines()
require(result.returncode == 0 and len(owners) == 1
and re.fullmatch(r"[A-Za-z0-9@_+][A-Za-z0-9@_.+-]*", owners[0]),
"No unique pacman owner for file")
package = owners[0]
result = command([system_tool("pacman"), "-Q", "--", package])
parts = result.stdout.split()
require(result.returncode == 0 and len(parts) == 2 and parts[0] == package
and re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9.+:~^_-]*", parts[1]),
"Cannot determine pacman package version")
version = parts[1]
manifest = Path("/var/lib/pacman/local") / (package + "-" + version) / "mtree"
matches = []
with gzip.open(manifest, "rt") as stream:
for line in stream:
fields = line.split()
if not fields or not fields[0].startswith("./"):
continue
name = re.sub(r"\\([0-7]{3})", lambda m: chr(int(m[1], 8)), fields[0][1:])
if name == path:
digests = [v[len("sha256digest="):] for v in fields[1:] if v.startswith("sha256digest=")]
require(len(digests) == 1 and re.fullmatch(r"[0-9a-f]{64}", digests[0]),
"No SHA-256 for file in pacman mtree")
matches.extend(digests)
require(len(matches) == 1, "No unique pacman file checksum; use build evidence")
return {"manager": "pacman", "package": package, "version": version,
"algorithm": "sha256", "digest": matches[0]}
def package_status(package, fixed):
if package["manager"] == "dpkg":
result = command([system_tool("dpkg"), "--compare-versions", package["version"], "ge", fixed])
require(result.returncode in (0, 1), "Package version comparison failed")
return "PATCHED" if result.returncode == 0 else "AFFECTED"
# Constrained values cannot inject Lua, RPM macros, or shell syntax.
for value in (package["version"], fixed):
require(re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9.+:~^_-]*", value), "Invalid package version")
if package["manager"] == "apk":
result = command([system_tool("apk"), "version", "-t", package["version"], fixed])
require(result.returncode == 0 and result.stdout.strip() in ("<", "=", ">"),
"APK version comparison failed")
return "AFFECTED" if result.stdout.strip() == "<" else "PATCHED"
if package["manager"] == "pacman":
result = command([system_tool("vercmp"), package["version"], fixed])
require(result.returncode == 0 and re.fullmatch(r"-?[0-9]+", result.stdout.strip()),
"pacman version comparison failed")
return "AFFECTED" if int(result.stdout.strip()) < 0 else "PATCHED"
require(package["manager"] == "rpm", "Unsupported version comparison")
expression = '%{lua:print(rpm.vercmp("' + package["version"] + '","' + fixed + '"))}'
result = command([system_tool("rpm"), "--eval", expression])
require(result.returncode == 0 and result.stdout.strip() in ("-1", "0", "1"),
"RPM version comparison unavailable")
return "AFFECTED" if result.stdout.strip() == "-1" else "PATCHED"
def inspect_runtime(runtime, distro, evidence):
"""Assess a discovered artifact while preserving running-image uncertainty.
Args:
runtime (Runtime): Discovered process or module identity.
distro (tuple[str, str] | None): Target OS ID and release.
evidence (dict): Validated vendor and build evidence.
Returns:
dict: Status, restart flag, retained evidence and actionable diagnostics.
Inspection failures remain UNKNOWN instead of clearing the artifact.
"""
record = {"pid": runtime.pid, "kind": runtime.kind, "path": runtime.path,
"status": "UNKNOWN", "restart_needed": runtime.deleted}
try:
if sys.platform == "win32":
record.update(inspect_file({"kind": runtime.kind, "path": runtime.path}, distro, evidence))
record.update(pid=runtime.pid, assessment_scope="observed-file")
record["file_status"] = record["status"]
record["file_cve_results"] = record["cve_results"]
record["cve_results"] = unknown_cves(runtime.kind, "Windows loaded-image identity is unverified")
record["status"] = "UNKNOWN"
record["detail"] += "; Windows module paths do not prove loaded-image identity; file status is separate"
record["diagnostics"].append(diagnostic("LOADED_IMAGE_UNVERIFIED", record["detail"], distro))
return record
if sys.platform == "darwin" and runtime.inode:
try:
current = os.stat(runtime.path)
record["restart_needed"] |= (current.st_dev, current.st_ino) != (runtime.device, runtime.inode)
except FileNotFoundError:
record["restart_needed"] = True
hashes, restart = hash_runtime(runtime)
record.update(sha256=hashes["sha256"], restart_needed=restart)
assess_file(record, hashes, distro, evidence)
except (AuditError, OSError, ValueError, IndexError, EOFError, zlib.error) as exc:
record["detail"] = str(exc)
record["diagnostics"] = [error_diagnostic(exc, distro, record.get("manager"))]
return explain_record(record, distro)
def inspect_file(item, distro, evidence):
"""Assess an installed file without asserting running-process coverage.
Args:
item (dict): Inventory kind and absolute file path.
distro (tuple[str, str] | None): Target OS ID and release.
evidence (dict): Validated operator evidence.
Returns:
dict: Installed-file verdict, evidence and unresolved-condition diagnostics.
"""
record = {"pid": 0, "kind": item["kind"], "path": item["path"], "status": "UNKNOWN",
"restart_needed": False, "assessment_scope": "installed-file"}
try:
assess_file(record, hash_file(item["path"]), distro, evidence)
except (AuditError, OSError, ValueError, IndexError, EOFError, zlib.error) as exc:
record["detail"] = str(exc)
record["diagnostics"] = [error_diagnostic(exc, distro, record.get("manager"))]
if sys.platform == "win32" and record["status"] == "UNKNOWN":
try:
record["version_hint"] = powershell_json(r'''
$ErrorActionPreference = 'Stop'
[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false)
$path = [Console]::In.ReadToEnd() | ConvertFrom-Json
$info = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($path)
@{product_version=$info.ProductVersion; file_version=$info.FileVersion} | ConvertTo-Json -Compress
''', item["path"])
except (AuditError, OSError, ValueError) as exc:
record["detail"] += "; Windows version resource unavailable: " + str(exc)
return explain_record(record, distro)
def finish(records, issues, mode="local", scope=None, diagnostics=None, metadata=None):
"""Summarize assessments without changing the established exit-code rules.
Args:
records (list[dict]): Artifact assessments.
issues (list[str]): Coverage or inspection failures preventing clearance.
mode (str): Local audit or transport/observation method.
scope (str | None): Operator-supplied deployment scope.
diagnostics (list[dict] | None): Structured explanations of report issues.
metadata (dict | None): Target/input identity captured by the caller.
Returns:
dict: Schema-1 report with additive diagnostics and metadata.
"""
summary = summarize_cves(records)
if "AFFECTED" in summary.values():
status = 1
elif (not records or issues or "UNKNOWN" in summary.values()
or any(row["status"] != "PATCHED" or row["restart_needed"] for row in records)):
status = 2
else:
status = 0
if diagnostics is None:
code = "TRANSPORT_FAILED" if mode in ("ssh", "container") else (
"OBSERVATION_ONLY" if mode != "local" else "DISCOVERY_INCOMPLETE")
diagnostics = [diagnostic(code, issue) for issue in issues]
if not records and not diagnostics:
diagnostics = [diagnostic("NO_RUNTIME_FOUND", "No runtime artifacts were assessed")]
metadata = metadata if metadata is not None else report_metadata()
metadata["host_role"] = "audit-target" if mode == "local" else "controller"
return {"schema": 1, "completed": True, "mode": mode, "scope": scope,
"cves": list(CVES), "cve_summary": summary,
"verdict": {0: "PATCHED", 1: "AFFECTED", 2: "UNKNOWN"}[status],
"exit_status": status, "runtimes": records, "issues": issues,
"diagnostics": diagnostics, "metadata": metadata}
def draft_inventory():
"""Discover candidate inventory paths without asserting deployment coverage.
Returns:
dict: UNKNOWN audit report containing an unconfirmed inventory draft.
Discovery errors are retained; inactive and hidden runtimes need review.
"""
try:
runtimes, issues = discover(None)
except (AuditError, OSError) as exc:
runtimes, issues = [], [str(exc)]
key = os.path.realpath if sys.platform == "darwin" else path_key
paths = {(run.kind, key(run.path)): run.path for run in runtimes}
draft = {"schema": 1, "scope": "REVIEW deployment scope", "coverage_confirmed": False,
"runtimes": [{"label": kind + " " + path, "kind": kind, "path": path}
for (kind, _), path in sorted(paths.items())],
"discovery_issues": issues,
"review_required": "Add inactive, renamed and hidden deployments; confirm scope and coverage before auditing."}
report = finish([], issues + ["Draft inventory requires operator review"],
diagnostics=[diagnostic("INVENTORY_UNCONFIRMED", draft["review_required"])] +
[diagnostic("DISCOVERY_INCOMPLETE", issue) for issue in issues])
draft["metadata"] = report["metadata"]
report["draft_inventory"] = draft
return report
def remote_main(payload):
try:
require(sys.version_info >= (3, 9), "Python 3.9+ is required")
result = draft_inventory() if payload.get("draft", False) else audit_local(
payload["inventory"], payload["evidence"], payload.get("files", False))
except (AuditError, OSError, ValueError, TypeError, EOFError, zlib.error) as exc:
result = finish([], ["Remote audit failed: " + str(exc)], diagnostics=[error_diagnostic(exc)])
result["token"] = payload["token"]
print(json.dumps(result))
return result["exit_status"]
def valid_remote_result(result, token, rc):
"""Check a remote envelope before trusting its verdict or rendering findings.
Args:
result (object): Decoded remote JSON.
token (str): Expected per-run result token.
rc (int): Transport process exit status.
Returns:
bool: Whether identity, field types, verdict and exit status are consistent.
"""
if not isinstance(result, dict) or result.get("token") != token:
return False
if result.get("schema") != 1 or result.get("completed") is not True:
return False
if result.get("cves") != list(CVES) or result.get("mode") != "local":
return False
records, issues = result.get("runtimes"), result.get("issues")
if not isinstance(records, list) or not isinstance(issues, list) or not all(isinstance(x, str) for x in issues):
return False
if not valid_diagnostics(result.get("diagnostics", [])):
return False
metadata = result.get("metadata")
if (not isinstance(metadata, dict) or not isinstance(metadata.get("target"), dict)
or not isinstance(metadata.get("timestamp_utc"), str)
or not isinstance(metadata.get("tool_version"), str)
or not isinstance(metadata.get("tool_sha256"), str)
or not re.fullmatch(r"[0-9a-f]{64}", metadata["tool_sha256"])):
return False
for row in records:
if (not isinstance(row, dict) or row.get("status") not in ("PATCHED", "AFFECTED", "UNKNOWN")
or type(row.get("restart_needed")) is not bool or type(row.get("pid")) is not int
or not isinstance(row.get("kind"), str) or row["kind"] not in KINDS
or not isinstance(row.get("path"), str)
or not valid_diagnostics(row.get("diagnostics", []))):
return False
if not valid_cve_results(row.get("cve_results"), row["kind"]):
return False
status = combine_statuses(r["status"] for r in row["cve_results"].values())
if row["status"] != ("PATCHED" if status == "NOT_AFFECTED" else status):
return False
if "file_status" in row or "file_cve_results" in row:
if (not valid_cve_results(row.get("file_cve_results"), row["kind"])
or row["pid"] <= 0 or any(r["status"] != "UNKNOWN" for r in row["cve_results"].values())):
return False
status = combine_statuses(r["status"] for r in row["file_cve_results"].values())
if row.get("file_status") != ("PATCHED" if status == "NOT_AFFECTED" else status):
return False
expected = finish(records, issues)
return (rc == result.get("exit_status") == expected["exit_status"]
and result.get("verdict") == expected["verdict"] and result.get("cve_summary") == expected["cve_summary"])
def valid_cve_results(value, kind):
return (isinstance(value, dict) and set(value) == set(artifact_cves(kind)) and all(
isinstance(row, dict) and row.get("status") in ("PATCHED", "AFFECTED", "UNKNOWN", "NOT_AFFECTED")
and isinstance(row.get("detail"), str) and isinstance(row.get("sources"), list)
and all(isinstance(source, str) for source in row["sources"]) for row in value.values()))
def valid_diagnostics(value):
"""Validate remote findings before rendering their fields.
Args:
value (object): Untrusted decoded JSON diagnostics value.
Returns:
bool: Whether all entries have known codes and safely renderable fields.
"""
return isinstance(value, list) and all(
isinstance(row, dict) and isinstance(row.get("code"), str) and row["code"] in NEXT_STEPS
and isinstance(row.get("detail"), str) and isinstance(row.get("next_step"), str)
and isinstance(row.get("guidance"), list)
and all(isinstance(url, str) and url.startswith("https://") for url in row["guidance"])
for row in value)
def remote_program(inventory, evidence, files=False, draft=False):
"""Serialize this script and configuration for an authenticated transport.
Args:
inventory (dict | None): Target inventory.
evidence (dict): Target evidence.
files (bool): Select installed-file assessment.
draft (bool): Discover an unconfirmed inventory instead of assessing files.
Returns:
tuple[str, str]: Per-run result token and Python source sent over stdin.
Raises:
OSError: This script cannot be read for transfer.
"""
token = secrets.token_hex(24)
payload = {"token": token, "inventory": inventory, "evidence": evidence, "files": files, "draft": draft}
source_bytes = Path(__file__).read_bytes()
source = source_bytes.decode("utf-8")
# repr embeds data as Python string literals; no target/config enters shell code.
source_hash = hashlib.sha256(source_bytes).hexdigest()
program = "import json\nns={'__name__':'node2shell_remote','AUDIT_SOURCE_SHA256':" + repr(source_hash) + "}\n"
program += "exec(compile(" + ascii(source) + ",'<audit>','exec'),ns)\n"
program += "raise SystemExit(ns['remote_main'](json.loads(" + repr(json.dumps(payload)) + ")))\n"
return token, program