Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
Backend,
Blob,
Coff,
Dex,
ELFCore,
ExceptionHandling,
FunctionHint,
Expand Down Expand Up @@ -95,6 +96,7 @@
"Backend",
"Blob",
"Coff",
"Dex",
"ELFCore",
"ExceptionHandling",
"FunctionHint",
Expand Down
2 changes: 2 additions & 0 deletions cle/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .gopclntab import GoFunction, GoPclntab, GoSymbol
from .ihex import Hex
from .java.apk import Apk
from .java.dex import Dex
from .java.jar import Jar
from .java.soot import Soot
from .macho import MachO
Expand Down Expand Up @@ -52,6 +53,7 @@
"NamedRegion",
"Jar",
"Apk",
"Dex",
"Soot",
"XBE",
"StaticArchive",
Expand Down
78 changes: 78 additions & 0 deletions cle/backends/java/dex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from __future__ import annotations

import logging

from cle.backends.backend import register_backend
from cle.errors import CLEError

from .soot import Soot

log = logging.getLogger(name=__name__)


class Dex(Soot):
"""
Backend for lifting bare DEX files to Soot.
"""

is_default = True # let CLE automatically use this backend

def __init__(
self,
dex_path,
binary_stream,
entry_point=None,
entry_point_params=(),
android_sdk=None,
android_api_version=None,
**options,
):
"""
:param dex_path: Path to the DEX file.
:param android_sdk: Path to Android SDK folder (e.g. "/home/angr/android/platforms")
:param android_api_version: Android API level to resolve the Android class library against. An APK
declares this in its manifest; a bare DEX file has no manifest, so it
has to be given here.

The following parameters are optional

:param entry_point: Fully qualified name of method that should be used as the entry point.
A bare DEX file declares no components, so there is none by default.
"""

log.info("Loading DEX from %s ...", dex_path)

example = (
' loading_opts = { "android_sdk" : "/home/angr/android/platforms",\n'
' "android_api_version" : 35 }\n'
' proj = angr.Project("/path/to/classes.dex", main_opts=loading_opts)'
)
if not android_sdk:
raise CLEError("\nPath to Android SDK must be specified explicitly, e.g.\n" + example)
if android_api_version is None:
raise CLEError(
"\nA bare DEX file has no manifest declaring an Android API level, so "
"android_api_version must be specified explicitly, e.g.\n" + example
)

# the actual lifting is done by the Soot superclass
super().__init__(
dex_path,
binary_stream,
input_format="dex",
android_sdk=android_sdk,
android_api_version=android_api_version,
entry_point=entry_point,
entry_point_params=entry_point_params,
**options,
)

@classmethod
def is_compatible(cls, stream) -> bool:
stream.seek(0)
identstring = stream.read(4)
stream.seek(0)
return identstring == b"dex\n"


register_backend("dex", Dex)
2 changes: 2 additions & 0 deletions cle/backends/java/soot.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __init__(
entry_point=None,
entry_point_params=(),
input_format=None,
android_api_version=None,
additional_jars=None,
additional_jar_roots=None,
jni_libs_ld_path=None,
Expand All @@ -56,6 +57,7 @@ def __init__(
self.binary,
input_format=input_format,
android_sdk=android_sdk,
android_api_version=android_api_version,
additional_jars=additional_jars,
additional_jar_roots=additional_jar_roots,
)
Expand Down
34 changes: 34 additions & 0 deletions tests/test_dex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from __future__ import annotations

import os
import tempfile
import zipfile

import pytest

import cle

TEST_BASE = os.path.join(os.path.dirname(os.path.realpath(__file__)), os.path.join("..", "..", "binaries"))
# The bytecode of the android1 test APK, which is also a valid input on its own.
APK = os.path.join(TEST_BASE, "tests", "java", "android1.apk")


def extract_classes_dex(target_dir: str) -> str:
with zipfile.ZipFile(APK) as archive:
return archive.extract("classes.dex", path=target_dir)


def test_dex_needs_an_android_sdk():
with tempfile.TemporaryDirectory() as target_dir:
dex = extract_classes_dex(target_dir)
with pytest.raises(cle.CLEError) as caught:
cle.Loader(dex, auto_load_libs=False)
assert "android_sdk" in str(caught.value)


def test_dex_needs_an_api_version():
with tempfile.TemporaryDirectory() as target_dir:
dex = extract_classes_dex(target_dir)
with pytest.raises(cle.CLEError) as caught:
cle.Loader(dex, auto_load_libs=False, main_opts={"android_sdk": target_dir})
assert "android_api_version" in str(caught.value)
Loading