📜  更改Linux机器MAC地址的Python脚本

📅  最后修改于: 2022-05-13 01:55:16.996000             🧑  作者: Mango

更改Linux机器MAC地址的Python脚本

什么是 MAC 地址:在计算机网络中,MAC 地址用于最低网络级别,网络接口相互通信。在此处查看详细信息。

需要更改 MAC 地址:

  • 绕过 MAC 地址过滤
  • 绕过某种 MITM 欺骗攻击
  • 避免在公共网络中跟踪设备

还有许多其他任务,例如在网络中变得匿名,并避免某些更改 MAC 地址变得有用的网络攻击。

在 Linux 机器中更改 MAC 地址:在 Linux 中,更改 MAC 地址的一种简单方法是使用ifconfig命令。
如果尚未安装,我们可以使用以下命令安装它:

sudo apt-get update
sudo apt-get install ifconfig

安装此软件包后,我们可以使用以下命令更改 MAC 地址:

sudo ifconfig  down
sudo ifconfig  hw ether  
sudo ifconfig  up

我们可以使用以下命令查看接口列表:

ifconfig

使用Python自动化:由于我们不可能每次都手动更改 MAC 地址,因此我们可以使用Python自动化更改 MAC 地址的过程。此脚本将在恒定时间内不断更改 MAC 地址。
下面是上述想法的实现。

# Python Script to change MAC Address
  
# Import Useful modules
import sys
import subprocess
import argparse
import random
import time
import re
  
  
# Function to get the interface name
def get_arguments():
    # This will give user a neat CLI
    parser = argparse.ArgumentParser()
    # We need the interface name
    parser.add_argument("-i", "--interface",
                dest="interface",
                help="Name of the interface. "
                "Type ifconfig for more details.")
    options = parser.parse_args()
    # Check if interface was given
    if options.interface:
        return options.interface
    else:
        parser.error("[!] Invalid Syntax. "
                     "Use --help for more details.")
  
  
# Function to change the MAC Address
def change_mac(interface, new_mac_address):
    # As explained above, these lines will
    # execute these commands for us
    subprocess.call(["sudo", "ifconfig", interface,
                     "down"])
    subprocess.call(["sudo", "ifconfig", interface,
                     "hw", "ether", new_mac_address])
    subprocess.call(["sudo", "ifconfig", interface,
                     "up"])
  
# Function to generate a random MAC Address
def get_random_mac_address():
    characters = "0123456789abcdef"
    random_mac_address = "00"
    for i in range(5):
        random_mac_address += ":" + \
                        random.choice(characters) \
                        + random.choice(characters)
    return random_mac_address
  
# Function to get the current MAC Address
# We will use it restore MAC address
# in case something goes wrong.
def get_current_mac(interface):
    output = subprocess.check_output(["ifconfig",
                                      interface])
    return re.search("\w\w:\w\w:\w\w:\w\w:\w\w:\w\w",
                  str(output)).group(0)
  
# Driver Program
if __name__ == "__main__":
    print("[* Welcome to MAC ADDRESS Changer *]")
    print("[*] Press CTRL-C to QUIT")
    # Change it to required value(in sec)
    TIME_TO_WAIT = 60
    interface = get_arguments()
    current_mac = get_current_mac(interface)
    try:
        while True:
            random_mac = get_random_mac_address()
            change_mac(interface, random_mac)
            new_mac_summary = subprocess.check_output(
                ["ifconfig", interface])
            if random_mac in str(new_mac_summary):
                print("\r[*] MAC Address Changed to",
                      random_mac,
                      end=" ")
                sys.stdout.flush()
            # Wait for a constant period of time
            time.sleep(TIME_TO_WAIT)
  
    except KeyboardInterrupt:
        # Restore the MAC before quitting.
        change_mac(interface, current_mac)
        print("\n[+] Quitting Program...")

输出: