Skip to main content
Bytecode (Python/.NET) & Android APK/Smali Reverse Engineering
Lesson 6 of 11

Bytecode (Python/.NET) & Android APK/Smali Reverse Engineering

Disassemble and decompile Python .pyc files, inspect .NET CIL assemblies, unpack Android APKs with jadx and apktool, and read or patch register-based Smali bytecode.

Advanced 32 min Reverse engineering
Authorised environments only — The techniques described here are used only in training labs and on systems you own or have written permission to test.

Learning objectives

  • Explain how stack-based Python/.NET bytecode and register-based Dalvik/Smali bytecode preserve metadata compared to native binaries
  • Disassemble Python 3 .pyc code objects using the standard dis and marshal modules when source decompilers fail
  • Choose appropriately between jadx for Java decompilation and apktool for Smali disassembly, patching, and rebuilding
  • Trace registers, type descriptors, conditional branches, and JNI native calls in Android Smali code

Recommended first: ELF & PE Binary Formats: Headers, Sections & ImportsStatic Analysis & Decompilation with Ghidra & IDA ConceptsDynamic Analysis & Debugging (GDB, ltrace, x64dbg)

#Why this matters

Not every target compiles straight to x86-64 or ARM64 machine code. Desktop utilities, enterprise tools, CTF challenges, and mobile applications frequently ship as bytecode: intermediate instructions interpreted or just-in-time compiled by a virtual machine such as CPython, the .NET Common Language Runtime (CLR), or the Android Runtime (ART).

Opening a .pyc file, a .NET .exe, or an Android .apk in Ghidra wastes time because native disassemblers expect raw CPU opcodes, not VM bytecode. Specialized bytecode tools recover full class names, method signatures, and near-original source code in seconds.

#Python bytecode (.pyc) and stack-based VMs

CPython compiles .py source files into .pyc bytecode caches before running them on a stack-based virtual machine. Since Python 3.7, a .pyc file starts with a 16-byte header (4-byte Python version magic number, 4-byte bitfield, and 8 bytes for timestamp/size or a content hash), followed by a marshal-serialized code object containing bytecode (co_code), constants (co_consts), and local names (co_varnames).

Automated decompilers such as uncompyle6 (which targets Python 2.7 through 3.8) or pycdc reconstruct .py source automatically. When you hit a newer Python version (3.11+) or obfuscated bytecode that breaks uncompyle6, skip the 16-byte header and inspect the raw instructions with Python's built-in marshal and dis modules (using the same Python minor version on trusted lab files, since marshal is not safe for untrusted hostile inputs):

Disassembling a lab .pyc file with Python 3
$ python3 -c "import marshal, dis; f=open('gate.pyc','rb'); f.seek(16); dis.dis(marshal.load(f))"
  1           0 RESUME                   0
  2           2 LOAD_CONST               1 ('S4mSU')
              4 STORE_FAST               1 (prefix)
  3           6 LOAD_FAST                0 (token)
              8 LOAD_FAST                1 (prefix)
             10 COMPARE_OP               2 (==)
             14 POP_JUMP_IF_FALSE        1 (to 18)
             16 RETURN_CONST             2 (True)
        >>   18 RETURN_CONST             3 (False)

Even without uncompyle6, the stack machine reads clearly: load 'S4mSU' into local prefix, push token and prefix, compare for equality (==), and return True if equal or False otherwise. Note also that packaged executables built with PyInstaller bundle the Python interpreter and .pyc archives inside a native PE or ELF wrapper, which tools like pyinstxtractor unpack back into .pyc files.

#.NET assemblies and Common Intermediate Language (CIL)

A .NET .exe or .dll uses the standard Windows PE container you saw in the binary formats lesson, yet Ghidra shows little native logic inside .text. A quick import check reveals mscoree.dll importing _CorExeMain (or _CorDllMain), which hands execution to the CLR.

Inside the PE sits a CLR header, rich metadata tables (preserving namespaces, class names, field types, and method signatures), and stack-based Common Intermediate Language (CIL) bytecode. Tools such as ILSpy (cross-platform via AvaloniaILSpy) decompile CIL back to clean C#. When developers apply obfuscators (such as symbol renaming or control-flow flattening), reading the CIL opcodes (ldstr, call, brfalse) directly in ILSpy helps untangle encrypted string decoders.

#Android APK anatomy: jadx vs apktool

