Skip to main content
 首页 » 编程设计

python之如何用scapy解码DHCP中继数据包的十六进制数据

2024年10月25日7txw1958

我想用 scapy 从我的 DHCP 服务器上的 DHCP 中继嗅探 DHCP 数据包,但我有一个问题,因为我唯一感兴趣的部分是用十六进制写的。

有没有一种方法可以强制 scapy 解码这部分或另一个库来做到这一点?

下面是一个被 scapy 嗅探的数据包的例子。我要解码的部分是“Unknown DHCPv6 Option”下的“data”:

###[ Ethernet ]### 
  dst= f4:cf:e2:4c:9c:ed 
  src= 00:22:bd:f8:35:47 
  type= 0x86dd 
###[ IPv6 ]### 
     version= 6L 
     tc= 0L 
     fl= 0L 
     plen= 149 
     nh= UDP 
     hlim= 255 
     src= 2001:db8:0:1::ee 
     dst= 2001:db8:0:1::ed 
###[ UDP ]### 
        sport= dhcpv6_server 
        dport= dhcpv6_server 
        len= 149 
        chksum= 0xd489 
###[ DHCPv6 Relay Forward Message (Relay Agent/Server Message) ]### 
           msgtype= RELAY-FORW 
           hopcount= 0 
           linkaddr= 2001:db8:0:1::e6 
           peeraddr= fe80::f6cf:e2ff:fe11:7ef9 
###[ Unknown DHCPv6 OPtion ]### 
              optcode= RELAY_MSG 
              optlen= 77 
              data= '\x01\xed\t{\x00\x01\x00\n\x00\x03\x00\x01B\x00\x14\\U\xb3\x00\x06\x00\x04\x00\x17\x00\x18\x00\x08\x00\x02\x00\x00\x00\x19\x00)\xe2\x11~\xf9\x00\x00\x0e\x10\x00\x00\x15\x18\x00\x1a\x00\x19\x00\x00\x1c \x00\x00*00 \x01\x0b\xc80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' 
###[ DHCP6 Option - Relay Agent Remote-ID ]### 
                 optcode= OPTION_REMOTE_ID 
                 optlen= 14 
                 enterprisenum= ciscoSystems 
                 remoteid= '\x00\x03\x08\x00\x00"\xbd\xf85G' 
###[ DHCP6 Interface-Id Option ]### 
                    optcode= INTERFACE_ID 
                    optlen= 4 
                    ifaceid= '\t\x01\x00\n' 

当我执行 packet[DHCP6OptUnknown].getfieldval(data) 它返回

'\x01\xed\t{\x00\x01\x00\n\x00\x03\x00\x01B\x00\x14\\U\xb3\x00\x06\x00\x04\x00\x17\x00\x18\x00\x08\x00\x02\x00\x00\x00\x19\x00)\xe2\x11~\xf9\x00\x00\x0e\x10\x00\x00\x15\x18\x00\x1a\x00\x19\x00\x00\x1c\x00\x00*00\x01\x0b\xc80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

这是我要的数据,里面我想获取服务器发送的Prefix委托(delegate)。

在此先感谢您的帮助!

Ps:抱歉我的英语不好:)

编辑: 修补 DHCP 中继数据包后如下所示:

###[ Ethernet ]### 
  dst= 00:22:bd:f8:35:47 
  src= f4:cf:e2:4c:9c:ed 
  type= 0x86dd 
###[ IPv6 ]### 
     version= 6L 
     tc= 0L 
     fl= 0L 
     plen= 192 
     nh= UDP 
     hlim= 64 
     src= 2001:db8:0:1::ed 
     dst= 2001:db8:0:1::ee 
###[ UDP ]### 
        sport= dhcpv6_server 
        dport= dhcpv6_server 
        len= 192 
        chksum= 0x26dc 
###[ DHCPv6 Relay Reply Message (Relay Agent/Server Message) ]### 
           msgtype= RELAY-REPL 
           hopcount= 0 
           linkaddr= 2001:db8:0:1::e6 
           peeraddr= fe80::f6cf:e2ff:fe11:7ef9 
###[ DHCP6 Interface-Id Option ]### 
              optcode= INTERFACE_ID 
              optlen= 4 
              ifaceid= '\t\x01\x00\n' 
###[ DHCP6 Relay Message Option ]### 
                 optcode= RELAY_MSG 
                 optlen= 138 
###[ DHCPv6 Reply Message ]### 
                    msgtype= REPLY 
                    trid= 0x8bcc55 
###[ DHCP6 Option - Identity Association for Prefix Delegation ]### 
                       optcode= OPTION_IA_PD 
                       optlen= 41 
                       iaid= 3792797433 
                       T1= 3600 
                       T2= 7200 
                       \iapdopt\ 
                        |###[ DHCP6 Option - IA_PD Prefix option ]### 
                        |  optcode= OPTION_IAPREFIX 
                        |  optlen= 25 
                        |  preflft= 4500 
                        |  validlft= 7200 
                        |  plen= 48 
                        |  prefix= 2001:db8:3000:: 
                        |  iaprefopts= '' 
###[ DHCP6 Client Identifier Option ]### 
                          optcode= CLIENTID 
                          optlen= 10 
                          \duid\ 
                           |###[ DUID - Based on Link-layer Address ]### 
                           |  type= Link-layer Address 
                           |  hwtype= Ethernet (10Mb) 
                           |  lladdr= 42:00:14:xx:xx:xx 
###[ DHCP6 Server Identifier Option ]### 
                             optcode= SERVERID 
                             optlen= 14 
                             \duid\ 
                              |###[ DUID - Link-layer address plus time ]### 
                              |  type= Link-layer address plus time 
                              |  hwtype= Ethernet (10Mb) 
                              |  timeval= Tue, 24 Mar 2015 03:09:53 +0000 (1427166593) 
                              |  lladdr= f4:cf:e2:4c:9c:ed 
###[ DHCP6 Preference Option ]### 
                                optcode= PREFERENCE 
                                optlen= 1 
                                prefval= 255 
###[ DHCP6 Option - DNS Recursive Name Server ]### 
                                   optcode= DNS Recursive Name Server Option 
                                   optlen= 32 
                                   dnsservers= [ 2001:db8:401::3, 2001:db8:1::16 ] 
###[ DHCP6 Option - Domain Search List option ]### 
                                      optcode= Domain Search List option 
                                      optlen= 12 
                                      dnsdomains= ['foo.net'] 

请您参考如下方法:

经过很多次,我找到了这个补丁:http://bb.secdev.org/scapy/pull-request/82/dhcpv6-relay-reply-message-relay-agent/diff#chg-scapy/layers/dhcp6.py它对我有用。所有数据包都已解码!