Slide 1

Slide 1 text

Hardening your Android App Scott Alexander-Bown Head of Android Mubaloo @scottyab

Slide 2

Slide 2 text

me: Scott Alexander-Bown •  Head of Android at Mubaloo •  Passionate about Android and mobile security •  Co-run SWmobile meetup group •  Follow me @scottyab Favourite apps

Slide 3

Slide 3 text

It's not about... Fear!

Slide 4

Slide 4 text

It's not about... 100%

Slide 5

Slide 5 text

Agenda •  Why? o  Attacker motivations o  Reverse engineering •  Hardening techniques o  Android Permissions o  Encryption and key management on Android o  Using SSL better o  Make it harder to pirate/repackage your app o  Device Administration policies o  Miscellaneous tips

Slide 6

Slide 6 text

•  Different ads •  Different market •  Extract assets or API keys •  Insert malware •  Software piracy •  Malware and security research •  Fun! Motivations for hacking an app

Slide 7

Slide 7 text

Reverse engineering Android app

Slide 8

Slide 8 text

Apktool •  http://bit.ly/apktool •  Apktool o  apktool d myapp.apk o  apktool b myapp newmyapp.apk

Slide 9

Slide 9 text

Santoku Linux •  Linux ISO •  Pre-installed platform SDKs, drivers, and utilities •  Decompilation and disassembly tools •  Scripts to detect common issues in mobile applications •  Scripts to automate decrypting binaries, deploying apps, enumerating app details, and more •  https://santoku-linux.com/

Slide 10

Slide 10 text

*Disclaimer: I work for Via Forensics as of Monday

Slide 11

Slide 11 text

Techniques for hardening

Slide 12

Slide 12 text

Protecting internal storage Creating world-readable files is very dangerous •  Do o  File creation mode: Context.MODE_PRIVATE •  Don't use o  MODE_WORLD_READABLE o  MODE_WORLD_WRITEABLE o  (deprecated in API level 17)

Slide 13

Slide 13 text

Permissions •  Are all the permissions required? Instead of •  Permission types (protection level) o  Normal o  Dangerous o  Signature

Slide 14

Slide 14 text

Custom Permission example • Declared custom permission • Another app/component using the permission

Slide 15

Slide 15 text

Don't leak permissions •  Protect entry points (receivers, services, content providers) •  Exported=false •  Context.checkCallingPermission("android.permission. CAMERA") •  Context.enforceCallingPerrmissions(...) •  Tip: Local broadcast manager for in app notifications

Slide 16

Slide 16 text

Encryption: 3rd party libs •  SQL Cipher o  256-bit AES Encrypt SQLite database o  http://sqlcipher.net/sqlcipher-for-android •  Keyczar - Open source cryptographic toolkit o  http://www.keyczar.org o  https://github.com/kruton/android-keyczar-demo •  IO Chiper - virtual encrypted disk o  Clone of java.io o  https://guardianproject.info/code/iocipher

Slide 17

Slide 17 text

Encryption: Key management Two 'ideal world' solutions •  Don't store the key on the device •  Use a system service (such as keychain) •  Tip: Minimise keys time in ram (null after using them)

Slide 18

Slide 18 text

Encryption: Generate random key •  Note: New Implementation of SecureRandom in Android 4.2

Slide 19

Slide 19 text

• Use a key derivation algorithm: PBK2F2 • (secure)random salt and iteration count • Tip: Ensure derivation method takes more than 100ms • Code for what to do and what not to do: https://github.com/ nelenkov/android-pbe Password based encryption (PBE)

Slide 20

Slide 20 text

Password based encryption (PBE)

Slide 21

Slide 21 text

Encryption: no no's •  Store encryption keys in app •  Log/debug statements with encryption keys •  Rely on OS encryption •  Write your own encryption algorithms

Slide 22

Slide 22 text

SSL •  Use Https by default •  What about Man in the middle (MITM) attacks? o  Trusting all certificates o  Compromised CA

Slide 23

Slide 23 text

SSL Tips •  Pay attention to security exceptions •  Verify the certificate issuing hostname •  SSL Pinning (public key pinning) o Android pinning - https://github.com/moxie0/ AndroidPinning o Android 4.2 - X509TrustManagerExtensions

Slide 24

Slide 24 text

