Texts: Katic is writing a Python script to create a TAP interface. For each Ethernet frame that it receives, it sends out a spoofed reply Ethernet frame with a fake source MAC address: aa:bb:cc:dd:ce:ff. Please complete the following code for this task.
#!/usr/bin/python3
import fcntl
import struct
import os
import time
from scapy.all import *
TUNSETIFF = 0x408454ca
IFF_TUN = 0x0001
IFF_TAP = 0x0002
IFF_NO_PI = 0x1000
# Create the tap interface
tap = os.open("/dev/net/tun", os.O_RDWR)
ifr = struct.pack('16sH', b'tap%d', IFF_TAP | IFF_NO_PI)
ifname_bytes = fcntl.ioctl(tap, TUNSETIFF, ifr)
# Get the interface name
ifname = ifname_bytes.decode('UTF-8')[:16].strip("x00")
print("Interface Name: {}".format(ifname))
while True:
# Get a packet from the tap interface
packet = os.read(tap, 2048)
if True:
ether = Ether(packet)
newether = Ether(src="aa:bb:cc:dd:ce:ff", dst=ether.src)
newpkt = newether/ether.payload
os.write(tap, bytes(newpkt))