Thomas Wagenaar
Thomas Wagenaar

Reputation: 6769

How to unpack a packets' wlan layer

So I have got the current raw socket:

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 = ""

As you see, it waits for an incoming packet on 192.168.2.4 on port 8000. I can successfully reach this port. I have looked at https://docs.python.org/3.4/library/struct.html to understand how I must understand the first layer (wlan), but I have no clue whatsoever how to do so.

I have tried googling but no luck, if you know how to unpack this layer, could you also tell me how you know you unpack it like that?

I know I have to do something like this

    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? 

EDIT:

I know that y must start with !, I also know that x has to be the total amount of bytes of that layer: (am I heading in the right direction)

enter image description here

Upvotes: 1

Views: 1027

Answers (1)

steve
steve

Reputation: 2638

Try this

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 should be a 3 element list of the [Dest Addr, Source Addr, Type/Opcode]

Upvotes: 1

Related Questions