-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmac_changer.py
82 lines (63 loc) · 2.23 KB
/
mac_changer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Author: gh0stxplt
#
# Date: 08/13/2021
#
#
# !/usr/bin/env python3
#Requirements
import random
import subprocess
import argparse
import re
#Validate args on execution
def getArgs():
parser = argparse.ArgumentParser() #argparse intialize
#Obtain interface
parser.add_argument(
"-i",
"--interface",
dest="interface",
help="Interface for MAC Address Change - e.g. wlan0")
options = parser.parse_args()
#verify arguments
if not options.interface:
parser.error("[-] No interface supplied. See --help for more info")
return options
#Generate random valid MAC
def hex(length): #Validate HEX
return ''.join(random.choice('0123456789ABCDEF') for _ in range(length))
def genMAC(): #Generates the MAC address
print("[+] Generating Random MAC Address")
rnd_mac = '00' + ':' + hex(2) + ':' + hex(2) + ':' + hex(2) + ':' + hex(
2) + ':' + hex(2)
print("[+] Random MAC is: " + rnd_mac)
return rnd_mac
#Changing interface MAC address
def chgMAC(interface):
print("[+] Changing MAC Address for interface " + interface +
" to randomly generated MAC: " + rnd_mac)
subprocess.call(["ifconfig", interface, "down"])
subprocess.call(["ifconfig", interface, "hw", "ether", rnd_mac])
subprocess.call(["ifconfig", interface, "up"])
#Get current mac address
def getCurrentMAC(interface):
ifconfig_result = subprocess.check_output(["ifconfig", options.interface]).decode()
mac_verify = re.search(r'\w\w:\w\w:\w\w:\w\w:\w\w:\w\w', ifconfig_result)
if mac_verify:
return mac_verify.group(0)
else:
print("[-] Could not read MAC address")
return
#Exec program, verify args exist, and display current MAC address
options = getArgs()
currentMAC = getCurrentMAC(options.interface)
print("[+] Current MAC Address = " + str(currentMAC))
rnd_mac = genMAC()
#Call MAC address change
chgMAC(options.interface)
#Verify MAC changed successfully
changedMAC = getCurrentMAC(options.interface)
if changedMAC.lower() == rnd_mac.lower():
print("[+] MAC Address successfully changed!")
else:
print("[-] MAC Address was not changed.")