Scapy的自动机,2次执行循环

Scapy's automata, 2 times executing cycle

我写了这段代码:

from scapy.layers.inet import ICMP,IP
from scapy.all import Raw

class GetIP(Automaton):
    def parse_args(self, **kargs):
        Automaton.parse_args(self, **kargs)

    def master_filter(self, pkt):
        return (ICMP in pkt and pkt[ICMP].type==8)

    @ATMT.state(initial=1)
    def BEGIN(self):
        print("begin state")
        raise self.WAITING()

    @ATMT.state()
    def WAITING(self):
        print("waiting state")
        pass

    @ATMT.state()
    def RECIEVED(self):
        print("recieved state")
        pass

    @ATMT.state(final=1)
    def END(self):
        print("END state")
        raise self.BEGIN()

    @ATMT.receive_condition(WAITING)
    def wait_icmp(self,pkt):
        print("wait-receive condition")
        self.client_ip = pkt[IP].src
        raise self.RECIEVED()

    @ATMT.condition(RECIEVED)
    def got_icmp(self):
        print("received condition")
        raise self.END()

    @ATMT.action(got_icmp)
    def post_icmp(self):
        print("action")
        print(self.client_ip)
GetIP().run()

当从 header 获取 ICMP 请求以显示 IP 时,IP 被打印了两次。

正在使用命令发送 1 个数据包:

ping 127.1 -c1

脚本输出:

WARNING: No route found for IPv6 destination :: (no default route?)
begin state 
waiting state 
wait-receive condition 
recieved state
received condition action
127.0.0.1 
END state 
begin state 
waiting state 
wait-receive condition 
recieved state 
received condition action
127.0.0.1 
END state 
begin state 
waiting state

Loopback 接口比较特殊,Scapy 有时会弄乱它们。

您可能应该使用 "real" 界面。