randmac.py: Generating LAA MAC Addresses
Sometimes, and in some select situations, I want to share a screenshot or some terminal output that contains MAC addresses. But for privacy reasons, I do not want to reveal the actual MAC addresses. Why? Because they’re unique identifiers (unless intentionally changed).
If the output only contained one or two physical addresses, I could manually mask them. But I wanted to explore another way to solve this problem. So, I wrote a Python module that pseudo randomizes MAC addresses for me.
So not to confuse myself, or others that may see the output, with what is the actual MAC vs randomized, I use the locally administered address (LAA) format. This means tje randomized MAC addresses from my module can be identified as such.
locally administered addresses
To differentiate MAC addresses, as stated above, the module creates Locally Administered Addresses (LAA).
The LAA is distinguished by setting the second least significant bit (LSB) of the addresses first octet. If the second LSB is set to 1, the address is administered locally.
For example, the first octet of
d6 (2 hex digits). The binary representation of the first octect is
11010110. Where bits 7-4 =
1101 or hex digit
d, and then bits 3-0 =
0110 or hex digit
The second LSB is 1. It’s bit 1 in the table below (the value two from the far right).
So this represents a unicast LAA. Here’s how we know it’s unicast:
- 0: unicast
- 1: multicast
- 0: globally unique (OUI enforced)
- 1: locally administered
Here are other possible values that identify unicast LAA addresses:
0 in the MAC address examples above could be any hexadecimal digit.
The module supports a few common MAC address formats and can output using any of these:
One caveat is the way I implemented formatting. It requires you to tell the module what format you want. It needs to know what format for output. For example providing something like
00-00-00-00-00-00 to the module would return something like
This is clunky, and could be improved. Let me know how you think it could be better.
I put the module on PyPi so that it can be installed using pip:
pip install randmac
>>> from randmac import RandMac >>> RandMac("00-00-00-00-00-00") '00-00-00-c7-11-fc' >>> RandMac("00:00:00:00:00:00", True) 'ba:ac:5f:09:fc:bb' >>> RandMac("0000.0000.0000", True) 'fe84.857f.900f'
$ python3 randmac.py 00:00:00:00:00:00 00:00:00:fc:e1:5b $ python3 randmac.py 00:00:00:00:00:00 -f 2a:81:b0:e7:1d:08
you can find it at these locations on the Internet:
I’d love to hear any constructive feedback or suggestions on how to improve this project.