dissect.target.helpers.configutil

Module Contents

Classes

PeekableIterator

Source gotten from:

ConfigurationParser

A configuration parser where you can configure certain aspects of the parsing mechanism.

Default

Parse a configuration file specified by separator and comment_prefixes.

Ini

Parses an ini file according using the built-in python ConfigParser

Txt

Read the file into content, and show the bumber of bytes read.

Xml

Parses an XML file. Ignores any constructor parameters passed from ``ConfigurationParser`.

ListUnwrapper

Provides utility functions to unwrap dictionary objects out of lists.

Json

Parses a JSON file.

Yaml

Parses a Yaml file.

Toml

Parses a Toml file.

ScopeManager

A (context)manager for dictionary scoping.

Indentation

This parser is used for files that use a single level of indentation to specify a different scope.

SystemD

A ConfigurationParser that specifically parses systemd configuration files.

ParserOptions

ParserConfig

Functions

parse

Parses the content of an path or entry to a dictionary.

parse_config

Attributes

dissect.target.helpers.configutil.HAS_YAML = True
dissect.target.helpers.configutil.HAS_TOML = True
class dissect.target.helpers.configutil.PeekableIterator(iterable)

Source gotten from: https://more-itertools.readthedocs.io/en/stable/_modules/more_itertools/more.html#peekable

__iter__()
__next__()
peek()
class dissect.target.helpers.configutil.ConfigurationParser(collapse: bool | Iterable[str] = False, collapse_inverse: bool = False, separator: tuple[str] = ('=',), comment_prefixes: tuple[str] = (';', '#'))

A configuration parser where you can configure certain aspects of the parsing mechanism.

parsed_data

The resulting dictionary after parsing.

Parameters:
  • collapse – A bool or an Iterator: If True: it will collapse all the resulting dictionary values. If an Iterable it will collapse on the keys defined in collapse.

  • collapse_inverse – Inverses the collapsing mechanism. Collapse on everything that is not inside collapse.

  • separator – Contains what values it should look for as a separator.

  • comment_prefixes – Contains what constitutes as a comment.

__getitem__(item: Any) dict | str
__contains__(item: str) bool
abstract parse_file(fh: TextIO) None

Parse the contents of fh into key/value pairs.

This function should set parsed_data as a side_effect.

Parameters:

fh – The text to parse.

get(item: str, default: Any | None = None) Any
read_file(fh: TextIO) None

Parse a configuration file.

Raises:

ConfigurationParsingError – If any exception occurs during during the parsing process.

keys() KeysView
items() ItemsView
class dissect.target.helpers.configutil.Default(*args, **kwargs)

Bases: ConfigurationParser

Parse a configuration file specified by separator and comment_prefixes.

This parser splits only on the first separator it finds:

key<separator>value -> {“key”: “value”}

key<separator>value

continuation

-> {“key”: “value continuation”}

# Unless we collapse values, we add them to a list to not overwrite any values. key<separator>value1 key<separator>value2

-> {key: [value1, value2]}

<empty_space><comment> -> skip

line_reader(fh: TextIO, strip_comments: bool = True) Iterator[str]
parse_file(fh: TextIO) None

Parse the contents of fh into key/value pairs.

This function should set parsed_data as a side_effect.

Parameters:

fh – The text to parse.

class dissect.target.helpers.configutil.Ini(*args, **kwargs)

Bases: ConfigurationParser

Parses an ini file according using the built-in python ConfigParser

parse_file(fh: io.TextIO) None

Parse the contents of fh into key/value pairs.

This function should set parsed_data as a side_effect.

Parameters:

fh – The text to parse.

class dissect.target.helpers.configutil.Txt(collapse: bool | Iterable[str] = False, collapse_inverse: bool = False, separator: tuple[str] = ('=',), comment_prefixes: tuple[str] = (';', '#'))

Bases: ConfigurationParser

Read the file into content, and show the bumber of bytes read.

parse_file(fh: TextIO) None

Parse the contents of fh into key/value pairs.

This function should set parsed_data as a side_effect.

Parameters:

fh – The text to parse.

class dissect.target.helpers.configutil.Xml(collapse: bool | Iterable[str] = False, collapse_inverse: bool = False, separator: tuple[str] = ('=',), comment_prefixes: tuple[str] = (';', '#'))

Bases: ConfigurationParser

Parses an XML file. Ignores any constructor parameters passed from ``ConfigurationParser`.

parse_file(fh: TextIO) None

Parse the contents of fh into key/value pairs.

This function should set parsed_data as a side_effect.

Parameters:

fh – The text to parse.

class dissect.target.helpers.configutil.ListUnwrapper

Provides utility functions to unwrap dictionary objects out of lists.

static unwrap(data: dict | list) dict | list

Transforms a list with dictionaries to a dictionary.

The order of the list is preserved. If no dictionary is found, the list remains untouched:

[“value1”, “value2”] -> [“value1”, “value2”]

{“data”: “value”} -> {“data”: “value”}

[{“data”: “value”}] -> {
“list_item0”: {

“data”: “value”

}

}

class dissect.target.helpers.configutil.Json(collapse: bool | Iterable[str] = False, collapse_inverse: bool = False, separator: tuple[str] = ('=',), comment_prefixes: tuple[str] = (';', '#'))

Bases: ConfigurationParser

Parses a JSON file.

parse_file(fh: TextIO)

Parse the contents of fh into key/value pairs.

This function should set parsed_data as a side_effect.

Parameters:

fh – The text to parse.

class dissect.target.helpers.configutil.Yaml(collapse: bool | Iterable[str] = False, collapse_inverse: bool = False, separator: tuple[str] = ('=',), comment_prefixes: tuple[str] = (';', '#'))

Bases: ConfigurationParser

Parses a Yaml file.

parse_file(fh: TextIO) None

Parse the contents of fh into key/value pairs.

This function should set parsed_data as a side_effect.

Parameters:

fh – The text to parse.

class dissect.target.helpers.configutil.Toml(collapse: bool | Iterable[str] = False, collapse_inverse: bool = False, separator: tuple[str] = ('=',), comment_prefixes: tuple[str] = (';', '#'))

Bases: ConfigurationParser

Parses a Toml file.

parse_file(fh: TextIO) None

Parse the contents of fh into key/value pairs.

This function should set parsed_data as a side_effect.

Parameters:

fh – The text to parse.

class dissect.target.helpers.configutil.ScopeManager

A (context)manager for dictionary scoping.

This class provides utility functions to keep track of scopes inside a dictionary.

_parents

A dictionary accounting what child belongs to which parent dictionary.

_root

The initial dictionary.

_current

The current dictionary.

_previous

The node before the current (changed) node.

__enter__() ScopeManager
__exit__(type: ScopeManager.__exit__.type[BaseException] | None, value: BaseException | None, traceback: types.TracebackType | None) None
push(name: str, keep_prev: bool = False) Literal[True]

Push a new key to the _current dictionary and return that we did.

pop(keep_prev: bool = False) bool

Pop _current and return whether we changed the _parents dictionary.

update(key: str, value: str) None

Update the _current dictionary with key and value.

update_prev(key: str, value: str) None

Update the _previous dictionary with key and value.

is_root() bool

Utility function to check whether the current dictionary is a root dictionary.

clean() None

Clean up the internal state. This is called automatically when ScopeManager is used as a contextmanager.

class dissect.target.helpers.configutil.Indentation(*args, **kwargs)

Bases: Default

This parser is used for files that use a single level of indentation to specify a different scope.

Examples of these files are the sshd_config file. Where “Match” statements use a single layer of indentation to specify a scope for the key value pairs.

The parser parses this as the following:

key value
key2 value2

-> {“key value”: {“key2”: “value2”}}

parse_file(fh: TextIO) None

Parse the contents of fh into key/value pairs.

This function should set parsed_data as a side_effect.

Parameters:

fh – The text to parse.

class dissect.target.helpers.configutil.SystemD(*args, **kwargs)

Bases: Indentation

A ConfigurationParser that specifically parses systemd configuration files.

Examples

>>> systemd_data = textwrap.dedent(
        '''
        [Section1]
        Key=Value
        [Section2]
        Key2=Value 2\
            Value 2 continued
        '''
    )
>>> parser = SystemD(io.StringIO(systemd_data))
>>> parser.parser_items
{
    "Section1": {
        "Key": "Value
    },
    "Section2": {
        "Key2": "Value2 Value 2 continued
    }
}
parse_file(fh: TextIO) None

Parse the contents of fh into key/value pairs.

This function should set parsed_data as a side_effect.

Parameters:

fh – The text to parse.

class dissect.target.helpers.configutil.ParserOptions
collapse: bool | set | None
collapse_inverse: bool | None
separator: tuple[str] | None
comment_prefixes: tuple[str] | None
class dissect.target.helpers.configutil.ParserConfig
parser: type[ConfigurationParser]
collapse: bool | set | None
collapse_inverse: bool | None
separator: tuple[str] | None
comment_prefixes: tuple[str] | None
create_parser(options: ParserOptions | None = None) ConfigurationParser
dissect.target.helpers.configutil.MATCH_MAP: dict[str, ParserConfig]
dissect.target.helpers.configutil.CONFIG_MAP: dict[tuple[str, Ellipsis], ParserConfig]
dissect.target.helpers.configutil.KNOWN_FILES: dict[str, type[ConfigurationParser]]
dissect.target.helpers.configutil.parse(path: dissect.target.filesystem.FilesystemEntry | dissect.target.helpers.fsutil.TargetPath, hint: str | None = None, *args, **kwargs) configparser.ConfigParser

Parses the content of an path or entry to a dictionary.

Parameters:
  • path – The path to either a directory or file.

  • hint – What kind of parser should be used.

  • collapse – Whether it should collapse everything or just a certain set of keys.

  • collapse_inverse – Invert the collapse function to collapse everything but the keys inside collapse.

  • separator – The separator that should be used for parsing.

  • comment_prefixes – What is specified as a comment.

Raises:

FileNotFoundError – If the path is not a file.

dissect.target.helpers.configutil.parse_config(entry: dissect.target.filesystem.FilesystemEntry, hint: str | None = None, options: ParserOptions | None = None) configparser.ConfigParser