【问题标题】:using python to determine dot1x protocol type in ethernet header使用python确定以太网标头中的dot1x协议类型
【发布时间】:2015-08-27 02:28:45
【问题描述】:

我正在使用 python 枚举dot1x 交换中的信息,但我无法解析以太网协议。我知道以太网类型字段是两个字节,dot1x 使用“888e”。我已经确认“888e”正在通过 Wireshark 传递,但我得到了以下输出。为什么显示的是“36488”而不是“888e”?

Destination MAC : 01:80:c2:00:00:03 Source MAC : c2:04:17:9c:f1:03 Protocol : 36488
Destination MAC : 01:80:c2:00:00:03 Source MAC : 08:00:27:83:5b:8b Protocol : 36488
Destination MAC : 01:80:c2:00:00:03 Source MAC : c2:04:17:9c:f1:03 Protocol : 36488
Destination MAC : 01:80:c2:00:00:03 Source MAC : 08:00:27:83:5b:8b Protocol : 36488
Destination MAC : 01:80:c2:00:00:03 Source MAC : c2:04:17:9c:f1:03 Protocol : 36488

我的代码:

import socket, sys
from struct import *

#Convert a string of 6 characters of ethernet address into a dash separated hex string
def eth_addr (a) :
    b = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" % (ord(a[0]) , ord(a[1]) , ord(a[2]), ord(a[3]), ord(a[4]) , ord(a[5]))
    return b

#create a AF_PACKET type raw socket (thats basically packet level)
#define ETH_P_ALL    0x0003          /* Every packet (be careful!!!) */
try:
    s = socket.socket( socket.AF_PACKET , socket.SOCK_RAW , socket.ntohs(0x0003))
except socket.error , msg:
    print 'Socket could not be created. Error Code : ' + str(msg[0]) + '   Message ' + msg[1]
    sys.exit()

# receive a packet
while True:
    packet = s.recvfrom(65565)

    #packet string from tuple
    packet = packet[0]

    #parse ethernet header
    eth_length = 14

    eth_header = packet[:eth_length]
    eth = unpack('!6s6sH' , eth_header)
    eth_protocol = socket.ntohs(eth[2])
    print 'Destination MAC : ' + eth_addr(packet[0:6]) + ' Source MAC : ' +  eth_addr(packet[6:12]) + ' Protocol : ' + str(eth_protocol)

【问题讨论】:

    标签: python protocols ethernet packet-capture


    【解决方案1】:

    这只是十六进制和十进制表示的问题。

    36488 是十六进制的8e88。此外,您正在进行ntohs() 转换以获得eth_protocol,它基本上改变了字节顺序,即将888e 转换为8e88

    如果您希望程序打印十六进制数字,请在 Python 文档中检查字符串格式 specs

    【讨论】:

    • 不敢相信我错过了。谢谢你。我最终将“eth_protocol = socket.ntohs(eth[2])”行替换为“eth_protocol = hex(eth[2])”。
    • @doby 很高兴为您提供帮助,如果此答案解决了您的问题,请单击答案旁边的复选标记将其标记为已接受。请参阅:How does accepting an answer work? 了解更多信息
    • 我在我的程序中使用同一行: s = socket.socket(socket.AF_PACKET , socket.SOCK_RAW , socket.ntohs(0x0003)) 并得到错误 .. _sock = _realsocket(family, type, proto) socket.error: [Errno 1] Operation not allowed .. 我应该用什么来代替这些参数?
    猜你喜欢
    • 2021-06-10
    • 1970-01-01
    • 2018-06-25
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    • 2020-06-04
    • 1970-01-01
    • 2015-11-18
    相关资源
    最近更新 更多