ScriptCollection 4.4.72__py3-none-any.whl → 4.4.73__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.
Files changed (34) hide show
  1. ScriptCollection/Executables.py +19 -0
  2. ScriptCollection/LinuxServerSecurityCheck/Checks/AccountChecks.py +71 -0
  3. ScriptCollection/LinuxServerSecurityCheck/Checks/ContainerChecks.py +34 -0
  4. ScriptCollection/LinuxServerSecurityCheck/Checks/FileSystemChecks.py +141 -0
  5. ScriptCollection/LinuxServerSecurityCheck/Checks/KernelChecks.py +49 -0
  6. ScriptCollection/LinuxServerSecurityCheck/Checks/LogChecks.py +39 -0
  7. ScriptCollection/LinuxServerSecurityCheck/Checks/NetworkChecks.py +67 -0
  8. ScriptCollection/LinuxServerSecurityCheck/Checks/PackageIntegrityChecks.py +62 -0
  9. ScriptCollection/LinuxServerSecurityCheck/Checks/PersistenceChecks.py +99 -0
  10. ScriptCollection/LinuxServerSecurityCheck/Checks/ProcessChecks.py +88 -0
  11. ScriptCollection/LinuxServerSecurityCheck/Checks/SSHChecks.py +56 -0
  12. ScriptCollection/LinuxServerSecurityCheck/Checks/__init__.py +0 -0
  13. ScriptCollection/LinuxServerSecurityCheck/LinuxServerSecurityChecker.py +90 -0
  14. ScriptCollection/LinuxServerSecurityCheck/LinuxSystemAccess.py +85 -0
  15. ScriptCollection/LinuxServerSecurityCheck/PackageOwnershipLookup.py +42 -0
  16. ScriptCollection/LinuxServerSecurityCheck/SecurityCheckBase.py +19 -0
  17. ScriptCollection/LinuxServerSecurityCheck/SecurityFinding.py +19 -0
  18. ScriptCollection/LinuxServerSecurityCheck/SuspiciousCommandPatterns.py +46 -0
  19. ScriptCollection/LinuxServerSecurityCheck/SystemAccessBase.py +244 -0
  20. ScriptCollection/LinuxServerSecurityCheck/SystemEntities.py +45 -0
  21. ScriptCollection/LinuxServerSecurityCheck/__init__.py +0 -0
  22. ScriptCollection/ScriptCollectionCore.py +1 -1
  23. ScriptCollection/TFCPS/Android/TFCPS_CodeUnitSpecific_Android.py +109 -5
  24. ScriptCollection/TFCPS/CPP/TFCPS_CodeUnitSpecific_CPP.py +1 -1
  25. ScriptCollection/TFCPS/Flutter/TFCPS_CodeUnitSpecific_Flutter.py +4 -4
  26. ScriptCollection/TFCPS/SCTaskRunnerServer.py +55 -9
  27. ScriptCollection/TFCPS/TFCPS_CodeUnitSpecific_Base.py +5 -2
  28. ScriptCollection/TFCPS/TFCPS_RemoteBuild.py +39 -11
  29. ScriptCollection/TFCPS/TFCPS_Tools_General.py +19 -0
  30. {scriptcollection-4.4.72.dist-info → scriptcollection-4.4.73.dist-info}/METADATA +177 -177
  31. {scriptcollection-4.4.72.dist-info → scriptcollection-4.4.73.dist-info}/RECORD +34 -14
  32. {scriptcollection-4.4.72.dist-info → scriptcollection-4.4.73.dist-info}/entry_points.txt +1 -0
  33. {scriptcollection-4.4.72.dist-info → scriptcollection-4.4.73.dist-info}/WHEEL +0 -0
  34. {scriptcollection-4.4.72.dist-info → scriptcollection-4.4.73.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,88 @@
1
+ import re
2
+ from ..SecurityCheckBase import SecurityCheckBase
3
+ from ..SecurityFinding import SecurityFinding, SecurityFindingSeverity
4
+ from ..SuspiciousCommandPatterns import SuspiciousCommandPatterns
5
+ from ..SystemAccessBase import SystemAccessBase
6
+
7
+
8
+ class ProcessesRunningFromTemporaryFoldersCheck(SecurityCheckBase):
9
+ """Legitimate software is not executed from world-writable temporary folders or from hidden folders, but dropped malware often is."""
10
+
11
+ def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
12
+ result: list[SecurityFinding] = []
13
+ for process in system.get_processes():
14
+ if process.executable is None:
15
+ continue
16
+ if SuspiciousCommandPatterns.is_in_temporary_folder(process.executable):
17
+ result.append(self.create_finding(SecurityFindingSeverity.High, f"Process runs from a temporary folder: {process.describe()}"))
18
+ elif SuspiciousCommandPatterns.contains_hidden_path_component(process.executable):
19
+ result.append(self.create_finding(SecurityFindingSeverity.High, f"Process runs from a hidden path: {process.describe()}"))
20
+ return result
21
+
22
+
23
+ class ProcessesWithDeletedExecutableCheck(SecurityCheckBase):
24
+ """Malware often deletes its executable after it was started.
25
+ A deleted executable can also be caused by a package-update without a restart of the process; in this case a new file exists at the same path."""
26
+
27
+ __deleted_suffix: str = " (deleted)"
28
+
29
+ def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
30
+ result: list[SecurityFinding] = []
31
+ for process in system.get_processes():
32
+ if process.executable is None or not process.executable.endswith(ProcessesWithDeletedExecutableCheck.__deleted_suffix):
33
+ continue
34
+ original_path = process.executable[:-len(ProcessesWithDeletedExecutableCheck.__deleted_suffix)]
35
+ if SuspiciousCommandPatterns.is_in_temporary_folder(original_path) or SuspiciousCommandPatterns.contains_hidden_path_component(original_path):
36
+ continue # already reported by ProcessesRunningFromTemporaryFoldersCheck
37
+ # /proc/<pid>/root resolves the path in the mount-namespace of the process, which is relevant for processes in containers.
38
+ if system.path_exists(f"/proc/{process.pid}/root{original_path}"):
39
+ result.append(self.create_finding(SecurityFindingSeverity.Low, f"Executable of process was replaced (probably by an update without restart): {process.describe()}"))
40
+ else:
41
+ result.append(self.create_finding(SecurityFindingSeverity.Mid, f"Executable of process was deleted and not replaced: {process.describe()}"))
42
+ return result
43
+
44
+
45
+ class ProcessesMasqueradingAsKernelThreadsCheck(SecurityCheckBase):
46
+ """Kernel-threads are shown in brackets (for example "[kworker/0:1]") and have no executable.
47
+ A user-space-process which names itself like that tries to hide."""
48
+
49
+ __kernel_thread_name_pattern = re.compile(r"^\[[^\]\s]+\]")
50
+
51
+ def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
52
+ result: list[SecurityFinding] = []
53
+ for process in system.get_processes():
54
+ if process.executable is not None and ProcessesMasqueradingAsKernelThreadsCheck.__kernel_thread_name_pattern.match(process.commandline):
55
+ result.append(self.create_finding(SecurityFindingSeverity.High, f"User-space-process disguises itself as kernel-thread: {process.describe()}"))
56
+ return result
57
+
58
+
59
+ class SuspiciousProcessCommandlinesCheck(SecurityCheckBase):
60
+ """Detects reverse-shells, crypto-miners and similar based on the commandline and the name of the running processes."""
61
+
62
+ def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
63
+ result: list[SecurityFinding] = []
64
+ for process in system.get_processes():
65
+ if process.is_kernel_thread_or_zombie():
66
+ continue
67
+ text = f"{process.name} {process.executable} {process.commandline}"
68
+ for pattern in SuspiciousCommandPatterns.find_matches(text):
69
+ result.append(self.create_finding(pattern.severity, f"Suspicious process ({pattern.description}): {process.describe()}"))
70
+ return result
71
+
72
+
73
+ class ProcessesWithPreloadedLibrariesCheck(SecurityCheckBase):
74
+ """LD_PRELOAD injects a library into a process, which is a common technique of user-space-rootkits."""
75
+
76
+ def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
77
+ result: list[SecurityFinding] = []
78
+ for process in system.get_processes():
79
+ if process.is_kernel_thread_or_zombie():
80
+ continue
81
+ environment = system.read_bytes(f"/proc/{process.pid}/environ", SystemAccessBase.maximal_size_of_text_files)
82
+ if environment is None:
83
+ continue
84
+ for variable in environment.split(b"\0"):
85
+ if variable.startswith(b"LD_PRELOAD=") and len(variable) > len(b"LD_PRELOAD="):
86
+ value = variable.decode("utf-8", errors="replace")
87
+ result.append(self.create_finding(SecurityFindingSeverity.Mid, f"Process has {value}: {process.describe()}"))
88
+ return result
@@ -0,0 +1,56 @@
1
+ from ..SecurityCheckBase import SecurityCheckBase
2
+ from ..SecurityFinding import SecurityFinding, SecurityFindingSeverity
3
+ from ..SystemAccessBase import SystemAccessBase
4
+
5
+
6
+ class SshServerConfigurationCheck(SecurityCheckBase):
7
+ """Checks the effective configuration of the ssh-server (as reported by "sshd -T")."""
8
+
9
+ def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
10
+ if not system.program_is_available("sshd"):
11
+ return []
12
+ program_result = system.run_program("sshd", ["-T"])
13
+ if program_result.exit_code != 0:
14
+ raise ValueError(f"\"sshd -T\" failed with exitcode {program_result.exit_code}: {program_result.stderr.strip()}")
15
+ configuration: dict[str, str] = {}
16
+ for line in program_result.stdout.splitlines():
17
+ fields = line.strip().split(" ", 1)
18
+ if len(fields) == 2:
19
+ configuration[fields[0].lower()] = fields[1].strip().lower()
20
+ result: list[SecurityFinding] = []
21
+ if configuration.get("permitemptypasswords") == "yes":
22
+ result.append(self.create_finding(SecurityFindingSeverity.High, "ssh-server allows logins with empty passwords (PermitEmptyPasswords yes)."))
23
+ if configuration.get("permitrootlogin") == "yes":
24
+ result.append(self.create_finding(SecurityFindingSeverity.Mid, "ssh-server allows root-logins with password (PermitRootLogin yes)."))
25
+ if configuration.get("passwordauthentication") == "yes":
26
+ result.append(self.create_finding(SecurityFindingSeverity.Low, "ssh-server allows password-authentication (PasswordAuthentication yes)."))
27
+ return result
28
+
29
+
30
+ class AuthorizedKeysCheck(SecurityCheckBase):
31
+ """Authorized ssh-keys are the most common persistence-mechanism of attackers."""
32
+
33
+ def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
34
+ minimal_uid = system.get_minimal_uid_of_regular_users()
35
+ result: list[SecurityFinding] = []
36
+ handled_home_folders: set[str] = set()
37
+ for user in system.get_users():
38
+ if user.home_folder in handled_home_folders or not user.home_folder.startswith("/"):
39
+ continue
40
+ handled_home_folders.add(user.home_folder)
41
+ for file_name in ["authorized_keys", "authorized_keys2"]:
42
+ file = f"{user.home_folder.rstrip('/')}/.ssh/{file_name}"
43
+ content = system.read_text(file)
44
+ if content is None:
45
+ continue
46
+ keys = [line.strip() for line in content.splitlines() if line.strip() != "" and not line.strip().startswith("#")]
47
+ if len(keys) == 0:
48
+ continue
49
+ if file_name == "authorized_keys2":
50
+ result.append(self.create_finding(SecurityFindingSeverity.Mid, f"\"{file}\" contains {len(keys)} key(s). This deprecated file is rarely used legitimately and often overlooked."))
51
+ if 0 < user.uid < minimal_uid:
52
+ result.append(self.create_finding(SecurityFindingSeverity.Mid, f"System-account \"{user.name}\" (uid {user.uid}) has {len(keys)} authorized ssh-key(s) in \"{file}\"."))
53
+ for key in keys:
54
+ if "command=" in key.split(" ", 1)[0]:
55
+ result.append(self.create_finding(SecurityFindingSeverity.Low, f"Authorized ssh-key in \"{file}\" has a forced command: {key[:200]}"))
56
+ return result
@@ -0,0 +1,90 @@
1
+ import time
2
+ from ..SCLog import SCLog, LogLevel
3
+ from .SecurityCheckBase import SecurityCheckBase
4
+ from .SecurityFinding import SecurityFinding, SecurityFindingSeverity
5
+ from .SystemAccessBase import SystemAccessBase
6
+ from .Checks.AccountChecks import NonRootAccountsWithUid0Check, AccountsWithoutPasswordCheck, DuplicateUidsCheck, SystemAccountsWithLoginShellCheck, SudoersWithoutPasswordCheck
7
+ from .Checks.SSHChecks import SshServerConfigurationCheck, AuthorizedKeysCheck
8
+ from .Checks.ProcessChecks import ProcessesRunningFromTemporaryFoldersCheck, ProcessesWithDeletedExecutableCheck, ProcessesMasqueradingAsKernelThreadsCheck, SuspiciousProcessCommandlinesCheck, ProcessesWithPreloadedLibrariesCheck
9
+ from .Checks.NetworkChecks import PromiscuousNetworkInterfacesCheck, SuspiciousListeningPortsCheck, ConnectionsToMiningPoolPortsCheck
10
+ from .Checks.PersistenceChecks import SuspiciousCommandsInAutostartLocationsCheck, LibraryPreloadConfigurationCheck, CrontabsOfSystemAccountsCheck, RecentlyChangedSecurityRelevantFilesCheck
11
+ from .Checks.FileSystemChecks import SetuidFilesCheck, SuspiciousFilesInTemporaryFoldersCheck, CriticalFilePermissionsCheck, ImmutableFilesCheck
12
+ from .Checks.KernelChecks import UnknownKernelModulesCheck, KernelTaintCheck
13
+ from .Checks.PackageIntegrityChecks import ModifiedPackageFilesCheck, UnpackagedPamModulesCheck
14
+ from .Checks.ContainerChecks import PrivilegedDockerContainersCheck
15
+ from .Checks.LogChecks import RedirectedShellHistoriesCheck, EmptyLoginRecordsCheck
16
+
17
+
18
+ class LinuxServerSecurityChecker:
19
+ """Runs read-only checks on a linux-server to detect signs of a compromise or of suspicious activities.
20
+ Every finding is logged as one line with the prefix "[High]", "[Mid]" or "[Low]".
21
+ The exitcode is the amount of high-findings (capped at maximal_exit_code_for_findings) or exit_code_for_application_error if a check could not be executed."""
22
+
23
+ maximal_exit_code_for_findings: int = 254
24
+ exit_code_for_application_error: int = 255
25
+
26
+ def __init__(self, log: SCLog, system: SystemAccessBase, checks: list[SecurityCheckBase]):
27
+ self.__log = log
28
+ self.__system = system
29
+ self.__checks = checks
30
+
31
+ @staticmethod
32
+ def get_default_checks() -> list[SecurityCheckBase]:
33
+ return [
34
+ NonRootAccountsWithUid0Check(),
35
+ AccountsWithoutPasswordCheck(),
36
+ DuplicateUidsCheck(),
37
+ SystemAccountsWithLoginShellCheck(),
38
+ SudoersWithoutPasswordCheck(),
39
+ SshServerConfigurationCheck(),
40
+ AuthorizedKeysCheck(),
41
+ ProcessesRunningFromTemporaryFoldersCheck(),
42
+ ProcessesWithDeletedExecutableCheck(),
43
+ ProcessesMasqueradingAsKernelThreadsCheck(),
44
+ SuspiciousProcessCommandlinesCheck(),
45
+ ProcessesWithPreloadedLibrariesCheck(),
46
+ PromiscuousNetworkInterfacesCheck(),
47
+ SuspiciousListeningPortsCheck(),
48
+ ConnectionsToMiningPoolPortsCheck(),
49
+ SuspiciousCommandsInAutostartLocationsCheck(),
50
+ LibraryPreloadConfigurationCheck(),
51
+ CrontabsOfSystemAccountsCheck(),
52
+ RecentlyChangedSecurityRelevantFilesCheck(),
53
+ SetuidFilesCheck(),
54
+ SuspiciousFilesInTemporaryFoldersCheck(),
55
+ CriticalFilePermissionsCheck(),
56
+ ImmutableFilesCheck(),
57
+ UnknownKernelModulesCheck(),
58
+ KernelTaintCheck(),
59
+ ModifiedPackageFilesCheck(),
60
+ UnpackagedPamModulesCheck(),
61
+ PrivilegedDockerContainersCheck(),
62
+ RedirectedShellHistoriesCheck(),
63
+ EmptyLoginRecordsCheck(),
64
+ ]
65
+
66
+ def run(self) -> int:
67
+ if not self.__system.current_user_is_root():
68
+ raise PermissionError("The security-check must be executed as root.")
69
+ findings: list[SecurityFinding] = []
70
+ failed_checks: list[str] = []
71
+ for check in self.__checks:
72
+ start = time.monotonic()
73
+ self.__log.log(f"Run check \"{check.get_name()}\".", LogLevel.Debug)
74
+ try:
75
+ check_findings = check.run(self.__system)
76
+ except Exception as exception: # pylint:disable=broad-exception-caught
77
+ # the other checks are still executed, because one failing check must not hide the findings of the other checks.
78
+ self.__log.log_exception(f"Check \"{check.get_name()}\" failed.", exception, LogLevel.Error)
79
+ failed_checks.append(check.get_name())
80
+ continue
81
+ for finding in check_findings:
82
+ self.__log.log(finding.to_line(), LogLevel.Information)
83
+ findings.extend(check_findings)
84
+ self.__log.log(f"Check \"{check.get_name()}\" finished in {time.monotonic() - start:.1f} seconds with {len(check_findings)} finding(s).", LogLevel.Debug)
85
+ amount_of_high_findings = len([finding for finding in findings if finding.severity == SecurityFindingSeverity.High])
86
+ self.__log.log(f"Summary: {len(findings)} finding(s), {amount_of_high_findings} with severity high, {len(failed_checks)} failed check(s).", LogLevel.Debug)
87
+ if len(failed_checks) > 0:
88
+ self.__log.log(f"The following checks failed: {', '.join(failed_checks)}", LogLevel.Error)
89
+ return LinuxServerSecurityChecker.exit_code_for_application_error
90
+ return min(amount_of_high_findings, LinuxServerSecurityChecker.maximal_exit_code_for_findings)
@@ -0,0 +1,85 @@
1
+ import errno
2
+ import os
3
+ import shutil
4
+ import struct
5
+ import subprocess
6
+ import time
7
+ from .SystemAccessBase import SystemAccessBase
8
+ from .SystemEntities import ProgramResult
9
+
10
+
11
+ class LinuxSystemAccess(SystemAccessBase):
12
+ """Accesses the local linux-system. Every method only reads, nothing is changed on the system."""
13
+
14
+ # _IOR('f', 1, long) on 64-bit-systems.
15
+ ioctl_get_flags: int = 0x80086601
16
+ timeout_of_programs_in_seconds: int = 1800
17
+ chunk_size: int = 65536
18
+
19
+ def current_user_is_root(self) -> bool:
20
+ return os.geteuid() == 0 # pylint:disable=no-member
21
+
22
+ def read_bytes(self, path: str, maximal_amount_of_bytes: int) -> bytes | None:
23
+ try:
24
+ with open(path, "rb") as file:
25
+ # read in chunks, because some files (for example in /proc/sys) refuse a single read with a large buffer.
26
+ chunks: list[bytes] = []
27
+ amount_of_read_bytes = 0
28
+ while amount_of_read_bytes < maximal_amount_of_bytes:
29
+ chunk = file.read(min(LinuxSystemAccess.chunk_size, maximal_amount_of_bytes - amount_of_read_bytes))
30
+ if len(chunk) == 0:
31
+ break
32
+ chunks.append(chunk)
33
+ amount_of_read_bytes += len(chunk)
34
+ return b"".join(chunks)
35
+ except (FileNotFoundError, NotADirectoryError, ProcessLookupError):
36
+ return None
37
+
38
+ def list_folder(self, path: str) -> list[str] | None:
39
+ try:
40
+ return os.listdir(path)
41
+ except (FileNotFoundError, NotADirectoryError, ProcessLookupError):
42
+ return None
43
+
44
+ def get_lstat(self, path: str) -> os.stat_result | None:
45
+ try:
46
+ return os.lstat(path)
47
+ except (FileNotFoundError, NotADirectoryError, ProcessLookupError):
48
+ return None
49
+
50
+ def path_exists(self, path: str) -> bool:
51
+ return os.path.exists(path)
52
+
53
+ def read_link(self, path: str) -> str | None:
54
+ try:
55
+ return os.readlink(path)
56
+ except (FileNotFoundError, NotADirectoryError, ProcessLookupError):
57
+ return None
58
+
59
+ def program_is_available(self, program: str) -> bool:
60
+ return shutil.which(program) is not None
61
+
62
+ def run_program(self, program: str, arguments: list[str]) -> ProgramResult:
63
+ environment = dict(os.environ)
64
+ environment["LC_ALL"] = "C"
65
+ completed_process = subprocess.run([program] + arguments, stdin=subprocess.DEVNULL, capture_output=True, text=True, errors="replace", env=environment, timeout=LinuxSystemAccess.timeout_of_programs_in_seconds, check=False)
66
+ return ProgramResult(completed_process.returncode, completed_process.stdout, completed_process.stderr)
67
+
68
+ def get_file_attribute_flags(self, path: str) -> int | None:
69
+ import fcntl # pylint:disable=import-outside-toplevel,import-error # fcntl only exists on unix-systems
70
+ try:
71
+ file_descriptor = os.open(path, os.O_RDONLY | os.O_NONBLOCK | os.O_NOFOLLOW) # pylint:disable=no-member
72
+ except FileNotFoundError:
73
+ return None
74
+ try:
75
+ buffer = fcntl.ioctl(file_descriptor, LinuxSystemAccess.ioctl_get_flags, b"\0" * 8) # pylint:disable=no-member
76
+ return struct.unpack("i", buffer[:4])[0]
77
+ except OSError as error:
78
+ if error.errno in (errno.ENOTTY, errno.EOPNOTSUPP, errno.EINVAL, errno.ENOSYS):
79
+ return None
80
+ raise
81
+ finally:
82
+ os.close(file_descriptor)
83
+
84
+ def get_current_time(self) -> float:
85
+ return time.time()
@@ -0,0 +1,42 @@
1
+ from .SystemAccessBase import SystemAccessBase
2
+
3
+
4
+ class PackageOwnershipLookup:
5
+ """Determines whether a file is owned by a package of the package-manager (dpkg or rpm)."""
6
+
7
+ __merged_usr_prefixes: tuple[str, ...] = ("/usr/bin/", "/usr/sbin/", "/usr/lib/", "/usr/lib32/", "/usr/lib64/", "/usr/libx32/")
8
+
9
+ def __init__(self, system: SystemAccessBase):
10
+ self.__system = system
11
+ self.__dpkg_files: set[str] | None = None
12
+
13
+ def is_available(self) -> bool:
14
+ return self.__uses_dpkg() or self.__system.program_is_available("rpm")
15
+
16
+ def is_owned_by_package(self, path: str) -> bool:
17
+ if self.__uses_dpkg():
18
+ return PackageOwnershipLookup.__canonicalize(path) in self.__get_dpkg_files()
19
+ if self.__system.program_is_available("rpm"):
20
+ return self.__system.run_program("rpm", ["-qf", path]).exit_code == 0
21
+ raise ValueError("No supported package-manager available.")
22
+
23
+ def __uses_dpkg(self) -> bool:
24
+ return self.__system.is_folder("/var/lib/dpkg/info")
25
+
26
+ def __get_dpkg_files(self) -> set[str]:
27
+ if self.__dpkg_files is None:
28
+ files: set[str] = set()
29
+ for entry in self.__system.list_folder("/var/lib/dpkg/info") or []:
30
+ if entry.endswith(".list"):
31
+ content = self.__system.read_text(f"/var/lib/dpkg/info/{entry}")
32
+ if content is not None:
33
+ files.update(PackageOwnershipLookup.__canonicalize(line.strip()) for line in content.splitlines() if line.strip() != "")
34
+ self.__dpkg_files = files
35
+ return self.__dpkg_files
36
+
37
+ @staticmethod
38
+ def __canonicalize(path: str) -> str:
39
+ """Maps "/usr/bin/x" and "/bin/x" to the same value, because on systems with merged /usr both refer to the same file and dpkg may know only one of them."""
40
+ if path.startswith(PackageOwnershipLookup.__merged_usr_prefixes):
41
+ return path[len("/usr"):]
42
+ return path
@@ -0,0 +1,19 @@
1
+ from abc import ABC, abstractmethod
2
+ from .SecurityFinding import SecurityFinding, SecurityFindingSeverity
3
+ from .SystemAccessBase import SystemAccessBase
4
+
5
+
6
+ class SecurityCheckBase(ABC):
7
+ """Base-class of all checks. A check only reads the system and must not change anything.
8
+ To add a new check: derive from this class, implement "run" and add an instance to LinuxServerSecurityChecker.get_default_checks."""
9
+
10
+ def get_name(self) -> str:
11
+ class_name = type(self).__name__
12
+ return class_name.removesuffix("Check")
13
+
14
+ @abstractmethod
15
+ def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
16
+ raise NotImplementedError
17
+
18
+ def create_finding(self, severity: SecurityFindingSeverity, message: str) -> SecurityFinding:
19
+ return SecurityFinding(self.get_name(), severity, message)
@@ -0,0 +1,19 @@
1
+ from dataclasses import dataclass
2
+ from enum import Enum
3
+
4
+
5
+ class SecurityFindingSeverity(Enum):
6
+ High = "High"
7
+ Mid = "Mid"
8
+ Low = "Low"
9
+
10
+
11
+ @dataclass(frozen=True)
12
+ class SecurityFinding:
13
+ check_name: str
14
+ severity: SecurityFindingSeverity
15
+ message: str
16
+
17
+ def to_line(self) -> str:
18
+ single_line_message = " ".join(self.message.splitlines())
19
+ return f"[{self.severity.value}] [{self.check_name}] {single_line_message}"
@@ -0,0 +1,46 @@
1
+ import re
2
+ from dataclasses import dataclass
3
+ from .SecurityFinding import SecurityFindingSeverity
4
+
5
+
6
+ @dataclass(frozen=True)
7
+ class SuspiciousCommandPattern:
8
+ description: str
9
+ regex: re.Pattern[str]
10
+ severity: SecurityFindingSeverity
11
+
12
+
13
+ class SuspiciousCommandPatterns:
14
+ """Patterns of commands which are typical for attackers (download-and-execute, reverse-shells, crypto-miners, hiding of traces)."""
15
+
16
+ temporary_folders: tuple[str, ...] = ("/tmp/", "/var/tmp/", "/dev/shm/")
17
+ """Folders which are writable for everybody and which are therefore typically used by attackers to drop their tools."""
18
+
19
+ __shell = r"(sudo\s+)?(/usr)?(/bin/)?(ba|da|z|k)?sh\b"
20
+ __patterns: list[SuspiciousCommandPattern] = [
21
+ SuspiciousCommandPattern("download piped into a shell", re.compile(r"\b(curl|wget)\b[^|;&\n]*\|\s*" + __shell, re.IGNORECASE), SecurityFindingSeverity.High),
22
+ SuspiciousCommandPattern("base64-decoded content piped into a shell", re.compile(r"\bbase64\s+(-d|--decode)\b[^\n]*\|\s*" + __shell, re.IGNORECASE), SecurityFindingSeverity.High),
23
+ SuspiciousCommandPattern("network-connection via /dev/tcp or /dev/udp (reverse-shell)", re.compile(r"/dev/(tcp|udp)/", re.IGNORECASE), SecurityFindingSeverity.High),
24
+ SuspiciousCommandPattern("netcat executing a program (reverse-shell)", re.compile(r"\b(nc|ncat|netcat)\b[^\n|;&]*\s-(e|c)\s", re.IGNORECASE), SecurityFindingSeverity.High),
25
+ SuspiciousCommandPattern("socat executing a program (reverse-shell)", re.compile(r"\bsocat\b[^\n]*\b(exec|system):", re.IGNORECASE), SecurityFindingSeverity.High),
26
+ SuspiciousCommandPattern("interactive shell with redirected input/output (reverse-shell)", re.compile(r"\b(ba|z)?sh\s+-i\b[^\n]*[<>]&", re.IGNORECASE), SecurityFindingSeverity.High),
27
+ SuspiciousCommandPattern("scripting-language opening a socket (reverse-shell)", re.compile(r"\b(python[0-9.]*|perl|ruby|php)\s+-(c|e|r)\s+[^\n]*\bsocket\b", re.IGNORECASE), SecurityFindingSeverity.High),
28
+ SuspiciousCommandPattern("crypto-mining-pool-protocol", re.compile(r"\bstratum\+(tcp|ssl|tls)://", re.IGNORECASE), SecurityFindingSeverity.High),
29
+ SuspiciousCommandPattern("known crypto-miner or malware", re.compile(r"\b(xmrig|xmr-stak|minerd|cpuminer|kdevtmpfsi|kinsing|kthreaddk)\b", re.IGNORECASE), SecurityFindingSeverity.High),
30
+ SuspiciousCommandPattern("hidden file in a temporary folder", re.compile(r"(?<![\w.])(/tmp|/var/tmp|/dev/shm)/\.(?!(X11-unix|ICE-unix|font-unix|XIM-unix|Test-unix)\b)[^\s/\"']", re.IGNORECASE), SecurityFindingSeverity.Mid),
31
+ SuspiciousCommandPattern("making a file immutable", re.compile(r"\bchattr\s+[^\n]*\+[a-z]*i", re.IGNORECASE), SecurityFindingSeverity.Mid),
32
+ SuspiciousCommandPattern("disabling the shell-history", re.compile(r"\bHISTFILE=/dev/null|\bunset\s+HISTFILE\b|\bHISTSIZE=0\b|\bhistory\s+-c\b"), SecurityFindingSeverity.Mid),
33
+ SuspiciousCommandPattern("preloading a library", re.compile(r"\bLD_PRELOAD="), SecurityFindingSeverity.Mid),
34
+ ]
35
+
36
+ @staticmethod
37
+ def find_matches(text: str) -> list[SuspiciousCommandPattern]:
38
+ return [pattern for pattern in SuspiciousCommandPatterns.__patterns if pattern.regex.search(text) is not None] # pylint:disable=no-member # false positive of pylint for re.Pattern-fields of dataclasses
39
+
40
+ @staticmethod
41
+ def is_in_temporary_folder(path: str) -> bool:
42
+ return path.startswith(SuspiciousCommandPatterns.temporary_folders)
43
+
44
+ @staticmethod
45
+ def contains_hidden_path_component(path: str) -> bool:
46
+ return any(part.startswith(".") and part not in (".", "..") for part in path.split("/"))