sniff(*args, **kwargs)
Sniff packets and return a list of packets.
Args:
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()
# coding=utf-8
# 嗅探数据包
from scapy.all import *
from scapy.layers.inet import IP
from scapy.layers.l2 import ARP
# print(lsc()) #lsc()列出所有的函数
print(help(sniff)) # 查看sniff函数有哪些参数
print(show_interfaces()) # 显示网卡信息
ifaces_wifi = "Intel(R) Wi-Fi 6 AX201 160MHz" #WIFI接口名称
iface_WM8 = "VMware Virtual Ethernet Adapter for VMnet8"
filter_tcp = "tcp"
filter_icmp = "icmp"
# iface指定抓包的网卡,不指定代表所有网卡
sniff(filter=filter_icmp, iface = iface_WM8, prn=lambda x:x.summary(),count = 3 )
sniff(filter=filter_tcp, iface = ifaces_wifi, prn=lambda x:x.summary(),count = 3 )
# stop_filter定义回调函数,满足条件(返回True)后停止抓包
def func_stop_filter(x):
src = x[IP].src
if src == "192.168.6.212":
print("src==192.168.6.212 返回true")
return True
else:
print("src!=192.168.6.212 返回False")
return False
sniff(filter=filter_icmp, prn=lambda x:x.summary(),stop_filter = func_stop_filter )
# store=1存储嗅探到的数据包,超时时间2秒, 即2秒后停止嗅探数据包
pkts = sniff(filter=filter_icmp, prn=lambda x:x.summary(),store=1, timeout = 2)
wrpcap("testScapySniffInterface.pcap",pkts)
print("offline从pcap中读取数据包")
print("------方法一: offline-------")
sniff(offline = "testScapySniffInterface.pcap",prn=lambda x:x.summary())
print("------方法二: rdpcap-------")
pkt_rdpcap_res = rdpcap("testScapySniffInterface.pcap")
print(pkt_rdpcap_res)
print(pkt_rdpcap_res[0]) # 读取第0个数据包
# L2socket = conf.L2listen 对各个iface进行监听,即Layer 2 socket。是在网络第二层(链路层)工作的套接字
print("--------L2socket = conf.L2listen 对各个iface进行监听--------")
sniff( prn=lambda x:x.summary(),count = 3, timeout = 5, L2socket = conf.L2listen )
# opend_socked = conf.L2listen 对各个iface进行监听
print("--------opend_socked 对指定socket对象调用recv方法抓包,而不会抓其他的包。--------")
s = conf.L2listen(type=ETH_P_ALL, iface=ifaces_wifi, filter="")
# sniff( prn=lambda x:x.summary(),count = 3, timeout = 5, opend_socket = s)
print("--------------lfilter------------")
'''
lfilter:类型为function,默认值为None。传入一个返回值为bool型的回调函数,一般为lambda函数。
会把每一个捕获到的包放入这个函数,当返回值为真时,才会放入返回的PacketList。
这个参数与filter参数不同,前者放入的是字符串,后者放入的是函数
'''
def func_lfilter(x):
try:
src = x[IP].src
if src == "192.168.6.212":
return True
else:
return False
except Exception as e:
print(e)
return False
sniff( prn=lambda x:x.summary(), timeout = 5, lfilter = func_lfilter)
'''
Help on function sniff in module scapy.sendrecv:
sniff(*args, **kwargs)
Sniff packets and return a list of packets.
Args:
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()
None
Source Index Name MAC IPv4 IPv6
libpcap 1 Software Loopback Interface 1 00:00:00:00:00:00 127.0.0.1
libpcap 9 WAN Miniport (IP)
None
Ether / IP / ICMP 192.168.6.1 > 192.168.6.212 echo-request 0 / Raw
Ether / IP / ICMP 192.168.6.212 > 192.168.6.1 echo-reply 0 / Raw
Ether / IP / ICMP 192.168.6.1 > 192.168.6.212 echo-request 0 / Raw
Ether / IP / TCP 192.168.6.195:55735 > 142.251.43.10:https S
Ether / IP / TCP 184.26.91.169:http > 192.168.6.195:55726 SA
Ether / IP / TCP 192.168.6.195:55726 > 184.26.91.169:http A
Ether / IP / ICMP 192.168.6.1 > 192.168.6.212 echo-request 0 / Raw
src!=192.168.6.212 返回False
Ether / IP / ICMP 192.168.6.212 > 192.168.6.1 echo-reply 0 / Raw
src==192.168.6.212 返回true
Ether / IP / ICMP 192.168.6.1 > 192.168.6.212 echo-request 0 / Raw
Ether / IP / ICMP 192.168.6.212 > 192.168.6.1 echo-reply 0 / Raw
offline从pcap中读取数据包
------方法一: offline-------
Ether / IP / ICMP 192.168.6.1 > 192.168.6.212 echo-request 0 / Raw
Ether / IP / ICMP 192.168.6.212 > 192.168.6.1 echo-reply 0 / Raw
------方法二: rdpcap-------
<testScapySniffInterface.pcap: TCP:0 UDP:0 ICMP:2 Other:0>
Ether / IP / ICMP 192.168.6.1 > 192.168.6.212 echo-request 0 / Raw
--------L2socket = conf.L2listen 对各个iface进行监听--------
Ether / IP / ICMP 192.168.6.1 > 192.168.6.212 echo-request 0 / Raw
Ether / IP / ICMP 192.168.6.212 > 192.168.6.1 echo-reply 0 / Raw
Ether / IP / ICMP 192.168.6.1 > 192.168.6.212 echo-request 0 / Raw
--------opend_socked 对指定socket对象调用recv方法抓包,而不会抓其他的包。--------
--------------lfilter------------
Ether / IP / ICMP 192.168.6.212 > 192.168.6.1 echo-reply 0 / Raw
Ether / IP / ICMP 192.168.6.212 > 192.168.6.1 echo-reply 0 / Raw
Ether / IP / ICMP 192.168.6.212 > 192.168.6.1 echo-reply 0 / Raw
Ether / IP / ICMP 192.168.6.212 > 192.168.6.1 echo-reply 0 / Raw
Layer [IP] not found
Layer [IP] not found
'''