Quick Baksmali Commands Every Reverse Engineer Should KnowBaksmali is the de facto disassembler for Android DEX bytecode. It translates Dalvik Executable (DEX) files into human-readable smali assembly, letting reverse engineers inspect, analyze, and modify app behavior at the bytecode level. This article covers the essential baksmali commands and workflows every reverse engineer should know, plus practical tips, gotchas, and small examples to speed up real-world analysis.
Why baksmali matters
Baksmali is commonly used alongside smali (the assembler) and tools like jadx, apktool, and dex2jar. While decompilers (jadx, CFR) produce high-level Java-like code, they can miss obfuscated or optimized constructs. Baksmali exposes the actual bytecode, giving you the most accurate representation of runtime behavior and enabling precise patches.
Installation and environment
- Java: baksmali runs on the JVM; have Java 8+ installed.
- Binaries: download the latest baksmali JAR from its GitHub releases (look for baksmali-x.x.x.jar).
- Typical usage is via the java -jar baksmali.jar command or by creating convenient shell aliases/wrappers.
Example alias (bash):
alias baksmali='java -jar /path/to/baksmali-x.x.x.jar'
Basic workflow overview
- Extract classes.dex from an APK (unzip or use apktool).
- Disassemble with baksmali to smali files (one file per class).
- Read and analyze or edit smali.
- Reassemble with smali to create classes.dex.
- Repack, sign, and install the modified APK.
Primary complementary tools:
- apktool — resource and manifest handling; rebuilds APK structure.
- smali — reassembler from smali back to DEX.
- jadx — fast Java-like decompilation for quick reading.
- dex-tools/dexdump — alternative DEX inspectors.
Essential baksmali commands
Note: replace baksmali.jar path with your jar or alias.
- Disassemble a classes.dex into a directory of smali files:
java -jar baksmali.jar disassemble classes.dex -o out/smali
- What it does: produces .smali files organized by package.
- Useful flags:
- -o, –output: output directory.
- –api-level
: target Android API level (affects instruction set/assumptions).
-
Disassemble an APK directly (when classes.dex is inside):
unzip -p app.apk classes.dex > classes.dex java -jar baksmali.jar disassemble classes.dex -o out/smali
Or use apktool to extract APK then baksmali.
-
Disassemble multiple DEX files:
java -jar baksmali.jar disassemble classes.dex classes2.dex -o out/smali
Baksmali will handle multidex, producing smali_classes2 directories if needed.
-
View help and all options:
java -jar baksmali.jar --help
-
Set API level (important for newer instruction sets or optimizations):
java -jar baksmali.jar disassemble classes.dex -o out/smali --api-level 30
Use an API level matching the app’s target/compile SDK to avoid disassembly differences.
-
Output to a single jar (smali + resources) — useful for shipping or quick distribution: Baksmali itself focuses on smali output; for packaging use smali (assemble) and usual zip/jar tooling after reassembly.
Commonly useful flags and options
- –no-parameter-registers: omit parameter register comments for readability.
- –baksmali-jar: (for wrapper scripts) specify path.
- –use-locals: attempt to infer and use local variable names (helpful readability).
- –api-level
: important for correct instruction decoding on newer Android versions. - –jar: disassemble classes from a JAR/AAR (if jar contains classes.dex).
- –help: lists all subcommands and flags.
Check the –help output for the exact flag names matching your baksmali version — some flags change across releases.
Example: Quick modification workflow
- Extract:
unzip -p app.apk classes.dex > classes.dex
- Disassemble:
java -jar baksmali.jar disassemble classes.dex -o smali_out --api-level 30
- Edit the target .smali file (e.g., change return constant or add logging).
- Reassemble:
java -jar smali.jar assemble smali_out -o new_classes.dex
- Replace classes.dex and rebuild APK:
cp new_classes.dex /tmp/app/ cd /tmp/app zip -r ../modified.apk *
- Sign modified APK (apksigner or jarsigner) and install.
Quick smali editing tips
- Always back up original classes.dex.
- Keep edits minimal and test frequently — a misplaced register or wrong invoke-kind will crash at runtime.
- Pay attention to .locals and .param comments when modifying; registers must be consistent.
- Use pseudo-instructions like const/4, const/16, const/high16 appropriately for constants.
- When adding methods, update the class definition headers (method count) correctly — using smali to reassemble catches many issues.
Troubleshooting common errors
- “smali: invalid instruction” — likely wrong API level or corrupted dex; try a different –api-level.
- Reassembly fails with register or label errors — check .locals and ensure labels are unique and branch targets exist.
- Runtime crashes after reassembly — examine logcat for VerifyErrors, IncompatibleClassChangeError, or NoSuchMethodError to pinpoint incorrect edits.
Quick reference cheat-sheet (commands)
- Disassemble a single dex: java -jar baksmali.jar disassemble classes.dex -o out/smali
- Disassemble multiple dex (multidex): java -jar baksmali.jar disassemble classes.dex classes2.dex -o out/
- Disassemble with API level: java -jar baksmali.jar disassemble classes.dex -o out/ –api-level 31
- Show help: java -jar baksmali.jar –help
Best practices and safety
- Use version control for smali edits (store smali files in a git repo).
- Prefer small, incremental changes and automated tests where possible.
- Combine baksmali with decompilers (jadx) for quicker context; use baksmali when precise bytecode-level control is needed.
- For obfuscated apps, search for string constants and anonymous inner classes; smali’s explicit references help trace flows.
Further reading and tools to pair with baksmali
- smali (assembler) — for reassembly.
- apktool — resource/manifests and rebuild convenience.
- jadx — high-level decompilation for context.
- dex-tools, dexdump — alternative DEX utilities.
- Bytecode patching scripts—useful for automating repetitive edits.
Baksmali is a lightweight but powerful tool in the reverse engineer’s toolbox. Mastering its common commands, flags, and the minimal edit/reassemble workflow will greatly speed up accurate, low-level analysis and patching of Android apps.
Leave a Reply