An Android .apk is a ZIP archive containing:

  • AndroidManifest.xml: Binary-encoded XML declaring permissions, exported activities, services, and android:debuggable.
  • classes.dex (and classes2.dex for multidex apps): Dalvik Executable bytecode run by ART.
  • resources.arsc and res/: Compiled string tables, layouts, and assets.
  • lib/<abi>/*.so: Native C/C++ shared libraries (for example arm64-v8a) loaded via JNI.
  • META-INF/: APK signature metadata.
  1. ExtractInspect APK ZIP layout
  2. Decompilejadx -> readable Java source (read-only)
  3. Disassembleapktool d -> Smali + decoded XML (editable)
  4. NativeGhidra -> lib/arm64-v8a/*.so JNI routines
Tool Primary output Can rebuild APK? Best used for
jadx / jadx-gui Decompiled Java source + decoded manifest No Fast reading, cross-referencing classes, finding secrets or logic flaws
apktool Smali assembly + decoded XML resources Yes (apktool b) Patching bytecode/manifest flags and repacking test APKs

#Reading and patching Smali bytecode

Unlike CPython and .NET CIL, Dalvik/ART bytecode is register-based. Each method declares local registers (v0, v1, ...) and parameter registers (p0, p1, ...). In an instance method, p0 always holds this; in a static method, p0 is the first explicit argument.

Smali uses compact JVM-style type descriptors: V (void), Z (boolean), I (int), J (long), Ljava/lang/String; (String), and [B (byte[]). Consider a decompiled lab check in Smali:

smali
.method public isLabUnlocked(I)Z
    .locals 1
    const/16 v0, 0x539
    if-ne p1, v0, :cond_denied
    const/4 v0, 0x1
    return v0
    :cond_denied
    const/4 v0, 0x0
    return v0
.end method

Here isLabUnlocked(I)Z takes one int (p1, because p0 is this) and returns a boolean (Z). It loads 0x539 (1337) into v0; if p1 != v0 (if-ne), it jumps to :cond_denied and returns 0 (false).

If an authorised test requires patching that check with apktool, you unpack with apktool d lab.apk, change if-ne to if-eq (or change the denied branch's const/4 v0, 0x0 to 0x1), rebuild with apktool b lab -o patched.apk, align with zipalign, and sign with apksigner before installing on an emulator. When Java/Smali calls System.loadLibrary("verify") and declares a native method, the logic moves into lib/<abi>/libverify.so under an exported JNI name such as Java_com_example_lab_Gate_checkPin, where Ghidra and GDB take over.

#Defending and common mistakes

From a defender's viewpoint, bytecode makes reverse engineering straightforward. Obfuscators (ProGuard/R8 on Android, ConfuserEx on .NET) rename classes to a.b.c and strip debug filenames, which slows reading but never protects secrets or authorization checks. Never embed API secrets, signing keys, or premium/admin enforcement inside .pyc, .NET, or .apk client code; enforce authentication and authorization on the server.

From an analyst's viewpoint, avoid these frequent errors:

  • Using the wrong tool for the runtime: Opening an APK or .NET assembly in Ghidra instead of jadx/apktool or ILSpy hides high-level metadata and type signatures.
  • Assuming uncompyle6 supports every Python version: Modern Python 3.9+ bytecode changes opcode formats frequently; fall back to dis or version-matched tools like pycdc.
  • Forgetting p0 rules in Smali: Treating p0 as the first explicit argument inside an instance method (where p0 is this) shifts every parameter analysis by one slot.
  • Installing an unsigned rebuilt APK: Running apktool b produces an unsigned archive that Android rejects (INSTALL_PARSE_FAILED_NO_CERTIFICATES) until aligned with zipalign and signed with apksigner.

#Summary

Python .pyc files, .NET CIL assemblies, and Android DEX bytecode preserve rich structural metadata that makes dedicated tools (dis, uncompyle6, ILSpy, jadx, apktool) far more effective than native disassemblers. Python and .NET run on stack-based VMs, whereas Android Smali uses a register-based model (v* locals and p* parameters) alongside explicit type descriptors. When mobile apps offload sensitive checks to .so libraries via JNI, combine jadx/apktool for the managed layer with Ghidra and dynamic debugging for the native layer.

Knowledge check

Sign in to take the test, complete lessons and save your progress. Log in

Practice: test your knowledge