使用短于 8 位的 PacketField 构建 scapy 数据包

发布于 2024-12-11 19:10:35 字数 1207 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

书信已泛黄 2024-12-18 19:10:35

我猜数据包总是字节对齐的,所以你可能必须以某种方式使用一个字段,例如:

class StatusField(FlagsField):
  name = '4 bit status'
  def __init__(self, name):
    FlagsField.__init__(self, name, 0, 4, ["a", "b", "c", "d"])

class TotalStatus(Packet):
  name = "8 bit status"
  fields_desc = [
    StatusField("axis0"),
    StatusField("axis1"),
  ] 

I guess that a Packet is always byte aligned so you may have to use a field somehow, e.g:

class StatusField(FlagsField):
  name = '4 bit status'
  def __init__(self, name):
    FlagsField.__init__(self, name, 0, 4, ["a", "b", "c", "d"])

class TotalStatus(Packet):
  name = "8 bit status"
  fields_desc = [
    StatusField("axis0"),
    StatusField("axis1"),
  ] 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文