Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Linux OS detection #809

Merged
merged 10 commits into from
Sep 3, 2024
7 changes: 3 additions & 4 deletions dissect/target/plugins/os/unix/linux/_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ def detect(cls, target: Target) -> Optional[Filesystem]:
fs.exists("/var")
and fs.exists("/etc")
and fs.exists("/opt")
or (fs.exists("/sys") or fs.exists("/proc"))
and not fs.exists("/Library")
or (fs.exists("/proc") or fs.exists("/sys"))
Horofic marked this conversation as resolved.
Show resolved Hide resolved
):
return fs
return None
if not (fs.exists("/windows") or fs.exists("/$MFT")) and not fs.exists("/Library"):
Horofic marked this conversation as resolved.
Show resolved Hide resolved
return fs

@export(property=True)
def ips(self) -> list[str]:
Expand Down
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ def fs_suse() -> Iterator[VirtualFilesystem]:
yield fs


@pytest.fixture
def fs_linux_sys(fs_linux: VirtualFilesystem) -> Iterator[VirtualFilesystem]:
fs_linux.makedirs("sys")
yield fs_linux


@pytest.fixture
def fs_linux_proc(fs_linux: VirtualFilesystem) -> Iterator[VirtualFilesystem]:
fs = fs_linux
Expand Down
30 changes: 30 additions & 0 deletions tests/plugins/os/unix/linux/test__os.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pathlib
from typing import Iterator

import pytest

from dissect.target import Target
from dissect.target.filesystem import VirtualFilesystem
from dissect.target.plugins.os.unix.linux._os import LinuxPlugin
from tests.conftest import make_os_target


@pytest.fixture
def target_linux_proc_sys(tmp_path: pathlib.Path) -> Iterator[Target]:
root_fs = VirtualFilesystem()
root_fs.makedirs("/proc")
root_fs.makedirs("/sys")
target_linux_proc_sys = make_os_target(tmp_path, LinuxPlugin, root_fs)

yield target_linux_proc_sys


def test_linux_os(target_linux: Target) -> None:
target_linux.add_plugin(LinuxPlugin)

assert target_linux.os == "linux"


def test_linux_os_proc_sys(target_linux_proc_sys: Target) -> None:
assert target_linux_proc_sys._os_plugin.detect(target_linux_proc_sys) is not None
assert target_linux_proc_sys.os == "linux"
25 changes: 24 additions & 1 deletion tests/plugins/os/windows/test__os.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from typing import Any, Optional
import pathlib
from typing import Any, Iterator, Optional

import pytest

from dissect.target.filesystem import Filesystem
from dissect.target.helpers.regutil import VirtualKey, VirtualValue
from dissect.target.plugins.os.unix.linux._os import LinuxPlugin
from dissect.target.plugins.os.windows._os import WindowsPlugin
from dissect.target.plugins.os.windows.registry import RegistryPlugin
from dissect.target.target import Target
from tests.conftest import make_mock_target


def current_version_key() -> str:
Expand All @@ -31,6 +35,17 @@ def win_plugin(version_target: Target):
return WindowsPlugin(version_target)


@pytest.fixture
def target_win_linux_folders(tmp_path: pathlib.Path, fs_win: Filesystem, fs_linux_sys: Filesystem) -> Iterator[Target]:
root_fs = fs_win
root_fs.mount("/", fs_linux_sys)

mock_target = next(make_mock_target(tmp_path))
mock_target.filesystems.add(root_fs)

yield mock_target


def map_version_value(target: Target, name: Optional[str], value: Any):
if name is not None:
hive = target.registry._root
Expand Down Expand Up @@ -235,3 +250,11 @@ def test_windowsplugin_version(
result = win_plugin.version

assert_value(result, value)


def test_windows_os_detection_with_linux_folders(target_win_linux_folders: Target) -> None:
fs_linux = LinuxPlugin.detect(target_win_linux_folders)
fs_windows = WindowsPlugin.detect(target_win_linux_folders)

assert fs_linux is None
assert fs_windows is not None
Loading