Skip to content

Commit

Permalink
Update macmaster.py
Browse files Browse the repository at this point in the history
  • Loading branch information
HalilDeniz authored Dec 8, 2023
1 parent 7b3e501 commit c53db5c
Showing 1 changed file with 82 additions and 5 deletions.
87 changes: 82 additions & 5 deletions macmaster/macmaster.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@
import os
import re
import random
import requests
import argparse
import netifaces
import subprocess
from scapy.all import sniff
from scapy.layers.inet import IP
from colorama import Fore, Style, init

from utils.version import __version__



init() # Colorama'yı başlat


VERSION = "1.0.0"
def packet_callback(packet):
try:
source_mac = packet.src
destination_mac = packet.dst
source_ip = packet[IP].src if IP in packet else "No IP"
destination_ip = packet[IP].dst if IP in packet else "No IP"
print(f"{Fore.GREEN}Source Mac :{Style.RESET_ALL} {source_mac} ==> {Fore.GREEN}Source IP:{Style.RESET_ALL} {source_ip}")
print(f"{Fore.GREEN}Destination Mac:{Style.RESET_ALL} {destination_mac} ==> {Fore.GREEN}Source IP:{Style.RESET_ALL} {destination_ip}")
except:
pass
def start_traffic_monitoring(interface):
print(f"{Fore.BLUE}Monitoring network traffic on{Style.RESET_ALL} {Fore.GREEN}{interface}...{Style.RESET_ALL}")
sniff(iface=interface, prn=packet_callback, store=False)

def show_version():
print(f"MacMaster Version: {VERSION}")
print(f"MacMaster Version: {__version__}")

def list_network_interfaces():
interfaces = netifaces.interfaces()
index = 0
print("Availables:")
for interface in interfaces:
index += 1
print(f"\t{Fore.CYAN}{index}){Style.RESET_ALL} {interface}")


def save_original_mac(interface):
Expand Down Expand Up @@ -70,7 +98,14 @@ def validate_mac(mac_address):
return False
return True


def restart_network_services():
try:
print(f"{Fore.BLUE}Restarting network services...{Style.RESET_ALL}")
subprocess.check_call(["sudo", "service", "networking", "restart"])
subprocess.check_call(["sudo", "service", "NetworkManager", "restart"])
print(f"{Fore.MAGENTA}Network services have been restarted successfully.{Style.RESET_ALL}")
except subprocess.CalledProcessError as e:
print(f"{Fore.RED}Failed to restart network services:{Style.RESET_ALL} {e}")

def change_mac(interface, new_mac):
original_mac = get_original_mac(interface)
Expand All @@ -84,6 +119,26 @@ def change_mac(interface, new_mac):
print(f"{Fore.MAGENTA}MAC address changed successfully.{Style.RESET_ALL}")


def is_wireless_interface(interface):
try:
iwconfig_output = subprocess.check_output(["iwconfig", interface], stderr=subprocess.STDOUT, text=True)
return 'no wireless extensions' not in iwconfig_output
except subprocess.CalledProcessError:
return False

def change_interface_mode(interface, mode):
if not is_wireless_interface(interface):
print(f"{Fore.RED}{interface}{Style.RESET_ALL} is not a wireless interface or not supported.")
return False
try:
subprocess.check_call(["sudo", "ifconfig", interface, "down"])
subprocess.check_call(["sudo", "iwconfig", interface, "mode", mode])
subprocess.check_call(["sudo", "ifconfig", interface, "up"])
print(f"Interface {Fore.GREEN}{interface}{Style.RESET_ALL} has been set to {Fore.GREEN}{mode}{Style.RESET_ALL} mode.")
return True
except:
print(f"{Fore.RED}Error occurred while changing mode of {interface}{Style.RESET_ALL}")
return False

def validate_interface(interface):
result = subprocess.run(["ifconfig", interface], capture_output=True, text=True)
Expand All @@ -96,20 +151,35 @@ def validate_interface(interface):
def main():
parser = argparse.ArgumentParser(description='MacMaster: Mac Address Changer')
parser.add_argument("--interface", "-i", help="Network interface to change MAC address")
parser.add_argument("--list-interfaces", "-li", action="store_true", help="List all network interfaces")
parser.add_argument("--version", "-V", action="store_true", help="Show the version of the program")
group = parser.add_mutually_exclusive_group(required=False)
group = parser.add_mutually_exclusive_group()
group.add_argument("--random", "-r", action="store_true", help="Set a random MAC address")
group.add_argument("--newmac", "-nm", type=str, help="Set a specific MAC address")
group.add_argument("--customoui", "-co", type=str, help="Set a custom OUI for the MAC address")
group.add_argument("--reset", "-rs", action="store_true", help="Reset MAC address to the original value")
parser.add_argument("--mode", type=str, choices=['managed', 'monitor','master','auto','repeater'], help="Change interface mode to managed or monitor")
parser.add_argument("--restart-network", "-rn", action="store_true", help="Restart network services")
parser.add_argument("--monitor-mac-traffic", "-mmt", action="store_true", help="Live Monitor Mac traffic")


args = parser.parse_args()

if args.version:
show_version()
exit()
if args.list_interfaces:
list_network_interfaces()
exit()
if args.restart_network:
restart_network_services()
exit()
if args.monitor_mac_traffic:
start_traffic_monitoring(args.interface)

if not args.interface:
parser.error("the following arguments are required: --interface/-i")
parser.print_help()
exit()

validate_interface(args.interface)
save_original_mac(args.interface)
Expand All @@ -136,5 +206,12 @@ def main():
else:
print(f"{Fore.RED}Unable to reset the MAC address. Original MAC address not found.{Style.RESET_ALL}")

if args.mode:
success = change_interface_mode(args.interface, args.mode)
if success:
print(f"{Fore.GREEN}Mode change to {args.mode} was successful.{Style.RESET_ALL}")
else:
print(f"{Fore.RED}Failed to change mode to {args.mode}.{Style.RESET_ALL}")

if __name__ == "__main__":
main()

0 comments on commit c53db5c

Please sign in to comment.