Example #1
0
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!'
Example #2
0
    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)
Example #3
0
    def test_nested_subranges(self):
        """
        Test nested subranges methods
        """
        br1 = ByteRange(0, 100)
        self.check_partitions(br1, True)

        # Add 2nd layer
        br11 = br1.add_subrange(0, 20)
        self.check_partitions(br1, False, br11, True)

        br12 = br1.add_subrange(20, 50)
        self.check_partitions(br1, False, br12, True)

        br13 = br1.add_subrange(70, 30)
        self.check_partitions(br1, True, br13, True)

        # Add 3rd layer
        br121 = br12.add_subrange(0, 30)
        self.check_partitions(br1, False, br12, False, br121, True)

        br122 = br12.add_subrange(30, 20)
        self.check_partitions(br1, True, br12, True, br122, True)

        # Add 4th layer
        br1211 = br121.add_subrange(0, 15)
        self.check_partitions(br1, False, br12, False, br121, False, br1211,
                              True)

        br1212 = br121.add_subrange(15, 10)
        self.check_partitions(br1, False, br12, False, br121, False, br1212,
                              True)

        br1213 = br121.add_subrange(25, 5)
        self.check_partitions(br1, True, br12, True, br121, True, br1213, True)

        # Verify all the absolute offsets
        self.assertEqual((0, 20), br11.abs_range())
        self.assertEqual((20, 35), br1211.abs_range())
        self.assertEqual((35, 45), br1212.abs_range())
        self.assertEqual((45, 50), br1213.abs_range())
        self.assertEqual((50, 70), br122.abs_range())
        self.assertEqual((70, 100), br13.abs_range())
Example #4
0
    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)
Example #5
0
    def test_nested_subranges(self):
        """
        Test nested subranges methods
        """
        br1 = ByteRange(0, 100)
        self.check_partitions(br1, True)

        # Add 2nd layer
        br11 = br1.add_subrange(0, 20)
        self.check_partitions(br1, False, br11, True)

        br12 = br1.add_subrange(20, 50)
        self.check_partitions(br1, False, br12, True)

        br13 = br1.add_subrange(70, 30)
        self.check_partitions(br1, True, br13, True)

        # Add 3rd layer
        br121 = br12.add_subrange(0, 30)
        self.check_partitions(br1, False, br12, False, br121, True)

        br122 = br12.add_subrange(30, 20)
        self.check_partitions(br1, True, br12, True, br122, True)

        # Add 4th layer
        br1211 = br121.add_subrange(0, 15)
        self.check_partitions(br1, False, br12, False, br121, False, br1211, True)

        br1212 = br121.add_subrange(15, 10)
        self.check_partitions(br1, False, br12, False, br121, False, br1212, True)

        br1213 = br121.add_subrange(25, 5)
        self.check_partitions(br1, True, br12, True, br121, True, br1213, True)

        # Verify all the absolute offsets
        self.assertEqual((0, 20), br11.abs_range())
        self.assertEqual((20, 35), br1211.abs_range())
        self.assertEqual((35, 45), br1212.abs_range())
        self.assertEqual((45, 50), br1213.abs_range())
        self.assertEqual((50, 70), br122.abs_range())
        self.assertEqual((70, 100), br13.abs_range())
Example #6
0
    def test_errors(self):
        br = ByteRange(0, 100)

        # Add 2 subranges
        br.add_subrange(20, 10)
        br.add_subrange(60, 20)

        # Add an overlapping subrange in front of 1st subrange
        self.assertRaises(ValueError, lambda: br.add_subrange(0, 21))

        # Add an overlapping subrange behind 1st subrange
        self.assertRaises(ValueError, lambda: br.add_subrange(29, 20))

        # Add an overlapping subrange in front of 2nd subrange
        self.assertRaises(ValueError, lambda: br.add_subrange(35, 26))

        # Add an overlapping subrange behind 2nd subrange
        self.assertRaises(ValueError, lambda: br.add_subrange(79, 10))

        # Add a subrange that goes beyond the parent byte range
        self.assertRaises(ValueError, lambda: br.add_subrange(90, 11))
Example #7
0
    def test_errors(self):
        br = ByteRange(0, 100)

        # Add 2 subranges
        br.add_subrange(20, 10)
        br.add_subrange(60, 20)

        # Add an overlapping subrange in front of 1st subrange
        self.assertRaises(ValueError, lambda: br.add_subrange(0, 21))

        # Add an overlapping subrange behind 1st subrange
        self.assertRaises(ValueError, lambda: br.add_subrange(29, 20))

        # Add an overlapping subrange in front of 2nd subrange
        self.assertRaises(ValueError, lambda: br.add_subrange(35, 26))

        # Add an overlapping subrange behind 2nd subrange
        self.assertRaises(ValueError, lambda: br.add_subrange(79, 10))

        # Add a subrange that goes beyond the parent byte range
        self.assertRaises(ValueError, lambda: br.add_subrange(90, 11))
Example #8
0
    def test_add_subrange(self):
        """
        Test add_subrange() method
        """
        br = ByteRange(0, 1000)

        self.check_partition(br, True)
        self.assertEqual('<BytesRange:0-1000>', str(br))

        # Add 1st subrange
        br.add_subrange(offset=100, length=51)
        self.check_partition(br, False)
        self.check_subranges(br, (100, 151))

        # Add a subrange in the front
        br.add_subrange(offset=50, length=7)
        self.check_partition(br, False)
        self.check_subranges(br, (50, 57), (100, 151))

        # Add a subrange in the end
        br.add_subrange(offset=200, length=800)
        self.check_partition(br, False)
        self.check_subranges(br, (50, 57), (100, 151), (200, 1000))

        # Fill out the remaining gap to completely cover the byte range
        br.add_subrange(offset=0, length=50)
        br.add_subrange(offset=57, length=43)
        br.add_subrange(offset=151, length=49)
        self.check_partition(br, True)
        self.check_subranges(br, (0, 50), (50, 57), (57, 100), (100, 151),
                             (151, 200), (200, 1000))
Example #9
0
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!'
Example #10
0
    def test_add_subrange(self):
        """
        Test add_subrange() method
        """
        br = ByteRange(0, 1000)

        self.check_partition(br, True)
        self.assertEqual('<BytesRange:0-1000>', str(br))

        # Add 1st subrange
        br.add_subrange(offset=100, length=51)
        self.check_partition(br, False)
        self.check_subranges(br, (100, 151))

        # Add a subrange in the front
        br.add_subrange(offset=50, length=7)
        self.check_partition(br, False)
        self.check_subranges(br, (50, 57), (100, 151))

        # Add a subrange in the end
        br.add_subrange(offset=200, length=800)
        self.check_partition(br, False)
        self.check_subranges(br, (50, 57), (100, 151), (200, 1000))

        # Fill out the remaining gap to completely cover the byte range
        br.add_subrange(offset=0, length=50)
        br.add_subrange(offset=57, length=43)
        br.add_subrange(offset=151, length=49)
        self.check_partition(br, True)
        self.check_subranges(br, (0, 50), (50, 57), (57, 100), (100, 151), (151, 200), (200, 1000))