Python Ethical Hacking - MAC Address & How to Change(3)

SIMPLE ALGORITHM

Goal  -> Check if MAC address was changed.

Steps:

1. Execute and read ifconfig.

2. Read the mac address from the output.

3. Check if MAC in ifconfig is what the user requested.

4. Print appropriate message.

To find the MAC address, we can use the Pythex tool.

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%201500%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%201000%20%20(Ethernet)%0A%20%20%20%20%20%20%20%20RX%20packets%20303175%20%20bytes%20386903781%20(368.9%20MiB)%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&multiline=0&dotall=0&verbose=0

Python Ethical Hacking - MAC Address & How to Change(3)

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 :::::

Python Ethical Hacking - MAC Address & How to Change(3)

上一篇:《Linux命令行与shell脚本编程大全》- 读书笔记1 - 基本的bash shell 命令


下一篇:用Python分析5万+个WordPress插件