def sniff_packet(self): packet_raw=self.sock.recvfrom(65565) packet=packet_raw[0] header_length=14 header=unpack('!6s6sH' , packet[0:header_length]) protocol=socket.ntohs(header[2]) if self.verbose_arp or self.verbose_udp or self.verbose_tcp or self.verbose_icmp: print '\n------------------------------------------------' + '\nDestination MAC : ' + self.eth_addr(packet[0:6]) + '\nSource MAC : ' + self.eth_addr(packet[6:12]) + '\nProtocol : ' + self.getProtocol(protocol)+'\n' if protocol==8 and (self.verbose_udp or self.verbose_tcp or self.verbose_icmp): # EGP # Exterior Gateway Protocol # eth_header=unpack('!BBHHHBBH4s4s',packet[header_length:header_length+20]) version = eth_header[0] >> 4 eth_header_length = eth_header[0] & 0xF protocol2 = eth_header[6] string='EGP Version : '+str(version)+'\nIP Header Length : '+str(eth_header_length)+'\nProtocol : '+str(self.getProtocol(protocol2))+'\n' if self.gui: add_logdata(string) else: print string if protocol2 == 1 and self.verbose_icmp: print self.icmp_protocol(packet,header_length+eth_header_length*4) elif protocol2 == 6 and self.verbose_tcp: print self.tcp_protocol(packet,header_length+eth_header_length*4) elif protocol2 == 17 and self.verbose_udp: print self.udp_protocol(packet,header_length+eth_header_length*4) else: pass #print 'not implement this protocol No.',protocol2 elif protocol==1544: print self.arp_protocol(packet) return packet
def icmp_protocol(self,packet,icmp_length): # ICMP # INTERNET CONTROL MESSAGE PROTOCOL # icmp_header = unpack('!BBH' , packet[icmp_length:icmp_length+4]) icmp_type = icmp_header[0] icmp_code = icmp_header[1] icmp_checksum = icmp_header[2] data_start = icmp_length + 4 data_size = len(packet) - data_start icmp_string='ICMP Protocol'+'\nType : '+str(icmp_type)+'\nCode : '+str(icmp_code)+'\nChecksum : '+str(icmp_checksum) if packet[data_start:]: icmp_string+='\nData : '+str(packet[data_start:])+'\n' add_logdata(icmp_string) if self.gui: icmp_string='' return icmp_string
def arp_protocol(self,packet): # ARP # # arp_header=unpack("2s2s1s1s2s6s4s6s4s",packet[14:42]) sip = socket.inet_ntoa(arp_header[6]) smac= self.eth_addr(arp_header[5]) dip = socket.inet_ntoa(arp_header[8]) dmac= self.eth_addr(arp_header[7]) self.map_ip_to_mac(sip,smac) self.map_ip_to_mac(dip,dmac) arp_string='' if self.verbose_arp: arp_string='ARP Protocol'+"\nHardware type : "+str(binascii.hexlify(arp_header[0]))+"\nProtocol type : "+str(binascii.hexlify(arp_header[1]))+"\nHardware size : "+str(binascii.hexlify(arp_header[2]))+"\nProtocol size : "+str(binascii.hexlify(arp_header[3]))+"\nOpcode : "+str(binascii.hexlify(arp_header[4]))+"\nSource MAC : "+str(smac)+"\nSource IP : "+str(sip)+"\nDest MAC : "+str(dmac)+"\nDest IP : "+str(dip)+'\n' add_logdata(arp_string) if self.gui: arp_string='' return arp_string
def udp_protocol(self,packet,udp_length): # UDP # # udp_header = unpack('!HHHH',packet[udp_length:udp_length+8]) source_port = udp_header[0] dest_port = udp_header[1] udp_length = udp_header[2] udp_checksum = udp_header[3] data_start = udp_length + 8 data_size = len(packet) -data_start udp_string='UDP Protocol'+'\nSource Port : '+str(source_port)+'\nDestination Port : '+str(dest_port)+'\nLength : '+str(udp_length)+'\nChecksum : '+str(udp_checksum) if packet[data_start:]: udp_string+='\nData : '+str(packet[data_start:])+'\n' add_logdata(udp_string) if self.gui: udp_string='' return udp_string
def tcp_protocol(self,packet,tcp_length): # TCP # TRANSMISSION CONTROL PROTOCOL # tcp_header = unpack('!HHLLBBHHH' , packet[tcp_length:tcp_length+20]) source_port = tcp_header[0] dest_port = tcp_header[1] sequence_number = tcp_header[2] ack_number = tcp_header[3] data_offset = tcp_header[4] tcp_header_length = data_offset >> 4 data_start = tcp_length + tcp_header_length*4 data_size = len(packet)-data_start tcp_string='TCP Protocol\nSource Port : '+str(source_port)+'\nDestination Port : '+str(dest_port)+'\nSequence Number : '+str(sequence_number)+'\nAcknowledgement Number : '+str(ack_number)+'\nData Offset : '+str(data_offset)+'\nTCP Header Length : '+str(tcp_header_length) if packet[data_start:]: tcp_string+='\nData : '+str(packet[data_start:])+'\n' add_logdata(text) if self.gui: tcp_string='' return tcp_string