scapy.cbor.cborfields

Classes that implement CBOR (Concise Binary Object Representation) data structures as packet fields. Modelled after scapy/asn1fields.py.

class scapy.cbor.cborfields.CBORF_ARRAY(*seq: Any, **kwargs: Any)[source]

Bases: CBORF_field[List[Any], List[Any]]

CBOR array with a fixed sequence of named, typed fields (major type 4). Analogous to ASN1F_SEQUENCE: each positional element corresponds to a specific CBORF_field. The CBOR array count must match the number of declared fields.

Example:

class MyCBOR(CBOR_Packet):
    CBOR_root = CBORF_ARRAY(
        CBORF_INTEGER("version", 1),
        CBORF_TEXT_STRING("name", ""),
    )
CBOR_tag = <CBORTag ARRAY[4]>
build(pkt: CBOR_Packet) bytes[source]
dissect(pkt: Any, s: bytes) bytes[source]
get_fields_list() List[CBORF_field[Any, Any]][source]
holds_packets = 1
is_empty(pkt: CBOR_Packet) bool[source]
m2i(pkt: Any, s: bytes) Tuple[Any, bytes][source]

Decode a CBOR array. Each element is decoded by its corresponding field in self.seq. The decoded values are set directly on the packet by each field’s dissect call, so this method returns an empty list (which is discarded by dissect).

class scapy.cbor.cborfields.CBORF_ARRAY_OF(name: str, default: Any, cls: _ARRAY_T)[source]

Bases: CBORF_field[List[CBOR_Packet | Type[CBORF_field[Any, Any]] | CBORF_PACKET | CBORF_field[Any, Any]], List[CBOR_Object[Any]]]

CBOR array of homogeneous elements (major type 4). Analogous to ASN1F_SEQUENCE_OF: variable-length array where every element shares the same type, specified by cls.

cls may be a CBORF_field class/instance (leaf type) or a CBOR_Packet subclass (structured type).

CBOR_tag = <CBORTag ARRAY[4]>
build(pkt: CBOR_Packet) bytes[source]
i2repr(pkt: CBOR_Packet, x: Any) str[source]
is_empty(pkt: CBOR_Packet) bool[source]
islist = 1
m2i(pkt: CBOR_Packet, s: bytes) Tuple[List[Any], bytes][source]
class scapy.cbor.cborfields.CBORF_BOOLEAN(name: str, default: _A | None)[source]

Bases: CBORF_field[bool, CBOR_FALSE | CBOR_TRUE]

CBOR boolean field (major type 7, simple values 20/21).

CBOR_tag = <CBORTag SIMPLE_AND_FLOAT[7]>
i2m(pkt: CBOR_Packet, x: Any) bytes[source]
m2i(pkt: CBOR_Packet, s: bytes) Tuple[CBOR_FALSE | CBOR_TRUE, bytes][source]
randval() RandChoice[source]
class scapy.cbor.cborfields.CBORF_BYTE_STRING(name: str, default: _A | None)[source]

Bases: CBORF_field[bytes, CBOR_BYTE_STRING]

CBOR byte string field (major type 2).

CBOR_tag = <CBORTag BYTE_STRING[2]>
m2i(pkt: CBOR_Packet, s: bytes) Tuple[CBOR_BYTE_STRING, bytes][source]
randval() RandString[source]
class scapy.cbor.cborfields.CBORF_FLOAT(name: str, default: _A | None)[source]

Bases: CBORF_field[float, CBOR_FLOAT]

CBOR float field (major type 7, double precision).

CBOR_tag = <CBORTag SIMPLE_AND_FLOAT[7]>
i2m(pkt: CBOR_Packet, x: Any) bytes[source]
m2i(pkt: CBOR_Packet, s: bytes) Tuple[CBOR_FLOAT, bytes][source]
randval() RandFloat[source]
class scapy.cbor.cborfields.CBORF_INTEGER(name: str, default: _A | None)[source]

Bases: CBORF_field[int, CBOR_UNSIGNED_INTEGER | CBOR_NEGATIVE_INTEGER]

CBOR integer field handling both positive and negative values.

i2m(pkt: CBOR_Packet, x: Any) bytes[source]
m2i(pkt: CBOR_Packet, s: bytes) Tuple[CBOR_UNSIGNED_INTEGER | CBOR_NEGATIVE_INTEGER, bytes][source]
randval() RandNum[source]
class scapy.cbor.cborfields.CBORF_MAP(*seq: Any, **kwargs: Any)[source]

Bases: CBORF_field[Dict[str, Any], Dict[str, Any]]

CBOR map with a fixed set of named, typed fields (major type 5).

Each field in seq represents one key-value pair. The key is the field’s name encoded as a CBOR text string. The value is encoded and decoded by the corresponding CBORF_field.

Example:

class MyCBOR(CBOR_Packet):
    CBOR_root = CBORF_MAP(
        CBORF_INTEGER("version", 1),
        CBORF_TEXT_STRING("name", ""),
    )
CBOR_tag = <CBORTag MAP[5]>
build(pkt: CBOR_Packet) bytes[source]
dissect(pkt: Any, s: bytes) bytes[source]
get_fields_list() List[CBORF_field[Any, Any]][source]
holds_packets = 1
is_empty(pkt: CBOR_Packet) bool[source]
m2i(pkt: Any, s: bytes) Tuple[Any, bytes][source]

Decode a CBOR map. Keys are decoded as CBOR items and matched to fields by name. Values are decoded by the matching field. Unknown keys are silently skipped.

class scapy.cbor.cborfields.CBORF_NEGATIVE_INTEGER(name: str, default: _A | None)[source]

Bases: CBORF_field[int, CBOR_NEGATIVE_INTEGER]

CBOR negative integer field (major type 1).

CBOR_tag = <CBORTag NEGATIVE_INTEGER[1]>
m2i(pkt: CBOR_Packet, s: bytes) Tuple[CBOR_NEGATIVE_INTEGER, bytes][source]
randval() RandNum[source]
class scapy.cbor.cborfields.CBORF_NULL(name: str, default: None = None)[source]

Bases: CBORF_field[None, CBOR_NULL]

CBOR null field (major type 7, simple value 22).

CBOR_tag = <CBORTag SIMPLE_AND_FLOAT[7]>
i2m(pkt: CBOR_Packet, x: Any) bytes[source]
is_empty(pkt: CBOR_Packet) bool[source]
m2i(pkt: CBOR_Packet, s: bytes) Tuple[CBOR_NULL, bytes][source]
class scapy.cbor.cborfields.CBORF_PACKET(name: str, default: CBOR_Packet | None, cls: Type[CBOR_Packet])[source]

Bases: CBORF_field[CBOR_Packet, CBOR_Packet | None]

CBOR field that encapsulates a nested CBOR_Packet.

The nested packet is encoded as-is (its CBOR_root.build() output) and decoded by instantiating cls from the current byte stream.

any2i(pkt: CBOR_Packet, x: Any) CBOR_Packet[source]
holds_packets = 1
i2m(pkt: CBOR_Packet, x: Any) bytes[source]
m2i(pkt: CBOR_Packet, s: bytes) Tuple[Any, bytes][source]
randval() CBOR_Packet[source]
class scapy.cbor.cborfields.CBORF_SEMANTIC_TAG(name: str, default: Any, tag_num: int, inner_field: CBORF_field[Any, Any])[source]

Bases: CBORF_field[Tuple[int, Any], CBOR_SEMANTIC_TAG]

CBOR semantic tag field (major type 6).

Wraps an inner_field with the given numeric tag_num. The inner field handles encoding and decoding of the tagged value. The outer field (named name) stores the CBOR_SEMANTIC_TAG wrapper (tag number + None placeholder), while the inner field stores its value under its own name on the packet.

Example:

class TimestampPkt(CBOR_Packet):
    CBOR_root = CBORF_SEMANTIC_TAG(
        "tag_info", None, 1, CBORF_INTEGER("ts", 0)
    )
