scapy.sendrecv

Functions to send and receive packets.

class scapy.sendrecv.AsyncSniffer(*args: Any, **kwargs: Any)[源代码]

基类:object

Sniff packets and return a list of packets.

参数:
  • count -- number of packets to capture. 0 means infinity.

  • store -- whether to store sniffed packets or discard them

  • prn -- function to apply to each packet. If something is returned, it is displayed. --Ex: prn = lambda x: x.summary()

  • session -- a session = a flow decoder used to handle stream of packets. --Ex: session=TCPSession See below for more details.

  • filter -- BPF filter to apply.

  • lfilter -- Python function applied to each packet to determine if further action may be done. --Ex: lfilter = lambda x: x.haslayer(Padding)

  • offline -- PCAP file (or list of PCAP files) to read packets from, instead of sniffing them

  • quiet -- when set to True, the process stderr is discarded (default: False).

  • timeout -- stop sniffing after a given time (default: None).

  • L2socket -- use the provided L2socket (default: use conf.L2listen).

  • opened_socket -- provide an object (or a list of objects) ready to use .recv() on.

  • stop_filter -- Python function applied to each packet to determine if we have to stop the capture after this packet. --Ex: stop_filter = lambda x: x.haslayer(TCP)

  • iface -- interface or list of interfaces (default: None for sniffing on all interfaces).

  • monitor -- use monitor mode. May not be available on all OS

  • started_callback -- called as soon as the sniffer starts sniffing (default: None).

The iface, offline and opened_socket parameters can be either an element, a list of elements, or a dict object mapping an element to a label (see examples below).

For more information about the session argument, see https://scapy.rtfd.io/en/latest/usage.html#advanced-sniffing-sniffing-sessions

Examples: synchronous
>>> sniff(filter="arp")
>>> sniff(filter="tcp",
...       session=IPSession,  # defragment on-the-flow
...       prn=lambda x: x.summary())
>>> sniff(lfilter=lambda pkt: ARP in pkt)
>>> sniff(iface="eth0", prn=Packet.summary)
>>> sniff(iface=["eth0", "mon0"],
...       prn=lambda pkt: "%s: %s" % (pkt.sniffed_on,
...                                   pkt.summary()))
>>> sniff(iface={"eth0": "Ethernet", "mon0": "Wifi"},
...       prn=lambda pkt: "%s: %s" % (pkt.sniffed_on,
...                                   pkt.summary()))
Examples: asynchronous
>>> t = AsyncSniffer(iface="enp0s3")
>>> t.start()
>>> time.sleep(1)
>>> print("nice weather today")
>>> t.stop()
join(*args: Any, **kwargs: Any) None[源代码]
start() None[源代码]

Starts AsyncSniffer in async mode

stop(join: bool = True) PacketList | None[源代码]

Stops AsyncSniffer if not in async mode

class scapy.sendrecv.SndRcvHandler(pks: SuperSocket, pkt: List[Packet] | Packet | SetGen[Packet] | _PacketList[Packet], timeout: int | None = None, inter: int = 0, verbose: int | None = None, chainCC: bool = False, retry: int = 0, multi: bool = False, rcv_pks: SuperSocket | None = None, prebuild: bool = False, _flood: _FloodGenerator | None = None, threaded: bool = False, session: Type[DefaultSession] | DefaultSession | None = None, chainEX: bool = False)[源代码]

基类:object

Util to send/receive packets, used by sr*(). Do not use directly.

This matches the requests and answers.

Notes::
  • threaded mode: enabling threaded mode will likely break packet timestamps, but might result in a speedup when sending a big amount of packets. Disabled by default

  • DEVS: store the outgoing timestamp right BEFORE sending the packet to avoid races that could result in negative latency. We aren't Stadia

results() Tuple[SndRcvList, PacketList][源代码]
scapy.sendrecv.bridge_and_sniff(if1: NetworkInterface | str, if2: NetworkInterface | str, xfrm12: Callable[[Packet], Packet | bool] | None = None, xfrm21: Callable[[Packet], Packet | bool] | None = None, prn: Callable[[Packet], Any] | None = None, L2socket: Type[SuperSocket] | None = None, *args: Any, **kargs: Any) PacketList[源代码]

Forward traffic between interfaces if1 and if2, sniff and return the exchanged packets.

参数:
  • if1 -- the interfaces to use (interface names or opened sockets).

  • if2 --

  • xfrm12 -- a function to call when forwarding a packet from if1 to if2. If it returns True, the packet is forwarded as it. If it returns False or None, the packet is discarded. If it returns a packet, this packet is forwarded instead of the original packet one.

  • xfrm21 -- same as xfrm12 for packets forwarded from if2 to if1.

The other arguments are the same than for the function sniff(), except for offline, opened_socket and iface that are ignored. See help(sniff) for more.

class scapy.sendrecv.debug[源代码]

基类:object

crashed_on: Tuple[Type[Packet], bytes] | None = None
match = <Matched: TCP:0 UDP:0 ICMP:0 Other:0>
recv = <Received: TCP:0 UDP:0 ICMP:0 Other:0>
sent = <Sent: TCP:0 UDP:0 ICMP:0 Other:0>
scapy.sendrecv.send(x: List[Packet] | Packet | SetGen[Packet] | _PacketList[Packet], iface: NetworkInterface | str | None = None, **kargs: Any) PacketList | None[源代码]

Send packets at layer 3

参数:
  • x -- the packets

  • inter -- time (in s) between two packets (default 0)

  • loop -- send packet indefinitely (default 0)

  • count -- number of packets to send (default None=1)

  • verbose -- verbose mode (default None=conf.verb)

  • realtime -- check that a packet was sent before sending the next one

  • return_packets -- return the sent packets

  • socket -- the socket to use (default is conf.L3socket(kargs))

  • iface -- the interface to send the packets on

  • monitor -- (not on linux) send in monitor mode

返回:

None

scapy.sendrecv.sendp(x: List[Packet] | Packet | SetGen[Packet] | _PacketList[Packet], iface: NetworkInterface | str | None = None, iface_hint: str | None = None, socket: SuperSocket | None = None, **kargs: Any) PacketList | None[源代码]

Send packets at layer 2

参数:
  • x -- the packets

  • inter -- time (in s) between two packets (default 0)

  • loop -- send packet indefinitely (default 0)

  • count -- number of packets to send (default None=1)

  • verbose -- verbose mode (default None=conf.verb)

  • realtime -- check that a packet was sent before sending the next one

  • return_packets -- return the sent packets

  • socket -- the socket to use (default is conf.L3socket(kargs))

  • iface -- the interface to send the packets on

  • monitor -- (not on linux) send in monitor mode

返回:

None

scapy.sendrecv.sendpfast(x: List[Packet] | Packet | SetGen[Packet] | _PacketList[Packet], pps: float | None = None, mbps: float | None = None, realtime: bool = False, loop: int | None = None, file_cache: bool = False, iface: NetworkInterface | str | None = None, replay_args: List[str] | None = None, parse_results: bool = False) Dict[str, Any] | None[源代码]

Send packets at layer 2 using tcpreplay for performance

参数:
  • pps -- packets per second

  • mbps -- MBits per second

  • realtime -- use packet's timestamp, bending time with real-time value

  • loop -- number of times to process the packet list. 0 implies infinite loop

  • file_cache -- cache packets in RAM instead of reading from disk at each iteration

  • iface -- output interface

  • replay_args -- List of additional tcpreplay args (List[str])

  • parse_results -- Return a dictionary of information outputted by tcpreplay (default=False)

返回:

stdout, stderr, command used

scapy.sendrecv.sndrcv(*args: Any, **kwargs: Any) Tuple[SndRcvList, PacketList][源代码]

