Droidcon it-2014-marco-grassi-viaforensics

60
REVERSE ENGINEERING, PENTESTING AND HARDENING OF ANDROID APPS Droidcon IT Torino 2014 Marco Grassi @marcograss - Mobile Security Analyst @ viaForensics 1

Transcript of Droidcon it-2014-marco-grassi-viaforensics

Page 1: Droidcon it-2014-marco-grassi-viaforensics

REVERSE ENGINEERING, PENTESTING AND HARDENING OF ANDROID APPS

Droidcon IT Torino 2014 !

Marco Grassi @marcograss

- Mobile Security Analyst @ viaForensics

���1

Page 2: Droidcon it-2014-marco-grassi-viaforensics

$ whoami

• R&D Team Member @ viaForensics

• Developer background (both Android and iOS)

• Part of my job is to attack and break mobile apps

���2

Page 3: Droidcon it-2014-marco-grassi-viaforensics

���3

Black Box Approach

=

We can use the app, Dynamic Analysis,

Inspection

+

Reverse Engineering, Static Analysis(Mainly)

APK

Page 4: Droidcon it-2014-marco-grassi-viaforensics

AGENDA• Reverse Engineering and Obfuscation

• Tampering Detection

• Logging

• File Storage

• Secure Network Communications

• IPC Attack Surface

• RAM memory attacks

• More Advanced Material : Runtime Manipulation

• Extra: Creating Cheats for Android Games : )

���4

REAL WORLD EXAMPLES

Page 5: Droidcon it-2014-marco-grassi-viaforensics

SANTOKU LINUXhttps://santoku-linux.com/

���5

Page 6: Droidcon it-2014-marco-grassi-viaforensics

PULLING THE APK FROM THE DEVICE

���6

• Often the APKs are downloaded from Google Play on the device, how can we extract them? Some solutions:

1. adb backup -apk com.mypackage (Works on Android 4.0 and newer)

2. Use a backup application (ASTRO File manager, Titanium Backup…)

3. adb shell , cd /data/app/, find your apk, then you can pull it with adb pull /data/app/mypackage.apk (requires a adb root shell on the device)

Page 7: Droidcon it-2014-marco-grassi-viaforensics

REVERSE ENGINEERING FREE TOOLS

• apktool and smali/baksmali

It will provide us a disassembled representation of the Dalvik bytecode, so sort “low level”, with registers, but very understandable because of bytecode metadata. Very useful to disable tampering protections, the code can be modified and the application can be recompiled and resigned.

���7

Page 8: Droidcon it-2014-marco-grassi-viaforensics

DISASSEMBLED SMALI CODE

���8

Page 9: Droidcon it-2014-marco-grassi-viaforensics

REVERSE ENGINEERING FREE TOOLS

• dex2jar + Java decompiler (jd-gui, jad …)

dex2jar will convert the .dex file to a .jar containing Java code

We can then use the freely available Java decompilers and obtain back a Java representation of the code.

Very readable if no obfuscation is in place.

���9

Page 10: Droidcon it-2014-marco-grassi-viaforensics

DECOMPILED JAVA CODE

���10

JD-GUI

Page 11: Droidcon it-2014-marco-grassi-viaforensics

REVERSE ENGINEERING PRO TOOLS

• JEB Decompiler

Renaming feature, very handy with obfuscated applications

Python APIs

Native Dalvik decompiler, it does not pass through Java byte code, decompilation is usually much better

���11

Page 12: Droidcon it-2014-marco-grassi-viaforensics

REVERSE ENGINEERING PRO TOOLS

• IDA + Hex Rays Decompiler

De facto the best interactive disassembler and decompiler on the market.

Impressive set of APIs, you can write modules or scripts for everything.

���12

Page 13: Droidcon it-2014-marco-grassi-viaforensics

REVERSE ENGINEERING PRO TOOLS

• Hopper Disassembler

Very nice disassembler and decompiler with a killer price.

���13

Page 14: Droidcon it-2014-marco-grassi-viaforensics

OBFUSCATION PROGUARD

• Free

• Integrated into the build environment

• NOT Android specific

• http://developer.android.com/tools/help/proguard.html

���14

Page 15: Droidcon it-2014-marco-grassi-viaforensics

DECOMPILED CODE WITH PROGUARD

���15

Page 16: Droidcon it-2014-marco-grassi-viaforensics

OBFUSCATION DEXGUARD

• Commercial product from ProGuard author.

• Android specific

• Native support to string and code encryption and tamper detection

• Very easy to use, with a config file like ProGuard

���16

Page 17: Droidcon it-2014-marco-grassi-viaforensics

DECOMPILED CODE WITH DEXGUARD

���17

Page 18: Droidcon it-2014-marco-grassi-viaforensics

TAMPERING DETECTION

���18

• Check at runtime if the application has been modified in any way or if the signature is changed.

• It can be done with the PackageManager class.

• Do the checks in multiple code points and use obfuscation, to avoid that it can be easily bypassed.

• If your app ships only through Google Play, check with the APIs that it has been installed from Google Play and not from Unknown Sources.

• If something is wrong, close the application without leaking informations where the protection code is, to make attacker’s life harder.

Page 19: Droidcon it-2014-marco-grassi-viaforensics

DEFEATING TAMPERING DETECTION WHY OBFUSCATION IS FUNDAMENTAL

���19

Why spend hours on implementing if our application has been modified, if there is a single point of failure?

!

If the attacker can easily find the code, it can modify the application and disable it.

Page 20: Droidcon it-2014-marco-grassi-viaforensics

LOGGING

• Remove Logcat logging from your production builds.

• It can be done with few lines in Proguard and Dexguard, they remove all the calls to Log.d, Log.e etc in the build process

• It’s very easy for third party malware or an attacker to access the Logs on Android.

���20

Page 21: Droidcon it-2014-marco-grassi-viaforensics

FILE STORAGE EXTERNAL STORAGE

• Try to avoid storing your data in the shared storage, almost any application can read it. (In 4.4 a small protection at permission level was added android.permission.READ_EXTERNAL_STORAGE, usually users does not check permissions too much anyway… Don’t rely on this.)

���21

My Personal Data stored in a Evernote Note, publicly readable by anyone.

CASE STUDY

Page 22: Droidcon it-2014-marco-grassi-viaforensics

FILE STORAGE PRIVATE APP FOLDER

• Encrypt your preferences/files

• With root access they can be modified, avoid store sensitive data at all if possible

• With a backup, they can be retrieved from the device usually

• The private folder can be found on the device at path /data/data/yourpackage

���22

That’s right.. It’s my User and my 36 character Password in PLAIN TEXT

CASE STUDY

Page 23: Droidcon it-2014-marco-grassi-viaforensics

FILE STORAGE SQLITE DATABASES

���23

shell@hammerhead:/ $ pm list packages | grep easy package:com.handyapps.easymoney shell@hammerhead:/ $ exit $ adb backup -apk com.handyapps.easymoney unpack the backup with https://github.com/nelenkov/android-backup-extractor

PASSCODE IN PLAIN TEXT RETRIEVED. FAIL!

CASE STUDY

Page 24: Droidcon it-2014-marco-grassi-viaforensics

SQLCIPHER

���24

http://sqlcipher.net/sqlcipher-for-android/

Very easy to use encrypted SQLite database. Don’t store the key with the safe.

The user must provide the password to access the content if possible.

Page 25: Droidcon it-2014-marco-grassi-viaforensics

#1 RULE: YOU DO NOT IMPLEMENT

YOUR OWN CRYPTOGRAPHY

#2 Rule: You do NOT implement your own Cryptography

���25

Page 26: Droidcon it-2014-marco-grassi-viaforensics

SECURE NETWORK COMMUNICATIONS• It’s your responsibility to protect data in transit!

• Don’t transmit sensitive information without SSL/TLS

• Implement if possibile Certificate Pinning, in this way your communications will be more resistant to MITM attacks, for example if a malicious certificate is pushed into the device, or if an attacker can impersonate your web service with a trusted certificate.

���26

Page 27: Droidcon it-2014-marco-grassi-viaforensics

IPC ATTACK SURFACE THE ANDROID MANIFEST

���27

• Avoid the flag android:debuggable=true in production, an attacker can attach with a debugger and execute arbitrary code in your app.

• Double check your exported components. Export a component to other processes only if it’s strictly necessary and at least protect the component with a permission. Android has some permissive defaults, some components are exported even if they are not declared exported=true, check the documentation.

• If you export a content provider or another component that grants access to data and accepts untrusted output, be careful on the input to avoid sql injections and path traversal attacks.

Page 28: Droidcon it-2014-marco-grassi-viaforensics

IPC ATTACK SURFACE EXAMPLE: SCREEN BYPASS

���28

McAfee Antivirus & Security !

Now patched It was possible to bypass the activation and use for free some functionalities. !

$ am start -a android.intent.action.MAIN -n com.wsandroid.suite/com.mcafee.main.MfeMain !

Credits: Sebastián Guerrero, @0xroot

CASE STUDY

Page 29: Droidcon it-2014-marco-grassi-viaforensics

1PASSWORD READER

• Password wallet application for Android, a companion application of the Mac/Windows client, to be able to share our passwords between our PC and the mobile device, leveraging Dropbox or the Shared Storage.

���29

CASE STUDY

Page 30: Droidcon it-2014-marco-grassi-viaforensics

BE CAREFUL WITH BROADCASTED INTENTS

���30

Vulnerable unprotected Broadcast Receiver to make the app timeout, with a Broadcasted Intent (Dangerous!)

CASE STUDY

Page 31: Droidcon it-2014-marco-grassi-viaforensics

LET’S INSTALL SOME MALWARE

���31

CASE STUDY

Page 32: Droidcon it-2014-marco-grassi-viaforensics

RESULTS

���32

The Malware catch the Broadcast Intent before of the wallet. It suppress it, so the Wallet never get the Intent and never go to timeout its session. !

What we learned: The system often is not trusted when doing IPC with Intents, and in any case we must protect the exposed parts of our application, auditing and remediating.

CASE STUDY

Page 33: Droidcon it-2014-marco-grassi-viaforensics

RAM MEMORY ATTACKS• An attacker can retrieve and

inspect the ram memory used by our application and search for sensitive informations.

• Avoid storing such sensitive informations inside instance or static variables.

���33

Page 34: Droidcon it-2014-marco-grassi-viaforensics

RAM MEMORY ATTACKS

• An easiest way to get an incomplete (VM only) chunk of live memory from our application is to use the “Dump HPROF” functionality in the monitor tool, with a debuggable application or a device with the flag ro.debuggable=1

���34

Page 35: Droidcon it-2014-marco-grassi-viaforensics

APPENDIXExtras with more advanced material

���35

Page 36: Droidcon it-2014-marco-grassi-viaforensics

RUNTIME MANIPULATION

Why modify the code of the application recompiling it when we can modify the code at runtime, without alerting the basic tampering detection?

���36

Page 37: Droidcon it-2014-marco-grassi-viaforensics

RUNTIME MANIPULATION

���37

We can change the behaviour of the applications and the system without touching any APK and we can enable/disable plugins with

ease. !

We must have a rooted phone and install a framework that will modify some low level components of the Android OS, to make

our life easier.

Page 38: Droidcon it-2014-marco-grassi-viaforensics

MOST POPULAR FRAMEWORKS

• Cydia Substrate

• Xposed Framework

���38

http://www.cydiasubstrate.com/

http://repo.xposed.info/

Page 39: Droidcon it-2014-marco-grassi-viaforensics

HOW CAN WE DEVELOP A PLUGIN AND WHAT WE CAN DO WITH IT?

���39

Page 40: Droidcon it-2014-marco-grassi-viaforensics

1PASSWORD READER

• Password wallet application for Android, a companion application of the Mac/Windows client, to be able to share our passwords between our PC and the mobile device, leveraging Dropbox or the Shared Storage.

���40

CASE STUDY

Page 41: Droidcon it-2014-marco-grassi-viaforensics

1PASSWORD: WHY SHARED STORAGE AND DROPBOX?

• This choices are forced for technical limitation in the sharing process between the PC and the device.

• Without root permissions, the user can only write in the shared folder, or the application can use third party services, such file sharing API by Dropbox, to share the wallet file.

���41

CASE STUDY

Page 42: Droidcon it-2014-marco-grassi-viaforensics

FIRST LOOK• The 1Password wallet is totally unobfuscated, so an attacker can

easily understand the logic of the application and the weak points.

• First weak spot: LOGS, the application disabled in productions the logging of the user credentials and other internal information to the Logcat, but the logs are only disabled, the code that logs at the critical points (even the user password) it’s in there.

���42

CASE STUDY

Page 43: Droidcon it-2014-marco-grassi-viaforensics

HELLO WORLD: WHAT CODE CHANGE? LET’S ENABLE LOGGING

���43

CASE STUDY

Page 44: Droidcon it-2014-marco-grassi-viaforensics

REPLACED METHODS

���44

CASE STUDYXposed Framework Plugin to re enable logging in this app

Page 45: Droidcon it-2014-marco-grassi-viaforensics

RESULTS

���45

12-03 22:49:24.614: I/Xposed(3402): logMsg - === BEGIN validate password: testing=== 12-03 22:49:24.614: I/Xposed(3402): logMsg - BEGIN decryptWithPBKDEF2 encrypted len=1056 password=testing iterations:71428 12-03 22:49:27.606: I/Xposed(3402): logMsg - derivedKeysLen=32baseKey.len=16 ivec=16 12-03 22:49:27.606: I/Xposed(3402): logMsg - END decryptWithPBKDEF2: result.len=1024 12-03 22:49:27.616: I/Xposed(3402): logMsg - SL5 key validation OK 12-03 22:49:27.616: I/Xposed(3402): logMsg - BEGIN decryptWithPBKDEF2 encrypted len=1056 password=testing iterations:71428 12-03 22:49:30.449: I/Xposed(3402): logMsg - derivedKeysLen=32baseKey.len=16 ivec=16 12-03 22:49:30.459: I/Xposed(3402): logMsg - END decryptWithPBKDEF2: result.len=1024 12-03 22:49:30.459: I/Xposed(3402): logMsg - SL3 key validation OK 12-03 22:49:30.459: I/Xposed(3402): logMsg - === END validate password

CASE STUDY

Page 46: Droidcon it-2014-marco-grassi-viaforensics

CANDY!Reverse Engineering it’s fun!

���46

Page 47: Droidcon it-2014-marco-grassi-viaforensics

LET’S USE RUNTIME MANIPULATION TO CHEAT IN ANDROID GAMES!

���47

Page 48: Droidcon it-2014-marco-grassi-viaforensics

AGIMAT

• Simple cheat engine/app for Android using runtime manipulation

• When more games are supported and if there is interest, it will be open sourced (no time)

���48

Page 49: Droidcon it-2014-marco-grassi-viaforensics

SUPER HEXAGONAddictive but difficult game for Android

���49

Page 50: Droidcon it-2014-marco-grassi-viaforensics

���50

It’s difficult? Let’s slow down the game with Reverse Engineering

and runtime manipulation!

Page 51: Droidcon it-2014-marco-grassi-viaforensics

VIDEO DEMO

���51

Page 52: Droidcon it-2014-marco-grassi-viaforensics

SECURITY IS A PROCESS.

���52

Page 53: Droidcon it-2014-marco-grassi-viaforensics

���53

https://viaforensics.com/products/vialab/

Page 54: Droidcon it-2014-marco-grassi-viaforensics

SECURE MOBILE DEVELOPMENT BEST PRACTICESAVOIDING COMMON PROBLEMS AND CREATING MORE SECURE

APPS FOR IOS AND ANDROID

���54

http://bit.ly/L1fBeT

Page 55: Droidcon it-2014-marco-grassi-viaforensics

���55

bit.ly/1doIWa7OWASP Mobile Security Project

Page 56: Droidcon it-2014-marco-grassi-viaforensics

Great book to start with Secure Android Development, written by my friend @scottyab

���56

Page 57: Droidcon it-2014-marco-grassi-viaforensics

GET CERTIFIEDbit.ly/1lwIGjl

���57

Page 58: Droidcon it-2014-marco-grassi-viaforensics

WE ARE HIRING!���58

Page 59: Droidcon it-2014-marco-grassi-viaforensics

���59

@0xroot, @abelenko, @ahoog42, Brendan , @Fuzion24, @insitusec, @giantpune, @JMDlux, @kevinswartz_1, @kstrzemp, @mattdorn, @pof,

@rozelaudric, @scottyab, Terence , @thomas_cannon, @tom_anderson2,

@viaforensics, @vialated and many others…

Page 60: Droidcon it-2014-marco-grassi-viaforensics

@marcograss

[email protected]

���60

EOF