Pythonの倫理的ハッキング - MACアドレス&方法を変更する(3)

単純なアルゴリズム

ゴール  - > MACアドレスが変更されたかどうかを確認してください。

ステップ:

1. ifconfigコマンドを実行してください。

2.出力からMACアドレスをお読みください。

ifconfigコマンドでMAC場合3.チェックは、ユーザーが要求したものです。

4.印刷適切なメッセージ。

 

MACアドレスを確認するには、我々はPythexツールを使用することができます。

https://docs.python.org/2.7/library/re.html

https://pythex.org/?regex=%5Cw%5Cw%3A%5Cw%5Cw%3A%5Cw%5Cw%3A%5Cw%5Cw%3A%5Cw%5Cw%3A%5Cw%5Cw&test_string=eth0%3A% 20flags%3D4163%3CUP%2CBROADCAST%2CRUNNING%2CMULTICAST%3E%、20%20mtu%20.15万%0Aの%20%20%20%20%20%20%20%20inetの%2010.0.0.37%、20%20netmaskの%20255.255.255.0% 20%20broadcast%2010.0.0.255%0Aの%20%20%20%20%20%20%20%20inet6%20fe80%3A%の3A211%3A22ffの%3Afe33%3A4411%、20%20prefixlen%2064パーセント20%20scopeid%200x20% 3Clink%3E%0A%20%20%20%20%20%20%20%20ether%2000パーセント3A11%3A22%3A33%3A44%3A11%20%20txqueuelen%20.1万%20%20(イーサネット)%の0A%20 %20386903781%20(368.9パーセント20MiB)%20%20%20%20%20%20%20RX%20packets%20303175% 20%20bytes%の0A%20%20%20%20%20%20%20%20RX% 20errors%200%20%20dropped%、200%、20%20overruns%、200%、20%20frame%200%の0A%20%20%20%20%20%20%20%20TX%20packets%2012392% 20%20bytes%201023481% 20(999。4%20KiB)%の0Aの%20%20%20%20%20%20%20%20TX%20errors%、200%、20%20dropped%200%20overruns%、200%、20%20carrier%、200%、20%20collisions%、200%の0A&IGNORECASE = 0&マルチライン= 0&DOTALL = 0&冗長= 0

 

 

EX: Python Code 

#!/usr/bin/env python

import subprocess
import optparse
import re

def get_arguments():
    parser = optparse.OptionParser()
    parser.add_option("-i", "--interface", dest="interface", help="Interface to change its MAC address")
    parser.add_option("-m", "--mac", dest="new_mac", help="New MAC address")
    (options, arguments) = parser.parse_args()
    if not options.interface:
        parser.error("[-] Please specify an interface, use --help for more info.")
    elif not options.new_mac:
        parser.error("[-] Please specify a new mac, use --help for more info.")
    return options

def change_mac(interface, new_mac):
    print("[+] Changing MAC address for " + interface + " to " + new_mac)
    subprocess.call(["ifconfig", interface, "down"])
    subprocess.call(["ifconfig", interface, "hw", "ether", new_mac])
    subprocess.call(["ifconfig", interface, "up"])

def get_current_mac(interface):
    ifconfig_result = subprocess.check_output(["ifconfig", interface])
    mac_address_search_result = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w", ifconfig_result)

    if mac_address_search_result:
        return mac_address_search_result.group(0)
    else:
        print("[-] Could not read MAC address.")

options = get_arguments()

current_mac = get_current_mac(options.interface)
print("Current MAC = " + str(current_mac))

change_mac(options.interface, options.new_mac)

current_mac = get_current_mac(options.interface)
if current_mac == options.new_mac:
    print("[+] MAC address was successfully changed to " + current_mac)
else:
    print("[-] MAC address did not get changed.")

 

Execute the following command to test the Python code:

python mac_changer.py -i eth0 -m 00:11:22:33:44:55

 

 

おすすめ

転載: www.cnblogs.com/keepmoving1113/p/11334210.html