Search code examples
pythonscapyautomata

Scapy's automata, 2 times executing cycle


I have written this code:

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()

When getting ICMP request to show IP from header, but IP gets printed twice.

Sending 1 packet with command:

ping 127.1 -c1

Script output:

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

Solution

  • Loopback interfaces are quite special, and Scapy sometimes messes with them.

    You should probably use a "real" interface.