scapython Debian中的错误

2024-06-16 11:51:35 发布

您现在位置:Python中文网/ 问答频道 /正文

下午好,我在Debian上创建了一个简单的嗅探器,但我无法运行它

from scapy.all import *


counter = 0

def analysis(pkt):
   global counter
   print(counter, ":  Ethernet")
   print("Src:", pkt["Ether"].src)
   print("Dst:", pkt["Ether"].dst)
   print(pkt.sprintf("Type: %Ether.type%"))

   if pkt["Ether"].type == 2054:     #ARP
      print("###ARP###")
      if pkt["ARP"].op == 1:
          print("echo-request")
      elif pkt["ARP"].op == 2:
          print("echo-reply")

   elif pkt["Ether"].type == 2048:   #IPv4
      print("###IPv4###")
      print("Src:", pkt["IP"].src)
      print("Dst:", pkt["IP"].dst)
      print("TTL:", pkt["IP"].ttl)
      print("High level protocol: ", pkt.sprintf("%IP.proto%"))    #udp 17
      print("Data size:", pkt["IP"].len-20)
      if pkt["IP"].proto == 17:
          print("     ###UDP###")
          print("     Src port:", pkt["UDP"].sport)
          print("     Dst port:", pkt["UDP"].dport)
          print("     Data size:", pkt["UDP"].len - 8)

  counter += 1
  print("---------------------------------------------------")


 if __name__ == '__main__':
    ifaces = os.listdir('/sys/class/net/')
    sniff(iface=ifaces, prn=analysis)

在Pycharm中运行时出错(在python3.7和3.9上尝试:

home/danya/Python-3.9.1/python /home/danya/PycharmProjects/main.py
  Traceback (most recent call last):
File "/home/danya/PycharmProjects/main.py", line 40, in <module>
  sniff(iface=ifaces, prn=analysis)
File "/home/danya/.local/lib/python3.9/site-packages/scapy/sendrecv.py", line 1263, in sniff
  sniffer._run(*args, **kwargs)
File "/home/danya/.local/lib/python3.9/site-packages/scapy/sendrecv.py", line 1127, in _run
  sniff_sockets[L2socket(type=ETH_P_ALL, iface=iface,
File "/home/danya/.local/lib/python3.9/site-packages/scapy/arch/linux.py", line 486, in init
  self.ins = socket.socket(
File "/home/danya/Python-3.9.1/Lib/socket.py", line 232, in init
  _socket.socket.__init__(self, family, type, proto, fileno)
PermissionError: [Errno 1] Operation not permitted

Process finished with exit code 1

Tags: inpyiphomeiftypelinecounter