scapy.cbor package

Package holding CBOR (Concise Binary Object Representation) related modules. Follows the same paradigm as ASN.1 implementation.

class scapy.cbor.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.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.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.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.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.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.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.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.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.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.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.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.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.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]
class scapy.cbor.CBORF_element[source]

Bases: object

class scapy.cbor.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.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]
class scapy.cbor.CBOR_ARRAY(val: _K)[source]

Bases: CBOR_Object[List[Any]]

CBOR array (major type 4)

strshow(lvl: int = 0) str[source]
tag = <CBORTag ARRAY[4]>
class scapy.cbor.CBOR_BYTE_STRING(val: _K)[source]

Bases: CBOR_Object[bytes]

CBOR byte string (major type 2)

tag = <CBORTag BYTE_STRING[2]>
exception scapy.cbor.CBOR_BadTag_Decoding_Error[source]

Bases: CBOR_Decoding_Error

class scapy.cbor.CBOR_Codecs[source]

Bases: object

CBOR = <CBORCodec CBOR[1]>
class scapy.cbor.CBOR_DECODING_ERROR(val: bytes | CBOR_Object[Any], exc: Exception | None = None)[source]

Bases: _CBOR_ERROR

CBOR decoding error object

enc(codec: Any = None) bytes[source]
exception scapy.cbor.CBOR_Decoding_Error[source]

Bases: CBOR_Error

exception scapy.cbor.CBOR_Encoding_Error[source]

Bases: CBOR_Error

exception scapy.cbor.CBOR_Error[source]

Bases: Scapy_Exception

class scapy.cbor.CBOR_FALSE[source]

Bases: CBOR_Object[bool]

CBOR false value

tag = <CBORTag SIMPLE_AND_FLOAT[7]>
class scapy.cbor.CBOR_FLOAT(val: _K)[source]

Bases: CBOR_Object[float]

CBOR floating-point number (major type 7)

tag = <CBORTag SIMPLE_AND_FLOAT[7]>
class scapy.cbor.CBOR_MAP(val: _K)[source]

Bases: CBOR_Object[Dict[Any, Any]]

CBOR map (major type 5)

strshow(lvl: int = 0) str[source]
tag = <CBORTag MAP[5]>
class scapy.cbor.CBOR_MajorTypes[source]

Bases: object

CBOR Major Types (RFC 8949)

ARRAY = <CBORTag ARRAY[4]>
BYTE_STRING = <CBORTag BYTE_STRING[2]>
MAP = <CBORTag MAP[5]>
NEGATIVE_INTEGER = <CBORTag NEGATIVE_INTEGER[1]>
SIMPLE_AND_FLOAT = <CBORTag SIMPLE_AND_FLOAT[7]>
TAG = <CBORTag TAG[6]>
TEXT_STRING = <CBORTag TEXT_STRING[3]>
UNSIGNED_INTEGER = <CBORTag UNSIGNED_INTEGER[0]>
name = 'CBOR_MAJOR_TYPES'
class scapy.cbor.CBOR_NEGATIVE_INTEGER(val: _K)[source]

Bases: CBOR_Object[int]

CBOR negative integer (major type 1)

tag = <CBORTag NEGATIVE_INTEGER[1]>
class scapy.cbor.CBOR_NULL[source]

Bases: CBOR_Object[None]

CBOR null value

tag = <CBORTag SIMPLE_AND_FLOAT[7]>
class scapy.cbor.CBOR_Object(val: _K)[source]

Bases: Generic[_K]

Base class for CBOR value objects

enc(codec: Any = None) bytes[source]
show(lvl: int = 0) None[source]
strshow(lvl: int = 0) str[source]
tag = None
class scapy.cbor.CBOR_SEMANTIC_TAG(val: _K)[source]

Bases: CBOR_Object[Tuple[int, Any]]

CBOR semantic tag (major type 6)

tag = <CBORTag TAG[6]>
class scapy.cbor.CBOR_SIMPLE_VALUE(val: _K)[source]

Bases: CBOR_Object[int]

CBOR simple value (major type 7)

tag = <CBORTag SIMPLE_AND_FLOAT[7]>
class scapy.cbor.CBOR_TEXT_STRING(val: _K)[source]

Bases: CBOR_Object[str]

CBOR text string (major type 3)

tag = <CBORTag TEXT_STRING[3]>
class scapy.cbor.CBOR_TRUE[source]

Bases: CBOR_Object[bool]

CBOR true value

tag = <CBORTag SIMPLE_AND_FLOAT[7]>
class scapy.cbor.CBOR_UNDEFINED[source]

Bases: CBOR_Object[None]

CBOR undefined value

tag = <CBORTag SIMPLE_AND_FLOAT[7]>
class scapy.cbor.CBOR_UNSIGNED_INTEGER(val: _K)[source]

Bases: CBOR_Object[int]

CBOR unsigned integer (major type 0)

tag = <CBORTag UNSIGNED_INTEGER[0]>
class scapy.cbor.CBORcodec_ARRAY[source]

Bases: CBORcodec_Object[List[Any]]

CBOR array codec (major type 4)

classmethod do_dec(s: bytes, context: Any | None = None, safe: bool = False) Tuple[CBOR_Object[List[Any]], bytes][source]
classmethod enc(obj: List[Any] | CBOR_Object[List[Any]]) bytes[source]
tag = <CBORTag ARRAY[4]>
class scapy.cbor.CBORcodec_BYTE_STRING[source]

Bases: CBORcodec_Object[bytes]

CBOR byte string codec (major type 2)

classmethod do_dec(s: bytes, context: Any | None = None, safe: bool = False) Tuple[CBOR_Object[bytes], bytes][source]
classmethod enc(obj: bytes | CBOR_Object[bytes]) bytes[source]
tag = <CBORTag BYTE_STRING[2]>
class scapy.cbor.CBORcodec_MAP[source]

Bases: CBORcodec_Object[Dict[Any, Any]]

CBOR map codec (major type 5)

classmethod do_dec(s: bytes, context: Any | None = None, safe: bool = False) Tuple[CBOR_Object[Dict[Any, Any]], bytes][source]
classmethod enc(obj: Dict[Any, Any] | CBOR_Object[Dict[Any, Any]]) bytes[source]
tag = <CBORTag MAP[5]>
class scapy.cbor.CBORcodec_NEGATIVE_INTEGER[source]

Bases: CBORcodec_Object[int]

CBOR negative integer codec (major type 1)

classmethod do_dec(s: bytes, context: Any | None = None, safe: bool = False) Tuple[CBOR_Object[int], bytes][source]
classmethod enc(obj: int | CBOR_Object[int]) bytes[source]
tag = <CBORTag NEGATIVE_INTEGER[1]>
class scapy.cbor.CBORcodec_Object[source]

Bases: Generic[_K]

Base CBOR codec class

classmethod cbor_object(val: _K) CBOR_Object[_K][source]
classmethod check_string(s: bytes) None[source]
codec = <CBORCodec CBOR[1]>
classmethod dec(s: bytes, context: Any | None = None, safe: bool = False) Tuple[_CBOR_ERROR | CBOR_Object[_K], bytes][source]
static decode_cbor_item(s: bytes, safe: bool = False) Tuple[CBOR_Object[Any], bytes][source]

Decode CBOR bytes to a CBOR_Object

classmethod do_dec(s: bytes, context: Any | None = None, safe: bool = False) Tuple[CBOR_Object[Any], bytes][source]

Decode CBOR data using automatic dispatch based on major type.

classmethod enc(s: _K) bytes[source]
static encode_cbor_item(item: Any) bytes[source]

Encode a Python value to CBOR bytes

classmethod safedec(s: bytes, context: Any | None = None) Tuple[_CBOR_ERROR | CBOR_Object[_K], bytes][source]
tag = <CBORTag UNSIGNED_INTEGER[0]>
class scapy.cbor.CBORcodec_SEMANTIC_TAG[source]

Bases: CBORcodec_Object[Tuple[int, Any]]

CBOR semantic tag codec (major type 6)

classmethod do_dec(s: bytes, context: Any | None = None, safe: bool = False) Tuple[CBOR_Object[Tuple[int, Any]], bytes][source]
classmethod enc(obj: Tuple[int, Any] | CBOR_Object[Tuple[int, Any]]) bytes[source]
tag = <CBORTag TAG[6]>
class scapy.cbor.CBORcodec_SIMPLE_AND_FLOAT[source]

Bases: CBORcodec_Object[int | float | bool | None]

CBOR simple values and floats codec (major type 7)

classmethod do_dec(s: bytes, context: Any | None = None, safe: bool = False) Tuple[CBOR_Object[Any], bytes][source]
classmethod enc(obj: int | float | bool | None | CBOR_Object[Any]) bytes[source]
tag = <CBORTag SIMPLE_AND_FLOAT[7]>
class scapy.cbor.CBORcodec_TEXT_STRING[source]

Bases: CBORcodec_Object[str]

CBOR text string codec (major type 3)

classmethod do_dec(s: bytes, context: Any | None = None, safe: bool = False) Tuple[CBOR_Object[str], bytes][source]
classmethod enc(obj: str | CBOR_Object[str]) bytes[source]
tag = <CBORTag TEXT_STRING[3]>
class scapy.cbor.CBORcodec_UNSIGNED_INTEGER[source]

Bases: CBORcodec_Object[int]

CBOR unsigned integer codec (major type 0)

classmethod do_dec(s: bytes, context: Any | None = None, safe: bool = False) Tuple[CBOR_Object[int], bytes][source]
classmethod enc(obj: int | CBOR_Object[int]) bytes[source]
tag = <CBORTag UNSIGNED_INTEGER[0]>
class scapy.cbor.RandCBORObject(objlist: List[Type[CBOR_Object[Any]]] | None = None)[source]

Bases: RandField[CBOR_Object[Any]]

Random CBOR object generator for fuzzing

Submodules