Files
Samsara/samsara/core/detection.py
oceans2alaska 610a3bb447 Privacy hardening, remote-peer IP masking and review fixes
- Add privacy projection (core/privacy.py), private report storage, bounded
  process capture; omit identities, free text and unapproved paths
- Mask remote peer IPs: only loopback/host/wildcard stay numeric, others are
  reduced to remote_scope private/public (schema + scoring updated)
- Map ss TCP states to canonical names; ASCII-only numeric fields; fix
  "Microsoft Visual C++" product matching (Python and PowerShell)
- Merge 7fed3b9: -Diagnostic switch, serialization exit code 3, PS7 module
  path, UI error detail; keep hardened task/certificate collectors
- Add audit docs, privacy docs and regression tests

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 18:50:24 -08:00

50 lines
2.1 KiB
Python

"""OS identification and explicit profile checks; never probes a remote host."""
from pathlib import Path
import platform
import shlex
PROFILES = [
('Auto-detect', 'auto'),
('Windows Server 2016', 'windows:2016'),
('Windows Server 2019', 'windows:2019'),
('Windows Server 2022', 'windows:2022'),
('Windows Server 2025', 'windows:2025'),
('Red Hat Enterprise Linux 7 (legacy)', 'rhel:7'),
('Red Hat Enterprise Linux 8', 'rhel:8'),
('Red Hat Enterprise Linux 9', 'rhel:9'),
('Red Hat Enterprise Linux 10', 'rhel:10'),
('RHEL-compatible Linux', 'rhel:any'),
]
def detect_local_os():
system = platform.system()
if system == 'Linux':
release = {}
for line in Path('/etc/os-release').read_text(encoding='utf-8').splitlines():
if '=' in line:
key, value = line.split('=', 1)
release[key] = ' '.join(shlex.split(value))
distro = release.get('ID', '')
related = set(release.get('ID_LIKE', '').split())
rhel = distro in {'rhel', 'rocky', 'almalinux', 'centos', 'ol'} or 'rhel' in related
return {'platform': 'linux', 'family': 'rhel' if rhel else 'linux',
'version': release.get('VERSION_ID', '').split('.')[0],
'name': release.get('PRETTY_NAME', 'Linux')}
if system == 'Windows':
# Do not infer a Server edition from a desktop kernel/build number.
return {'platform': 'windows', 'family': 'windows', 'version': '',
'name': 'Windows ' + platform.win32_ver()[0]}
raise ValueError('Local collection is available on Linux. Import an inventory file for other platforms.')
def check_profile(detected, profile):
if profile == 'auto':
return
valid = {value for _, value in PROFILES}
if profile not in valid:
raise ValueError('Unknown operating system profile.')
family, version = profile.split(':')
if detected['family'] != family or (version != 'any' and detected['version'] != version):
raise ValueError('Detected operating system does not match the selected profile. Select Auto-detect to assess this computer.')