-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchange_mac_address.py
66 lines (46 loc) · 2.17 KB
/
change_mac_address.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
""" Spoof your current MAC address with a custom MAC address. """
import subprocess
import argparse
import re
import random
RE_MAC_ADDRESS = r"([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})"
# TOOD: check validity provided MAC address
def main():
# Parse arguments
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('interface', type=str,
help='Interface form which to change the MAC address')
parser.add_argument("-m", '--new-mac', type=str, default=None,
help='New MAC address to use for the provided interface')
args = parser.parse_args()
current_mac = get_current_mac_address(args.interface)
if current_mac is None:
print("Could not find a MAC address for interface", args.interface)
exit(2)
change_mac_address(interface=args.interface,
new_mac_address=args.new_mac)
def change_mac_address(interface: str, new_mac_address: str = None):
if new_mac_address is None:
new_mac_address = generate_mac_address()
subprocess.call(["ifconfig", interface, "down"])
subprocess.call(["ifconfig", interface, "hw", "ether", new_mac_address])
subprocess.call(["ifconfig", interface, "up"])
def get_current_mac_address(interface: str):
global RE_MAC_ADDRESS
cmd_output = subprocess.check_output(["ifconfig", interface])
re_search = re.search(RE_MAC_ADDRESS, cmd_output)
if re_search:
return re_search.group(0)
else:
return None
def generate_mac_address(separator: str = ":") -> str:
return "02%s00%s00%s%02x%s%02x%s%02x" % (separator,
separator,
separator,
random.randint(0, 255),
separator,
random.randint(0, 255),
separator,
random.randint(0, 255))
if __name__ == '__main__':
main()