The good way to do this would be to implement an AnsweringMachine
class.
A quick-and-dirty (but working) way would be to use sniff
with a special prn
function:
def answer(p):
p = p[IP]
send(IP(dst=p.src, src=p.dst)/TCP(dport=p.sport, sport=p.dport,
ack=p.seq + 1, flags='SA'))
sniff(filter='tcp and tcp[tcpflags] & (tcp-syn|tcp-ack) == tcp-syn',
prn=answer, store=False)
You might want to add firewall rules to prevent your host's IP stack from sending RST-ACK packets before Scapy has a chance to send the SYN-ACK. You might also want to adjust the filter to answer only to packets destined for your host (or a particular host/network).