Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android security (for developers) at jDays 2015

Android security (for developers) at jDays 2015

Slides from the 'Android Security For Developers' session at jDays 2015 in Göteborg, Sweden

AppFoundry

March 17, 2015
Tweet

More Decks by AppFoundry

Other Decks in Programming

Transcript

  1. Android
    security
    FOR DEVELOPERS
    FILIP MAELBRANCKE

    View Slide

  2. Your host
    Filip Maelbrancke
    Consultant @ AppFoundry
    [email protected]
    @fmaelbrancke

    View Slide

  3. View Slide

  4. X

    View Slide

  5. Security = managing risk
    ASSET VULNERABILITY
    THREAT

    View Slide

  6. Security = managing risk
    ASSET VULNERABILITY
    THREAT

    View Slide

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

    View Slide

  8. Always out
    Vulnerability / Exploitability
    • Stolen
    • Forgotten
    • Lost

    View Slide

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

    View Slide

  10. Android security MODEL
    Game X
    Game Y
    System
    Contacts Email Google Play
    Verify app signature
    App sandbox
    Permissions
    application isolation

    View Slide

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

    View Slide

  12. Security
    APP HARDENING DATA NETWORK SERVICES

    View Slide

  13. View Slide

  14. Securing the app
    JAVA
    CLASS
    DEX

    View Slide

  15. reverse engineer

    View Slide

  16. obtain apk
    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

    View Slide

  17. apk structure
    apk = zip
    APK
    AndroidManifest
    classes.dex
    Resources

    View Slide

  18. reverse engineer
    TOOLS
    • Apktool
    • Dex2jar
    • JADX

    View Slide

  19. Reverse engineer
    smali / baksmali
    apktool low level
    disassembled Dex bytecode
    code
    code can be modified
    recompile / resign

    View Slide

  20. Reverse engineer
    apktool d myapp.apk

    View Slide

  21. Reverse engineer

    View Slide

  22. Reverse engineer

    View Slide

  23. Reverse engineer
    convert .dex file to a .jar with java
    bytecode
    DEX2JAR dex -> java
    java decompiler
    code
    very readable

    View Slide

  24. Reverse engineer

    View Slide

  25. Reverse engineer
    command-line / GUI tools
    JADX

    View Slide

  26. Reverse engineer
    Jeb Decompiler
    PAID dex -> java
    native dex decompiler

    View Slide

  27. reverse engineer

    View Slide

  28. Obfuscation

    View Slide

  29. Proguard
    obfuscate
    optimize
    Shrink

    View Slide

  30. proguard
    obfuscation

    View Slide

  31. proguard

    View Slide

  32. proguard
    configuration

    View Slide

  33. 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

    View Slide

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

    View Slide

  35. dexguard

    View Slide

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

    View Slide

  37. TAMPER DETECTION

    View Slide

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

    View Slide

  39. INSTALLER
    PLAY STORE INSTALLER

    View Slide

  40. debugger
    Debugger check

    View Slide

  41. debugger
    Debugger check

    View Slide

  42. emulator
    EMULATOR check

    View Slide

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

    View Slide

  44. SIGNING KEY
    Valid signing key

    View Slide

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

    View Slide

  46. View Slide

  47. Data protection
    laws that govern data protection
    Law obligation
    beyond legal obligations ➪ moral
    obligation

    View Slide

  48. 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

    View Slide

  49. 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

    View Slide

  50. keylogger

    View Slide

  51. Android not enough?
    rooted devices
    internal storage full disk crypto
    brute forcing

    View Slide

  52. CRYPTO

    View Slide

  53. crypto in Android = JCA
    APP
    JCA (Java Cryptography Architecture)
    Provider Provider
    Message Digest
    Key Generation
    Digital Signature
    ...

    View Slide

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

    View Slide

  55. 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

    View Slide

  56. GPS dynamic Security provider
    Since Google Play Services 5
    google play services replacement
    for the platform’s bundled provider
    security patches
    rapid delivery
    frequently updated
    by Google

    View Slide

  57. dynamic security provider
    setup
    dependencies {
    compile 'com.google.android.gms:play-services:6.1.+'
    }

    View Slide

  58. dynamic security provider
    setup

    View Slide

  59. Android security providers

    View Slide

  60. Conscrypt

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  64. 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

    View Slide

  65. 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

    View Slide

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

    View Slide

  67. KEYCHain?
    Keystore provider
    • Since Android 4.3
    • Can be hardware-backed

    View Slide

  68. Android keystore
    available from Android 4.3
    java security provider APP
    can generate & save private keys
    keys
    private to each app

    View Slide

  69. keystore

    View Slide

  70. keystore

    View Slide

  71. network

    View Slide

  72. Secure communication channel
    use https
    Use SSL / TLS
    • Confidentiality
    • Authentication
    VALIDATION
    Hostname verification
    Certificate pinning
    http://android-ssl.org/

    View Slide

  73. secure communication channel
    hostname verification

    View Slide

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

    View Slide

  75. certificate authorities

    View Slide

  76. certificate authorities
    https://bluebox.com/blog/technical/questioning-the-chain-of-trust-investigations-into-
    the-root-certificates-on-mobile-devices

    View Slide

  77. Trustmanager
    StrongTrustManager
    • Validate whole certificate chain
    • Debian certificate store

    View Slide

  78. self signed cert

    View Slide

  79. anti-pattern
    don’t trust all!

    View Slide

  80. Self-signed cert
    import in your app
    certificate custom trustmanager
    no man-in-the-middle attacks

    View Slide

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

    View Slide

  82. 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 trustmanager
    Based on keystore
    Load into keystore
    SSL context
    Init SSL context with TrustManager
    https://developer.android.com/training/articles/security-ssl.html

    View Slide

  83. NOGOTOFAIL
    Test tool man in the middle server
    attempts to inject
    attacks into connections
    checks
    https://github.com/google/nogotofail

    View Slide

  84. View Slide

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

    View Slide

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

    View Slide

  87. View Slide

  88. 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 scale of misuse indicates a widespread misunderstanding of
    how to properly use cryptography in Android development.


    View Slide

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

    View Slide

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

    View Slide

  91. 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)

    View Slide

  92. 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/…


    View Slide

  93. Questions?
    Filip Maelbrancke
    Consultant @ AppFoundry
    [email protected]
    @fmaelbrancke

    View Slide