-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdns_packet.py
168 lines (152 loc) · 4.73 KB
/
dns_packet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import struct
from enum import Enum
class DNSException(BaseException):
"""
Any exceptions cause by this class
"""
pass
HEADER_FORMAT = "!HHHHHH"
ID_FORMAT = "!H"
class DNSPacket:
def __init__(self, data):
"""
Fields of DNS packet according to RFC 1035
"""
self.data = data
self.HEADER = None
self.QNAME = None
self.len_name = None
self.QTYPE = None
self.QCLASS = None
self.ATYPE = None
self.ACLASS = None
self.QR = None
try:
self.parse_header()
self.parse_qname()
self.parse_packet_type()
self.parse_type()
self.parse_class()
except Exception as e:
raise DNSException("Smth wrong with packet header")
def parse_packet_type(self):
"""
Parse QR flag
"""
flags = struct.unpack("!BB", self.data[2:4])
flag = flags[0] >> 7 # Get the first QR flag
self.QR = flag
def parse_header(self):
"""
Parse data from dns header
"""
self.HEADER = struct.unpack(HEADER_FORMAT, self.data[0:12])
def parse_qname(self):
"""
Parse QNAME from dns question part of packet
Make an assumption that we have only 1 question
"""
name_len = 0
for b in self.data[12:]:
name_len += 1
if b == 0 or b == struct.pack("B", 0):
break
name = self.data[12:12 + name_len]
self.len_name = name_len
self.QNAME = struct.unpack(str(self.len_name) + "s", name)
def add_id(self, p_id):
"""
Add id before main data
:param p_id: DNS packet id
"""
reply_id = struct.pack(ID_FORMAT, p_id)
self.data = reply_id + self.data[2:]
def parse_type(self):
"""
Parse type of dns question or dns answer
0 - QUESTION or 1 - ANSWER
"""
# class DNSTypes(Enum):
# A = 1
# NS = 2
# MD = 3
# MF = 4
# CNAME = 5
# SOA = 6
# MB = 7
# MG = 8
# MR = 9
# NULL = 10
# WKS = 11
# PTR = 12
# HINFO = 13
# MINFO = 14
# MX = 15
# TXT = 16
types = {
1: "A",
2: "NS",
3: "MD",
4: "MF",
5: "CNAME",
6: "SOA",
7: "MB",
8: "MG",
9: "MR",
10: "NULL",
11: "WKS",
12: "PTR",
13: "HINFO",
14: "MINFO",
15: "MX",
16: "TXT"
}
if self.QR == 0:
type_of_question = struct.unpack("!H", self.data[12 + self.len_name:12 + self.len_name + 2])[0]
self.QTYPE = types[type_of_question]
elif self.QR == 1:
type_of_answer = struct.unpack("!H", self.data[12 + self.len_name + 4 + 2:12 + self.len_name + 4 + 4])[0]
self.ATYPE = types[type_of_answer]
else:
raise DNSException("Invalid QR - either Question or ANSWER")
def parse_class(self):
"""
Parse class of dns question or answer
0 - QUESTION or 1 - ANSWER
"""
classes = {
1: "IN",
2: "CS",
3: "CH",
4: "HS"
}
if self.QR == 0:
class_of_question = struct.unpack("!H", self.data[12 + self.len_name + 2:12 + self.len_name + 4])[0]
self.QCLASS = classes[class_of_question]
elif self.QR == 1:
class_of_answer = struct.unpack("!H", self.data[12 + self.len_name + 4 + 4:12 + self.len_name + 4 + 6])[0]
self.ACLASS = classes[class_of_answer]
else:
raise DNSException("Invalid QR - either Question or ANSWER")
def get_ttl(self):
"""
Get answer TTL
"""
begin = 12 + self.len_name + 4 + 6
end = 12 + self.len_name + 4 + 10
return struct.unpack("!I", self.data[begin:end])[0]
def get_rdata_len(self, begin, end):
"""
Get the length of data in response (RDATA)
"""
return struct.unpack("!H", self.data[begin:end])[0]
def set_ttl(self, cache_time, cache_ttl, curr_time):
begin = 12 + self.len_name + 4 + 10
end = 12 + self.len_name + 4 + 12
# For every answer set ttl
for answer in range(self.HEADER[3]):
rdata_len = self.get_rdata_len(begin, end)
new_ttl = struct.pack("!I", int(cache_ttl - curr_time + cache_time))
self.data = self.data[0:begin - 4] + new_ttl + self.data[end - 2:]
begin += rdata_len + 12
end += rdata_len + 12