import subprocess
# Subprocess is a module allows us to execute system commands.
# Commands depend on the OS which executes the script.
# E.g: subprocess.call("cmd",Shell=True)
# subprocess.call(["cmd","arg1","arg2"])
import optparse
# Optparse is used for handling cmd line arguments
import re
# This is a regix module
def get_arguments():
parser = optparse.OptionParser()
parser.add_option("-i","--interface", dest="interface",help="Interface to change MAC for.")
parser.add_option("-m","--mac", dest="address",help="New MAC address")
parser.add_option("-r","--result",dest="result",help="Set to 1 if you want to see the result")
(options, args) = parser.parse_args()
interface = options.interface
address = options.address
result = options.result
if not interface:
parser.error("[-] Please check if the interface is specified\n")
elif not address:
parser.error("[-] Please check if the MAC is specified\n")
return interface,address,result
def checkIfMACChanged(interface,address):
ipRes = subprocess.check_output(["sudo","ip","link","show",interface])
return re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w",ipRes).group(0) == address
def changeMAC(interface,address,ifShowRes="0"):
#print "SHUTTING DOWN ETH0"
#subprocess.call("sudo ip link set dev " + interface + " down ",shell=True)
subprocess.call(["sudo","ip","link","set","dev",interface,"down"])
#print "CHANGING THE MAC TO 00:22:33:44:55:66"
#subprocess.call("sudo ip link set dev "+ interface +" address " + address,shell=True)
subprocess.call(["sudo","ip","link","set","dev",interface,"address", address])
#print "START UP ETH0"
#subprocess.call("sudo ip link set dev "+ interface +" up",shell=True)
subprocess.call(["sudo","ip","link","set","dev",interface,"up"])
ifChanged = checkIfMACChanged(interface,address)
if ifChanged:
print "\n[+] The MAC is changed to "+ address + "\n"
else:
print "\n[-] Fail to change the MAC address\n"
if ifShowRes == "1":
subprocess.call("sudo ip link show",shell=True)
if __name__=='__main__':
interface,address,result = get_arguments()
changeMAC(interface,address,result)
Based on PYTHON 2.7. Tested on Kali Linux
Usage:
Use -h to see help
python macChanger.py -h
python macChanger.py -i "eth0" -m 00:11:22:33:44:55
Reference: "Learn Python & Ethical Hacking From Scratch" by Zaid Sabih. (On Udemy)