Before we get started, have you tried our new Python Code Assistant? It's like having an expert coder at your fingertips. Check it out!
The MAC address is a unique identifier assigned to each network interface in any device that connects to a network. Changing this address has many benefits, including MAC address blocking prevention; if your MAC address is blocked on an access point, you simply change it to continue using that network.
This tutorial will teach you how to change your MAC address on both Windows and Linux environments using Python.
We don't have to install anything, as we'll be using the subprocess module in Python, interacting with the ifconfig
command on Linux and getmac
, reg
, and wmic
commands on Windows.
Related: Build over 35 Ethical Hacking Scripts & Tools with Python EBook
To get started, open up a new Python file and import the libraries:
We will have a choice to randomize a new MAC address or change it to a specified one. As a result, let's make a function to generate and return a MAC address:
We use the string module to get the hexadecimal digits used in MAC addresses; we remove the lowercase characters and use the random module to sample from those characters.
Next, let's make another function that uses the ifconfig
command to get the current MAC address of our machine:
We use the check_output()
function from the subprocess module that runs the command on the default shell and returns the command output.
The MAC address is located just after the "ether"
word, we use the re.search()
method to grab that.
Now that we have our utilities, let's make the core function to change the MAC address:
Pretty straightforward, the change_mac_address()
function accepts the interface and the new MAC address as parameters, it disables the interface, changes the MAC address, and enables it again.
Now that we have everything, let's use the argparse module to wrap up our script:
We have a total of three parameters to pass to this script:
interface
: The network interface name you want to change the MAC address of, you can get it using ifconfig
or ip
commands in Linux.-r
or --random
: Whether we generate a random MAC address instead of a specified one.-m
or --mac
: The new MAC address we want to change to, don't use this with the -r
parameter.In the main code, we use the get_current_mac_address()
function to get the old MAC, we change the MAC, and then we run get_current_mac_address()
again to check if it's changed. Here's a run:
My interface name is wlan0
, and I've chosen -r
to randomize a MAC address. Here's the output:
Let's change to a specified MAC address now:
Output:
The change is reflected on the machine and other machines in the same network and the router.
Read also: How to Make an HTTP Proxy in Python
On Windows, we will be using three main commands, which are:
getmac
: This command returns a list of network interfaces and their MAC addresses and transport name; the latter is not shown when an interface is not connected.reg
: This is the command used to interact with the Windows registry. We can use the winreg
module for the same purpose. However, I preferred using the reg
command.Let's get started:
network_interface_reg_path
is the path in the registry where network interface details are located. We use transport_name_regex
and mac_address_regex
regular expressions to extract the transport name and the MAC address of each connected adapter, respectively, from the getmac
command.
Next, let's make two simple functions, one for generating random MAC addresses (like before, but in Windows format), and one for cleaning MAC addresses when the user specifies it:
Master Ethical Hacking with Python by building 35+ Tools from scratch. Get your copy now!
Download EBookFor some reason, only 2, 4, A, and E characters work as the second character on the MAC address on Windows 10. I have tried the other even characters but with no success.
Below is the function responsible for getting the available adapters' MAC addresses:
It uses the getmac
command on Windows and returns a list of MAC addresses along with their transport name.
When the above function returns more than one adapter, we need to prompt the user to choose which adapter to change the MAC address. The below function does that:
Now let's make our function to change the MAC address of a given adapter transport name that is extracted from the getmac
command:
The change_mac_address()
function uses the reg QUERY
command on Windows to query the network_interface_reg_path
we specified at the beginning of the script, it will return the list of all available adapters, and we distinguish the target adapter by its transport name.
After we find the target network interface, then we use reg add
command to add a new NetworkAddress
entry in the registry specifying the new MAC address. The function also returns the adapter index, which we'll need later on the wmic
command.
Of course, the MAC address change isn't reflected immediately when the new registry entry is added. We need to disable the adapter and enable it again. Below functions do it:
The adapter system number is required by wmic
command, and luckily we get it from our previous change_mac_address()
function.
And we're done! Let's make our main code:
Since the network interface choice is prompted after running the script (whenever two or more interfaces are detected), we don't have to add an interface argument.
The main code is simple:
get_connected_adapters_mac_address()
function.change_mac_address()
function to change the MAC address for the given adapter's transport name.disable_adapter()
and enable_adapter()
functions respectively, so the MAC address change is reflected.Alright, we're done with the script. Before you try it, you must ensure you run as an administrator. I've named the script as mac_address_changer_windows.py
:
Output:
Let's try with a random MAC:
Output:
I was prompted to choose the adapter, I've chosen the first, and the MAC address is changed to a random MAC address. Let's confirm with the getmac
command:
Output:
The operation was indeed successful! Let's try with a specified MAC:
Output:
Awesome! In this tutorial, you have learned how to make a MAC address changer on any Linux or Windows machine.
If you don't have ifconfig
command installed, you have to install it via apt install net-tools
on Debian/Ubuntu or yum install net-tools
on Fedora/CentOS.
You can get the complete code for both environments here.
Finally, we have an Ethical Hacking with Python Ebook, where we build over 35 hacking tools and scripts with Python from scratch! Make sure to check it out if you're interested.
Learn also: How to Execute Shell Commands in a Remote Machine in Python
Please note that we don't take any responsibility if you misuse the script provided in this tutorial; stay ethical!
Happy hacking ♥
Take the stress out of learning Python. Meet our Python Code Assistant – your new coding buddy. Give it a whirl!
View Full Code Transform My Code
Got a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!