[英]Layer not found errors from scapy
我正在尝试使用 scapy 嗅探数据包并不断收到以下错误,但不知道如何修复它。
这是代码和错误:
from scapy.layers.inet import IP, TCP, UDP
from scapy.all import *
import socket
import datetime
def sniff_packets(pkt):
time = datetime.datetime.now()
# classifying packets into TCP
if pkt.haslayer(TCP):
# classyfying packets into TCP Incoming packets
if socket.gethostbyname(socket.gethostname()) == pkt[IP].dst:
print(str("[") + str(time) + str("]") + " " + "TCP-IN:{}".format(
len(pkt[TCP])) + " Bytes" + " " + "SRC-MAC:" + str(pkt.src) + " " + `enter code here`"DST-MAC:" + str(
pkt.dst) + " " + "SRC-PORT:" + str(pkt.sport) + " " + "DST-PORT:" + str(
pkt.dport) + " " + "SRC-IP:" + str(pkt[IP].src) + " " + "DST-IP:" + str(
pkt[IP].dst))
if socket.gethostbyname(socket.gethostname()) == pkt[IP].src:
print(str("[") + str(time) + str("]") + " " + "TCP-OUT:{}".format(
len(pkt[TCP])) + " Bytes" + " " + "SRC-MAC:" + str(pkt.src) + " " + `enter code here`"DST-MAC:" + str(
pkt.dst) + " " + "SRC-PORT:" + str(pkt.sport) + " " + "DST-PORT:" + str(
pkt.dport) + " " + "SRC-IP:" + str(pkt[IP].src) + " " + "DST-IP:" + str(
pkt[IP].dst))
# classifying packets into UDP
if pkt.haslayer(UDP):
if socket.gethostbyname(socket.gethostname()) == pkt[IP].src:
# classyfying packets into UDP Outgoing packets
print(str("[") + str(time) + str("]") + " " + "UDP-OUT:{}".format(
len(pkt[UDP])) + " Bytes " + " " + "SRC-MAC:" + str(pkt.src) + " " + `enter code here`"DST-
MAC:" + str(
pkt.dst) + " " + "SRC-PORT:" + str(pkt.sport) + " " + "DST-PORT:" + str(
pkt.dport) + " " + "SRC-IP:" + str(pkt[IP].src) + " " + "DST-IP:" + str(
pkt[IP].dst))
if socket.gethostbyname(socket.gethostname()) == pkt[IP].dst:
# classyfying packets into UDP Incoming packets
print(str("[") + str(time) + str("]") + " " + "UDP-IN:{}".format(
len(pkt[UDP])) + " Bytes " + " " + "SRC-MAC:" + str(pkt.src) + " " + `enter code here`"DST-MAC:" + str(
pkt.dst) + " " + "SRC-PORT:" + str(pkt.sport) + " " + "DST-PORT:" + str(
pkt.dport) + " " + "SRC-IP:" + str(pkt[IP].src) + " " + "DST-IP:" + str(
pkt[IP].dst))
#wrpcap("test.pcap", )
if __name__ == '__main__':
sniff(prn=sniff_packets)
Traceback (most recent call last):
File "C:\Users\tathi\CYBR-260-FINAL\final1.py", line 48, in <module>
sniff(prn=sniff_packets)
File "C:\Users\tathi\venv\lib\site-packages\scapy\sendrecv.py", line 1263, in sniff
sniffer._run(*args, **kwargs)
File "C:\Users\tathi\venv\lib\site-packages\scapy\sendrecv.py", line 1210, in _run
session.on_packet_received(p)
File "C:\Users\tathi\venv\lib\site-packages\scapy\sessions.py", line 108, in
on_packet_received
result = self.prn(pkt)
File "C:\Users\tathi\CYBR-260-FINAL\final1.py", line 28, in sniff_packets
if socket.gethostbyname(socket.gethostname()) == pkt[IP].src:
File "C:\Users\tathi\venv\lib\site-packages\scapy\packet.py", line 1344, in __getitem__
raise IndexError("Layer [%s] not found" % name)
IndexError: Layer [IP] not found
看起来pkt
中没有关键IP
。 您可以通过检查if IP in pkt
来检查密钥是否存在,然后继续执行其他代码。
def sniff_packets(pkt):
if not IP in pkt:
return
... # Code that checks for IP value in pkt
或者,您可以检查任何特定于域的函数,如执行等效操作的pkt.haslayer
。
def sniff_packets(pkt):
if not pkt.haslayer(IP):
return
... # Code that checks for IP value in pkt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.