Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit41be2e6

Browse files
committed
add mac address changer tutorial
1 parent349589f commit41be2e6

File tree

4 files changed

+176
-0
lines changed

4 files changed

+176
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
3434
-[How to Make an HTTP Proxy in Python](https://www.thepythoncode.com/article/writing-http-proxy-in-python-with-mitmproxy). ([code](ethical-hacking/http-mitm-proxy))
3535
-[How to Extract Chrome Cookies in Python](https://www.thepythoncode.com/article/extract-chrome-cookies-python). ([code](ethical-hacking/chrome-cookie-extractor))
3636
-[How to Extract Saved WiFi Passwords in Python](https://www.thepythoncode.com/article/extract-saved-wifi-passwords-in-python). ([code](ethical-hacking/get-wifi-passwords))
37+
-[How to Make a MAC Address Changer in Python](https://www.thepythoncode.com/article/make-a-mac-address-changer-in-python). ([code](ethical-hacking/mac-address-changer))
3738

3839
-###[Machine Learning](https://www.thepythoncode.com/topic/machine-learning)
3940
-###[Natural Language Processing](https://www.thepythoncode.com/topic/nlp)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#[How to Make a MAC Address Changer in Python](https://www.thepythoncode.com/article/make-a-mac-address-changer-in-python)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
importsubprocess
2+
importstring
3+
importrandom
4+
importre
5+
6+
7+
defget_random_mac_address():
8+
"""Generate and return a MAC address in the format of Linux"""
9+
# get the hexdigits uppercased
10+
uppercased_hexdigits=''.join(set(string.hexdigits.upper()))
11+
# 2nd character must be 0, 2, 4, 6, 8, A, C, or E
12+
mac=""
13+
foriinrange(6):
14+
forjinrange(2):
15+
ifi==0:
16+
mac+=random.choice("02468ACE")
17+
else:
18+
mac+=random.choice(uppercased_hexdigits)
19+
mac+=":"
20+
returnmac.strip(":")
21+
22+
23+
defget_current_mac_address(iface):
24+
# use the ifconfig command to get the interface details, including the MAC address
25+
output=subprocess.check_output(f"ifconfig{iface}",shell=True).decode()
26+
returnre.search("ether (.+) ",output).group().split()[1].strip()
27+
28+
29+
30+
defchange_mac_address(iface,new_mac_address):
31+
# disable the network interface
32+
subprocess.check_output(f"ifconfig{iface} down",shell=True)
33+
# change the MAC
34+
subprocess.check_output(f"ifconfig{iface} hw ether{new_mac_address}",shell=True)
35+
# enable the network interface again
36+
subprocess.check_output(f"ifconfig{iface} up",shell=True)
37+
38+
39+
if__name__=="__main__":
40+
importargparse
41+
parser=argparse.ArgumentParser(description="Python Mac Changer on Linux")
42+
parser.add_argument("interface",help="The network interface name on Linux")
43+
parser.add_argument("-r","--random",action="store_true",help="Whether to generate a random MAC address")
44+
parser.add_argument("-m","--mac",help="The new MAC you want to change to")
45+
args=parser.parse_args()
46+
iface=args.interface
47+
ifargs.random:
48+
# if random parameter is set, generate a random MAC
49+
new_mac_address=get_random_mac_address()
50+
elifargs.mac:
51+
# if mac is set, use it instead
52+
new_mac_address=args.mac
53+
# get the current MAC address
54+
old_mac_address=get_current_mac_address(iface)
55+
print("[*] Old MAC address:",old_mac_address)
56+
# change the MAC address
57+
change_mac_address(iface,new_mac_address)
58+
# check if it's really changed
59+
new_mac_address=get_current_mac_address(iface)
60+
print("[+] New MAC address:",new_mac_address)
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
importsubprocess
2+
importregexasre
3+
importstring
4+
importrandom
5+
6+
# the registry path of network interfaces
7+
network_interface_reg_path=r"HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e972-e325-11ce-bfc1-08002be10318}"
8+
# the transport name regular expression, looks like {AF1B45DB-B5D4-46D0-B4EA-3E18FA49BF5F}
9+
transport_name_regex=re.compile("{.+}")
10+
# the MAC address regular expression
11+
mac_address_regex=re.compile(r"([A-Z0-9]{2}[:-]){5}([A-Z0-9]{2})")
12+
13+
defget_random_mac_address():
14+
"""Generate and return a MAC address in the format of WINDOWS"""
15+
# get the hexdigits uppercased
16+
uppercased_hexdigits=''.join(set(string.hexdigits.upper()))
17+
# 2nd character must be 2, 4, A, or E
18+
returnrandom.choice(uppercased_hexdigits)+random.choice("24AE")+"".join(random.sample(uppercased_hexdigits,k=10))
19+
20+
21+
defclean_mac(mac):
22+
"""Simple function to clean non hexadecimal characters from a MAC address
23+
mostly used to remove '-' and ':' from MAC addresses and also uppercase it"""
24+
return"".join(cforcinmacifcinstring.hexdigits).upper()
25+
26+
27+
defget_connected_adapters_mac_address():
28+
# make a list to collect connected adapter's MAC addresses along with the transport name
29+
connected_adapters_mac= []
30+
# use the getmac command to extract
31+
forpotential_macinsubprocess.check_output("getmac").decode().splitlines():
32+
# parse the MAC address from the line
33+
mac_address=mac_address_regex.search(potential_mac)
34+
# parse the transport name from the line
35+
transport_name=transport_name_regex.search(potential_mac)
36+
ifmac_addressandtransport_name:
37+
# if a MAC and transport name are found, add them to our list
38+
connected_adapters_mac.append((mac_address.group(),transport_name.group()))
39+
returnconnected_adapters_mac
40+
41+
42+
defget_user_adapter_choice(connected_adapters_mac):
43+
# print the available adapters
44+
fori,optioninenumerate(connected_adapters_mac):
45+
print(f"#{i}:{option[0]},{option[1]}")
46+
iflen(connected_adapters_mac)<=1:
47+
# when there is only one adapter, choose it immediately
48+
returnconnected_adapters_mac[0]
49+
# prompt the user to choose a network adapter index
50+
try:
51+
choice=int(input("Please choose the interface you want to change the MAC address:"))
52+
# return the target chosen adapter's MAC and transport name that we'll use later to search for our adapter
53+
# using the reg QUERY command
54+
returnconnected_adapters_mac[choice]
55+
except:
56+
# if -for whatever reason- an error is raised, just quit the script
57+
print("Not a valid choice, quitting...")
58+
exit()
59+
60+
61+
defchange_mac_address(adapter_transport_name,new_mac_address):
62+
# use reg QUERY command to get available adapters from the registry
63+
output=subprocess.check_output(f"reg QUERY "+network_interface_reg_path.replace("\\\\","\\")).decode()
64+
forinterfaceinre.findall(rf"{network_interface_reg_path}\\\d+",output):
65+
# get the adapter index
66+
adapter_index=int(interface.split("\\")[-1])
67+
interface_content=subprocess.check_output(f"reg QUERY{interface.strip()}").decode()
68+
ifadapter_transport_nameininterface_content:
69+
# if the transport name of the adapter is found on the output of the reg QUERY command
70+
# then this is the adapter we're looking for
71+
# change the MAC address using reg ADD command
72+
changing_mac_output=subprocess.check_output(f"reg add{interface} /v NetworkAddress /d{new_mac_address} /f").decode()
73+
# print the command output
74+
print(changing_mac_output)
75+
# break out of the loop as we're done
76+
break
77+
# return the index of the changed adapter's MAC address
78+
returnadapter_index
79+
80+
81+
defdisable_adapter(adapter_index):
82+
# use wmic command to disable our adapter so the MAC address change is reflected
83+
disable_output=subprocess.check_output(f"wmic path win32_networkadapter where index={adapter_index} call disable").decode()
84+
returndisable_output
85+
86+
87+
defenable_adapter(adapter_index):
88+
# use wmic command to enable our adapter so the MAC address change is reflected
89+
enable_output=subprocess.check_output(f"wmic path win32_networkadapter where index={adapter_index} call enable").decode()
90+
returnenable_output
91+
92+
93+
if__name__=="__main__":
94+
importargparse
95+
parser=argparse.ArgumentParser(description="Python Windows MAC changer")
96+
parser.add_argument("-r","--random",action="store_true",help="Whether to generate a random MAC address")
97+
parser.add_argument("-m","--mac",help="The new MAC you want to change to")
98+
args=parser.parse_args()
99+
ifargs.random:
100+
# if random parameter is set, generate a random MAC
101+
new_mac_address=get_random_mac_address()
102+
elifargs.mac:
103+
# if mac is set, use it after cleaning
104+
new_mac_address=clean_mac(args.mac)
105+
106+
connected_adapters_mac=get_connected_adapters_mac_address()
107+
old_mac_address,target_transport_name=get_user_adapter_choice(connected_adapters_mac)
108+
print("[*] Old MAC address:",old_mac_address)
109+
adapter_index=change_mac_address(target_transport_name,new_mac_address)
110+
print("[+] Changed to:",new_mac_address)
111+
disable_adapter(adapter_index)
112+
print("[+] Adapter is disabled")
113+
enable_adapter(adapter_index)
114+
print("[+] Adapter is enabled again")

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp