import argparse
import socket
import requests
from scapy.all import ARP, Ether, srp

def get_user_input():
    print("Choose the target IP range:")
    print("1. /24: 256 IP addresses (2^8), commonly used for small networks.")
    print("2. /23: 512 IP addresses (2^9), suitable for a medium-sized network.")
    print("3. /22: 1,024 IP addresses (2^10), larger than /23.")
    print("4. /21: 2,048 IP addresses (2^11), larger than /22.")
    print("5. Custom")

    choice = input("Enter your choice (1/2/3/4/5): ")

    if choice == "1":
        return "192.168.1.1/24"
    elif choice == "2":
        return "192.168.1.1/23"
    elif choice == "3":
        return "192.168.1.1/22"
    elif choice == "4":
        return "192.168.1.1/21"
    elif choice == "5":
        custom_range = input("Enter custom IP range (e.g., 192.168.1.1/20): ")
        return custom_range
    else:
        print("Using default range (192.168.1.1/24).")
        return "192.168.1.1/24"

def process_mac_addresses(mac_addresses):
    # Initialize lists
    real_vendor_info = []

    # Process each MAC address
    for client in mac_addresses:
        mac_address = client['mac']
        vendor_info = get_vendor_info(mac_address)
        dns_name = get_dns_name(client['ip'])

        print(f"IP Address: \033[33m{client['ip']}\033[0m | MAC Address: {mac_address} | Vendor: {vendor_info} | DNS Name: {dns_name}")

        if "Error:" not in vendor_info and vendor_info != "*NO COMPANY*" and not mac_address.startswith(
                ("01:00:5E:", "33:33:")) and mac_address != "00:00:00:00:00:00" and mac_address != "FF:FF:FF:FF:FF:FF":
            real_vendor_info.append(vendor_info)

    if real_vendor_info:
        print("\n--- General Report ---")
        print(f"Total Number of Devices Found: \033[96m{len(mac_addresses)}\033[0m")  # Cyan text
        print(f"Total Number of *NO COMPANY*: \033[94m{real_vendor_info.count('*NO COMPANY*')}\033[0m")  # Blue text
        print(f"Total Number of Errors: \033[91m{mac_addresses.count('Error:')}\033[0m")  # Red text

def get_vendor_info(mac_address):
    api_url = f"https://api.maclookup.app/v2/macs/{mac_address}/company/name"
    try:
        response = requests.get(api_url)
        if response.status_code == 200:
            vendor_info = response.text.strip()
            return f"\033[92m{vendor_info}\033[0m" if vendor_info != "*NO COMPANY*" else f"\033[94m{vendor_info}\033[0m"  # Green text for other vendors, blue for *NO COMPANY*
        else:
            return f"\033[91mError: for {mac_address}\033[0m"  # Red text for errors
    except requests.exceptions.RequestException as e:
        return f"\033[91mError: {e}\033[0m"  # Red text for errors

def get_dns_name(ip_address):
    try:
        # Perform a reverse DNS lookup
        hostname, _, _ = socket.gethostbyaddr(ip_address)
        return f"\033[94m{hostname}\033[0m" if hostname != ip_address else "Unable to resolve"  # Blue text for resolved DNS names, regular text for IP address
    except socket.herror:
        return "\033[94mUnable to resolve\033[0m"  # Blue text for "Unable to resolve"

def get_mac_addresses(target_ip):
    try:
        # create ARP packet
        arp = ARP(pdst=target_ip)
        # create the Ether broadcast packet
        # ff:ff:ff:ff:ff:ff MAC address indicates broadcasting
        ether = Ether(dst="ff:ff:ff:ff:ff:ff")
        # stack them
        packet = ether / arp

        result = srp(packet, timeout=3, verbose=0)[0]

        # a list of clients, we will fill this in the upcoming loop
        clients = []

        for sent, received in result:
            # for each response, append ip and mac address to `clients` list
            clients.append({'ip': received.psrc, 'mac': received.hwsrc})

        return clients
    except Exception as e:
        print(f"\033[91mError getting MAC addresses: {e}\033[0m")  # Red text for errors
        return []

def main():
    # Get target IP range from user
    target_ip = get_user_input()

    # Get MAC addresses
    mac_addresses = get_mac_addresses(target_ip)

    if not mac_addresses:
        print("No MAC addresses found.")
        return

    # Process MAC addresses
    process_mac_addresses(mac_addresses)

if __name__ == "__main__":
    main()

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *