Create your own MAC Address Changer using Python


mac address changer

This post may contain affiliate links/ads and I may earn a small commission when you click on the links/ads at no additional cost to you. As an Amazon Affiliate, I earn from qualifying purchases. Techsphinx also participates in the StationX Affiliate program. You can read my full disclaimer here.

In this post, we are going to create our own MAC address changer program using python. There are a lot of MAC changer programs out there, but if you want to learn how a MAC address changer actually works and want to create your own program rather than using tools created by someone else out there (Don’t be a “script kiddie”), then keep reading the post till the end.

Pre-requisites:

  1. Computer or Laptop
  2. A Linux operating system, installed as a host or in a virtual machine.
  3. Python Basics.

Let’s Start by covering some basics.

What is MAC Address?

Media Access Control (MAC) Address, is a Physical, Permanent and Unique address assigned to a networking device by the manufacturer.

Every Ethernet Card, Wi-Fi card and Bluetooth have a MAC Address. There are so many networking devices so to have a unique address for each device, MAC address is made using six two-digit hexadecimal numbers separated by colons (“:”)

Example of a MAC address = 00:0c:43:b4:c1:8e

MAC address is physical, it means even if you take an Ethernet card from one computer and plug it into another, the MAC address will not change. It’s integrated into your network card.

For Newbies: MAC (Media Access Control) address is not associated with the Mac (Macintosh) of Apple. So, Don’t confuse MAC address with Mac of Apple.

Can you change MAC address?

OK, so now we know what a MAC address is, but as I previously mentioned MAC address is permanent, i.e. It can’t be changed. Then what is the meaning of this post, how can we create a MAC changer program if we can’t change it?

Actually, MAC address integrated into your NIC (Network Interface Card) by the manufacturer can’t be changed, but we can change/spoof it in the operating system, and that’s what this post is about.

Why do we need to change MAC Address?

This is a common question asked by many.

Yeah, you don’t need to change MAC address if you are just a normal person operating the device, but if you are a system admin, network engineer or a HACKER, then you should have the knowledge to change MAC address.

System admin and network engineer face many networking issues, so to have knowledge of MAC address is a must to troubleshoot errors or to do some development work.

As for a hacker you may have already guessed, mostly to conceal the identity or to infiltrate a particular network where MAC filtering is enabled.

Benefits for hackers after changing the MAC.

  1. Conceal the identity (To become anonymous on the network).
  2. Impersonate other devices to steal data or to conduct fraudulent acts.
  3. Infiltrate networks where only specific devices have access to or where MAC filtering is enabled.

How to see MAC Address in Windows, Linux, Android?

Ok, so every NIC has a MAC address, but how can we see our MAC address?

There are different tools out there and some are inbuilt in your operating system that provides the facility to see MAC address.

Let’s see how to know MAC address by using:

Windows:

Graphical method:

  1. Go to Control Panel > Network and Sharing Centre
  2. Click on the connection you want to find the MAC address.
  3. A popup dialog box will open then click on details.
  4. Another popup dialog box will open, showing you all the details of the network, you can find MAC address (Physical Address) there.

Command Line Method:

  1. Open cmd (command prompt).
  2. Type “ipconfig /all” (without quotes) and hit enter.
  3. You will see MAC address and other details of all the NIC that are connected.

Linux:

  1. Open Terminal.
  2. Type “ifconfig” (without quotes)
  3. You will see the MAC address (HW addr) there.

Android:

  1. Go to settings and open About Phone.
  2. Click on status.
  3. Check Wi-Fi MAC address there.

How to change MAC in Linux?

We are creating a MAC changer program for this, but let me show how to do it manually so that you understand the process behind it.

Please take a backup of the original MAC address. (Copy and paste it a text file.)

First, you have to shut down the driver for the interface you want to change the MAC address, so open the terminal and type:

ifconfig eth0 down

Now, enter the new MAC address after the “hw” flag. (In my case it’s 00:1a:2b:3e:44:c5, make sure it has 12 characters separated by a colon)

ifconfig eth0 hw 00:1a:2b:3e:44:c5

Finally, activate the interface again.

ifconfig eth0 up

Now, we have done it manually, so let’s create our MAC address changer.

Write your own MAC address changer program using python.

Create a new python file, in my case its “mac_changer.py”.

Learn about the following modules:
1.) subprocess
2.) re
3.) optparse
4.) random

Let’s import all the necessary modules.

#!/usr/bin/env python

import subprocess
import re
import optparse
import random

Create a function with options to select the interface and specify the new MAC address.

def get_arguments():
	parser = optparse.OptionParser()
	parser.add_option("-i", "--interface", dest="interface", help="Interface to change its MAC address")
	parser.add_option("-m", "--mac", dest="new_mac", help="New MAC Address")
	parser.add_option("-r", "--random", action="store_true", dest="rand_mac", help="Random MAC Address")
	(options, arguments) = parser.parse_args()
	if not options.interface:
		parser.error("[-] Please specify an interface, use --help for more info.")
	elif str(options.new_mac) == "None" and str(options.rand_mac) == "None":
	 	parser.error("[-] Please specify a new MAC or choose -r for random MAC, use --help for more info.")
	return options

Now, Create a function for generating and changing to random MAC address.

def random_mac(interface):
	create_random_mac = "02:00:00:%02x:%02x:%02x" % (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
	subprocess.call(["ifconfig", interface, "down"])
	subprocess.call(["ifconfig", interface, "hw", "ether", create_random_mac])
	subprocess.call(["ifconfig", interface, "up"])
	global RANDOM_MAC
	RANDOM_MAC = create_random_mac

Create function to change MAC address provided by user.

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"])

Create a function to retrieve the current MAC.

def get_current_mac(interface):
	ifconfig_result = subprocess.check_output(["ifconfig", 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)
	else:
		print("[-] Could not read MAC address")

Call the get_argument function.

options = get_arguments()

Get the current MAC address and store it in a file as a backup in case of any errors or if you want to return to your old MAC address.

current_mac = get_current_mac(options.interface)
print("Current MAC = " + str(current_mac))
backup_cmd = "echo '" + current_mac + "' > backup_mac.txt"
subprocess.check_output(backup_cmd, shell=True)

At last call random_mac function or change_mac function as per the requirements of the user.

if options.rand_mac == True:
	# Call random_mac function.
	random_mac(options.interface)
	# Get the new MAC address in use and print the message.
	current_mac = get_current_mac(options.interface)
	print(current_mac)
	if current_mac == RANDOM_MAC:
		print("[+] MAC address changed successfully to " + current_mac)
	else:
		print("[-] Could not change MAC address")
elif str(options.new_mac) != "None": 
	# Execute the change_mac function.
	change_mac(options.interface, options.new_mac)
	# Get the new MAC address in use and print the message.
	current_mac = get_current_mac(options.interface)
	print(current_mac)
	if current_mac == options.new_mac:
		print("[+] MAC address changed successfully to " + current_mac)
	else:
		print("[-] Could not change MAC address")
else:
	# IF something goes wrong.
	print("Error changing MAC")

Congratulations, for successfully creating a MAC changer program.

If you encountered any errors or doubts, please feel free to ask in the comments.

I hope you enjoyed creating this small tool.

If you like this post, then follow Techsphinx on Facebook and Twitter for more reviews, tricks, tips and tutorials.

This article needs update or correction? Report the issue here so I can update it.


Like it? Share with your friends!

Rahul R Nair

Rahul is obsessed with technology and electronic devices. He is also the founder of TechSphinx. Being a technophile, he is always busy doing some techy stuff or learning about the latest technologies. When not busy with his usual routine (staring at the computer screen) he likes to write and share his knowledge with the world.
Subscribe
Notify of
guest
4 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
4
0
Would love your thoughts, please comment.x
()
x