Slide 1

Slide 1 text

Comprehensive guide to tracking protection on Android David González - Software Engineer DuckDuckGo Øredev - 8 Nov 2022

Slide 2

Slide 2 text

Agenda 1. What is a tracker? 2. Web vs App trackers 3. Why should I care? 4. How to block them

Slide 3

Slide 3 text

Did you know that 90% of Websites and apps are tracking you?

Slide 4

Slide 4 text

What is a tracker?

Slide 5

Slide 5 text

Piece of software whose task is to gather information on the person using the application or website

Slide 6

Slide 6 text

Types of trackers 1. Crash reporters 2. Analytics

Slide 7

Slide 7 text

Types of trackers 1. Crash reporters 2. Analytics 3. Profiling

Slide 8

Slide 8 text

Types of trackers 1. Crash reporters 2. Analytics 3. Profiling 4. Identification

Slide 9

Slide 9 text

Types of trackers 1. Crash reporters 2. Analytics 3. Profiling 4. Identification 5. Ads

Slide 10

Slide 10 text

Types of trackers 1. Crash reporters 2. Analytics 3. Profiling 4. Identification 5. Ads 6. Location

Slide 11

Slide 11 text

Web trackers

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

App trackers

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Why should I care?

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

(https://www.washingtonpost.com/technology/2022/09/22/health-apps-privacy/)

Slide 19

Slide 19 text

Knowing what to block

Slide 20

Slide 20 text

T R A C K E R R A D A R • DuckDuckGo Tracker Radar is a data set about trackers that is automatically generated and maintained through continuous crawling and analysis. • This data set is publicly available in (https:// github.com/duckduckgo/tracker-radar) to use for research and for generating tracker block lists. And, the code behind it is open source

Slide 21

Slide 21 text

Generating a blocklist for Android 21 • Top 300 apps from https://androidrank.org/ • Install root certi fi cate • Start using apps • Record tra ff i c • Looks for inputs

Slide 22

Slide 22 text

Blocking web trackers

Slide 23

Slide 23 text

Block web trackers class BrowserWebViewClient() : WebViewClient() { override fun shouldInterceptRequest( webView: WebView, request: WebResourceRequest ) : WebResourceResponse? { private fun blockRequest( trackingEvent: TrackingEvent, request: WebResourceRequest, webViewClientListener: WebViewClientListener? ) : WebResourceResponse { trackingEvent.surrogateId ?. let { surrogateId -> val surrogate = resourceSurrogates.get(surrogateId) if (surrogate.responseAvailable) { Timber.d("Surrogate found for ${request.url}") webViewClientListener ?. surrogateDetected(surrogate) return WebResourceResponse(surrogate.mimeType, "UTF-8", surrogate.jsFunction.byteInputStream()) } } Timber.d("Blocking request ${request.url}") privacyProtectionCountDao.incrementBlockedTrackerCount() return WebResourceResponse(null, null, null) }

Slide 24

Slide 24 text

Block web trackers class BrowserWebViewClient() : WebViewClient() { override fun shouldInterceptRequest( webView: WebView, request: WebResourceRequest ) : WebResourceResponse? { private fun blockRequest( trackingEvent: TrackingEvent, request: WebResourceRequest, webViewClientListener: WebViewClientListener? ) : WebResourceResponse { trackingEvent.surrogateId ?. let { surrogateId -> val surrogate = resourceSurrogates.get(surrogateId) if (surrogate.responseAvailable) { Timber.d("Surrogate found for ${request.url}") webViewClientListener ?. surrogateDetected(surrogate) return WebResourceResponse(surrogate.mimeType, "UTF-8", surrogate.jsFunction.byteInputStream()) } } Timber.d("Blocking request ${request.url}") privacyProtectionCountDao.incrementBlockedTrackerCount() return WebResourceResponse(null, null, null) }

Slide 25

Slide 25 text

Block web trackers class BrowserWebViewClient() : WebViewClient() { private fun blockRequest( trackingEvent: TrackingEvent, request: WebResourceRequest, webViewClientListener: WebViewClientListener? ) : WebResourceResponse { trackingEvent.surrogateId ?. let { surrogateId -> val surrogate = resourceSurrogates.get(surrogateId) if (surrogate.responseAvailable) { Timber.d("Surrogate found for ${request.url}") webViewClientListener ?. surrogateDetected(surrogate) return WebResourceResponse(surrogate.mimeType, "UTF-8", surrogate.jsFunction.byteInputStream()) } } Timber.d("Blocking request ${request.url}") privacyProtectionCountDao.incrementBlockedTrackerCount() return WebResourceResponse(null, null, null) } }

Slide 26

Slide 26 text

Block web trackers class BrowserWebViewClient() : WebViewClient() { private fun blockRequest( trackingEvent: TrackingEvent, request: WebResourceRequest, webViewClientListener: WebViewClientListener? ) : WebResourceResponse { trackingEvent.surrogateId ?. let { surrogateId -> val surrogate = resourceSurrogates.get(surrogateId) if (surrogate.responseAvailable) { Timber.d("Surrogate found for ${request.url}") webViewClientListener ?. surrogateDetected(surrogate) return WebResourceResponse(surrogate.mimeType, "UTF-8", surrogate.jsFunction.byteInputStream()) } } Timber.d("Blocking request ${request.url}") privacyProtectionCountDao.incrementBlockedTrackerCount() return WebResourceResponse(null, null, null) } }

Slide 27

Slide 27 text

Block web trackers class BrowserWebViewClient() : WebViewClient() { private fun blockRequest( trackingEvent: TrackingEvent, request: WebResourceRequest, webViewClientListener: WebViewClientListener? ) : WebResourceResponse { trackingEvent.surrogateId ?. let { surrogateId -> val surrogate = resourceSurrogates.get(surrogateId) if (surrogate.responseAvailable) { Timber.d("Surrogate found for ${request.url}") webViewClientListener ?. surrogateDetected(surrogate) return WebResourceResponse(surrogate.mimeType, "UTF-8", surrogate.jsFunction.byteInputStream()) } } Timber.d("Blocking request ${request.url}") privacyProtectionCountDao.incrementBlockedTrackerCount() return WebResourceResponse(null, null, null) } }

Slide 28

Slide 28 text

Block web trackers class BrowserWebViewClient() : WebViewClient() { private fun blockRequest( trackingEvent: TrackingEvent, request: WebResourceRequest, webViewClientListener: WebViewClientListener? ) : WebResourceResponse { trackingEvent.surrogateId ?. let { surrogateId -> val surrogate = resourceSurrogates.get(surrogateId) if (surrogate.responseAvailable) { Timber.d("Surrogate found for ${request.url}") webViewClientListener ?. surrogateDetected(surrogate) return WebResourceResponse(surrogate.mimeType, "UTF-8", surrogate.jsFunction.byteInputStream()) } } Timber.d("Blocking request ${request.url}") privacyProtectionCountDao.incrementBlockedTrackerCount() return WebResourceResponse(null, null, null) } }

Slide 29

Slide 29 text

Blocking third party cookies

Slide 30

Slide 30 text

Block third party cookies private suspend fun processThirdPartyCookiesSetting( webView: WebView, uri: Uri ) { val host = uri.host ?: return val domain = authCookiesAllowedDomainsRepository.getDomain(host) withContext(dispatchers.main()) { if (domain != null && hasUserIdCookie()) { Timber.d("Cookies enabled for $uri") cookieManagerProvider.get().setAcceptThirdPartyCookies(webView, true) } else { Timber.d("Cookies disabled for $uri") cookieManagerProvider.get().setAcceptThirdPartyCookies(webView, false) } domain ?. let { deleteHost(it) } } }

Slide 31

Slide 31 text

Block third party cookies private suspend fun processThirdPartyCookiesSetting( webView: WebView, uri: Uri ) { val host = uri.host ?: return val domain = authCookiesAllowedDomainsRepository.getDomain(host) withContext(dispatchers.main()) { if (domain != null && hasUserIdCookie()) { Timber.d("Cookies enabled for $uri") cookieManagerProvider.get().setAcceptThirdPartyCookies(webView, true) } else { Timber.d("Cookies disabled for $uri") cookieManagerProvider.get().setAcceptThirdPartyCookies(webView, false) } domain ?. let { deleteHost(it) } } }

Slide 32

Slide 32 text

Block third party cookies private suspend fun processThirdPartyCookiesSetting( webView: WebView, uri: Uri ) { val host = uri.host ?: return val domain = authCookiesAllowedDomainsRepository.getDomain(host) withContext(dispatchers.main()) { if (domain != null && hasUserIdCookie()) { Timber.d("Cookies enabled for $uri") cookieManagerProvider.get().setAcceptThirdPartyCookies(webView, true) } else { Timber.d("Cookies disabled for $uri") cookieManagerProvider.get().setAcceptThirdPartyCookies(webView, false) } domain ?. let { deleteHost(it) } } }

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

App Tracking Protection

Slide 35

Slide 35 text

Identify calling app private fun getPackageIdForUid(uid: Int) : String { val packages: Array? try { packages = packageManager.getPackagesForUid(uid) } catch (e: SecurityException) { Timber.e(e, "Failed to get package ID for UID : $uid due to security violation.") return "unknown" } if (packages.isNullOrEmpty()) { Timber.w("Failed to get package ID for UID : $uid") return "unknown" } return packages.f i rst() }

Slide 36

Slide 36 text

Identify calling app private fun getPackageIdForUid(uid: Int) : String { val packages: Array? try { packages = packageManager.getPackagesForUid(uid) } catch (e: SecurityException) { Timber.e(e, "Failed to get package ID for UID : $uid due to security violation.") return "unknown" } if (packages.isNullOrEmpty()) { Timber.w("Failed to get package ID for UID : $uid") return "unknown" } return packages.f i rst() }

Slide 37

Slide 37 text

Blocking an app tracker

Slide 38

Slide 38 text

Blocking an app tracker // Called from native code private fun isAddressAllowed(packet: Packet) : Allowed? { packet.allowed = (callback.get() ?. isAddressBlocked(packet.toAddressRR()) == false) return if (packet.allowed) Allowed() else null }

Slide 39

Slide 39 text

Blocking an app tracker /** * Called by the VPN network to know if a particular IP address is blocked or not. * This can be combined with the [onDnsResolved] callback, to get the hostname of the * [addressRR] and then decide whether that hostname should be blocked or not * @param addressRR is the address record */ fun isAddressBlocked(addressRR : AddressRR) : Boolean

Slide 40

Slide 40 text

Blocking an app tracker override fun isAddressBlocked(addressRR : AddressRR) : Boolean { val hostname = addressLookupLruCache[addressRR.address] ?: return false val domainAllowed = shouldAllowDomain(hostname, addressRR.uid) return !domainAllowed }

Slide 41

Slide 41 text

Blocking an app tracker private fun shouldAllowDomain(name: String, uid: Int) : Boolean { val packageId = getPackageIdForUid(uid) val type = appTrackerRepository.f i ndTracker(name, packageId) if (type is AppTrackerType.ThirdParty && !isTrackerInExceptionRules(packageId = packageId, hostname = name)) { val trackingApp = appNamesCache[packageId] ?: appNameResolver.getAppNameForPackageId(packageId) // if the app name is unknown, do not block if (trackingApp.isUnknown()) return true VpnTracker( trackerCompanyId = type.tracker.trackerCompanyId, domain = type.tracker.hostname, trackingApp = TrackingApp(trackingApp.packageId, trackingApp.appName) ).run { appTrackerRecorder.insertTracker(this) } return false } return true }

Slide 42

Slide 42 text

Blocking an app tracker private fun shouldAllowDomain(name: String, uid: Int) : Boolean { val packageId = getPackageIdForUid(uid) val type = appTrackerRepository.f i ndTracker(name, packageId) if (type is AppTrackerType.ThirdParty && !isTrackerInExceptionRules(packageId = packageId, hostname = name)) { val trackingApp = appNamesCache[packageId] ?: appNameResolver.getAppNameForPackageId(packageId) // if the app name is unknown, do not block if (trackingApp.isUnknown()) return true VpnTracker( trackerCompanyId = type.tracker.trackerCompanyId, domain = type.tracker.hostname, trackingApp = TrackingApp(trackingApp.packageId, trackingApp.appName) ).run { appTrackerRecorder.insertTracker(this) } return false } return true }

Slide 43

Slide 43 text

Blocking an app tracker private fun shouldAllowDomain(name: String, uid: Int) : Boolean { val packageId = getPackageIdForUid(uid) val type = appTrackerRepository.f i ndTracker(name, packageId) if (type is AppTrackerType.ThirdParty && !isTrackerInExceptionRules(packageId = packageId, hostname = name)) { val trackingApp = appNamesCache[packageId] ?: appNameResolver.getAppNameForPackageId(packageId) // if the app name is unknown, do not block if (trackingApp.isUnknown()) return true VpnTracker( trackerCompanyId = type.tracker.trackerCompanyId, domain = type.tracker.hostname, trackingApp = TrackingApp(trackingApp.packageId, trackingApp.appName) ).run { appTrackerRecorder.insertTracker(this) } return false } return true }

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

App breakage

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

github.com/duckduckgo/Android

Slide 48

Slide 48 text

Thank you