예제 #1
0
def PrefixedBytes(lengthfield):
    """
    Defines a length prefixed bytearray.

    Args:
        lengthfield (:class:`Subconstruct`): The length subcon.

    Returns:
        PrefixedBytes: A length prefixed bytesarray
    """
    return construct.Prefixed(lengthfield, construct.GreedyBytes)
        high = obj >> 32
        IdaVarInt32._build(high, stream, context, path)

        return obj


#######################################
#
# Basic types & helpers
#
#######################################

# String prefixed with a variable int size
VarString = con.PascalString(IdaVarInt32, "utf8")
# Bytes buffer prefixed with a variable int size
VarBuff = con.Prefixed(IdaVarInt32, con.GreedyBytes)
# IDA typedefs
ea_t = asize_t = adiff_t = con.ExprAdapter(IdaVarInt64, con.obj_ - 1,
                                           con.obj_ + 1)


# "template" for defining object list, prefixed with a variable int size
def ObjectList(obj):
    return con.PrefixedArray(IdaVarInt32, obj)


#######################################
#
# Lumina types
#
#######################################
예제 #3
0
        construct.Aligned(
            3,
            construct.Struct(
                header=construct.BitStruct(
                    has_seed_hash=construct.Rebuild(
                        construct.Flag, construct.this._.seed_hash != None),
                    bytes_rotation=construct.Rebuild(
                        construct.BitsInteger(7),
                        lambda ctx: single_byte_hash(ctx._.generator_params) >>
                        1,
                    )),
                seed_hash=construct.If(construct.this.header.has_seed_hash,
                                       construct.Bytes(5)),
                randovania_version=construct.Bytes(4),  # short git hash
                generator_params=construct.ExprAdapter(
                    construct.Prefixed(construct.VarInt,
                                       construct.GreedyBytes),
                    # parsing
                    decoder=create_rotator(inverse=True),
                    # building
                    encoder=create_rotator(inverse=False),
                ),
            ))),
    permalink_checksum=construct.Checksum(
        construct.Bytes(2),
        lambda data: hashlib.blake2b(data, digest_size=2).digest(),
        construct.this.fields.data,
    ),
    end=construct.Terminated,
)

예제 #4
0
import construct as c

from . import CompactUint, ConstFlag, Optional

BitcoinBytes = c.Prefixed(CompactUint, c.GreedyBytes)
"""Bitcoin string of bytes.

Encoded as a CompactUint length followed by that many bytes.
"""

TxHash = c.Transformed(c.Bytes(32), lambda b: b[::-1], 32, lambda b: b[::-1], 32)
"""Transaction hash, encoded as a reversed sequence of bytes."""

TxInput = c.Struct(
    "tx" / TxHash,
    "index" / c.Int32ul,
    "script_sig" / BitcoinBytes,
    "sequence" / c.Int32ul,
)
"""Transaction input."""

TxOutput = c.Struct("value" / c.Int64ul, "script_pubkey" / BitcoinBytes)
"""Transaction output."""

TxInputWitness = c.PrefixedArray(CompactUint, BitcoinBytes)
"""Array of witness records."""

Transaction = c.Struct(
    "version" / c.Int32ul,
    "segwit" / ConstFlag(b"\x00\x01"),
    "inputs" / c.PrefixedArray(CompactUint, TxInput),
예제 #5
0
        super().__init__(c.Optional(c.Const(const)))

    def _encode(self, obj, context, path):
        return self.const if obj else None

    def _decode(self, obj, context, path):
        return obj is not None


CompactUint = CompactUintAdapter(CompactUintStruct)

TxInput = c.Struct(
    "tx" / c.Bytes(32),
    "index" / c.Int32ul,
    # TODO coinbase tx
    "script" / c.Prefixed(CompactUint, c.GreedyBytes),
    "sequence" / c.Int32ul,
)

TxOutput = c.Struct(
    "value" / c.Int64ul,
    "pk_script" / c.Prefixed(CompactUint, c.GreedyBytes),
)

StackItem = c.Prefixed(CompactUint, c.GreedyBytes)
TxInputWitness = c.PrefixedArray(CompactUint, StackItem)

Transaction = c.Struct(
    "version" / c.Int32ul,
    "segwit" / ConstFlag(b"\x00\x01"),
    "inputs" / c.PrefixedArray(CompactUint, TxInput),
예제 #6
0

u8 = partial(unsigned, 8)
u16 = partial(unsigned, 16)
u32 = partial(unsigned, 32)
u64 = partial(unsigned, 64)

i8 = partial(signed, 8)
i16 = partial(signed, 16)
i32 = partial(signed, 32)
i64 = partial(signed, 64)

string = partial(attribute, construct.PascalString(construct.Int16ul, "utf8"), str)

data = partial(
    attribute, construct.Prefixed(construct.Int32ul, construct.GreedyBytes), bytes
)


def enum(constructor, type, **kwargs):
    return attribute(constructor, type, converter=type, **kwargs)


def struct(type, **kwargs):
    return attribute(type._construct, type, converter=lambda x: type(**x), **kwargs)


def constructify(class_):
    """
    A decorator that assembles the individual field constructs into one Struct construct.
    """
예제 #7
0

_is_init_reply = _create_matcher(_INIT_RESPONSE, b'\x01')
_is_keepalive_response = _create_matcher(_KEEPALIVE_RESPONSE, b'\x05')
_is_uknown_message_error = _create_matcher(_UNKNOWN_MESSAGE_RESPONSE, b'\x85')
_is_encryption_missing_error = _create_matcher(_ENCRYPTION_SETUP_RESPONSE,
                                               b'\x15')
_is_encryption_setup_error = _create_matcher(_ENCRYPTION_SETUP_RESPONSE,
                                             b'\x14')

_FREESTYLE_MESSAGE = construct.Struct(
    'hid_report' / construct.Const(0, construct.Byte),
    'message_type' / construct.Byte,
    'command' / construct.Padded(
        63,  # command can only be up to 62 bytes, but one is used for length.
        construct.Prefixed(construct.Byte, construct.GreedyBytes)),
)

_FREESTYLE_ENCRYPTED_MESSAGE = construct.Struct(
    'hid_report' / construct.Const(0, construct.Byte),
    'message_type' / construct.Byte,
    'command' / construct.Padded(
        63,  # command can only be up to 62 bytes, but one is used for length.
        construct.GreedyBytes),
)

_TEXT_COMPLETION_RE = re.compile(b'CMD (?:OK|Fail!)')
_TEXT_REPLY_FORMAT = re.compile(
    b'^(?P<message>.*)CKSM:(?P<checksum>[0-9A-F]{8})\r\n'
    b'CMD (?P<status>OK|Fail!)\r\n$', re.DOTALL)
예제 #8
0
        TOKEN_CON,
        "command_data_length"
        / construct.Rebuild(
            construct.Int16ub, construct.len_(construct.this.command_data)
        ),
        construct.Const(bytes([0x00, 0x00])),
        "command_data" / construct.Byte[construct.this.command_data_length],
    ),
)

MSC_TRANSPORT_RESPONSE_PACKET = construct.Padded(
    BLOCK_SIZE,
    construct.Struct(
        HEADER_CON,
        RANDOM_TOKEN_CON,
        "response_data" / construct.Prefixed(construct.Int16ub, construct.GreedyBytes),
    ),
)


def _format_hex_for_log(data: bytes, length=200) -> str:
    return " ".join(re.findall("....", data.hex()[:length]))


class MscTransport:
    """Transport adapter that implements the mass storage class (MSC) interface to
    the TSE. This transport adapter uses a single file in the root directory of the
    user-accessible portion of the TSE, named ``TSE-IO.bin``, for communication.
    By writing to and reading from this file (bypassing OS buffers) request/response
    cycles can be exchanged with the TSE."""
예제 #9
0
    "_unknData" / C.Default(Byte[69], [0 for _ in range(69)]),
)

ClassDefinition = DataPointer(
    Int64ul,
    Struct("hash" / Int64ul,
           "members" / C.PrefixedArray(Int64ul, ClassMemberDefinition)),
    "definitionData")

ClassDefinitionList = C.FocusedSeq(
    "definitions", "_count" / C.Rebuild(Int32ul, C.len_(this.definitions)),
    "definitions" / C.Prefixed(
        Int32ul,
        C.Aligned(
            8,
            C.FocusedSeq(
                "definitions",
                "definitions" / ClassDefinition[this._._count],
                DataEntries("definitionData"),
                DataEntries("names"),
            ))))

# Hierarchy handling
varcount = 0


def varHandling(this):
    global varcount
    ret = varcount
    varcount += 1
    return ret
예제 #10
0
import functools
import socket

import construct

try:
    import anyio
except ImportError:
    ASYNC_AVAILABLE = False
else:
    ASYNC_AVAILABLE = True

PACKET_PARSER = construct.GreedyRange(
    construct.Prefixed(
        construct.Int32sl,
        construct.Struct("id" / construct.Int32sl, "type" / construct.Int32sl,
                         "body" / construct.CString("utf8"),
                         construct.Default(construct.CString("utf8"), ""))))


class RCONBaseError(Exception):
    """Exception base for all exceptions in this library"""


class ClientBusy(RCONBaseError):
    """Client is already busy with another call"""


class InvalidPassword(RCONBaseError):
    """RCON password is incorrect"""
예제 #11
0
 def struct(cls):
     return construct.Struct(
         "signature" / construct.Const(b"vf"),
         "version" / _LMArchiveVersionValidator(construct.Int32ul),
         "count" / construct.Int32ul,
         "filenames" / construct.Array(
             construct.this.count,
             construct.IfThenElse(
                 construct.this.version >= 100,
                 construct.Prefixed(
                     construct.Int32ul,
                     construct.Transformed(
                         construct.GreedyString('cp932'),
                         LMObfuscator().transform_bytes,
                         None,
                         LMObfuscator().transform_bytes,
                         None,
                     ),
                 ),
                 construct.PascalString(construct.Int32ul, 'cp932'),
             )),
         "offsets" / construct.Array(
             construct.this.count + 1,
             construct.Struct(
                 "offset_low" / construct.IfThenElse(
                     construct.this._.version >= 100,
                     construct.Transformed(
                         construct.Int32ul,
                         LMObfuscator().transform_int,
                         4,
                         LMObfuscator().transform_int,
                         4,
                     ),
                     construct.Int32ul,
                 ),
                 # offset_high always 0 if ver < 101
                 "offset_high" / construct.IfThenElse(
                     construct.this._.version >= 101,
                     construct.Transformed(
                         construct.Int32ul,
                         LMObfuscator().transform_int,
                         4,
                         LMObfuscator().transform_int_high,
                         4,
                     ),
                     construct.Int32ul,
                 ),
             ),
         ),
         "compress_types" /
         construct.Array(construct.this.count,
                         construct.Enum(construct.Byte, LMCompressType)),
         "unk1s" / construct.Array(
             construct.this.count,
             # construct.Transformed(
             #     construct.Int32ul,
             #     LMObfuscator().transform_int,
             #     4,
             #     LMObfuscator().transform_int,
             #     4,
             # ),
             construct.Int32ul,
         ),
         "checksums" / construct.Array(
             construct.this.count,
             construct.Int32ul,
         ),
         "encrypt_flags" / construct.Array(
             construct.this.count,
             construct.Byte,
         ),
     )
예제 #12
0
                    construct.PascalString(construct.Int16ub, 'utf-8'))),
        }))

RequestPacket = construct.Struct(
    'request_type' / construct.Enum(construct.Int8ub, RequestType),
    'data' / construct.Switch(
        lambda ctx: int(ctx.request_type), {
            RequestType.UPLOAD_IDB_START.value:
            construct.Struct(
                'idb_name' /
                construct.PascalString(construct.Int16ub, 'utf-8'),
                'idb_hash' / construct.Bytes(SHA1_HASH_BYTES_LENGTH),
                'idb_size' / construct.Int32ub),
            RequestType.IDB_CHUNK.value:
            construct.Struct('data' / construct.Prefixed(
                construct.VarInt,
                construct.Compressed(construct.GreedyBytes, 'zlib'))),
            RequestType.DOWNLOAD_IDB_START.value:
            construct.Struct('idb_name' / construct.PascalString(
                construct.Int16ub, 'utf-8')),
            RequestType.GET_ALL_IDBS_RESPONSE.value:
            construct.GreedyRange(
                construct.Struct(
                    "name" / construct.PascalString(construct.VarInt, "utf-8"),
                    "size" / construct.Int32ub))
        }))

