Slide 1

Slide 1 text

Attacking Android Applications Dario Incalza @h4oxer

Slide 2

Slide 2 text

$ whoami @h4oxer • Mobile Security Expert @ ZIONSECURITY • Pentesting of mobile applications • Bug bounty hunting • www.darioincalza.be • Tweets @h4oxer

Slide 3

Slide 3 text

ZIONSECURITY • Experts in web, mobile, IoT and infrastructure security • Pentesting – architectural and implementation • Secure Software Development • Security Consultancy • HQ in Rotselaar, Belgium • www.zionsecurity.com @h4oxer

Slide 4

Slide 4 text

Outline • Android Applications 101 • Attack Surfaces • Attacking Applications Statically • Attacking Applications Dynamically • Countermeasures @h4oxer

Slide 5

Slide 5 text

Android Applications 101 @h4oxer

Slide 6

Slide 6 text

Android Platform Stack @h4oxer Linux Kernel Hardware Abstraction Layer (HAL) Native C/C++ Libraries Android Runtime (ART) Core libraries Java API Framework Applications

Slide 7

Slide 7 text

Android Application • .apk - Android Package • Uploaded to Play Store or sideloaded • Can be dumped from the device • A regular .zip file • Always signed @h4oxer

Slide 8

Slide 8 text

APK Content @h4oxer AndroidManifest.xml resources.arsc classes.dex res assets Native C/C++ - .so files

Slide 9

Slide 9 text

Build Process @h4oxer Source Code Resource Files Jar Libraries AAR Libraries Compilers DEX Files Compiled Res

Slide 10

Slide 10 text

DEX Bytecode • Register-based bytecode • Executed by Dalvik / ART Runtime • Compiled to native code - dex2oat • Can be reversed to Java source • Smali bytecode – intermediate bytecode @h4oxer

Slide 11

Slide 11 text

Smali Bytecode @h4oxer

Slide 12

Slide 12 text

Attack Surfaces @h4oxer

Slide 13

Slide 13 text

Attack Models • Reverse engineering offline & online • App on device – without root • App on device – with root • Man-in-the-Middle • Physical access to device @h4oxer

Slide 14

Slide 14 text

Research Questions • How does the app handle authentication? • Access tokens, storage credentials, proper session management • How does the app store user data? • Cryptography, storage location, API keys, exposed content providers • How does the app communicate? • HTTP, HTTPS, Certificate pinning • How does the app protect itself? • Obfuscation, dynamic checks @h4oxer

Slide 15

Slide 15 text

@h4oxer

Slide 16

Slide 16 text

Attacking Applications Statically @h4oxer

Slide 17

Slide 17 text

Reverse Engineering Process @h4oxer APK .so libraries Classes.de x Resources Xml resources unzip AAPT Java Source Smali bytecode dex2jar Baksmali/smal i X86/ARM/x86- 64 Radare2/Hopper/IDAPro

Slide 18

Slide 18 text

$ aapt d xmltree file.apk AndroidManifest.xml @h4oxer

Slide 19

Slide 19 text

$ aapt d xmltree file.apk AndroidManifest.xml @h4oxer

Slide 20

Slide 20 text

$ apktool d file.apk @h4oxer

Slide 21

Slide 21 text

Bytecodeviewer @h4oxer

Slide 22

Slide 22 text

DroidGraph @h4oxer https://github.com/DarioI/droidgraph

Slide 23

Slide 23 text

Cryptography 101 • Symmetric Crypto = one key for encryption/decryption • AES, 3DES, Blowfish, … • Public-key Crypto = private and public key • Encrypt with private key and decrypt with public key = digital signature • Encrypt with public key and decrypt with private key = confidentiality @h4oxer

Slide 24

Slide 24 text

Locate Cryptography • Which crypto library is used? • Android, BouncyCastle, SpongyCastle, etc. • Does it use a hardcoded crypto key? • Does it use broken crypto? • MD5, RC4, AES in EBC, etc. • Where does it store cryptography keys? @h4oxer

Slide 25

Slide 25 text

Locate Cryptography • In Smali bytecode • Ljavax/crypto/Cipher; • Ljavax/crypto/spec/PBEKeySpec; • Ljavax/crypto/SecretKey; @h4oxer

Slide 26

Slide 26 text

Locate Cryptography grep -r "Ljavax/crypto/Cipher" @h4oxer

Slide 27

Slide 27 text

Locate Cryptography @h4oxer

Slide 28

Slide 28 text

@h4oxer

Slide 29

Slide 29 text

Attacking Applications Dynamically @h4oxer

Slide 30

Slide 30 text

Motivation • App might protect itself • Need runtime information • Trigger other code execution paths @h4oxer

Slide 31

Slide 31 text

Tools • Hooking Frameworks • LD_PRELOAD, Frida, xPosed,… • Emulators • Android SDK, Genymotion, Nathan Emulator • Debuggers • Radare2, IDA Pro, GDB, JDB, … • MiTM Proxies • Mitmproxy, Burp, Charles, … @h4oxer

Slide 32

Slide 32 text

Frida • Dynamic instrumentation toolkit • Debug a live process • Loads JavaScript (JS) Engine • Write, inject and execute JS script to interact • Server (on phone) and client (on pc) code @h4oxer

Slide 33

Slide 33 text

Frida – frida_client.py @h4oxer scriptname = sys.argv[1] # get script fd = open(scriptname, "r") # open script procname = sys.argv[2] # get process # define callback function def on_message(message, data): print(message) print(data) #Connect to Frida on the phone session = frida.get_usb_device().attach(procname) #Create script script = session.create_script(fd.read()) fd.close() script.on('message', on_message) script.load()

Slide 34

Slide 34 text

Frida – hooks.js @h4oxer Dalvik.perform(function () { var WebView = Dalvik.use("android.webkit.WebView"); WebView.loadUrl.overload("java.lang.String").implementation = function (s) { send(s.toString()); this.loadUrl.overload("java.lang.String").call(this, s); }; }); $ python frida_client.py hooks.js com.example.webview

Slide 35

Slide 35 text

xPosed Framework • Framework for hooking method calls • Implemented as hooking modules • Separate APK file that hooks into other running applications • Disadvantage: need to build APK for hooking • xPosed bridge jar is injected in every application @h4oxer

Slide 36

Slide 36 text

xPosed Framework @h4oxer public class Example implements IXposedHookLoadPackage { public void handleLoadPackage(final LoadPackageParam lpparam) { if (!lpparam.packageName.equals("com.example.webview")) return; findAndHookMethod("android.webkit.WebView”, lpparam.classLoader, "loadUrl","java.lang.String", new XC_MethodHook() { @Override protected void beforeHookedMethod(MethodHookParam param){ String url = (String) param.args[0]; XposedBridge.log("URL:"+ url); } @Override protected void afterHookedMethod(MethodHookParam param) { // this will be called after } }); } }

Slide 37

Slide 37 text

SSL 101 • SSL Certificate = cryptographically secured piece of information • Certificates are issued by CAs • Android device trusts a set of root Cas • SSL Validation = check if certificate from server is trusted @h4oxer

Slide 38

Slide 38 text

Problem @h4oxer Applicatio n MiTM Server Identify? Identify? Returns Cert Returns MiTM Cert

Slide 39

Slide 39 text

MiTM Attack • Allows you to test/discover/fuzz APIs • Install trusted user certificate • Trusted by all applications on < Android 6.0 • Install trusted system certificate • User certificates are not trusted by all apps on Android 6.0+ • Requires root @h4oxer

Slide 40

Slide 40 text

MiTM Attack @h4oxer $ emulator –avd mitm_device –http-proxy http://localhost:3030 $ mitmproxy –p 3030

Slide 41

Slide 41 text

MiTM Attack @h4oxer

Slide 42

Slide 42 text

Countermeasures @h4oxer

Slide 43

Slide 43 text

Countermeasures • Static Protection • Obfuscators • Packers @h4oxer • Dynamic Protection • Root Detectors • Emulator Detectors • Integrity Checkers • SSL Pinning

Slide 44

Slide 44 text

SafetyNet API • Google’s opinion on device • Result - JWT – JSON Web Token • Result: compatible or not • Can be cryptographically verified • Good protection against dynamic attacks @h4oxer

Slide 45

Slide 45 text

Key Take Aways • Think about a secure design • Apply a layered approach to security • Make sure you hack you application first @h4oxer

Slide 46

Slide 46 text

QA? Thank you! @h4oxer