Python Script To Change MAC Address

Raj Upadhyay
4 min readSep 9, 2020

--

A MAC address is 48 bits in length. It’s also known as “physical address” and it is the unique identifier that is assigned to the NIC (Network Interface Card) of the computer.

MAC (Media Access Control) addresses are permanent by design, several mechanisms allow modification, or “spoofing”, of the MAC address.

To simply change the MAC address we can do the following command.

sudo ifconfig interface_name down
sudo ifconfig interface_name hw ether new_mac_address
sudo ifconfig interface_name up
MAC Address Changed

Let’s Learn How to change MAC using Python. ( For Linux)

1. Import Libraries

libraries

subprocess: This module allows us to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. Read More..

argparse: It’s recommended a command-line parsing module in the Python standard library. It’s used to get command line arguments into your program.

re: We will use “re”(Regular expression) to do some string matching operations.

2. Let see the input format and validate it.

python3 macchanger.py -i eth0 -m 00:11:22:33:44:55

So as we can user need to specify -i and -m arguments in the command-line. So first let’s get command-line arguments using the custom get_arguments function.

get_arguments
get_arguments function

firstly we created argparse object “parser”.

parser = argparse.ArgumentParser()

We added two arguments. one for interface and second for mac address.

  1. The value of interface (-i) arguments will go to “interface” variable which we have specified via dest=” “.
  2. same way new_mac_address will be stored in the “new_mac” variable.
parser.add_argument("-i", "--interface", dest="interface", help="")
parser.add_argument("-m", "--mac", dest="new_mac", help="")

With the help of the parse_args function, we get all arguments and we stored it in the options variable.

options = parser.parse_args()

Now in the next step, we check the user has a given interface and a new mac address or not.

if not options.interface:
parser.error("Please specify an interface,use --help for info")
elif not options.new_mac:
parser.error("Please specify a new mac, use --help for more info")

If the user has given the correct arguments then we will return it.

3. Now let’s check the Current MAC address before changing it.

To check the current mac address we call the get_current_mac function which takes Interface as an argument.

get_current_mac

Let’s see get_current_mac function.

def get_current_mac(interface):
ifconfig_result = subprocess.check_output(["ifconfig", interface])
a=re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w", ifconfig_result)
if a:
return a.group(0)
else:
print("[-] Could not read MAC address")

We used subprocess and called check_output function in which we have given the command “ifconfig interface” so it will return ifconfig result for the specified interface. ( we have simply done the following thing, we opened a terminal and given command ifconfig interface and then we get the output of that command. this whole process is done in the background.)

ifconfig_result = subprocess.check_output(["ifconfig", interface])

The next thing we done is to identify the “MAC” address from the output. so we used Regular Expression “re” and if we found the MAC address then we return it else we give an error that the MAC address not found.

a=re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w", ifconfig_result)
if a:
return a.group(0)
else:
print("[-] Could not read MAC address")

4. Now let’s change the MAC address according to the given Input.

To accomplish this task we will create one custom function known as change_mac which takes interface and mac address as an argument.

change_mac function

Change_mac function:

def change_mac(interface, new_mac):
print("Changing MAC address for" + interface + "to" + new_mac)
subprocess.call(["ifconfig", interface, "down"])
subprocess.call(["ifconfig", interface, "hw", "ether", new_mac])
subprocess.call(["ifconfig", interface, "up"])

we are doing three operations in the background.

  1. we will turn off specified interface.
  2. with the help of ” hw”, we will specify our new MAC address.
  3. we will turn on the specified interface.

So this is how we changed our MAC address.

5. Now let’s again call the get_current_mac function to check the MAC address is changed or not.

get_current_mac

6. Now let’s check if the current MAC address is the same as the user has specified via command-line arguments or not.

Mac address is changed or not.

Here we are simply comparing the current MAC address with the user given input.

So let’s combine all the steps.

Full Script

Note: Changing the MAC address via any scripts or software is not permanent, Once you reboot your PC/Laptop, it will revert to the MAC address physically stored in the card.

You can get this code at the following link.

Thank you.

--

--

Raj Upadhyay
Raj Upadhyay

Written by Raj Upadhyay

DFIR Consultant || #LoveToPlayCTF #infosec #cybersecurity #4n6

No responses yet