杰瑞科技汇

androguard Python如何用?工具功能解析

Of course! Here is a comprehensive guide to using Androguard with Python, covering what it is, how to install it, and practical examples for Android malware analysis.

androguard Python如何用?工具功能解析-图1
(图片来源网络,侵删)

What is Androguard?

Androguard is a full Python toolkit to play with Android files. It's a powerful open-source library designed for security researchers, malware analysts, and developers who need to inspect, modify, or reverse-engineer Android applications (APKs, AABs, ODEX files, etc.).

Its core components are:

  1. androguard.core.bytecodes.axml: A parser for Android's binary XML format (resources.arsc, AndroidManifest.xml). It converts them into a readable tree structure.
  2. androguard.core.bytecodes.dvm: A Dalvik VM (DVM) and Android (ART) bytecode parser. It can disassemble and analyze the Dalvik bytecode inside an APK.
  3. androguard.core.bytecodes.apk: The main class that ties everything together. It loads an APK and provides easy access to its manifest, resources, classes, and methods.

Androguard is essentially the Python equivalent of tools like apktool, dex2jar, and jadx all rolled into one, but with the flexibility of a programming language.


Installation

The easiest way to install Androguard is using pip. It's highly recommended to do this inside a Python virtual environment to avoid dependency conflicts.

androguard Python如何用?工具功能解析-图2
(图片来源网络,侵删)
# Create and activate a virtual environment (optional but recommended)
python -m venv androguard_env
source androguard_env/bin/activate  # On Windows: androguard_env\Scripts\activate
# Install Androguard
pip install androguard

Androguard has several optional dependencies for more advanced features like GUI support or better graph visualization. You can install them with:

# For GUI support (e.g., to view the GUI)
pip install androguard[gui]
# For graph visualization (e.g., to create call graphs)
pip install androguard[graphviz]

Note: If you encounter issues, especially on Linux, you might need to install system-level dependencies first:

  • Debian/Ubuntu: sudo apt-get install python3-dev zlib1g-dev
  • Fedora/CentOS: sudo dnf install python3-devel zlib-devel

Core Concepts and Key Classes

When you work with Androguard, you'll primarily interact with these objects:

  • APK: Represents the entire APK file. You get this by loading an APK.
  • DalvikVMFormat: Represents a DEX file (.dex or .odex) inside the APK. An APK can have multiple DEX files (classes.dex, classes2.dex, etc.).
  • AndroidManifest: Represents the parsed AndroidManifest.xml.
  • Analysis: A crucial class that performs cross-file analysis, linking classes, methods, and strings across all DEX files in an APK.

Practical Examples

Let's dive into some common tasks.

androguard Python如何用?工具功能解析-图3
(图片来源网络,侵删)

Example 1: Basic APK Information

This example shows how to load an APK and extract basic information like its package name, version, and permissions.

from androguard.core.bytecodes.axml import ARSCParser
from androguard.core.bytecodes.apk import APK
# Path to your APK file
apk_path = "path/to/your/app.apk"
# Load the APK
try:
    a = APK(apk_path)
    print(f"Package Name: {a.get_package()}")
    print(f"Version Name: {a.get_androidversion_name()}")
    print(f"Version Code: {a.get_androidversion_code()}")
    print(f"Main Activity: {a.get_main_activity()}")
    # Get all declared permissions
    print("\nPermissions:")
    for permission in a.get_permissions():
        print(f" - {permission}")
    # Get all activities
    print("\nActivities:")
    for activity in a.get_activities():
        print(f" - {activity}")
except Exception as e:
    print(f"Error loading APK: {e}")

Example 2: Analyzing the AndroidManifest.xml

The manifest is a treasure trove of information. Androguard parses it into a readable object.

from androguard.core.bytecodes.apk import APK
apk_path = "path/to/your/app.apk"
a = APK(apk_path)
manifest = a.get_android_manifest()
# Get the manifest as a string (can be large)
# print(manifest.get_xml())
# Get specific tags and attributes
print(f"Min SDK Version: {manifest.get_min_sdk_version()}")
print(f"Target SDK Version: {manifest.get_target_sdk_version()}")
# Get all services declared in the manifest
print("\nServices:")
for service in manifest.get_services():
    print(f" - Name: {service.get_name()}, Permission: {service.get_permission()}")
# Get all providers
print("\nProviders:")
for provider in manifest.get_providers():
    print(f" - Name: {provider.get_name()}, Authorities: {provider.get_authorities()}")

Example 3: Disassembling and Analyzing DEX Code

This is where Androguard shines. We can disassemble methods and analyze the control flow.

