Slide 1

Slide 1 text

Application security ANDROID FOR DEVELOPERS FILIP MAELBRANCKE

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

X

Slide 4

Slide 4 text

Security = managing risk ASSET VULNERABILITY THREAT

Slide 5

Slide 5 text

Security = managing risk ASSET VULNERABILITY THREAT

Slide 6

Slide 6 text

All in one device Increases threat proBability ! • GPS • Contacts • Camera • Email (work) • Wallet

Slide 7

Slide 7 text

Always out Vulnerability / Exploitability ! • Stolen • Forgotten • Lost !

Slide 8

Slide 8 text

Everyone uses it Vulnerability / Exploitability ! • Weak pins • Use of open public WiFi !

Slide 9

Slide 9 text

Everyone uses it

Slide 10

Slide 10 text

Android security

Slide 11

Slide 11 text

Android security model ! ! ! ! ! ! ! ! ! ! Game X ! ! ! Game Y ! System ! ! ! ! ! ! ! ! ! ! ! ! ! ! Contacts ! ! ! Email ! ! ! Google Play ! Verify app signature ! ! App sandbox ! ! Permissions application isolation

Slide 12

Slide 12 text

typical mobile app MOBILE APPLICATION UI LOCAL STORAGE REMOTING LAYER REMOTE API COMMUNICATION CHANNEL

Slide 13

Slide 13 text

Security APP DATA NETWORK SERVICES

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Securing the app JAVA CLASS DEX

Slide 16

Slide 16 text

reverse engineer

Slide 17

Slide 17 text

OBTAIN APK FROM DEVICE adb backup -apk be.myapp ADB BACKUP app Titanium, Astro, Helium adb shell pm list packages -f adb pull /data/app/be.myapp-1.apk

Slide 18

Slide 18 text

APK structure APK = zip APK AndroidManifest classes.dex Resources

Slide 19

Slide 19 text

reverse engineer TOOLS ! • Apktool • Dex2jar • Apk to Java !

Slide 20

Slide 20 text

reverse engineer smali / baksmali APKTOOL Low level disassembled Dalvik bytecode CODE code can be modified recompile / resign

Slide 21

Slide 21 text

reverse engineer apktool d myapp.apk

Slide 22

Slide 22 text

reverse engineer

Slide 23

Slide 23 text

reverse engineer code

Slide 24

Slide 24 text

reverse engineer convert .dex file to a .jar with java bytecode DEX2JAR dex -> java java decompiler CODE very readable

Slide 25

Slide 25 text

reverse engineer

Slide 26

Slide 26 text

reverse engineer Jeb Decompiler PAID dex -> java native dalvik decompiler

Slide 27

Slide 27 text

reverse engineer

Slide 28

Slide 28 text

Obfuscation

Slide 29

Slide 29 text

Proguard obfuscate optimize Shrink

Slide 30

Slide 30 text

proguard obfuscation

Slide 31

Slide 31 text

proguard

Slide 32

Slide 32 text

proguard configuration

Slide 33

Slide 33 text

proguard Beware!

Slide 34

Slide 34 text

proguard loggingwrapper

Slide 35

Slide 35 text

proguard configuration

Slide 36

Slide 36 text

proguard BEtter

Slide 37

Slide 37 text

other techniques If possible, run code at server! server String encryption Hide sensitive strings eg “Secure” Native code Java Native Interface reflection Proxy Introduces indirection Class encryption Use DexGuard

Slide 38

Slide 38 text

dexguard Same config proguard++ Commercial Good value for the money Tamper checks

Slide 39

Slide 39 text

dexguard

Slide 40

Slide 40 text

proguard tips Test! release build Mapping.txt Save! Crash? Supported on Crashlytics, Crittercism, ...

Slide 41

Slide 41 text

TAMPER DETECTION

Slide 42

Slide 42 text

Environment 1.installer 2.debugger / 3.BINARY Validation Tamper detection / protection

Slide 43

Slide 43 text

INSTALLER PLAY STORE INSTALLER

Slide 44

Slide 44 text

debugger Debugger check

Slide 45

Slide 45 text

debugger Debugger check

Slide 46

Slide 46 text

emulator EMULATOR check

Slide 47

Slide 47 text

SIGNING KEY Valid signing key ! • SHA1 of signing cert • Embed • Check with runtime signature !

Slide 48

Slide 48 text

SIGNING KEY Valid signing key

Slide 49

Slide 49 text

rooted device root detection ! • Check typical apps / files • Check keys • /system r/w !

Slide 50

Slide 50 text

tamper detection tips + use obfuscation! multiple checks Tampering detected Close application Don’t leak where the protection code is

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

local Data protection Avoid it if you can Avoid External storage Avoid external storage for sensitive information For critical info set android:saveEnabled="false" Backup set android:allowBackup=false proper permissions MODE_PRIVATE with files

Slide 53

Slide 53 text

local Data protection getWindow().setFlags(LayoutParams.FLAG _SECURE, LayoutParams.FLAG_SECURE); avoid screen shots LOGOUT on inactivity if usability allows and clear the cached information

Slide 54

Slide 54 text

keylogger

Slide 55

Slide 55 text

ANDROID NOT ENOUGH? rooted devices Internal Storage Full disk crypto brute forcing

Slide 56

Slide 56 text

encryption

Slide 57

Slide 57 text

JCA APP JCA (Java Cryptography Architecture) Provider Provider Message Digest Key Generation Digital Signature ...

Slide 58

Slide 58 text

JCA Bouncy Castle Android OpenSSL APP JCA (Java Cryptography Architecture) Harmony

Slide 59

Slide 59 text

bouncy castle Android = subset of upstream release cut-down CONSISTENT Consistent crypto across Android versions MINIMAL change github.com/rtyley/spongycastle Spongy castle Repackage of Bouncy Castle for Android

Slide 60

Slide 60 text

encryption libs SQLCipher sqlcipher.net ! • Modified version of SQLite • AES-256 encryption • Drop-in replacement ! iocipher guardianproject.info/code/ iocipher ! Virtual encrypted disk

Slide 61

Slide 61 text

key management Store along with the data (file private to the app) Store Embed Embed in source code (obfuscated ?) EASY TO EXTRACT

Slide 62

Slide 62 text

key management don’t store Don’t store the key on the device Have it entered each time necessary Store In systems service SOLUTIONS

Slide 63

Slide 63 text

key derivation Long random strings of bits encryption keys people vs keys Users are familiar with passwords Crypto algo PBKDF2WithHmacSHA1 password based encryption Generate strong crypto keys based on humanly-manageable passwords

Slide 64

Slide 64 text

proper key derivation Using a salt protects from table- assisted / pre-computed dictionary attacks SALT key stretching Repeat the key derivation operation multiple times to produce the final key Slows down brute force attacks

Slide 65

Slide 65 text

key derivation https://github.com/nelenkov/android-pbe http://nelenkov.blogspot.jp/2012/04/using-password-based-encryption-on.html Nikolay Elenkov

Slide 66

Slide 66 text

KEYCHain? Keystore provider ! • Since Android 4.3 • Can be hardware- backed https://github.com/nelenkov/android-keystore Nikolay Elenkov

Slide 67

Slide 67 text

network

Slide 68

Slide 68 text

Secure communication channel use https Use SSL / TLS ! • Confidentiality • Authentication ! VALIDATION Hostname verification ! Certificate pinning

Slide 69

Slide 69 text

secure communication channel hostname verification

Slide 70

Slide 70 text

SSL certificates CA issued, Android recognized CA issued self-signed certificates behaviour change custom TrustManager

Slide 71

Slide 71 text

self-signed cert

Slide 72

Slide 72 text

anti pattern don’t trust all!

Slide 73

Slide 73 text

self-signed cert Certificate Custom trustmanager NO man-in-the-middle attacks import in your app

Slide 74

Slide 74 text

Certificate authorities

Slide 75

Slide 75 text

Trustmanager StrongTrustManager ! • Validate whole certificate chain • Debian certificate store !

Slide 76

Slide 76 text

certificate pinning with expected certificate / public key Associate host hashing anonymize certificate / public key

Slide 77

Slide 77 text

certificate pinning echo | openssl s_client -connect host:443 2>&1 | sed -ne '/-BEGIN CERTIFICATE-/,/- END CERTIFICATE-/p' > mycertificate.pem get certificate (openssl) embed in application /res/raw Custom Based on keystore Load into keystore SSL context Init SSL context with TrustManager https://developer.android.com/training/articles/security-ssl.html

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

Securing services Controls ! • Kill switch for specific functionality • Server downtime communication • Mandatory update mechanism !

Slide 80

Slide 80 text

securing services Backend REST and APIs can have similar vulnerabilities to web applications mitigate follow OWASP top 10

Slide 81

Slide 81 text

Effective security Using CryptoLint, we performed a study on cryptographic implementations in 11,748 Android applications. Overall we find that 10,327 programs – 88% in total – use cryptography inappropriately. The raw s c a l e o f m i s u s e i n d i c a t e s a w i d e s p r e a d misunderstanding of how to properly use cryptography in Android development. “ ”

Slide 82

Slide 82 text

effective security hardcoded passphrases manually seeded SecureRandom insufficient key generation iterations hardcoded salts non-random initialization vectors

Slide 83

Slide 83 text

security testing Static analysis Manual code review design review Analysis Static Dynamic Penetration testing

Slide 84

Slide 84 text

suggested reading Android Security Cookbook
 Keith Makan / Scott Alexander-Bown (9781782167167) Android Security Internals 
 Nikolay Elenkov (9781593275815) Android Hacker’s Handbook
 Joshua J. Drake et al. (9781118608647) Application Security for the Android platform
 Jeff Six (9781449315078) 


Slide 85

Slide 85 text

suggested reading developer.android.com
 https://developer.android.com/training/articles/security-tips.html 
 https://source.android.com/devices/tech/security/ OWASP
 https://www.owasp.org/index.php/OWASP_Mobile_Security_Project Google+ community 
 Android security discussions Blogs
 http://nelenkov.blogspot.com.tr/…


Slide 86

Slide 86 text

Filip maelbrancke TWITTER: @fmaelbrancke EMAIL: filip@maelbrancke.net THANK YOU EMAIL: info@appfoundry.be consultant @ AppFoundry