-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmac_chang.py
More file actions
78 lines (48 loc) · 2.42 KB
/
mac_chang.py
File metadata and controls
78 lines (48 loc) · 2.42 KB
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
# #!/usr/bin/env python
import subprocess # Subprocess library
import optparse
import re
def get_arguments():
parser = optparse.OptionParser() # OptionParser class
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 more info")
elif not options.new_mac:
parser.error("[-] Please specify an new mac, use --help for more info")
return options
def change_mac(interface, new_mac):
print("[+] Changing MAC address for " + interface + " to " + new_mac)
subprocess.call(["ifconfig", interface, "down"]) # SECOND WAY OF CALLING SUBPROCESS, MORE SECURE
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]).decode()
mac_address_search_result = re.search(r"([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}", ifconfig_result)
if mac_address_search_result:
return mac_address_search_result.group(0)
else:
print("[-] Could not read mac address")
options = get_arguments()
# (options, arguments) = get_arguments()
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 successfully changed to ', current_mac)
else:
print('[-] MAC address did not get changed')
# interface = "eth0" # Variables help make code shorter and easy to edit
# interface = raw_input("Interface > ") # function is used in python 2.7
# interface = options.interface
# new_mac = options.new_mac # Changing variable once changes its value everywhere
# subprocess.call("ifconfig "+ interface +" down", shell=True) # Security issue, check doc for more info
# subprocess.call("ifconfig "+ interface +" hw ether " + new_mac, shell=True)
# subprocess.call("ifconfig "+ interface +" up", shell=True)
# Algorithm to check if program/script worked from ifconfig
# Execute and read ifconfig
# Read the mac address from output - using re module
# Check if MAC in ifconfig is changed
# Print Affirmation