-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspoofmac.py
94 lines (72 loc) · 2.36 KB
/
spoofmac.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
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python3
"""Spoof MAC
Spoofing a MAC address with Python
Usage: $ python3 spoofmac.py
"""
import argparse
import os
import random
import re
import subprocess
import sys
import time
def is_root() -> bool:
"""Checks if root user
Returns:
bool: True if root else False
"""
return os.geteuid() == 0
def get_rand() -> str:
"""Gets random valid MAC char
Returns:
str: A single char
"""
return random.choice("abcdef0123456789")
def new_mac() -> str:
"""Creates a new MAC address
Returns:
str: A complete valid MAC string
"""
return ":".join(get_rand() + get_rand() for _ in range(6))
def get_mac(interface: str) -> str:
"""Gets the MAC address
Args:
interface (str): The ethernet interface to get
Returns:
str: A complete valid MAC string
"""
output = subprocess.check_output(["ifconfig", interface])
mac = re.findall("([0-9a-f]{2}(?::[0-9a-f]{2}){5})", str(output))[0]
return mac
def change_mac(mac: str, interface: str) -> None:
"""Creates a new MAC address and changes it using subprocess
Args:
mac (str): A provided MAC address
interface (str): The ethernet interface to change
"""
subprocess.call(["sudo", "ifconfig", interface, "down"])
subprocess.call(["sudo", "ifconfig", interface, "hw", "ether", mac])
subprocess.call(["sudo", "ifconfig", interface, "up"])
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Create a new MAC address")
parser.add_argument("-i", "--interface", help="name of the interface")
args = parser.parse_args()
if not is_root():
print('Must be <root> to run, please try using "sudo python3"...')
sys.exit(1)
interface = args.interface if args.interface else "eth0"
current_mac = get_mac(interface)
print(f"Current MAC: {current_mac}")
try:
while True:
mac = new_mac()
change_mac(mac, interface)
summary = subprocess.check_output(["ifconfig", interface])
if mac in str(summary):
print(f"New MAC: {get_mac(interface)}")
break
time.sleep(10)
except KeyboardInterrupt:
change_mac("00:0c:29:8d:a2:64", interface)
print("Interrupt detected, exiting...")
print(f"Changing to original MAC: {get_mac(interface)}")