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

How to extract Android API calls invoked by Android app #904

Open
a2t2 opened this issue Aug 11, 2022 · 1 comment
Open

How to extract Android API calls invoked by Android app #904

a2t2 opened this issue Aug 11, 2022 · 1 comment

Comments

@a2t2
Copy link

a2t2 commented Aug 11, 2022

I want to get all the Android SDK APIs invoked by an Android app. I have the following sample code.

#!/usr/bin/python3

from sys import argv
from androguard.core.bytecodes import dvm
from androguard.core.analysis import analysis
from androguard.misc import AnalyzeAPK, AnalyzeDex

a, d, dx = AnalyzeAPK(argv[1])

print(a.get_package())

pkg = a.get_package()
pkg = pkg.replace(".", "/")

print(pkg)

# Get method in external classes
for cl in list(dx.get_external_classes()):
    
    for mt in cl.get_methods():
    
        # Need to check whether calls are from dev defined class/methods
        for cls, call, _ in mt.get_xref_from():
            
            # Check 1:
            # if not cls.is_android_api() and not cls.is_external():
            # if not cls.is_external():
            if pkg in str(cls.name):
                
                print(str(mt.full_name))
                print('--> called from ' + str(cls.name))

For Check 1, I tried three options. When using cls.is_android_api() or cls.is_external(), in the output I will see API calls not directly made by my app. For example I will see the following in the output:

Ljava/lang/Object; <init> ()V
--> called from Landroidx/core/view/ViewGroupCompat;

I want to ignore API calls made by AOSP code / libraries and focus on only the calls directly made by the app in Java code. For now I decided to use an approach which checks the package name for caller.

I have the following questions:
(1) Are there any API calls which I will miss if I use the package name check as discussed above ?
(2) The Androguard documentation has the following note.

image

(2-a) Does the call AnalyzeAPK() ensure all DEX files are loaded ?
(2-b) Classes not defined because they are dynamically loaded later: is there a way to identify this via any API call ?

@maryam-tanha
Copy link

maryam-tanha commented Aug 19, 2022

Hi,
This is the code I have written with some help from online resources. Can anyone confirm that it is the correct way to extract all API calls?

import logging
from androguard.core.bytecodes.apk import APK
from androguard.misc import AnalyzeAPK

class App(object):
    """
    this class describes an app
    """

    def __init__(self, app_path):
        self.logger = logging.getLogger(self.__class__.__name__)
        self.app_path = app_path
        self.apk = APK(self.app_path)
        
        
    def extract_api_calls(self):
        app, list_of_dex, dx = AnalyzeAPK(self.app_path)
        api_calls = []
        for cl in dx.get_external_classes():
            if cl.is_android_api():
                api_calls.append(cl.name)
        return(api_calls)  

chosen_app = App(file_path)
print("\nAPI calls are:\n")
print(chosen_app.extract_api_calls())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants