dhcp server including dhcp discover,offer,request,ack problem

Question:

this is my dhcp server:

import socket
from scapy.all import *
from scapy.layers.dhcp import BOOTP, DHCP
from scapy.layers.inet import UDP, IP
from scapy.layers.l2 import Ether


def dhcp_server():
    # Server parameters
    server_ips = ['127.0.0.1']
    server_port = 67

    # Create a UDP socket for DHCP communication
    dhcp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    dhcp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    dhcp_socket.bind(('127.0.0.1', server_port))
    print(f"DHCP server listening on port {server_port}")

    # Loop to handle incoming DHCP requests
    while True:
        # Receive a DHCP request from a client
        dhcp_request, client_address = dhcp_socket.recvfrom(1024)
        print(f"Received DHCP request from {client_address[0]}:{client_address[1]}")

        # Parse the DHCP request packet
        dhcp_packet = DHCP(dhcp_request)
        dhcp_options = dhcp_packet[DHCP].options
        if not dhcp_options:
            print(f"Invalid DHCP request from {client_address[0]}:{client_address[1]}")
            continue
        dhcp_message_type = [opt[1] for opt in dhcp_options if opt[0] == 'message-type'][0]
        client_mac_address = dhcp_packet[Ether].src

        # Assign an IP address to the client
        if dhcp_message_type == 1:  # DHCP Discover
            # Create a DHCP offer packet
            dhcp_offer = Ether(src=get_if_hwaddr('eth0'), dst=client_mac_address) / IP(src=server_ips[0],
                                                                                       dst='255.255.255.255') / UDP(
                sport=67, dport=68) / BOOTP(op=2, yiaddr='0.0.0.0', siaddr=server_ips[0],
                                            chaddr=client_mac_address) / DHCP(
                options=[('message-type', 'offer'), ('subnet_mask', '255.255.255.0'), ('domain_name_server', '8.8.8.8'),
                         ('router', server_ips[0]), ('lease_time', 120)])

            # Send the DHCP offer packet to the client
            sendp(dhcp_offer, iface='eth0', verbose=0)
            print(f"Sent DHCP offer to {client_address[0]}:{client_address[1]}")

        elif dhcp_message_type == 3:  # DHCP Request
            # Create a DHCP acknowledgement packet
            dhcp_ack = Ether(src=get_if_hwaddr('eth0'), dst=client_mac_address) / IP(src=server_ips[0],
                                                                                     dst='255.255.255.255') / UDP(
                sport=67, dport=68) / BOOTP(op=2, yiaddr=dhcp_packet[BOOTP].yiaddr, siaddr=server_ips[0],
                                            chaddr=client_mac_address) / DHCP(
                options=[('message-type', 'ack'), ('subnet_mask', '255.255.255.0'), ('domain_name_server', '8.8.8.8'),
                         ('router', server_ips[0]), ('lease_time', 120)])

            # Send the DHCP acknowledgement packet to the client
            sendp(dhcp_ack, iface='eth0', verbose=0)
            print(f"Sent DHCP acknowledgement to {client_address[0]}:{client_address[1]}")


if __name__ == "__main__":
    dhcp_server()

my dhcp server listens to incomming requests and respond to them accordingly.

this is my client:

import socket
import struct

# Client parameters
server_ip = '127.0.0.1'
server_port = 67

# Create a UDP socket for DHCP communication
dhcp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
dhcp_socket.bind(('0.0.0.0', 68))  # Use port 68 for DHCP client

# Construct the DHCP request message
# See https://datatracker.ietf.org/doc/html/rfc2131#section-2
# for the format of a DHCP request message
transaction_id = b'x39x03xF3x26'  # 4-byte transaction ID
flags = b'x00x01'  # Broadcast flag (bit 15) set to 1
client_hw_address = b'x08x00x27x01x9CxEA'  # Example MAC address
client_ip_address = b'x00x00x00x00'  # Requesting a new IP address
server_ip_address = b'x00x00x00x00'  # Server IP address is initially 0
relay_agent_ip_address = b'x00x00x00x00'  # No relay agent in this case
client_mac_padding = b'x00x00x00x00x00x00x00x00x00x00'
dhcp_options = b'x63x82x53x63'  # Magic cookie + DHCP options
dhcp_options += b'x35x01x01'  # DHCP Message Type option: DHCPREQUEST
dhcp_options += b'x32x04xc0xa8x01x64'  # Requested IP address option
dhcp_options += b'x0fx06x00x00x3cx50x4bx05xdc'  # Hostname option
dhcp_options += b'x0cx0cx70x79x74x68x6fx6ex2dx62x6fx6fx74'  # Hostname option
dhcp_options += b'x37x04x01x03x06x2c'  # Parameter Request List option
dhcp_request = b'x01'  # Message Type: DHCPDISCOVER
dhcp_request += client_mac_padding + client_hw_address
dhcp_request += client_mac_padding + client_ip_address
dhcp_request += server_ip_address + relay_agent_ip_address
dhcp_request += transaction_id + client_mac_padding
dhcp_request += dhcp_options + b'xff'  # End of DHCP options

# Send the DHCP request to the DHCP server
dhcp_socket.sendto(dhcp_request, (server_ip, server_port))
print(f"Sent DHCP request to {server_ip}:{server_port}")

# Wait for the DHCP response from the server
dhcp_response, server_address = dhcp_socket.recvfrom(1024)
print(f"Received DHCP response '{dhcp_response.hex()}' from {server_address[0]}:{server_address[1]}")
if len(dhcp_response) < 240:
    print('Error: Malformed DHCP response')
else:
    dhcp_header = struct.unpack('!B B B B 4s 2s 2s 4s 4s 4s 4s 6s', dhcp_response[:28])
    dhcp_options = dhcp_response[240:]
    dhcp_message_type = dhcp_options[2]
    if dhcp_message_type == 0x02:  # DHCP offer
        offered_ip_address = socket.inet_ntoa(dhcp_options[16:20])
        print(f"DHCP server offered IP address {offered_ip_address}")
        dhcp_xid = dhcp_header[3]
        # Construct the DHCP request packet
        dhcp_request = struct.pack('!B B B B 4s 2s 2s 4s 4s 4s 4s 6s', 0x01, 0x01, 0x06, 0x00,
                                   dhcp_header[4], dhcp_header[5], dhcp_header[6], b'x00x00x00x00',
                                   dhcp_header[8], b'x00x00x00x00', dhcp_header[10], dhcp_header[11])
        dhcp_request += struct.pack('!B B B B', 0x35, 0x01, 0x03, 0x0f)  # DHCP message type: Request
        dhcp_request += struct.pack('!B B B B', 0x32, 0x04, 0x00, 0x00, 0x1c)  # DHCP requested IP address
        dhcp_request += struct.pack('!B B B B', 0x37, 0x03, 0x03, 0x01, 0x06)  # DHCP parameter request list
        dhcp_request += struct.pack('!B B B B', 0xff, 0x00, 0x00, 0x00)  # End of options
        # Send the DHCP request packet to the server
        dhcp_socket.sendto(dhcp_request, (server_ip, server_port))
        print(f"Sent DHCP request for IP address {offered_ip_address}")
        # Wait for the DHCP response to the request
        dhcp_response, server_address = dhcp_socket.recvfrom(1024)
        print(f"Received DHCP response '{dhcp_response.hex()}' from {server_address[0]}:{server_address[1]}")
        if len(dhcp_response) < 240:
            print('Error: Malformed DHCP response')
        else:
            dhcp_header = struct.unpack('!B B B B 4s 2s 2s 4s 4s 4s 4s 6s', dhcp_response[:28])
            dhcp_options = dhcp_response[240:]
            dhcp_message_type = dhcp_options[2]
            if dhcp_message_type == 0x05:  # DHCP ack
                assigned_ip_address = socket.inet_ntoa(dhcp_options[16:20])
                print(f"DHCP server assigned IP address {assigned_ip_address}")

my client send a ping to google.com and wait for a respond from the server using the localhost ip and it is not working, i get a message that the dhcp discover sent and that’s it, no respond.

now there is a problem that the server is listening to the incomming request but when i activate my client it does not resond to the packet, is there anything i could fix in my code for it to work properly, i have tried several client and ways to improve it but yet it is not working,

Asked By: Ronin Saad

||

Answers:

the problem is solved, no need to answer anymore.

Answered By: Ronin Saad

as a request of @krishh to share how it went about solving the problem: so the idea was that i need to extract the whole info of the packet before send a response, so when i get an DHCP Discoer i need to return a DHCP offer which is to offer and ip to the client then if he wants this ip then he in his turn sends a request and gets and ack by the server so this how is the handshake is done and the respond is sent, the code i shared had to be modified so it can have all of the packet headers properly as well as the client so it can request properly.

Answered By: Ronin Saad