From 8abb553a1d7cc78384e127d7d24799ec177b001a Mon Sep 17 00:00:00 2001 From: Jeroen Beckers Date: Wed, 17 Feb 2021 13:23:42 +0100 Subject: [PATCH] Added warnings for loaded classes (#403) * Added warnings for loaded classes * Fix unittests --- objection/commands/android/hooking.py | 14 ++++++++++++-- tests/commands/android/test_hooking.py | 17 +++++++++++------ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/objection/commands/android/hooking.py b/objection/commands/android/hooking.py index 18f14015..4d959561 100644 --- a/objection/commands/android/hooking.py +++ b/objection/commands/android/hooking.py @@ -52,7 +52,9 @@ def _should_dump_return_value(args: list) -> bool: def show_android_classes(args: list = None) -> None: """ - Show the currently loaded classes. + Show the currently loaded classes. + Note that Java classes are only loaded when they are used, + so not all classes may be present. :param args: :return: @@ -269,7 +271,9 @@ def set_method_return_value(args: list = None) -> None: def search_class(args: list) -> None: """ - Searches the current Android application for a class. + Searches the currently loaded classes for a class. + Note that Java classes are only loaded when they are used, + so if you don't get results, the class might not have been used yet. :param args: :return: @@ -279,6 +283,9 @@ def search_class(args: list) -> None: click.secho('Usage: android hooking search classes ', bold=True) return + click.secho('Note that Java classes are only loaded when they are used,' + ' so if the expected class has not been found, it might not have been loaded yet.', fg='yellow') + search = args[0] found = 0 @@ -311,6 +318,9 @@ def search_methods(args: list) -> None: class_filter = args[1] if len(clean_argument_flags(args)) > 1 else None found = 0 + click.secho('Note that Java classes are only loaded when they are used,' + ' so if the expected class has not been found, it might not have been loaded yet.', fg='yellow') + if not class_filter: click.secho('Warning, searching all classes may take some time and in some cases, ' 'crash the target application.', fg='yellow') diff --git a/tests/commands/android/test_hooking.py b/tests/commands/android/test_hooking.py index 304f5434..86e445dc 100644 --- a/tests/commands/android/test_hooking.py +++ b/tests/commands/android/test_hooking.py @@ -241,7 +241,7 @@ def test_search_class_handles_empty_data(self, mock_api): with capture(search_class, ['com.foo.bar']) as o: output = o - self.assertEqual(output, '\nFound 0 classes\n') + self.assertEqual(output, 'Note that Java classes are only loaded when they are used, so if the expected class has not been found, it might not have been loaded yet.\n\nFound 0 classes\n') @mock.patch('objection.state.connection.state_connection.get_api') def test_search_class(self, mock_api): @@ -253,7 +253,8 @@ def test_search_class(self, mock_api): with capture(search_class, ['com.foo.bar']) as o: output = o - expected_output = """com.foo.bar + expected_output = """Note that Java classes are only loaded when they are used, so if the expected class has not been found, it might not have been loaded yet. +com.foo.bar com.foo.bar.baz Found 2 classes @@ -279,7 +280,8 @@ def test_search_class_handles_empty_data_with_no_filter(self, mock_confirm, mock with capture(search_methods, ['hteeteepee']) as o: output = o - expected_output = """Warning, searching all classes may take some time and in some cases, crash the target application. + expected_output = """Note that Java classes are only loaded when they are used, so if the expected class has not been found, it might not have been loaded yet. +Warning, searching all classes may take some time and in some cases, crash the target application. Found 0 classes, searching methods (this may take some time)... Found 0 methods @@ -295,7 +297,8 @@ def test_search_class_handles_empty_data_with_filter(self, mock_api): with capture(search_methods, ['hteeteepee', 'com.foo']) as o: output = o - expected_output = """Found 0 classes, searching methods (this may take some time)... + expected_output = """Note that Java classes are only loaded when they are used, so if the expected class has not been found, it might not have been loaded yet. +Found 0 classes, searching methods (this may take some time)... Filtering classes with com.foo Found 0 methods @@ -315,7 +318,8 @@ def test_search_class_with_no_filter(self, mock_confirm, mock_api): with capture(search_methods, ['hteeteepee']) as o: output = o - expected_output = """Warning, searching all classes may take some time and in some cases, crash the target application. + expected_output = """Note that Java classes are only loaded when they are used, so if the expected class has not been found, it might not have been loaded yet. +Warning, searching all classes may take some time and in some cases, crash the target application. Found 1 classes, searching methods (this may take some time)... invoke_hteeteepee_method @@ -338,7 +342,8 @@ def test_search_class_with_filter(self, mock_confirm, mock_api): with capture(search_methods, ['hteeteepee', 'com.test']) as o: output = o - expected_output = """Found 2 classes, searching methods (this may take some time)... + expected_output = """Note that Java classes are only loaded when they are used, so if the expected class has not been found, it might not have been loaded yet. +Found 2 classes, searching methods (this may take some time)... Filtering classes with com.test invoke_hteeteepee_method