ScriptCollection 4.4.72__py3-none-any.whl → 4.4.74__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.
- ScriptCollection/Executables.py +19 -0
- ScriptCollection/LinuxServerSecurityCheck/Checks/AccountChecks.py +71 -0
- ScriptCollection/LinuxServerSecurityCheck/Checks/ContainerChecks.py +34 -0
- ScriptCollection/LinuxServerSecurityCheck/Checks/FileSystemChecks.py +141 -0
- ScriptCollection/LinuxServerSecurityCheck/Checks/KernelChecks.py +49 -0
- ScriptCollection/LinuxServerSecurityCheck/Checks/LogChecks.py +39 -0
- ScriptCollection/LinuxServerSecurityCheck/Checks/NetworkChecks.py +67 -0
- ScriptCollection/LinuxServerSecurityCheck/Checks/PackageIntegrityChecks.py +62 -0
- ScriptCollection/LinuxServerSecurityCheck/Checks/PersistenceChecks.py +99 -0
- ScriptCollection/LinuxServerSecurityCheck/Checks/ProcessChecks.py +88 -0
- ScriptCollection/LinuxServerSecurityCheck/Checks/SSHChecks.py +56 -0
- ScriptCollection/LinuxServerSecurityCheck/Checks/__init__.py +0 -0
- ScriptCollection/LinuxServerSecurityCheck/LinuxServerSecurityChecker.py +90 -0
- ScriptCollection/LinuxServerSecurityCheck/LinuxSystemAccess.py +85 -0
- ScriptCollection/LinuxServerSecurityCheck/PackageOwnershipLookup.py +42 -0
- ScriptCollection/LinuxServerSecurityCheck/SecurityCheckBase.py +19 -0
- ScriptCollection/LinuxServerSecurityCheck/SecurityFinding.py +19 -0
- ScriptCollection/LinuxServerSecurityCheck/SuspiciousCommandPatterns.py +46 -0
- ScriptCollection/LinuxServerSecurityCheck/SystemAccessBase.py +244 -0
- ScriptCollection/LinuxServerSecurityCheck/SystemEntities.py +45 -0
- ScriptCollection/LinuxServerSecurityCheck/__init__.py +0 -0
- ScriptCollection/ProgramRunnerPopen.py +4 -2
- ScriptCollection/ProgramRunnerSudo.py +11 -5
- ScriptCollection/ScriptCollectionCore.py +18 -6
- ScriptCollection/TFCPS/Android/TFCPS_CodeUnitSpecific_Android.py +109 -5
- ScriptCollection/TFCPS/CPP/TFCPS_CodeUnitSpecific_CPP.py +1 -1
- ScriptCollection/TFCPS/Flutter/TFCPS_CodeUnitSpecific_Flutter.py +4 -4
- ScriptCollection/TFCPS/SCTaskRunnerServer.py +55 -9
- ScriptCollection/TFCPS/TFCPS_CodeUnitSpecific_Base.py +5 -2
- ScriptCollection/TFCPS/TFCPS_CodeUnit_BuildCodeUnits.py +158 -2
- ScriptCollection/TFCPS/TFCPS_RemoteBuild.py +39 -11
- ScriptCollection/TFCPS/TFCPS_Tools_General.py +19 -0
- {scriptcollection-4.4.72.dist-info → scriptcollection-4.4.74.dist-info}/METADATA +177 -177
- {scriptcollection-4.4.72.dist-info → scriptcollection-4.4.74.dist-info}/RECORD +37 -17
- {scriptcollection-4.4.72.dist-info → scriptcollection-4.4.74.dist-info}/entry_points.txt +1 -0
- {scriptcollection-4.4.72.dist-info → scriptcollection-4.4.74.dist-info}/WHEEL +0 -0
- {scriptcollection-4.4.72.dist-info → scriptcollection-4.4.74.dist-info}/top_level.txt +0 -0
ScriptCollection/Executables.py
CHANGED
|
@@ -17,6 +17,8 @@ from .TFCPS.TFCPS_Tools_General import TFCPS_Tools_General
|
|
|
17
17
|
from .TFCPS.DotNet.NuGetPackageSourceRegistration import NuGetPackageSourceRegistration
|
|
18
18
|
from .OCIImages.OCIImageManager import OCIImageManager
|
|
19
19
|
from .AgentRunner import AgentRunner, AgentRunnerConfiguration, CodingAgent
|
|
20
|
+
from .LinuxServerSecurityCheck.LinuxServerSecurityChecker import LinuxServerSecurityChecker
|
|
21
|
+
from .LinuxServerSecurityCheck.LinuxSystemAccess import LinuxSystemAccess
|
|
20
22
|
|
|
21
23
|
|
|
22
24
|
def RunAgentInSandbox() -> int:
|
|
@@ -1451,3 +1453,20 @@ def WebpToPng() -> int:
|
|
|
1451
1453
|
output_file = GeneralUtilities.resolve_relative_path(args.outputfile, os.getcwd())
|
|
1452
1454
|
ScriptCollectionCore().convert_webp_to_png(input_file, output_file, args.replace)
|
|
1453
1455
|
return 0
|
|
1456
|
+
|
|
1457
|
+
|
|
1458
|
+
def LinuxServerSecurityCheck() -> int:
|
|
1459
|
+
parser = argparse.ArgumentParser(description=f"Runs read-only checks on a linux-server to detect signs of a compromise or of suspicious activities (for example backdoor-accounts, processes running from temporary folders, reverse-shells, crypto-miners, persistence in cron or systemd, unexpected setuid-files, modified package-files, rootkit-indicators). Nothing is changed on the system. Must be executed as root. Every finding is printed to stdout as one line with the prefix \"[High]\", \"[Mid]\" or \"[Low]\". Exit-code: the amount of high-findings (capped at {LinuxServerSecurityChecker.maximal_exit_code_for_findings}), or {LinuxServerSecurityChecker.exit_code_for_application_error} if an error occurred in the check itself.")
|
|
1460
|
+
parser.add_argument('-o', '--outputfile', required=False, default=None, help="Optional file to which the findings are additionally appended (with timestamp).")
|
|
1461
|
+
verbosity_values = ", ".join(f"{lvl.value}={lvl.name}" for lvl in LogLevel)
|
|
1462
|
+
parser.add_argument('-v', '--verbosity', required=False, default=3, help=f"Sets the loglevel. Possible values: {verbosity_values}")
|
|
1463
|
+
args = parser.parse_args()
|
|
1464
|
+
try:
|
|
1465
|
+
output_file = GeneralUtilities.resolve_relative_path(args.outputfile, os.getcwd()) if args.outputfile is not None else None
|
|
1466
|
+
log: SCLog = SCLog(output_file, LogLevel(int(args.verbosity)))
|
|
1467
|
+
log.add_overhead_to_logfile = True
|
|
1468
|
+
return LinuxServerSecurityChecker(log, LinuxSystemAccess(), LinuxServerSecurityChecker.get_default_checks()).run()
|
|
1469
|
+
except Exception as exception: # pylint:disable=broad-exception-caught
|
|
1470
|
+
# an error must be distinguishable from findings, because "nothing found" and "the check could not be done" have opposite meanings for the caller.
|
|
1471
|
+
GeneralUtilities.write_message_to_stderr(f"The security-check could not be executed: {exception}\n{traceback.format_exc()}")
|
|
1472
|
+
return LinuxServerSecurityChecker.exit_code_for_application_error
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
from ..SecurityCheckBase import SecurityCheckBase
|
|
2
|
+
from ..SecurityFinding import SecurityFinding, SecurityFindingSeverity
|
|
3
|
+
from ..SystemAccessBase import SystemAccessBase
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class NonRootAccountsWithUid0Check(SecurityCheckBase):
|
|
7
|
+
"""Every account with uid 0 has root-privileges. Attackers add such accounts as backdoor."""
|
|
8
|
+
|
|
9
|
+
def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
|
|
10
|
+
return [self.create_finding(SecurityFindingSeverity.High, f"Account \"{user.name}\" has uid 0 (root-privileges).") for user in system.get_users() if user.uid == 0 and user.name != "root"]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class AccountsWithoutPasswordCheck(SecurityCheckBase):
|
|
14
|
+
"""An empty password-field in /etc/shadow allows a login without password."""
|
|
15
|
+
|
|
16
|
+
def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
|
|
17
|
+
content = system.read_text("/etc/shadow")
|
|
18
|
+
if content is None:
|
|
19
|
+
return []
|
|
20
|
+
result: list[SecurityFinding] = []
|
|
21
|
+
for line in content.splitlines():
|
|
22
|
+
fields = line.split(":")
|
|
23
|
+
if len(fields) >= 2 and fields[0] != "" and fields[1] == "":
|
|
24
|
+
result.append(self.create_finding(SecurityFindingSeverity.High, f"Account \"{fields[0]}\" has an empty password in /etc/shadow (login without password possible)."))
|
|
25
|
+
return result
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class DuplicateUidsCheck(SecurityCheckBase):
|
|
29
|
+
"""Multiple accounts with the same uid share all permissions, which can be used to hide a backdoor-account."""
|
|
30
|
+
|
|
31
|
+
def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
|
|
32
|
+
accounts_by_uid: dict[int, list[str]] = {}
|
|
33
|
+
for user in system.get_users():
|
|
34
|
+
accounts_by_uid.setdefault(user.uid, []).append(user.name)
|
|
35
|
+
return [self.create_finding(SecurityFindingSeverity.Mid, f"Uid {uid} is used by multiple accounts: {', '.join(names)}.") for uid, names in accounts_by_uid.items() if len(names) > 1 and uid != 0]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class SystemAccountsWithLoginShellCheck(SecurityCheckBase):
|
|
39
|
+
"""System-accounts (for example www-data) usually have no login-shell. A login-shell can be a sign of a manipulated account."""
|
|
40
|
+
|
|
41
|
+
__non_login_shells: tuple[str, ...] = ("nologin", "false", "sync", "shutdown", "halt")
|
|
42
|
+
|
|
43
|
+
def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
|
|
44
|
+
minimal_uid = system.get_minimal_uid_of_regular_users()
|
|
45
|
+
result: list[SecurityFinding] = []
|
|
46
|
+
for user in system.get_users():
|
|
47
|
+
shell_name = user.shell.rsplit("/", 1)[-1]
|
|
48
|
+
if 0 < user.uid < minimal_uid and user.shell != "" and shell_name not in SystemAccountsWithLoginShellCheck.__non_login_shells:
|
|
49
|
+
result.append(self.create_finding(SecurityFindingSeverity.Low, f"System-account \"{user.name}\" (uid {user.uid}) has the login-shell \"{user.shell}\"."))
|
|
50
|
+
return result
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class SudoersWithoutPasswordCheck(SecurityCheckBase):
|
|
54
|
+
"""NOPASSWD-rules allow privilege-escalation without knowing a password."""
|
|
55
|
+
|
|
56
|
+
def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
|
|
57
|
+
files: list[str] = []
|
|
58
|
+
if system.is_regular_file("/etc/sudoers"):
|
|
59
|
+
files.append("/etc/sudoers")
|
|
60
|
+
if system.is_folder("/etc/sudoers.d"):
|
|
61
|
+
files.extend(system.list_regular_files_recursively("/etc/sudoers.d"))
|
|
62
|
+
result: list[SecurityFinding] = []
|
|
63
|
+
for file in files:
|
|
64
|
+
content = system.read_text(file)
|
|
65
|
+
if content is None:
|
|
66
|
+
continue
|
|
67
|
+
for line_number, line in enumerate(content.splitlines(), start=1):
|
|
68
|
+
stripped_line = line.strip()
|
|
69
|
+
if not stripped_line.startswith("#") and "NOPASSWD" in stripped_line:
|
|
70
|
+
result.append(self.create_finding(SecurityFindingSeverity.Low, f"Sudo-rule without password in \"{file}\" line {line_number}: {stripped_line}"))
|
|
71
|
+
return result
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from ..SecurityCheckBase import SecurityCheckBase
|
|
3
|
+
from ..SecurityFinding import SecurityFinding, SecurityFindingSeverity
|
|
4
|
+
from ..SystemAccessBase import SystemAccessBase
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class PrivilegedDockerContainersCheck(SecurityCheckBase):
|
|
8
|
+
"""A privileged container, a container in the pid-namespace of the host or a container with the root-folder of the host mounted
|
|
9
|
+
has full access to the host. Attackers who can access the docker-daemon use such containers to get root on the host."""
|
|
10
|
+
|
|
11
|
+
def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
|
|
12
|
+
if not system.program_is_available("docker"):
|
|
13
|
+
return []
|
|
14
|
+
container_ids_result = system.run_program("docker", ["ps", "--quiet", "--no-trunc"])
|
|
15
|
+
if container_ids_result.exit_code != 0:
|
|
16
|
+
return [self.create_finding(SecurityFindingSeverity.Low, f"Docker-containers can not be checked because \"docker ps\" failed: {container_ids_result.stderr.strip()}")]
|
|
17
|
+
container_ids = [line.strip() for line in container_ids_result.stdout.splitlines() if line.strip() != ""]
|
|
18
|
+
if len(container_ids) == 0:
|
|
19
|
+
return []
|
|
20
|
+
inspect_result = system.run_program("docker", ["inspect"] + container_ids)
|
|
21
|
+
if inspect_result.exit_code != 0:
|
|
22
|
+
raise ValueError(f"\"docker inspect\" failed with exitcode {inspect_result.exit_code}: {inspect_result.stderr.strip()}")
|
|
23
|
+
result: list[SecurityFinding] = []
|
|
24
|
+
for container in json.loads(inspect_result.stdout):
|
|
25
|
+
name = str(container.get("Name", "")).lstrip("/")
|
|
26
|
+
host_configuration = container.get("HostConfig") or {}
|
|
27
|
+
if host_configuration.get("Privileged") is True:
|
|
28
|
+
result.append(self.create_finding(SecurityFindingSeverity.Low, f"Docker-container \"{name}\" runs privileged."))
|
|
29
|
+
if host_configuration.get("PidMode") == "host":
|
|
30
|
+
result.append(self.create_finding(SecurityFindingSeverity.Low, f"Docker-container \"{name}\" runs in the pid-namespace of the host."))
|
|
31
|
+
for mount in container.get("Mounts") or []:
|
|
32
|
+
if mount.get("Source") == "/":
|
|
33
|
+
result.append(self.create_finding(SecurityFindingSeverity.Mid, f"Docker-container \"{name}\" has the root-folder of the host mounted at \"{mount.get('Destination')}\"."))
|
|
34
|
+
return result
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import re
|
|
2
|
+
import stat
|
|
3
|
+
from ..PackageOwnershipLookup import PackageOwnershipLookup
|
|
4
|
+
from ..SecurityCheckBase import SecurityCheckBase
|
|
5
|
+
from ..SecurityFinding import SecurityFinding, SecurityFindingSeverity
|
|
6
|
+
from ..SuspiciousCommandPatterns import SuspiciousCommandPatterns
|
|
7
|
+
from ..SystemAccessBase import SystemAccessBase
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class SetuidFilesCheck(SecurityCheckBase):
|
|
11
|
+
"""Setuid-/setgid-files allow privilege-escalation. Attackers place them (for example a copy of a shell) as backdoor.
|
|
12
|
+
The whole filesystem is searched except pseudo-filesystems, network-filesystems and the storage of container-runtimes
|
|
13
|
+
(whose images legitimately contain setuid-files like "su")."""
|
|
14
|
+
|
|
15
|
+
excluded_folders: set[str] = {"/proc", "/sys", "/dev", "/var/lib/docker", "/var/lib/containerd", "/var/lib/containers", "/var/lib/lxc", "/var/lib/lxd", "/snap", "/var/snap", "/var/lib/snapd"}
|
|
16
|
+
included_folders_below_excluded_folders: list[str] = ["/dev/shm"]
|
|
17
|
+
excluded_filesystem_types: set[str] = {"proc", "sysfs", "devtmpfs", "devpts", "cgroup", "cgroup2", "securityfs", "debugfs", "tracefs", "pstore", "bpf", "configfs", "fusectl", "mqueue", "hugetlbfs", "autofs", "binfmt_misc", "nsfs", "overlay", "squashfs", "efivarfs", "rpc_pipefs",
|
|
18
|
+
"nfs", "nfs4", "cifs", "smb3", "smbfs", "sshfs", "9p", "ceph", "glusterfs"}
|
|
19
|
+
standard_folders: tuple[str, ...] = ("/usr/bin/", "/usr/sbin/", "/bin/", "/sbin/", "/usr/lib/", "/usr/lib32/", "/usr/lib64/", "/usr/libexec/", "/lib/", "/lib32/", "/lib64/")
|
|
20
|
+
manually_installed_software_folders: tuple[str, ...] = ("/usr/local/", "/opt/")
|
|
21
|
+
|
|
22
|
+
def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
|
|
23
|
+
package_ownership = PackageOwnershipLookup(system)
|
|
24
|
+
result: list[SecurityFinding] = []
|
|
25
|
+
excluded_folders = self.__get_excluded_folders(system)
|
|
26
|
+
for start_folder in ["/"] + SetuidFilesCheck.included_folders_below_excluded_folders:
|
|
27
|
+
for path, file_stat in system.list_files_recursively(start_folder, excluded_folders):
|
|
28
|
+
if not stat.S_ISREG(file_stat.st_mode) or file_stat.st_mode & (stat.S_ISUID | stat.S_ISGID) == 0:
|
|
29
|
+
continue
|
|
30
|
+
kind = "setuid" if file_stat.st_mode & stat.S_ISUID != 0 else "setgid"
|
|
31
|
+
description = f"{kind}-file \"{path}\" (owner-uid {file_stat.st_uid}, mode {oct(stat.S_IMODE(file_stat.st_mode))})"
|
|
32
|
+
if SuspiciousCommandPatterns.is_in_temporary_folder(path) or SuspiciousCommandPatterns.contains_hidden_path_component(path):
|
|
33
|
+
result.append(self.create_finding(SecurityFindingSeverity.High, f"{description} in a temporary or hidden folder."))
|
|
34
|
+
elif path.startswith(SetuidFilesCheck.standard_folders):
|
|
35
|
+
if package_ownership.is_available() and not package_ownership.is_owned_by_package(path):
|
|
36
|
+
result.append(self.create_finding(SecurityFindingSeverity.High, f"{description} is not owned by any installed package."))
|
|
37
|
+
elif path.startswith(SetuidFilesCheck.manually_installed_software_folders):
|
|
38
|
+
result.append(self.create_finding(SecurityFindingSeverity.Low, f"{description} in a folder for manually installed software."))
|
|
39
|
+
else:
|
|
40
|
+
result.append(self.create_finding(SecurityFindingSeverity.Mid, f"{description} in an unusual location."))
|
|
41
|
+
return result
|
|
42
|
+
|
|
43
|
+
def __get_excluded_folders(self, system: SystemAccessBase) -> set[str]:
|
|
44
|
+
result = set(SetuidFilesCheck.excluded_folders)
|
|
45
|
+
mounts = system.read_text("/proc/mounts")
|
|
46
|
+
if mounts is None:
|
|
47
|
+
raise FileNotFoundError("/proc/mounts does not exist.")
|
|
48
|
+
for line in mounts.splitlines():
|
|
49
|
+
fields = line.split()
|
|
50
|
+
if len(fields) >= 3 and (fields[2] in SetuidFilesCheck.excluded_filesystem_types or fields[2].startswith("fuse.")):
|
|
51
|
+
# /proc/mounts escapes whitespaces as octal-sequences (for example "\040").
|
|
52
|
+
result.add(re.sub(r"\\([0-7]{3})", lambda match: chr(int(match.group(1), 8)), fields[1]))
|
|
53
|
+
result.discard("/")
|
|
54
|
+
return result
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class SuspiciousFilesInTemporaryFoldersCheck(SecurityCheckBase):
|
|
58
|
+
"""Attackers drop their tools into world-writable temporary folders, often hidden (name starting with ".") or with misleading names like "..." ."""
|
|
59
|
+
|
|
60
|
+
temporary_folders: list[str] = ["/tmp", "/var/tmp", "/dev/shm"]
|
|
61
|
+
usual_hidden_entries: set[str] = {".X11-unix", ".ICE-unix", ".font-unix", ".XIM-unix", ".Test-unix"}
|
|
62
|
+
__usual_hidden_entry_pattern = re.compile(r"^(\.X[0-9]+-lock|\.veracrypt_aux_mnt[0-9]+)$")
|
|
63
|
+
__misleading_name_pattern = re.compile(r"^[.\s]+$")
|
|
64
|
+
__elf_magic: bytes = b"\x7fELF"
|
|
65
|
+
|
|
66
|
+
def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
|
|
67
|
+
result: list[SecurityFinding] = []
|
|
68
|
+
for temporary_folder in SuspiciousFilesInTemporaryFoldersCheck.temporary_folders:
|
|
69
|
+
for entry in sorted(system.list_folder(temporary_folder) or []):
|
|
70
|
+
if SuspiciousFilesInTemporaryFoldersCheck.__misleading_name_pattern.match(entry):
|
|
71
|
+
result.append(self.create_finding(SecurityFindingSeverity.High, f"Entry with misleading name \"{temporary_folder}/{entry}\" (only dots and whitespaces)."))
|
|
72
|
+
elif entry.startswith(".") and entry not in SuspiciousFilesInTemporaryFoldersCheck.usual_hidden_entries and not SuspiciousFilesInTemporaryFoldersCheck.__usual_hidden_entry_pattern.match(entry):
|
|
73
|
+
result.append(self.create_finding(SecurityFindingSeverity.Mid, f"Hidden entry \"{temporary_folder}/{entry}\" in a temporary folder."))
|
|
74
|
+
for path, file_stat in system.list_files_recursively(temporary_folder, set()):
|
|
75
|
+
if not stat.S_ISREG(file_stat.st_mode):
|
|
76
|
+
continue
|
|
77
|
+
header = system.read_bytes(path, 4)
|
|
78
|
+
if header == SuspiciousFilesInTemporaryFoldersCheck.__elf_magic:
|
|
79
|
+
severity = SecurityFindingSeverity.High if SuspiciousCommandPatterns.contains_hidden_path_component(path) else SecurityFindingSeverity.Mid
|
|
80
|
+
result.append(self.create_finding(severity, f"Executable binary (ELF) \"{path}\" (owner-uid {file_stat.st_uid}, size {file_stat.st_size} bytes) in a temporary folder."))
|
|
81
|
+
return result
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
class CriticalFilePermissionsCheck(SecurityCheckBase):
|
|
85
|
+
"""Wrong permissions of critical files allow privilege-escalation or reading of password-hashes."""
|
|
86
|
+
|
|
87
|
+
files_which_must_not_be_world_writable: list[str] = ["/etc/passwd", "/etc/group", "/etc/sudoers", "/etc/crontab", "/etc/ld.so.preload", "/etc/rc.local", "/etc/profile", "/etc/bash.bashrc", "/etc/environment", "/etc/ssh/sshd_config"]
|
|
88
|
+
folders_whose_files_must_not_be_world_writable: list[str] = ["/etc/sudoers.d", "/etc/cron.d", "/etc/cron.hourly", "/etc/cron.daily", "/etc/cron.weekly", "/etc/cron.monthly", "/etc/systemd/system", "/etc/profile.d", "/etc/pam.d", "/etc/ssh/sshd_config.d", "/root"]
|
|
89
|
+
files_which_must_not_be_world_readable: list[str] = ["/etc/shadow", "/etc/gshadow"]
|
|
90
|
+
|
|
91
|
+
def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
|
|
92
|
+
result: list[SecurityFinding] = []
|
|
93
|
+
world_writable_candidates: list[str] = list(CriticalFilePermissionsCheck.files_which_must_not_be_world_writable)
|
|
94
|
+
for folder in CriticalFilePermissionsCheck.folders_whose_files_must_not_be_world_writable:
|
|
95
|
+
if system.is_folder(folder):
|
|
96
|
+
world_writable_candidates.append(folder)
|
|
97
|
+
world_writable_candidates.extend(system.list_regular_files_recursively(folder))
|
|
98
|
+
for home_folder in system.get_home_folders():
|
|
99
|
+
world_writable_candidates.extend(f"{home_folder.rstrip('/')}/.ssh/{file}" for file in ["authorized_keys", "authorized_keys2"])
|
|
100
|
+
for path in world_writable_candidates:
|
|
101
|
+
file_stat = system.get_lstat(path)
|
|
102
|
+
if file_stat is None or stat.S_ISLNK(file_stat.st_mode):
|
|
103
|
+
continue
|
|
104
|
+
if stat.S_ISDIR(file_stat.st_mode) and file_stat.st_mode & stat.S_ISVTX != 0:
|
|
105
|
+
continue
|
|
106
|
+
if file_stat.st_mode & stat.S_IWOTH != 0:
|
|
107
|
+
result.append(self.create_finding(SecurityFindingSeverity.High, f"\"{path}\" is writable for everybody (mode {oct(stat.S_IMODE(file_stat.st_mode))})."))
|
|
108
|
+
for path in CriticalFilePermissionsCheck.files_which_must_not_be_world_readable:
|
|
109
|
+
file_stat = system.get_lstat(path)
|
|
110
|
+
if file_stat is not None and file_stat.st_mode & (stat.S_IROTH | stat.S_IWOTH) != 0:
|
|
111
|
+
result.append(self.create_finding(SecurityFindingSeverity.High, f"\"{path}\" is readable or writable for everybody (mode {oct(stat.S_IMODE(file_stat.st_mode))})."))
|
|
112
|
+
return result
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
class ImmutableFilesCheck(SecurityCheckBase):
|
|
116
|
+
"""Attackers make their persistence-files immutable (chattr +i) so that they can not be removed easily."""
|
|
117
|
+
|
|
118
|
+
__flag_immutable: int = 0x10
|
|
119
|
+
__flag_append_only: int = 0x20
|
|
120
|
+
files: list[str] = ["/etc/passwd", "/etc/shadow", "/etc/group", "/etc/sudoers", "/etc/crontab", "/etc/ld.so.preload", "/etc/rc.local", "/etc/hosts", "/etc/resolv.conf", "/etc/ssh/sshd_config"]
|
|
121
|
+
folders: list[str] = ["/etc/cron.d", "/var/spool/cron", "/etc/sudoers.d", "/etc/systemd/system", "/etc/profile.d"]
|
|
122
|
+
|
|
123
|
+
def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
|
|
124
|
+
candidates: list[str] = list(ImmutableFilesCheck.files)
|
|
125
|
+
for folder in ImmutableFilesCheck.folders:
|
|
126
|
+
if system.is_folder(folder):
|
|
127
|
+
candidates.extend(system.list_regular_files_recursively(folder))
|
|
128
|
+
for home_folder in system.get_home_folders():
|
|
129
|
+
candidates.extend(f"{home_folder.rstrip('/')}/.ssh/{file}" for file in ["authorized_keys", "authorized_keys2"])
|
|
130
|
+
result: list[SecurityFinding] = []
|
|
131
|
+
for path in candidates:
|
|
132
|
+
if not system.is_regular_file(path):
|
|
133
|
+
continue
|
|
134
|
+
flags = system.get_file_attribute_flags(path)
|
|
135
|
+
if flags is None:
|
|
136
|
+
continue
|
|
137
|
+
if flags & ImmutableFilesCheck.__flag_immutable != 0:
|
|
138
|
+
result.append(self.create_finding(SecurityFindingSeverity.Mid, f"\"{path}\" is immutable (chattr +i)."))
|
|
139
|
+
if flags & ImmutableFilesCheck.__flag_append_only != 0:
|
|
140
|
+
result.append(self.create_finding(SecurityFindingSeverity.Mid, f"\"{path}\" is append-only (chattr +a)."))
|
|
141
|
+
return result
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
from ..SecurityCheckBase import SecurityCheckBase
|
|
2
|
+
from ..SecurityFinding import SecurityFinding, SecurityFindingSeverity
|
|
3
|
+
from ..SystemAccessBase import SystemAccessBase
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class UnknownKernelModulesCheck(SecurityCheckBase):
|
|
7
|
+
"""A loaded kernel-module which is not part of the module-tree of the running kernel was loaded manually from somewhere else (for example by a rootkit via insmod)."""
|
|
8
|
+
|
|
9
|
+
def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
|
|
10
|
+
loaded_modules = system.read_text("/proc/modules")
|
|
11
|
+
if loaded_modules is None:
|
|
12
|
+
return [] # kernel without support for loadable modules
|
|
13
|
+
kernel_release = system.read_text("/proc/sys/kernel/osrelease")
|
|
14
|
+
if kernel_release is None:
|
|
15
|
+
raise FileNotFoundError("/proc/sys/kernel/osrelease does not exist.")
|
|
16
|
+
modules_dep_file = f"/lib/modules/{kernel_release.strip()}/modules.dep"
|
|
17
|
+
modules_dep = system.read_text(modules_dep_file)
|
|
18
|
+
loaded_module_names = [line.split()[0] for line in loaded_modules.splitlines() if line.strip() != ""]
|
|
19
|
+
if modules_dep is None:
|
|
20
|
+
if len(loaded_module_names) == 0:
|
|
21
|
+
return []
|
|
22
|
+
return [self.create_finding(SecurityFindingSeverity.Low, f"Loaded kernel-modules can not be verified because \"{modules_dep_file}\" does not exist.")]
|
|
23
|
+
known_module_names: set[str] = set()
|
|
24
|
+
for line in modules_dep.splitlines():
|
|
25
|
+
module_path = line.split(":", 1)[0].strip()
|
|
26
|
+
if module_path != "":
|
|
27
|
+
known_module_names.add(UnknownKernelModulesCheck.__normalize(module_path.rsplit("/", 1)[-1].split(".", 1)[0]))
|
|
28
|
+
return [self.create_finding(SecurityFindingSeverity.Mid, f"Loaded kernel-module \"{name}\" is not part of the module-tree of the running kernel ({modules_dep_file}).")
|
|
29
|
+
for name in loaded_module_names if UnknownKernelModulesCheck.__normalize(name) not in known_module_names]
|
|
30
|
+
|
|
31
|
+
@staticmethod
|
|
32
|
+
def __normalize(module_name: str) -> str:
|
|
33
|
+
return module_name.replace("-", "_")
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class KernelTaintCheck(SecurityCheckBase):
|
|
37
|
+
"""The kernel is tainted when proprietary, out-of-tree or unsigned modules were loaded, which is also the case for many rootkits."""
|
|
38
|
+
|
|
39
|
+
__relevant_flags: dict[int, str] = {0: "proprietary module was loaded", 12: "out-of-tree module was loaded", 13: "unsigned module was loaded"}
|
|
40
|
+
|
|
41
|
+
def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
|
|
42
|
+
content = system.read_text("/proc/sys/kernel/tainted")
|
|
43
|
+
if content is None:
|
|
44
|
+
return []
|
|
45
|
+
taint = int(content.strip())
|
|
46
|
+
reasons = [description for bit, description in KernelTaintCheck.__relevant_flags.items() if taint & (1 << bit) != 0]
|
|
47
|
+
if len(reasons) == 0:
|
|
48
|
+
return []
|
|
49
|
+
return [self.create_finding(SecurityFindingSeverity.Low, f"Kernel is tainted (value {taint}): {', '.join(reasons)}.")]
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from ..SecurityCheckBase import SecurityCheckBase
|
|
2
|
+
from ..SecurityFinding import SecurityFinding, SecurityFindingSeverity
|
|
3
|
+
from ..SystemAccessBase import SystemAccessBase
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class RedirectedShellHistoriesCheck(SecurityCheckBase):
|
|
7
|
+
"""Attackers link the shell-history to /dev/null to hide their commands."""
|
|
8
|
+
|
|
9
|
+
history_files: list[str] = [".bash_history", ".zsh_history", ".sh_history", ".ash_history", ".history"]
|
|
10
|
+
|
|
11
|
+
def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
|
|
12
|
+
result: list[SecurityFinding] = []
|
|
13
|
+
for home_folder in system.get_home_folders():
|
|
14
|
+
for history_file in RedirectedShellHistoriesCheck.history_files:
|
|
15
|
+
path = f"{home_folder.rstrip('/')}/{history_file}"
|
|
16
|
+
if system.is_symlink(path):
|
|
17
|
+
target = system.read_link(path)
|
|
18
|
+
if target == "/dev/null":
|
|
19
|
+
result.append(self.create_finding(SecurityFindingSeverity.Mid, f"Shell-history \"{path}\" is linked to /dev/null."))
|
|
20
|
+
return result
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class EmptyLoginRecordsCheck(SecurityCheckBase):
|
|
24
|
+
"""Attackers clear the login-records to hide their logins.
|
|
25
|
+
Because logrotate creates an empty file when it rotates, an empty file is only reported if no rotated file exists."""
|
|
26
|
+
|
|
27
|
+
login_record_files: list[str] = ["/var/log/wtmp", "/var/log/lastlog"]
|
|
28
|
+
|
|
29
|
+
def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
|
|
30
|
+
result: list[SecurityFinding] = []
|
|
31
|
+
for path in EmptyLoginRecordsCheck.login_record_files:
|
|
32
|
+
file_stat = system.get_lstat(path)
|
|
33
|
+
if file_stat is not None and file_stat.st_size == 0 and not self.__rotated_file_exists(system, path):
|
|
34
|
+
result.append(self.create_finding(SecurityFindingSeverity.Mid, f"Login-records \"{path}\" are empty although they were never rotated."))
|
|
35
|
+
return result
|
|
36
|
+
|
|
37
|
+
def __rotated_file_exists(self, system: SystemAccessBase, path: str) -> bool:
|
|
38
|
+
folder, file_name = path.rsplit("/", 1)
|
|
39
|
+
return any(entry != file_name and entry.startswith(file_name + ".") for entry in system.list_folder(folder) or [])
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
from ..SecurityCheckBase import SecurityCheckBase
|
|
2
|
+
from ..SecurityFinding import SecurityFinding, SecurityFindingSeverity
|
|
3
|
+
from ..SystemAccessBase import SystemAccessBase
|
|
4
|
+
from ..SystemEntities import SocketInformation, ProcessInformation
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def _describe_socket(socket_information: SocketInformation, processes_by_inode: dict[int, list[ProcessInformation]]) -> str:
|
|
8
|
+
processes = processes_by_inode.get(socket_information.inode, [])
|
|
9
|
+
owner = "; ".join(process.describe() for process in processes) if len(processes) > 0 else "unknown process"
|
|
10
|
+
return f"{socket_information.protocol} {socket_information.local_address}:{socket_information.local_port} -> {socket_information.remote_address}:{socket_information.remote_port} (owner: {owner})"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class PromiscuousNetworkInterfacesCheck(SecurityCheckBase):
|
|
14
|
+
"""A network-interface in promiscuous mode receives all packets, which is used for sniffing.
|
|
15
|
+
Interfaces which are ports of a bridge (for example the veth-interfaces of docker) are promiscuous by design and therefore ignored."""
|
|
16
|
+
|
|
17
|
+
__flag_promiscuous: int = 0x100
|
|
18
|
+
|
|
19
|
+
def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
|
|
20
|
+
result: list[SecurityFinding] = []
|
|
21
|
+
for interface in sorted(system.list_folder("/sys/class/net") or []):
|
|
22
|
+
flags = system.read_text(f"/sys/class/net/{interface}/flags")
|
|
23
|
+
if flags is None or system.path_exists(f"/sys/class/net/{interface}/brport"):
|
|
24
|
+
continue
|
|
25
|
+
if int(flags.strip(), 16) & PromiscuousNetworkInterfacesCheck.__flag_promiscuous != 0:
|
|
26
|
+
result.append(self.create_finding(SecurityFindingSeverity.Mid, f"Network-interface \"{interface}\" is in promiscuous mode (possible sniffing)."))
|
|
27
|
+
return result
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class SuspiciousListeningPortsCheck(SecurityCheckBase):
|
|
31
|
+
"""Ports which are typically used by backdoors and botnets."""
|
|
32
|
+
|
|
33
|
+
suspicious_ports: set[int] = {1337, 1524, 4444, 6666, 6667, 12345, 27374, 31337, 54321}
|
|
34
|
+
__state_tcp_listening: str = "0A"
|
|
35
|
+
__state_udp_unconnected: str = "07"
|
|
36
|
+
|
|
37
|
+
def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
|
|
38
|
+
processes_by_inode: dict[int, list[ProcessInformation]] | None = None
|
|
39
|
+
result: list[SecurityFinding] = []
|
|
40
|
+
for socket_information in system.get_sockets():
|
|
41
|
+
is_listening = (socket_information.protocol.startswith("tcp") and socket_information.state == SuspiciousListeningPortsCheck.__state_tcp_listening) or (socket_information.protocol.startswith("udp") and socket_information.state == SuspiciousListeningPortsCheck.__state_udp_unconnected)
|
|
42
|
+
if is_listening and socket_information.local_port in SuspiciousListeningPortsCheck.suspicious_ports:
|
|
43
|
+
if processes_by_inode is None:
|
|
44
|
+
processes_by_inode = system.get_processes_by_socket_inode()
|
|
45
|
+
result.append(self.create_finding(SecurityFindingSeverity.Mid, f"Listening on a port which is typical for backdoors: {_describe_socket(socket_information, processes_by_inode)}"))
|
|
46
|
+
return result
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class ConnectionsToMiningPoolPortsCheck(SecurityCheckBase):
|
|
50
|
+
"""Established outgoing connections to ports which are typically used by crypto-mining-pools."""
|
|
51
|
+
|
|
52
|
+
mining_pool_ports: set[int] = {3333, 3334, 4444, 5555, 7777, 14433, 14444, 45560, 45700}
|
|
53
|
+
__state_tcp_established: str = "01"
|
|
54
|
+
__state_tcp_listening: str = "0A"
|
|
55
|
+
|
|
56
|
+
def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
|
|
57
|
+
processes_by_inode: dict[int, list[ProcessInformation]] | None = None
|
|
58
|
+
result: list[SecurityFinding] = []
|
|
59
|
+
sockets = system.get_sockets()
|
|
60
|
+
# a connection whose local port is a listening port is an incoming connection, so its remote port is only the ephemeral port of the client.
|
|
61
|
+
listening_ports = {socket_information.local_port for socket_information in sockets if socket_information.protocol.startswith("tcp") and socket_information.state == ConnectionsToMiningPoolPortsCheck.__state_tcp_listening}
|
|
62
|
+
for socket_information in sockets:
|
|
63
|
+
if socket_information.protocol.startswith("tcp") and socket_information.state == ConnectionsToMiningPoolPortsCheck.__state_tcp_established and socket_information.remote_port in ConnectionsToMiningPoolPortsCheck.mining_pool_ports and socket_information.local_port not in listening_ports:
|
|
64
|
+
if processes_by_inode is None:
|
|
65
|
+
processes_by_inode = system.get_processes_by_socket_inode()
|
|
66
|
+
result.append(self.create_finding(SecurityFindingSeverity.Mid, f"Connection to a port which is typical for crypto-mining-pools: {_describe_socket(socket_information, processes_by_inode)}"))
|
|
67
|
+
return result
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
from ..PackageOwnershipLookup import PackageOwnershipLookup
|
|
2
|
+
from ..SecurityCheckBase import SecurityCheckBase
|
|
3
|
+
from ..SecurityFinding import SecurityFinding, SecurityFindingSeverity
|
|
4
|
+
from ..SystemAccessBase import SystemAccessBase
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class ModifiedPackageFilesCheck(SecurityCheckBase):
|
|
8
|
+
"""Compares the checksums of the installed files with the checksums of the package-manager.
|
|
9
|
+
Rootkits often replace programs like ps, ls, netstat or ssh, or PAM-modules."""
|
|
10
|
+
|
|
11
|
+
executable_folders: tuple[str, ...] = ("/usr/bin/", "/usr/sbin/", "/bin/", "/sbin/")
|
|
12
|
+
library_folders: tuple[str, ...] = ("/usr/lib/", "/usr/lib32/", "/usr/lib64/", "/usr/libexec/", "/lib/", "/lib32/", "/lib64/")
|
|
13
|
+
|
|
14
|
+
def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
|
|
15
|
+
if system.program_is_available("dpkg"):
|
|
16
|
+
program_result = system.run_program("dpkg", ["--verify"])
|
|
17
|
+
elif system.program_is_available("rpm"):
|
|
18
|
+
program_result = system.run_program("rpm", ["-Va", "--nomtime"])
|
|
19
|
+
else:
|
|
20
|
+
return []
|
|
21
|
+
result: list[SecurityFinding] = []
|
|
22
|
+
for line in program_result.stdout.splitlines():
|
|
23
|
+
fields = line.split()
|
|
24
|
+
if len(fields) < 2 or len(fields[0]) < 9:
|
|
25
|
+
continue
|
|
26
|
+
flags = fields[0]
|
|
27
|
+
is_configuration_file = len(fields) >= 3 and fields[1] == "c"
|
|
28
|
+
path = fields[-1]
|
|
29
|
+
if flags[2] != "5" or is_configuration_file:
|
|
30
|
+
continue
|
|
31
|
+
if path.startswith(ModifiedPackageFilesCheck.executable_folders) or "/security/pam_" in path:
|
|
32
|
+
result.append(self.create_finding(SecurityFindingSeverity.High, f"Content of \"{path}\" differs from the installed package (verification-flags \"{flags}\")."))
|
|
33
|
+
elif path.startswith(ModifiedPackageFilesCheck.library_folders):
|
|
34
|
+
result.append(self.create_finding(SecurityFindingSeverity.Mid, f"Content of \"{path}\" differs from the installed package (verification-flags \"{flags}\")."))
|
|
35
|
+
return result
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class UnpackagedPamModulesCheck(SecurityCheckBase):
|
|
39
|
+
"""A PAM-module which does not belong to a package can capture passwords or allow logins with a master-password."""
|
|
40
|
+
|
|
41
|
+
library_folders: list[str] = ["/usr/lib", "/usr/lib64", "/lib", "/lib64"]
|
|
42
|
+
|
|
43
|
+
def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
|
|
44
|
+
package_ownership = PackageOwnershipLookup(system)
|
|
45
|
+
if not package_ownership.is_available():
|
|
46
|
+
return []
|
|
47
|
+
result: list[SecurityFinding] = []
|
|
48
|
+
for folder in self.__get_pam_module_folders(system):
|
|
49
|
+
for entry in sorted(system.list_folder(folder) or []):
|
|
50
|
+
path = f"{folder}/{entry}"
|
|
51
|
+
if entry.endswith(".so") and system.is_regular_file(path) and not package_ownership.is_owned_by_package(path):
|
|
52
|
+
result.append(self.create_finding(SecurityFindingSeverity.High, f"PAM-module \"{path}\" is not owned by any installed package."))
|
|
53
|
+
return result
|
|
54
|
+
|
|
55
|
+
def __get_pam_module_folders(self, system: SystemAccessBase) -> list[str]:
|
|
56
|
+
result: list[str] = []
|
|
57
|
+
for library_folder in UnpackagedPamModulesCheck.library_folders:
|
|
58
|
+
if not system.is_folder(library_folder) or system.path_is_alias(f"{library_folder}/security"):
|
|
59
|
+
continue
|
|
60
|
+
candidates = [f"{library_folder}/security"] + [f"{library_folder}/{entry}/security" for entry in sorted(system.list_folder(library_folder) or [])]
|
|
61
|
+
result.extend(candidate for candidate in candidates if system.is_folder(candidate))
|
|
62
|
+
return result
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import datetime
|
|
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 AutostartLocations:
|
|
9
|
+
"""Files and folders which cause an automatic execution of commands (cron, systemd, shell-startup-files, ...)."""
|
|
10
|
+
|
|
11
|
+
system_files: list[str] = ["/etc/crontab", "/etc/rc.local", "/etc/profile", "/etc/bash.bashrc", "/etc/bashrc", "/etc/environment", "/etc/zsh/zshrc", "/etc/ssh/sshrc"]
|
|
12
|
+
system_folders: list[str] = ["/etc/cron.d", "/etc/cron.hourly", "/etc/cron.daily", "/etc/cron.weekly", "/etc/cron.monthly", "/var/spool/cron", "/etc/profile.d", "/etc/init.d", "/etc/update-motd.d", "/etc/udev/rules.d", "/etc/xdg/autostart",
|
|
13
|
+
"/etc/systemd/system", "/etc/systemd/user", "/run/systemd/system", "/usr/lib/systemd/system", "/usr/lib/systemd/user", "/lib/systemd/system", "/lib/systemd/user", "/usr/local/lib/systemd/system"]
|
|
14
|
+
user_files: list[str] = [".bashrc", ".bash_profile", ".bash_login", ".bash_logout", ".profile", ".zshrc", ".zprofile", ".ssh/rc"]
|
|
15
|
+
user_folders: list[str] = [".config/autostart", ".config/systemd/user"]
|
|
16
|
+
|
|
17
|
+
@staticmethod
|
|
18
|
+
def get_files(system: SystemAccessBase) -> list[str]:
|
|
19
|
+
files: list[str] = [file for file in AutostartLocations.system_files if system.is_regular_file(file)]
|
|
20
|
+
folders: list[str] = list(AutostartLocations.system_folders)
|
|
21
|
+
for home_folder in system.get_home_folders():
|
|
22
|
+
files.extend(f"{home_folder.rstrip('/')}/{file}" for file in AutostartLocations.user_files if system.is_regular_file(f"{home_folder.rstrip('/')}/{file}"))
|
|
23
|
+
folders.extend(f"{home_folder.rstrip('/')}/{folder}" for folder in AutostartLocations.user_folders)
|
|
24
|
+
for folder in folders:
|
|
25
|
+
if system.is_folder(folder) and not system.path_is_alias(folder):
|
|
26
|
+
files.extend(system.list_regular_files_recursively(folder))
|
|
27
|
+
return files
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class SuspiciousCommandsInAutostartLocationsCheck(SecurityCheckBase):
|
|
31
|
+
"""Detects persistence-mechanisms (download-and-execute, reverse-shells, miners, ...) in cron-jobs, systemd-units, shell-startup-files and similar."""
|
|
32
|
+
|
|
33
|
+
def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
|
|
34
|
+
result: list[SecurityFinding] = []
|
|
35
|
+
for file in AutostartLocations.get_files(system):
|
|
36
|
+
content = system.read_bytes(file, SystemAccessBase.maximal_size_of_text_files)
|
|
37
|
+
if content is None or system.is_binary_content(content):
|
|
38
|
+
continue
|
|
39
|
+
for line_number, line in enumerate(content.decode("utf-8", errors="replace").splitlines(), start=1):
|
|
40
|
+
if line.strip().startswith("#"):
|
|
41
|
+
continue
|
|
42
|
+
for pattern in SuspiciousCommandPatterns.find_matches(line):
|
|
43
|
+
result.append(self.create_finding(pattern.severity, f"Suspicious command ({pattern.description}) in \"{file}\" line {line_number}: {line.strip()[:300]}"))
|
|
44
|
+
return result
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class LibraryPreloadConfigurationCheck(SecurityCheckBase):
|
|
48
|
+
"""/etc/ld.so.preload loads a library into every process. This is the typical installation-location of user-space-rootkits."""
|
|
49
|
+
|
|
50
|
+
def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
|
|
51
|
+
content = system.read_text("/etc/ld.so.preload")
|
|
52
|
+
if content is None:
|
|
53
|
+
return []
|
|
54
|
+
entries = [line.strip() for line in content.splitlines() if line.strip() != "" and not line.strip().startswith("#")]
|
|
55
|
+
if len(entries) == 0:
|
|
56
|
+
return []
|
|
57
|
+
return [self.create_finding(SecurityFindingSeverity.High, f"/etc/ld.so.preload loads libraries into every process: {', '.join(entries)}")]
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class CrontabsOfSystemAccountsCheck(SecurityCheckBase):
|
|
61
|
+
"""System-accounts (for example www-data after a compromised web-application) usually have no crontab."""
|
|
62
|
+
|
|
63
|
+
__crontab_folders: list[str] = ["/var/spool/cron/crontabs", "/var/spool/cron"]
|
|
64
|
+
|
|
65
|
+
def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
|
|
66
|
+
minimal_uid = system.get_minimal_uid_of_regular_users()
|
|
67
|
+
system_accounts = {user.name: user.uid for user in system.get_users() if 0 < user.uid < minimal_uid}
|
|
68
|
+
result: list[SecurityFinding] = []
|
|
69
|
+
for folder in CrontabsOfSystemAccountsCheck.__crontab_folders:
|
|
70
|
+
for entry in sorted(system.list_folder(folder) or []):
|
|
71
|
+
file = f"{folder}/{entry}"
|
|
72
|
+
if entry in system_accounts and system.is_regular_file(file):
|
|
73
|
+
result.append(self.create_finding(SecurityFindingSeverity.Mid, f"System-account \"{entry}\" (uid {system_accounts[entry]}) has a crontab: \"{file}\"."))
|
|
74
|
+
return result
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class RecentlyChangedSecurityRelevantFilesCheck(SecurityCheckBase):
|
|
78
|
+
"""Lists security-relevant files which were changed recently.
|
|
79
|
+
The ctime is used instead of the mtime, because the mtime can be set arbitrarily (for example with "touch") while the ctime can not."""
|
|
80
|
+
|
|
81
|
+
amount_of_days: int = 7
|
|
82
|
+
files: list[str] = ["/etc/passwd", "/etc/shadow", "/etc/group", "/etc/gshadow", "/etc/sudoers", "/etc/crontab", "/etc/rc.local", "/etc/ld.so.preload", "/etc/ssh/sshd_config", "/etc/hosts", "/etc/resolv.conf"]
|
|
83
|
+
folders: list[str] = ["/etc/sudoers.d", "/etc/cron.d", "/etc/cron.hourly", "/etc/cron.daily", "/etc/cron.weekly", "/etc/cron.monthly", "/var/spool/cron", "/etc/systemd/system", "/etc/profile.d", "/etc/ssh/sshd_config.d", "/etc/pam.d", "/etc/ld.so.conf.d"]
|
|
84
|
+
|
|
85
|
+
def run(self, system: SystemAccessBase) -> list[SecurityFinding]:
|
|
86
|
+
candidates: list[str] = list(RecentlyChangedSecurityRelevantFilesCheck.files)
|
|
87
|
+
for folder in RecentlyChangedSecurityRelevantFilesCheck.folders:
|
|
88
|
+
if system.is_folder(folder):
|
|
89
|
+
candidates.extend(path for path, _ in system.list_files_recursively(folder, set()))
|
|
90
|
+
for home_folder in system.get_home_folders():
|
|
91
|
+
candidates.extend(f"{home_folder.rstrip('/')}/.ssh/{file}" for file in ["authorized_keys", "authorized_keys2"])
|
|
92
|
+
minimal_ctime = system.get_current_time() - RecentlyChangedSecurityRelevantFilesCheck.amount_of_days * SystemAccessBase.seconds_per_day
|
|
93
|
+
result: list[SecurityFinding] = []
|
|
94
|
+
for path in candidates:
|
|
95
|
+
file_stat = system.get_lstat(path)
|
|
96
|
+
if file_stat is not None and file_stat.st_ctime >= minimal_ctime:
|
|
97
|
+
moment = datetime.datetime.fromtimestamp(file_stat.st_ctime, datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
|
|
98
|
+
result.append(self.create_finding(SecurityFindingSeverity.Low, f"\"{path}\" was changed within the last {RecentlyChangedSecurityRelevantFilesCheck.amount_of_days} days (at {moment})."))
|
|
99
|
+
return result
|