Scapy raw function to send a packet and receive its answer. WARNING: This is an internal function. Using sr/srp/sr1/srp is more appropriate in many cases.

scapy.sendrecv.sndrcvflood(pks: SuperSocket, pkt: List[Packet] | Packet | SetGen[Packet] | _PacketList[Packet], inter: int = 0, maxretries: int | None = None, verbose: int | None = None, chainCC: bool = False, timeout: int | None = None) Tuple[SndRcvList, PacketList][源代码]

sndrcv equivalent for flooding.

scapy.sendrecv.sniff(*args: Any, **kwargs: Any) PacketList[源代码]

Sniff packets and return a list of packets.

参数:
  • count -- number of packets to capture. 0 means infinity.

  • store -- whether to store sniffed packets or discard them

  • prn -- function to apply to each packet. If something is returned, it is displayed. --Ex: prn = lambda x: x.summary()

  • session -- a session = a flow decoder used to handle stream of packets. --Ex: session=TCPSession See below for more details.

  • filter -- BPF filter to apply.

  • lfilter -- Python function applied to each packet to determine if further action may be done. --Ex: lfilter = lambda x: x.haslayer(Padding)

  • offline -- PCAP file (or list of PCAP files) to read packets from, instead of sniffing them

  • quiet -- when set to True, the process stderr is discarded (default: False).

  • timeout -- stop sniffing after a given time (default: None).

  • L2socket -- use the provided L2socket (default: use conf.L2listen).

  • opened_socket -- provide an object (or a list of objects) ready to use .recv() on.

  • stop_filter -- Python function applied to each packet to determine if we have to stop the capture after this packet. --Ex: stop_filter = lambda x: x.haslayer(TCP)

  • iface -- interface or list of interfaces (default: None for sniffing on all interfaces).

  • monitor -- use monitor mode. May not be available on all OS

  • started_callback -- called as soon as the sniffer starts sniffing (default: None).

The iface, offline and opened_socket parameters can be either an element, a list of elements, or a dict object mapping an element to a label (see examples below).

For more information about the session argument, see https://scapy.rtfd.io/en/latest/usage.html#advanced-sniffing-sniffing-sessions

Examples: synchronous
>>> sniff(filter="arp")
>>> sniff(filter="tcp",
...       session=IPSession,  # defragment on-the-flow
...       prn=lambda x: x.summary())
>>> sniff(lfilter=lambda pkt: ARP in pkt)
>>> sniff(iface="eth0", prn=Packet.summary)
>>> sniff(iface=["eth0", "mon0"],
...       prn=lambda pkt: "%s: %s" % (pkt.sniffed_on,
...                                   pkt.summary()))
>>> sniff(iface={"eth0": "Ethernet", "mon0": "Wifi"},
...       prn=lambda pkt: "%s: %s" % (pkt.sniffed_on,
...                                   pkt.summary()))
Examples: asynchronous
>>> t = AsyncSniffer(iface="enp0s3")
>>> t.start()
>>> time.sleep(1)
>>> print("nice weather today")
>>> t.stop()
scapy.sendrecv.sr(x: List[Packet] | Packet | SetGen[Packet] | _PacketList[Packet], promisc: bool | None = None, filter: str | None = None, iface: NetworkInterface | str | None = None, nofilter: int = 0, *args: Any, **kargs: Any) Tuple[SndRcvList, PacketList][源代码]

Send and receive packets at layer 3

参数:
  • pks -- SuperSocket instance to send/receive packets

  • pkt -- the packet to send

  • timeout -- how much time to wait after the last packet has been sent

  • inter -- delay between two packets during sending

  • verbose -- set verbosity level

  • chainCC -- if True, KeyboardInterrupts will be forwarded

  • retry -- if positive, how many times to resend unanswered packets if negative, how many times to retry when no more packets are answered

  • multi -- whether to accept multiple answers for the same stimulus

  • rcv_pks -- if set, will be used instead of pks to receive packets. packets will still be sent through pks

  • prebuild -- pre-build the packets before starting to send them. Automatically enabled when a generator is passed as the packet

  • _flood --

  • threaded -- if True, packets will be sent in an individual thread

  • session -- a flow decoder used to handle stream of packets

  • chainEX -- if True, exceptions during send will be forwarded

