Module refinery.units.formats.pe.dotnet.dnds

Expand source code Browse git
from __future__ import annotations

from refinery.lib.dotnet.deserialize import BinaryFormatterParser
from refinery.lib.structures import struct_to_json
from refinery.lib.types import Param
from refinery.units import Arg
from refinery.units.formats import JSONEncoderUnit


class dnds(JSONEncoderUnit):
    """
    Deserialize .NET BinaryFormatter data and output as JSON.

    Stands for "DotNet DeSerialize": Expects data that has been serialized using the .NET
    class "BinaryFormatter". The output is a representation of the deserialized data in JSON
    format.
    """

    @classmethod
    def handles(cls, data) -> bool | None:
        if len(data) >= 17 and data[0] == 0 and data[5:9] == b'\xFF\xFF\xFF\xFF':
            return True

    def __init__(
        self,
        dereference: Param[bool, Arg.Switch('-r', '--keep-references', off=True,
            help='Do not resolve Object references in serialized data.')] = True,
        encode=None, digest=None, arrays=False
    ):
        super().__init__(encode=encode, digest=digest, arrays=arrays, dereference=dereference)

    def process(self, data):
        self.log_debug('initializing parser, will fail on malformed stream')
        bf = BinaryFormatterParser(
            data,
            keep_meta=True,
            dereference=self.args.dereference,
            ignore_errors=not self.log_debug(),
        )

        return self.to_json([
            {
                'Type': repr(record),
                'Data': struct_to_json(record),
            } for record in bf
        ])

Classes

class dnds (dereference=True, encode=None, digest=None, arrays=False)

Deserialize .NET BinaryFormatter data and output as JSON.

Stands for "DotNet DeSerialize": Expects data that has been serialized using the .NET class "BinaryFormatter". The output is a representation of the deserialized data in JSON format.

Expand source code Browse git
class dnds(JSONEncoderUnit):
    """
    Deserialize .NET BinaryFormatter data and output as JSON.

    Stands for "DotNet DeSerialize": Expects data that has been serialized using the .NET
    class "BinaryFormatter". The output is a representation of the deserialized data in JSON
    format.
    """

    @classmethod
    def handles(cls, data) -> bool | None:
        if len(data) >= 17 and data[0] == 0 and data[5:9] == b'\xFF\xFF\xFF\xFF':
            return True

    def __init__(
        self,
        dereference: Param[bool, Arg.Switch('-r', '--keep-references', off=True,
            help='Do not resolve Object references in serialized data.')] = True,
        encode=None, digest=None, arrays=False
    ):
        super().__init__(encode=encode, digest=digest, arrays=arrays, dereference=dereference)

    def process(self, data):
        self.log_debug('initializing parser, will fail on malformed stream')
        bf = BinaryFormatterParser(
            data,
            keep_meta=True,
            dereference=self.args.dereference,
            ignore_errors=not self.log_debug(),
        )

        return self.to_json([
            {
                'Type': repr(record),
                'Data': struct_to_json(record),
            } for record in bf
        ])

Ancestors

Subclasses

Class variables

var reverse

The type of the None singleton.

Inherited members