from androguard.core.bytecodes.apk import APK
from androguard.core.bytecodes.dvm import DalvikVMFormat
from androguard.core.analysis.analysis import Analysis
apk_path = "path/to/your/app.apk"
a = APK(apk_path)
# Get the main DEX file (classes.dex)
dex = a.get_dex()
# Perform analysis
dx = Analysis(dex)
# Find a specific class (e.g., a class with "MainActivity" in its name)
# The get_class() method expects the internal format: Lcom/example/MainActivity;
main_activity_class = dx.get_class("Lcom/example/MainActivity;")
if main_activity_class:
    print(f"\nFound class: {main_activity_class.name}")
    # Get all methods from this class
    for method in main_activity_class.get_methods():
        print(f"\n--- Disassembling Method: {method.name} ---")
        # Get the bytecode instructions
        for instruction in method.get_instructions():
            # instruction.get_name() is the mnemonic (e.g., 'invoke-virtual')
            # instruction.get_output() gives a more detailed string
            print(instruction.get_output())
        # You can also get the method's code block
        # method.get_code() returns a Code object
        # code = method.get_code()
        # if code:
        #     print(f"  Registers: {code.get_registers_size()}")
        #     print(f"  Ins Size: {code.get_ins_size()}")
        #     print(f"  Out Size: {code.get_outs_size()}")

Example 4: Finding Suspicious API Calls

A common task in malware analysis is to look for calls to sensitive APIs (e.g., getDeviceId, startActivity for phishing, etc.).

from androguard.core.bytecodes.apk import APK
from androguard.core.analysis.analysis import Analysis
# List of suspicious API patterns to search for
SUSPICIOUS_APIS = [
    "Landroid/telephony/TelephonyManager;->getDeviceId",
    "Landroid/telephony/TelephonyManager;->getSubscriberId",
    "Landroid/content/ContentResolver;->query",
    "Landroid/net/Uri;->parse",
    "Landroid/content/Intent;-><init>", # Often used to start phishing activities
]
apk_path = "path/to/your/app.apk"
a = APK(apk_path)
dx = Analysis(a.get_dex())
print("\n--- Searching for Suspicious API Calls ---")
for current_class in dx.get_classes():
    for method in current_class.get_methods():
        for instruction in method.get_instructions():
            # Check if the instruction is a method invocation
            if instruction.get_name() in ('invoke-virtual', 'invoke-direct', 'invoke-static', 'invoke-interface'):
                method_name = instruction.get_class_name() + "->" + instruction.get_name()
                if any(api in method_name for api in SUSPICIOUS_APIS):
                    print(f"  Found suspicious call in: {current_class.name} -> {method.name}")
                    print(f"    Instruction: {instruction.get_output()}")

Example 5: Building a Call Graph

A call graph shows which methods call other methods. This is essential for understanding the application's logic and entry points.

from androguard.core.bytecodes.apk import APK
from androguard.core.analysis.analysis import Analysis
from androguard.misc import AnalyzeAPK
apk_path = "path/to/your/app.apk"
# AnalyzeAPK is a convenient function that returns (a, d, dx)
a, d, dx = AnalyzeAPK(apk_path)
print("\n--- Building Call Graph ---")
# The dx object contains the call graph
# dx.get_call_graph() returns a networkx.DiGraph object
call_graph = dx.get_call_graph()
# Find all entry points (methods called from outside the app, e.g., by the OS)
# These are typically methods with the 'android:exported="true"' attribute
entry_points = []
for method in call_graph.nodes():
    # A simple heuristic: methods in activities that are main activities or exported
    if "Activity" in method.class_name and a.is_main_activity(method.class_name.replace("L", "").replace(";", "")):
        entry_points.append(method)
print(f"Found {len(entry_points)} potential entry points.")
# Let's take the first main activity and see what it calls
if entry_points:
    start_method = entry_points[0]
    print(f"\nTracing calls from: {start_method}")
    # Use networkx to find all methods reachable from the start_method
    reachable = nx.descendants(call_graph, start_method)
    print(f"Reachable methods: {len(reachable)}")
    # for method in reachable:
    #     print(f"  - {method}")

Advanced Features and Tips

  • GUI Tool: Androguard comes with a simple GUI. Run it from the command line:

    androguard-gui

    This allows you to load an APK and browse its structure, manifest, and disassembled code interactively.

  • Obfuscation: Androguard can help detect obfuscation by analyzing method and class names. Very short, non-standard names (e.g., a, b, c) or names with random strings are strong indicators.

  • Multi-DEX Support: Modern apps often use multiple DEX files (classes.dex, classes2.dex, etc.). The APK and Analysis objects handle this automatically, linking classes across all DEX files.

  • Community and Help: If you get stuck, the best place to look is the Androguard GitHub page for issues and documentation. The official documentation can be a bit sparse, so reading the source code is often necessary.

Limitations

  • Performance: For very large or complex apps, Androguard can be slower than dedicated tools like jadx or Ghidra.
  • Decompilation Quality: The decompiler isn't as polished as jadx's, so the output might sometimes be less readable.
  • Dynamic Analysis: Androguard is primarily a static analysis tool. It doesn't run the app to observe its behavior.

Conclusion

Androguard is an incredibly versatile and powerful tool for anyone working with Android applications at a low level. Its Python-based nature allows for automation, custom analysis scripts, and deep integration into existing security workflows. While it might have a steeper learning curve than some graphical tools, the control and flexibility it offers are unparalleled for in-depth reverse engineering.

分享:
扫描分享到社交APP
上一篇
下一篇