def test_decode(self): hdr1 = MachHeader64(self.executable_x86_64[0:32]) self.assertEqual(MachHeader64.MH_MAGIC64, hdr1.magic) self.assertEqual(CpuType.ENUMS['CPU_TYPE_X86_64'], hdr1.cputype) self.assertEqual( CpuSubType.X86_64_SUBTYPES['CPU_SUBTYPE_X86_64_ALL'] | CpuSubType.CPU_SUBTYPE_LIB64, hdr1.cpusubtype) self.assertEqual(16, hdr1.ncmds) self.assertEqual(1296, hdr1.sizeofcmds) self.assertEqual( '<mach_header_64: magic=MH_MAGIC64, cputype=CPU_TYPE_X86_64, ' 'cpusubtype=CPU_SUBTYPE_X86_64_ALL, filetype=MH_EXECUTE, ncmds=16, ' 'sizeofcmds=1296, flags=MH_TWOLEVEL,MH_PIE,MH_NOUNDEFS,MH_DYLDLINK, reserved=0>', str(hdr1)) hdr2 = MachHeader64(self.object_x86_64[0:32]) self.assertEqual(MachHeader64.MH_MAGIC64, hdr2.magic) self.assertEqual(CpuType.ENUMS['CPU_TYPE_X86_64'], hdr1.cputype) self.assertEqual( CpuSubType.X86_64_SUBTYPES['CPU_SUBTYPE_X86_64_ALL'] | CpuSubType.CPU_SUBTYPE_LIB64, hdr1.cpusubtype) self.assertEqual(4, hdr2.ncmds) self.assertEqual(512, hdr2.sizeofcmds) self.assertEqual( '<mach_header_64: magic=MH_MAGIC64, cputype=CPU_TYPE_X86_64, ' 'cpusubtype=CPU_SUBTYPE_X86_64_ALL, filetype=MH_OBJECT, ncmds=4, ' 'sizeofcmds=512, flags=MH_SUBSECTIONS_VIA_SYMBOLS, reserved=0>', str(hdr2))
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 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!'