[Editor comment: Dude! A Scapy article by Josh Wright that can help us stay in scope and follow rules of engagement in a pen test? What's not to like? -Ed.]
By Joshua Wright, InGuardians
I participate on the Scapy mailing list, helping out with questions where I am able. Recently, I saw a question that piqued my interest:
"What I'm looking to do is identify the MAC addresses of client devices without actually sniffing any packets containing actual data relating to website content, email content etc. [...] Are there any packets I could look at that would contain the MAC of client devices but not contain any online usage data as outlined?"
If we want to investigate the presence of wireless client devices but want to avoid capturing any data frames, we can focus on management frames. WiFi networks use management frames to establish a connection to the network, disconnect from the network, and more. Three management frames are easily distinguishable as being sent by client devices only:
- Probe Request Frames
- Association Request Frames
- Reassociation Request Frames
Using Scapy, it is easy to write a little Python that checks for the IEEE 802.11 type field to determine if a frame is a management frame, then examine the subtype field to determine if it is one of the three only sent by client devices.
First, we need to put a wireless card interface into monitor mode, as shown:
root@bt:~# airmon-ng start wlan0
Interface Chipset Driver
wlan0 Ralink RT2870/3070 rt2800usb - [phy0]
(monitor mode enabled on mon0)
With the mon0 interface receiving packets in monitor mode, we can run the following Scapy script. I've added a lot of comments in the script to help explain what is happening.
# The previous line ensures that this script is run under the context
# of the Python interpreter. Next, import the Scapy functions:
from scapy.all import *
# Define the interface name that we will be sniffing from, you can
# change this if needed.
interface = "mon0"
# Next, declare a Python list to keep track of client MAC addresses
# that we have already seen so we only print the address once per client.
observedclients = 
# The sniffmgmt() function is called each time Scapy receives a packet
# (we'll tell Scapy to use this function below with the sniff() function).
# The packet that was sniffed is passed as the function argument, "p".
# Define our tuple (an immutable list) of the 3 management frame
# subtypes sent exclusively by clients. I got this list from Wireshark.
stamgmtstypes = (0, 2, 4)
# Make sure the packet has the Scapy Dot11 layer present
# Check to make sure this is a management frame (type=0) and that
# the subtype is one of our management frame subtypes indicating a
# a wireless client
if p.type == 0 and p.subtype in stamgmtstypes:
# We only want to print the MAC address of the client if it
# hasn't already been observed. Check our list and if the
# client address isn't present, print the address and then add
# it to our list.
if p.addr2 not in observedclients:
# With the sniffmgmt() function complete, we can invoke the Scapy sniff()
# function, pointing to the monitor mode interface, and telling Scapy to call
# the sniffmgmt() function for each packet received. Easy!
Next, we simply run the script:
# python client-nodata-enum.py
WARNING: No route found for IPv6 destination :: (no default route?)
From here, lots of opportunities become available. For example, we could disconnect each client from the network by adding the following line after the print statement:
We might do this in a penetration test to gently disconnect each user from the network once while capturing the activity to observe the EAP types in use (since clients reconnect automatically, this will typically have little impact to the network, but it's a good idea to ensure your customer is prepared for a potential outage if clients do not reconnect gracefully).
I'm not sure why the original request wanted to obtain a list of client MAC addresses without looking at data frames, but it's a simple task with Scapy. In the Ethical Hacking Wireless SEC617 course, we spend a good amount of time on Scapy with an emphasis on wireless fuzzing, while the Advanced Penetration Testing SEC660 course leverages Scapy for a number of wired-side attack techniques.
You can grab the script in this module, without comments, from my website at http://www.willhackforsushi.com/code/client-nodata-enum.py.