def process_remainder(name, remainder): '''Process the remaining binary.''' # Make the packets output dir if it doesn't exist if not os.path.isdir(PACKETS_DIR): os.makedirs(PACKETS_DIR) remainder_text_filename = PACKETS_DIR + os.sep + name + ".txt" remainder_pcap_filename = PACKETS_DIR + os.sep + name + '_' + datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S-%f") + ".pcap" piped = '\n' + name + " piped to \'" + remainder_pcap_filename + "\'." # Don't process payload for Ethertype 0x0000, just print it if ethertype == EMPTY or converter.hex_equals_hex(ethertype, "0x0000"): print name print converter.binary_to_hex(remainder) return remainder = converter.binary_to_hex(remainder)[2:] # Remainder must be divisible by 2 if len(remainder) % 2 != 0: raise custom_exceptions.PayloadIsInvalidException(remainder) # Format remainder output remainder_output = "00000000" while remainder != EMPTY: remainder_output += SPACE + remainder[:2] remainder = remainder[2:] remainder_output += NEW_LINE print "Creating file \'" + remainder_text_filename + "\'..." txt_file = open(remainder_text_filename, 'w') txt_file.write(remainder_output) txt_file.close() print "Converting to \'.pcap\' file..." if converter.hex_equals_hex(ethertype, "0x0001"): # L2 header is passed along fabric subprocess.call(["text2pcap", remainder_text_filename, remainder_pcap_filename]) else: # append Ethertype subprocess.call(["text2pcap", '-e', ethertype, remainder_text_filename, remainder_pcap_filename]) print piped print "Removing file \'" + remainder_text_filename + "\'..." os.remove(remainder_text_filename) if open_wireshark: print "Opening Wireshark..." subprocess.Popen(["wireshark", '-r', remainder_pcap_filename])
def flush_section(self): # this is done by section so that dividing lines can be added manually in between """Processes the current section and clears it, nothing is printed.""" # self.lines - the lines in the current section # running_binary - the binary across multiple lines that does not divide up evenly into nibbles # running_lines - the lines associated with the running binary # running_indent - the current indent associated with the running binary if self.lines != []: running_lines = [] running_binary = EMPTY running_indent = 0 for line in self.lines: # go through each line in the section and process it running_lines.append(line) running_binary += line.data[2] if len(running_binary) % 4 == 0: # end the current running_section self.to_print.append(Line([H_LINE, H_LINE, H_LINE, H_LINE])) for section_line in running_lines: # Add the hex for the first part of the running_section if section_line == running_lines[0]: section_line.data[1] = converter.binary_to_hex(running_binary) # Pad the binary with dots at the beginning and end section_line.data[2] = (DOT * running_indent) + section_line.data[2] running_indent = len(section_line.data[2]) % 8 if running_indent != 0: section_line.data[2] += DOT * (8 - (len(section_line.data[2]) % 8)) # Break the binary into sections of eight cur_binary_section = section_line while cur_binary_section.data[2] != EMPTY: next_binary_section = Line( [EMPTY, EMPTY, cur_binary_section.data[2][8:], EMPTY], cur_binary_section.center ) cur_binary_section.data[2] = insert(cur_binary_section.data[2][:8], SPACE, 4) self.to_print.append(cur_binary_section) # Add the line to the print! cur_binary_section = next_binary_section if section_line != running_lines[-1]: self.to_print.append(Line([H_LINE, EMPTY, H_LINE, H_LINE])) running_lines = [] running_binary = EMPTY # Clear fields self.lines = []