scapy.fields

Fields: basic data structures that make up parts of packets.

class scapy.fields.ActionField(fld: Field[Any, Any], action_method: str, **kargs: Any)[源代码]

基类:_FieldContainer

any2i(pkt: Packet | None, val: int) Any[源代码]
fld
class scapy.fields.BCDFloatField(name: str, default: Any, fmt: str = 'H')[源代码]

基类:Field[float, int]

i2m(pkt: Packet | None, x: float | None) int[源代码]
m2i(pkt: Packet | None, x: int) float[源代码]
class scapy.fields.BitEnumField(name: str, default: int | None, size: int, enum: Dict[int, str], **kwargs: Any)[源代码]

基类:_BitField[Union[List[int], int]], _EnumField[int]

any2i(pkt: Packet | None, x: Any) List[int] | int[源代码]
i2repr(pkt: Packet | None, x: List[int] | int) Any[源代码]
i2s: Dict[I, str] | None
i2s_cb: Callable[[I], str] | None
s2i: Dict[str, I] | None
s2i_cb: Callable[[str], I] | None
class scapy.fields.BitExtendedField(name: str, default: Any | None, extension_bit: int)[源代码]

基类:Field[Optional[int], bytes]

Bit Extended Field

This type of field has a variable number of bytes. Each byte is defined as follows: - 7 bits of data - 1 bit an an extension bit:

  • 0 means it is last byte of the field ("stopping bit")

  • 1 means there is another byte after this one ("forwarding bit")

To get the actual data, it is necessary to hop the binary data byte per byte and to check the extension bit until 0

addfield(pkt: Packet | None, s: bytes, val: int | None) bytes[源代码]
extended2str(x: int | None) bytes[源代码]
extension_bit
getfield(pkt: Any | None, s: bytes) Tuple[bytes, int | None][源代码]
i2m(pkt: Any | None, x: int | None) bytes[源代码]
m2i(pkt: Any | None, x: bytes) int | None[源代码]
prepare_byte(x: int) int[源代码]
str2extended(x: bytes = b'') Tuple[bytes, int | None][源代码]
class scapy.fields.BitField(name: str, default: I | None, size: int, tot_size: int = 0, end_tot_size: int = 0)[源代码]

基类:_BitField[int]

Field to handle bits.

参数:
  • name -- name of the field

  • default -- default value

  • size -- size (in bits). If negative, Low endian

  • tot_size -- size of the total group of bits (in bytes) the bitfield is in. If negative, Low endian.

  • end_tot_size -- same but for the BitField ending a group.

Example - normal usage:

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|             A             |               B               | C |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

                         Fig. TestPacket

class TestPacket(Packet):
    fields_desc = [
        BitField("a", 0, 14),
        BitField("b", 0, 16),
        BitField("c", 0, 2),
    ]

Example - Low endian stored as 16 bits on the network:

x x x x x x x x x x x x x x x x
a [b] [   c   ] [      a      ]

Will first get reversed during dissecion:

x x x x x x x x x x x x x x x x
[      a        ] [b] [   c   ]

class TestPacket(Packet):
    fields_desc = [
        BitField("a", 0, 9, tot_size=-2),
        BitField("b", 0, 2),
        BitField("c", 0, 5, end_tot_size=-2)
    ]
class scapy.fields.BitFieldLenField(name: str, default: Optional[int], size: int, length_of: Optional[Union[Callable[[Optional[Packet]], int], str]]  # noqa: E501 = None, count_of: Optional[str] = None, adjust: Callable[[Optional[Packet], int], int]  # noqa: E501 = <function BitFieldLenField.<lambda>>, tot_size: int = 0, end_tot_size: int = 0)[源代码]

基类:BitField

adjust
count_of
end_tot_size
i2m(pkt: Packet | None, x: Any | None) int[源代码]
length_of
tot_size
class scapy.fields.BitFixedLenField(name: str, default: int | None, length_from: Callable[[Packet], int])[源代码]

基类:BitField

addfield(pkt: Packet, s: Tuple[bytes, int, int] | bytes, val: int) Tuple[bytes, int, int] | bytes[源代码]
getfield(pkt: Packet, s: Tuple[bytes, int] | bytes) Tuple[Tuple[bytes, int], int] | Tuple[bytes, int][源代码]
length_from
class scapy.fields.BitMultiEnumField(name: str, default: int, size: int, enum: Dict[int, Dict[int, str]], depends_on: Callable[[Packet | None], int])[源代码]

基类:_BitField[Union[List[int], int]], _MultiEnumField[int]

any2i(pkt: Packet | None, x: Any) List[int] | int[源代码]
depends_on
i2repr(pkt: Packet | None, x: List[int] | int) str | List[str][源代码]
i2s: Dict[I, str] | None
i2s_cb: Callable[[I], str] | None
i2s_multi
s2i: Dict[str, I] | None
s2i_all: Dict[str, I]
s2i_cb: Callable[[str], I] | None
s2i_multi: Dict[I, Dict[str, I]]
class scapy.fields.BitScalingField(name: str, default: int, size: int, *args: Any, **kwargs: Any)[源代码]

基类:_ScalingField, BitField

A ScalingField that is a BitField

class scapy.fields.BoundStrLenField(name: str, default: bytes, minlen: int = 0, maxlen: int = 255, length_from: Callable[[Packet], int] | None = None)[源代码]

基类:StrLenField

maxlen
minlen
randval() RandBin[源代码]
class scapy.fields.ByteEnumField(name: str, default: int | None, enum: Dict[int, str])[源代码]

基类:EnumField[int]

class scapy.fields.ByteEnumKeysField(name: str, default: int | None, enum: Dict[int, str])[源代码]

基类:ByteEnumField

ByteEnumField that picks valid values when fuzzed.

randval() RandEnumKeys[源代码]
class scapy.fields.ByteField(name: str, default: int | None)[源代码]

基类:Field[int, int]

class scapy.fields.CharEnumField(name: str, default: str, enum: Dict[str, str] | Tuple[Callable[[str], str], Callable[[str], str]], fmt: str = '1s')[源代码]

基类:EnumField[str]

any2i_one(pkt: Packet | None, x: str) str[源代码]
class scapy.fields.ConditionalField(fld: AnyField, cond: Callable[[Packet], bool])[源代码]

基类:_FieldContainer

addfield(pkt: Packet, s: bytes, val: Any) bytes[源代码]
any2i(pkt: Packet | None, x: Any) Any[源代码]
cond
fld
getfield(pkt: Packet, s: bytes) Tuple[bytes, Any][源代码]
i2h(pkt: Packet | None, val: Any) Any[源代码]
class scapy.fields.DestField(name: str, default: str)[源代码]

基类:Field[str, bytes]

classmethod bind_addr(layer: Type[Packet], addr: str, **condition: Any) None[源代码]
bindings: Dict[Type[Packet], Tuple[str, Any]] = {}
defaultdst
dst_from_pkt(pkt: Packet) str[源代码]
class scapy.fields.DestIP6Field(name: str, default: str)[源代码]

基类:IP6Field, DestField

bindings: Dict[Type[Packet], Tuple[str, Any]] = {<class 'scapy.contrib.ospf.OSPFv3_Hdr'>: [('ff02::5', {})], <class 'scapy.layers.inet.UDP'>: [('ff02::fb', {'dport': 5353}), ('ff02::66', {'dport': 2029})]}
i2h(pkt: Packet | None, x: str | Net6 | None) str[源代码]
i2m(pkt: Packet | None, x: str | Net6 | None) bytes[源代码]
class scapy.fields.Emph(fld: Any)[源代码]

基类:_FieldContainer

Empathize sub-layer for display

fld
class scapy.fields.EnumField(name: str, default: I | None, enum: Dict[I, str] | Dict[str, I] | List[str] | DADict[I, str] | Type[Enum] | Tuple[Callable[[I], str], Callable[[str], I]], fmt: str = 'H')[源代码]

基类:_EnumField[I]

i2s: Dict[I, str] | None
i2s_cb: Callable[[I], str] | None
s2i: Dict[str, I] | None
s2i_cb: Callable[[str], I] | None
class scapy.fields.FCSField(*args: Any, **kwargs: Any)[源代码]

基类:TrailerField

A FCS field that gets appended at the end of the packet (not layer).

fld
i2repr(pkt: Packet | None, x: int) str[源代码]
class scapy.fields.Field(name: str, default: Any, fmt: str = 'H')[源代码]

基类:Generic[I, M]

For more information on how this works, please refer to the 'Adding new protocols' chapter in the online documentation:

https://scapy.readthedocs.io/en/stable/build_dissect.html

addfield(pkt: Packet, s: bytes, val: I | None) bytes[源代码]

Add an internal value to a string

Copy the network representation of field val (belonging to layer pkt) to the raw string packet s, and return the new string packet.

any2i(pkt: Packet | None, x: Any) I | None[源代码]

Try to understand the most input values possible and make an internal value from them

copy() Field[I, M][源代码]
default
do_copy(x: I) I[源代码]
fmt
getfield(pkt: Packet, s: bytes) Tuple[bytes, I][源代码]

Extract an internal value from a string

Extract from the raw packet s the field value belonging to layer pkt.

Returns a two-element list, first the raw packet string after having removed the extracted field, second the extracted field itself in internal representation.

h2i(pkt: Packet | None, x: Any) I[源代码]

Convert human value to internal value

holds_packets = 0
i2count(pkt: Packet | None, x: I) int[源代码]

Convert internal value to a number of elements usable by a FieldLenField. Always 1 except for list fields

i2h(pkt: Packet | None, x: I) Any[源代码]

Convert internal value to human value

i2len(pkt: Packet, x: Any) int[源代码]

Convert internal value to a length usable by a FieldLenField

i2m(pkt: Packet | None, x: I | None) M[源代码]

Convert internal value to machine value

i2repr(pkt: Packet | None, x: I) str[源代码]

Convert internal value to a nice representation

islist = 0
ismutable = False
m2i(pkt: Packet | None, x: M) I[源代码]

Convert machine value to internal value

name
owners: List[Type[Packet]]
randval() VolatileValue[Any][源代码]

Return a volatile object whose value is both random and suitable for this field

register_owner(cls: Type[Packet]) None[源代码]
struct
sz: int
exception scapy.fields.FieldAttributeException[源代码]

基类:Scapy_Exception

class scapy.fields.FieldLenField(name: str, default: Optional[Any], length_of: Optional[str] = None, fmt: str = 'H', count_of: Optional[str] = None, adjust: Callable[[Packet, int], int] = <function FieldLenField.<lambda>>)[源代码]

基类:Field[int, int]

adjust
count_of
i2m(pkt: Packet | None, x: int | None) int[源代码]
length_of
class scapy.fields.FieldListField(name: str, default: List[AnyField] | None, field: AnyField, length_from: Callable[[Packet], int] | None = None, count_from: Callable[[Packet], int] | None = None, max_count: int | None = None)[源代码]

基类:Field[List[Any], List[Any]]

addfield(pkt: Packet, s: bytes, val: List[Any] | None) bytes[源代码]
any2i(pkt: Packet | None, x: List[Any]) List[Any][源代码]
count_from
field
getfield(pkt: Packet, s: bytes) Any[源代码]
i2count(pkt: Packet | None, val: List[Any]) int[源代码]
i2len(pkt: Packet, val: List[Any]) int[源代码]
i2repr(pkt: Packet | None, x: List[Any]) str[源代码]
islist = 1
length_from
max_count
exception scapy.fields.FieldValueRangeException[源代码]

基类:Scapy_Exception

class scapy.fields.FixedPointField(name: str, default: int, size: int, frac_bits: int = 16)[源代码]

基类:BitField

any2i(pkt: Packet | None, val: float | None) int | None[源代码]
frac_bits
i2h(pkt: Packet | None, val: int | None) EDecimal | None[源代码]
i2repr(pkt: Packet | None, val: int) str[源代码]
class scapy.fields.FlagValue(value: List[str] | int | str, names: List[str] | str)[源代码]

基类:object

copy() FlagValue[源代码]
flagrepr() str[源代码]
multi
names
value
class scapy.fields.FlagValueIter(flagvalue: FlagValue)[源代码]

基类:object

cursor
flagvalue
next() str[源代码]
class scapy.fields.FlagsField(name: str, default: int | FlagValue | None, size: int, names: List[str] | str | Dict[int, str])[源代码]

基类:_BitField[Optional[Union[int, FlagValue]]]

Handle Flag type field

Make sure all your flags have a label

Example (list):
>>> from scapy.packet import Packet
>>> class FlagsTest(Packet):
        fields_desc = [FlagsField("flags", 0, 8, ["f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7"])]  # noqa: E501
>>> FlagsTest(flags=9).show2()
###[ FlagsTest ]###
  flags     = f0+f3
Example (str):
>>> from scapy.packet import Packet
>>> class TCPTest(Packet):
        fields_desc = [
            BitField("reserved", 0, 7),
            FlagsField("flags", 0x2, 9, "FSRPAUECN")
        ]
>>> TCPTest(flags=3).show2()
###[ FlagsTest ]###
  reserved  = 0
  flags     = FS
Example (dict):
>>> from scapy.packet import Packet
>>> class FlagsTest2(Packet):
        fields_desc = [
            FlagsField("flags", 0x2, 16, {
                0x0001: "A",
                0x0008: "B",
            })
        ]
参数:
  • name -- field's name

  • default -- default value for the field

  • size -- number of bits in the field (in bits). if negative, LE

  • names -- (list or str or dict) label for each flag If it's a str or a list, the least Significant Bit tag's name is written first.

any2i(pkt: Packet | None, x: Any) FlagValue | None[源代码]
i2h(pkt: Packet | None, x: Any) FlagValue | None[源代码]
i2repr(pkt: Packet | None, x: Any) str[源代码]
ismutable = True
m2i(pkt: Packet | None, x: int) FlagValue | None[源代码]
names
class scapy.fields.IEEEDoubleField(name: str, default: int | None)[源代码]

基类:Field[int, int]

class scapy.fields.IEEEFloatField(name: str, default: int | None)[源代码]

基类:Field[int, int]

class scapy.fields.IP6Field(name: str, default: str | None)[源代码]

基类:Field[Optional[Union[str, Net6]], bytes]

any2i(pkt: Packet | None, x: str | None) str[源代码]
h2i(pkt: Packet | None, x: str | None) str[源代码]
i2h(pkt: Packet | None, x: str | Net6 | None) str[源代码]
i2m(pkt: Packet | None, x: str | Net6 | None) bytes[源代码]
i2repr(pkt: Packet | None, x: str | Net6 | None) str[源代码]
m2i(pkt: Packet | None, x: bytes) str[源代码]
randval() RandIP6[源代码]
class scapy.fields.IP6PrefixField(name: str, default: Tuple[str, int], wordbytes: int = 1, length_from: Callable[[Packet], int] | None = None)[源代码]

基类:_IPPrefixFieldBase

class scapy.fields.IPField(name: str, default: str | None)[源代码]

基类:Field[Union[str, Net], bytes]

any2i(pkt: Packet | None, x: Any) Any[源代码]
h2i(pkt: Packet | None, x: AnyStr | List[AnyStr]) Any[源代码]
i2h(pkt: Packet | None, x: str | Net | None) str[源代码]
i2m(pkt: Packet | None, x: str | Net | None) bytes[源代码]
i2repr(pkt: Packet | None, x: str | Net) str[源代码]
m2i(pkt: Packet | None, x: bytes) str[源代码]
randval() RandIP[源代码]
resolve(x: str) str[源代码]
class scapy.fields.IPPrefixField(name, default, wordbytes=1, length_from=None)[源代码]

基类:_IPPrefixFieldBase

class scapy.fields.IntEnumField(name: str, default: int | None, enum: Dict[int, str])[源代码]

基类:EnumField[int]

class scapy.fields.IntEnumKeysField(name: str, default: int | None, enum: Dict[int, str])[源代码]

基类:IntEnumField

IntEnumField that picks valid values when fuzzed.

randval() RandEnumKeys[源代码]
class scapy.fields.IntField(name: str, default: int | None)[源代码]

基类:Field[int, int]

class scapy.fields.LE3BytesEnumField(name: str, default: int | None, enum: Dict[int, str])[源代码]

基类:LEThreeBytesField, _EnumField[int]

any2i(pkt: Packet | None, x: Any) int[源代码]
i2repr(pkt: Packet | None, x: Any) List[str] | str[源代码]
i2s: Dict[I, str] | None
i2s_cb: Callable[[I], str] | None
s2i: Dict[str, I] | None
s2i_cb: Callable[[str], I] | None
class scapy.fields.LEFieldLenField(name: str, default: int, length_of: Optional[str] = None, fmt: str = '<H', count_of: Optional[str] = None, adjust: Callable[[Packet, int], int] = <function LEFieldLenField.<lambda>>)[源代码]

基类:FieldLenField

class scapy.fields.LEIntEnumField(name: str, default: int, enum: Dict[int, str])[源代码]

基类:EnumField[int]

class scapy.fields.LEIntField(name: str, default: int | None)[源代码]

基类:Field[int, int]

class scapy.fields.LELongEnumField(name: str, default: int, enum: Dict[int, str] | List[str])[源代码]

基类:EnumField[int]

class scapy.fields.LELongField(name: str, default: int | None)[源代码]

基类:LongField

class scapy.fields.LEMACField(name: str, default: Any | None)[源代码]

基类:MACField

i2m(pkt: Packet | None, x: str | None) bytes[源代码]
m2i(pkt: Packet | None, x: bytes) str[源代码]
class scapy.fields.LEShortEnumField(name: str, default: int, enum: Dict[int, str] | List[str])[源代码]

基类:EnumField[int]

class scapy.fields.LEShortField(name: str, default: int | None)[源代码]

基类:Field[int, int]

class scapy.fields.LESignedIntField(name: str, default: int)[源代码]

基类:Field[int, int]

class scapy.fields.LESignedLongField(name: str, default: Any | None)[源代码]

基类:Field[int, int]

class scapy.fields.LESignedShortField(name: str, default: int | None)[源代码]

基类:Field[int, int]

class scapy.fields.LEThreeBytesField(name: str, default: int | None)[源代码]

基类:ByteField

addfield(pkt: Packet, s: bytes, val: int | None) bytes[源代码]
getfield(pkt: Packet | None, s: bytes) Tuple[bytes, int][源代码]
scapy.fields.LEX3BytesField(*args: Any, **kwargs: Any) Any[源代码]
class scapy.fields.LSBExtendedField(name: str, default: Any | None)[源代码]

基类:BitExtendedField

class scapy.fields.LenField(name: str, default: ~typing.Any | None, fmt: str = 'H', adjust: ~typing.Callable[[int], int] = <function LenField.<lambda>>)[源代码]

基类:Field[int, int]

If None, will be filled with the size of the payload

adjust
i2m(pkt: Packet | None, x: int | None) int[源代码]
class scapy.fields.LongField(name: str, default: int)[源代码]

基类:Field[int, int]

class scapy.fields.MACField(name: str, default: Any | None)[源代码]

基类:Field[Optional[str], bytes]

any2i(pkt: Packet | None, x: Any) str[源代码]
i2m(pkt: Packet | None, x: str | None) bytes[源代码]
i2repr(pkt: Packet | None, x: str | None) str[源代码]
m2i(pkt: Packet | None, x: bytes) str[源代码]
randval() RandMAC[源代码]
class scapy.fields.MSBExtendedField(name: str, default: Any | None)[源代码]

基类:BitExtendedField

exception scapy.fields.MaximumItemsCount[源代码]

基类:Scapy_Exception

class scapy.fields.MayEnd(fld: Any)[源代码]

基类:_FieldContainer

Allow packet dissection to end after the dissection of this field if no bytes are left.

A good example would be a length field that can be 0 or a set value, and where it would be too annoying to use multiple ConditionalFields

Important note: any field below this one MUST default to an empty value, else the behavior will be unexpected.

fld
class scapy.fields.MultiEnumField(name: str, default: int, enum: Dict[I, Dict[I, str]], depends_on: Callable[[Packet | None], I], fmt: str = 'H')[源代码]

基类:_MultiEnumField[int], EnumField[int]

depends_on
i2s_multi
s2i_all: Dict[str, I]
s2i_multi: Dict[I, Dict[str, I]]
class scapy.fields.MultiFlagsEntry(short, long)[源代码]

基类:tuple

long

Alias for field number 1

short

Alias for field number 0

class scapy.fields.MultiFlagsField(name: str, default: Set[str], size: int, names: Dict[int, Dict[int, MultiFlagsEntry]], depends_on: Callable[[Packet | None], int])[源代码]

基类:_BitField[Set[str]]

any2i(pkt: Packet | None, x: Any) Set[str][源代码]
depends_on
i2m(pkt: Packet | None, x: Set[str] | None) int[源代码]
i2repr(pkt: Packet | None, x: Set[str]) str[源代码]
m2i(pkt: Packet | None, x: int) Set[str][源代码]
names
class scapy.fields.MultipleTypeField(flds: List[Tuple[Field[Any, Any], Any]], dflt: Field[Any, Any])[源代码]

基类:_FieldContainer

MultipleTypeField are used for fields that can be implemented by various Field subclasses, depending on conditions on the packet.

It is initialized with flds and dflt.

dflt is the default field type, to be used when none of the conditions matched the current packet.

flds is a list of tuples (fld, cond), where fld if a field type, and cond a "condition" to determine if fld is the field type that should be used.

cond is either:

  • a callable cond_pkt that accepts one argument (the packet) and returns True if fld should be used, False otherwise.

  • a tuple (cond_pkt, cond_pkt_val), where cond_pkt is the same as in the previous case and cond_pkt_val is a callable that accepts two arguments (the packet, and the value to be set) and returns True if fld should be used, False otherwise.

See scapy.layers.l2.ARP (type "help(ARP)" in Scapy) for an example of use.

addfield(pkt: Packet, s: bytes, val: Any) bytes[源代码]
any2i(pkt: Packet | None, val: Any) Any[源代码]
default
dflt
property fld: Field[Any, Any]
flds
getfield(pkt: Packet, s: bytes) Tuple[bytes, Any][源代码]
h2i(pkt: Packet | None, val: Any) Any[源代码]
i2h(pkt: Packet, val: Any) Any[源代码]
i2len(pkt: Packet, val: Any) int[源代码]
i2m(pkt: Packet | None, val: Any | None) Any[源代码]
i2repr(pkt: Packet | None, val: Any) str[源代码]
name
register_owner(cls: Type[Packet]) None[源代码]
class scapy.fields.NBytesField(name: str, default: int | None, sz: int)[源代码]

基类:Field[int, List[int]]

addfield(pkt: Packet | None, s: bytes, val: int | None) bytes[源代码]
getfield(pkt: Packet | None, s: bytes) Tuple[bytes, int][源代码]
i2m(pkt: Packet | None, x: int | None) List[int][源代码]
i2repr(pkt: Packet | None, x: int) str[源代码]
m2i(pkt: Packet | None, x: List[int] | int) int[源代码]
randval() RandNum[源代码]
class scapy.fields.NetBIOSNameField(name: str, default: bytes, length: int = 31)[源代码]

基类:StrFixedLenField

i2m(pkt: Packet | None, y: bytes | None) bytes[源代码]
m2i(pkt: Packet | None, x: bytes) bytes[源代码]
class scapy.fields.OByteField(name: str, default: int | None)[源代码]

基类:ByteField

i2repr(pkt: Packet | None, x: int) str[源代码]
class scapy.fields.OUIField(name: str, default: int)[源代码]

基类:X3BytesField

A field designed to carry a OUI (3 bytes)

i2repr(pkt: Packet | None, val: int) str[源代码]
class scapy.fields.ObservableDict(*args: Dict[int, str], **kw: Any)[源代码]

基类:Dict[int, str]

Helper class to specify a protocol extendable for runtime modifications

observe(observer: _EnumField[Any]) None[源代码]
update(anotherDict)[源代码]
class scapy.fields.PacketField(name: str, default: Optional[K], pkt_cls: Union[Callable[[bytes], Packet], Type[Packet]]  # noqa: E501)[源代码]

基类:_PacketFieldSingle[BasePacket]

randval() Packet[源代码]
class scapy.fields.PacketLenField(name: str, default: Packet, cls: Union[Callable[[bytes], Packet], Type[Packet]]  # noqa: E501, length_from: Optional[Callable[[Packet], int]]  # noqa: E501 = None)[源代码]

基类:_PacketFieldSingle[Optional[BasePacket]]

getfield(pkt: Packet, s: bytes) Tuple[bytes, BasePacket | None][源代码]
length_from
class scapy.fields.PacketListField(name: str, default: Optional[List[BasePacket]], pkt_cls: Optional[Union[Callable[[bytes], Packet], Type[Packet]]]  # noqa: E501 = None, count_from: Optional[Callable[[Packet], int]] = None, length_from: Optional[Callable[[Packet], int]] = None, next_cls_cb: Optional[Callable[[Packet, List[BasePacket], Optional[Packet], bytes], Type[Packet]]]  # noqa: E501 = None, max_count: Optional[int] = None)[源代码]

基类:_PacketField[List[BasePacket]]

PacketListField represents a list containing a series of Packet instances that might occur right in the middle of another Packet field. This field type may also be used to indicate that a series of Packet instances have a sibling semantic instead of a parent/child relationship (i.e. a stack of layers). All elements in PacketListField have current packet referenced in parent field.

addfield(pkt: Packet, s: bytes, val: Any) bytes[源代码]
any2i(pkt: Packet | None, x: Any) List[BasePacket][源代码]
count_from
getfield(pkt: Packet, s: bytes) Tuple[bytes, List[BasePacket]][源代码]
i2count(pkt: Packet | None, val: List[BasePacket]) int[源代码]
i2len(pkt: Packet | None, val: List[Packet]) int[源代码]
i2m(pkt: Packet | None, i: Any) bytes[源代码]
islist = 1
length_from
max_count
next_cls_cb
class scapy.fields.PadField(fld: Field[Any, Any], align: int, padwith: bytes | None = None)[源代码]

基类:_FieldContainer

Add bytes after the proxified field so that it ends at the specified alignment from its beginning

addfield(pkt: Packet, s: bytes, val: Any) bytes[源代码]
fld
getfield(pkt: Packet, s: bytes) Tuple[bytes, Any][源代码]
padlen(flen: int, pkt: Packet) int[源代码]
class scapy.fields.RawVal(val: bytes = b'')[源代码]

基类:object

A raw value that will not be processed by the field and inserted as-is in the packet string.

Example:

>>> a = IP(len=RawVal("####"))
>>> bytes(a)
b'F\x00####\x00\x01\x00\x005\xb5\x00\x00\x7f\x00\x00\x01\x7f\x00\x00\x01\x00\x00'
class scapy.fields.ReversePadField(fld: Field[Any, Any], align: int, padwith: bytes | None = None)[源代码]

基类:PadField

Add bytes BEFORE the proxified field so that it starts at the specified alignment from its beginning

addfield(pkt: Packet, s: bytes, val: Any) bytes[源代码]
fld
getfield(pkt: Packet, s: bytes) Tuple[bytes, Any][源代码]
original_length(pkt: Packet) int[源代码]
class scapy.fields.ScalingField(name: str, default: float, scaling: int | float = 1, unit: str = '', offset: int | float = 0, ndigits: int = 3, fmt: str = 'B')[源代码]

基类:_ScalingField, Field[Union[int, float], Union[int, float]]

Handle physical values which are scaled and/or offset for communication

示例

>>> from scapy.packet import Packet
>>> class ScalingFieldTest(Packet):
        fields_desc = [ScalingField('data', 0, scaling=0.1, offset=-1, unit='mV')]  # noqa: E501
>>> ScalingFieldTest(data=10).show2()
###[ ScalingFieldTest ]###
  data= 10.0 mV
>>> hexdump(ScalingFieldTest(data=10))
0000  6E                                               n
>>> hexdump(ScalingFieldTest(data=b"m"))
0000  6D                                               m
>>> ScalingFieldTest(data=b"m").show2()
###[ ScalingFieldTest ]###
  data= 9.9 mV

bytes(ScalingFieldTest(...)) will produce 0x6E in this example. 0x6E is 110 (decimal). This is calculated through the scaling factor and the offset. "data" was set to 10, which means, we want to transfer the physical value 10 mV. To calculate the value, which has to be sent on the bus, the offset has to subtracted and the scaling has to be applied by division through the scaling factor. bytes = (data - offset) / scaling bytes = ( 10 - (-1) ) / 0.1 bytes = 110 = 0x6E

If you want to force a certain internal value, you can assign a byte- string to the field (data=b"m"). If a string of a bytes object is given to the field, no internal value conversion will be applied

参数:
  • name -- field's name

  • default -- default value for the field

  • scaling -- scaling factor for the internal value conversion

  • unit -- string for the unit representation of the internal value

  • offset -- value to offset the internal value during conversion

  • ndigits -- number of fractional digits for the internal conversion

  • fmt -- struct.pack format used to parse and serialize the internal value from and to machine representation # noqa: E501

class scapy.fields.SecondsIntField(name: str, default: int, use_msec: bool = False, use_micro: bool = False, use_nano: bool = False)[源代码]

基类:Field[float, int]

i2repr(pkt: Packet | None, x: float | None) str[源代码]
use_micro
use_msec
use_nano
class scapy.fields.ShortEnumField(name: str, default: int, enum: Dict[int, str] | Dict[str, int] | Tuple[Callable[[int], str], Callable[[str], int]] | DADict[int, str])[源代码]

基类:EnumField[int]

i2s: Dict[I, str] | None
i2s_cb: Callable[[I], str] | None
s2i: Dict[str, I] | None
s2i_cb: Callable[[str], I] | None
class scapy.fields.ShortEnumKeysField(name: str, default: int, enum: Dict[int, str] | Dict[str, int] | Tuple[Callable[[int], str], Callable[[str], int]] | DADict[int, str])[源代码]

基类:ShortEnumField

ShortEnumField that picks valid values when fuzzed.

randval() RandEnumKeys[源代码]
class scapy.fields.ShortField(name: str, default: int | None)[源代码]

基类:Field[int, int]

class scapy.fields.SignedByteField(name: str, default: int | None)[源代码]

基类:Field[int, int]

class scapy.fields.SignedIntEnumField(name: str, default: int | None, enum: Dict[int, str])[源代码]

基类:EnumField[int]

class scapy.fields.SignedIntField(name: str, default: int)[源代码]

基类:Field[int, int]

class scapy.fields.SignedLongField(name: str, default: int | None)[源代码]

基类:Field[int, int]

class scapy.fields.SignedShortField(name: str, default: int | None)[源代码]

基类:Field[int, int]

class scapy.fields.SourceIP6Field(name: str, dstname: str)[源代码]

基类:IP6Field

dstname
i2h(pkt: Packet | None, x: str | Net6 | None) str[源代码]
i2m(pkt: Packet | None, x: str | Net6 | None) bytes[源代码]
class scapy.fields.SourceIPField(name: str, dstname: str | None)[源代码]

基类:IPField

dstname
i2h(pkt: Packet | None, x: str | Net | None) str[源代码]
i2m(pkt: Packet | None, x: str | Net | None) bytes[源代码]
class scapy.fields.StrEnumField(name: str, default: bytes, enum: Dict[str, str] | None = None, **kwargs: Any)[源代码]

基类:_StrEnumField, StrField

enum
class scapy.fields.StrField(name: str, default: I | None, fmt: str = 'H', remain: int = 0)[源代码]

基类:_StrField[bytes]

class scapy.fields.StrFieldUtf16(name: str, default: I | None, fmt: str = 'H', remain: int = 0)[源代码]

基类:StrField

any2i(pkt: Packet | None, x: str | None) bytes[源代码]
h2i(pkt: Packet | None, x: str | None) bytes[源代码]
i2h(pkt: Packet | None, x: bytes) str[源代码]
i2repr(pkt: Packet | None, x: bytes) str[源代码]
class scapy.fields.StrFixedLenEnumField(name: str, default: bytes, enum: Optional[Dict[str, str]] = None, length: Optional[int] = None, length_from: Optional[Callable[[Optional[Packet]], int]]  # noqa: E501 = None)[源代码]

基类:_StrEnumField, StrFixedLenField

enum
class scapy.fields.StrFixedLenField(name: str, default: Optional[bytes], length: Optional[int] = None, length_from: Optional[Callable[[Packet], int]]  # noqa: E501 = None)[源代码]

基类:StrField

addfield(pkt: Packet, s: bytes, val: bytes | None) bytes[源代码]
getfield(pkt: Packet, s: bytes) Tuple[bytes, bytes][源代码]
i2repr(pkt: Packet | None, v: bytes) str[源代码]
length_from
randval() RandBin[源代码]
class scapy.fields.StrFixedLenFieldUtf16(name: str, default: Optional[bytes], length: Optional[int] = None, length_from: Optional[Callable[[Packet], int]]  # noqa: E501 = None)[源代码]

基类:StrFixedLenField, StrFieldUtf16

class scapy.fields.StrLenEnumField(name: str, default: bytes, enum: Dict[str, str] | None = None, **kwargs: Any)[源代码]

基类:_StrEnumField, StrLenField

enum
class scapy.fields.StrLenField(name: str, default: bytes, length_from: Callable[[Packet], int] | None = None, max_length: Any | None = None)[源代码]

基类:StrField

StrField with a length

参数:
  • length_from -- a function that returns the size of the string

  • max_length -- max size to use as randval

ON_WIRE_SIZE_UTF16 = True
getfield(pkt: Any, s: bytes) Tuple[bytes, bytes][源代码]
length_from
max_length
randval() RandBin[源代码]
class scapy.fields.StrLenFieldUtf16(name: str, default: bytes, length_from: Callable[[Packet], int] | None = None, max_length: Any | None = None)[源代码]

基类:StrLenField, StrFieldUtf16

class scapy.fields.StrNullField(name: str, default: I | None, fmt: str = 'H', remain: int = 0)[源代码]

基类:StrField

ALIGNMENT = 1
DELIMITER = b'\x00'
addfield(pkt: Packet, s: bytes, val: bytes | None) bytes[源代码]
getfield(pkt: Packet, s: bytes) Tuple[bytes, bytes][源代码]
i2len(pkt: Packet | None, x: Any) int[源代码]
randval() RandTermString[源代码]
class scapy.fields.StrNullFieldUtf16(name: str, default: I | None, fmt: str = 'H', remain: int = 0)[源代码]

基类:StrNullField, StrFieldUtf16

ALIGNMENT = 2
DELIMITER = b'\x00\x00'
class scapy.fields.StrStopField(name: str, default: str, stop: bytes, additional: int = 0)[源代码]

基类:StrField

additional
getfield(pkt: Packet | None, s: bytes) Tuple[bytes, bytes][源代码]
randval() RandTermString[源代码]
stop
class scapy.fields.ThreeBytesField(name: str, default: int)[源代码]

基类:Field[int, int]

addfield(pkt: Packet, s: bytes, val: int | None) bytes[源代码]
getfield(pkt: Packet, s: bytes) Tuple[bytes, int][源代码]
class scapy.fields.TrailerBytes[源代码]

基类:bytes

Reverses slice operations to take from the back of the packet, not the front

class scapy.fields.TrailerField(fld: Field[Any, Any])[源代码]

基类:_FieldContainer

Special Field that gets its value from the end of the packet (Note: not layer, but packet).

Mostly used for FCS

addfield(pkt: Packet, s: bytes, val: int | None) bytes[源代码]
fld
getfield(pkt: Packet, s: bytes) Tuple[bytes, int][源代码]
class scapy.fields.UTCTimeField(name: str, default: int, use_msec: bool = False, use_micro: bool = False, use_nano: bool = False, epoch: Tuple[int, int, int, int, int, int, int, int, int] | None = None, strf: str = '%a, %d %b %Y %H:%M:%S %z', custom_scaling: int | None = None, fmt: str = 'I')[源代码]

基类:Field[float, int]

custom_scaling
delta
epoch
i2m(pkt: Packet | None, x: float | None) int[源代码]
i2repr(pkt: Packet | None, x: float) str[源代码]
strf
use_micro
use_msec
use_nano
class scapy.fields.UUIDEnumField(name: str, default: int | None, enum: Any, uuid_fmt: int = 0)[源代码]

基类:UUIDField, _EnumField[UUID]

any2i(pkt: Packet | None, x: Any) UUID[源代码]
i2repr(pkt: Packet | None, x: UUID) Any[源代码]
i2s: Dict[I, str] | None
i2s_cb: Callable[[I], str] | None
s2i: Dict[str, I] | None
s2i_cb: Callable[[str], I] | None
class scapy.fields.UUIDField(name: str, default: int | None, uuid_fmt: int = 0)[源代码]

基类:Field[UUID, bytes]

Field for UUID storage, wrapping Python's uuid.UUID type.

The internal storage format of this field is uuid.UUID from the Python standard library.

There are three formats (uuid_fmt) for this field type:

  • FORMAT_BE (default): the UUID is six fields in big-endian byte order, per RFC 4122.

    This format is used by DHCPv6 (RFC 6355) and most network protocols.

  • FORMAT_LE: the UUID is six fields, with time_low, time_mid and time_high_version in little-endian byte order. This doesn't change the arrangement of the fields from RFC 4122.

    This format is used by Microsoft's COM/OLE libraries.

  • FORMAT_REV: the UUID is a single 128-bit integer in little-endian byte order. This changes the arrangement of the fields.

    This format is used by Bluetooth Low Energy.

Note: You should use the constants here.

The "human encoding" of this field supports a number of different input formats, and wraps Python's uuid.UUID library appropriately:

  • Given a bytearray, bytes or str of 16 bytes, this class decodes UUIDs in wire format.

  • Given a bytearray, bytes or str of other lengths, this delegates to uuid.UUID the Python standard library. This supports a number of different encoding options -- see the Python standard library documentation for more details.

  • Given an int or long, presumed to be a 128-bit integer to pass to uuid.UUID.

  • Given a tuple:

    • Tuples of 11 integers are treated as having the last 6 integers forming the node field, and are merged before being passed as a tuple of 6 integers to uuid.UUID.

    • Otherwise, the tuple is passed as the fields parameter to uuid.UUID directly without modification.

      uuid.UUID expects a tuple of 6 integers.

Other types (such as uuid.UUID) are passed through.

FORMATS = (0, 1, 2)
FORMAT_BE = 0
FORMAT_LE = 1
FORMAT_REV = 2
any2i(pkt: Optional[Packet], x: Any  # noqa: E501) UUID | None[源代码]
i2m(pkt: Packet | None, x: UUID | None) bytes[源代码]
m2i(pkt: Packet | None, x: bytes) UUID[源代码]
static randval() RandUUID[源代码]
uuid_fmt
class scapy.fields.X3BytesField(name: str, default: int)[源代码]

基类:ThreeBytesField, XByteField

i2repr(pkt: Packet | None, x: int) str[源代码]
class scapy.fields.XBitField(name: str, default: I | None, size: int, tot_size: int = 0, end_tot_size: int = 0)[源代码]

基类:BitField

i2repr(pkt: Packet | None, x: int) str[源代码]
class scapy.fields.XByteEnumField(name: str, default: int | None, enum: Dict[int, str])[源代码]

基类:ByteEnumField

i2repr_one(pkt: Packet | None, x: int) str[源代码]
class scapy.fields.XByteField(name: str, default: int | None)[源代码]

基类:ByteField

i2repr(pkt: Packet | None, x: int) str[源代码]
class scapy.fields.XIntField(name: str, default: int | None)[源代码]

基类:IntField

i2repr(pkt: Packet | None, x: int) str[源代码]
class scapy.fields.XLE3BytesEnumField(name: str, default: int | None, enum: Dict[int, str])[源代码]

基类:LE3BytesEnumField

class scapy.fields.XLE3BytesField(name: str, default: int | None)[源代码]

基类:LEThreeBytesField, XByteField

i2repr(pkt: Packet | None, x: int) str[源代码]
class scapy.fields.XLEIntField(name: str, default: int | None)[源代码]

基类:LEIntField, XIntField

i2repr(pkt: Packet | None, x: int) str[源代码]
class scapy.fields.XLELongField(name: str, default: int | None)[源代码]

基类:LELongField, XLongField

i2repr(pkt: Packet | None, x: int) str[源代码]
class scapy.fields.XLEShortField(name: str, default: int | None)[源代码]

基类:LEShortField, XShortField

i2repr(pkt: Packet | None, x: int) str[源代码]
class scapy.fields.XLEStrLenField(name: str, default: bytes, length_from: Callable[[Packet], int] | None = None, max_length: Any | None = None)[源代码]

基类:XStrLenField

i2m(pkt: Packet | None, x: bytes | None) bytes[源代码]
m2i(pkt: Packet | None, x: bytes) bytes[源代码]
class scapy.fields.XLongField(name: str, default: int)[源代码]

基类:LongField

i2repr(pkt: Packet | None, x: int) str[源代码]
class scapy.fields.XNBytesField(name: str, default: int | None, sz: int)[源代码]

基类:NBytesField

i2repr(pkt: Packet | None, x: int) str[源代码]
class scapy.fields.XShortEnumField(name: str, default: int, enum: Dict[int, str] | Dict[str, int] | Tuple[Callable[[int], str], Callable[[str], int]] | DADict[int, str])[源代码]

基类:ShortEnumField

class scapy.fields.XShortField(name: str, default: int | None)[源代码]

基类:ShortField

i2repr(pkt: Packet | None, x: int) str[源代码]
class scapy.fields.XStrField(name: str, default: I | None, fmt: str = 'H', remain: int = 0)[源代码]

基类:_XStrField, StrField

StrField which value is printed as hexadecimal.

class scapy.fields.XStrFixedLenField(name: str, default: Optional[bytes], length: Optional[int] = None, length_from: Optional[Callable[[Packet], int]]  # noqa: E501 = None)[源代码]

基类:_XStrField, StrFixedLenField

StrFixedLenField which value is printed as hexadecimal.

class scapy.fields.XStrLenField(name: str, default: bytes, length_from: Callable[[Packet], int] | None = None, max_length: Any | None = None)[源代码]

基类:_XStrField, StrLenField

StrLenField which value is printed as hexadecimal.

class scapy.fields.YesNoByteField(name: str, default: int, config: Dict[str, Any] | None = None)[源代码]

基类:ByteField

A byte based flag field that shows representation of its number based on a given association

In its default configuration the following representation is generated:

x == 0 : 'no' x != 0 : 'yes'

In more sophisticated use-cases (e.g. yes/no/invalid) one can use the config attribute to configure. Key-value, key-range and key-value-set associations that will be used to generate the values representation.

  • A range is given by a tuple (<first-val>, <last-value>) including the last value.

  • A single-value tuple is treated as scalar.

  • A list defines a set of (probably non consecutive) values that should be associated to a given key.

All values not associated with a key will be shown as number of type unsigned byte.

For instance:

config = {
    'no' : 0,
    'foo' : (1,22),
    'yes' : 23,
    'bar' : [24,25, 42, 48, 87, 253]
}

Generates the following representations:

x == 0 : 'no'
x == 15: 'foo'
x == 23: 'yes'
x == 42: 'bar'
x == 43: 43

Another example, using the config attribute one could also revert the stock-yes-no-behavior:

config = {
        'yes' : 0,
        'no' : (1,255)
}

Will generate the following value representation:

x == 0 : 'yes'
x != 0 : 'no'
eval_fn
i2repr(pkt: Packet | None, x: int) str[源代码]