This document discusses iOS application penetration testing from the perspective of a penetration tester. It begins with an overview of iOS applications and the iOS monoculture, covering code signing, sandboxing, and encryption. It then discusses various techniques a penetration tester may use, including checking compile options, exploiting URL schemes, analyzing insecure data storage in databases, property lists, keyboard caches, image caches, and error logs. It also covers runtime analysis using tools like Clutch, Class-Dump-Z, and Cycript to decrypt binaries, dump classes, and interact with running apps. Examples are provided of potential attacks against apps that involve bypassing locks, extracting hardcoded keys, or injecting malicious code. Defense techniques are also briefly explained.
2. Angenda
• What’s the iOS App?
• Monoculture
– Code Signing
– Sandboxing
– Encryption
• The Perspective of Pen Tester
– Compile Option
– URLScheme
– Insecure Database
– Runtime Analysis
– Transport Security
3. What’s the iOS App?
• Development in Xcode
– You don’t have to buy a Mac since this is for
BlackBox Assessment
• Applications written primarily in
Objective-C Compiled(ARM) Executable
– Need to background about the structure of Objective-C
• Encrypted Executable
• Installed by “mobile” user
– Compared to each Android apps which use
different UID/GID
4. Angenda
• What’s the iOS App?
• Monoculture
– Code Signing
– Sandboxing
– Encryption
• The Perspective of Pen Tester
– Compile Option
– URLScheme
– Insecure Database
– Runtime Analysis
– Transport Security
6. Monoculture – Code Signing
• Provisioning Profile
–Asymmetric cryptography
–UDIDS(prevents running of restricted
devices)
–Certificate(with trust based on the
signing by a developer)
–App ID in the info.plist
7. Monoculture – Code Signing
• Developer Certificates
Developer Program
Device
Testing
Ad Hoc App Store In-House
Apple Developer No No No No
University Program Yes No No No
Standard Program Yes Yes Yes No
Enterprise Program Yes Yes No Yes
9. Monoculture – Code Signing
• Signed Applications
– All iOS executable binaries and applications
must be signed by a trusted certificate
[Ad-hoc code signed executable]
10. Monoculture – Code Signing
• Signed Applications
– All iOS executable binaries and applications
must be signed by a trusted certificate
[Normal code signed executable]
13. Monoculture – SandBoxing
• Sandboxing
– Third party apps assigned “container” seatbelt
profile
– Function calls hooked by the TrustedBSD
layer will pass through Sandbox.kext for
policy enforcement
Compared to
Andorid
UID/GID
15. Monoculture - Encryption
• EMF!
– Used to encrypt the filesystem
• Dkey
– Used to encrypt the master key of the
protection class "NSFileProtectionNone" (the
majority of files)
• BAGI
– Used to produce the encryption keys for the
other master keys
16. Monoculture - Encryption
• Data Protection
Level Description
No Protection The file is not encrypted on the file-system
Complete
Protection
The file is encrypted on the file-system and
inaccessible when the device is locked
Complete
Unless Open
The file is encrypted on the file-system and inaccessible
while closed. When a device is unlocked an app can
maintain an open handle to the file even after it is
subsequently locked, however during this time the file will
not be encrypted
Complete Until
First User
Authentication
The file is encrypted on the file-system and inaccessible
until the device is unlocked for the first time. This helps
offer some protection against attacks the require a
device reboot
Dkey
on Plog
17. Monoculture - Encryption
• File System Encryption
– Every File is encrypted with unique key "cprotect"
key
– File key is stored in the file metadata
– Metadata is encrypted with EMF Key
– Content is grouped into protection class
– Each protection class assigned a master
encryption key
– Master keys are protected by device key and
passcode
– Protected master keys form system keybag
23. Monoculture – Old Things
• PIE(Positon Independent Executable)
– Exploit mitigation security feature that allows an application to
take full advantage of ASLR
• Stack-Smashing Protection
– “Canary” is placed on the stack directly before the local variables
to protect the saved base pointer
• Automatic Reference Counting
– Introduced in iOS SDK version 5.0 to move the responsibility of
memory management from the developer to the compiler
– Offers some security benefits as it reduces the likelihood of
developers introducing memory corruption(specifically object
use-after-free and double free vulnerabilities)
24. Angenda
• What’s the iOS App?
• Monoculture
– Code Signing
– Sandboxing
– Encryption
• The Perspective of Pen Tester
– Compile Option
– URLScheme
– Insecure Database
– Runtime Analysis
– Transport Security
30. Angenda
• What’s the iOS App?
• Monoculture
– Code Signing
– Sandboxing
– Encryption
• The Perspective of Pen Tester
– Compile Option
– URLScheme
– Insecure Database
– Runtime Analysis
– Transport Security
31. URLSchema - Overview
• Noreal Inter-Process Communication
• Apps prohibited from sharing because of
sandbox
• Apps sometimes need to share data
• Apps can register a custom protocol
handler
32. URLSchema - Overview
• Two methods for implementing protocol
handlers
• handleOpenURL
– Now deprecated
• openURL
– Provides bundle identifier
– Allows developer to validate source app
33. URLSchema - Attack
• Skype registers the “skype://” protocol
handler
• Malicious web site could make calls
• Skype app did not prompt or validate
before call
– Allows developer to validate source app
• Play the Video
34. Angenda
• What’s the iOS App?
• Monoculture
– Code Signing
– Sandboxing
– Encryption
• The Perspective of Pen Tester
– Compile Option
– URLScheme
– Insecure Database
– Runtime Analysis
– Transport Security
36. Databases - Overview
• Data stored in the Sqlite in un-
encrypted
• SQLite files are stored un-encrypted
in the iOS backups
• Delete files can be recovered by
carving the HFS Journal
• Not deleted completely
40. Database - Defense
• Do not store sensitive data in clear text
• Use custom encryption
• Protect Sqlite files with data protection API
• Implement classes for secure file wipe
• Purge the data upon deletion with VACUUM SQL
command
– VACUUM rebuild the databases
– Doing it for every delete consume time
• Before deleting the SQL record, replace the
data with junk values
– Data and Junk value length has to be same
45. Insecure Data Storage
• Databases(SQLite, coreData)
• Plists
• Keyboard Cache
• Image Cache
• Keychain
• Cookie Binary
• Error Logs
• Hidden Files(ex. Citi Mobile app)
46. Plists - Overview
• Property list files - Key value pairs
stored in binary
• Identified by a file header – bplist
• Designed to store user's properties
and configuration information
• Not protected by Data protection
• But Apps store credentials such as
username, password, and session
information
49. Plists – Defense
• Do not store sensitive data in Plist files
• If required, use custom encryption
• Create plist files Library/Caches folder
– iTunes does not backup caches directory
• For better security, Implement classes for
secure file wipe
– Before deleting the file overwrite the file bytes with
junk values
50. Insecure Data Storage
• Databases(SQLite, coreData)
• Plists
• Keyboard Cache
• Image Cache
• Keychain
• Cookie Binary
• Error Logs
• Hidden Files(ex. Citi Mobile app)
51. Keyboard Cache - Overview
• Designed to auto complete the
predictive common words
• Secure fields are not stored
– passwords are safe
• String with all digits are not stored
– Pins and credit card number are safe
• Data typed into text fields are cached
53. Keyboard Cache - Attack
• Keyboard Cache
– iPassword
– What if it is banking information?
54. Keyboard Cache - Defense
• Keyboard Cache
– Disable auto correction
• Mark it as a secure field
– mytextField.secureTextEntry = YES
• Copy&Paste
55. Insecure Data Storage
• Databases(SQLite, coreData)
• Plists
• Keyboard Cache
• Image Cache
• Keychain
• Cookie Binary
• Error Logs
• Hidden Files(ex. Citi Mobile app)
56. Image Cache - Overview
• Takes screen shots of the application
to trick you
• Sensitive data may get cached
– App directory/Library/Caches/Snapshots
57. Image Cache - Attack
• Image Cache
– Mail App
Press Home Button
Two Times
58. Image Cache - Attack
• Image Cache
– Mail App
IPod
My Local
Machine
59. Defense – Image Cache
• Remove sensitive data or change the
screen before the
applicationDidEnterBackground()
function returns
• Instead of hiding or removing sensitive
data you can also prevent back-
grounding altogerher by setting the
"Application does not run in background"
property in the application's Info.plist file
63. Insecure Data Storage
• Databases(SQLite, coreData)
• Plists
• Keyboard Cache
• Image Cache
• Keychain
• Cookie Binary
• Error Logs
• Hidden Files(ex. Citi Mobile app)
64. Keychain - Overview
• Keychain encryption is tied to the device
– Protected entries are tied to the user's passcode
• Keychain file is accessible to all the
application
• Application can only acess it's own key
chain items
– Based on app keychain access group
65. Keychain - Overview
Attribute Description
kSecAttrAccessibleAlways The keychain item is always accessible
kSecAttrAccessibleWhenUnlocked
The keychain item is only accessible
when the device is unlocked.
kSecAttrAccessibleAfterFirstUnlock
They keychain item is only accessible after
the first unlock from boot. This helps offer
some protection against attacks that require
a device reboot
kSecAttrAccessibleAlwaysThisDevice
Only
The keychain item is always accessible but
cannot be migrated to other devices
kSecAttrAccessibleWhenUnlockedThi
sDeviceOnly
The keychain item is only accessible when
the device is unlocked and may not be
migrated to other devices
kSecAttrAccessibleAfterFirstUnlock
ThisDeviceOnly
The keychain item is accessible after the
first unlock from boot and may not be
migrated to other devices
67. Keychain - Defense
• Do not store sensitive data in clear text
• Encrypt the data using custom
encryption
• Use data protection API while storing
data in keychain
• By default entries are created with
kSecAttrAccessibleWhenUnlocked data
protection
68. Insecure Data Storage
• Databases(SQLite, coreData)
• Plists
• Keyboard Cache
• Image Cache
• Keychain
• Cookie Binary
• Error Logs
• Hidden Files(ex. Citi Mobile app)
69. Coockies Binary
• Binary file to store the cookies
• Persistent cookies are stored along
with the flags(Secure, HTTPOnly)
• Most iOS apps not prompt the user
for login every time and creates
persistent cookies
71. Insecure Data Storage
• Databases(SQLite, coreData)
• Plists
• Keyboard Cache
• Image Cache
• Keychain
• Cookie Binary
• Error Logs
• Hidden Files(ex. Citi Mobile app)
72. Error Logs
• Apps may write sensitive data in logs
– Debugging(NSLog calls)
– Trouble shooting
– Request & Response
• Located at - /private/var/log/syslog
• Syslog is out of sandbox - Any app can
access it
• Do not write sensitive data in the syslog
file
73. Insecure Data Storage
• Databases(SQLite, coreData)
• Plists
• Keyboard Cache
• Image Cache
• Keychain
• Cookie Binary
• Error Logs
• Hidden Files(ex. Citi Mobile app[Fixed])
74. Angenda
• What’s the iOS App?
• Monoculture
– Code Signing
– Sandboxing
– Encryption
• The Perspective of Pen Tester
– Compile Option
– URLScheme
– Insecure Database
– Runtime Analysis
– Transport Security
75. Runtime Analysis - Overview
• Developed in Objective
– Superset of C
– Exiting methods can be overwritten easily
• Self distributed Apps are not
encrypted
• AppStore binaries are encrypted
• Decrypts the apps when loaded
into memory
76. Runtime Analysis - Tools
• Clutch
– Decrypt the binary
• Class-Dump-Z
– Extract the class information and identify
interesting class
• Cycript
– Combination of JavaScript and Objective-C
interpreter
– Gives access to all classes and instance
variables within the app
• GDB & IDA Pro
Samples: Coupang, Password_Keeper, piracydemo2
78. Runtime Analysis –
Decrypt Manually
(gdb) set sharedlibrary load-rules ".*" ".*" none
(gdb) set inferior-auto-start-dyld off
(gdb) set sharedlibrary preload-libraries off
(gdb) rb doModInitFunctions
Breakpoint 1 at 0x2fe0d526
<function, no debug info>
__dyld__ZN16ImageLoaderMachO18doModInitFunctionsERKN11Ima
ge
(gdb) r
Starting program: /private/var/mobile/Applications/3DC4CAC7-
A429-449E-BB6C-A985E3A644
Program received signal SIGABRT, Aborted.
0x3a5b7350 in ?? ()
(gdb) dump memory coupang.bin 12288 950272
(gdb) kill
Kill the program being debugged? (y or n) y
(gdb) q
114. Transport Security - UDID
• Much like the Pentium 3 CPU, each
Apple iPhone is equipped with a unique,
application-visible serial number(40-
characcter) called a Unique Device ID
(UDID).
• According to Apple, the use of UDIDs
allows businesses to ensure that devices
continue to comply with required policies.
• Not Any more!!!
118. Transport Security - GeoLocation
• Provided by the Core Location framework
• Avoid being “too accurate”
• Can be set by one of the following
constants:
– kCLLocationAccuracyBestForNavigation;
– kCLLocationAccuracyBest;
– kCLLocationAccuracyNearestTenMeters;
– kCLLocationAccuracyHundredMeters;
– kCLLocationAccuracyKilometer;
– kCLLocationAccuracyThreeKilometers;
122. Transport Security - SSL
• Vunerable to MITM
– ex) Global Banking App(It uses
“allowsAnyHTTPSCertificateForHost” which
overrides self-signed certificate)