or run

tessl search
Log in

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/dnslib@0.9.x
tile.json

tessl/pypi-dnslib

tessl install tessl/pypi-dnslib@0.9.0

Simple library to encode/decode DNS wire-format packets

Agent Success

Agent success rate when using this tile

97%

Improvement

Agent success rate improvement when using this tile compared to baseline

1.76x

Baseline

Agent success rate without this tile

55%

task.mdevals/scenario-3/

DNS Message Builder

Build a DNS message builder tool that constructs DNS query and response packets and encodes them to wire format for network transmission.

Background

DNS (Domain Name System) messages are binary packets transmitted over networks. Creating these packets requires encoding DNS records into the proper wire format according to RFC 1035 specifications. Your tool should allow users to construct DNS messages programmatically and output them as binary data suitable for network transmission.

Requirements

Core Functionality

Implement a DNS message builder in dns_builder.py with the following capabilities:

  1. Query Construction: Create a DNS query message for a domain name with a specified record type (e.g., A, AAAA, MX, TXT). The query should include:

    • A proper DNS header with query flags
    • A question section with the domain name and query type
    • The ability to encode the complete message to wire format
  2. Response Construction: Create a DNS response message that includes:

    • A proper DNS header with response flags
    • A question section matching the original query
    • One or more answer records with appropriate data
    • The ability to encode the complete message to wire format
  3. Record Support: Support at least these record types in answers:

    • A records (IPv4 addresses)
    • AAAA records (IPv6 addresses)
    • MX records (mail exchange with priority)
    • TXT records (text data)
  4. Wire Format Encoding: Provide functionality to convert DNS messages to binary wire format suitable for transmission over UDP or TCP.

Implementation Details

Create a module with the following functions:

  • build_query(domain: str, record_type: str) -> bytes: Constructs a DNS query and returns it in wire format
  • build_response(domain: str, record_type: str, answers: list) -> bytes: Constructs a DNS response with the given answers and returns it in wire format

Where answers is a list of dictionaries containing record data. For example:

  • A record: {"address": "192.0.2.1", "ttl": 300}
  • AAAA record: {"address": "2001:db8::1", "ttl": 300}
  • MX record: {"preference": 10, "exchange": "mail.example.com", "ttl": 300}
  • TXT record: {"text": "v=spf1 -all", "ttl": 300}

Example Usage

from dns_builder import build_query, build_response

# Build a query for an A record
query_bytes = build_query("example.com", "A")
# Returns binary data ready for network transmission

# Build a response with multiple A records
response_bytes = build_response(
    "example.com",
    "A",
    [
        {"address": "192.0.2.1", "ttl": 300},
        {"address": "192.0.2.2", "ttl": 300}
    ]
)

Dependencies { .dependencies }

dnslib { .dependency }

Python library for DNS packet manipulation and wire format encoding/decoding.

Test Cases

Test 1: Build A record query @test

from dns_builder import build_query

# Build query
query_bytes = build_query("example.com", "A")

# Verify it's binary data
assert isinstance(query_bytes, bytes)
assert len(query_bytes) > 0

# Verify it's a valid DNS packet (should start with transaction ID and flags)
assert len(query_bytes) >= 12  # Minimum DNS header size

Test 2: Build A record response @test

from dns_builder import build_response

# Build response with single A record
response_bytes = build_response(
    "example.com",
    "A",
    [{"address": "192.0.2.53", "ttl": 3600}]
)

# Verify it's binary data
assert isinstance(response_bytes, bytes)
assert len(response_bytes) > 0

Test 3: Build MX record response @test

from dns_builder import build_response

# Build response with MX records
response_bytes = build_response(
    "example.com",
    "MX",
    [
        {"preference": 10, "exchange": "mail1.example.com", "ttl": 3600},
        {"preference": 20, "exchange": "mail2.example.com", "ttl": 3600}
    ]
)

# Verify it's binary data
assert isinstance(response_bytes, bytes)
assert len(response_bytes) > 0

Testing

Create tests in dns_builder.test.py that verify:

  • Queries can be built for different record types
  • Responses can be built with various answer records
  • The output is valid binary data in wire format
  • Multiple answer records can be included in a single response

Constraints

  • Use standard Python 3.6+ features
  • The wire format must be RFC 1035 compliant
  • Binary output should be directly usable for network transmission