def start(self): input_list = [self.sock, sys.stdin] self.running = True self.start_promisc() while self.running: inputready, outputready, exceptready = select.select( input_list, [], []) for s in inputready: if s == self.sock: try: packet = self.sock.recvfrom(65565) except socket.timeout, e: err = e.args[0] # this next if/else is a bit redundant, but illustrates how the # timeout exception is setup if err == 'timed out': sleep(1) print 'recv timed out, retry later' continue else: print e # continue sys.exit(1) except socket.error, e: print e sys.exit(1) else: # forward packet sender = packet[0] packet = packet[0] dest_mac, source_mac, eth_protocol = self.parse_ethernet_header( packet) # self.packetisreceived(eth_protocol) if eth_protocol == 8: print "IP Packet" #ip_packet = ipv4datgram(packet) #ip_packet.parse_ip_packet() self.parse_ip_packet(packet) ip_packet = ipv4datagram(source_mac, dest_mac, eth_protocol, packet) ip_packet.parse_ip_packet(packet) #fire event #self.packetisreceived(ip_packet) self.packetisreceived(ip_packet) # self.running = False # break # continue if s == sys.stdin: dummy = sys.stdin.readline() running = False else: continue
def start(self): input_list = [self.sock,sys.stdin] self.running = True self.start_promisc() while self.running: inputready,outputready,exceptready = select.select(input_list,[],[]) for s in inputready: if s == self.sock: try: packet = self.sock.recvfrom(65565); except socket.timeout, e: err = e.args[0] # this next if/else is a bit redundant, but illustrates how the # timeout exception is setup if err == 'timed out': sleep(1) print 'recv timed out, retry later' continue else: print e # continue sys.exit(1) except socket.error, e: print e sys.exit(1) else: # forward packet sender = packet[0] packet = packet[0] dest_mac, source_mac, eth_protocol = self.parse_ethernet_header(packet) # self.packetisreceived(eth_protocol) if eth_protocol == 8: print "IP Packet" #ip_packet = ipv4datgram(packet) #ip_packet.parse_ip_packet() self.parse_ip_packet(packet) ip_packet = ipv4datagram(source_mac,dest_mac,eth_protocol,packet) ip_packet.parse_ip_packet(packet) #fire event #self.packetisreceived(ip_packet) self.packetisreceived(ip_packet) # self.running = False # break # continue if s == sys.stdin: dummy = sys.stdin.readline() running = False else: continue
def open_packets(self,event): list_packets = self.data_store.readfile(None) for packet in list_packets: dest_mac, source_mac, eth_protocol = self.sniffer_obj.parse_ethernet_header(packet) if eth_protocol == 8: print "Loading IP Packet" # self.sniffer_obj.parse_ip_packet(packet) ip_packet = ipv4datagram(source_mac,dest_mac,eth_protocol,packet) self.data_store.add_packet(ip_packet) data_ = {1: (str(ip_packet.id),str(time.clock()),str(ip_packet.source_addr),str(ip_packet.dest_addr),str(eth_protocol),ip_packet.getprotocol(),str(ip_packet.ttl))} items = data_.items() for key, data in items: index = self.list.InsertStringItem(sys.maxint, data[0]) self.list.SetStringItem(index, 1, data[1]) self.list.SetStringItem(index, 2, data[2]) self.list.SetStringItem(index, 3, data[3]) self.list.SetStringItem(index, 4, data[4]) self.list.SetStringItem(index, 5, data[5]) self.list.SetStringItem(index, 6, data[6])
def OnOpen(self,e): # In this case, the dialog is created within the method because # the directory name, etc, may be changed during the running of the # application. In theory, you could create one earlier, store it in # your frame object and change it when it was called to reflect # current parameters / values dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*|*", wx.OPEN) if dlg.ShowModal() == wx.ID_OK: self.filename=dlg.GetFilename() self.dirname=dlg.GetDirectory() filehandle=open(os.path.join(self.dirname, self.filename),'rb') print os.path.join(self.dirname, self.filename) list_packets = self.data_store.readfile(filehandle) #self.control.SetValue(filehandle.read()) dlg.Destroy() for packet in list_packets: dest_mac, source_mac, eth_protocol = self.sniffer_obj.parse_ethernet_header(packet) if eth_protocol == 8: print "Loading IP Packet" # self.sniffer_obj.parse_ip_packet(packet) ip_packet = ipv4datagram(source_mac,dest_mac,eth_protocol,packet) self.data_store.add_packet(ip_packet) data_ = {1: (str(ip_packet.id),str(time.clock()),str(ip_packet.source_addr),str(ip_packet.dest_addr),str(eth_protocol),ip_packet.getprotocol(),str(ip_packet.ttl))} items = data_.items() for key, data in items: index = self.list.InsertStringItem(sys.maxint, data[0]) self.list.SetStringItem(index, 1, data[1]) self.list.SetStringItem(index, 2, data[2]) self.list.SetStringItem(index, 3, data[3]) self.list.SetStringItem(index, 4, data[4]) self.list.SetStringItem(index, 5, data[5]) self.list.SetStringItem(index, 6, data[6]) filehandle.close()
def capture(self): self.sniffer_obj.running=True self.sniffer_obj.start_promisc() input_list = [self.sniffer_obj.sock,sys.stdin] inputready,outputready,exceptready = select.select(input_list,[],[]) for s in inputready: if s == self.sniffer_obj.sock: try: packet = self.sniffer_obj.sock.recvfrom(65565); except socket.timeout, e: err = e.args[0] if err == 'timed out': sleep(1) print 'recv timed out, retry later' continue else: print e # continue sys.exit(1) except socket.error, e: print e sys.exit(1) else: # forward packet sender = packet[0] packet = packet[0] dest_mac, source_mac, eth_protocol = self.sniffer_obj.parse_ethernet_header(packet) # self.sniffer_obj.packetisreceived(eth_protocol) #str(serial) if eth_protocol == 8: print "IP Packet" self.sniffer_obj.parse_ip_packet(packet) ip_packet = ipv4datagram(source_mac,dest_mac,eth_protocol,packet) if self.has_filter: val = self.filter(self.filter_option,self.filter_string,ip_packet) print "Filter Value \n\n",val if val: self.data_store.add_packet(ip_packet) data_ = {1: (str(ip_packet.id),str(time.clock()),str(ip_packet.source_addr),str(ip_packet.dest_addr),str(len(ip_packet.data)),ip_packet.getprotocol(),str(ip_packet.ttl))} items = data_.items() for key, data in items: index = self.list.InsertStringItem(sys.maxint, data[0]) self.list.SetStringItem(index, 1, data[1]) self.list.SetStringItem(index, 2, data[2]) self.list.SetStringItem(index, 3, data[3]) self.list.SetStringItem(index, 4, data[4]) self.list.SetStringItem(index, 5, data[5]) self.list.SetStringItem(index, 6, data[6]) else: self.data_store.add_packet(ip_packet) data_ = {1: (str(ip_packet.id),str(time.clock()),str(ip_packet.source_addr),str(ip_packet.dest_addr),str(len(ip_packet.data)),ip_packet.getprotocol(),str(ip_packet.ttl))} items = data_.items() for key, data in items: index = self.list.InsertStringItem(sys.maxint, data[0]) self.list.SetStringItem(index, 1, data[1]) self.list.SetStringItem(index, 2, data[2]) self.list.SetStringItem(index, 3, data[3]) self.list.SetStringItem(index, 4, data[4]) self.list.SetStringItem(index, 5, data[5]) self.list.SetStringItem(index, 6, data[6]) # ip_packet.parse_ip_packet(packet) #fire event #print "ip packet",ip_packet self.sniffer_obj.packetisreceived(ip_packet)