SSL: wipe the slate clean •  Don't use a CA! •  Server side o  create your own 4096bit signing certificate (keep offline) o  sign your certs for the web services •  Client/app o  include the signing cert (in a keystore) o  validate against it

Slide 25

Slide 25 text

Make your app harder to pirate •  Google License Verification Library o  Modify LVL source as much as possible o  com.android.vending.licensing.* o  Focus on core of the LVL logic: LicenseChecker and LicenseValidator. •  Offload license validation to a trusted server

Slide 26

Slide 26 text

Tamper resistance •  Checksum of the app code with validation check on server or unlocker app •  Reflection based tamper checks •  Check installer is from play store

Slide 27

Slide 27 text

Obfuscation: Proguard •  Proguard been around for 10+ years •  Project properties file, uncomment #proguard.config= •  Only applied when building release versions •  Entry points should be excluded -keep public class * extends android.app.Activity •  Most popular 3rd party libs/jars come with proguard config •  Bonus: ~50% reduction in .apk size

Slide 28

Slide 28 text

•  Optimize and obfuscate tuned for the Android platform/ Dalvik bytecode. •  Encrypt strings •  Encrypt entire classes •  Hide access to sensitive APIs •  Add tamper detection •  Thoroughly remove Android logging code •  More info: http://www.saikoa.com/ Obfuscation: Dexguard

Slide 29

Slide 29 text

Remove logging using Proguard

Slide 30

Slide 30 text

Device Management Policies •  Since Android 2.2 •  Enforce o  Device lock o  Passcode type (pin, pattern) o  Password complexity o  Device encryption (3.0+) o  Device wipe •  New policies are added in each release •  Policies set by different apps can only change policies to make them stronger •  Cannot uninstall an app while the device admin is still active

Slide 31

Slide 31 text

Device Management Policies •  Define a policy as an xml resource •  Reference in manifest •  Create a Device Administration broadcast receiver •  Implement a Device Policy Controller o  DevicePolicyManager.isAdminActive?

Slide 32

Slide 32 text

Misc tips Validation •  User input - SQL Injection Anti tamper •  Detect rooted device •  Detect emulator •  isDebuggable? Web views •  Disable Javascript •  Use https •  Validate URLS •  Restrict JavaScript interface

Slide 33

Slide 33 text

Misc tips Avoid unsecured components •  Don't use SMS for sensitive data •  Don't use SD card •  Avoid sensitive data in public intents •  Avoid sensitive data in sticky broadcasts •  allowBackups=“false” GUID (Privacy concern) •  Generate a large unique number •  Don't use phone number or IMEI

Slide 34

Slide 34 text

Misc tips: development practices Infrastructure •  Code •  Keystore and password •  App store user credentials o  Enable 2 step authentication o  Grant access rather than share account details Process •  Educate developers o  Don't ignore the lint warnings •  Audit / security code review

Slide 35

Slide 35 text

Summary •  Go hack your own apps •  Using https isn't enough pin your certs •  Encrypt app data •  Proguard your apps •  Android is getting more secure

Slide 36

Slide 36 text

Q&A @scottyab [email protected]

Slide 37

Slide 37 text

Bonus Slides Security features of Jelly Bean (Android 4.1 & 4.2)

Slide 38

Slide 38 text

Security enhancements in Jelly Bean *Data collected during a 14-day period ending on March 4, 2013

Slide 39

Slide 39 text

Security enhancements in Jelly Bean •  New implementation of SecureRandom •  Javascript Interface methods in WebViews must now be annotated (@JavascriptInterface) •  Application verification

Slide 40

Slide 40 text

Security enhancements in Jelly Bean •  Content Provider default access has changed •  Remote blacklisting CAs •  Secure USB debugging •  Hidden developer options

Slide 41

Slide 41 text

Ref/More info... Using Cryptography to Store Credentials Safely http://android-developers.blogspot.co.uk/2013/02/using-cryptography-to-store-credentials.html Security Enhancements in Jelly Bean http://android-developers.blogspot.co.uk/2013/02/security-enhancements-in-jelly-bean.html Security Tips https://developer.android.com/training/articles/security-tips.html 42 tips on app security https://viaforensics.com/resources/reports/best-practices-ios-android-secure-mobile- development/ Why Eve and Mallory Love Android: An Analysis of Android SSL (In)Security http://www2.dcsec.uni-hannover.de/files/android/p50-fahl.pdf @scottyab [email protected]