CBOR_tag = <CBORTag TAG[6]>
build(pkt: CBOR_Packet) bytes[source]
dissect(pkt: CBOR_Packet, s: bytes) bytes[source]
get_fields_list() List[CBORF_field[Any, Any]][source]
m2i(pkt: CBOR_Packet, s: bytes) Tuple[CBOR_SEMANTIC_TAG, bytes][source]
class scapy.cbor.cborfields.CBORF_TEXT_STRING(name: str, default: _A | None)[source]

Bases: CBORF_field[str, CBOR_TEXT_STRING]

CBOR text string field (major type 3).

CBOR_tag = <CBORTag TEXT_STRING[3]>
m2i(pkt: CBOR_Packet, s: bytes) Tuple[CBOR_TEXT_STRING, bytes][source]
randval() RandString[source]
class scapy.cbor.cborfields.CBORF_UNDEFINED(name: str, default: None = None)[source]

Bases: CBORF_field[None, CBOR_UNDEFINED]

CBOR undefined field (major type 7, simple value 23).

CBOR_tag = <CBORTag SIMPLE_AND_FLOAT[7]>
i2m(pkt: CBOR_Packet, x: Any) bytes[source]
is_empty(pkt: CBOR_Packet) bool[source]
m2i(pkt: CBOR_Packet, s: bytes) Tuple[CBOR_UNDEFINED, bytes][source]
class scapy.cbor.cborfields.CBORF_UNSIGNED_INTEGER(name: str, default: _A | None)[source]

Bases: CBORF_field[int, CBOR_UNSIGNED_INTEGER]

CBOR unsigned integer field (major type 0).

CBOR_tag = <CBORTag UNSIGNED_INTEGER[0]>
m2i(pkt: CBOR_Packet, s: bytes) Tuple[CBOR_UNSIGNED_INTEGER, bytes][source]
randval() RandNum[source]
exception scapy.cbor.cborfields.CBORF_badsequence[source]

Bases: Exception

class scapy.cbor.cborfields.CBORF_element[source]

Bases: object

class scapy.cbor.cborfields.CBORF_field(name: str, default: _A | None)[source]

Bases: CBORF_element, Generic[_I, _A]

CBOR_tag: Any | None = None
any2i(pkt: CBOR_Packet, x: Any) _I[source]
build(pkt: CBOR_Packet) bytes[source]
copy() CBORF_field[_I, _A][source]
default: _A | None
dissect(pkt: CBOR_Packet, s: bytes) bytes[source]
do_copy(x: Any) Any[source]
extract_packet(cls: Type[CBOR_Packet], s: bytes, _underlayer: CBOR_Packet | None = None) Tuple[CBOR_Packet, bytes][source]
get_fields_list() List[CBORF_field[Any, Any]][source]
holds_packets = 0
i2h(pkt: CBOR_Packet, x: _I) Any[source]
i2m(pkt: CBOR_Packet, x: bytes | _I | _A) bytes[source]
i2repr(pkt: CBOR_Packet, x: _I) str[source]
is_empty(pkt: CBOR_Packet) bool[source]
islist = 0
m2i(pkt: CBOR_Packet, s: bytes) Tuple[_A, bytes][source]
owners: List[Type[CBOR_Packet]]
randval() RandField[_I][source]
register_owner(cls: Type[CBOR_Packet]) None[source]
set_val(pkt: CBOR_Packet, val: Any) None[source]
class scapy.cbor.cborfields.CBORF_optional(field: CBORF_field[Any, Any])[source]

Bases: CBORF_element

Wrapper making a CBORF_field optional.

During decoding, if the next CBOR item does not match the expected major type, the field value is set to None and the stream is left unchanged.

any2i(pkt: CBOR_Packet, x: Any) Any[source]
build(pkt: CBOR_Packet) bytes[source]
dissect(pkt: CBOR_Packet, s: bytes) bytes[source]
i2repr(pkt: CBOR_Packet, x: Any) str[source]
m2i(pkt: CBOR_Packet, s: bytes) Tuple[Any, bytes][source]