我找到了互联网上建议的 EDNS、EDNSOptions 类,用于将 EDNS 选项添加到 DNS 服务器中的响应数据包。但是在scapy.layers.dns中找不到这些类。我需要一些关于如何将 ENDS 选项添加到 DNS 响应的帮助。
下面是我正在使用的 DNS 服务器的代码。
from scapy.layers.l2 import Ether
from scapy.sendrecv import sendp, sniff
from scapy.layers.dns import DNS, DNSRR
import dns.edns
response = dns.message.Message()
from scapy.layers.inet import IP, UDP
# Set the interface to listen and respond on
net_interface = "Wi-Fi"
# # Berkeley Packet Filter for sniffing specific DNS packet only
packet_filter = " and ".join([
"udp dst port 53", # Filter UDP port 53
"udp[10] & 0x80 = 0", # DNS queries only
"dst host 192.168.9.0" # IP source <ip>
])
def dns_reply(packet):
# Construct the DNS packet
# Construct the Ethernet header by looking at the sniffed packet
eth = Ether(
src=packet[Ether].dst,
dst=packet[Ether].src
)
# Construct the IP header by looking at the sniffed packet
ip = IP(
src=packet[IP].dst,
dst=packet[IP].src
)
# Construct the UDP header by looking at the sniffed packet
udp = UDP(
dport=packet[UDP].sport,
sport=packet[UDP].dport
)
# Construct the DNS response by looking at the sniffed packet and manually
dns = DNS(
id=packet[DNS].id,
qd=packet[DNS].qd,
aa=1,
rd=0,
qr=1,
qdcount=1,
ancount=1,
nscount=0,
arcount=1,
an=DNSRR(rrname='example.com', ttl=60, rdata='1.2.3.4'),
ar=DNSRR(
rrname=packet[DNS].qd.qname,
type='OPT', # set the resource record type to OPT for EDNS
ttl=0,
rclass=4096, # set the rclass to 4096 to indicate the use of EDNS
rdlen=None,
))
# Put the full packet together
response_packet = eth / ip / udp / dns
# Send the DNS response
sendp(response_packet, iface=net_interface)
# Sniff for a DNS query matching the 'packet_filter' and send a specially crafted reply
sniff(filter=packet_filter, prn=dns_reply, store=0, iface=net_interface, count=1)