-
Notifications
You must be signed in to change notification settings - Fork 0
/
dhcp.py
242 lines (216 loc) · 9.26 KB
/
dhcp.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import socket
import sys
import struct
class DecodeError(Exception):
pass
class NotImplemented(Exception):
pass
class packet_base:
def __init__(self, packet = None):
self.file_overload = False
self.sname_overload = False
self.message_type = 2 #1 byte (2 for boot reply, 1 for boot request)
self.hardware_type = 1 #1 byte
self.hardware_address_len = 6 # 1 byte
self.hops = 0 # 1 byte
self.transaction_id = 0 # 4 bytes will have to take from client
self.seconds_elapsed = 0 # 2 bytes, may have to set?
self.bootp_flags = 0 # 2 bytes, 0=unicast
self.client_ip = 0 # 4 bytes, ip should just be set to whatever the client is asking with.
self.your_client_ip = 0 # 4 bytes, ip I think this is what I'm offering?
self.next_serve = 0 # 4 bytes, ip, leave blank, might be related to bootp
self.relay_agent = 0 # 4 bytes, ip, leave blank I think related to dhcp relays
self.client_mac = 0 # 6 bytes, mac address grab from client
self.hardware_padding = 0 # 10 bytes, this and the above should be 16 bytes
self.server_host_name = 0 # 64 bytes
self.file = 0 # 128 bytes, will not be used
self.magic_cookie = 0x63825363 # 4 bytes, needs to be set to 63 82 53 63
self.options = [] # format for options will be (type, length, option/data), with a length of byte, byte, variable len
# be able to receive options of at least 312 bytes, might be some weird padding to align to work boundaries
if not packet is None:
self.decode(packet)
def decode(self, packet):
self.magic_cookie = packet[236:240].hex()
if self.magic_cookie == '63825363':
try:
format = '>BBBB'
(self.message_type,
self.hardware_type,
self.hardware_address_len,
self.hops) = struct.unpack(format, packet[:4])
self.transaction_id = packet[4:8].hex()
self.seconds_elapsed = struct.unpack('>H', packet[8:10])[0]
self.bootp_flags = packet[10:12].hex()
self.client_ip = socket.inet_ntoa(packet[12:16])
self.your_client_ip = socket.inet_ntoa(packet[16:20])
self.next_serve = socket.inet_ntoa(packet[20:24])
self.relay_agent = socket.inet_ntoa(packet[24:28])
self.client_mac = packet[28:34].hex()
self.hardware_padding = packet[34:44].hex()
self.decodeoptions(packet[240:len(packet)])
for option in self.options:
if option[0] == 51:
if option[2] == 1 or option[2] == 3:
self.decodeoptions(packet[108:236])
self.file_overload = True
if option[2] == 2 or option[2] == 3:
self.decodeoptions(packet[44:108])
self.sname_overload = True
break
if not self.file_overload:
self.file = packet[108:236]
if not self.sname_overload:
self.server_host_name = struct.unpack('64s', packet[44:108])[0]
except Exception as e:
raise DecodeError('This is not a decodeable DHCP packet')
else:
raise DecodeError('This is not a decodeable DHCP packet')
def encode(self):
data = struct.pack('>BBBB', self.message_type,
self.hardware_type,
self.hardware_address_len,
self.hops)
data+= bytearray.fromhex(self.transaction_id)
data+= struct.pack('>H', self.seconds_elapsed)
data+= bytearray.fromhex(self.bootp_flags)
data+= socket.inet_aton(self.client_ip)
data+= socket.inet_aton(self.your_client_ip)
data+= socket.inet_aton(self.next_serve)
data+= socket.inet_aton(self.relay_agent)
data+= bytearray.fromhex(self.client_mac)
data+= bytearray.fromhex(self.hardware_padding)
data+= self.server_host_name
data+= self.file
data+= self.magic_cookie
options = self.encodeoptions()
if not options == None:
data+= self.encodeoptions()
if self.file_overload or self.sname_overload:
raise NotImplemented('Feature not implemented')
return data
def encodeoptions(self):
data = b''
for option in self.options:
data += struct.pack('>BB', option[0], option[1])
data += option[2]
return data
def decodeoptions(self, data):
options = []
counter = 0
try:
while counter <= len(data):
opt_type = struct.unpack('B', data[counter:counter+1])[0]
if opt_type == 0:
counter += 1
continue
if opt_type == 255:
options.append((255, 0, b''))
break
counter += 1
length = struct.unpack('B', data[counter:counter+1])[0]
counter += 1
opt_data = data[counter:counter+length]
counter += length
options.append((opt_type, length, opt_data))
except Exception as e:
print(e)
print(f'something is screwy with option decoding: {data}')
if self.options is not None:
self.options += options
else:
self.options = options
def printpacket(self):
print(f'message_type: {self.message_type}')
print(f'hardware_type: {self.hardware_type}')
print(f'hardware_address_len: {self.hardware_address_len}')
print(f'hops: {self.hops}')
print(f'transaction_id: {self.transaction_id}')
print(f'seconds_elapsed: {self.seconds_elapsed}')
print(f'bootp_flags: {self.bootp_flags}')
print(f'client_ip: {self.client_ip}')
print(f'your_client_ip: {self.your_client_ip}')
print(f'next_serve: {self.next_serve}')
print(f'relay_agent: {self.relay_agent}')
print(f'client_mac: {self.client_mac}')
print(f'hardware_padding: {self.hardware_padding}')
print(f'server_host_name: {self.server_host_name}')
print(f'options: {self.options}')
print(f'file_overload: {self.file_overload}')
print(f'sname_overload: {self.sname_overload}')
print(f'file: {self.file}')
print(f'magic_cookie: {self.magic_cookie}')
def build(self, message_type, transaction_id, offering_ip, client_mac, seconds_elapsed=0,
bootp_flags='0000', client_ip="0.0.0.0", next_serve="0.0.0.0",
relay_agent="0.0.0.0", file=None, server_host_name=None, options=[],
file_overload=False, sname_overload=False):
self.file_overload = file_overload
self.sname_overload = sname_overload
self.message_type = message_type
self.transaction_id = transaction_id # 4 bytes will have to take from client
self.seconds_elapsed = seconds_elapsed # 2 bytes, may have to set?
self.bootp_flags = bootp_flags # 2 bytes, 0x0000 is unicast, 0x8000 is broadcast
self.client_ip = client_ip # 4 bytes, ip should just be set to whatever address the client talks to me with
self.your_client_ip = offering_ip # 4 bytes, ip I think this is what I'm offering?
self.next_serve = next_serve # 4 bytes, ip, leave blank, might be related to bootp
self.relay_agent = relay_agent # 4 bytes, ip, leave blank I think related to dhcp relays
self.client_mac = client_mac # 6 bytes, mac address grab from client
self.hardware_padding = '00000000000000000000' # 10 bytes, this and the above should be 16 bytes
if server_host_name == None:
self.server_host_name = b'\x00' * 64
else:
self.server_host_name = server_host_name # 64 bytes
if file == None: # 128 bytes, will not be used
self.file = b'\x00' * 128
else:
self.file = file
self.magic_cookie = b'\x63\x82\x53\x63' # 4 bytes, needs to be set to 63 82 53 63
self.options = options # format for options will be (type, length, option/data), with a length of byte, byte, variable len
# be able to receive options of at least 312 bytes, might be some weird padding to align to work boundaries
main_data = self.encode()
main_data += self.encodeoptions()
class dhcp_offer(packet_base):
def __init__(self):
super().__init__()
def build_from_packet(self, packet, ip, lease_time, netmask=None, router=None, dns=None, server_ip=None):
tar_packet = packet_base(packet=packet)
options = []
options.append((53, 1, struct.pack('>B', 2))) #type will be dhcp offer
if server_ip:
options.append((54, 4, socket.inet_aton(server_ip)))
options.append((51, 4, struct.pack('>I',lease_time)))
if netmask:
options.append((1, 4, socket.inet_aton(netmask)))
if router:
options.append((3, 4, socket.inet_aton(router)))
if dns:
options.append((6, 4, socket.inet_aton(dns)))
options.append((255, 0, b''))
return self.build(2, tar_packet.transaction_id, ip, tar_packet.client_mac, tar_packet.seconds_elapsed, tar_packet.bootp_flags, options=options)
class dhcp_ack(packet_base):
def __init__(self):
super().__init__()
def build_from_packet(self, packet, ip, lease_time, netmask=None, router=None, dns=None, server_ip=None):
tar_packet = packet_base(packet=packet)
options = []
options.append((53, 1, struct.pack('>B', 5))) #type will be dhcp ack
if server_ip:
options.append((54, 4, socket.inet_aton(server_ip)))
options.append((51, 4, struct.pack('>I',lease_time)))
if netmask:
options.append((1, 4, socket.inet_aton(netmask)))
if router:
options.append((3, 4, socket.inet_aton(router)))
if dns:
options.append((6, 4, socket.inet_aton(dns)))
options.append((255, 0, b''))
return self.build(2, tar_packet.transaction_id, ip, tar_packet.client_mac, tar_packet.seconds_elapsed, tar_packet.bootp_flags, options=options)
class dhcp_nack(packet_base):
def __init__(self):
super().__init__()
def build_for_host(self, mac, transaction_id, server_ip=None):
options = []
options.append((53, 1, struct.pack('>B', 6))) #type will be dhcp nck
if server_ip:
options.append((54, 4, socket.inet_aton(server_ip)))
options.append((255, 0, b''))
return self.build(2, transaction_id, '0.0.0.0', mac, options=options)