如何解包数据包的wlan层

时间:2015-05-19 17:12:40

标签: python sockets struct layer unpack

所以我得到了当前的原始套接字:

from socket import socket, AF_INET, SOCK_RAW, SOCK_STREAM
from struct import unpack
from binascii import hexlify

rawsocket = socket(AF_INET, SOCK_RAW)
rawsocket.bind(("192.168.2.4", 8000))

while True:
    request = rawsocket.recvfrom(2048)

    #this part is for the sake of the question
    if len(request) is not 0:
        print(request)

    request = ""

如您所见,它等待端口192.168.2.48000上的传入数据包。我可以成功到达这个港口。我看了https://docs.python.org/3.4/library/struct.html以了解我必须如何理解第一层(wlan),但我不知道如何做到这一点。

我尝试了谷歌搜索,但没有运气,如果你知道如何打开这一层,你能否告诉我你怎么知道你这样拆开它?

我知道我必须做这样的事情

    header = request[0][0:x] #But how for must x go for the wlan layer?
    hd = unpack("y", header) #But what fmt must y be? how do I know? 

编辑:

我知道y必须以!开头,我也知道x必须是该图层的总字节数:(我正朝着正确的方向前进)

enter image description here

1 个答案:

答案 0 :(得分:1)

试试这个

header = request[0][0:14]
hd = unpack("!6s6s2s", header)

dest_addr = hexlify(hd[0])
source_addr = hexlify(hd[1])
type = hexlify(hd[2])

print "destination: {0}".format(dest_addr)
print "source: {0}".format(source_addr)
print "destination: {0}".format(type)

hd应该是[Dest Addr,Source Addr,Type / Opcode]的3元素列表