python - 带有自定义图层的 Scapy sr()

标签 python networking scapy bacnet

当使用 sr() 或 srp() 函数时——Scapy 如何知道收到的数据包是对我发送的数据包的答复?

我编写了一个模仿 BACNet 的自定义协议(protocol)。我可以将 WHO_IS 数据包发送到 BACNet 设备,设备使用 I_AM 数据包进行应答,该数据包由于层绑定(bind)而被正确反汇编,但 sr 函数无法将其识别为应答。我如何让 Scapy 接受这些数据包作为答案?

更新: 这就是我的图层类的样子。我认为 answers() 方法看起来不错,但它仍然不起作用。在实现 answers() 方法时,我可能有什么误解吗? 在我的理解中,self 指的是层本身的类,other 是接收到的相关数据包。因此,为了将有效负载传递到下一个更高层,我传递了 other.payload 并使用下一个更高层类调用了 answers() 方法。 这些层像 Ether/IP/UDP/BVLC/NPDU/APDU 一样堆叠。

class BVLC(Packet):

    name = "BVLC"
    fields_desc = [
       # many fields
                   ]

    def post_build(self, pkt, pay):
        if self.length == None:                
            pkt = pkt[0:2] + struct.pack('!H', len(pay)+len(pkt)) + pkt[4:]  
        return pkt+pay

    def answers(self, other):
        if not isinstance(other,BVLC):
            return 0
        else:
            return NPDU.answers(other.payload)


class NPDU(Packet):

    name = "NPDU"
    fields_desc = [ 
        # again, many fields
                   ]

    def answers(self, other):
        if not isinstance(other,NPDU):
            return 0
        else:
            return APDU.answers(other.payload)

class APDU(Packet):

    name = "APDU"
    fields_desc = [
          # many fields and the only relevant field in this case
            ConditionalField(
                ByteEnumField(
                    "serviceChoice", None, APDU_Service_Unconfirmed.revDict()),
                    lambda pkt: pkt.pduType == APDU_Type.CONFIRMED_SERVICE_REQUEST or
                                pkt.pduType == APDU_Type.UNCONFIRMED_SERVICE_REQUEST), 
                   ]

    def answers(self, other):
        if not isinstance(other,APDU):
            return 0
        else:
            if self.serviceChoice == APDU_Service_Unconfirmed.WHO_IS and\
               other.serviceChoice == APDU_Service_Unconfirmed.I_AM:
                return 1
        return 0

最佳答案

Scapy sr 只是为每个发送的数据包调用 recv_packet.answers(sent_packet)。 因此,您必须为您的层实现 def answers(),请参阅:

def answers(self, other):
    """DEV: true if self is an answer from other"""
    if other.__class__ == self.__class__:
        return self.payload.answers(other.payload)
    return 0

这是原始 TCP 层的摘录:

def answers(self, other):
    if not isinstance(other, TCP):
        return 0
    if conf.checkIPsrc:
        if not ((self.sport == other.dport) and
                (self.dport == other.sport)):
            return 0
    if (abs(other.seq-self.ack) > 2+len(other.payload)):
        return 0
    return 1

关于python - 带有自定义图层的 Scapy sr(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33121580/

相关文章:

python - 画出 turtle python 的形状

python - 有序字典不排序

networking - 为什么我的ip地址以192开头?

azure - Databricks 文件触发器 - 如何将存储防火墙列入白名单

python - 如何在python中发送数据包?

python - 从特定目录继承的 Django 加载模板

python - 我们可以将多少个类/实例注册到 xml rpc 服务器中?

c - 生成数据包

windows - 是否可以使用 Python/Scapy for Windows 编写简单的防火墙?

python - 测试请求中的延迟时间