This document discusses Android security and hacking techniques. It covers the Android architecture including its use of Linux kernels and Java libraries. It describes Android's permission model and how apps are sandboxed. It discusses techniques for hacking Android like rooting devices, decompiling apps, intercepting network traffic, and exploiting intents. It also covers ways attackers can leak information and how to mitigate security risks.
3. Open Source
• Android itself is open source (Link Ch 4b)
• But the Google apps included with most
Android phones are closed-source
• Many device manufacturers and carriers
modify Android
– Closed-source device drivers and apps
– Leads to fragmentation: two devices with the
same hardware but different carriers can be
running very different software
– More total sales, many different Android devices
6. Linux Kernel
• A way for applications to interact with devices
• Manages processes and memory
• Android versions prior to 4.0 used 2.6 Linux
kernel
• Later versions use 3.x Linux Kernel
7. Libraries
– OpenGL (for 2D/3D graphics)
– SQLite for databases
– WebKit (for rendering Web pages)
– Others
• Written in C/C++
• Also Dalvik VM and core Java libraries
• All this together is called the "Android
Runtime" component
8. Application Framework
• A way for Android apps to access
– Telephone functionality
– Creating UI (User Interface) elements
– GPS
– File system
• Important in Android security model
9. Apps
• Usually Written in Java
– Compiled to Dalvik bytecode using the Android
Software Development Kit (SDK)
• Can also be written in C/C++
– Using Native Development Kit (NDK)
11. Permission Based
• Apps must be explicitly granted permission to
take action
• Permissions enforced in two places
– Kernel level
– Application Framework level
12. Kernel Permissions
• Each app has a unique user ID
– File ownership based on User ID
• Can only access the resources and
functionality it has explicit permissions for
• This is "sandboxing"
– Apps cannot access resources of other apps
– Apps cannot access hardware components they
have not been given permission to use
14. Viewing Users with ps
• Apps run as u0_a1, u0_a2, etc.
• Permissions for data directories
15. Application Framework Access Control
• App must declare a permission
for each component it accesses
in its manifest file
– AndroidManifest.xml
• Permissions are shown to the
user at install time
– User can chose whether to allow
the permissions
– If not, the app can't be installed
• Once installed, the app has
only those permissions, such as
android.permission.INTERNET
17. Permission Categories
• Normal
– Low-risk
– Don't require explicit approval from users at install
time
• Dangerous
– Require explicit approval from users at install time
18. Permission Categories
• Signature
– Defined by an application in its manifest
– Functionality exposed by applications that declare
this permission can only be accessed by other
applications that were signed by the same
certificate
• signatureOrSystem
– Same as Signature, but applications installed on
the /system partition can also access this
functionality
19. App Signing
• All apps must be signed to be installed
• Android allows self-signed certificates
– Developers can generate their own signing
certificates
• The only security mechanisms that use
signatures are the signature or
signatureOrSystem permissions
20. Address Space Layout
Randomization (ASLR)
• Added in Android 4.0
• Makes it more difficult to exploit memory
corruption issues
• Randomizes locations of key memory sections
– Such as stack and heap
• In 4.0, several locations, including the heap
were not randomized
• Android 4.1 has full ASLR
21. No eXecute (NX) Bit
• Added in Android 2.3
• Allows you to set the heap and stack as
nonexecutable
– Helps prevent memory corruption attacks
23. Four Components
• Activities, Content providers, Broadcast
receivers, Services
– Entry points: ways the user or another app on the
same device can enter
• The more components that are exported, the
larger the attack surface
– Component header will contain this line:
android:exported="true'
24. Intents
• Applications primarily use intents
– Asynchronous messages
• To perform interprocess, or intercomponent,
communication
25. Activities
• Define a single screen of an applications user
interface
• Android promotes reusability of activities
• This saves developers time and work
• Increases the attack surface of an app
26. Content Providers
• Exposes ability to query, insert, update, or
delete application-specific data
– to other apps and internal components
• App might store data in SQLite database or a
flat file
• Calling component won't know what storage
method is used
• Poorly written content providers may expose
data or be vulnerable to SQL injection
27. Broadcast Receivers
• Respond to broadcast intents
• Apps should not blindly trust data from
broadcast intents
– It could be from a hostile app or a remote system
28. Services
• Run in the background
• Perform lengthy operations
• Started by another component when it sends
an intent
• Services should not blindly trust the data
contained within the intent
29. Data Storage
• Internal storage in
nonvolatile memory (NAND
flash)
– Private to one app
• External storage on an SD
card
– Also NAND flash, but
removable
– Available to all apps
– Image from amazon.com
30. File Types
• Apps are free to create any type of file, but
the API comes with support for
– SQLite databases
– Shared preference files in an XML-based format
• SQL injection attacks are a risk, via intents or
other input
31. NFC (Near Field Communication)
• Radio communication, including
– NFC tags
– Some RFID tag protocols
– Contactless smart cards
– Mobile devices
• Short range: a few cm
– image from newegg.com
32. NFC in Android
• Introduced in 2010 with Gingerbread
• Expanded in 2.3.3 to support a variety of NFC
formats
• Card Emulation mode came In 2.3.4
– An NFC reader can read data from the Secure
Element (SE) within the phone
– Card emulation is not exposed via the Android
SDK
– Only Google or carriers have this capability
33. Google Wallet
• Lets you use your phone for a
credit card
• Released with Android 2.3.4
34. NFC Peer-to-Peer
• Allows two NFC-enabled devices to
communicate directly
• Android Beam allows users to share data by
tapping their devices together
• NFC tags are also used in advertisements
36. SDK (Software Development Kit)
• Allows developers to build and debug Android
apps
– Runs on Windows, Mac OS X, or Linux
• In Dec., 2014, Google released Android Studio
– A full IDE (Integrated Development Environment)
– Links Ch 4e, 4f
37. Android Emulator
• Helps developers
test apps without
an actual mobile
device
• Simulates
common hardware
– ARMv5 CPU
– SIM card
– Flash memory
partitions
38. Value of the Emulator
• Allows developers and researchers to test
Android apps quickly in different versions of
Android
• Drawbacks
– Android Virtual Device (AVD) cannot send or
receive phone calls or SMS messages
– But it can emulate them, and send them to other
AVDs
• Can define an HTTP/HTTPS proxy to intercept
and manipulate Web traffic
39. Android Debug
Bridge
• Command-line tool
• Allows you to communicate with a mobile
device via a USB cable or an SVD running
within an emulator
• Connects to device's daemon running on TCP
port 5037
40. Useful ADB Commands
• push
– Copies a file from your computer to the mobile
device
• pull
– Copies a file from the mobile device to your
computer
• logcat
– Shows logging information on the console
– Useful to see if an app or the OS is logging
sensitive information
41. Useful ADB Commands
• install
– Copies an application package file (APK) to the
mobile device and installs the app
– Useful for side-loading apps (so you don't have to
use Google Play)
• shell
– Starts a remote shell on the mobile device
– Allows you to execute arbitrary commands
44. Root
• The root user can directly access system
resources
• Bypassing the permissions checks normally
required
• Giving the app that runs as root full control
over the device and other apps running on it
46. How Gingerbreak Works
• Vulnerability in volume manager daemon
– /system/bin/vold
• Method DirectVolume::handlePartitionAdded
– Sets an array index using an integer passed in
– Does not check for negative integers
– Using negative values, exploit can access arbitrary
memory locations
47. How Gingerbreak Works
• Exploit writes to vold's global offset table
(GOT)
• Overwrites several function calls
– such as strcmp() and atoi()
• With calls to system()
• Then another call to vold to execute an app
via system(), with vold's elevated privileges
48. GingerBreak
• Packaged into several rooting tools
– Such as SuperOneClick
• And some one-click rooting APKs
– Malicious app could include GingerBreak as a way
to gain elevated privileges on a device
49. GingerBreak Countermeasures
• Update your device
– GingerBreak exploit was fixed in Android 2.3.4
• But some manufacturers/carriers allow
updates
• Antivirus apps should detect the presence of
GingerBreak
– An alternative
50. Ice Cream Sandwich
init chmod/chown Vuln
• Vuln introduced with Ice Cream Sandwich
(4.0.x) in init
• If the init.rc script has an entry like this
– mkdir /data/local/tmp 0771 shell shell
• Even if the mkdir failed, init would set the
ownership and permissions for the shell user
– The ADB user
51. Symlink
• If /data/local is writable by the shell user,
• Creating a symlink to /system there allows the
ADB user read/write access to the /system
partition
• ADB user can add files to /system, such as su
• Thus gaining root access to a device
52. Ice Cream Sandwich
init chmod/chown Countermeasures
• Keep devices updated
• Make sure Android debugging is turned off
• And keep device locked, so attacker can't turn
it on
54. Static Analysis
• Source code is generally kept confidential by
app developers
• A binary, compiled app can be analyzed by
disassembling or decompiling them, into
– Smali assembly code (used by Dalvik VM), or
– Java code
59. Attacks via Decompiling and
Disassembly
• Insert Trojan code, like keyloggers
• Find encryption methods & keys
• Change variables to bypass client-side
authentication or input validation
• Cheat at games
61. Decompiling, Disassembly, and
Repackaging Countermeasures
• Every binary can be reverse-engineered
– Given enough time and effort
• Never store secrets on the client-side
• Never rely on client-side authentication or
client-side validation
• Obfuscate source code
– ProGuard (free) or Arxan (commercial)
65. HTTP Traffic
• Trivial to intercept
• No protections
• Any hacker in a coffeehouse can do it with
– ARP poisoning
– A rogue access point like the WiFi Pineapple
• Very difficult for the end user to detect
66. HTTPS
• Specifically designed to
prevent MITM attacks
• Only very powerful
adversaries like nation-
states or big corporations
can get real Certificate
Authority certificates
• Without one, targets will
see a warning like this
67. Problems
• End-users are not often aware of the
difference between HTTP and HTTPS
• The mobile app may not display any
information about the connection
• Mobile apps don't always verify all the
security features in HTTPS, so users get less
protection
68. Auditing HTTPS
Traffic
• If you want to see
HTTPS traffic, you must
import your proxy's
certificate into your
test mobile device
• With "Install from SD
Card" option in Settings
69. Manipulating Network Traffic
Countermeasures
• Don't disable certificate verification and
validation
– Often done during development and debugging
for convenience
– May be left that way when app is distributed
• Example of trust chain
70. Certificate Pinning
• Adds another layer of verification to
certificates
• Browser knows which CA should validating
sites
– Either by consulting a whitelist maintained by
Google, or
– Remembering which CA was used the first time
the site was used
• Warns user if CA changes
– Link Ch 4z62
71. Don't Trust the Client
• Any data from the client may be malicious
• Perform strict input validation and output
encoding on the server side
73. Intents
• Primary inter-process communication (IPC)
method used by Android apps
• Intents can start internal components, or pass
data to them, or go to other apps
• Those are called "External Intents"
• Links Ch 4z62-64
74. Code to Send an SMS Intent
• Receiving app needs an "Intent Filter" like
– android.provider.Telephony.SMS_RECEIVED
• If an app is not running, receiving an Intent
starts it
• Malicious intents can launch other apps, and
inject data into them
76. Method to Create File
• Executes a shell command, injecting the
filename from the calling app
77. Command Injection
• Suppose the calling app sends a filename of:
– "MyFIle.txt; cat /data/data/com.test/secrets.xml >
/sdcard/secrets.xml"
• The exploit will expose any secrets the test
app can access
78. Command Injection
Countermeasures
• Don't make Intents "exported" unless they are
really needed by other apps
– Leave them internal to their own app
• Perform input validation on all data received
from intents
– Block special characters like ";" and ">" in a
filename
79. Command Injection
Countermeasures
• Custom permissions will warn the user that an
App has permissions when it is installed
– Not much use, in practice
• Signature-level permissions
– Require any app that wants to send intents to be
signed with the same key as the receiving app
– That is, from the same company
– This can be defeated by re-signing the app
81. Malicious NFC Tags
• An NFC tag can contain a URL pointing to a
malicious site
– Such as a WebKit exploit
• Tags are cheap to buy online
• Can be written to by any NFC-enabled phone
83. NFC Attacks
• Attackers can make poster with malicious tags
• Or replace tags on existing posters
• Or overwrite NFC tags in place
– If the tag was not properly write-protected
• Tag could send user to Google Play
• Tag could directly attack an app that uses NFC
with unexpected input
84. Malicious NFC Tag
Countermeasures
• Keep NFC disabled when you aren't using it
• Unfortunately, there's no way to tell if a tag is
malicious without reading it
– Unless there is evidence of tampering
• Developers need to validate data from NFC
• Tags must be write-protected
85. Attacking Apps via NFC Events
• Android allows apps to create fake NFC Intents
– Creating a fake NFC tag
• There's an NFCDemo App
• So a vulnerable NFC-using app could be
exploited even without an NFC tag
86. NFC Event Countermeasures
• Validation of data received from NFC tags
• Custom permissions, or making the Activity
private, won't work here
– App must receive NFC intents from an external
source
88. Leakage via Internal Files
• Each app has a unique User Identifier (UID)
and Group Identifier (GID)
– App runs as that user
• But an app can create a file set to
– MODE_WORLD_READABLE or
– MODE_WORLD_WRITEABLE
• That file would be visible to all apps on the
device, and could be sent to the Internet by
any app with INTERNET permission
89. Android SQLite Journal Information
Disclosure (CVE-2011-3901)
• SQLite engine created its rollback journal as
globally readable and writeable
• Normally deleted after each transaction
• But setting improper permissions allows
hostile apps on the same device to read
– Personal info., session tokens, URL history, etc.
91. SQLite Journal Information
Disclosure Countermeasures
• Install latest patches
• App developers should avoid creating world-
readable or world-writeable files
92. • Skype App stored personal data in world-
readable files
– Link Ch 4z65
93. Leakage via External Storage
• Files stored on the SD card
– Or the emulated SD card
• Are globally readable and writeable to every
app on the device
• Apps should only store data they want to
share on external storage
94. • App seems to never have been updated, and
later removed from the Google Play Store
– Links Ch 4z66-67
95. Nessus Information Disclosure
Countermeasures
• Credentials should not be stored in plaintext
• They should be encrypted
– One good system is AES
– Key from Password-Based Key Derivation Function
2 (PBKDF2)
– Using a password from the user and a device-
specific salt
96. Information Leakage via Logs
• Every app can read the logs, if it requests the
android.permission.READ_LOGS permission at
install time
• Some developers don't realize this and put
sensitive information into the logs
99. Facebook SDK Information
Disclosure Countermeasures
• Update Facebook SDK to latest version
• App developers should not log any sensitive
information
103. content:// URI Scheme Information
Disclosure Countermeasures
• Patched poorly in 2.3, and re-patched in 2.3.4
• End user can do these things:
– Use a different browser, such as Opera
– Disable JavaScript in the Android browser
(impractical)
– Unmount the SD card (impractical because many
apps need it)
104. General Mitigation v.
Information Leakage
• Logs
– Don't log sensitive info.
• Files, shared preferences, and SQLite
databases
– Don't store sensitive info. in plaintext
– Don't create globally readable or writeable files
– Don't store sensitive info. on SD card without
encrypting it properly
105. General Mitigation v.
Information Leakage
• WebKit (WebView)
– WebView is the Android class that displays Web
pages in WebKit
– Apps should clear the cache periodically if it's
used to view sensitive websites
– App can disable caching
106. General Mitigation v.
Information Leakage
• WebKit (WebView) continued
– WebKit stores sensitive data in the app's data
directory, including
• Previously entered form data
• HTTP authentication credentials
• Cookies
– On a non-rooted device, other apps won't be able
to steal data from the WebKit data store
– But if a phone is stolen and rooted, that data can
be taken
107. General Mitigation v.
Information Leakage
• Inter-Process Communication (IPC)
– Don't expose sensitive info. via broadcast
receivers, activities, and services exposed to other
apps
– Don't send sensitive data in intents to other
processes
– Make components nonexportable
108. General Mitigation v.
Information Leakage
• Networking
– Don't use network sockets for IPC
– Use TLS to transmit sensitive data
– CarrierIQ did this (next slide)
• Link Ch 4z72