在 ScaPy 中使用 LenFields

发布于 2025-01-08 02:26:13 字数 681 浏览 2 评论 0原文

我正在 ScaPy 中编写一些协议,但很难理解 len 字段的工作原理。阅读http://trac.secdev.org/scapy/wiki/LengthFields后我'我同样开明。

显然,我们有他们所说的“lenfield”(保存字段的长度)和“varfield”(包含实际的可变长度字段)。但是,似乎唯一可以作为可变字段的字段是 PacketLenField、StrFixedLenField 和 StrLenField。剩下的田地在哪里?为什么不能有可变长度的 BitField 或 XBitField?

例如我得到:

BitFieldLenField('lenfield', None, 8, length_of='varfield')

这正确地返回了 8 的长度(在这种情况下我想要的字段是 8 个八位位组/ 4 个十六进制对长)。到目前为止一切都很好。然而,当我尝试获取该字段时,似乎我唯一的选择是 StrLenField ,它返回类似“A\x81\x11\x11P 3”的内容

所以,我的问题是:如何将该字段恢复为实际的十六进制在数据包中?我是否缺少内置字段中的某些内容,或者我是否必须创建类似 XBitLenField 的内容?

感谢您的帮助。

I'm writing some protocols in ScaPy, but am having difficulty understanding how the len fields work. After reading http://trac.secdev.org/scapy/wiki/LengthFields I'm no less enlightened.

Obviously, we have what they call 'lenfield' (Holds the length of the field) and 'varfield' (Contains the actual variable length field). However, it seems that the only fields that can be varfields are PacketLenField, StrFixedLenField, and StrLenField. Where's the rest of the fields? Why can't you have a variable length BitField, or XBitField?

For example I've got:

BitFieldLenField('lenfield', None, 8, length_of='varfield')

This correctly returns a length of 8 (the field I want in this case is 8 octets/ 4 hex pairs long). All good so far. However, when I try and get that field, it seems like my only option is a StrLenField which returns something like 'A\x81\x11\x11P 3'

So, my question is: How do I get that field back as the actual hex in the packet? Am I missing something in the built in fields, or do I have to create something like an XBitLenField?

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

梦醒时光 2025-01-15 02:26:14

这个答案是针对 Scapy 2.2.0 的。

LenField 通常成对工作,如下所示:

FieldLenField("len", None, length_of="data")
StrLenField("data", "", length_from=lambda pkt:pkt.len)

请注意 length_oflength_from 如何将两个字段链接在一起。 length_of值告诉Scapy在发送数据包时如何计算“len”字段(注意默认值为None,表示应该自动计算)。 length_from 值告诉 Scapy 在剖析数据包时要向 "data" 字段输入多少字节。

有一个名为 LenField 的特殊字段,可以单独使用。再次注意,默认值是None,这意味着它应该自动计算。当发送时,Scapy将简单地计算“len”作为当前层之后的字节数。

LenField("len", None)

现在已经引入了长度字段,您可以使用 Python 将 StrLenField 的输出编码为十六进制。

# In layer definition
BitFieldLenField("lenfield", None, 8, length_of="varfield")
StrLenField("varfield", "", length_from=lambda pkt:pkt.lenfield)

# In main script
print(pck[mylayer].varfield.encode("hex"))    # Replace "mylayer" with the name of your new layer

This answer is with respect to Scapy 2.2.0.

LenFields typically work in pairs like the following:

FieldLenField("len", None, length_of="data")
StrLenField("data", "", length_from=lambda pkt:pkt.len)

Notice how the length_of and length_from link the two fields together. The length_of value tells Scapy how to calculate the "len" field when sending the packet (Notice the default value is None, to indicate it should be calculate automatically). The length_from value tells Scapy how many bytes to input into the "data" field when it is dissecting the packet.

There is a special field called LenField which can be used by itself. Notice again that the default value is None meaning it should be calculated automatically. When sending Scapy will simply calculate "len" as the number of bytes following the current layer.

LenField("len", None)

Now that Length Fields have been introduced, you can use Python to encode the output of the StrLenField into hex.

# In layer definition
BitFieldLenField("lenfield", None, 8, length_of="varfield")
StrLenField("varfield", "", length_from=lambda pkt:pkt.lenfield)

# In main script
print(pck[mylayer].varfield.encode("hex"))    # Replace "mylayer" with the name of your new layer
能怎样 2025-01-15 02:26:14

您可以重写Fields对象,例如StrLenField:

# String to hex split by space
def str2hex(string):
    return ' '.join('%02x' % ord(b) for b in string)


class XStrLenField(scapy.fields.StrLenField):

    def i2repr(self, pkt, x):
        return str2hex(scapy.fields.StrLenField.i2m(self, pkt, x))

之后您可以在field_desc[]中使用XStrLenField:

XStrLenField("rand", "", length_from=lambda x: x.len)

You can rewrite Fields object, for instance StrLenField:

# String to hex split by space
def str2hex(string):
    return ' '.join('%02x' % ord(b) for b in string)


class XStrLenField(scapy.fields.StrLenField):

    def i2repr(self, pkt, x):
        return str2hex(scapy.fields.StrLenField.i2m(self, pkt, x))

After you can use the XStrLenField in field_desc[]:

XStrLenField("rand", "", length_from=lambda x: x.len)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文