本文整理汇总了Python中scapy.packet.Packet方法的典型用法代码示例。如果您正苦于以下问题:Python packet.Packet方法的具体用法?Python packet.Packet怎么用?Python packet.Packet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scapy.packet
的用法示例。
在下文中一共展示了packet.Packet方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: pkt2uptime
# 需要导入模块: from scapy import packet [as 别名]
# 或者: from scapy.packet import Packet [as 别名]
def pkt2uptime(pkt, HZ=100):
"""Calculate the date the machine which emitted the packet booted using TCP timestamp # noqa: E501
pkt2uptime(pkt, [HZ=100])"""
if not isinstance(pkt, Packet):
raise TypeError("Not a TCP packet")
if isinstance(pkt, NoPayload):
raise TypeError("Not a TCP packet")
if not isinstance(pkt, TCP):
return pkt2uptime(pkt.payload)
for opt in pkt.options:
if opt[0] == "Timestamp":
# t = pkt.time - opt[1][0] * 1.0/HZ
# return time.ctime(t)
t = opt[1][0] / HZ
return t
raise TypeError("No timestamp option")
示例2: h2i
# 需要导入模块: from scapy import packet [as 别名]
# 或者: from scapy.packet import Packet [as 别名]
def h2i(self, pkt, s):
t = ()
if isinstance(s, str):
t = time.strptime(s)
t = t[:2] + t[2:-3]
else:
if not s:
y, m, d, h, min, sec, rest, rest, rest = time.gmtime(time.time()) # noqa: E501
t = (y, m, d, h, min, sec)
else:
t = s
return t
###########################
# Packet abstract class #
###########################
示例3: GuessAvpType
# 需要导入模块: from scapy import packet [as 别名]
# 或者: from scapy.packet import Packet [as 别名]
def GuessAvpType(p, **kargs):
if len(p) > AVP_Code_length + AVP_Flag_length:
# Set AVP code and vendor
avpCode = struct.unpack("!I", p[:AVP_Code_length])[0]
vnd = bool(struct.unpack(
"!B", p[AVP_Code_length:AVP_Code_length + AVP_Flag_length])[0] & 128) # noqa: E501
vndCode = vnd and struct.unpack("!I", p[8:12])[0] or 0
# Check if vendor and code defined and fetch the corresponding AVP
# definition
if vndCode in AvpDefDict:
AvpVndDict = AvpDefDict[vndCode]
if avpCode in AvpVndDict:
# Unpack only the first 4 tuple items at this point
avpName, AVPClass, flags = AvpVndDict[avpCode][:3]
result = AVPClass(p, **kargs)
result.name = 'AVP ' + avpName
return result
# Packet too short or AVP vendor or AVP code not found ...
return AVP_Unknown(p, **kargs)
示例4: getfield
# 需要导入模块: from scapy import packet [as 别名]
# 或者: from scapy.packet import Packet [as 别名]
def getfield(self, pkt, s):
# type: (Optional[packet.Packet], Union[str, Tuple[str, int]]) -> Tuple[Union[Tuple[str, int], str], int] # noqa: E501
"""
:param packet.Packet|None pkt: the packet instance containing this field instance; probably unused. # noqa: E501
:param str|(str, int) s: either a str if size%8==0 or a tuple with the string to parse from and the number of # noqa: E501
bits already consumed by previous bitfield-compatible fields.
:return: (str|(str, int), int): Returns the remaining string and the parsed value. May return a tuple if there # noqa: E501
are remaining bits to parse in the first byte. Returned value is equal to default value # noqa: E501
:raises: AssertionError
"""
r = super(HPackMagicBitField, self).getfield(pkt, s)
assert (
isinstance(r, tuple) and
len(r) == 2 and
isinstance(r[1], six.integer_types)
), 'Second element of BitField.getfield return value expected to be an int or a long; API change detected' # noqa: E501
assert r[1] == self._magic, 'Invalid value parsed from s; error in class guessing detected!' # noqa: E501
return r
示例5: any2i
# 需要导入模块: from scapy import packet [as 别名]
# 或者: from scapy.packet import Packet [as 别名]
def any2i(self, pkt, x):
# type: (Optional[packet.Packet], Union[None, str, int]) -> Optional[int] # noqa: E501
"""
A "x" value as a string is parsed as a binary encoding of a UVarInt. An int is considered an internal value. # noqa: E501
None is returned as is.
:param packet.Packet|None pkt: the packet containing this field; probably unused. # noqa: E501
:param str|int|None x: the value to convert.
:return: int|None: the converted value.
:raises: AssertionError
"""
if isinstance(x, type(None)):
return x
if isinstance(x, six.integer_types):
assert(x >= 0)
ret = self.h2i(pkt, x)
assert(isinstance(ret, six.integer_types) and ret >= 0)
return ret
elif isinstance(x, bytes):
ret = self.m2i(pkt, x)
assert (isinstance(ret, six.integer_types) and ret >= 0)
return ret
assert False, 'EINVAL: x: No idea what the parameter format is'
示例6: i2len
# 需要导入模块: from scapy import packet [as 别名]
# 或者: from scapy.packet import Packet [as 别名]
def i2len(self, pkt, x):
# type: (Optional[packet.Packet], int) -> int
"""
:param packet.Packet|None pkt: unused.
:param int x: the positive or null value whose binary size if requested. # noqa: E501
:raises: AssertionError
"""
assert(x >= 0)
if x < self._max_value:
return 1
# x is expressed over multiple bytes
x -= self._max_value
i = 1
if x == 0:
i += 1
while x > 0:
x >>= 7
i += 1
ret = i
assert(ret >= 0)
return ret
示例7: addfield
# 需要导入模块: from scapy import packet [as 别名]
# 或者: from scapy.packet import Packet [as 别名]
def addfield(self, pkt, s, val):
# type: (Optional[packet.Packet], Union[str, Tuple[str, int, int]], Optional[int]) -> str # noqa: E501
"""
:param packet.Packet|None pkt: the packet instance containing this field instance. This parameter must not be # noqa: E501
None if the val parameter is.
:param str|(str, int, long) s: the string to append this field to. A tuple indicates that some bits were already # noqa: E501
generated by another bitfield-compatible field. This MUST be the case if "size" is not 8. The int is the # noqa: E501
number of bits already generated in the first byte of the str. The long is the value that was generated by the # noqa: E501
previous bitfield-compatible fields.
:param int|None val: the positive or null value to be added. If None, the value is computed from pkt. # noqa: E501
:return: str: s concatenated with the machine representation of this field. # noqa: E501
:raises: AssertionError
"""
if val is None:
assert isinstance(pkt, packet.Packet), \
'EINVAL: pkt: Packet expected when val is None; received {}'.format(type(pkt)) # noqa: E501
val = self._compute_value(pkt)
return super(FieldUVarLenField, self).addfield(pkt, s, val)
示例8: message_from_packet
# 需要导入模块: from scapy import packet [as 别名]
# 或者: from scapy.packet import Packet [as 别名]
def message_from_packet(self, packet):
"""
:param packet: scapy.packet.Packet
:return: message
"""
self._check_packet(packet)
message = self._dispatch_message_from_packet(packet)
if message:
if isinstance(message, FLE.Notification):
self._setup_on_fle_notification(packet, message)
return message
if self._is_packet_fle_initial(packet):
message = self._fle_message_from_packet(packet)
assert isinstance(message, FLE.Initial)
self._setup_on_fle_initial(packet, message)
return message
raise BadPacket("Unknown packet")
示例9: handle_packet_in
# 需要导入模块: from scapy import packet [as 别名]
# 或者: from scapy.packet import Packet [as 别名]
def handle_packet_in(self, ind_info):
self.log.info('Received-Packet-In', ind_info=ind_info)
# LLDP packet trap from the NNI port is a special case.
# The logical port cannot be derived from the pon_id and
# gem_port. But, the BAL flow_id for the LLDP packet is
# fixed. Hence, check this flow_id and do the necessary
# handling.
if ind_info['flow_id'] == ASFVOLT16_LLDP_DL_FLOW_ID:
logical_port = MIN_ASFVOLT_NNI_LOGICAL_PORT_NUM + \
ind_info['intf_id']
else:
logical_port = self.get_logical_port_from_pon_id_and_gem_port(
ind_info['intf_id'],
ind_info['svc_port'])
if not logical_port:
self.log.error("uni-logical_port-not-found")
return
pkt = Ether(ind_info['packet'])
kw = dict(
logical_device_id=self.logical_device_id,
logical_port_no=logical_port,
)
self.log.info('sending-packet-in', **kw)
self.adapter_agent.send_packet_in(packet=str(pkt), **kw)
示例10: parse
# 需要导入模块: from scapy import packet [as 别名]
# 或者: from scapy.packet import Packet [as 别名]
def parse(self, plist):
"""Update the builder using the provided `plist`. `plist` can
be either a Packet() or a PacketList().
"""
if not isinstance(plist, PacketList):
plist = PacketList(plist)
for pkt in plist[LLTD]:
if LLTDQueryLargeTlv in pkt:
key = "%s:%s:%d" % (pkt.real_dst, pkt.real_src, pkt.seq)
self.types_offsets[key] = (pkt[LLTDQueryLargeTlv].type,
pkt[LLTDQueryLargeTlv].offset)
elif LLTDQueryLargeTlvResp in pkt:
try:
key = "%s:%s:%d" % (pkt.real_src, pkt.real_dst, pkt.seq)
content, offset = self.types_offsets[key]
except KeyError:
continue
loc = slice(offset, offset + pkt[LLTDQueryLargeTlvResp].len)
key = "%s > %s [%s]" % (
pkt.real_src, pkt.real_dst,
LLTDQueryLargeTlv.fields_desc[0].i2s.get(content, content),
)
data = self.data.setdefault(key, array("B"))
datalen = len(data)
if datalen < loc.stop:
data.extend(array("B", b"\x00" * (loc.stop - datalen)))
data[loc] = array("B", pkt[LLTDQueryLargeTlvResp].value)
示例11: send
# 需要导入模块: from scapy import packet [as 别名]
# 或者: from scapy.packet import Packet [as 别名]
def send(self, x):
try:
return SuperSocket.send(self, x)
except socket.error as msg:
if msg.errno == 22 and len(x) < conf.min_pkt_size:
padding = b"\x00" * (conf.min_pkt_size - len(x))
if isinstance(x, Packet):
return SuperSocket.send(self, x / Padding(load=padding))
else:
return SuperSocket.send(self, raw(x) + padding)
raise
示例12: h2i
# 需要导入模块: from scapy import packet [as 别名]
# 或者: from scapy.packet import Packet [as 别名]
def h2i(self, pkt, x):
# type: (Optional[packet.Packet], int) -> int
"""
:param packet.Packet|None pkt: the packet instance containing this field instance; probably unused # noqa: E501
:param int x: unused; must be equal to default value
:return: int; default value
:raises: AssertionError
"""
assert x == self._magic, \
'EINVAL: x: This field is magic. Do not attempt to modify it. Expected value: {}'.format(self._magic) # noqa: E501
return super(HPackMagicBitField, self).h2i(pkt, self._magic)
示例13: i2h
# 需要导入模块: from scapy import packet [as 别名]
# 或者: from scapy.packet import Packet [as 别名]
def i2h(self, pkt, x):
# type: (Optional[packet.Packet], int) -> int
"""
:param packet.Packet|None pkt: the packet instance containing this field instance; probably unused # noqa: E501
:param int x: unused; must be equal to default value
:return: int; default value
:raises: AssertionError
"""
assert x == self._magic, \
'EINVAL: x: This field is magic. Do not attempt to modify it. Expected value: {}'.format(self._magic) # noqa: E501
return super(HPackMagicBitField, self).i2h(pkt, self._magic)
示例14: i2m
# 需要导入模块: from scapy import packet [as 别名]
# 或者: from scapy.packet import Packet [as 别名]
def i2m(self, pkt, x):
# type: (Optional[packet.Packet], int) -> int
"""
:param packet.Packet|None pkt: the packet instance containing this field instance; probably unused # noqa: E501
:param int x: unused; must be equal to default value
:return: int; default value
:raises: AssertionError
"""
assert x == self._magic, \
'EINVAL: x: This field is magic. Do not attempt to modify it. Expected value: {}'.format(self._magic) # noqa: E501
return super(HPackMagicBitField, self).i2m(pkt, self._magic)
示例15: m2i
# 需要导入模块: from scapy import packet [as 别名]
# 或者: from scapy.packet import Packet [as 别名]
def m2i(self, pkt, x):
# type: (Optional[packet.Packet], Union[str, Tuple[str, int]]) -> int
"""
A tuple is expected for the "x" param only if "size" is different than 8. If a tuple is received, some bits # noqa: E501
were consumed by another field. This field consumes the remaining bits, therefore the int of the tuple must # noqa: E501
equal "size".
:param packet.Packet|None pkt: unused.
:param str|(str, int) x: the string to convert. If bits were consumed by a previous bitfield-compatible field. # noqa: E501
:raises: AssertionError
"""
assert(isinstance(x, bytes) or (isinstance(x, tuple) and x[1] >= 0))
if isinstance(x, tuple):
assert (8 - x[1]) == self.size, 'EINVAL: x: not enough bits remaining in current byte to read the prefix' # noqa: E501
val = x[0]
else:
assert isinstance(x, bytes) and self.size == 8, 'EINVAL: x: tuple expected when prefix_len is not a full byte' # noqa: E501
val = x
if self._detect_multi_byte(val[0]):
ret = self._parse_multi_byte(val)
else:
ret = orb(val[0]) & self._max_value
assert(ret >= 0)
return ret