SlideShare a Scribd company logo
Disassembling Dalvik Bytecode
Alain Leon
Background
What is Android?
Android is an operating system by Google that uses a Linux kernel and runs its
applications on a VM, formerly known as Dalvik
The programs that run on Android are packaged and distributed as APK files
Inside each APK file, there is an executable DEX file which is what actually gets run
when the program starts
Android has the largest installed base of all operating systems of any kind
What is Dalvik?
It’s a VM but it’s not the Java VM
Register-based VM made more efficient when running on
battery-powered, relatively low CPU/RAM smartphones
You write Java source that compiles to Java bytecode which then
gets translated to Dalvik bytecode
Successor is Android Runtime (ART), introduced in KitKat (4.4+),
completely replaced Dalvik in Lollipop (5.0+), which
compiles-on-install rather than JIT
What is an APK?
Android Package
This is what you download and install
from the Google Play store
It’s really just a zip file containing an app
Holds the app’s assets and Dalvik
bytecode (in .dex or .odex format)
What is bytecode?
Not machine code
DEX = Dalvik Executable
Intermediate found in Java .class files and
Dalvik .dex files
Translated between .dex and .class using
the dx tool
Machine code is only created at runtime
by the Just-In-Time (JIT) compiler
What is JIT compilation?
Mix between traditional ahead-of-time compiling and interpreting
Machine code is generated during runtime
Combines the speed of compiled code with the flexibility of interpretation
At the cost of overhead of an interpreter + the additional overhead of compiling
Allows for adaptive optimization such as dynamic recompilation
Think re.compile() from Python
What is the Android NDK?
Android Native Development Kit
A set of tools that allow you to leverage C and C++ code in
your Android apps
Uses the Java Native Interface (JNI) to expose Java calls to
underlying system
Used by Cocos2d-x, game development tools written in C++
Cocos is compiled as a shared library and shipped inside the
APK
Hacking at the Surface Level
Use a Macro to “Bot” the Game
Was the goal of my last talk
Use macros or scripts to automate some
repeatable circuit to gain in-game
currencies all day every day
Prone to errors
Slow, human level gain
Too Bad It’s Not Really That Cool
Hacking at the REST Level
Wireshark
Sniff the traffic to and from an Android emulator
Make a malicious imposter client
Replay the get/put/posts using curl or python
Fail: Google Play Services uses OAuth 2.0
Sends ephemeral Base64-URL-encoded token
LinkedIn - Disassembling Dalvik Bytecode
Hacking at the APK/DEX level
Get the APK
Find on Google Play and use that URL at an APK Downloader website or
Enable USB Debugging, install Android SDK, connect your smartphone and:
adb shell pm list packages | grep khux
adb shell pm path com.square_enix.android_googleplay.khuxww
adb pull /data/app/com.square_enix.android_googleplay.khuxww-1/base.apk
DEX Bytecode Disassembling (Baksmaling)
Two ways, recommend doing both:
Directly: Convert to bytecode to a readable format (Baksmali, Jasmine, etc.)
apktool d -f “khux.apk” -o smali
Indirectly: Convert to Java first, then use Java’s decompiling tools
dex2jar -> Java Decompiler (JD-Core, JD-GUI, etc.)
Smali Dalvik Bytecode Representation
Apply Changes
Change variables, convert to hex first!
const/16 v0, 9bff
Output variables to the Android log
const-string v0, "grep_for_this_breh:"
invoke-static {v0, p1}, Landroid/util/Log;->e(Ljava/lang/String;Ljava/lang/String;)I
APK Reassembling
apktool b -f smali/ -o khux_rekt.apk
jarsigner (Android SDK) - sign the apk with your own keystore or..
https://github.com/appium/sign
java -jar sign.jar modded.apk
zipalign (Android SDK) - (optional) ensures that all uncompressed data starts with a
particular alignment relative to the start of the file, reducing app’s RAM footprint
zipalign 4 modded.s.apk aligned.apk
Reinstall the APK
Uninstall the original APK if it’s still on the device
Install the modded APK
adb install aligned.apk
Disable or uninstall Facebook if you’re having problems with Facebook login
Watch the logs
adb logcat | grep grep_for_this_breh
Hacking at the Shared Object Level
Shared Object Analysis
libcocos2dcpp.so was the only meaningful difference
When diff tells you “Binary files differ”, you can convert to hex and try again.
xxd hacked.so > hacked.hex
vimdiff hacked.hex unhacked.hex
You can also try a byte-for-byte comparison
cmp -l file1.so file2.so
This prints out the line number of the changes and their differences in octal
LinkedIn - Disassembling Dalvik Bytecode
Machine Code Disassembly
Get the Android NDK
Find the right objdump for your architecture
For Android smartphones, it’s usually ARM little
endian, arm-linux-androideabi
/path/to/arch/objdump -d haxt.so > haxt.asm
You can also use Hex-Keys IDA Pro (Interactive
Disassembler) for multiarch disassembly
LinkedIn - Disassembling Dalvik Bytecode
Machine Code Decompilation
Bring the .so all the way back up to the C level (Hex-Rays Decompiler)
Vs. disassembling, it’s more readable but it can be inaccurate and it takes much longer.
The End

