使用短于 8 位的 PacketField 构建 scapy 数据包
我正在尝试使用 scapy 添加一个新协议,并且在构建存储长度低于一个字节的其他 BitEnumField“数据包”的数据包时遇到了困难。我想知道是否有一种解决方法可以使其工作(无需将数据包捆绑到完整字节字段中)。下面是示例:
from scapy.packet import Packet
from scapy.fields import *
class Status(Packet):
name = '4 bit status'
fields_desc = [
BitEnumField('a', 0, 1, {0:'Disabled', 1:'Enabled'}),
BitEnumField('b', 0, 1, {0:'Disabled', 1:'Active'}),
BitEnumField('c', 0, 1, {0:'Disabled', 1:'Active'}),
BitEnumField('d', 0, 1, {0:'Disabled', 1:'Active'}),
]
#this is added for debug purposes only
def post_build(self, pkt,pay):
print "pkt:", pkt, "type:", type(pkt)
return pkt+pay
现在我可以理解为什么 Status().show2()
会失败,而 pkt
只有 4 位长。但这个也死了(我猜是因为每个数据包都是独立形成的):
class TotalStatus(Packet):
name = "8 bit status"
fields_desc = [
PacketField('axis0', Status(), Status),
PacketField('axis1', Status(), Status),
]
TotalStatus().show2()
给你提供了冗长的回溯,以 self.post_build()
结尾cat pkt
tuple 和有效负载的其余部分,这是空字符串。即:<代码>>>类型错误:只能将元组(不是“str”)连接到元组
有什么方法可以避免将位字段捆绑到完整字节中吗?
I'm trying to add a new protocol with scapy, and I'm running into difficulties building packets that store other BitEnumField "packets" that are under one byte of length. I was wondering if there is a workaround to make it work (without bundling packets into full byte fields). Here is the example:
from scapy.packet import Packet
from scapy.fields import *
class Status(Packet):
name = '4 bit status'
fields_desc = [
BitEnumField('a', 0, 1, {0:'Disabled', 1:'Enabled'}),
BitEnumField('b', 0, 1, {0:'Disabled', 1:'Active'}),
BitEnumField('c', 0, 1, {0:'Disabled', 1:'Active'}),
BitEnumField('d', 0, 1, {0:'Disabled', 1:'Active'}),
]
#this is added for debug purposes only
def post_build(self, pkt,pay):
print "pkt:", pkt, "type:", type(pkt)
return pkt+pay
Now I can understand why Status().show2()
fails with pkt
being only 4 bits long. but this one dies too (I guess because each packet is formed independently):
class TotalStatus(Packet):
name = "8 bit status"
fields_desc = [
PacketField('axis0', Status(), Status),
PacketField('axis1', Status(), Status),
]
TotalStatus().show2()
gives you lengthy traceback that ends in self.post_build()
unable to cat pkt
tuple and the rest of payload, which is empty string. I.e.:>>> TypeError: can only concatenate tuple (not "str") to tuple
Is there any way to avoid bundling of bit fields into full bytes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜数据包总是字节对齐的,所以你可能必须以某种方式使用一个字段,例如:
I guess that a Packet is always byte aligned so you may have to use a field somehow, e.g: