Created
June 10, 2021 13:25
-
-
Save nick3499/8ffed5f1f0fb4ad0c66df7d6edba1edb to your computer and use it in GitHub Desktop.
Python 3: Spoof MAC Address: random.randint(), random.choice(), subprocess.run()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 athex(16)in order to return a pair of hex characters, instead of one hex character. For example:Instead of padding zeros for 0–15, I simply skipped them.