More Related Content

PDF
Understanding the Dalvik bytecode with the Dedexer tool
PDF
Inc0gnito 2015 Android DEX Analysis Technique
PPTX
Dancing with dalvik
PDF
The dedexer disassembler
PPTX
How to implement a simple dalvik virtual machine
PDF
NDK Primer (Wearable DevCon 2014)
PPTX
Java 7 - New Features - by Mihail Stoynov and Svetlin Nakov
PDF
In Vogue Dynamic
Understanding the Dalvik bytecode with the Dedexer tool
Inc0gnito 2015 Android DEX Analysis Technique
Dancing with dalvik
The dedexer disassembler
How to implement a simple dalvik virtual machine
NDK Primer (Wearable DevCon 2014)
Java 7 - New Features - by Mihail Stoynov and Svetlin Nakov
In Vogue Dynamic

What's hot (20)

PDF
Debugging Python with gdb
PPT
Introduction to llvm
ODP
I Know Kung Fu - Juggling Java Bytecode
PDF
Introduction to the LLVM Compiler System
PDF
차세대컴파일러, VM의미래: 애플 오픈소스 LLVM
PPT
PDF
LLVM Compiler - Link Time Optimization
PPTX
PHP Development Tools 2.0 - Success Story
PPTX
Open arkcompiler
PDF
NDK Primer (AnDevCon Boston 2014)
PDF
Lifting The Veil - Reading Java Bytecode
PDF
Lifting The Veil - Reading Java Bytecode During Lunchtime
PDF
Specialized Compiler for Hash Cracking
PDF
(COSCUP 2015) A Beginner's Journey to Mozilla SpiderMonkey JS Engine
PPTX
C++ CoreHard Autumn 2018. Создание пакетов для открытых библиотек через conan...
PDF
Toward dynamic analysis of obfuscated android malware
PDF
ProbeDroid - Crafting Your Own Dynamic Instrument Tool on Android for App Beh...
PDF
Part II: LLVM Intermediate Representation
PDF
不深不淺,帶你認識 LLVM (Found LLVM in your life)
Debugging Python with gdb
Introduction to llvm
I Know Kung Fu - Juggling Java Bytecode
Introduction to the LLVM Compiler System
차세대컴파일러, VM의미래: 애플 오픈소스 LLVM
LLVM Compiler - Link Time Optimization
PHP Development Tools 2.0 - Success Story
Open arkcompiler
NDK Primer (AnDevCon Boston 2014)
Lifting The Veil - Reading Java Bytecode
Lifting The Veil - Reading Java Bytecode During Lunchtime
Specialized Compiler for Hash Cracking
(COSCUP 2015) A Beginner's Journey to Mozilla SpiderMonkey JS Engine
C++ CoreHard Autumn 2018. Создание пакетов для открытых библиотек через conan...
Toward dynamic analysis of obfuscated android malware
ProbeDroid - Crafting Your Own Dynamic Instrument Tool on Android for App Beh...
Part II: LLVM Intermediate Representation
不深不淺,帶你認識 LLVM (Found LLVM in your life)
Ad

Viewers also liked (18)

PPTX
Let's talk about jni
PDF
Android internals 05 - Dalvik VM (rev_1.1)
PDF
Understanding the Dalvik Virtual Machine
PDF
Hacking your Droid (Aditya Gupta)
DOCX
Smali语法
PPTX
Reverse engineering android apps
PPTX
Toward Reverse Engineering of VBA Based Excel Spreadsheets Applications
PPT
Reverse Engineering Android Application
PPTX
Reverse Engineering .NET and Java
PDF
Android reverse engineering - Analyzing skype
PPT
Android JNI
PDF
How to reverse engineer Android applications—using a popular word game as an ...
PPTX
Garbage Collection of Java VM
PDF
Practice of Android Reverse Engineering
PDF
Learning by hacking - android application hacking tutorial
PDF
Inside Android's Dalvik VM - NEJUG Nov 2011
PPTX
Google ART (Android RunTime)
PPT
IEEE Day 2013 - Reverse Engineering an Android Application
Let's talk about jni
Android internals 05 - Dalvik VM (rev_1.1)
Understanding the Dalvik Virtual Machine
Hacking your Droid (Aditya Gupta)
Smali语法
Reverse engineering android apps
Toward Reverse Engineering of VBA Based Excel Spreadsheets Applications
Reverse Engineering Android Application
Reverse Engineering .NET and Java
Android reverse engineering - Analyzing skype
Android JNI
How to reverse engineer Android applications—using a popular word game as an ...
Garbage Collection of Java VM
Practice of Android Reverse Engineering
Learning by hacking - android application hacking tutorial
Inside Android's Dalvik VM - NEJUG Nov 2011
Google ART (Android RunTime)
IEEE Day 2013 - Reverse Engineering an Android Application
Ad

Similar to LinkedIn - Disassembling Dalvik Bytecode (20)

PPTX
Advance Android Application Development
PDF
lecture-2-android-dev.pdf
PDF
Introduction to the Android NDK
PDF
Mobile development in 2020
PPTX
Getting started with the NDK
PDF
Android Development
PPTX
Toğrul Tağıyev - Müxtəlif dillərdə Android proqramlaşdırma
PPTX
Alternatives to Java for Android development
PPTX
Toğrul Tağıyev - Müxtəlif dillərdə Android proqramlaşdırma
PDF
Beginners guide-to-reverse-engineering-android-apps-pau-oliva-fora-viaforensi...
PPT
Alternatives to Java for Android development
PDF
Getting Native with NDK
PPTX
Using the android ndk - DroidCon Paris 2014
PPTX
Manish Chasta - Securing Android Applications
PDF
Droidcon uk2012 androvm
PPTX
PPTX
OWF12/PAUG Conf Days Alternative to google's android emulator, daniel fages, ...
ODP
Android porting for dummies @droidconin 2011
PDF
Introduction to the Android NDK
DOCX
Android_Studio_Structure.docx
Advance Android Application Development
lecture-2-android-dev.pdf
Introduction to the Android NDK
Mobile development in 2020
Getting started with the NDK
Android Development
Toğrul Tağıyev - Müxtəlif dillərdə Android proqramlaşdırma
Alternatives to Java for Android development
Toğrul Tağıyev - Müxtəlif dillərdə Android proqramlaşdırma
Beginners guide-to-reverse-engineering-android-apps-pau-oliva-fora-viaforensi...
Alternatives to Java for Android development
Getting Native with NDK
Using the android ndk - DroidCon Paris 2014
Manish Chasta - Securing Android Applications
Droidcon uk2012 androvm
OWF12/PAUG Conf Days Alternative to google's android emulator, daniel fages, ...
Android porting for dummies @droidconin 2011
Introduction to the Android NDK
Android_Studio_Structure.docx

LinkedIn - Disassembling Dalvik Bytecode

  • 3. What is Android? Android is an operating system by Google that uses a Linux kernel and runs its applications on a VM, formerly known as Dalvik The programs that run on Android are packaged and distributed as APK files Inside each APK file, there is an executable DEX file which is what actually gets run when the program starts Android has the largest installed base of all operating systems of any kind
  • 4. What is Dalvik? It’s a VM but it’s not the Java VM Register-based VM made more efficient when running on battery-powered, relatively low CPU/RAM smartphones You write Java source that compiles to Java bytecode which then gets translated to Dalvik bytecode Successor is Android Runtime (ART), introduced in KitKat (4.4+), completely replaced Dalvik in Lollipop (5.0+), which compiles-on-install rather than JIT
  • 5. What is an APK? Android Package This is what you download and install from the Google Play store It’s really just a zip file containing an app Holds the app’s assets and Dalvik bytecode (in .dex or .odex format)
  • 6. What is bytecode? Not machine code DEX = Dalvik Executable Intermediate found in Java .class files and Dalvik .dex files Translated between .dex and .class using the dx tool Machine code is only created at runtime by the Just-In-Time (JIT) compiler
  • 7. What is JIT compilation? Mix between traditional ahead-of-time compiling and interpreting Machine code is generated during runtime Combines the speed of compiled code with the flexibility of interpretation At the cost of overhead of an interpreter + the additional overhead of compiling Allows for adaptive optimization such as dynamic recompilation Think re.compile() from Python
  • 8. What is the Android NDK? Android Native Development Kit A set of tools that allow you to leverage C and C++ code in your Android apps Uses the Java Native Interface (JNI) to expose Java calls to underlying system Used by Cocos2d-x, game development tools written in C++ Cocos is compiled as a shared library and shipped inside the APK
  • 9. Hacking at the Surface Level
  • 10. Use a Macro to “Bot” the Game Was the goal of my last talk Use macros or scripts to automate some repeatable circuit to gain in-game currencies all day every day Prone to errors Slow, human level gain Too Bad It’s Not Really That Cool
  • 11. Hacking at the REST Level
  • 12. Wireshark Sniff the traffic to and from an Android emulator Make a malicious imposter client Replay the get/put/posts using curl or python Fail: Google Play Services uses OAuth 2.0 Sends ephemeral Base64-URL-encoded token
  • 14. Hacking at the APK/DEX level
  • 15. Get the APK Find on Google Play and use that URL at an APK Downloader website or Enable USB Debugging, install Android SDK, connect your smartphone and: adb shell pm list packages | grep khux adb shell pm path com.square_enix.android_googleplay.khuxww adb pull /data/app/com.square_enix.android_googleplay.khuxww-1/base.apk
  • 16. DEX Bytecode Disassembling (Baksmaling) Two ways, recommend doing both: Directly: Convert to bytecode to a readable format (Baksmali, Jasmine, etc.) apktool d -f “khux.apk” -o smali Indirectly: Convert to Java first, then use Java’s decompiling tools dex2jar -> Java Decompiler (JD-Core, JD-GUI, etc.)
  • 17. Smali Dalvik Bytecode Representation
  • 18. Apply Changes Change variables, convert to hex first! const/16 v0, 9bff Output variables to the Android log const-string v0, "grep_for_this_breh:" invoke-static {v0, p1}, Landroid/util/Log;->e(Ljava/lang/String;Ljava/lang/String;)I
  • 19. APK Reassembling apktool b -f smali/ -o khux_rekt.apk jarsigner (Android SDK) - sign the apk with your own keystore or.. https://github.com/appium/sign java -jar sign.jar modded.apk zipalign (Android SDK) - (optional) ensures that all uncompressed data starts with a particular alignment relative to the start of the file, reducing app’s RAM footprint zipalign 4 modded.s.apk aligned.apk
  • 20. Reinstall the APK Uninstall the original APK if it’s still on the device Install the modded APK adb install aligned.apk Disable or uninstall Facebook if you’re having problems with Facebook login Watch the logs adb logcat | grep grep_for_this_breh
  • 21. Hacking at the Shared Object Level
  • 22. Shared Object Analysis libcocos2dcpp.so was the only meaningful difference When diff tells you “Binary files differ”, you can convert to hex and try again. xxd hacked.so > hacked.hex vimdiff hacked.hex unhacked.hex You can also try a byte-for-byte comparison cmp -l file1.so file2.so This prints out the line number of the changes and their differences in octal
  • 24. Machine Code Disassembly Get the Android NDK Find the right objdump for your architecture For Android smartphones, it’s usually ARM little endian, arm-linux-androideabi /path/to/arch/objdump -d haxt.so > haxt.asm You can also use Hex-Keys IDA Pro (Interactive Disassembler) for multiarch disassembly
  • 26. Machine Code Decompilation Bring the .so all the way back up to the C level (Hex-Rays Decompiler) Vs. disassembling, it’s more readable but it can be inaccurate and it takes much longer.