November 21, 2026 by debugactiveprocess6 minutes
Android malware ships its logic inside a Dalvik Executable, the classes.dex
file packed into every APK. Writing detections against a DEX with the original
YARA meant treating it as an opaque blob: you searched for raw byte sequences
and hoped the strings you picked survived the next repack. Obfuscators made that
brittle, and a single renamed resource could sink a rule.
YARA-X has a dex module that parses the DEX format properly: instead of
guessing at offsets, you get the file’s string pool, type pool, method and field
references, class definitions and header as structured data. Version 1.24 builds
on it with a set of matching functions, has_string, has_method and
has_class, that turn those parsed pools into one-line checks (and keep
contains_string, contains_method and contains_class as aliases for rules
ported from the original YARA). In this post we use them to take apart a real
Android banking-trojan dropper.
Import it like any other module:
import "dex"
rule is_dalvik {
condition:
dex.is_dex
}dex.is_dex is your gate: it is true only when the file parses as a valid
Dalvik executable, so every rule below starts with it.
What makes the module powerful is how DEX stores code. A DEX file keeps separate pools of the entities the bytecode references: a string pool, a type pool, a method pool, a field pool, and the definitions of the classes this file actually contains. The three matching functions each look at a different pool, and the distinction matters:
dex.has_string(s) searches the string pool. That pool holds string
literals, but also method and field names and every type descriptor, so a
framework type like Landroid/app/AppComponentFactory; shows up here as a
string even when the app only references it.dex.has_method(...) searches the method pool, which lists every method
the code defines or calls. When an app invokes System.loadLibrary(), the
framework method is resolved through a method reference, even though the app
never defines it, so this is how you detect the APIs an app reaches for.dex.has_class(name) matches only the classes defined in this DEX,
that is, the app’s own code. It does not match a framework class the app
merely uses. To test for that, match the class descriptor with has_string,
or match a method on it with has_method.That last point trips people up, so keep it in mind: has_class is for the
app’s own classes, has_method and has_string are for the framework surface
it touches.
Class and method names use the JVM descriptor form: a class is
Lpackage/Name;, and methods are matched by their bare name, optionally scoped
to a class. Each function also takes a regular expression.
Our sample is the second stage of an Android banking-trojan dropper (the
Mamont family), SHA-256
0524d5b51635af3bacead6eb2faacd1d698ae50a71de80a4c2afe574bf12453c
(VirusTotal).
Its classes.dex is tiny, about 11 KB: a stage that does nothing but unpack and
launch the real payload, which is exactly the behavior the dex module makes
easy to see.
Dumping the module output shows the classes the dropper defines:
$ yr dump -m dex classes.dex
dex:
...
class_defs:
- class: "Lcom/vuji/luzudomi/KuvajuhiwabeApp5962;"
superclass: "Landroid/app/Application;"
- class: "Lcom/vuji/luzudomi/TalavovuFactory58e8;"
superclass: "Landroid/app/AppComponentFactory;"
- class: "Lcom/vuji/luzudomi/TalavovuFactory58e8$NoOpProvider;"
superclass: "Landroid/content/ContentProvider;"Three tells are already visible. The package and class names
(KuvajuhiwabeApp5962, TalavovuFactory58e8) are machine-generated gibberish
with hex suffixes, the hallmark of an automated packer. The app subclasses
Application, so it runs code the moment the process starts. And one of its
classes extends Landroid/app/AppComponentFactory;, a startup hook that lets
the dropper intercept component creation before anything else runs, a technique
that is almost never legitimate.
The string and method pools fill in the rest. The dropper carries a custom
injectNclDex string and pulls in the full reflection toolkit
(Class.forName, ClassLoader.loadClass, Method.invoke), writes a file with
FileOutputStream, and loads native code with System.loadLibrary. That is a
complete load-and-run chain: decrypt a payload to disk, load it reflectively,
hand off to native code.
We turn each observation into a line, picking the right function for the pool it lives in:
import "dex"
rule Android_Mamont_Dropper {
meta:
description = "Android banking-trojan dropper (Mamont) that reflectively loads a hidden payload"
sample = "0524d5b51635af3bacead6eb2faacd1d698ae50a71de80a4c2afe574bf12453c"
condition:
dex.is_dex and
// The dropper's own obfuscated classes (has_class: defined classes).
dex.has_class(/^Lcom\/vuji\/luzudomi\/[A-Za-z]+[0-9a-f]{3,};/) and
// A custom routine that injects the second-stage DEX (has_string).
dex.has_string("injectNclDex") and
// Resolves and invokes the payload by reflection (has_method: called APIs).
dex.has_method("Ljava/lang/Class;", "forName") and
dex.has_method("Ljava/lang/reflect/Method;", "invoke") and
// Writes the payload to disk and pulls in native code.
dex.has_method("Ljava/io/FileOutputStream;", "write") and
dex.has_method("Ljava/lang/System;", "loadLibrary")
}Note the division of labor: has_class matches the dropper’s own classes,
while the framework APIs it calls are matched with has_method, and the custom
marker with has_string. Running it confirms the match:
$ yr scan Android_Mamont_Dropper.yar classes.dex
Android_Mamont_Dropper classes.dexThe package name is specific to this sample and will change with the next build.
The behavior will not. The AppComponentFactory startup hook plus a reflective
loader is a strong, reusable signal, and we can key on it directly by iterating
the structured class_defs and reading each class’s superclass:
import "dex"
rule Android_AppComponentFactory_Dropper {
condition:
dex.is_dex and
// Any class that hooks app startup by extending AppComponentFactory.
for any c in dex.class_defs : (
c.superclass == "Landroid/app/AppComponentFactory;"
) and
// ... backed by a reflective load-and-run chain.
dex.has_method("Ljava/lang/Class;", "forName") and
dex.has_method("Ljava/lang/reflect/Method;", "invoke") and
dex.has_method("Ljava/io/FileOutputStream;", "write") and
dex.has_method("Ljava/lang/System;", "loadLibrary")
}This one also matches our sample, and it is not tied to any package name, so it stands a chance against the next variant of the family.
Obfuscators rename an app’s own classes, which is why we matched the package with a regexp above. The regexp overloads work on methods too, and are handy for catching reflective calls or a family of generated names at once:
import "dex"
rule reflective_calls {
condition:
dex.is_dex and
dex.has_method(/Ljava\/lang\/reflect\/(Method|Constructor);/, /invoke|newInstance/)
}If you already maintain DEX rules for the original YARA, the contains_*
aliases let them run unchanged:
import "dex"
rule ported_rule {
condition:
dex.contains_method("loadClass") and
dex.contains_string("injectNclDex")
}The module also exposes the DEX header and a couple of convenience functions, useful for triage and for finding repacks of a known sample:
import "dex"
rule known_repack {
condition:
dex.is_dex and
// SHA-1 signature and Adler-32 checksum from the header.
dex.signature() == "3fbb4237ca06b5b0682362cef22f344832e6c736" and
dex.header.version == 35 and
dex.header.file_size < 20000
}dex.header carries the magic, version, checksum, SHA-1 signature, sizes and
section offsets, while dex.map_list enumerates the item sections in the file.
Pivoting on an exact signature() is a quick way to find byte-identical
repacks, and unusual header values often flag hand-crafted or malformed DEX
files meant to trip up naive parsers.
Matching on raw bytes in a DEX was always a losing game against repacking and
obfuscation. By parsing the format and exposing the string, type, method and
class pools, YARA-X’s dex module lets you write rules against what an app
actually does. Keep the three functions straight, has_class for the app’s own
classes, has_method and has_string for the framework surface it touches, and
reach for class_defs and the regexp overloads when you want to generalize, and
a handful of behavioral indicators joined with and give you detections that
hold up across a whole family rather than a single sample.