def from_bytes(cls, data: bytes) -> Tuple[ShareInfo1Container, int]:
        entries_read: int = struct_unpack('<I', data[:4])[0]
        pointer_data = Pointer.from_bytes(data[4:]).representation
        max_count = struct_unpack('<I', pointer_data[:4])[0]

        offset = 4
        entry_share_type_list: List[ShareType] = []
        for _ in range(entries_read):
            entry_share_type_list.append(
                ShareType.from_int(struct_unpack('<I', pointer_data[offset+4:offset+8])[0])
            )
            offset += 12

        entries: List[ShareInfo1] = []
        for i in range(entries_read):
            netname_cvs = ConformantVaryingString.from_bytes(data=pointer_data[offset:])
            offset += calculate_pad_length(len(netname_cvs))
            remark_cvs = ConformantVaryingString.from_bytes(pointer_data[offset:])
            offset += calculate_pad_length(len(remark_cvs))

            entries.append(
                ShareInfo1(
                    netname=netname_cvs.representation,
                    share_type=entry_share_type_list[i],
                    remark=remark_cvs.representation
                )
            )

        return cls(entries=tuple(entries)), 8 + offset
Example #2
0
    def from_bytes(cls,
                   data: ByteString,
                   base_offset: int = 0) -> BaseRegOpenKeyRequest:
        data = memoryview(data)[base_offset:]
        offset = 0

        key_handle: bytes = cls._KEY_HANDLE_STRUCT.unpack_from(
            buffer=data, offset=offset)[0]
        offset += cls._KEY_HANDLE_STRUCT.size

        ndr_sub_key_name = RRPUnicodeString.from_bytes(data=data,
                                                       base_offset=offset)
        offset += calculate_pad_length(len(ndr_sub_key_name))

        options = RegOptions.from_int(value=cls._OPTIONS_STRUCT.unpack_from(
            buffer=data, offset=offset)[0])
        offset += cls._OPTIONS_STRUCT.size

        sam_desired = Regsam.from_int(
            value=cls._SAM_DESIRED_STRUCT.unpack_from(buffer=data,
                                                      offset=offset)[0])

        return cls(key_handle=key_handle,
                   sub_key_name=ndr_sub_key_name.representation,
                   options=options,
                   sam_desired=sam_desired)
Example #3
0
    def from_bytes(cls,
                   data: ByteString,
                   base_offset: int = 0,
                   strict: bool = False) -> BaseRegSetValueRequest:

        data = memoryview(data)[base_offset:]
        offset = 0

        key_handle: bytes = cls._KEY_HANDLE_STRUCT.unpack_from(
            buffer=data, offset=offset)[0]
        offset += cls._KEY_HANDLE_STRUCT.size

        ndr_sub_key_name = RRPUnicodeString.from_bytes(data[offset:])
        offset += calculate_pad_length(length_unpadded=len(ndr_sub_key_name))

        value_type = RegValueType(
            cls._VALUE_TYPE_STRUCT.unpack_from(buffer=data, offset=offset)[0])
        offset += cls._VALUE_TYPE_STRUCT.size

        ndr_value = UnidimensionalConformantArray.from_bytes(data=bytes(
            data[offset:offset + UnidimensionalConformantArray.STRUCTURE_SIZE +
                 calcsize(REG_VALUE_TYPE_TO_STRUCT_FORMAT[value_type])]))
        offset += len(ndr_value)

        value_len: int = cls._VALUE_LEN_STRUCT.unpack_from(buffer=data,
                                                           offset=offset)[0]
        # TODO: Check if correct?

        return cls(key_handle=key_handle,
                   sub_key_name=ndr_sub_key_name.representation,
                   value_type=value_type,
                   value=b''.join(ndr_value.representation))
Example #4
0
    def from_bytes(cls,
                   data: ByteString,
                   base_offset: int = 0,
                   strict: bool = False) -> BaseRegCreateKeyRequest:

        data = memoryview(data)[base_offset:]
        offset = 0

        key_handle: bytes = cls._KEY_HANDLE_STRUCT.unpack_from(
            buffer=data, offset=offset)[0]
        offset += cls._KEY_HANDLE_STRUCT.size

        ndr_sub_key_name = RRPUnicodeString.from_bytes(data=data[offset:])
        offset += calculate_pad_length(len(ndr_sub_key_name))

        ndr_class_name = RRPUnicodeString.from_bytes(data=data[offset:])
        offset += calculate_pad_length(len(ndr_class_name))

        options = RegOptions.from_int(value=cls._OPTIONS_STRUCT.unpack_from(
            buffer=data, offset=offset)[0])
        offset += cls._OPTIONS_STRUCT.size

        sam_desired = Regsam.from_int(
            value=cls._SAM_DESIRED_STRUCT.unpack_from(buffer=data,
                                                      offset=offset)[0])
        offset += cls._SAM_DESIRED_STRUCT.size

        security_attributes_pointer = Pointer.from_bytes(data=data,
                                                         base_offset=offset)
        offset += Pointer.structure_size
        security_attributes = RPCSecurityAttributes.from_bytes(
            data=security_attributes_pointer.representation)
        offset += calculate_pad_length(len(security_attributes))

        disposition_pointer = Pointer.from_bytes(data=data, base_offset=offset)
        offset += Pointer.structure_size
        disposition = Disposition(
            cls._DISPOSITION_STRUCT.unpack_from(
                buffer=disposition_pointer.representation)[0])

        return cls(key_handle=key_handle,
                   sub_key_name=ndr_sub_key_name.representation,
                   class_name=ndr_class_name.representation,
                   options=options,
                   sam_desired=sam_desired,
                   security_attributes=security_attributes,
                   disposition=disposition)
Example #5
0
    def from_bytes(cls, data: ByteString, base_offset: int = 0) -> BaseRegSaveKeyRequest:
        data = memoryview(data)[base_offset:]
        offset = 0

        key_handle: bytes = cls._KEY_HANDLE_STRUCT.unpack_from(buffer=data, offset=base_offset)[0]
        offset += cls._KEY_HANDLE_STRUCT.size

        ndr_save_path = RRPUnicodeString.from_bytes(data=data, base_offset=offset)
        offset += calculate_pad_length(length_unpadded=len(ndr_save_path))

        security_attributes_ndr_pointer = Pointer.from_bytes(data=data, base_offset=offset)
        offset += Pointer.structure_size
        security_attributes = RPCSecurityAttributes.from_bytes(data=security_attributes_ndr_pointer.representation)
        offset += calculate_pad_length(length_unpadded=len(security_attributes))

        return cls(
            key_handle=key_handle,
            save_path=ndr_save_path.representation,
            security_attributes=security_attributes
        )
Example #6
0
def unpack_structure(data: ByteString,
                     structure: dict[str, tuple[Type, ...]],
                     offset: int = 0) -> dict[str, Any]:

    data = memoryview(data)[offset:]
    offset = 0

    structure_values: dict[str, Any] = {}

    for value_name, item_types in structure.items():
        item_data = data[offset:]

        item_types_deque: Deque[Type] = deque(item_types)

        while True:
            try:
                item_type = item_types_deque.popleft()
            except IndexError:
                break

            if item_type in {LPDWORD, LPBYTE, LPBYTE_VAR}:
                item_types_deque.extendleft(reversed(get_args(item_type)))
            elif item_type in {DWORD}:
                struct = CTYPE_TO_STRUCT[item_type.__supertype__]

                item_data = struct.unpack_from(buffer=item_data)[0]
                offset += struct.size
            elif item_type is Pointer:
                pointer = Pointer.from_bytes(data=item_data)
                item_data = pointer.representation
                offset += Pointer.structure_size
            else:
                if isinstance(item_data, (ByteString, memoryview)):
                    item_data = item_type.from_bytes(item_data)

                    if issubclass(item_type, NDRType):
                        offset += calculate_pad_length(
                            length_unpadded=len(item_data))
                    else:
                        offset += len(item_data)

                    if hasattr(item_data, 'representation'):
                        item_data = item_data.representation
                else:
                    item_data = item_type(item_data)

        structure_values[value_name] = item_data

    return structure_values