-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmac_change.py
50 lines (37 loc) · 2.03 KB
/
mac_change.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
import subprocess
import regex as re
import string
import random
# the registry path of network interfaces
network_interface_reg_path = r"HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e972-e325-11ce-bfc1-08002be10318}"
# the transport name regular expression, looks like {AF1B45DB-B5D4-46D0-B4EA-3E18FA49BF5F}
transport_name_regex = re.compile("{.+}")
# the MAC address regular expression
mac_address_regex = re.compile(r"([A-Z0-9]{2}[:-]){5}([A-Z0-9]{2})")
def get_connected_adapters_mac_address():
# make a list to collect connected adapter's MAC addresses along with the transport name
connected_adapters_mac = []
# use the getmac command to extract
for potential_mac in subprocess.check_output("getmac").decode("cp850").splitlines():
# parse the MAC address from the line
mac_address = mac_address_regex.search(potential_mac)
# parse the transport name from the line
transport_name = transport_name_regex.search(potential_mac)
if mac_address and transport_name:
# if a MAC and transport name are found, add them to our list
connected_adapters_mac.append((mac_address.group(), transport_name.group()))
return connected_adapters_mac
def disable_adapter(adapter_index):
# use wmic command to disable our adapter so the MAC address change is reflected
disable_output = subprocess.check_output(f"wmic path win32_networkadapter where index={adapter_index} call disable").decode()
return disable_output
def enable_adapter(adapter_index):
# use wmic command to enable our adapter so the MAC address change is reflected
enable_output = subprocess.check_output(f"wmic path win32_networkadapter where index={adapter_index} call enable").decode()
return enable_output
if __name__ == "__main__":
connected_adapters_mac = get_connected_adapters_mac_address()
disable_adapter( connected_adapters_mac)
print("[+] Adapter is disabled")
enable_adapter( connected_adapters_mac)
print("[+] Adapter is enabled again")