scapy.sendrecv.sr1(*args: Any, **kargs: Any) Packet | None[源代码]

Send packets at layer 3 and return only the first answer

参数:
  • pks -- SuperSocket instance to send/receive packets

  • pkt -- the packet to send

  • timeout -- how much time to wait after the last packet has been sent

  • inter -- delay between two packets during sending

  • verbose -- set verbosity level

  • chainCC -- if True, KeyboardInterrupts will be forwarded

  • retry -- if positive, how many times to resend unanswered packets if negative, how many times to retry when no more packets are answered

  • multi -- whether to accept multiple answers for the same stimulus

  • rcv_pks -- if set, will be used instead of pks to receive packets. packets will still be sent through pks

  • prebuild -- pre-build the packets before starting to send them. Automatically enabled when a generator is passed as the packet

  • _flood --

  • threaded -- if True, packets will be sent in an individual thread

  • session -- a flow decoder used to handle stream of packets

  • chainEX -- if True, exceptions during send will be forwarded

scapy.sendrecv.sr1flood(x: List[Packet] | Packet | SetGen[Packet] | _PacketList[Packet], promisc: bool | None = None, filter: str | None = None, iface: NetworkInterface | str | None = None, nofilter: int = 0, *args: Any, **kargs: Any) Packet | None[源代码]

Flood and receive packets at layer 3 and return only the first answer

参数:
  • prn -- function applied to packets received

  • verbose -- set verbosity level

  • nofilter -- put 1 to avoid use of BPF filters

  • filter -- provide a BPF filter

  • iface -- listen answers only on the given interface

scapy.sendrecv.sr_func(*args: Any, **kargs: Any) Packet | None[源代码]

Send packets at layer 3 and return only the first answer

参数:
  • pks -- SuperSocket instance to send/receive packets

  • pkt -- the packet to send

  • timeout -- how much time to wait after the last packet has been sent

  • inter -- delay between two packets during sending

  • verbose -- set verbosity level

  • chainCC -- if True, KeyboardInterrupts will be forwarded

  • retry -- if positive, how many times to resend unanswered packets if negative, how many times to retry when no more packets are answered

  • multi -- whether to accept multiple answers for the same stimulus

  • rcv_pks -- if set, will be used instead of pks to receive packets. packets will still be sent through pks

  • prebuild -- pre-build the packets before starting to send them. Automatically enabled when a generator is passed as the packet

  • _flood --

  • threaded -- if True, packets will be sent in an individual thread

  • session -- a flow decoder used to handle stream of packets

  • chainEX -- if True, exceptions during send will be forwarded

scapy.sendrecv.srflood(x: List[Packet] | Packet | SetGen[Packet] | _PacketList[Packet], promisc: bool | None = None, filter: str | None = None, iface: NetworkInterface | str | None = None, nofilter: bool | None = None, *args: Any, **kargs: Any) Tuple[SndRcvList, PacketList][源代码]

Flood and receive packets at layer 3

参数:
  • prn -- function applied to packets received

  • unique -- only consider packets whose print

  • nofilter -- put 1 to avoid use of BPF filters

  • filter -- provide a BPF filter

  • iface -- listen answers only on the given interface

scapy.sendrecv.srloop(pkts: List[Packet] | Packet | SetGen[Packet] | _PacketList[Packet], *args: Any, **kargs: Any) Tuple[SndRcvList, PacketList][源代码]

Send a packet at layer 3 in loop and print the answer each time srloop(pkts, [prn], [inter], [count], ...) --> None

