Skip to content

Commit

Permalink
Linux network plugin: NetworkManager & systemd-networkd (#932)
Browse files Browse the repository at this point in the history
- Implement Network plugin for Linux.
- Support NetworkManager and systemd-network
  • Loading branch information
twiggler authored Nov 20, 2024
1 parent ed9ec23 commit 6e6a546
Show file tree
Hide file tree
Showing 23 changed files with 598 additions and 20 deletions.
4 changes: 2 additions & 2 deletions dissect/target/helpers/configutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ def create_parser(self, options: Optional[ParserOptions] = None) -> Configuratio
}


def parse(path: Union[FilesystemEntry, TargetPath], hint: Optional[str] = None, *args, **kwargs) -> ConfigParser:
def parse(path: Union[FilesystemEntry, TargetPath], hint: Optional[str] = None, *args, **kwargs) -> ConfigurationParser:
"""Parses the content of an ``path`` or ``entry`` to a dictionary.
Args:
Expand Down Expand Up @@ -922,7 +922,7 @@ def parse_config(
entry: FilesystemEntry,
hint: Optional[str] = None,
options: Optional[ParserOptions] = None,
) -> ConfigParser:
) -> ConfigurationParser:
parser_type = _select_parser(entry, hint)

parser = parser_type.create_parser(options)
Expand Down
18 changes: 12 additions & 6 deletions dissect/target/helpers/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,44 +145,50 @@ def DynamicDescriptor(types): # noqa

COMMON_INTERFACE_ELEMENTS = [
("string", "name"),
("string[]", "mac"),
("string", "type"),
("boolean", "enabled"),
("string", "mac"),
("net.ipaddress[]", "dns"),
("net.ipaddress[]", "ip"),
("net.ipaddress[]", "gateway"),
("net.ipnetwork[]", "network"),
("string", "source"),
]


UnixInterfaceRecord = TargetRecordDescriptor(
"unix/network/interface",
COMMON_INTERFACE_ELEMENTS,
[
*COMMON_INTERFACE_ELEMENTS,
("boolean", "dhcp_ipv4"), # NetworkManager allows for dual-stack configurations.
("boolean", "dhcp_ipv6"),
("datetime", "last_connected"),
("varint[]", "vlan"),
("string", "configurator"),
],
)

WindowsInterfaceRecord = TargetRecordDescriptor(
"windows/network/interface",
[
*COMMON_INTERFACE_ELEMENTS,
("varint", "vlan"),
("net.ipnetwork[]", "network"),
("varint", "metric"),
("stringlist", "search_domain"),
("datetime", "first_connected"),
("datetime", "last_connected"),
("net.ipaddress[]", "subnetmask"),
("boolean", "dhcp"),
("varint", "vlan"),
],
)

MacInterfaceRecord = TargetRecordDescriptor(
"macos/network/interface",
[
*COMMON_INTERFACE_ELEMENTS,
("varint", "vlan"),
("net.ipnetwork[]", "network"),
("varint", "interface_service_order"),
("boolean", "dhcp"),
("varint", "vlan"),
],
)

Expand Down
21 changes: 20 additions & 1 deletion dissect/target/helpers/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from __future__ import annotations

import logging
import re
import urllib.parse
from datetime import datetime, timezone, tzinfo
from enum import Enum
from pathlib import Path
from typing import BinaryIO, Callable, Iterator, Optional, Union
from typing import BinaryIO, Callable, Iterator, Optional, TypeVar, Union

from dissect.util.ts import from_unix

Expand All @@ -24,6 +26,23 @@ def findall(buf: bytes, needle: bytes) -> Iterator[int]:
offset += 1


T = TypeVar("T")


def to_list(value: T | list[T]) -> list[T]:
"""Convert a single value or a list of values to a list.
Args:
value: The value to convert.
Returns:
A list of values.
"""
if not isinstance(value, list):
return [value]
return value


class StrEnum(str, Enum):
"""Sortable and serializible string-based enum"""

Expand Down
2 changes: 1 addition & 1 deletion dissect/target/plugins/general/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def with_ip(self, ip_addr: str) -> Iterator[InterfaceRecord]:
@internal
def with_mac(self, mac: str) -> Iterator[InterfaceRecord]:
for interface in self.interfaces():
if interface.mac == mac:
if mac in interface.mac:
yield interface

@internal
Expand Down
3 changes: 2 additions & 1 deletion dissect/target/plugins/os/unix/bsd/osx/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ def _interfaces(self) -> Iterator[MacInterfaceRecord]:
network=network,
interface_service_order=interface_service_order,
dhcp=dhcp,
mac=[],
_target=self.target,
)

except Exception as e:
self.target.log.warning("Error reading configuration for network device %s: %s", name, e)
self.target.log.warning("Error reading configuration for network device %s", name, exc_info=e)
continue
Loading

0 comments on commit 6e6a546

Please sign in to comment.