Skip to content

Commit be8efc7

Browse files
committed
v0.6a
1 parent 1fd3514 commit be8efc7

File tree

3 files changed

+69
-34
lines changed

3 files changed

+69
-34
lines changed

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7-
## [0.6] - 2020-09-25
7+
## [0.6a] - 2020-09-27
88
### Added
9-
- Open UDP socket to prevent ICMP Destination unreachable (Port unreachable)
9+
- Open UDP socket to prevent ICMP Destination unreachable (Port unreachable) [0.6]
10+
- Add Match DHCP Message Type = Release [0.6a]
1011
### Fixed
11-
- Fix issiue with DHCP-Relay answers.
12+
- Fix issiue with DHCP-Relay answers. [0.6]
1213

1314
## [0.5] - 2020-08-07
1415
### Added

tftp/nb_import.yml

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,57 +5,62 @@ VM0020200000:
55
lease_time: 3600
66
mac: '50:00:20:20:00:00'
77
name_server: 8.8.8.8
8-
router: 10.240.43.254
8+
router: 10.240.40.1
99
subnet_mask: 255.255.252.0
10-
tftp_server_address: 10.240.40.254
10+
tftp_server_address: 192.168.254.91
1111
vendor_specific: 1:nb_VM0020200000.sh,3:tftp
1212
VM0021210000:
1313
domain: juniper.lab
1414
hostname: Spine-21
1515
ip: 10.240.40.21
1616
lease_time: 3600
17+
mac: '50:00:21:21:00:00'
1718
name_server: 8.8.8.8
18-
router: 10.240.43.254
19+
router: 10.240.40.1
1920
subnet_mask: 255.255.252.0
20-
tftp_server_address: 10.240.40.254
21+
tftp_server_address: 192.168.254.91
2122
vendor_specific: 1:nb_VM0021210000.sh,3:tftp
2223
VM0030300000:
2324
domain: juniper.lab
2425
hostname: Leaf-30
2526
ip: 10.240.40.30
2627
lease_time: 3600
2728
name_server: 8.8.8.8
28-
router: 10.240.43.254
29+
mac: '50:00:30:30:00:00'
30+
router: 10.240.40.1
2931
subnet_mask: 255.255.252.0
30-
tftp_server_address: 10.240.40.254
32+
tftp_server_address: 192.168.254.91
3133
vendor_specific: 1:nb_VM0030300000.sh,3:tftp
3234
VM0031310000:
3335
domain: juniper.lab
3436
hostname: Leaf-31
3537
ip: 10.240.40.31
3638
lease_time: 3600
3739
name_server: 8.8.8.8
38-
router: 10.240.43.254
40+
mac: '50:00:31:31:00:00'
41+
router: 10.240.40.1
3942
subnet_mask: 255.255.252.0
40-
tftp_server_address: 10.240.40.254
43+
tftp_server_address: 192.168.254.91
4144
vendor_specific: 1:nb_VM0031310000.sh,3:tftp
4245
VM0032320000:
4346
domain: juniper.lab
4447
hostname: Leaf-32
4548
ip: 10.240.40.32
4649
lease_time: 3600
4750
name_server: 8.8.8.8
48-
router: 10.240.43.254
51+
mac: '50:00:32:32:00:00'
52+
router: 10.240.40.1
4953
subnet_mask: 255.255.252.0
50-
tftp_server_address: 10.240.40.254
54+
tftp_server_address: 192.168.254.91
5155
vendor_specific: 1:nb_VM0032320000.sh,3:tftp
5256
VM0033330000:
5357
domain: juniper.lab
5458
hostname: Leaf-33
5559
ip: 10.240.40.33
5660
lease_time: 3600
5761
name_server: 8.8.8.8
58-
router: 10.240.43.254
62+
mac: '50:00:33:33:00:00'
63+
router: 10.240.40.1
5964
subnet_mask: 255.255.252.0
60-
tftp_server_address: 10.240.40.254
65+
tftp_server_address: 192.168.254.91
6166
vendor_specific: 1:nb_VM0033330000.sh,3:tftp

ztp.py

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
##########################################################
33
# ZTP (DHCP+TFTP+HTTP service)
44
# Created by: Zdolinski Artur
5-
# Version: 0.6 [20200925]
5+
# Version: 0.6a [20200927]
66
#
77
# if you need - you can disable cache (__pycache__)
88
# > bash# export PYTHONDONTWRITEBYTECODE=1
@@ -34,6 +34,14 @@
3434
os.environ['PYTHONUNBUFFERED'] = '1'
3535
conf.sniff_promisc=True
3636

37+
38+
# handle_dhcp_packet(packet)
39+
# get_option(dhcp_options, key)
40+
# threaded(fn)
41+
# handler(signal_received, frame)
42+
# chaddr_to_mac(chaddr)
43+
# op43(text_value)
44+
3745
def handle_dhcp_packet(packet):
3846
if DHCP in packet:
3947
# Write PCAP File if needed
@@ -52,7 +60,7 @@ def handle_dhcp_packet(packet):
5260
chaddr = packet[BOOTP].chaddr
5361
src_mac = packet[Ether].src
5462
dhcp_src_mac = chaddr_to_mac(chaddr)
55-
63+
5664
# Direction
5765
if packet[Ether].src == kwargs['my_mac']:
5866
direction = colored(kwargs['interface']+"| ->[Snd]", 'green')
@@ -74,7 +82,7 @@ def handle_dhcp_packet(packet):
7482

7583
# Match DHCP Message Type = Replay (2)
7684
elif DHCP_message_type == 2:
77-
subnet_mask = get_option(packet[DHCP].options, 'subnet_mask')
85+
#subnet_mask = get_option(packet[DHCP].options, 'subnet_mask')
7886
lease_time = get_option(packet[DHCP].options, 'lease_time')
7987
router = get_option(packet[DHCP].options, 'router')
8088
name_server = get_option(packet[DHCP].options, 'name_server')
@@ -102,7 +110,11 @@ def handle_dhcp_packet(packet):
102110
# Match DHCP Message Type = Ack (5)
103111
elif DHCP_message_type == 5:
104112
print(direction + colored('[Ack]['+str(hex(xid))+'] ', 'yellow') + "DHCP Server "+packet[IP].src+" ("+src_mac+") acked "+packet[BOOTP].yiaddr)
105-
113+
114+
# Match DHCP Message Type = Release (7)
115+
elif DHCP_message_type == 7:
116+
print(direction + colored('[Release]['+str(hex(xid))+'] ', 'red') +'DHCP Release from ('+dhcp_src_mac+') - IP: ' + str(packet[BOOTP].ciaddr) )
117+
106118
# Match DHCP Message Type = Inform (8)
107119
elif DHCP_message_type == 8:
108120
vendor_class_id = get_option(packet[DHCP].options, 'vendor_class_id')
@@ -155,6 +167,26 @@ def chaddr_to_mac(chaddr):
155167
mac_format_fix = ":".join(map("{0:0>2}".format, mac_format.split(':')))
156168
return str(mac_format_fix)
157169

170+
def op43(text_value):
171+
ret = b""
172+
xparam = text_value.replace(" ","").split(",")
173+
for param in xparam:
174+
p = param.split(":")
175+
try:
176+
p[1]
177+
except:
178+
return
179+
tag = int(p[0])
180+
value = p[1]
181+
ret += struct.pack("BB", tag, len(str(value))) + str(value).encode()
182+
ret += struct.pack("B", 255)
183+
return(ret)
184+
185+
# DhcpResponder
186+
# -> __init__(self)
187+
# -> get_parameters(self, path)
188+
# -> send_offer(self, packet, offer)
189+
# -> send_ack(self, packet, offer)
158190
class DhcpResponder(object):
159191
def __init__(self):
160192
pass
@@ -282,6 +314,11 @@ def send_ack(self, packet, offer):
282314
packet = ethernet / ip / udp / bootp / dhcp
283315
sendp(packet, iface=kwargs['interface'], verbose=False)
284316

317+
318+
# HttpServer(object)
319+
# -> __init__(self, port=80, **kwargs)
320+
# -> start(self)
321+
# -> stop(self)
285322
class HttpServer(object):
286323
def __init__(self, port=80, **kwargs):
287324
self.port = int(kwargs['port_http'])
@@ -315,6 +352,10 @@ def stop(self):
315352
self.httpd.shutdown()
316353
return
317354

355+
# TftpServer(object)
356+
# -> __init__(self, port=69, **kwargs)
357+
# -> stop(self)
358+
# -> start(self)
318359
class TftpServer(object):
319360
def __init__(self, port=69, **kwargs):
320361
self.port = int(kwargs['port_tftp'])
@@ -335,21 +376,9 @@ def start(self):
335376
except OSError:
336377
print (colored('[Warning] ', 'red') + 'TFTP '+str(self.my_ip)+':'+str(self.port)+' port in use')
337378

338-
def op43(text_value):
339-
ret = b""
340-
xparam = text_value.replace(" ","").split(",")
341-
for param in xparam:
342-
p = param.split(":")
343-
try:
344-
p[1]
345-
except:
346-
return
347-
tag = int(p[0])
348-
value = p[1]
349-
ret += struct.pack("BB", tag, len(str(value))) + str(value).encode()
350-
ret += struct.pack("B", 255)
351-
return(ret)
352-
379+
############
380+
### MAIN ###
381+
############
353382
if __name__ == "__main__":
354383
signal(SIGINT, handler)
355384
while True:

0 commit comments

Comments
 (0)