def main(): # Parse command-line option parser = argparse.ArgumentParser() group = parser.add_mutually_exclusive_group() group.add_argument('-i', '--interactive', action='store_true', help='run in interactive (command-line) mode') group.add_argument('-g', '--gui', action='store_true', help='run in graphical mode') parser.add_argument('-v', '--verbose', action='store_true', default=False, help='verbose logs') parser.add_argument('file', nargs='?', help='binary file to be analyzed') # Add all supported commands as option flags CommandLine.configure_parser(parser) if len(sys.argv) == 1: parser.print_help() sys.exit(1) else: options = parser.parse_args() ProgressIndicator.ENABLED = options.verbose if options.gui: AnsiText.ENABLE_COLOR = False root = Tk.Tk() gui = Gui(root) if options.file is not None: gui.load_file(options.file) try: root.mainloop() except KeyboardInterrupt: print '\nGoodBye!' root.destroy() else: # Read and parse the file bytes_ = Bytes(options.file) byte_range = ByteRange(0, len(bytes_), data=bytes_) # Determine if the first header is a fat header, mach header or neither if MachHeader.is_valid_header(bytes_.bytes) or MachHeader64.is_valid_header(bytes_.bytes): mach_o = MachO(byte_range) byte_range.data = mach_o elif FatHeader.is_valid_header(bytes_.bytes): fat = Fat(byte_range) byte_range.data = fat else: print 'ERROR: Cannot find neither fat nor mach header in the beginning of the binary.' sys.exit(1) cli = CommandLine(byte_range) cli.parse_options(options) while options.interactive: try: line = raw_input('>> ') cli.run(line) except (EOFError, KeyboardInterrupt): options.interactive = False print '\nGoodbye!'
def test_decode(self): hdr1 = MachHeader(self.executable_i386[0:28]) self.assertEqual(MachHeader.MH_MAGIC, hdr1.magic) self.assertEqual(CpuType.CPU_TYPE_X86, hdr1.cputype) self.assertEqual(3, hdr1.cpusubtype) self.assertEqual(16, hdr1.ncmds) self.assertEqual(1060, hdr1.sizeofcmds) self.assertEqual( '<mach_header: magic=MH_MAGIC, cputype=CPU_TYPE_I386, cpusubtype=CPU_SUBTYPE_X86_ALL, ' 'filetype=MH_EXECUTE, ncmds=16, sizeofcmds=1060, flags=MH_TWOLEVEL,MH_PIE,' 'MH_NO_HEAP_EXECUTION,MH_NOUNDEFS,MH_DYLDLINK>', str(hdr1)) hdr2 = MachHeader(self.object_i386[0:28]) self.assertEqual(MachHeader.MH_MAGIC, hdr2.magic) self.assertEqual(CpuType.CPU_TYPE_X86, hdr1.cputype) self.assertEqual(CpuSubType.X86_SUBTYPES['CPU_SUBTYPE_X86_ALL'], hdr1.cpusubtype) self.assertEqual(4, hdr2.ncmds) self.assertEqual(312, hdr2.sizeofcmds) self.assertEqual( '<mach_header: magic=MH_MAGIC, cputype=CPU_TYPE_I386, cpusubtype=CPU_SUBTYPE_X86_ALL, ' 'filetype=MH_OBJECT, ncmds=4, sizeofcmds=312, flags=MH_SUBSECTIONS_VIA_SYMBOLS>', str(hdr2))
def load_file(self, file_path): # Read and parse the file bytes_ = Bytes(file_path) byte_range = ByteRange(0, len(bytes_), data=bytes_) IndexedHeader.reset_indices() # Determine if the first header is a fat header, mach header or neither if MachHeader.is_valid_header(bytes_.bytes) or MachHeader64.is_valid_header(bytes_.bytes): mach_o = MachO(byte_range) byte_range.data = mach_o elif FatHeader.is_valid_header(bytes_.bytes): fat = Fat(byte_range) byte_range.data = fat else: print 'ERROR: Cannot find neither fat nor mach header in the beginning of the binary.' return self.load(byte_range, bytes_) self.set_subtitle(file_path)
def load_file(self, file_path): # Read and parse the file bytes_ = Bytes(file_path) byte_range = ByteRange(0, len(bytes_), data=bytes_) IndexedHeader.reset_indices() # Determine if the first header is a fat header, mach header or neither if MachHeader.is_valid_header( bytes_.bytes) or MachHeader64.is_valid_header(bytes_.bytes): mach_o = MachO(byte_range) byte_range.data = mach_o elif FatHeader.is_valid_header(bytes_.bytes): fat = Fat(byte_range) byte_range.data = fat else: print 'ERROR: Cannot find neither fat nor mach header in the beginning of the binary.' return self.load(byte_range, bytes_) self.set_subtitle(file_path)
def main(): # Parse command-line option parser = argparse.ArgumentParser() group = parser.add_mutually_exclusive_group() group.add_argument('-i', '--interactive', action='store_true', help='run in interactive (command-line) mode') group.add_argument('-g', '--gui', action='store_true', help='run in graphical mode') parser.add_argument('-v', '--verbose', action='store_true', default=False, help='verbose logs') parser.add_argument('file', nargs='?', help='binary file to be analyzed') # Add all supported commands as option flags CommandLine.configure_parser(parser) if len(sys.argv) == 1: parser.print_help() sys.exit(1) else: options = parser.parse_args() ProgressIndicator.ENABLED = options.verbose if options.gui: AnsiText.ENABLE_COLOR = False root = Tk.Tk() gui = Gui(root) if options.file is not None: gui.load_file(options.file) try: root.mainloop() except KeyboardInterrupt: print '\nGoodBye!' root.destroy() else: # Read and parse the file bytes_ = Bytes(options.file) byte_range = ByteRange(0, len(bytes_), data=bytes_) # Determine if the first header is a fat header, mach header or neither if MachHeader.is_valid_header( bytes_.bytes) or MachHeader64.is_valid_header(bytes_.bytes): mach_o = MachO(byte_range) byte_range.data = mach_o elif FatHeader.is_valid_header(bytes_.bytes): fat = Fat(byte_range) byte_range.data = fat else: print 'ERROR: Cannot find neither fat nor mach header in the beginning of the binary.' sys.exit(1) cli = CommandLine(byte_range) cli.parse_options(options) while options.interactive: try: line = raw_input('>> ') cli.run(line) except (EOFError, KeyboardInterrupt): options.interactive = False print '\nGoodbye!'