[Python] I'm using Scapy in Python3 to request a DNS query, but I'm not getting a response

Discussão em 'Python' iniciado por Stack, Junho 10, 2025.

  1. Stack

    Stack Membro Participativo

    Environment: WSL Ubuntu Network: Bridge When requesting DNS through dns.resolver in Python, a response is received, but when importing Scapy and requesting a DNS query, no response is received. No firewall was set up, and scapy was installed from https://github.com/secdev/scapy/archive/master.zip.

    from scapy.all import *

    def send_dns_query(domain, record_type="A", dns_server="8.8.8.8", iface="eth0"):
    """
    Send a DNS query to a specified DNS server for a specific record type.
    :param domain: The domain to query for (e.g., 'example.com')
    :param record_type: The DNS record type to query (e.g., 'A', 'AAAA', 'MX', etc.)
    :param dns_server: The DNS server to send the query to (default: '8.8.8.8')
    :param iface: The network interface to use (default: eth0)
    :return: None
    """
    # DNS 쿼리 패킷 생성
    dns_query = IP(dst=dns_server) / UDP(dport=53) / DNS(rd=1, qd=DNSQR(qname=domain, qtype=record_type))

    # 패킷 전송 및 응답 받기
    try:
    response = sr1(dns_query, timeout=10, iface=iface, verbose=0) # 응답 대기 시간 10초로 설정

    # 응답이 오면
    if response:
    print(f"\n[{record_type}] 레코드 결과:")
    response.show() # 응답 패킷을 출력해서 응답이 오고 있는지 확인
    if response.haslayer(DNS):
    if response[DNS].ancount > 0:
    for i in range(response[DNS].ancount):
    rrname = response[DNS].an.rrname.decode() # 레코드 이름
    rdata = response[DNS].an.rdata # 레코드 데이터
    if isinstance(rdata, bytes):
    rdata = rdata.decode(errors="ignore")
    print(f"{rrname}: {rdata}")
    else:
    print("응답이 없습니다.")
    else:
    print(f"{record_type} 레코드에 대한 응답이 없습니다.")
    except Exception as e:
    print(f"쿼리 실패: {e}")

    def get_records(domain):
    """
    Get TXT and MX DNS records for a domain using scapy.
    :param domain: The domain to query for
    :return: None
    """
    query_types = [
    'TXT', # 텍스트 레코드
    'MX', # 메일 교환기
    ]

    for record_type in query_types:
    try:
    send_dns_query(domain, record_type)
    except Exception as e:
    print(f"{record_type} 레코드 쿼리 실패: {e}")

    if __name__ == '__main__':
    get_records('example.com')

    Continue reading...

Compartilhe esta Página