Skip to content

Instantly share code, notes, and snippets.

@nick3499
Created June 10, 2021 13:25
Show Gist options
  • Save nick3499/8ffed5f1f0fb4ad0c66df7d6edba1edb to your computer and use it in GitHub Desktop.
Save nick3499/8ffed5f1f0fb4ad0c66df7d6edba1edb to your computer and use it in GitHub Desktop.
Python 3: Spoof MAC Address: random.randint(), random.choice(), subprocess.run()
#!/usr/bin/python3
'''Spoof MAC address. (as a cybersecurity enhancement)'''
from random import randint
from random import choice
from subprocess import run
# pseudo-randomly generated MAC address
mac_addr = f"{hex(choice(range(16, 255, 2)))[2:]}:\
{hex(randint(16, 256))[2:]}:\
{hex(randint(16, 256))[2:]}:\
{hex(randint(16, 256))[2:]}:\
{hex(randint(16, 256))[2:]}:\
{hex(randint(16, 256))[2:]}"
print(mac_addr)
run(["sudo", "ip", "link", "set", "enp2s0", "address", mac_addr], check=True)
@nick3499
Copy link
Author

Based on how the first two binary bits in the first octet are used, basically, an even number must be passed to hex(), and, for the convenience of the script, those values should start at hex(16) in order to return a pair of hex characters, instead of one hex character. For example:

>>> hex(15)[2:]
'f'
>>> hex(16)[2:]
'10'

Instead of padding zeros for 0–15, I simply skipped them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment