Skip to content

Commit

Permalink
v0.6a
Browse files Browse the repository at this point in the history
  • Loading branch information
azdolinski committed Sep 27, 2020
1 parent 1fd3514 commit be8efc7
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 34 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

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

## [0.5] - 2020-08-07
### Added
Expand Down
29 changes: 17 additions & 12 deletions tftp/nb_import.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,62 @@ VM0020200000:
lease_time: 3600
mac: '50:00:20:20:00:00'
name_server: 8.8.8.8
router: 10.240.43.254
router: 10.240.40.1
subnet_mask: 255.255.252.0
tftp_server_address: 10.240.40.254
tftp_server_address: 192.168.254.91
vendor_specific: 1:nb_VM0020200000.sh,3:tftp
VM0021210000:
domain: juniper.lab
hostname: Spine-21
ip: 10.240.40.21
lease_time: 3600
mac: '50:00:21:21:00:00'
name_server: 8.8.8.8
router: 10.240.43.254
router: 10.240.40.1
subnet_mask: 255.255.252.0
tftp_server_address: 10.240.40.254
tftp_server_address: 192.168.254.91
vendor_specific: 1:nb_VM0021210000.sh,3:tftp
VM0030300000:
domain: juniper.lab
hostname: Leaf-30
ip: 10.240.40.30
lease_time: 3600
name_server: 8.8.8.8
router: 10.240.43.254
mac: '50:00:30:30:00:00'
router: 10.240.40.1
subnet_mask: 255.255.252.0
tftp_server_address: 10.240.40.254
tftp_server_address: 192.168.254.91
vendor_specific: 1:nb_VM0030300000.sh,3:tftp
VM0031310000:
domain: juniper.lab
hostname: Leaf-31
ip: 10.240.40.31
lease_time: 3600
name_server: 8.8.8.8
router: 10.240.43.254
mac: '50:00:31:31:00:00'
router: 10.240.40.1
subnet_mask: 255.255.252.0
tftp_server_address: 10.240.40.254
tftp_server_address: 192.168.254.91
vendor_specific: 1:nb_VM0031310000.sh,3:tftp
VM0032320000:
domain: juniper.lab
hostname: Leaf-32
ip: 10.240.40.32
lease_time: 3600
name_server: 8.8.8.8
router: 10.240.43.254
mac: '50:00:32:32:00:00'
router: 10.240.40.1
subnet_mask: 255.255.252.0
tftp_server_address: 10.240.40.254
tftp_server_address: 192.168.254.91
vendor_specific: 1:nb_VM0032320000.sh,3:tftp
VM0033330000:
domain: juniper.lab
hostname: Leaf-33
ip: 10.240.40.33
lease_time: 3600
name_server: 8.8.8.8
router: 10.240.43.254
mac: '50:00:33:33:00:00'
router: 10.240.40.1
subnet_mask: 255.255.252.0
tftp_server_address: 10.240.40.254
tftp_server_address: 192.168.254.91
vendor_specific: 1:nb_VM0033330000.sh,3:tftp
67 changes: 48 additions & 19 deletions ztp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
##########################################################
# ZTP (DHCP+TFTP+HTTP service)
# Created by: Zdolinski Artur
# Version: 0.6 [20200925]
# Version: 0.6a [20200927]
#
# if you need - you can disable cache (__pycache__)
# > bash# export PYTHONDONTWRITEBYTECODE=1
Expand Down Expand Up @@ -34,6 +34,14 @@
os.environ['PYTHONUNBUFFERED'] = '1'
conf.sniff_promisc=True


# handle_dhcp_packet(packet)
# get_option(dhcp_options, key)
# threaded(fn)
# handler(signal_received, frame)
# chaddr_to_mac(chaddr)
# op43(text_value)

def handle_dhcp_packet(packet):
if DHCP in packet:
# Write PCAP File if needed
Expand All @@ -52,7 +60,7 @@ def handle_dhcp_packet(packet):
chaddr = packet[BOOTP].chaddr
src_mac = packet[Ether].src
dhcp_src_mac = chaddr_to_mac(chaddr)

# Direction
if packet[Ether].src == kwargs['my_mac']:
direction = colored(kwargs['interface']+"| ->[Snd]", 'green')
Expand All @@ -74,7 +82,7 @@ def handle_dhcp_packet(packet):

# Match DHCP Message Type = Replay (2)
elif DHCP_message_type == 2:
subnet_mask = get_option(packet[DHCP].options, 'subnet_mask')
#subnet_mask = get_option(packet[DHCP].options, 'subnet_mask')
lease_time = get_option(packet[DHCP].options, 'lease_time')
router = get_option(packet[DHCP].options, 'router')
name_server = get_option(packet[DHCP].options, 'name_server')
Expand Down Expand Up @@ -102,7 +110,11 @@ def handle_dhcp_packet(packet):
# Match DHCP Message Type = Ack (5)
elif DHCP_message_type == 5:
print(direction + colored('[Ack]['+str(hex(xid))+'] ', 'yellow') + "DHCP Server "+packet[IP].src+" ("+src_mac+") acked "+packet[BOOTP].yiaddr)


# Match DHCP Message Type = Release (7)
elif DHCP_message_type == 7:
print(direction + colored('[Release]['+str(hex(xid))+'] ', 'red') +'DHCP Release from ('+dhcp_src_mac+') - IP: ' + str(packet[BOOTP].ciaddr) )

# Match DHCP Message Type = Inform (8)
elif DHCP_message_type == 8:
vendor_class_id = get_option(packet[DHCP].options, 'vendor_class_id')
Expand Down Expand Up @@ -155,6 +167,26 @@ def chaddr_to_mac(chaddr):
mac_format_fix = ":".join(map("{0:0>2}".format, mac_format.split(':')))
return str(mac_format_fix)

def op43(text_value):
ret = b""
xparam = text_value.replace(" ","").split(",")
for param in xparam:
p = param.split(":")
try:
p[1]
except:
return
tag = int(p[0])
value = p[1]
ret += struct.pack("BB", tag, len(str(value))) + str(value).encode()
ret += struct.pack("B", 255)
return(ret)

# DhcpResponder
# -> __init__(self)
# -> get_parameters(self, path)
# -> send_offer(self, packet, offer)
# -> send_ack(self, packet, offer)
class DhcpResponder(object):
def __init__(self):
pass
Expand Down Expand Up @@ -282,6 +314,11 @@ def send_ack(self, packet, offer):
packet = ethernet / ip / udp / bootp / dhcp
sendp(packet, iface=kwargs['interface'], verbose=False)


# HttpServer(object)
# -> __init__(self, port=80, **kwargs)
# -> start(self)
# -> stop(self)
class HttpServer(object):
def __init__(self, port=80, **kwargs):
self.port = int(kwargs['port_http'])
Expand Down Expand Up @@ -315,6 +352,10 @@ def stop(self):
self.httpd.shutdown()
return

# TftpServer(object)
# -> __init__(self, port=69, **kwargs)
# -> stop(self)
# -> start(self)
class TftpServer(object):
def __init__(self, port=69, **kwargs):
self.port = int(kwargs['port_tftp'])
Expand All @@ -335,21 +376,9 @@ def start(self):
except OSError:
print (colored('[Warning] ', 'red') + 'TFTP '+str(self.my_ip)+':'+str(self.port)+' port in use')

def op43(text_value):
ret = b""
xparam = text_value.replace(" ","").split(",")
for param in xparam:
p = param.split(":")
try:
p[1]
except:
return
tag = int(p[0])
value = p[1]
ret += struct.pack("BB", tag, len(str(value))) + str(value).encode()
ret += struct.pack("B", 255)
return(ret)

############
### MAIN ###
############
if __name__ == "__main__":
signal(SIGINT, handler)
while True:
Expand Down

0 comments on commit be8efc7

Please sign in to comment.