APK metadata¶
An APK object is the package: the archive, its manifest, its resources and
its signatures. The code inside it is a separate object, the analysis context,
which the APK's analysis() method returns (Go: Analysis()).
The examples come in Python, Go and Java. Pick your language in the tabs.
| Open an APK | Python | Go | Java |
|---|---|---|---|
| open the archive | dexpp.Apk.open(path) |
dexpp.OpenAPK(path) |
Apk.open(path) |
| open bytes in memory | dexpp.Apk.from_bytes(data) |
dexpp.OpenAPKBytes(data, label) |
Apk.fromBytes(data) |
| get its code | apk.analysis() |
apk.Analysis() |
apk.analysis() |
Opening fails when the path does not exist, when it exists but cannot be read
(a directory, for example), or when the file is not a ZIP archive. Getting the
code fails when the APK has no classes.dex, or when one of its DEX files
cannot be parsed.
| Failure | Python raises | Go returns | Java throws |
|---|---|---|---|
| the path does not exist | FileNotFoundError |
dexpp.ErrFileNotFound |
NoSuchFileException |
| the path cannot be read | OSError |
dexpp.ErrCannotRead |
FileSystemException |
not a ZIP archive, or no classes.dex |
dexpp.InvalidApkError |
dexpp.ErrInvalidAPK |
InvalidApkException |
| a DEX file cannot be parsed | dexpp.InvalidDexError |
dexpp.ErrInvalidDex |
InvalidDexException |
In Go, compare an error with these values through errors.Is. See
Errors in Go and Errors on the
Java page.
Opening an APK from memory¶
Sometimes a program has the bytes of an APK, not a file: a server that receives an upload, or a tool that downloads APKs. It can open the bytes directly, without writing them to a file first.
# The bytes can come from anywhere: a download, an upload, a database.
with open("tests/data/sample.apk", "rb") as file:
data: bytes = file.read()
in_memory: dexpp.Apk = dexpp.Apk.from_bytes(data, label="sample.apk")
in_memory_manifest: dexpp.Manifest | None = in_memory.manifest
assert in_memory_manifest is not None
print(in_memory_manifest.package)
# threads=1 keeps all the work on the calling thread.
code: dexpp.AnalysisContext = in_memory.analysis(threads=1)
print("classes:", [cls.descriptor for cls in code.classes()])
# The label starts the error messages, where Apk.open puts the path.
try:
dexpp.Apk.from_bytes(b"not an apk", label="upload.apk")
except dexpp.InvalidApkError as error:
print("InvalidApkError:", error)
// The bytes can come from anywhere: a download, an upload, a database.
data, err := os.ReadFile("../../../tests/data/sample.apk")
if err != nil {
log.Fatal(err)
}
inMemory, err := dexpp.OpenAPKBytes(data, "sample.apk")
if err != nil {
log.Fatal(err)
}
defer inMemory.Close()
if pkg, ok := inMemory.Package(); ok {
fmt.Println(pkg)
}
// WithThreads(1) keeps all the work on the calling thread.
code, err := inMemory.Analysis(dexpp.WithThreads(1))
if err != nil {
log.Fatal(err)
}
defer code.Close()
var descriptors []string
for _, class := range code.Classes() {
descriptors = append(descriptors, class.Descriptor())
}
fmt.Println("classes:", descriptors)
// The label starts the error messages, where OpenAPK puts the path.
_, err = dexpp.OpenAPKBytes([]byte("not an apk"), "upload.apk")
if errors.Is(err, dexpp.ErrInvalidAPK) {
fmt.Println("ErrInvalidAPK:", err)
}
// The bytes can come from anywhere: a download, an upload, a database.
byte[] data = Files.readAllBytes(Path.of("../../tests/data/sample.apk"));
try (Apk inMemory = Apk.fromBytes(data, "sample.apk")) {
System.out.println(inMemory.packageName().orElseThrow());
// analysis(1) keeps all the work on the calling thread.
try (AnalysisContext code = inMemory.analysis(1)) {
List<String> descriptors = new ArrayList<>();
for (DexClass cls : code.classes()) {
descriptors.add(cls.descriptor());
}
System.out.println("classes: " + descriptors);
}
}
// The label starts the error messages, where Apk.open puts the path.
byte[] notAnApk = "not an apk".getBytes(StandardCharsets.US_ASCII);
try (Apk broken = Apk.fromBytes(notAnApk, "upload.apk")) {
System.out.println(broken.entries());
} catch (InvalidApkException e) {
System.out.println("InvalidApkException: " + e.getMessage());
}
- The APK keeps its own copy of the bytes. Your copy can be changed or freed as soon as the call returns.
- The bytes get the same checks as a file given to
open. Bytes that are not a ZIP archive fail with the same invalid APK error. - The label is a name for the bytes, such as the name of an uploaded file.
Error messages start with it, where
openputs the path. The errors of the APK'sanalysis()use it too. Without a label, the messages start with<apk>. In Go the label is a required argument, and an empty label gives<apk>.
The threads of the analysis¶
The APK's analysis() method takes a thread count, like loading an APK with
the analysis context (see Threads):
| Language | Get the code on the calling thread only |
|---|---|
| Python | apk.analysis(threads=1) |
| Go | apk.Analysis(dexpp.WithThreads(1)) |
| Java | apk.analysis(1) |
A count of 1 keeps all the work on the calling thread: decompressing the DEX files now, and building the call graph later. Use it when your program already analyses several APKs in parallel, or when it must not start threads. A count of 0, the default, lets the library choose.
The APK keeps the analysis it builds. The first call builds it, and a later call with the same thread count returns the same analysis, so its call graph and other structures are built only once. A call with a different thread count builds a second, separate analysis, which holds its own copy of the DEX files. Both give the same answers. So choose one thread count and use it everywhere in a program.
The manifest¶
The manifest is the decoded AndroidManifest.xml. An APK may have no manifest,
or one that cannot be decoded. Then there is no manifest: Python gives None,
Go gives false as the second result of Manifest(), and Java gives an empty
Optional. The examples check this before they use the manifest.
apk: dexpp.Apk = dexpp.Apk.open("tests/data/sample.apk")
manifest: dexpp.Manifest | None = apk.manifest
assert manifest is not None
print(manifest.package)
print("version:", manifest.version_code, manifest.version_name)
print("sdk:", manifest.min_sdk, "to", manifest.target_sdk)
print("uses:", manifest.permissions)
print("declares:", manifest.declared_permissions)
print("debuggable:", manifest.debuggable, "allow backup:", manifest.allow_backup)
apk, err := dexpp.OpenAPK("../../../tests/data/sample.apk")
if err != nil {
log.Fatal(err)
}
defer apk.Close()
manifest, ok := apk.Manifest()
if !ok {
log.Fatal("no manifest")
}
fmt.Println(manifest.Package)
// An attribute the manifest may leave out is a pointer, nil when it is not set.
if manifest.VersionCode != nil && manifest.VersionName != nil {
fmt.Println("version:", *manifest.VersionCode, *manifest.VersionName)
}
if manifest.MinSDK != nil && manifest.TargetSDK != nil {
fmt.Println("sdk:", *manifest.MinSDK, "to", *manifest.TargetSDK)
}
fmt.Println("uses:", manifest.Permissions)
for _, permission := range manifest.DeclaredPermissions {
fmt.Println("declares:", permission.Name)
}
if manifest.Debuggable != nil && manifest.AllowBackup != nil {
fmt.Println("debuggable:", *manifest.Debuggable, "allow backup:", *manifest.AllowBackup)
}
try (Apk apk = Apk.open(Path.of("../../tests/data/sample.apk"))) {
Manifest manifest = apk.manifest().orElseThrow();
System.out.println(manifest.packageName());
// An attribute the manifest may leave out is an Optional, empty when it is not set.
if (manifest.versionCode().isPresent() && manifest.versionName().isPresent()) {
System.out.println("version: " + manifest.versionCode().getAsLong() + " "
+ manifest.versionName().get());
}
if (manifest.minSdk().isPresent() && manifest.targetSdk().isPresent()) {
System.out.println("sdk: " + manifest.minSdk().getAsLong() + " to "
+ manifest.targetSdk().getAsLong());
}
System.out.println("uses: " + manifest.permissions());
for (DeclaredPermission permission : manifest.declaredPermissions()) {
System.out.println("declares: " + permission.name());
}
if (manifest.debuggable().isPresent() && manifest.allowBackup().isPresent()) {
System.out.println("debuggable: " + manifest.debuggable().get()
+ " allow backup: " + manifest.allowBackup().get());
}
}
Some attributes may be left out of a manifest, such as the version code, the
minimum SDK or debuggable. When one is not set, its value is absent: None
in Python, a nil pointer in Go, an empty Optional in Java. So an absent
debuggable means "not written in the manifest", which Android treats as
false.
Values given by a resource reference¶
A manifest can give a boolean or integer attribute through a resource
reference instead of a plain value, for example
android:debuggable="@bool/debug" or android:exported="@bool/is_exported".
dexpp then looks the value up in the APK's resource table (resources.arsc),
in the default configuration, and the attribute holds that value. The
reference id itself is kept in a second attribute, whose name ends in
reference:
| Value | Python | Go | Java |
|---|---|---|---|
| version code | version_code_reference |
VersionCodeReference |
versionCodeReference() |
| minimum SDK | min_sdk_reference |
MinSDKReference |
minSdkReference() |
| target SDK | target_sdk_reference |
TargetSDKReference |
targetSdkReference() |
debuggable |
debuggable_reference |
DebuggableReference |
debuggableReference() |
allowBackup |
allow_backup_reference |
AllowBackupReference |
allowBackupReference() |
exported, on a component |
exported_reference |
ExportedReference |
exportedReference() |
The reference is absent for a plain value or a missing attribute.
A reference can fail to resolve: the APK has no resource table, the table has
no such id, the value has another type (a string where a boolean is expected),
or the attribute names a theme attribute (?attr/...), whose value depends on
the theme the app runs with. Then the value is absent, but the reference still
holds the id. So the two cases stay apart:
- value absent, reference absent: the attribute is not in the manifest;
- value absent, reference set: the attribute is in the manifest, but its value is unknown.
When a resource has variants, for example a values-v31 folder that changes a
boolean on Android 12 and newer, only the default value is used. On a device
that matches the variant, Android uses the other value.
dexpp reads the android: attributes the way Android does: by their resource
id, not by their name. In the binary manifest, each attribute name is a string,
and a table in the file gives the framework attributes their ids, such as
0x01010010 for android:exported. Android only looks at the id. So an app
that renames the attribute strings cannot hide a value from dexpp, and an
attribute that is only named like android:exported, without its id, is
ignored, as Android ignores it.
Components¶
A component is an activity, a service, a broadcast receiver or a content
provider. Component names are always full class names: a manifest entry such
as .MainActivity is completed with the package name.
for component in manifest.components:
state: str = "exported" if component.is_exported is True else "private"
print(component.kind.name, component.name, state)
activities: list[dexpp.Component] = manifest.components_of(dexpp.ComponentKind.ACTIVITY)
launcher: dexpp.Component = activities[0]
print(launcher.intent_filters[0].actions)
for _, component := range manifest.Components {
state := "private"
if component.IsExported {
state = "exported"
}
fmt.Println(component.Kind, component.Name, state)
}
activities := manifest.ComponentsOf(dexpp.ComponentActivity)
launcher := activities[0]
fmt.Println(launcher.IntentFilters[0].Actions)
for (Component component : manifest.components()) {
String state = component.isExported() ? "exported" : "private";
System.out.println(component.kind() + " " + component.name() + " " + state);
}
List<Component> activities = manifest.componentsOf(ComponentKind.ACTIVITY);
Component launcher = activities.get(0);
System.out.println(launcher.intentFilters().get(0).actions());
There are two ways to ask whether a component is exported:
exported(Go:Exported, Java:exported()) is what the manifest says: true, false, or absent when theandroid:exportedattribute is missing or is a reference that could not be resolved.is_exported(Go:IsExported, Java:isExported()) is the effective answer. When the attribute is missing, a component with at least one intent filter counts as exported. This is the rule Android applied before Android 12. When the attribute is a reference that could not be resolved (the value is absent and the reference is set),is_exportedis true: the developer set the flag, its value is unknown, and for triage "possibly exported" is the safe answer. Guessing from the intent filters could hide an exported component.
Which components can another app reach¶
Exported is not the same as reachable. An exported component can still be protected by a permission that no other app can get.
is_reachable (Go: IsReachable, Java: isReachable()) is true when the
component is exported and it is not guarded by a signature-level
permission that the app itself declares. A permission with the signature or
signatureOrSystem protection level is only granted to apps signed with the
same key, so a third-party app cannot hold it.
guarded_apk: dexpp.Apk = dexpp.Apk.open("tests/data/reachable.apk")
guarded: dexpp.Manifest | None = guarded_apk.manifest
assert guarded is not None
for component in guarded.components:
if component.is_exported is True and component.is_reachable is False:
permission: str | None = component.permission
assert permission is not None
level: int | None = guarded.declared_permission_protection_level(permission)
print(f"{component.name} is exported, but guarded by {permission} (level {level})")
reachable: list[dexpp.Component] = [c for c in guarded.components if c.is_reachable is True]
print(len(reachable), "of", len(guarded.components), "components are reachable")
guardedAPK, err := dexpp.OpenAPK("../../../tests/data/reachable.apk")
if err != nil {
log.Fatal(err)
}
defer guardedAPK.Close()
guarded, ok := guardedAPK.Manifest()
if !ok {
log.Fatal("no manifest")
}
reachable := 0
for _, component := range guarded.Components {
if component.IsReachable {
reachable++
} else if component.IsExported && component.Permission != nil {
permission := *component.Permission
level, _ := guarded.DeclaredPermissionProtectionLevel(permission)
fmt.Printf("%s is exported, but guarded by %s (level %d)\n", component.Name, permission, level)
}
}
fmt.Println(reachable, "of", len(guarded.Components), "components are reachable")
try (Apk guardedApk = Apk.open(Path.of("../../tests/data/reachable.apk"))) {
Manifest guarded = guardedApk.manifest().orElseThrow();
for (Component component : guarded.components()) {
if (component.isExported() && !component.isReachable()) {
String permission = component.permission().orElseThrow();
OptionalInt level = guarded.declaredPermissionProtectionLevel(permission);
System.out.printf("%s is exported, but guarded by %s (level %d)%n",
component.name(), permission, level.getAsInt());
}
}
long reachable = guarded.components().stream().filter(Component::isReachable).count();
System.out.println(reachable + " of " + guarded.components().size()
+ " components are reachable");
}
Two cases stay reachable on purpose:
- A component guarded by a
normalordangerouspermission. Any app can request those. - A component guarded by a permission the manifest does not declare, such as a platform permission. dex++ cannot know its protection level from this APK alone, so it does not assume the component is protected.
declared_permission_protection_level(name) (Go:
DeclaredPermissionProtectionLevel, Java:
declaredPermissionProtectionLevel) returns the raw Android value for a
permission the app declares. For a permission it does not declare, there is no
value: None in Python, false as the second result in Go, an empty
OptionalInt in Java. The low four bits are the base level: 0 normal,
1 dangerous, 2 signature, 3 signatureOrSystem. A protection level given by a
resource reference is resolved like the other values. When it cannot be
resolved, there is no value for that declared permission either, and the
permission does not count as a guard: a component it protects stays reachable.
Resources¶
The resource table is the decoded resources.arsc. An APK may have none, or
one that cannot be decoded: then Python gives None, Go gives nil and Java
gives an empty Optional. The table resolves resource ids to names and to
string, boolean and integer values.
table: dexpp.ResourceTable | None = apk.resources
assert table is not None
app_name: int | None = table.id_of("string", "app_name")
assert app_name is not None
name: dexpp.ResourceName | None = table.name_of(app_name)
assert name is not None
print(hex(app_name), name.package, name.type, name.entry)
print(table.resolve_string(app_name))
print(apk.resolve_string(app_name))
table := apk.Resources()
if table == nil {
log.Fatal("no resource table")
}
appName, ok := table.IDOf("string", "app_name")
if !ok {
log.Fatal("no @string/app_name")
}
name, ok := table.NameOf(appName)
if !ok {
log.Fatal("no name for ", appName)
}
fmt.Printf("%#x %s %s %s\n", appName, name.Package, name.Type, name.Entry)
fmt.Println(table.ResolveString(appName))
fmt.Println(apk.ResolveString(appName))
ResourceTable table = apk.resources().orElseThrow();
int appName = table.idOf("string", "app_name").orElseThrow();
ResourceName name = table.nameOf(appName).orElseThrow();
System.out.printf("%#x %s %s %s%n",
appName, name.packageName(), name.type(), name.entry());
System.out.println(table.resolveString(appName));
System.out.println(apk.resolveString(appName));
| Lookup | Python | Go | Java |
|---|---|---|---|
| the id of a resource, by type and entry name | id_of(type, entry) |
IDOf(type, entry) |
idOf(type, entry) |
| the package, type and entry name of an id | name_of(id) |
NameOf(id) |
nameOf(id) |
| the value of a string resource | resolve_string(id) |
ResolveString(id) |
resolveString(id) |
| the value of a boolean resource | resolve_bool(id) |
ResolveBool(id) |
resolveBool(id) |
| the value of an integer resource | resolve_int(id) |
ResolveInt(id) |
resolveInt(id) |
- Each lookup has no result when the id is missing or its value has another type, such as a string where a boolean is asked for.
- The APK's own
resolve_string(id)(Go:ResolveString, Java:resolveString) is a shortcut for the same string lookup. - Like Android, the boolean and the integer lookups accept any integer-typed
value: a boolean read as an integer gives its raw data (aapt2 writes
trueas -1), and an integer read as a boolean is true when it is not 0.
A resource whose value is a reference to another resource, such as
<bool name="a">@bool/b</bool>, is followed to the end, for up to 20 steps as
Android does. When a resource has several variants, for example one per
language, the value of the default configuration is returned.
Signing¶
The signing information (Python apk.signing, Go apk.Signing(), Java
apk.signing()) tells you which signature schemes are present and which
certificates signed the APK. It never fails: an unsigned APK gives a value
whose is_signed (Go: IsSigned(), Java: isSigned()) is false, with no
certificates.
signed_apk: dexpp.Apk = dexpp.Apk.open("tests/data/signed.apk")
signing: dexpp.SigningInfo = signed_apk.signing
print("signed:", signing.is_signed)
print("v1:", signing.v1, "v2:", signing.v2, "v3:", signing.v3)
for certificate in signing.certificates:
print(certificate.subject)
print("valid", certificate.not_before, "to", certificate.not_after)
print("sha256", certificate.sha256_hex)
signedAPK, err := dexpp.OpenAPK("../../../tests/data/signed.apk")
if err != nil {
log.Fatal(err)
}
defer signedAPK.Close()
signing := signedAPK.Signing()
fmt.Println("signed:", signing.IsSigned())
fmt.Println("v1:", signing.V1, "v2:", signing.V2, "v3:", signing.V3)
for _, certificate := range signing.Certificates {
fmt.Println(certificate.Subject)
fmt.Println("valid", certificate.NotBefore, "to", certificate.NotAfter)
fmt.Println("sha256", certificate.SHA256Hex)
}
try (Apk signedApk = Apk.open(Path.of("../../tests/data/signed.apk"))) {
SigningInfo signing = signedApk.signing();
System.out.println("signed: " + signing.isSigned());
System.out.println("v1: " + signing.v1() + " v2: " + signing.v2()
+ " v3: " + signing.v3());
for (Certificate certificate : signing.certificates()) {
System.out.println(certificate.subject());
System.out.println("valid " + certificate.notBefore()
+ " to " + certificate.notAfter());
System.out.println("sha256 " + certificate.sha256Hex());
}
}
v1 is JAR signing (the META-INF/*.RSA, *.DSA or *.EC files). v2 and
v3 are the APK Signature Schemes; v3 also covers v3.1. A certificate that
appears in several schemes is listed once.
Identification only
dex++ reads the certificates. It does not check that the signatures
are valid or that the file's content matches its digests. Use Android's
apksigner verify when you need to know that an APK is correctly signed.
Archive entries¶
entries() (Go: Entries()) returns the name of every entry, in the order of
the archive's table of contents (the ZIP central directory). Each call reads
the whole table of contents again, and a large app can have thousands of
entries, so keep the list when you need it more than once.
read(name) (Go: Read(name)) returns the uncompressed bytes of one entry.
When the archive has no entry with that name, there are no bytes: None in
Python, false as the second result in Go, an empty Optional in Java.
Limits¶
- ZIP64 archives and encrypted entries are not supported.
- Entries must be stored or deflate-compressed.
- When a name appears more than once in the archive, the first entry is used.