('处理 DHCP 数据包的包 - 包括 DHCP 客户端',)
项目描述
DHCP Python
版本 0.1.4
DHCP 客户端的 Python 实现和操作 DHCP 数据包的工具。包括:
- DHCP 数据包解析器,返回 Python 对象
- 支持 RFC 2132 中的所有 DHCP 选项
- 一个基本的 DHCP 客户端
安装
pip install dhcppython
要求
- Python 3.8.0 或更高版本
注意:这已经在 Ubuntu 18.04 和 Windows WSL 上进行了测试。可能会或可能不会在其他平台上运行。
数据包解析器
两个文件有助于数据包解析:dhcppython.packet和dhcppython.options. 仅对于大多数操作dhcppython.packet是必需的。
dhcppython.packet
中的主要课程dhcppython.packet是DHCPPacket. 该类DHCPPacket包含多个用于解析和构造 DHCP 数据包的构造函数。
将有线格式的数据包转换为 Python 对象
给定一个格式的 DHCP 数据包bytes(例如从套接字直接读取 DHCP 数据包会得到什么),可以通过调用from_bytes和提供字节来实例化 DHCPPacket 对象。
>>> pkt = dhcppython.packet.DHCPPacket.from_bytes(b'\x01\x01\x06\x00\xea\xbe\xc3\x97\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x8cE\x00E\x12\t\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\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\x00c\x82Sc5\x01\x01=\x07\x01\x8cE\x00E\x12\t9\x02\x05\xdc<\x0eandroid-dhcp-9\x0c\tGalaxy-S97\n\x01\x03\x06\x0f\x1a\x1c3:;+\xff')
>>> pkt
DHCPPacket(op='BOOTREQUEST', htype='ETHERNET', hlen=6, hops=0, xid=3938370455, secs=1, flags=0, ciaddr=IPv4Address('0.0.0.0'), yiaddr=IPv4Address('0.0.0.0'), siaddr=IPv4Address('0.0.0.0'), giaddr=IPv4Address('0.0.0.0'), chaddr='8C:45:00:45:12:09', sname=b'', file=b'', options=OptionList([MessageType(code=53, length=1, data=b'\x01'), ClientIdentifier(code=61, length=7, data=b'\x01\x8cE\x00E\x12\t'), MaxDHCPMessageSize(code=57, length=2, data=b'\x05\xdc'), VendorClassIdentifier(code=60, length=14, data=b'android-dhcp-9'), Hostname(code=12, length=9, data=b'Galaxy-S9'), ParameterRequestList(code=55, length=10, data=b'\x01\x03\x06\x0f\x1a\x1c3:;+'), End(code=255, length=0, data=b'')]))
将 DHCPPacket 对象转换为有线格式
给定一个 DHCPPacket 对象,您可以通过访问asbytes对象的属性轻松地以有线格式输出相应的 DHCP 数据包。
>>> pkt.asbytes
b'\x01\x01\x06\x00\xea\xbe\xc3\x97\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x8cE\x00E\x12\t\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\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\x00c\x82Sc5\x01\x01=\x07\x01\x8cE\x00E\x12\t9\x02\x05\xdc<\x0eandroid-dhcp-9\x0c\tGalaxy-S97\n\x01\x03\x06\x0f\x1a\x1c3:;+\xff'
此字节输出适合通过套接字发送到 DHCP 服务器。
DHCPPacket 类的其他构造函数
- 默认的低级构造函数(不推荐):
>>> pkt = dhcppython.packet.DHCPPacket(op="BOOTREQUEST", htype="ETHERNET", hlen=6, hops=0, xid=123456, secs=0, flags=0, ciaddr=ipaddress.IPv4Address(0), yiaddr=ipaddress.IPv4Address(0), siaddr=ipaddress.IPv4Address(0), giaddr=ipaddress.IPv4Address(0), chaddr="DE:AD:BE:EF:C0:DE", sname=b'', file=b'', options=dhcppython.options.OptionList([dhcppython.options.options.short_value_to_object(53, "DHCPDISCOVER")]))
>>> pkt
DHCPPacket(op='BOOTREQUEST', htype='ETHERNET', hlen=6, hops=0, xid=123456, secs=0, flags=0, ciaddr=IPv4Address('0.0.0.0'), yiaddr=IPv4Address('0.0.0.0'), siaddr=IPv4Address('0.0.0.0'), giaddr=IPv4Address('0.0.0.0'), chaddr='DE:AD:BE:EF:C0:DE', sname=b'', file=b'', options=OptionList([MessageType(code=53, length=1, data=b'\x01')]))
- 特定于四种主要 DHCP 消息类型的更高级别的构造函数:DISCOVER、OFFER、REQUEST、ACK:
>>> dhcppython.packet.DHCPPacket.Discover('de:ad:be:ef:c0:de')
DHCPPacket(op='BOOTREQUEST', htype='ETHERNET', hlen=6, hops=0, xid=4249353806, secs=0, flags=32768, ciaddr=IPv4Address('0.0.0.0'), yiaddr=IPv4Address('0.0.0.0'), siaddr=IPv4Address('0.0.0.0'), giaddr=IPv4Address('0.0.0.0'), chaddr='de:ad:be:ef:c0:de', sname=b'', file=b'', options=OptionList([MessageType(code=53, length=1, data=b'\x01')]))
>>> dhcppython.packet.DHCPPacket.Offer('de:ad:be:ef:c0:de', seconds=0, tx_id=4249353806, yiaddr=ipaddress.IPv4Address('192.168.56.4'))
DHCPPacket(op='BOOTREPLY', htype='ETHERNET', hlen=6, hops=0, xid=4249353806, secs=0, flags=32768, ciaddr=IPv4Address('0.0.0.0'), yiaddr=IPv4Address('192.168.56.4'), siaddr=IPv4Address('0.0.0.0'), giaddr=IPv4Address('0.0.0.0'), chaddr='de:ad:be:ef:c0:de', sname=b'', file=b'', options=OptionList([MessageType(code=53, length=1, data=b'\x02')]))
>>> dhcppython.packet.DHCPPacket.Request('de:ad:be:ef:c0:de', seconds=0, tx_id=4249353806)
DHCPPacket(op='BOOTREQUEST', htype='ETHERNET', hlen=6, hops=0, xid=4249353806, secs=0, flags=32768, ciaddr=IPv4Address('0.0.0.0'), yiaddr=IPv4Address('0.0.0.0'), siaddr=IPv4Address('0.0.0.0'), giaddr=IPv4Address('0.0.0.0'), chaddr='de:ad:be:ef:c0:de', sname=b'', file=b'', options=OptionList([MessageType(code=53, length=1, data=b'\x03')]))
>>> dhcppython.packet.DHCPPacket.Ack('de:ad:be:ef:c0:de', seconds=0, tx_id=4249353806, yiaddr=ipaddress.IPv4Address('192.168.56.4'))
DHCPPacket(op='BOOTREPLY', htype='ETHERNET', hlen=6, hops=0, xid=4249353806, secs=0, flags=32768, ciaddr=IPv4Address('0.0.0.0'), yiaddr=IPv4Address('192.168.56.4'), siaddr=IPv4Address('0.0.0.0'), giaddr=IPv4Address('0.0.0.0'), chaddr='de:ad:be:ef:c0:de', sname=b'', file=b'', options=OptionList([MessageType(code=53, length=1, data=b'\x05')]))
dhcppython.options
该模块提供以下类:
- RFC 2132 中描述的所有 DHCP 选项
- 未编码选项的未知选项类
- 如果需要其他选项,可以轻松扩展的抽象 Option 类
- 用于管理 DHCP 选项的数据结构 -
OptionList - 更高杠杆的期权工厂——
OptionDirectory
dhcppython.options.options对象和 Option 类提供了一个高级 API :
bytes_to_object通过调用方法从字节创建一个选项对象
>>> opt = dhcppython.options.options.bytes_to_object(b"\x3d\x07\x01\x8c\x45\x00\x45\x12\x09")
>>> opt
ClientIdentifier(code=61, length=7, data=b'\x01\x8cE\x00E\x12\t')
- 获取选项对象值的人类可读字典
>>> opt.value
{'client_identifier': {'hwtype': 1, 'hwaddr': '8C:45:00:45:12:09'}}
- 从其值的人类可读字典创建一个选项对象:
>>> dhcppython.options.options.value_to_object({'client_identifier': {'hwtype': 1, 'hwaddr': '8C:45:00:45:12:09'}})
ClientIdentifier(code=61, length=7, data=b'\x01\x8cE\x00E\x12\t')
或者
>>> dhcppython.options.options.short_value_to_object(61, {'hwtype': 1, 'hwaddr': '8C:45:00:45:12:09'})
ClientIdentifier(code=61, length=7, data=b'\x01\x8cE\x00E\x12\t')
- 将选项值的人类可读 dict 转换为字节表示
>>> dhcppython.options.options.value_to_bytes({'client_identifier': {'hwtype': 1, 'hwaddr': '8C:45:00:45:12:09'}})
b'=\x07\x01\x8cE\x00E\x12\t'
- 在给定其 Option 对象的情况下获取选项的字节表示
>>> opt = dhcppython.options.ClientIdentifier(code=61, length=7, data=b'\x01\x8cE\x00E\x12\t')
>>> opt.asbytes
b'=\x07\x01\x8cE\x00E\x12\t'
该类OptionList提供了一组非常方便的方法来管理 DHCP 选项列表。
OptionList从Option对象列表创建一个实例
>>> opt_list = dhcppython.options.OptionList(
... [
... dhcppython.options.options.short_value_to_object(61, {'hwtype': 1, 'hwaddr': "8c:45:00:23:45:67"}),
... dhcppython.options.options.short_value_to_object(57, 1500),
... dhcppython.options.options.short_value_to_object(60, "android-dhcp-9"),
... dhcppython.options.options.short_value_to_object(12, "Galaxy-S9"),
... dhcppython.options.options.short_value_to_object(55, [1, 3, 6, 15, 26, 28, 51, 58, 59, 43])
... ]
... )
>>> opt_list
OptionList([ClientIdentifier(code=61, length=7, data=b'\x01\x8cE\x00#Eg'), MaxDHCPMessageSize(code=57, length=2, data=b'\x05\xdc'), VendorClassIdentifier(code=60, length=14, data=b'android-dhcp-9'), Hostname(code=12, length=9, data=b'Galaxy-S9'), ParameterRequestList(code=55, length=10, data=b'\x01\x03\x06\x0f\x1a\x1c3:;+')])
OptionList通过其选项代码检索任何选项
>>> opt_list.by_code(12)
Hostname(code=12, length=9, data=b'Galaxy-S9')
>>> opt_list.by_code(13)
>>>
append使用该方法附加(添加)任何选项
>>> opt_list.append(dhcppython.options.options.short_value_to_object(53, "DHCPDISCOVER"))
>>> opt_list
OptionList([ClientIdentifier(code=61, length=7, data=b'\x01\x8cE\x00#Eg'), MaxDHCPMessageSize(code=57, length=2, data=b'\x05\xdc'), VendorClassIdentifier(code=60, length=14, data=b'android-dhcp-9'), Hostname(code=12, length=9, data=b'Galaxy-S9'), ParameterRequestList(code=55, length=10, data=b'\x01\x03\x06\x0f\x1a\x1c3:;+'), MessageType(code=53, length=1, data=b'\x01')])
- 防止重复选项(就地重复覆盖)
>>> opt_list
OptionList([ClientIdentifier(code=61, length=7, data=b'\x01\x8cE\x00#Eg'), MaxDHCPMessageSize(code=57, length=2, data=b'\x13\x88'), VendorClassIdentifier(code=60, length=14, data=b'android-dhcp-9'), Hostname(code=12, length=9, data=b'Galaxy-S9'), ParameterRequestList(code=55, length=10, data=b'\x01\x03\x06\x0f\x1a\x1c3:;+'), MessageType(code=53, length=1, data=b'\x01')])
- 允许像列表一样迭代
>>> for opt in opt_list:
... print(opt)
...
ClientIdentifier(code=61, length=7, data=b'\x01\x8cE\x00#Eg')
MaxDHCPMessageSize(code=57, length=2, data=b'\x13\x88')
VendorClassIdentifier(code=60, length=14, data=b'android-dhcp-9')
Hostname(code=12, length=9, data=b'Galaxy-S9')
ParameterRequestList(code=55, length=10, data=b'\x01\x03\x06\x0f\x1a\x1c3:;+')
MessageType(code=53, length=1, data=b'\x01')
DHCP 客户端
一个非常原始的 DHCP 客户端包含在dhcppython.client模块的这个包中。客户端能够与 DHCP 服务器协商租约,并且可以配置为使用:
- 给定的接口
- 将广播数据包或单播数据包发送到特定服务器的选项
- 在 giaddr 字段中设置一个继电器
- “欺骗”MAC 地址
- 指定随请求发送的选项
协商租约的高级接口是对象的get_lease方法dhcppython.client.DHCPClient。此方法通过 DORA DHCP 握手并返回一个Lease命名元组,其中包括以下所有数据包:
>>> 导入 dhcppython
>>> 客户端 = dhcppython 。客户端。DHCPClient (接口= “enp0s8” )
>>> 租约 = 客户端。get_lease ( mac_addr = "de:ad:be:ef:c0:de" , broadcast = True , relay = None , server = "255.255.255.255" , options_list = None )
租约 成功: 192.168.56.3 -- DE : AD : BE : EF : C0 : DE -- 经过3 毫秒 >>>租约( discover = DHCPPacket ( op = 'BOOTREQUEST' , htype = ' ETHERNET' , hlen = 6 , hops = 0 , xid = 2829179566 ,秒= 0 ,标志= 32768 ,ciaddr =
IPv4Address ( '0.0.0.0' ), yiaddr = IPv4Address ( '0.0.0.0' ), siaddr = IPv4Address ( '0.0.0.0' ), giaddr = IPv4Address ( '0.0.0.0' ), chaddr = 'de:ad:be :ef:c0:de' , sname = b '' , file = b '' , options = OptionList ([ MessageType ( code = 53 , length = 1 , data=b'\x01')])), offer=DHCPPacket(op='BOOTREPLY', htype='ETHERNET', hlen=6, hops=0, xid=2829179566, secs=0,