簡體   English   中英

未找到來自 scapy 的圖層錯誤

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM