Repository avatar
Other Tools
v2.1.1
active

deoptfuscator

io.github.Gyoonus/deoptfuscator

Undo counter-based control-flow obfuscation in Android apps and read their code statically.

Documentation

Deoptfuscator

CI Latest release

Deoptfuscator removes a specific control-flow obfuscation technique from Android apps: opaque predicates driven by static counter fields (see How it works). It was selected for BlackHat Arsenal USA 2020 and is described in the IEEE Access paper "Deoptfuscator: Defeating Advanced Control-flow Obfuscation Using Android Runtime (ART)". Coding agents can use it too: an MCP server lets them deobfuscate an APK, read its code and record the names they work out. Everything is static, and no APK is installed or run.

The deoptfuscator command on the obfuscated 2buntu fixture: a method full of opaque predicates, the same method after deobfuscate, and a recorded class name

This is real output on the obfuscated test fixture. It shows one method with its opaque predicates (the counter field accesses are highlighted), the same method after deobfuscate, and a recorded name. The animation is made with scripts/make_demo.py.

Quick start

For agents in Claude Code, install the plugin: the MCP server and a skill that tells the agent how to use it (more clients and the tools):

/plugin install deoptfuscator --marketplace Gyoonus/deoptfuscator

To deobfuscate an APK from the command line, use the Docker image (linux/amd64), which has every tool:

docker run --rm -v "$PWD:/work" ghcr.io/gyoonus/deoptfuscator app.apk
# -> app_deobfuscated.apk, zipaligned and signed with a debug key

Or build it yourself with docker build -t deoptfuscator . and run deoptfuscator instead of ghcr.io/gyoonus/deoptfuscator. Without Docker, use the release tarball for Linux or macOS.

Options:

deoptfuscator.py APK [-o OUT.apk] [-c CONFIG] [-k KEEP_RULES]
                     [--keystore KS --ks-pass pass:...]
                     [--no-sign] [--report FILE] [--json]
                     [--keep-workdir] [-v]
  • config/default.json (default) removes the opaque predicates and cleans up method bodies only: constant propagation, dead code elimination, copy propagation, block deduplication and register allocation. Classes and methods stay as they are.
  • config/aggressive.json additionally runs the Redex optimizations the v1 pipeline used (inlining, CSE, peephole, ...), for smaller, easier-to-read code that is further from the original structure.

v2 is a re-implementation on top of Redex. It needs no AOSP checkout, no prebuilt ART libraries and no Git LFS, and it builds and runs on current Linux. The original ART-based implementation (v1) that the paper describes is kept at tag v1.

Use with agents (MCP)

deoptfuscator mcp serves Deoptfuscator to coding agents as an MCP server (stdio). An agent can deobfuscate an APK, then read its code: search classes and strings, show bytecode, follow cross-references, and record the names it works out for obfuscated classes, methods and fields. Everything is static: no APK is installed or run.

Claude Code, as a plugin with the server and a skill:

/plugin install deoptfuscator --marketplace Gyoonus/deoptfuscator

On Claude Code older than 2.1.275, add the marketplace first: /plugin marketplace add Gyoonus/deoptfuscator, then /plugin install deoptfuscator@deoptfuscator. Auto-update is off by default for marketplaces other than Anthropic's: turn it on in /plugin (Marketplaces), or update with claude plugin update deoptfuscator@deoptfuscator. The skill, analyze-apk, gives the agent the workflow below and the tool's limits, and tells it to treat text inside an app as data, not instructions. Without the plugin, copy it to ~/.claude/skills/analyze-apk/.

Claude Code, the server only (use either this or the plugin, not both):

claude mcp add deoptfuscator -- uvx deoptfuscator mcp

Other MCP clients: the server is listed in the MCP Registry as io.github.Gyoonus/deoptfuscator, or configure it by hand:

