AttributeError:'NoneType'对象没有属性'split'错误

时间:2020-04-20 20:11:00

标签: python ip scapy

我正在运行此代码以寻求arp请求和以太币。当我运行它时,它会给我消息

 Traceback (most recent call last):
  File "network_scanner.py", line 16, in <module>
    scan('10.0.2.1/24')
  File "network_scanner.py", line 8, in scan
    arp_request.show()
  File "/usr/local/lib/python2.7/dist-packages/scapy-2.4.3- 
   py2.7.egg/scapy/packet.py", line 1261, in show
    return self._show_or_dump(dump, indent, lvl, label_lvl)
  File "/usr/local/lib/python2.7/dist-packages/scapy-2.4.3- 
   py2.7.egg/scapy/packet.py", line 1235, in _show_or_dump
    reprval = f.i2repr(self, fvalue)
  File "/usr/local/lib/python2.7/dist-packages/scapy-2.4.3- 
   py2.7.egg/scapy/fields.py", line 376, in i2repr
    return fld.i2repr(pkt, val)
  File "/usr/local/lib/python2.7/dist-packages/scapy-2.4.3- 
   py2.7.egg/scapy/fields.py", line 502, in i2repr
    x = self.i2h(pkt, x)
  File "/usr/local/lib/python2.7/dist-packages/scapy-2.4.3- 
   py2.7.egg/scapy/layers/l2.py", line 136, in i2h
    iff = self.getif(pkt)
  File "/usr/local/lib/python2.7/dist-packages/scapy-2.4.3- 
   py2.7.egg/scapy/layers/l2.py", line 132, in <lambda>
    self.getif = (lambda pkt: pkt.route()[0]) if getif is None else getif
  File "/usr/local/lib/python2.7/dist-packages/scapy-2.4.3- 
   py2.7.egg/scapy/layers/l2.py", line 400, in route
    fld, dst = fld._find_fld_pkt_val(self, dst)
  File "/usr/local/lib/python2.7/dist-packages/scapy-2.4.3- 
   py2.7.egg/scapy/fields.py", line 313, in _find_fld_pkt_val
    if val == dflts_pkt[self.name] and self.name not in pkt.fields:
  File "/usr/local/lib/python2.7/dist-packages/scapy-2.4.3- 
   py2.7.egg/scapy/base_classes.py", line 133, in __eq__
    p2, nm2 = self._parse_net(other)
  File "/usr/local/lib/python2.7/dist-packages/scapy-2.4.3- 
   py2.7.egg/scapy/base_classes.py", line 99, in _parse_net
    tmp = net.split('/') + ["32"]
    AttributeError: 'NoneType' object has no attribute 'split'

我下载了错误的内容还是输入错误?有没有简单的解决方法,我对这种东西很陌生,所以我想增加我的知识。我正在运行的代码如下

#!/usr/bin/env python

import scapy.all as scapy

def scan(ip):
    arp_request = scapy.ARP(pdst=ip)
    arp_request.show()
    broadcast = scapy.Ether(dst='ff:ff:ff:ff:ff:ff')
    broadcast.show()
    arp_request_broadcast = broadcast/arp_request
    print(arp_request_broadcast.summary())
    arp_request_broadcast.show()

scan('10.0.2.1/24')

任何能够给我脚踏实地答案的人都会被赞赏,因为我不是一个狡猾的向导。 谢谢

2 个答案:

答案 0 :(得分:1)

您可以在单个数据包上而不是一组数据包上使用.show().summary()10.0.2.1/24是一个包含256个地址的子网,因此当您将其传递到ARP时,它将创建256个ARP数据包。

import scapy.all as scapy

def scan(ip):
    arp_requests = scapy.ARP(pdst=ip)
    for req in arp_requests:
        req.show()
    broadcast = scapy.Ether(dst='ff:ff:ff:ff:ff:ff')
    broadcast.show()
    arp_request_broadcast = broadcast/arp_requests
    arp_request_broadcast.summary() 
    for req in arp_request_broadcast:
        req.show()

scan('10.0.2.1/24')

如果您在此处遍历所有数据包,您的代码不会出错。

输出将如下所示(仅显示每个for循环的最后一个数据包):

...<truncated>
###[ ARP ]###
  hwtype= 0x1
  ptype= IPv4
  hwlen= None
  plen= None
  op= who-has
  hwsrc= 6c:96:cf:d8:7f:e7
  psrc= 192.168.1.246
  hwdst= 00:00:00:00:00:00
  pdst= 10.0.2.255

...<truncated>
###[ Ethernet ]###
  dst= ff:ff:ff:ff:ff:ff
  src= 6c:96:cf:d8:7f:e7
  type= ARP
###[ ARP ]###
     hwtype= 0x1
     ptype= IPv4
     hwlen= None
     plen= None
     op= who-has
     hwsrc= 6c:96:cf:d8:7f:e7
     psrc= 192.168.1.246
     hwdst= 00:00:00:00:00:00
     pdst= 10.0.2.255

答案 1 :(得分:0)

这是Scapy中的错误,现已修复。您的代码看起来不错(是的,您可以给scapy.ARP()输入一个IP范围)。

错误:https://github.com/secdev/scapy/issues/2400

修复:https://github.com/secdev/scapy/pull/2395

更新后的代码:https://github.com/secdev/scapy

只需更新scapy,您可能会很方便。

代码:

import scapy.all as scapy

def scan(ip):
    arp_requests = scapy.ARP(pdst=ip)
    for req in arp_requests:
        req.show()
    broadcast = scapy.Ether(dst='ff:ff:ff:ff:ff:ff')
    broadcast.show()
    arp_request_broadcast = broadcast/arp_requests
    arp_request_broadcast.summary()
    for req in arp_request_broadcast:
        req.show()

scan('10.0.2.1/24')

输出(仅最后几个数据包摘要):

###[ ARP ]### 
     hwtype    = 0x1
     ptype     = IPv4
     hwlen     = None
     plen      = None
     op        = who-has
     hwsrc     = 08:00:27:46:c6:d8
     psrc      = 10.0.2.7
     hwdst     = 00:00:00:00:00:00
     pdst      = 10.0.2.253

###[ Ethernet ]### 
  dst       = ff:ff:ff:ff:ff:ff
  src       = 08:00:27:46:c6:d8
  type      = ARP
###[ ARP ]### 
     hwtype    = 0x1
     ptype     = IPv4
     hwlen     = None
     plen      = None
     op        = who-has
     hwsrc     = 08:00:27:46:c6:d8
     psrc      = 10.0.2.7
     hwdst     = 00:00:00:00:00:00
     pdst      = 10.0.2.254

###[ Ethernet ]### 
  dst       = ff:ff:ff:ff:ff:ff
  src       = 08:00:27:46:c6:d8
  type      = ARP
###[ ARP ]### 
     hwtype    = 0x1
     ptype     = IPv4
     hwlen     = None
     plen      = None
     op        = who-has
     hwsrc     = 08:00:27:46:c6:d8
     psrc      = 10.0.2.7
     hwdst     = 00:00:00:00:00:00
     pdst      = 10.0.2.255


Process finished with exit code 0