scapy.sendrecv.srp(x: List[Packet] | Packet | SetGen[Packet] | _PacketList[Packet], promisc: bool | None = None, iface: NetworkInterface | str | None = None, iface_hint: str | None = None, filter: str | None = None, nofilter: int = 0, type: int = 3, *args: Any, **kargs: Any) Tuple[SndRcvList, PacketList][源代码]

Send and receive packets at layer 2

参数:
  • pks -- SuperSocket instance to send/receive packets

  • pkt -- the packet to send

  • timeout -- how much time to wait after the last packet has been sent

  • inter -- delay between two packets during sending

  • verbose -- set verbosity level

  • chainCC -- if True, KeyboardInterrupts will be forwarded

  • retry -- if positive, how many times to resend unanswered packets if negative, how many times to retry when no more packets are answered

  • multi -- whether to accept multiple answers for the same stimulus

  • rcv_pks -- if set, will be used instead of pks to receive packets. packets will still be sent through pks

  • prebuild -- pre-build the packets before starting to send them. Automatically enabled when a generator is passed as the packet

  • _flood --

  • threaded -- if True, packets will be sent in an individual thread

  • session -- a flow decoder used to handle stream of packets

  • chainEX -- if True, exceptions during send will be forwarded

scapy.sendrecv.srp1(*args: Any, **kargs: Any) Packet | None[源代码]

Send and receive packets at layer 2 and return only the first answer

参数:
  • pks -- SuperSocket instance to send/receive packets

  • pkt -- the packet to send

  • timeout -- how much time to wait after the last packet has been sent

  • inter -- delay between two packets during sending

  • verbose -- set verbosity level

  • chainCC -- if True, KeyboardInterrupts will be forwarded

  • retry -- if positive, how many times to resend unanswered packets if negative, how many times to retry when no more packets are answered

  • multi -- whether to accept multiple answers for the same stimulus

  • rcv_pks -- if set, will be used instead of pks to receive packets. packets will still be sent through pks

  • prebuild -- pre-build the packets before starting to send them. Automatically enabled when a generator is passed as the packet

  • _flood --

  • threaded -- if True, packets will be sent in an individual thread

  • session -- a flow decoder used to handle stream of packets

  • chainEX -- if True, exceptions during send will be forwarded

scapy.sendrecv.srp1flood(x: List[Packet] | Packet | SetGen[Packet] | _PacketList[Packet], promisc: bool | None = None, filter: str | None = None, iface: NetworkInterface | str | None = None, nofilter: int = 0, *args: Any, **kargs: Any) Packet | None[源代码]

Flood and receive packets at layer 2 and return only the first answer

参数:
  • prn -- function applied to packets received

  • verbose -- set verbosity level

  • nofilter -- put 1 to avoid use of BPF filters

  • filter -- provide a BPF filter

  • iface -- listen answers only on the given interface

scapy.sendrecv.srpflood(x: List[Packet] | Packet | SetGen[Packet] | _PacketList[Packet], promisc: bool | None = None, filter: str | None = None, iface: NetworkInterface | str | None = None, iface_hint: str | None = None, nofilter: bool | None = None, *args: Any, **kargs: Any) Tuple[SndRcvList, PacketList][源代码]

Flood and receive packets at layer 2

参数:
  • prn -- function applied to packets received

  • unique -- only consider packets whose print

  • nofilter -- put 1 to avoid use of BPF filters

  • filter -- provide a BPF filter

  • iface -- listen answers only on the given interface

scapy.sendrecv.srploop(pkts: List[Packet] | Packet | SetGen[Packet] | _PacketList[Packet], *args: Any, **kargs: Any) Tuple[SndRcvList, PacketList][源代码]

Send a packet at layer 2 in loop and print the answer each time srloop(pkts, [prn], [inter], [count], ...) --> None

scapy.sendrecv.tshark(*args: Any, **kargs: Any) None[源代码]

Sniff packets and print them calling pkt.summary(). This tries to replicate what text-wireshark (tshark) would look like