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

Add binary configuration parser #893

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions dissect/target/filesystems/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,14 @@
Returns:
A file-like object holding a byte representation of :attr:`parser_items`.
"""

if isinstance(self.parser_items, ConfigurationParser):
# Currently trying to open the underlying entry
return self.entry.open()

if isinstance(self.parser_items, bytes):
return io.BytesIO(self.parser_items)

Check warning on line 256 in dissect/target/filesystems/config.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/filesystems/config.py#L256

Added line #L256 was not covered by tests

output_data = self._write_value_mapping(self.parser_items)
return io.BytesIO(bytes(output_data, "utf-8"))

Expand Down
26 changes: 23 additions & 3 deletions dissect/target/helpers/configutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
def get(self, item: str, default: Optional[Any] = None) -> Any:
return self.parsed_data.get(item, default)

def read_file(self, fh: TextIO) -> None:
def read_file(self, fh: TextIO | io.BytesIO) -> None:
"""Parse a configuration file.

Raises:
Expand Down Expand Up @@ -303,6 +303,14 @@
self.parsed_data = {"content": fh.read(), "size": str(fh.tell())}


class Bin(ConfigurationParser):

"""Read the file into ``binary`` and show the number of bytes read"""

def parse_file(self, fh: io.BytesIO) -> None:
self.parsed_data = {"binary": fh.read(), "size": str(fh.tell())}


class Xml(ConfigurationParser):
"""Parses an XML file. Ignores any constructor parameters passed from ``ConfigurationParser`."""

Expand Down Expand Up @@ -733,6 +741,8 @@
"*/sysconfig/network-scripts/ifcfg-*": ParserConfig(Default),
"*/sysctl.d/*.conf": ParserConfig(Default),
"*/xml/*": ParserConfig(Xml),
"*.bashrc": ParserConfig(Txt),
"*/vim/vimrc*": ParserConfig(Txt),
}

CONFIG_MAP: dict[tuple[str, ...], ParserConfig] = {
Expand All @@ -744,6 +754,13 @@
"cnf": ParserConfig(Default),
"conf": ParserConfig(Default, separator=(r"\s",)),
"sample": ParserConfig(Txt),
"sh": ParserConfig(Txt),
"key": ParserConfig(Txt),
"crt": ParserConfig(Txt),
"pem": ParserConfig(Txt),
"pl": ParserConfig(Txt), # various admin panels
"lua": ParserConfig(Txt), # wireshark etc.
"txt": ParserConfig(Txt),
"systemd": ParserConfig(SystemD),
"template": ParserConfig(Txt),
"toml": ParserConfig(Toml),
Expand All @@ -759,6 +776,7 @@
"nsswitch.conf": ParserConfig(Default, separator=(":",)),
"lsb-release": ParserConfig(Default),
"catalog": ParserConfig(Xml),
"ld.so.cache": ParserConfig(Bin),
"fstab": ParserConfig(
CSVish,
separator=(r"\s",),
Expand Down Expand Up @@ -832,9 +850,11 @@
parser_type = _select_parser(entry, hint)

parser = parser_type.create_parser(options)

with entry.open() as fh:
open_file = io.TextIOWrapper(fh, encoding="utf-8")
if not isinstance(parser, Bin):
open_file = io.TextIOWrapper(fh, encoding="utf-8")
else:
open_file = io.BytesIO(fh.read())

Check warning on line 857 in dissect/target/helpers/configutil.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/helpers/configutil.py#L857

Added line #L857 was not covered by tests
parser.read_file(open_file)

return parser
Expand Down
15 changes: 14 additions & 1 deletion tests/helpers/test_configutil.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from __future__ import annotations

import textwrap
from io import StringIO
from io import BytesIO, StringIO
from pathlib import Path
from typing import TYPE_CHECKING, Union

import pytest

from dissect.target.exceptions import FileNotFoundError
from dissect.target.helpers.configutil import (
Bin,
ConfigurationParser,
CSVish,
Default,
Expand Down Expand Up @@ -269,6 +270,18 @@ def test_json_syntax(data_string: str, expected_data: Union[dict, list]) -> None
assert parser.parsed_data == expected_data


@pytest.mark.parametrize(
"data, expected_data",
[
(b"\x00\x01\x02", {"binary": b"\x00\x01\x02", "size": "3"}),
],
)
def test_bin_parser(data: bytes, expected_data: dict) -> None:
parser = Bin()
parser.parse_file(BytesIO(data))
assert parser.parsed_data == expected_data


@pytest.mark.parametrize(
"fields, separator, comment, data_string, expected_data",
[
Expand Down
Loading