#!/usr/bin/python import scapy.all as scapy import random import time import subprocess def send_response(t): t.src, t.dst = t.dst, t.src # swap ethernet addresses ip = t.getlayer("IP") ip.src, ip.dst = ip.dst, ip.src t.dport, t.sport = t.sport, t.dport t.getlayer("TCP").flags |= 0x10 # set the ACK flag t.ack = t.seq + 1 del t.getlayer("TCP").chksum scapy.sendp(t, iface="eth0", verbose=False) print("Filtering input from 10.0.0.1...") subprocess.call("iptables -I INPUT -s 10.0.0.1 -j DROP", shell=True) try: scapy.sniff(filter="host 10.0.0.2 and tcp[tcpflags] == tcp-syn", iface="eth0", prn=send_response, store=False) finally: print("Accepting input from 10.0.0.1 again...") subprocess.call("iptables -D INPUT -s 10.0.0.1 -j DROP", shell=True)