Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.88 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 , 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.            
  74.             print 'Source Port : ' + str(source_port) + ' Dest Port : ' + str(dest_port) + ' Sequence Number : ' + str(sequence) + ' Acknowledgement : ' + str(acknowledgement) + ' TCP header length : ' + str(tcph_length)
  75.            
  76.             h_size = eth_length + iph_length + tcph_length * 4
  77.             data_size = len(packet) - h_size
  78.            
  79.             #get data from the packet
  80.             data = packet[h_size:]
  81.            
  82.             print 'Data : ' + data
  83.  
  84.         #ICMP Packets
  85.         elif protocol == 1 :
  86.             u = iph_length + eth_length
  87.             icmph_length = 4
  88.             icmp_header = packet[u:u+4]
  89.  
  90.             #now unpack them :)
  91.             icmph = unpack('!BBH' , icmp_header)
  92.            
  93.             icmp_type = icmph[0]
  94.             code = icmph[1]
  95.             checksum = icmph[2]
  96.            
  97.             print 'Type : ' + str(icmp_type) + ' Code : ' + str(code) + ' Checksum : ' + str(checksum)
  98.            
  99.             h_size = eth_length + iph_length + icmph_length
  100.             data_size = len(packet) - h_size
  101.            
  102.             #get data from the packet
  103.             data = packet[h_size:]
  104.            
  105.             print 'Data : ' + data
  106.  
  107.         #UDP packets
  108.         elif protocol == 17 :
  109.             u = iph_length + eth_length
  110.             udph_length = 8
  111.             udp_header = packet[u:u+8]
  112.  
  113.             #now unpack them :)
  114.             udph = unpack('!HHHH' , udp_header)
  115.            
  116.             source_port = udph[0]
  117.             dest_port = udph[1]
  118.             length = udph[2]
  119.             checksum = udph[3]
  120.            
  121.             print 'Source Port : ' + str(source_port) + ' Dest Port : ' + str(dest_port) + ' Length : ' + str(length) + ' Checksum : ' + str(checksum)
  122.            
  123.             h_size = eth_length + iph_length + udph_length
  124.             data_size = len(packet) - h_size
  125.            
  126.             #get data from the packet
  127.             data = packet[h_size:]
  128.            
  129.             print 'Data : ' + data
  130.  
  131.         #some other IP packet like IGMP
  132.         else :
  133.             print 'Protocol other than TCP/UDP/ICMP'
  134.            
  135.         print
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement