1
+ # MAC Address Changer in Python
2
+ # The commands required to change the mac address from the terminal:
3
+ # ifconfig eth0 down (To switch off the eth0 connection)
4
+ # ifconfig eth0 hw ether "MAC ADDRESS ASSIGNED" (hw denotes Hardware)
5
+ # ifconfig eth0 up (Switch the service back on)
6
+
7
+ # A module called "subprocess" is used to execute linux(any) system commands depending on the OS.
8
+ # We use a function of module subprocess called "call" that runs the system commands in the foreground
9
+ # and waits for the command to finish before executing the next command
10
+ # Syntax = subprocess.call("COMMAND",Shell=True)
11
+
12
+ # SIOCSIFFLAGS->Operation not permitted : Run the command as Root
13
+ # make sure to run the program as root
14
+
15
+ # optparse helps in passing arguments by taking input from user
16
+ # !/usr/bin/env python3 -> sha-bang
17
+ import subprocess
18
+ import optparse
19
+ import re
20
+
21
+
22
+ def get_arguments ():
23
+ # parser object: knows everything about the arguments and parsing them ie entity that handles user input
24
+ # parser is a variable that holds optparse.OptionParser() class ie an instance is created
25
+ parser = optparse .OptionParser ()
26
+ parser .add_option ("-i" , "--interface" , dest = "interface" , help = "Interface to change it's MAC address" )
27
+ parser .add_option ("-m" , "--mac" , dest = "new_mac" , help = "New MAC address" )
28
+ (option , arguments ) = parser .parse_args ()
29
+ if not option .interface :
30
+ parser .error ("Please specify an interface and use --help for more information" )
31
+ elif not option .new_mac :
32
+ parser .error ("Please specify a new_mac and use --help for more information" )
33
+ return option
34
+
35
+
36
+ # options : value of input whose destination is given and arguments are for -i and -- interface etc.
37
+
38
+ def change_mac (interface , new_mac ):
39
+ print ("# Changing the MAC Address of " + interface + "to" + new_mac )
40
+ subprocess .call (["ifconfig" , interface , "down" ])
41
+ subprocess .call (["ifconfig" , interface , "hw ether" , new_mac ])
42
+ subprocess .call (["ifconfig" , interface , "up" ])
43
+
44
+
45
+ def get_current_mac (interface ):
46
+ ifconfig_result = subprocess .check_output (["ifconfig" , interface ])
47
+ print (ifconfig_result )
48
+
49
+ # Extracting MAC Address using REGEX
50
+ mac_address_search_result = re .search (r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w" , ifconfig_result )
51
+ if mac_address_search_result :
52
+ return mac_address_search_result .group (0 )
53
+ else :
54
+ print ("Couldn't read MAC address" )
55
+
56
+
57
+ # subprocess.call("ifconfig "+interface+" down", shell=True) is not safe as interface allows
58
+ # any command to be given by the user which makes the tool vulnerable like enter 2 commands
59
+ # (using ; that marks end of command) , Hence we use the second syntax of Subprocess
60
+
61
+ options = get_arguments ()
62
+ current_mac = get_current_mac (options .interface )
63
+ print ("Current MAC = " + str (current_mac ))
64
+ change_mac (options .interface , options .new_mac )
65
+
66
+ current_mac = get_current_mac (options .interface )
67
+ if current_mac == options .new_mac :
68
+ print ("MAC Address was successfully changed to " + current_mac )
69
+ else :
70
+ print ("MAC Address did not change" )
0 commit comments