RevetherPacket = construct.Struct(
    'header' / construct.Struct(
        'version' / construct.Const(LATEST_VERSION, construct.Int8ub),
        'type' / construct.Enum(construct.Int8ub, PacketType),
예제 #13
0
@attr.s
class Stat:
    type = u16()
    dev = u16()
    qid = struct(Qid)
    mode = file_mode()
    atime = u32()
    mtime = u32()
    length = u64()
    name = string()
    uid = string()
    gid = string()
    muid = string()


Stat._construct = construct.Prefixed(
    construct.Int16ul, construct.Prefixed(construct.Int16ul, Stat._construct))


# Tversion tag[2] msize[4] version[s]
# Rversion tag[2] msize[4] version[s]
@constructify
@attr.s
class Version:
    tag = u16()
    msize = u32()
    version = string()


Tversion = Version
Rversion = Version
예제 #14
0
import os, sys, binascii
from io import BytesIO

import construct as c

from formats.compact_uint import CompactUint
from formats.transaction import Transaction
from formats import bip174, protobuf

HARDENED_FLAG = 1 << 31

# current key-value based format:

KeyValue = c.Struct(
    "key" / c.Prefixed(CompactUint, c.Struct(
        "type" / c.Byte,
        "data" / c.GreedyBytes,
    )),
    "value" / c.Prefixed(CompactUint, c.GreedyBytes),
    "ofs" / c.Tell,
)

Sequence = c.FocusedSeq("content",
    "content" / c.GreedyRange(
        c.FocusedSeq(
            "keyvalue",
            "terminator" / c.Peek(c.Byte),
            c.StopIf(c.this.terminator == 0),
            "keyvalue" / KeyValue,
        )
    ),
    c.Const(b'\0'),
예제 #15
0
from . import CompactUint, Optional
from .transaction import Transaction, TxOutput

PSBT_PROPRIETARY_BYTE = 0xFC


class PsbtError(Exception):
    pass


# fmt: off
PsbtKeyValue = c.Struct(
    "key" / c.Prefixed(
        CompactUint,
        c.Struct(
            "type" / CompactUint,
            "data" / Optional(c.GreedyBytes),
        )),
    "value" / c.Prefixed(CompactUint, c.GreedyBytes),
)

PsbtProprietaryKey = c.Struct(
    "prefix" / c.CString("utf-8"),
    "subtype" / CompactUint,
    "data" / Optional(c.GreedyBytes),
)

PsbtSequence = c.FocusedSeq(
    "content",
    "content" / c.GreedyRange(PsbtKeyValue),
    c.Const(b"\0"),
예제 #16
0
    with the added twist that 0 means set and 1 means unset.
    We feed it to a `BitStruct` that expects a big-endian sequence where bits have
    the traditional meaning. We must therefore do a bitwise negation of each byte,
    and return them in reverse order. This is the same transformation both ways,
    fortunately, so we don't need two separate functions.
    """
    return bytes(~b & 0xFF for b in data)[::-1]


# fmt: off
Toif = c.Struct(
    "magic" / c.Const(b"TOI"),
    "format" / c.Enum(c.Byte, full_color=b"f", grayscale=b"g"),
    "width" / c.Int16ul,
    "height" / c.Int16ul,
    "data" / c.Prefixed(c.Int32ul, c.GreedyBytes),
)

VendorTrust = c.Transformed(
    c.BitStruct(
        "reserved" / c.Default(c.BitsInteger(9), 0),
        "show_vendor_string" / c.Flag,
        "require_user_click" / c.Flag,
        "red_background" / c.Flag,
        "delay" / c.BitsInteger(4),
    ), _transform_vendor_trust, 2, _transform_vendor_trust, 2)

VendorHeader = c.Struct(
    "_start_offset" / c.Tell,
    "magic" / c.Const(b"TRZV"),
    "_header_len" / c.Padding(4),
예제 #17
0

TransportDataTupleType = Tuple[TransportDataType, Union[bytes, int, str,
                                                        List[int]]]

TRANSPORT_DATA_PARAMETER = construct.Select(
    # BYTE
    construct.Struct(
        "data_type" / construct.Const(bytes([TransportDataType.BYTE])),
        construct.Const(bytes([0x00, 0x01])),
        "data" / construct.Byte,
    ),
    # BYTE_ARRAY
    construct.Struct(
        "data_type" / construct.Const(bytes([TransportDataType.BYTE_ARRAY])),
        "data" / construct.Prefixed(construct.Int16ub, construct.GreedyBytes),
    ),
    # SHORT
    construct.Struct(
        "data_type" / construct.Const(bytes([TransportDataType.SHORT])),
        construct.Const(bytes([0x00, 0x02])),
        "data" / construct.Int16ub,
    ),
    # STRING
    construct.Struct(
        "data_type" / construct.Const(bytes([TransportDataType.STRING])),
        "data" /
        construct.Prefixed(construct.Int16ub, construct.GreedyString("ascii")),
    ),
    # LONG_ARRAY
    construct.Struct(