diff --git a/dissect/target/plugins/os/unix/_os.py b/dissect/target/plugins/os/unix/_os.py index 7b475deabd..ecf7d855e6 100644 --- a/dissect/target/plugins/os/unix/_os.py +++ b/dissect/target/plugins/os/unix/_os.py @@ -75,7 +75,7 @@ def users(self, sessions: bool = False) -> Iterator[UnixUserRecord]: # Yield users found in passwd files. for passwd_file in PASSWD_FILES: if (path := self.target.fs.path(passwd_file)).exists(): - for line in path.open("rt"): + for line in path.open("rt", errors="surrogateescape"): line = line.strip() if not line or line.startswith("#"): continue @@ -85,13 +85,12 @@ def users(self, sessions: bool = False) -> Iterator[UnixUserRecord]: current_user = (pwent.get(0), pwent.get(5), pwent.get(6)) if current_user in seen_users: continue - seen_users.add(current_user) yield UnixUserRecord( name=pwent.get(0), passwd=pwent.get(1), - uid=pwent.get(2), - gid=pwent.get(3), + uid=pwent.get(2) or None, + gid=pwent.get(3) or None, gecos=pwent.get(4), home=posix_path(pwent.get(5)), shell=pwent.get(6), diff --git a/tests/conftest.py b/tests/conftest.py index 8cd7049fd6..6184dd70e9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -441,6 +441,7 @@ def target_unix_users(target_unix: Target, fs_unix: Filesystem) -> Iterator[Targ passwd = """ root:x:0:0:root:/root:/bin/bash user:x:1000:1000:user:/home/user:/bin/bash + +@ngtest:x::::: """ fs_unix.map_file_fh("/etc/passwd", BytesIO(textwrap.dedent(passwd).encode())) yield target_unix diff --git a/tests/plugins/os/unix/test__os.py b/tests/plugins/os/unix/test__os.py index ffb3c917e1..edf66d4260 100644 --- a/tests/plugins/os/unix/test__os.py +++ b/tests/plugins/os/unix/test__os.py @@ -134,7 +134,7 @@ def test_parse_hostname_string( def test_users(target_unix_users: Target) -> None: users = list(target_unix_users.users()) - assert len(users) == 2 + assert len(users) == 3 assert users[0].name == "root" assert users[0].uid == 0 @@ -148,6 +148,12 @@ def test_users(target_unix_users: Target) -> None: assert users[1].home == posix_path("/home/user") assert users[1].shell == "/bin/bash" + assert users[2].name == "+@ngtest" + assert users[2].uid is None + assert users[2].gid is None + assert users[2].home == posix_path("") + assert users[2].shell == "" + @pytest.mark.parametrize( "expected_arch, elf_buf",