Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#!/usr/bin/env python
import random
import struct
import sys
RR_TYPE_A = 1
RR_TYPE_NS = 2
RR_TYPE_CNAME = 5
RR_TYPE_SOA = 6
RR_TYPE_PTR = 12
RR_TYPE_MX = 15
RR_TYPE_TXT = 16
RR_CLASS_IN = 1
def gen_header(ident, flags, qcount, acount, nscount, arcount):
return struct.pack("!HHHHHH",
ident, flags,
qcount, acount,
nscount, arcount)
def gen_name(name):
buffer = b""
for part in name.split('.'):
label = part.encode("ascii")
buffer += bytes([len(label)]) + label
return buffer + b'\0'
def gen_rr(name, typ, clas, ttl, rd):
buffer = gen_name(name)
buffer += struct.pack("!HHIH", typ, clas, ttl, len(rd))
buffer += rd
return buffer
sys.stdout.buffer.write(gen_header(random.getrandbits(16), 0x0001, 0, 2, 0, 0))
sys.stdout.buffer.write(gen_rr('', RR_TYPE_TXT, RR_CLASS_IN, 0xabcd, b'b'))
sys.stdout.buffer.write(gen_rr('', RR_TYPE_TXT, RR_CLASS_IN, 0xabcd, b'g'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment