Author: Lei Long, black snow, steamed rice @ Alibaba Mobile Security
0x01 code signing (CodeSign)
In order to protect the developer's copyright and piracy prevention application, Apple has a very strict system signature protection mechanisms. Want to develop iOS apps, developers must first register an account and apply for Apple-related certificate, otherwise the program can only be run on the simulator, can not debug on a real machine, can not be added to our App Store. In addition to traditional signature scheme, Apple has also additional security precautions Team ID, used to enhance the security of iOS system
(1) Traditional signature scheme - Digital Certificates
Traditional signature mechanism, the digital certificate mechanism iOS used in the system. A digital certificate is a kind of digital content validation method, which first content usage digest algorithm (such as MD5, SHA1) hash value is generated for a fixed length (can be understood as a summary of the original content), and then use the private key of this summary encrypting the original content to obtain a digital signature. Together with the original recipient receives the content and digital signature, first generates a summary of the original content with the same digest algorithm, while using the public key to decrypt the digital signature to obtain a summary of 2, then 1 and 2 Summary of comparison summary, if the same, then verify the original content is valid. We obtained a digital certificate from Apple MC (Member Center) is to be the legitimate Apple CA signed certificate names. The iOS device before executing the app, first of all to verify the CA's signature is legitimate, then by the certificate in our public key to verify whether indeed the app developer release, and she had not been tampered with on the program. Theoretically want to break or circumvent this signature mechanisms need to be able to get Apple's private key, or signature verification process to find vulnerabilities.
(2) The signature verification implementation
iOS before you run the code, the code will run will be signed check. Signature verification mechanism is running in the kernel. So you want to turn off this check, the need for the system to escape the job. Vm_fault_enter core provisions in the majority of cases, has performed bit page needs to be signed validity checks to the page to check if the signature is invalid setting process will kill flag. Signature verification are two cases; if the binary is platform binary, the hash value of the system will directly check binary exists in trustcache in. If the binary is a third-party application, it will first check is performed in the kernel page corresponding to hash value, and the corresponding hash signature page by a user mode process amfid verify its correctness.
(3). Team ID
Team ID was first presented in iOS 8, when iOS 9 were further strengthened. Team ID appears mainly in order to prevent an attacker to load your own dynamic libraries do not belong to their own executable, it is common examples: jailbreak process will be dynamic libraries loaded into system processes, the ability to obtain the execution of arbitrary code outside the sandbox; malicious applications escape valuable information will be loaded into someone else's app runtime environment, steal account passwords and other by sandbox own dynamic libraries. So the specific validation logic Team ID is designed according to this principle. In special cases, the system can only process dynamic loading system. Third-party app according to your Team ID to determine which has the same dylib Team ID's can be loaded.
0x02 sandbox mechanism (SandBox)
Many systems have a sandbox mechanism, but like iOS rarely so complicated. iOS from UID / GID permission, MAC and entitlement three dimensions to achieve the sandbox mechanism of the entire system:
(1). UID / GID permission
In general, the process will be divided into iOS permissions root and mobile, special module (such as baseband) have their own user group. Note that, app is running in the mobile all third-party rights.
(2). IOS Mandatory Access Control
iOS in the MAC TrustedBSD Mac Framework based on the realization, in particular the kernel interfaces, the specific location of insert permission hook check (mac _ ** call), check whether the current process call occurs when MAC police called to meet.
The MAC police process mainly through sandbox profile. Sandbox profile for each Apple system default app or process, such as: Which file is readable and writable, and which can not; which system call can be called and what is not, and so on.
For system processes, in general, Apple will be equipped with different system processes different sandbox profile, both to meet the business needs, but also follow the principle of minimizing the privilege.
For third-party app, it is unified with sandbox profile, called the Container, the profile of which up to thousands of content restrictions. Very restrictive, so that only a few can be accessed within syscall third party app. Some very common Andrews calls, such as fork, exec, etc. create a child process of system calls in the third-party app are unable to force. We often say that the sandbox escape, in fact, the purpose is to jump out of the container sandbox profile.
Entitlement appears mainly to the above two dimensions are Wufajiejue permission to check for problems.
Imagine a scenario:
A process is a service, process B is the client, both by IPC communication.
A service interface process provided respectively: a1, a2, where a1 can only hope that the interface B access.
Because checking occurs in user mode can not be used directly TrustedBSD Mac Framework, also need to have a more simple query mode, so you need to add permission to check in a2 interface code. Based on entitlement validation framework is in the background of the demand was put forward. Business processes only need to focus on the content of entitlement, and entitlement to ensure the correctness of the signature. "Com.apple.mobile.installd" service must have a corresponding "com.apple.private.mobileinstall.allowedSPI" For example you want to delete all app provides access interfaces entitlement job. And lockdownd This service is used to interact with and iTunes to install, upgrade, delete the application, so this service for communication with installd service, app delete operation, you need to have "com.apple.private.mobileinstall.allowedSPI" this entitlement:
0x03 utilization to mitigate (Exploit Mitigation)
In addition to the common Stack Canaries, ASLR and DEP mitigation techniques such as the use outside, iOS there are many senior or exclusive use of mitigation techniques:
(1). Stack canary protection (Stack Canaries)
Canary stack protection are known to be placed in a random value between the data buffer and control. When the buffer overflows, the first to be destroyed is usually the canary value. So when authentication data canary fail, it means there has been a buffer overflow, triggering protection mechanisms, and the program stops running.
(2) The address randomization (ASLR / KASLR)
In order to increase the difficulty of forecasting the destination address of the attacker, to prevent attackers from directly targeting attack code location, user mode processes each time you start the executable file base address are randomly generated. And, after each phone restart, the core kernel mach-o base address is random.
(3) Data Execution Prevention (DEP)
DEP is designed to prevent data pages to execute code. Typically, the default does not execute code from the stack and heap. DEP detects code that is running from these locations and raises an exception when execution occurs. In mprotect corresponding kernel implementation, execution and allowed page was given permission to write both simultaneously. When the permissions page is changed or a new page mmap to memory, vm_fault_enter checks whether the page has the execute bit, bit if executed, would make this page signature check.
(4). Heap release element protection (Heap Free Element Protection)
In iOS, if you modify a zone has been released free element, when the memory manager allocates memory again to the free element will occur randomly panic. Specific logic is that when the element is released, the kernel will generate some content filled in element according to reboot token is created. So that one user can not know what state is filled, on the other hand, when the kernel memory allocation can know this token element has not been modified, if it is modified to generate panic.
(5). Stack element address randomization (Random Heap Element Address)
iOS system in the process of freeing memory block, random memory would be released after treatment order free queue, this Anquancuoshi mainly attacker can not be predicted using the corresponding element in the layout according to the timing of the kernel heap spray interface calls .
(6). Kernel Patch Protection (Kernel Patch Protection)
ARMv8-A architecture defines four levels of exceptions, were EL0 to EL3, wherein the larger the number, the representative of privilege (privilege) the greater:
EL0: unprivileged mode (unprivileged)
EL1: Caozuojitong kernel mode (OS kernel mode)
EL2: virtual machine monitor mode (Hypervisor mode)
EL3: TrustZone monitor mode
KPP is to run in the Application Process EL3, the purpose is to ensure: a read-only page can not be modified, page table can not be modified, executed page can not be modified.
Although there are numerous iOS security mechanisms and mitigation measures, but that does not mean iOS system unbreakable. Sometimes some obscure small mistake can lead to the butterfly effect, eventually leading to the collapse of the entire security system. Through research on the latest iOS 9.3.4, our team still found a number of security issues on the iOS system, and even cause the entire system to be controlled. Being on the video below demonstrates the highest authority on the latest iOS 9.3.4 and install cydia procedure:
1. Hacking from iOS 8 to iOS 9, POC 2015.
2. ARMv8 wiki
3. To Sign and Protect - COPS in OS X and iOS, RSA 2015
Certificates and signatures mechanism 4. Talk iOS apps