{
  "mcpServers": {
    "deoptfuscator": {
      "command": "uvx",
      "args": ["deoptfuscator", "mcp"]
    }
  }
}
ToolWhat it does
overviewPackage, manifest components, permissions, largest packages, share of obfuscated-looking class names
deobfuscateRemoves the counter-based opaque predicates; writes app_deobfuscated.apk and a JSON report next to the input (needs redex-all)
list_classes, searchClasses; string constants (with the methods using them) and names
showA class or method as Dalvik bytecode, with access flags, overrides, and every reference to app code tagged with its id (# m345)
xrefsCallers of a method, readers and writers of a field, subclasses and users of a class
renameRecords a name, with the evidence, for a class, method or field (see below)
export_namesWrites the names as a ProGuard mapping

Classes, methods and fields are referred to by short ids (c12, m345, f6) that stay the same for a given file, or by descriptor or Java name. After deobfuscate, a method that was 34 instructions of opaque predicates in the 2buntu fixture reads:

class c75 public final Lo/ˎˎ; extends Landroid/database/sqlite/SQLiteOpenHelper;
  method m331 public final onCreate(Landroid/database/sqlite/SQLiteDatabase;)V  // overrides Landroid/database/sqlite/SQLiteOpenHelper;
      0000: const-string v0, "CREATE TABLE article (_id INTEGER PRIMARY KEY AUTOINCREMENT, ...);"
      0002: invoke-virtual v2, v0, Landroid/database/sqlite/SQLiteDatabase;->execSQL(Ljava/lang/String;)V
      0005: return-void

Names given with rename are kept in app.names.json next to the APK, shared by app.apk and app_deobfuscated.apk, and shown in place of the obfuscated ones from then on (Lo/ˎˎ; becomes Lcom/twobuntu/db/ArticleHelper;; inner classes follow their outer class's name). A method is renamed together with the methods it overrides or is overridden by; methods that override Android platform methods keep their names, and names that would clash are refused. The APK itself is never modified.

Requirements: Python 3.10 or later. uvx installs the deoptfuscator package from PyPI with androguard and mcp, and a newer Python if yours is older. If the first start takes longer than the client waits (/mcp shows the server failed or still reconnecting), run uvx deoptfuscator --version once in a terminal, then reconnect. deobfuscate also needs redex-all on PATH or in $REDEX, from the release tarball for Linux x86_64 or macOS (Apple silicon, macOS 15 or later), or built from source. The macOS binaries are not notarized: download the tarball with curl or gh, or after a browser download run xattr -dr com.apple.quarantine on the extracted directory. Platform overrides are recognized with the Android SDK's android.jar, found through ANDROID_HOME (without it, only java.lang.Object's methods are known).

Every tool is also a command, for agents that only have a shell (pip install deoptfuscator, or prefix each command with uvx):

deoptfuscator overview app.apk
deoptfuscator deobfuscate app.apk      # app_deobfuscated.apk, unsigned, and a report
deoptfuscator show app_deobfuscated.apk m331
deoptfuscator rename app.apk c75 com.twobuntu.db.ArticleHelper --note "SQLiteOpenHelper of the twobuntu database"

For scripts, deoptfuscator deobfuscate APK --json and deoptfuscator APK --json print the result as one JSON object: the output APK, the report's path, and the counts (opaque predicates resolved, counter fields removed, candidates kept, by reason). deoptfuscator APK --report FILE writes the pass's full report. The commands exit with 0 on success, 1 on an error (the message goes to stderr, and with --json also to stdout as {"error": ...}) and 2 on a usage error.

Example: an agent on the 2buntu fixture

An agent got only tests/fixtures/2buntu/obfuscated.apk, these tools and a one-paragraph task: find out what the app does, and recover the original names of its obfuscated classes, methods and fields. One session took 68 turns, 18 minutes and $9.63 of API use. In it, the agent did the following:

  • It called deobfuscate first and read only the deobfuscated APK after that.
  • It named all 82 obfuscated classes and all 182 obfuscated methods that do not override platform methods. It also named 301 of the 314 fields. The other 13 are synthetic this$0 fields, a name that the server refused at the time; it accepts them now.
  • It described the app correctly as a news reader for 2buntu.com, and named the bundled libraries: Universal Image Loader, parts of Apache Commons IO, and Android support code.

Checked against the original names, it got 172 of 178 right in the bundled image loader and 21 of 30 in the app's own code. Both are public open source, so some of this may be the model remembering them rather than reading the code. For code a model has never seen, take these numbers as an upper bound.

The same session without redex-all, where the agent read the obfuscated code throughout, was about as accurate but covered less: 153 methods and 265 fields. The opaque predicates make methods longer, so show cut 27 of its 145 listings at 400 lines, against 2 of 123 after deobfuscation. Sessions with other models and efforts cost from $0.19 to $23.97. Effort mattered: at medium effort, the two cheaper models named mostly classes and then stopped. Each of these results comes from a single session. The task, harness, transcripts and scoring are in the experiment's write-up.

How it works

The obfuscation technique Deoptfuscator targets drives opaque predicates with private static int counter fields. Counters are updated from each other with parity-preserving arithmetic, and the inserted branches test the parity of the result:

# <clinit>: a = 0 (even), b = 1 (odd)
sget v1, Lo/AuX;->ॱˎ:I        # a, always even
add-int/lit8 v1, v1, 0xf
rem-int/lit16 v2, v1, 0x80
sput v2, Lo/AuX;->ॱˋ:I        # b = (a + 15) % 128, always odd
rem-int/lit8 v1, v1, 0x2
if-nez v1, :cond_22           # (a + 15) is always odd: always taken

Only one side of each branch is the original code; the other side is junk (in the test app, for example, code that divides by zero). The parity invariant guarantees that the original side is always taken.

Deoptfuscator fixes each counter to the value its class initializer gives it and lets an optimizing compiler fold the predicates away. v1 used ART's optimizing compiler for this and assumed the rewrite was safe. v2 does it in one Redex pass, DeoptfuscatorPass (redex-pass/), and only rewrites a counter when it can prove that this preserves the app's behaviour:

  1. Candidates are non-final, non-volatile private static int fields.
  2. Values read from a candidate may only flow through parity-preserving integer arithmetic into if-eqz / if-nez tests of x % 2 or x & 1, or into writes to other candidates. Values written to it may only be computed from constants and candidates.
  3. Every write must keep the parity of the field's initial value. The pass checks this by evaluating each written expression over parities, and it checks that no read can happen before <clinit> stores the initial value.
  4. Then the parity is the same at every read, so every read is replaced by the initial value and every write is deleted, along with the field. Redex's ConstantPropagationPass and LocalDcePass fold the predicates and drop the junk branches.

Redex's IR type checker runs after every pass, so a broken transformation aborts the run instead of producing an invalid APK.

The pass writes a report of the fields it removed (and of the candidates it kept, with the reason). --report FILE saves it; the deobfuscate tool and subcommand write it next to the output APK.

Redex is run with config/keep-all.pro, which keeps every class and member, so nothing is removed or renamed just because Redex cannot see it used (pass your own rules with --keep-rules).

Results

On the obfuscated 2buntu app in tests/fixtures/2buntu, measured with tests/dexmetrics.py:

obfuscatedv1 (2020)v2 defaultv2 aggressive
opaque branches5855600
counter updates58557700
switches4341199
instructions19,94013,4067,3457,010
methods437437437380

opaque branches counts rem-int ..., 2 results that decide an if-eqz / if-nez, and counter updates counts rem-int ..., 128 counter updates. v2 proves all 585 predicates safe to fold and leaves one candidate field alone, a real program field.

On an app obfuscated with Obfuscapk's control-flow obfuscators (ArithmeticBranch, Goto, Reorder): ringdroid, in tests/fixtures/obfuscapk:

originalobfuscatedv2 on originalv2 on obfuscated
conditional branches394592388388
instructions9,42432,2319,0629,064
mean method size21.874.420.920.9

These predicates are not counter-based, so DeoptfuscatorPass resolves none of them. The rest of the default pipeline removes all 198 inserted branches: constant propagation, dead code elimination and the other cleanup passes. The obfuscated app ends with as many branches as the original one after the same cleanup.

Building without Docker

You need the Redex build dependencies (Ubuntu 22.04 or later):

sudo apt-get install cmake ninja-build g++ git python3 \
  binutils-dev libiberty-dev libjemalloc-dev libjsoncpp-dev liblz4-dev \
  liblzma-dev zlib1g-dev libprotobuf-dev protobuf-compiler \
  libboost-filesystem-dev libboost-iostreams-dev libboost-program-options-dev \
  libboost-regex-dev libboost-system-dev libboost-thread-dev \
  zipalign apksigner dexdump

scripts/build_redex.sh              # Redex at the commit in REDEX_COMMIT
export REDEX=$PWD/build/redex-build/redex-all
python3 deoptfuscator.py app.apk
python3 tests/test_golden.py        # golden test against v1's output

The first build compiles all of Redex (about an hour on 4 cores); after changing the pass, re-running the script only recompiles the pass.

Limitations

  • DeoptfuscatorPass handles the counter-based opaque predicates described above. Predicates that constant propagation can fold on its own, such as Obfuscapk's ArithmeticBranch, are removed by the rest of the pipeline (see Results). Other control-flow obfuscation (for example control-flow flattening), identifier renaming, string encryption, class encryption and layout obfuscation are not undone. The agent tools help recover names, but they do not undo renaming in the APK.
  • The safety argument covers bytecode only: code that reads the counter fields through reflection or JNI would see them gone.
  • The output is signed with the debug key in deoptfuscator.keystore (password 123456) and is meant for analysis.

Publication

@article{you2022deoptfuscator,
    title = "Deoptfuscator: Defeating Advanced Control-flow Obfuscation Using Android Runtime (ART)",
    journal = "IEEE Access",
    volume = "10",
    pages = "61426-61440",
    year = "2022",
    issn = "2169-3536",
    doi = "10.1109/ACCESS.2022.3181373",
    url = "https://ieeexplore.ieee.org/document/9791370",
    author = "You, Geunha and Kim, Gyoosik and Han, Sangchul and Park, Minkyu and Cho, Seong-je",
    keywords = "Android app, malicious app, obfuscation, deobfuscation, control-flow obfuscation"
}

Contact

  • E-mail: gyoonus at gmail dot com [Gyoosik Kim (김규식)]

Acknowledgement

This research was supported by Basic Science Research Program through the National Research Foundation of Korea (NRF) funded by the Ministry of Science and ICT (no. 2018R1A2B2004830).

NRF

PYPI
deoptfuscator
Install Command
pip install deoptfuscator
Runtime: uvx