Write your own mac changer Program in python : Coding for Cyber Security (Program №1)

The First program that I had written was a basic MAC Address Changer program which is available on github. I have explained how to create your own macchanger with python or you can also use the github tool that I have uploaded. If you are a beginner in Cyber Security, you can get started with writing your first program!

What is the use of a MAC Changer?

Each network interface on our device has a different MAC address so that they can be uniquely identified. Anonymity is a major concern in the area of cyber security and hence, we need to spoof our MAC Address so that it becomes a bit more difficult to recognize our devices on the network. We can use tools to choose our MAC address according to our choices, which makes the OPERATING SYSTEM believe that the MAC address of the NIC is the one that we have chosen.

Getting Started with writing a MAC CHANGER PROGRAM in python… (You can use PYCHARM).

  1. Steps to spoof MAC address from the terminal (that we are also going to use in the program).
# ifconfig <interface> down
# ifconfig <interface> hw ether <new mac address>
# ifconfig <interface> up

You can look for the available interfaces on your device by typing “ifconfig” in the terminal. For example : eth0, wlan0.

Adding this to our program :

We can do this by importing the subprocess module. We have created a function named changing_mac and added the above commands to it.

import subprocess#function for changing MAC address
def changing_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"])

2. You might have seen that some tools require arguments such as -h or — help for listing all the command line options. We can add this to our code by importing the optparse module.

import optparse#function for handling user input(arguments)
def getting_arguments():
parser = optparse.OptionParser()
parser.add_option("-i", "--interface", dest="interface", help="enter the interface")
parser.add_option("-m", "--mac", dest="new_mac", help="enter new mac")
(options, arguments) = parser.parse_args()
if not options.interface:
parser.error("[-] Interface not specified, use --help for more information.")
elif not options.new_mac:
parser.error("[-] Mac address not specified, use --help for more information.")
return options

3. To ensure that the user has entered the correct format of MAC address, we import re module.

import re#function for getting search results
def get_current_mac(interface):
ifconfig_result = subprocess.check_output(["ifconfig", options.interface])

mac_address_search_result = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w", ifconfig_result)
if mac_address_search_result:
return mac_address_search_result.group(0)
print("[-] Could not read Mac Address.")

4. Now, our program is ready. We will be calling the functions. We can also add a code that verifies whether the mac address actually got changed or not. For this, we need to add the following code to our program.

#verifying whether MAC has changed or not
current_mac = get_current_mac(options.interface)
if current_mac == options.new_mac:
print("[+] MAC Address was successfully changed to " + current_mac)
print("[-] MAC address did not get changed.")

After performing all of these steps, our code is ready to run.


You can also change your MAC temporarily by running the following commands —

# git clone https://www.github.com/An4ndita/MAC_Changer.git
# cd MAC_Changer
# python mac.py -i <interface> -m <new mac address>

That’s all.! I hope that you find it useful!

Happy Hacking 🙂 Remember that this content is made available for educational & informational purposes only!🌼 Follow me for more articles on cyber security and please give me your feedback. 🤩



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store