Skip to content

Instantly share code, notes, and snippets.

@caueb
Created October 7, 2018 06:24
Show Gist options
  • Save caueb/c816fa77924a0a6b8821cdc04f36cb54 to your computer and use it in GitHub Desktop.
Save caueb/c816fa77924a0a6b8821cdc04f36cb54 to your computer and use it in GitHub Desktop.
MAC Address Changer
#!/usr/bin/env python
import subprocess
import optparse
import re
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")
(options, arguments) = parser.parse_args()
if not options.interface:
parser.error("[+] Please specify an interface, use --help for info.")
elif not options.new_mac:
parser.error("[+] Please specify an MAC address, use --help for info.")
return options
def change_mac(interface, new_mac):
print("[+] Changing MAC adress for " + interface + " to " + new_mac)
subprocess.call(["ifconfig", interface, "down"])
subprocess.call(["ifconfig", interface, "hw", "ether", new_mac])
subprocess.call(["ifconfig", interface, "up"])
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.decode("utf-8"))
if mac_address_search_result:
return mac_address_search_result.group(0)
else:
print("[-] Could not resolve MAC address")
def get_original_mac(interface):
p1 = subprocess.Popen(['dmesg'], stdout=subprocess.PIPE)
p2 = subprocess.Popen(['grep', interface], stdin=p1.stdout, stdout=subprocess.PIPE)
p1.stdout.close()
original_mac_result, err = p2.communicate()
original_mac_search_result = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w", original_mac_result.decode("utf-8"))
if original_mac_search_result:
return original_mac_search_result.group(0)
else:
print("[-] Could not resolve ORIGINAL MAC address")
options = get_arguments()
original_mac = get_original_mac(options.interface)
print("Original MAC = " + str(original_mac))
current_mac = get_current_mac(options.interface)
print("Current MAC = " + str(current_mac))
change_mac(options.interface, options.new_mac)
current_mac = get_current_mac(options.interface)
if current_mac == options.new_mac:
print("[+] MAC address was changed successfully.")
else:
print("[-] MAC address did NOT get changed.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment