parse_dump.py 3.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/usr/bin/python
from scapy.all import *
from binascii import *
import base64
import sys
import dns.rdata
import dns.rrset
from struct import *

fr = open(sys.argv[1] + ".raw_data", 'wb')
fp = open(sys.argv[1] + ".parsed_data", 'wb')

def chop_and_write_rr_query(rr):
	name = dns.name.from_text(rr.qname)
15
#	print rr.qname
16 17 18

	wire = name.to_wire()
	fp.write(pack('B', len(wire)))
19
#	print len(wire)
20 21 22 23 24 25
	fp.write(wire)
	fp.write(pack('H', rr.qtype))
	fp.write(pack('H', rr.qclass))

def chop_and_write_rr_response(rr):
	name = dns.name.from_text(rr.rrname)
26
#	print rr.rrname
27 28 29 30 31 32 33 34 35 36

	wire = name.to_wire()
	fp.write(pack('B', len(wire)))
	fp.write(wire)
	fp.write(pack('H', rr.type))
	fp.write(pack('H', rr.rclass))
	fp.write(pack('L', rr.ttl))

	try:
		rdata = dns.rdata.from_wire(rr.rclass, rr.type, rr.rdata, 0, len(rr.rdata))
37
		fp.write(pack('H', len(rr.rdata)))
38
#		print "type ", rr.type, "length ", len(rr.rdata)
39
#		OPT has length 0 - it should have no rdata
40 41 42 43
		rdata.to_wire(fp)
	except:

		try:
44 45 46
#			if rr.rdata[0] != '\#':
			rdata = dns.rdata.from_text(rr.rclass, rr.type, rr.rdata)
			try:
47
				fp.write(pack('H', len(rdata)))
48
			except:
49
				# no length - no way to know wire length
50
				try:
51
#					print "unknown length for type", rr.type
52 53 54 55 56 57 58 59 60 61 62 63 64
#						if rr.type == 2:
#							fp.seek(1, 1)
#							old = fp.tell()
#							rdata.to_wire(fp)
#							size = fp.tell() - old
#							fp.seek(-(size + 1), 1)
#							fp.write(pack('B', size))
#							fp.seek(0, 2)
#						else:
					rdata.to_wire(fp)
				except Exception as e:
					print 'Error, exiting: ', e
					sys.exit(-1)
65 66
		except Exception as e:
			print 'Error,', e
67 68
			print 'could not parse rdata type: ', rr.type
			print 'dumping directly (hopefully it is SOA)'
69 70
# i need to do some kind of rollback here...
			fp.write(pack('H', len(rr.rdata)))
71 72
			fp.write(rr.rdata)

Jan Včelák's avatar
Jan Včelák committed
73

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
	if rr.type == 50:
		f = open('nsec3debug', 'wb')
		rdata.to_wire(f)
		f.close()

def chop_and_write_section_response(section):
	if section == None:
		return
	i = 0
	rr = section.getlayer(i);
	while rr != None:
		chop_and_write_rr_response(rr)
		i += 1
		rr = section.getlayer(i)

def chop_and_write_section_query(section):
	if section == None:
		return
	i = 0
	rr = section.getlayer(i);
	while rr != None:
		chop_and_write_rr_query(rr)
		i += 1
		rr = section.getlayer(i)

def chop_and_write_packet(packet):
	fp.write(pack('H', packet.id))
#	fp.write(pack('H', packet.qr))
#	fp.write(pack('H', packet.opcode))
#	fp.write(pack('H', packet.aa)) #TODO these are not uint16_t
Jan Včelák's avatar
Jan Včelák committed
104
#	fp.write(pack('H', packet.rcode))
105 106 107 108
	fp.write(pack('H', packet.qdcount))
	fp.write(pack('H', packet.ancount))
	fp.write(pack('H', packet.nscount))
	fp.write(pack('H', packet.arcount))
Jan Včelák's avatar
Jan Včelák committed
109

110 111
#write query flag
	fp.write(pack('H', packet.qr))
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

	chop_and_write_section_query(packet.qd)
	chop_and_write_section_response(packet.an)
	chop_and_write_section_response(packet.ns)
	chop_and_write_section_response(packet.ar)

packets = rdpcap(sys.argv[1])

total_length = len(packets)

fr.write(pack('L', total_length))
fp.write(pack('L', total_length))

for packet in packets:
	try:
		data = a2b_hex(str(packet['DNS']).encode('hex'))
128
		fr.write(pack('H', packet.qr))
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
		fr.write(pack('H', len(data)))
		fr.write(data)
		chop_and_write_packet(packet['DNS'])
	except IndexError:
		print 'non-DNS packet'
		total_length -= 1

fr.seek(0)
fp.seek(0)

fr.write(pack('L', total_length))
fp.write(pack('L', total_length))

print 'written ', total_length, 'packets'

fr.close()
fp.close()