Package Position | Name of the value | Example | Representation | Byte | Explanation | |
one | Boot request | one | Hex | one | Message type 1 - request from client to server, 2 - response from server to client | |
2 | Hardware type | one | Hex | one | Type of hardware address in this protocol 1 - MAC | |
3 | Hardware adrees length | 6 | Hex | one | MAC address length | |
four | Hops | one | Hex | one | Number of intermediate routes | |
five | Transaction ID | 23: cf: de: 1d | Hex | four | The unique identifier of the transaction. Generates the client at the beginning of the request operation | |
7 | Second elapsed | 0 | Hex | four | Time in seconds since the beginning of the process of obtaining the address | |
9 | Bootp flags | 0 | Hex | 2 | Some flags that can be set, as an indication of the parameters of the protocol | |
eleven | Client IP address | 0.0.0.0 | Line | four | Client IP address (if any) | |
15 | Your client IP address | 0.0.0.0 | Line | four | IP address offered by the server (if any) | |
nineteen | Next server IP address | 0.0.0.0 | Line | four | Server IP (if known) | |
23 | Relay agent IP address | 172.16.114.41 | Line | four | IP address of relay agent (for example, switch) | |
27 | Client MAC address | 14: d6: 4d: a7: c9: 55 | Hex | 6 | MAC address of the sender of the packet (client) | |
31 | Client hardware address padding | Hex | ten | Reserved place. Usually filled with zeros | ||
41 | Server host name | Line | 64 | The name of the DHCP server. Normally not transmitted | ||
105 | Boot file name | Line | 128 | The file name on the server used by diskless stations when booting | ||
235 | Magic cookie | 63: 82: 53: 63 | Hex | four | The "magic" number by which including it can be determined that this packet belongs to the DHCP protocol | |
DHCP options. Can go in any order | ||||||
236 | Option number | 53 | Dec | one | Option 53 defining the type of DHCP packet 1 - DHCPDISCOVER 3 - DHCPREQUEST 2 - DHCPOFFER 5 - DHCPACK 8 - DHCPINFORM | |
Option length | one | Dec | one | |||
Option value | one | Dec | one | |||
Option number | 50 | Dec | one | What IP address does the client want to receive | ||
Option length | four | Dec | one | |||
Option value | 172.16.134.61 | Line | four | |||
Option number | 55 | one | The network parameters requested by the client. The composition may be different 01 - net mask 03 - Gateway 06 - DNS oc - Host Name 0f - network domain name 1c - address of the broadcast request (Broadcast) 42 - TFTP server name 79 - Classless Static Route | |||
Option length | eight | one | ||||
Option value | 01: 03: 06: 0c: 0f: 1c: 42: 79 | eight | ||||
Option number | 82 | Dec | Option 82, in which the MAC address of the repeater device and some additional values ​​are transmitted. Most often, the switch port on which the final DHCP client is running. This option is “nested” with additional parameters. The first byte is the “suboption” number, the second is its length, then its value. In this case, in option 82, the suboptions are nested: Agent Circuit ID = 00: 04: 00: 01: 00: 04, where the last two bytes are the DHCP client port from which the request came Agent Remote ID = 00: 06: c8: be: 19: 93: 11: 48 - the MAC address of the device of the DHCP relay | |||
Option length | 18 | Dec | ||||
Option value | 01:06 00: 04: 00: 01: 00: 04 02:08 00: 06: c8: be: 19: 93: 11: 48 | Hex | ||||
End of package | 255 | Dec | one | 255 symbolizes the end of the pack. |
Package Position | Name of value (common) | Example | Representation | Byte | Explanation |
one | Boot request | one | Hex | one | Message type 1 - request from client to server, 2 - response from server to client |
2 | Hardware type | one | Hex | one | Type of hardware address in this protocol 1 - MAC |
3 | Hardware adrees length | 6 | Hex | one | MAC address length |
four | Hops | one | Hex | one | Number of intermediate routes |
five | Transaction ID | 23: cf: de: 1d | Hex | four | The unique identifier of the transaction. Generates the client at the beginning of the request operation |
7 | Second elapsed | 0 | Hex | four | Time in seconds since the beginning of the process of obtaining the address |
9 | Bootp flags | 0 | Hex | 2 | Some flags that can be set, as an indication of the parameters of the protocol. In this case, 0 indicates the type of Unicast request. |
eleven | Client IP address | 0.0.0.0 | Line | four | Client IP address (if any) |
15 | Your client IP address | 172.16.134.61 | Line | four | IP address offered by the server (if any) |
nineteen | Next server IP address | 0.0.0.0 | Line | four | Server IP (if known) |
23 | Relay agent IP address | 172.16.114.41 | Line | four | IP address of relay agent (for example, switch) |
27 | Client MAC address | 14: d6: 4d: a7: c9: 55 | Hex | 6 | MAC address of the sender of the packet (client) |
31 | Client hardware address padding | Hex | ten | Reserved place. Usually filled with zeros | |
41 | Server host name | Line | 64 | The name of the DHCP server. Normally not transmitted | |
105 | Boot file name | Line | 128 | The file name on the server used by diskless stations when booting | |
235 | Magic cookie | 63: 82: 53: 63 | Hex | four | The "magic" number by which including it can be determined that this packet belongs to the DHCP protocol |
DHCP options. Can go in any order | |||||
236 | Option number | 53 | Dec | one | Option 53 defining DHCP 2 packet type - DHCPOFFER |
Option length | one | Dec | one | ||
Option value | 2 | Dec | one | ||
Option number | one | Dec | one | Option offering DHCP client network mask | |
Option length | four | Dec | one | ||
Option value | 255.255.224.0 | Line | four | ||
Option number | 3 | Dec | one | Option offering DHCP client default gateway | |
Option length | four | Dec | one | ||
Option value | 172.16.12.1 | Line | four | ||
Option number | 6 | Dec | one | Option offering DHCP client DNS | |
Option length | four | Dec | one | ||
Option value | 8.8.8.8 | Line | four | ||
Option number | 51 | Dec | one | The lifetime of the issued network parameters in seconds after which the DHCP client must request them again | |
Option length | four | Dec | one | ||
Option value | 86400 | Dec | four | ||
Option number | 82 | Dec | one | Option 82, repeats what came in DHCPDISCOVER | |
Option length | 18 | Dec | one | ||
Option value | 01: 08: 00: 06: 00 01: 01: 00: 00: 01 02: 06: 00: 03: 0f 26: 4d: ec | Dec | 18 | ||
End of package | 255 | Dec | one | 255 symbolizes the end of the pack. |
Package Position | Name of value (common) | Example | Representation | Byte | Explanation |
one | Boot request | one | Hex | one | Message type 1 - request from client to server, 2 - response from server to client |
2 | Hardware type | one | Hex | one | Type of hardware address in this protocol 1 - MAC |
3 | Hardware adrees length | 6 | Hex | one | MAC address length |
four | Hops | one | Hex | one | Number of intermediate routes |
five | Transaction ID | 23: cf: de: 1d | Hex | four | The unique identifier of the transaction. Generates the client at the beginning of the request operation |
7 | Second elapsed | 0 | Hex | four | Time in seconds since the beginning of the process of obtaining the address |
9 | Bootp flags | 8,000 | Hex | 2 | Some flags that can be set, as an indication of the protocol parameters. In this case, “Broadcast” is set. |
eleven | Client IP address | 0.0.0.0 | Line | four | Client IP address (if any) |
15 | Your client IP address | 172.16.134.61 | Line | four | IP address offered by the server (if any) |
nineteen | Next server IP address | 0.0.0.0 | Line | four | Server IP (if known) |
23 | Relay agent IP address | 172.16.114.41 | Line | four | IP address of relay agent (for example, switch) |
27 | Client MAC address | 14: d6: 4d: a7: c9: 55 | Hex | 6 | MAC address of the sender of the packet (client) |
31 | Client hardware address padding | Hex | ten | Reserved place. Usually filled with zeros | |
41 | Server host name | Line | 64 | The name of the DHCP server. Normally not transmitted | |
105 | Boot file name | Line | 128 | The file name on the server used by diskless stations when booting | |
235 | Magic cookie | 63: 82: 53: 63 | Hex | four | The "magic" number by which including it can be determined that this packet belongs to the DHCP protocol |
DHCP options. Can go in any order | |||||
236 | Option number | 53 | Dec | 3 | Option 53 defining DHCP 3 packet type - DHCPREQUEST |
Option length | one | Dec | one | ||
Option value | 3 | Dec | one | ||
Option number | 61 | Dec | one | Client ID: 01 (for Ehernet) + client MAC address | |
Option length | 7 | Dec | one | ||
Option value | 01: 2c: ab: 25: ff: 72: a6 | Hex | 7 | ||
Option number | 60 | Dec | "Vendor class identifier". In my case, it reports the DHCP client version. Perhaps other devices return something else. Windows for example reports MSFT 5.0 | ||
Option length | eleven | Dec | |||
Option value | udhcp 0.9.8 | Line | |||
Option number | 55 | one | The network parameters requested by the client. The composition may be different 01 - net mask 03 - Gateway 06 - DNS oc - Host Name 0f - network domain name 1c - address of the broadcast request (Broadcast) 42 - TFTP server name 79 - Classless Static Route | ||
Option length | eight | one | |||
Option value | 01: 03: 06: 0c: 0f: 1c: 42: 79 | eight | |||
Option number | 82 | Dec | one | Option 82, repeats what came in DHCPDISCOVER | |
Option length | 18 | Dec | one | ||
Option value | 01: 08: 00: 06: 00 01: 01: 00: 00: 01 02: 06: 00: 03: 0f 26: 4d: ec | Dec | 18 | ||
End of package | 255 | Dec | one | 255 symbolizes the end of the pack. |
Package Position | Name of value (common) | Example | Representation | Byte | Explanation |
one | Boot request | 2 | Hex | one | Message type 1 - request from client to server, 2 - response from server to client |
2 | Hardware type | one | Hex | one | Type of hardware address in this protocol 1 - MAC |
3 | Hardware adrees length | 6 | Hex | one | MAC address length |
four | Hops | one | Hex | one | Number of intermediate routes |
five | Transaction ID | 23: cf: de: 1d | Hex | four | The unique identifier of the transaction. Generates the client at the beginning of the request operation |
7 | Second elapsed | 0 | Hex | four | Time in seconds since the beginning of the process of obtaining the address |
9 | Bootp flags | 8,000 | Hex | 2 | Some flags that can be set, as an indication of the protocol parameters. In this case, “Broadcast” is set. |
eleven | Client IP address | 0.0.0.0 | Line | four | Client IP address (if any) |
15 | Your client IP address | 172.16.134.61 | Line | four | IP address offered by the server (if any) |
nineteen | Next server IP address | 0.0.0.0 | Line | four | Server IP (if known) |
23 | Relay agent IP address | 172.16.114.41 | Line | four | IP address of relay agent (for example, switch) |
27 | Client MAC address | 14: d6: 4d: a7: c9: 55 | Hex | 6 | MAC address of the sender of the packet (client) |
31 | Client hardware address padding | Hex | ten | Reserved place. Usually filled with zeros | |
41 | Server host name | Line | 64 | The name of the DHCP server. Normally not transmitted | |
105 | Boot file name | Line | 128 | The file name on the server used by diskless stations when booting | |
235 | Magic cookie | 63: 82: 53: 63 | Hex | four | The "magic" number by which including it can be determined that this packet belongs to the DHCP protocol |
DHCP options. Can go in any order | |||||
236 | Option number | 53 | Dec | 3 | Option 53 defining the type of DHCP packet 5 - DHCPACK |
Option length | one | Dec | one | ||
Option value | five | Dec | one | ||
Option number | one | Dec | one | Option offering DHCP client network mask | |
Option length | four | Dec | one | ||
Option value | 255.255.224.0 | Line | four | ||
Option number | 3 | Dec | one | Option offering DHCP client default gateway | |
Option length | four | Dec | one | ||
Option value | 172.16.12.1 | Line | four | ||
Option number | 6 | Dec | one | Option offering DHCP client DNS | |
Option length | four | Dec | one | ||
Option value | 8.8.8.8 | Line | four | ||
Option number | 51 | Dec | one | The lifetime of the issued network parameters in seconds after which the DHCP client must request them again | |
Option length | four | Dec | one | ||
Option value | 86400 | Dec | four | ||
Option number | 82 | Dec | one | Option 82, repeats what came in DHCPDISCOVER | |
Option length | 18 | Dec | one | ||
Option value | 01: 08: 00: 06: 00 01: 01: 00: 00: 01 02: 06: 00: 03: 0f 26: 4d: ec | Dec | 18 | ||
End of package | 255 | Dec | one | 255 symbolizes the end of the pack. |
pkg install python3 python3 -m ensurepip pip3 install mysql-connector
sudo apt-get install python3 sudo apt-get install pip3 sudo pip3 install mysql-connector
<? xml version = "1.0"?> <config> <dhcpserver> <host> 0.0.0.0 </ host> <broadcast> 255.255.255.255 </ broadcast> <DHCPServer> 192.168.0.71 </ DHCPServer> <LeaseTime> 8600 </ LeaseTime> <ThreadLimit> 1 </ ThreadLimit> <defaultMask> 255.255.255.0 </ defaultMask> <defaultRouter> 192.168.0.1 </ defaultRouter> <defaultDNS> 8.8.8.8 </ defaultDNS> </ dhcpserver> <mysql> <host> localhost </ host> <username> test </ username> <password> test </ password> <basename> pydhcp </ basename> </ mysql> <options> <option> option_82_hex: sw_port1: 20: 22 </ option> <option> option_82_hex: sw_port2: 16: 18 </ option> <option> option_82_hex: sw_mac: 26: 40 </ option> </ options> <query> <offer_count> 3 </ offer_count> <offer_1> select ip, mask, router, dns from users where upper (mac) = upper ('{option_82_AgentRemoteId_hex}') and upper (port) = upper ('{option_82_AgentCircuitId_port_hex}') </ offer_1> <offer_2> select ip, mask, router, dns from users where upper (mac) = upper ('{sw_mac}') and upper (port) = upper ('{sw_port2}') </ offer_2> <offer_3> select ip, mask, router, dns from users where upper (mac) = upper ('{ClientMacAddress}') </ offer_3> <history_sql> insert into history (id, dt, mac, ip, comment) values ​​(null, now (), '{ClientMacAddress}', '{RequestedIpAddress}', 'DHCPACK / INFORM') </ history_sql> </ query> </ config>
option_82_hex:sw_port1:20:22
option_82_hex:sw_mac:26:40
- the DHCPINFORM package came to port 67, from 0025224ad764, b '\ x91 \ xa5 \ xe0 \ xa3 \ xa5 \ xa9- \ x8f \ x8a', ('172.30.114.25', 68) {'ClientMacAddress': '0025224ad764', 'ClientMacAddressByte': b '\ x00%' J \ xd7d ', 'HType': 'Ethernet', 'HostName': b '\ x91 \ xa5 \ xe0 \ xa3 \ xa5 \ xa9- \ x8f \ x8a', 'ReqListDNS': True, 'ReqListDomainName': True, 'ReqListPerfowmRouterDiscover': True, 'ReqListRouter': True, 'ReqListStaticRoute': True, 'ReqListSubnetMask': True, 'ReqListVendorSpecInfo': 43, 'RequestedIpAddress': '0.0.0.0', 'Vendor': b'MSFT 5.0 ', 'chaddr': '0025224ad764', 'ciaddr': '172.30.128.13', 'flags': b '\ x00 \ x00', 'giaddr': '172.30.114.25', 'gpoz': 308, 'hlen': 6, 'hops': 1, 'htype': 'MAC', 'magic_cookie': b'c \ x82Sc ', 'op': 'DHCPINFORM', 'option12': 12, 'option53': 53, 'option55': 55, 'option60': 60, 'option61': 61, 'option82': 82, 'option_82_byte': b '\ x12 \ x01 \ x06 \ x00 \ x04 \ x00 \ x01 \ x00 \ x06 \ x02 \ x08 \ x00' b '\ x06 \ x00 \ x1eX \ x9e \ xb2 \ xad', 'option_82_hex': '12010600040001000602080006001e589eb2ad', 'option_82_len': 18, 'option_82_str': "b '\\ x12 \\ x01 \\ x06 \\ x00 \\ x04 \\ x00 \\ x01 \\ x00 \\ x06 \\ x02 \\ x08 \\ x00 \\ x06 \\ x00 \ \ x1eX \\ x9e \\ xb2 \\ xad '", 'result': False, 'secs': 768, 'siaddr': '0.0.0.0', 'sw_mac': '001e589eb2ad', 'sw_port1': '06', 'xidbyte': b '<\ x89} \ x8c', 'xidhex': '3c897d8c', 'yiaddr': '0.0.0.0'}
tree = minidom.parse (gconfig ["config_file"]) mconfig = tree.getElementsByTagName ("mysql") for elem in mconfig: gconfig ["mysql_host"] = elem.getElementsByTagName ("host") [0] .firstChild.data gconfig ["mysql_username"] = elem.getElementsByTagName ("username") [0] .firstChild.data gconfig ["mysql_password"] = elem.getElementsByTagName ("password") [0] .firstChild.data gconfig ["mysql_basename"] = elem.getElementsByTagName ("basename") [0] .firstChild.data dconfig = tree.getElementsByTagName ("dhcpserver") for elem in dconfig: gconfig ["broadcast"] = elem.getElementsByTagName ("broadcast") [0] .firstChild.data gconfig ["dhcp_host"] = elem.getElementsByTagName ("host") [0] .firstChild.data gconfig ["dhcp_LeaseTime"] = elem.getElementsByTagName ("LeaseTime") [0] .firstChild.data gconfig ["dhcp_ThreadLimit"] = int (elem.getElementsByTagName ("ThreadLimit") [0] .firstChild.data) gconfig ["dhcp_Server"] = elem.getElementsByTagName ("DHCPServer") [0] .firstChild.data gconfig ["dhcp_defaultMask"] = elem.getElementsByTagName ("defaultMask") [0] .firstChild.data gconfig ["dhcp_defaultRouter"] = elem.getElementsByTagName ("defaultRouter") [0] .firstChild.data gconfig ["dhcp_defaultDNS"] = elem.getElementsByTagName ("defaultDNS") [0] .firstChild.data qconfig = tree.getElementsByTagName ("query") for elem in qconfig: gconfig ["offer_count"] = elem.getElementsByTagName ("offer_count") [0] .firstChild.data for num in range (int (gconfig ["offer_count"])): gconfig ["offer _" + str (num + 1)] = elem.getElementsByTagName ("offer _" + str (num + 1)) [0] .firstChild.data gconfig ["history_sql"] = elem.getElementsByTagName ("history_sql") [0] .firstChild.data options = tree.getElementsByTagName ("options") for elem in options: node = elem.getElementsByTagName ("option") for options in node: optionsMod.append (options.firstChild.data)
def PacketWork (data, addr): ... # implementation of parsing the incoming packet, and the answer to it ... while true: data, addr = udp_socket.recvfrom (1024) # waiting for the UDP packet thread = threading. Thread (target = PacketWork, args = (data, addr,)). start () # how come - run in the background the previously defined PacketWork function with parameters while threading.active_count ()> gconfig ["dhcp_ThreadLimit"]: time.sleep (1) # if the number of threads already running is greater than in the settings, wait until they become fewer
udp_socket = socket.socket (socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) udp_socket.bind ((gconfig ["dhcp_host"], 67))
udp_socket.setsockopt (socket.SOL_SOCKET, socket.SO_BROADCAST, 1) # switch the socket to send broadcast mode rz = udp_socket.sendto (packetack, (gconfig ["broadcast"], 68))
udp_socket.setsockopt (socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # switch the socket to the "many listeners" mode rz = udp_socket.sendto (packetack, addr)
res ["xidhex"] = data [4: 8] .hex () res ["xidbyte"] = data [4: 8]
res ["flags"] = pack ('BB', data [10], data [11])
res ["ciaddr"] = socket.inet_ntoa (pack ('BBBB', data [12], data [13], data [14], data [15]));
res = res + socket.inet_pton (socket.AF_INET, gconfig ["dhcp_Server"])
Source: https://habr.com/ru/post/445338/
All Articles