|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""A simple script used to alter a systems MAC address. User provides the interface, and can supply a MAC address or |
| 3 | + choose to use a randomly generated one.""" |
| 4 | + |
| 5 | +import subprocess |
| 6 | +import argparse |
| 7 | +import random |
| 8 | +import re |
| 9 | + |
| 10 | +# Generates MAC address |
| 11 | +def generate_mac(): |
| 12 | + mac = ':'.join(("%012x" % random.randint(0, 0xFFFFFFFFFFFF))[i:i+2] for i in range(0, 12, 2)) |
| 13 | + return mac |
| 14 | + |
| 15 | +# Getting user supplied arguments from terminal. |
| 16 | +def get_arguments(): |
| 17 | + parser.argparse.ArgumentParser() |
| 18 | + |
| 19 | + parser.add_argument('-i', '--interface', dest='interface', help='Interface to change MAC address') |
| 20 | + parser.add_argument('-m', '--mac', dest='mac', help='Specify new MAC address. Type "random" for random MAC.') |
| 21 | + |
| 22 | + (options) = parser.parse_ards() |
| 23 | + |
| 24 | + # call to random_mac |
| 25 | + if option.mac == 'random': |
| 26 | + option.mac = generate_mac() |
| 27 | + |
| 28 | + if not options.interface: |
| 29 | + parser.error('[-] Please specify an interface, use --help for more info.') |
| 30 | + elif not options.mac: |
| 31 | + parser.error('[-] Please specify a new MAC, use --help for more info') |
| 32 | + |
| 33 | + return options |
| 34 | + |
| 35 | +def change_mac(interface, mac): |
| 36 | + print(f'[+] Changing MAC address for interface {interface} to {mac}\n') |
| 37 | + |
| 38 | + subprocess.call(['ifconfig', interface, 'down']) |
| 39 | + |
| 40 | + subprocess.call(['ifconfig', interface, 'hw', 'ether', mac]) |
| 41 | + |
| 42 | + subprocess.call(['ifconfig', interface, 'up']) |
| 43 | + |
| 44 | + subprocess.call(['ifconfig', interface]) |
| 45 | + |
| 46 | + |
| 47 | +def get_current_mac(interface): |
| 48 | + ifconfig_result = subprocess.check_output(['ifconfig', interface]) |
| 49 | + mac_address_search_result = re.search(r"[0-9A-F]{2}[:-]){5}([0-9A-F]{2})", infconfig_result.decode(), re.IGNORECASE) |
| 50 | + |
| 51 | + if mac_address_search_results: |
| 52 | + return mac_address_search_results.group(0) |
| 53 | + else: |
| 54 | + print('[-] Could not read the MAC address.') |
| 55 | + |
| 56 | +options = get_arguments() |
| 57 | +current_mac = get_current_mac(options.interface) |
| 58 | +print(f'Current MAC: {str{current_mac}}') |
| 59 | + |
| 60 | +change_mac(options.interface, options.mac) |
| 61 | + |
| 62 | +current_mac = get_current_mac(options.interface) |
| 63 | +if current_mac == options.mac: |
| 64 | + print(f'[+] MAC address was successfully changed to {current_mac}.') |
| 65 | +else: |
| 66 | + print('[-] MAC address was not changed.') |
0 commit comments