Skip to content

DHCP unpack issue #1901

Open
Open
@zmw12306

Description

@zmw12306

Closing this for now, will reopen if the problem still persists

I am using the latest version 0.12.0, but still having issue.
if File "/home/youwei/.local/share/pipx/venvs/impacket/lib/python3.10/site-packages/impacket/dhcp.py", line 181, in unpackOptions:
value = self.unpack(format, options[i+2:i+2+size])
then I have:

Traceback (most recent call last):
  File "/home/youwei/XDiff/Impacket/DHCPOption.py", line 35, in <module>
    dhcp_packet_decoded = dhcp_decoder.decode(dhcp_packet)
  File "/home/youwei/.local/share/pipx/venvs/impacket/lib/python3.10/site-packages/impacket/ImpactDecoder.py", line 969, in decode
    if dhcp.DhcpPacket(aBuffer[off:])['cookie'] == dhcp.DhcpPacket.MAGIC_NUMBER:
  File "/home/youwei/.local/share/pipx/venvs/impacket/lib/python3.10/site-packages/impacket/dhcp.py", line 150, in __init__
    structure.Structure.__init__(self, data, alignment)
  File "/home/youwei/.local/share/pipx/venvs/impacket/lib/python3.10/site-packages/impacket/structure.py", line 91, in __init__
    self.fromString(data)
  File "/home/youwei/.local/share/pipx/venvs/impacket/lib/python3.10/site-packages/impacket/structure.py", line 156, in fromString
    self[field[0]] = self.unpack(field[1], data[:size], dataClassOrCode = dataClassOrCode, field = field[0])
  File "/home/youwei/.local/share/pipx/venvs/impacket/lib/python3.10/site-packages/impacket/structure.py", line 311, in unpack
    return eval(dataClassOrCode, {}, fields)
  File "<string>", line 1, in <module>
  File "/home/youwei/.local/share/pipx/venvs/impacket/lib/python3.10/site-packages/impacket/dhcp.py", line 181, in unpackOptions
    value = self.unpack(format, options[i+2:i+2+size])
  File "/home/youwei/.local/share/pipx/venvs/impacket/lib/python3.10/site-packages/impacket/structure.py", line 386, in unpack
    return dataClassOrCode(data)
  File "/home/youwei/.local/share/pipx/venvs/impacket/lib/python3.10/site-packages/six.py", line 644, in b
    return s.encode("latin-1")
AttributeError: ("'bytearray' object has no attribute 'encode'", "When unpacking field 'options | _ | b''[:0]'"). Did you mean: 'decode'?

if I changed File "/home/youwei/.local/share/pipx/venvs/impacket/lib/python3.10/site-packages/impacket/dhcp.py", line 181, in unpackOptions into (https://github.com/fortra/impacket/pull/1858/files):
value = self.unpack(format, bytes(options[i+2:i+2+size])), I get:

struct.error: ('unpack requires a buffer of 4 bytes', "When unpacking field 'options | _ | b''[:0]'")

Here is my code:

import struct
from impacket import ImpactDecoder
from impacket import dhcp
import socket

# Basic DHCP packet construction
dhcp_packet = (
    b'\x01'              # Message type: Boot Request (1 byte)
    b'\x01'              # Hardware type: Ethernet (1 byte)
    b'\x06'              # Hardware address length: 6 (1 byte)
    b'\x00'              # Hops: 0 (1 byte)
    b'\x39\x03\xF3\x26'  # Transaction ID: Random (4 bytes)
    b'\x00\x00'          # Seconds elapsed: 0 (2 bytes)
    b'\x80\x00'          # Flags: 0x8000 (Broadcast) (2 bytes)
    b'\x00\x00\x00\x00'  # Client IP address: 0.0.0.0 (4 bytes)
    b'\x00\x00\x00\x00'  # Your (client) IP address: 0.0.0.0 (4 bytes)
    b'\x00\x00\x00\x00'  # Next server IP address: 0.0.0.0 (4 bytes)
    b'\x00\x00\x00\x00'  # Relay agent IP address: 0.0.0.0 (4 bytes)
    b'\x00\x26\x9e\x04\x0a\x5b'  # Client MAC address (6 bytes)
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'  # Client hardware address padding (10 bytes)
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'  # Server name padding (64 bytes)
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'   # Boot filename padding (128 bytes)
    b'\x63\x82\x53\x63'  # Magic cookie: DHCP (4 bytes)
    # DHCP Options:
    b'\x35\x01\x01'
    b'\x32\x04\xc0\xa8\x01\x64'
    b'\x33\x04\x01\xa8\x01\x64'      # Option: (53) DHCP Message Type (Discover) (3 bytes)
    # b'\x3d\x07\x01\x00\x26\x9e\x04\x0a\x5b'  # Option: (61) Client identifier (7 bytes)
    b'\x37\x01\x03\x01\x06'  # Option: (55) Parameter Request List (5 bytes)
    # b'\xff'              # End Option (1 byte)
)


dhcp_decoder = ImpactDecoder.BootpDecoder()
dhcp_packet_decoded = dhcp_decoder.decode(dhcp_packet)
print(dhcp_packet_decoded)

# # Decode the DHCP message from the UDP packet
# dhcp_decoder = ImpactDecoder.DHCPDecoder()
# dhcp_packet_decoded = dhcp_decoder.decode(dhcp_packet)

# # Print the decoded DHCP packet
# print(dhcp_packet_decoded.getOptionValue('message-type'))
# print(dhcp_packet_decoded.getOptionValue('client-id'))
# print(dhcp_packet_decoded.getOptionValue('parameter-request-list'))


def construct_dhcp_discover_packet():
    # Define the magic cookie and the DHCP options for the packet
    magic_cookie = 0x63825363  # Standard DHCP magic cookie
    dhcp_options = [
        ('message-type', 1),  # DHCP Discover (message type 1)
        ('requested-ip', 0xc0a80164),  # Requested IP address (192.168.1.100)
    ]

    # Create a DhcpPacket object
    dhcp_packet = dhcp.DhcpPacket()

    # Set the magic cookie
    dhcp_packet.fields['cookie'] = magic_cookie

    # Set and pack the DHCP options
    dhcp_packet.fields['options'] = dhcp_packet.packOptions(dhcp_options)



    # Return the packed packet
    return dhcp_packet.fields['options']

# Example usage
packed_dhcp_discover = construct_dhcp_discover_packet()
print(f"Packed DHCP Discover: {packed_dhcp_discover}")

Originally posted by @zmw12306 in #1820

Metadata

Metadata

Labels

lowLow priority item

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions