#dhcp-server #dhcp #cli-tool #dhcpv6 #dhcpv4 #networking #script-runner

app dhcpm

A cli for mocking DHCP messages and running rhai scripts to test DHCP servers. Aims to support v4 & v6, thought v6 is as of yet unfinished.

4个版本

0.2.3 2023年5月16日
0.2.1 2022年7月2日
0.2.0 2022年6月8日
0.1.0 2022年3月18日

#352 in 网络编程

每月 26 次下载

MIT 许可证

85KB
2K SLoC

dhcpm

赞助商

感谢 Bluecat 对此项目的赞助! dhcpm 利用 dhcproto 进行DHCP协议检查。查看DHCP协议。

关于

A cli tool (and dhcp script runner!) for constructing & sending mocked dhcp client messages. dhcpm won't actually mess with the IP assigned to your network interfaces, it is intended to mock dhcp messages for testing dhcp servers. It aims to support v4 & v6, though v6 support is unfinished. Allows sending dhcp messages to non-default ports, and can be scripted with rhai.

This software is a personal project and should be considered beta. I use the basic cli features often, but the scripting features are new.

安装

您可以使用以下命令进行安装:

cargo install dhcpm

要包含rhai脚本功能,请添加

cargo install dhcpm --features "script"

使用

> dhcpm --help

Usage: dhcpm <target> [-b <bind>] [-i <interface>] [-p <port>] [-t <timeout>] [--output <output>] [--script <script>] [--no-retry <no-retry>] [<command>] [<args>]

dhcpm is a cli tool for sending dhcpv4/v6 messages

ex  dhcpv4:
        dhcpm 255.255.255.255 discover          (broadcast discover to default dhcp port)
        dhcpm 192.168.0.255 discover            (broadcast discover on interface bound to 192.168.0.x)
        dhcpm 0.0.0.0 -p 9901 discover          (unicast discover to 0.0.0.0:9901)
        dhcpm 192.168.0.1 dora                  (unicast DORA to 192.168.0.1)
        dhcpm 192.168.0.1 dora -o 118,C0A80001  (unicast DORA, incl opt 118:192.168.0.1)
    dhcpv6:
        dhcpm ::0 -p 9901 inforeq       (unicast inforeq to [::0]:9901)
        dhcpm ff02::1:2 inforeq         (multicast inforeq to default port)

Positional Arguments:
  target            ip address to send to

Options:
  -b, --bind        address to bind to [default: INADDR_ANY:0]
  -i, --interface   interface to use (requires root or `cap_net_raw`) [default:
                    None - selected by OS]
  -p, --port        which port use. [default: 67 (v4) or 546 (v6)]
  -t, --timeout     query timeout in seconds [default: 5]
  --output          select the log output format (json|pretty|debug) [default:
                    pretty]
  --no-retry        setting to "true" will prevent re-sending if we don't get a
                    response [default: false]
  --help            display usage information

Commands:
  discover          Send a DISCOVER msg
  request           Send a REQUEST msg
  release           Send a RELEASE msg
  inform            Send an INFORM msg
  decline           Send a DECLINE msg
  dora              Sends Discover then Request
  inforeq           Send a INFORMATION-REQUEST msg (dhcpv6)

通过任意端口发送DHCP

这将构建一个发现消息并将其单播到 192.168.0.1:9901

dhcpm 192.168.0.1 -p 9901 discover

dhpcm 将绑定到 0.0.0.0:0 以进行回复,这意味着服务器必须支持通过任意端口进行DHCP通信。要使用标准端口进行通信,请勿提供 --port 选项。如果没有适当的权限, dhcpm 将监听默认端口。

dhcpm 192.168.0.1 discover

这将向 192.168.0.1:67 单播,并尝试监听 0.0.0.0:68。您可以通过--bind选项更改dhcpm监听哪个地址:端口号。

广播与单播

要发送广播消息(设置广播标志),请使用网络广播地址 255.255.255255

dhcpm 255.255.255.255 discover

使用特定接口

您可以通过传递--interface/-i参数将绑定到特定接口,例如--interface enp6s0。使用此方法,您将仅通过该设备接收/发送响应。例如,

dhcpm 255.255.255.255 -i enp6s0 discover --chaddr random

您还可以使用 Linux 上的 ip addr 来获取特定接口的广播地址

2: enp6s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.130/24 brd 192.168.0.255 scope global noprefixroute enp6s0

注意 brd 192.168.0.255。您可以将此传递给 dhcpm,内核应选择该接口进行广播(例如本例中的 enp6s0

dhcpm 192.168.0.255 discover

消息参数

每个子命令(如 discover/request/release 等)都有子选项。例如,默认情况下,dhcpm 将使用默认接口的 MAC 地址,您可以通过发送适当的选项来覆盖此设置

dhcpm 255.255.255.255 discover --chaddr "80:FA:5B:41:10:6B"

dhcpv6

在 DHCPv6 中,许多消息都在多播组 ff02::1:2 上发送,但响应通常在链路本地地址(以 fe80 开头)上以单播形式返回。如果您的另一个 DHCPv6 客户端正在监听 [::0]:546,即 DHCPv6 客户端端口,则 dhcpm 将无法接收此数据。其他进程可能会首先读取数据报。

例如,我的机器上

> sudo lsof -Pi UDP
...
NetworkMa     711            root   20u  IPv6 12173080      0t0  UDP leshowbox:546

监听此 [::0]:546,因此在此进程被杀死之前,dhcpm 无法打印回复。尽管如此,我经常发现使用 dhpcm 生成消息,然后在 wireshark 或 tcpdump 中查看响应的有效性是足够的。

使用 v6 指定接口时,需要加入多播组。

> sudo dhcpm ff02::1:2 -i enp6s0 inforeq

脚本支持

使用 rhai 支持脚本。使用script功能编译 dhcpm 并使用 --script 选项提供一个路径。

dhcpm 255.255.255.255 --script test.rhai

在脚本中,您可以使用以下方式创建新的 discover 参数

let args = discover::args_default();

您可以使用 args.send() 发送此消息。

脚本支持的消息类型

  • discover::args_default()
  • request::args_default()
  • release::args_default()
  • inform::args_default()

请注意您选择的脚本,特别是如果您使用只有 sudo 可用的端口,因为脚本中的任意代码将以您赋予它的任何权限执行。

依赖关系

~11-23MB
~342K SLoC