Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.94 KB | None | 0 0
  1. #Packet sniffer in python
  2. #For Linux - Sniffs all incoming and outgoing packets :)
  3. #Silver Moon ([email protected])
  4. #credits: http://www.binarytides.com/python-packet-sniffer-code-linux/
  5.  
  6. import socket, sys
  7. from struct import *
  8.  
  9. #Convert a string of 6 characters of ethernet address into a dash separated hex string
  10. def eth_addr (a) :
  11.   b = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" % (ord(a[0]) , ord(a[1]) , ord(a[2]), ord(a[3]), ord(a[4]) , ord(a[5]))
  12.   return b
  13.  
  14. #create a AF_PACKET type raw socket (thats basically packet level)
  15. #define ETH_P_ALL    0x0003          /* Every packet (be careful!!!) */
  16. try:
  17.     s = socket.socket( socket.AF_PACKET , socket.SOCK_RAW , socket.ntohs(0x0003))
  18. except socket.error as msg:
  19.     print('Socket could not be created. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
  20.     sys.exit()
  21.  
  22. # receive a packet
  23. while True:
  24.     packet = s.recvfrom(65565)
  25.    
  26.     #packet string from tuple
  27.     packet = packet[0]
  28.    
  29.     #parse ethernet header
  30.     eth_length = 14
  31.    
  32.     eth_header = packet[:eth_length]
  33.     eth = unpack('!6s6sH' , eth_header)
  34.     eth_protocol = socket.ntohs(eth[2])
  35.     print('Destination MAC : ' + eth_addr(packet[0:6]) + ' Source MAC : ' + eth_addr(packet[6:12]) + ' Protocol : ' + str(eth_protocol))
  36.  
  37.     #Parse IP packets, IP Protocol number = 8
  38.     if eth_protocol == 8 :
  39.         #Parse IP header
  40.         #take first 20 characters for the ip header
  41.         ip_header = packet[eth_length:20+eth_length]
  42.        
  43.         #now unpack them :)
  44.         iph = unpack('!BBHHHBBH4s4s' , ip_header)
  45.  
  46.         version_ihl = iph[0]
  47.         version = version_ihl >> 4
  48.         ihl = version_ihl & 0xF
  49.  
  50.         iph_length = ihl * 4
  51.  
  52.         ttl = iph[5]
  53.         protocol = iph[6]
  54.         s_addr = socket.inet_ntoa(iph[8]);
  55.         d_addr = socket.inet_ntoa(iph[9]);
  56.  
  57.         print('Version : ' + str(version) + ' IP Header Length : ' + str(ihl) + ' TTL : ' + str(ttl) + ' Protocol : ' + str(protocol) + ' Source Address : ' + str(s_addr) + ' Destination Address : ' + str(d_addr))
  58.  
  59.         #TCP protocol
  60.         if protocol == 6:
  61.             t = iph_length + eth_length
  62.             tcp_header = packet[t:t+20]
  63.  
  64.             #now unpack them :)
  65.             tcph = unpack('!HHLLBBHHH' , tcp_header)
  66.            
  67.             source_port = tcph[0]
  68.             dest_port = tcph[1]
  69.             sequence = tcph[2]
  70.             acknowledgement = tcph[3]
  71.             doff_reserved = tcph[4]
  72.             tcph_length = doff_reserved >> 4
  73.             #hostname = socket.gethostbyaddr(d_addr)
  74.  
  75.             print('Source Port : ' + str(source_port) + ' Dest Port : ' + str(dest_port) + ' Sequence Number : ' + str(sequence) + ' Acknowledgement : ' + str(acknowledgement) + ' TCP header length : ' + str(tcph_length))
  76.            
  77.             h_size = eth_length + iph_length + tcph_length * 4
  78.             data_size = len(packet) - h_size
  79.            
  80.             #get data from the packet
  81.             data = packet[h_size:]
  82.            
  83.             print('Data : ' + data)
  84.  
  85.         #ICMP Packets
  86.         elif protocol == 1 :
  87.             u = iph_length + eth_length
  88.             icmph_length = 4
  89.             icmp_header = packet[u:u+4]
  90.  
  91.             #now unpack them :)
  92.             icmph = unpack('!BBH' , icmp_header)
  93.            
  94.             icmp_type = icmph[0]
  95.             code = icmph[1]
  96.             checksum = icmph[2]
  97.            
  98.             print('Type : ' + str(icmp_type) + ' Code : ' + str(code) + ' Checksum : ' + str(checksum))
  99.            
  100.             h_size = eth_length + iph_length + icmph_length
  101.             data_size = len(packet) - h_size
  102.            
  103.             #get data from the packet
  104.             data = packet[h_size:]
  105.            
  106.             print('Data : ' + data)
  107.  
  108.         #UDP packets
  109.         elif protocol == 17 :
  110.             u = iph_length + eth_length
  111.             udph_length = 8
  112.             udp_header = packet[u:u+8]
  113.  
  114.             #now unpack them :)
  115.             udph = unpack('!HHHH' , udp_header)
  116.            
  117.             source_port = udph[0]
  118.             dest_port = udph[1]
  119.             length = udph[2]
  120.             checksum = udph[3]
  121.            
  122.             print('Source Port : ' + str(source_port) + ' Dest Port : ' + str(dest_port) + ' Length : ' + str(length) + ' Checksum : ' + str(checksum))
  123.            
  124.             h_size = eth_length + iph_length + udph_length
  125.             data_size = len(packet) - h_size
  126.            
  127.             #get data from the packet
  128.             data = packet[h_size:]
  129.            
  130.             print('Data : ' + data)
  131.  
  132.         #some other IP packet like IGMP
  133.         else :
  134.             print('Protocol other than TCP/UDP/ICMP')
  135.            
  136.         print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement