Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use get_encoded_methods() when decompiling #1035

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions androguard/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ def arsc(input_,
'--format', '-f', 'format_',
help='Additionally write control flow graphs for each method, specify '
'the format for example png, jpg, raw (write dot file), ...',
type=click.Choice(['png', 'jpg', 'raw'])
)
@click.option(
'--jar', '-j',
Expand Down
2 changes: 1 addition & 1 deletion androguard/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def export_apps_to_format(filename,
shutil.move(filenamejar, os.path.join(output, "classes.jar"))
print("End")

for method in vm.get_methods():
for method in vm.get_encoded_methods():
if methods_filter_expr:
msig = "{}{}{}".format(method.get_class_name(), method.get_name(),
method.get_descriptor())
Expand Down
11 changes: 7 additions & 4 deletions androguard/core/bytecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ def method2format(output, _format="png", mx=None, raw=None):
:param androguard.core.analysis.analysis.MethodAnalysis mx: specify the MethodAnalysis object
:param dict raw: use directly a dot raw buffer if None
"""
# pydot is optional!
# pydot is optional, it's only needed for png, jpg formats
import pydot

if raw:
Expand Down Expand Up @@ -465,9 +465,12 @@ def method2format(output, _format="png", mx=None, raw=None):
logger.warning("The graph generated for '{}' has too many subgraphs! "
"Only plotting the first one.".format(output))
for g in d:
getattr(g, "write_" + _format.lower())(output)
break

try:
getattr(g, "write_" + _format.lower())(output)
break
except FileNotFoundError:
logger.error("Could not write graph image, ensure graphviz is installed!")
raise

def method2png(output, mx, raw=False):
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_callgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def testCallgraphTotalNodesEdges(self):
# num external nodes
self.assertEqual(total_external_nodes, 1000)

# num external nodes
# num internal nodes
self.assertEqual(total_internal_nodes, 2600)

# total num edges
Expand Down
37 changes: 37 additions & 0 deletions tests/test_cli_decompile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from androguard.cli.main import export_apps_to_format
from androguard import session

import os
import shutil
import unittest

test_dir = os.path.dirname(os.path.abspath(__file__))

class TestCLIDecompile(unittest.TestCase):
@classmethod
def setUpClass(cls):
test_testactivity_apk_path = os.path.join(test_dir, 'data/APK/TestActivity.apk')
cls.s = session.Session()
with open(test_testactivity_apk_path, "rb") as fd:
cls.s.add(test_testactivity_apk_path, fd.read())

def testDecompileDefaults(self):
"""test decompile command using default cli settings"""
export_apps_to_format(
None,
self.s,
os.path.join(test_dir,'tmp_TestActivity_decompilation'),
None,
False,
None,
None)

@classmethod
def tearDownClass(cls):
decomp_dir = os.path.join(test_dir, 'tmp_TestActivity_decompilation')
if os.path.exists(decomp_dir):
shutil.rmtree(decomp_dir)


if __name__ == '__main__':
unittest.main()