繁体   English   中英

Python - 通过解包以太网帧获取 0xc0a8 类型期望 0x800 不期望数据

[英]Python - Not Expecting Data by Unpacking Ethernet Frame Getting 0xc0a8 on Type Expecting 0x800

我有这个 Python function 解包以太网框架:

以太网帧

def ethernet_frame(data):
        ipheader = struct.unpack('!6s6sH', data[0:14])
        dest_mac = binascii.hexlify(ipheader[0])
        src_mac = binascii.hexlify(ipheader[1])
        proto = ""

        protoType = ipheader[2]
        nextProto = hex(protoType)
        
        print(nextProto)
        if(nextProto == '0x800'):
            proto = 'IPV4'
        elif(nextProto == '0x86dd'):
            proto = 'IPV6'
        print(nextProto)
        
        return dest_mac, src_mac, proto, data[14:]

我期待获得 IPV4 的 0x800 或 IPV6 的 0x86dd。 但是在打开框架后我得到了 0xc0a8。 有人可以解释为什么我会收到这些意外数据吗? 以及如何修复它以获得正确的数据?

听起来您可能正在阅读 IP header 作为以太网 header。 0x0800是以太网 header -- data[12:13]的最后两个字节。 IP header 前置地址为 12 字节长,接下来的两个字节 (ip_header[12:13]) 是 ZA12A3079E14CED42B1AZ0 源地址的前两个字节。 十进制的0xc0a8是 (192, 168) - 在我看来,这就像内部 IP 地址 192.168.xx 的前两个字节

(IIRC), the IP header is embedded directly after the ethernet header in the yellow data segment of your diagram, so if you tried to read a second ethernet header after the first one, for example, this could happen.

(顺便说一句——您不需要将protoType转换为字符串来将其与十六进制值进行比较—— if protoType == 0x0800 / elif protoType == 0x86dd则可以简单地做)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM