Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan...

43
Roberto Gassirà - Roberto Piccirillo MILAN 25-26 NOVEMBER 2016

Transcript of Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan...

Page 1: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

Roberto Gassirà - Roberto Piccirillo

MILAN 25-26 NOVEMBER 2016

Page 2: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

2

● Senior Security Analysts for Mobile Security Lab○ Vulnerability Assessment (IT, Mobile Application)○ Android Secure Development

Increasing Android app security for freeWho we are

● Roberto Gassirà@robgas

[email protected]

● Roberto Piccirillo@robpicone

[email protected]

Page 3: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

Increasing Android app security for freePotentially Hostile Environment

Page 4: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

4

Mobile Application can run in a Potentially Hostile Environment

Potentially Hostile EnvironmentIntroduction

Page 5: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

5

Free Open Wifi ...

Potentially Hostile EnvironmentUnreliable Communication Channels

… Free user data

Threat:Traffic Snooping

Page 6: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

6

Potentially Hostile EnvironmentUnreliable Communication Channels

Free WPA2 Wifi ...

… Free user data (MITM)

Threat: MITM

Page 7: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

7

Potentially Hostile EnvironmentUnreliable Communication Channels

Under attack...

Threat: Information Gathering

Page 8: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

8

Rooting

Potentially Hostile EnvironmentTampered Device

BootLoader Unlock Local/remote Exploit

Page 9: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

9

Rooting -> Android platform security compromised

Potentially Hostile EnvironmentTampered Device

No more application

sandbox

Page 10: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

10

Potentially Hostile EnvironmentTampered Device

Hooking/Instrumentation

Threat:Code Hijacking

onCreate()

isDeviceTampered()

...()EXIT

falsetrue

Hooking...

isDeviceTampered()

false

Page 11: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

11

Mobile Threats for Developers

● Advanced Device Owner○ Remove Bloatware/Customization

Attacker

● Mobile Cybercriminal○ Application analysis

● Potentially Harmful Applications○ Steal info/money

Page 12: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

12

Mobile Threats for DevelopersMalware Infection

Apps from “Unknown sources”

Apps from “Unknown sites”

Page 14: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

14

Mobile Threats for Developers

Tampered Device Detection

Free Weapons for Developers

SafetyNet API

● Allows an app to analyze the device where it is installed

● Check if the device has passed the Compatibility Test Suite (CTS)

Check the integrity of the device

(Rooted?Hooked?Infected?)

● Provided by Google Play Services

Page 15: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

15

Mobile Threats for Developers

Key Material Protection

Free Weapons for Developers

AndroidKeyStore

● Asymmetric and Symmetric Keys (API 23+) Secure Container with Hardware Backend

Secure CommunicationNetwork Security

Configuration

● Network security settings (certificate pinning, trusted CA, ...) customized with a safe and declarative configuration file

Page 16: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

Increasing Android app security for freeDetecting Tampered Device

Page 17: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

17

Detecting Tampered Device

https://developer.android.com/training/safetynet/index.html

Checking Device Compatibility

Page 18: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

18

Detecting Tampered Device

https://developers.google.com/android/guides/api-client

Access Google API

SafetyNet service

build.gradle

Create an instance of Google API Client

Page 19: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

19

Detecting Tampered DeviceSend Compatibility Check Request

Generate a random one time nonce to defeat

replay attacks

Send the request

AttestationResult

Page 20: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

20

● Formatted in JSON Web Signature format○ RSA256 Signed JSON

Detecting Tampered DeviceAttestation Result

JWS Signature

JWS Payload

JWS Header

Device passed Compatibility Test Suite

Device integrity statustrue: OK

false: TAMPERED

Page 21: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

21

Detecting Tampered Device

● Google provides Android Device Verification API for validating the response

Validate Compatibility Check Response

POST "https://www.googleapis.com/androidcheck/v1/attestations/verify?key="

{ "signedAttestation": }JWS

Signature

JWS Payload

JWS Header

{ “isValidSignature”: true }

Page 22: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

Increasing Android app security for freeEnhancing Network Security

Page 23: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

23

● MITM attack:○ Is a well-known technique used by an attacker to setup a proxy to intercept traffic

between your application and backend servers

● How○ ARP poisoning○ DNS poisoning○ Rouge proxy○ etc

Enhancing Network SecurityMITM attack

Page 24: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

24

● HTTP and HTTPS:○ HTTP: all data sent are in clear○ HTTPS: all data sent are ciphered (Digital Certificates and Session Keys)

● Implement MITM attack on HTTP (easier)

● Implement MITM attack on HTTPS (harder)○ Not impossible

Enhancing Network SecurityMITM with HTTP or HTTPS

Page 25: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

25

Enhancing Network SecurityHow SSL works

Page 26: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

26

Digital certificateNetwork Security Configuration

● Most important:○ Common name

○ Issuer name

○ Not Valid Before

○ Not Valid After

○ Public Key

○ Signature

Remember “Public Key Info” section

Page 27: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

27

● Use HTTPS is not enough to mitigate some risks due to MITM Attacks○ But in almost all cases should be mandatory use it

● To be more secure it’s important:○ Check the common name of server digital certificate○ Verify the issuer of server digital certificate○ Trust the issuer of server digital certificate

● In the last years is usual:○ Check the server public key (Pinning certificate or sometime called SSL Pinning)○ More code to implement this technique

Enhancing Network SecurityHTTPS key security points

Android Nougat offers new features to perform easily checks to make HTTPS more secure

Page 28: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

28

● Uses declarative configuration file to:○ Enforce HTTPS for specified domain used into your application○ Use certificate pinning ○ Trust only specific Certification Authority or use specific Self-signed certificate○ Debug secure connections without modify code

● What you need:

Enhancing Network SecurityNetwork Security Configuration

AndroidManifest.xml

Page 29: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

29

Enhancing Network SecurityConfiguration file format

Contains all Network Configuration

Default configuration for all connections

Configurations for one or more domains

Configurations valid only for debug purpose

Page 30: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

30

● Get error when try to connect using HTTP

Enhancing Network SecurityEnforce HTTPS

Enforce HTTPS

HTTP Connection

Error:“Cleartext HTTP traffic to

android-developers.blogspot.it not permitted”

Page 31: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

31

● Use yours CA to verify yours certificate

Enhancing Network SecurityDigital Certificate with custom CA

Enforce HTTPS for the domaincodemotion.milan.2016

Use cacert certificate to verify server certificate

● If cacert is not used the app get an error

Page 32: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

32

● Force your application to use a specific public key● In previous Android version you had to write boring code to implement

certificate pinning● Now you need calculate the sha256 of Public Key Info of X509 digital

certificate

Enhancing Network SecurityCertificate pinning

sha256 base64

PinDigest

Page 33: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

33

● If server public key is different the application get an error

Enhancing Network SecurityCertificate pinning

● Add PinDigest with Expiration date

Page 34: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

34

● In our analysis is horrible to find out the all SSL checks are off to overcame problem into development environment

● Now it is possible to add debug configuration without modify any line of code

● When you build in “release-mode” debug configuration is not considered

Enhancing Network SecuritySafe debug

Page 35: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

35

● You could define a base configuration for all connections

● You could insert more PinDigest

● You could define which CA store will be used to verify certificates:○ User○ System

● You could use self signed-certificate

Enhancing Network SecurityOther options

Page 36: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

Increasing Android app security for freeKey Management Evolution

Page 37: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

37

Key Management Evolution

● Android KeyStore Provider introduced with API level 18○ Based on Android Keystore System to store cryptographic keys

● Until API level 22 only asymmetric keys○ For info: https://speakerdeck.com/mseclab/android-key-management

● With API level 23+ also symmetric Keys

AndroidKeyStore Provider

Asymmetric

Asymmetric + Symmetric

Page 38: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

38

Key Management EvolutionGenerating Symmetric Key

Page 39: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

39

Key Management EvolutionFingerprint Authentication

Page 40: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

40

Key Management EvolutionAndroidKeyStore Security Features

● Preventing extraction of the key material from application process

● Preventing extraction of the key material from Android device

● Key material never enters the application process:○ App cryptographic operations are performed by system process ○

● Key materials may be bound to the secure hardware:○ Trust Execution Environment (TEE)○ Secure Element

● More and more processors are equipped with TEE:○ Snapdragon 808 (Nexus 5x), Snapdragon 810 (Nexus 6P), Snapdragon 820 (Galaxy S7)

etc

Page 41: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

Increasing Android app security for freeThe Bill

Page 42: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

42

The Bill

● Detecting Tampered Device: Free

● Enhancing Network Security: Free

● Key Management Evolution: Free

Total = Free :)

How much costs

Page 43: Increasing Android app security for free - Roberto Gassirà, Roberto Piccirillo - Codemotion Milan 2016

Web: www.mseclab.com www.consulkthink.itMail: [email protected]:+39-06-4549 2416Fax:+39-06-4549 2454

Grazie per l’attenzione