這篇文章主要講解了“Policy的內(nèi)核表示方法是什么”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Policy的內(nèi)核表示方法是什么”吧!
潼關(guān)網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),潼關(guān)網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為潼關(guān)超過千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)營銷網(wǎng)站建設(shè)要多少錢,請找那個(gè)售后服務(wù)好的潼關(guān)做網(wǎng)站的公司定做!
在 iOS 中有兩個(gè)重要的內(nèi)核擴(kuò)展,分別是 AppleMobileFileIntegrity.kext
和 Sandbox.kext
。
根據(jù) The iPhone Wiki 對 AMFI 的定義[1]:
AppleMobileFileIntegrity(.kext), which can go by its full name com.apple.driver.AppleMobileFileIntegrity, is an iOS kernel extension which serves as the corner stone of iOS's code entitlements model. It is one of the Sandbox's (com.apple.security.sandbox) dependencies, along with com.apple.kext.AppleMatch (which, like on OS X, is responsible for parsing the Sandbox language rules).
即 AMFI.kext
是實(shí)現(xiàn) iOS Code Entitlements
的基礎(chǔ)組件,它和 AppleMatch.kext
(用于解析 Sandbox DSL) 都是 Sandbox.kext
的依賴。
可能有人對 Entitlements 并不熟悉,它代表著 App 擁有的權(quán)限。在正向開發(fā)中,如果我們?yōu)?App 開啟 Capability 就會(huì)生成對應(yīng)的 XML Units 插入到 App.entitlements
,某些 Capability 只有特定的證書才能生成合法簽名。通過這種手段可以限制 Userland App 的權(quán)限,從而保證系統(tǒng)安全。
在運(yùn)行時(shí),內(nèi)核擴(kuò)展會(huì)注冊 Mac Policy 并 hook 特定的 Mach Calls[1]:
Affectionately known as AMFI, this kext can be found in the iOS 5.0 iPod 4,1 kernel around 0x805E499C (start) and 0x805E3EE8 (Initialization function). The latter function registers a MAC policy (using the kernel exported mac_policy_register), which is used to hook various system operations and enforce Apple's tight security policy.
根據(jù) Wiki,AMFI 會(huì) hook 需要 task_for_pid-allow
權(quán)限的 Mach Call[1]:
This kext recognizes the task_for_pid-allow entitlement (among others) and is responsible for hooking this Mach call, which retrieves the Mach task port associated with a BSD process identifier. Given this port, one can usurp control of the task/PID, reading and writing its memory, debugging, etc. It is therefore enabled only if the binary is digitally signed with a proper entitlement file, specifying task_for_pid-allow.
即 AMFI.kext
會(huì)識別 entitlements 中的 task_for_pid-allow
,并 Hook 相關(guān) Mach Call,該 Mach Call 會(huì)通過 BSD 進(jìn)程標(biāo)識符查詢特定進(jìn)程的任務(wù)端口返回給調(diào)用者,使得調(diào)用者可以篡改進(jìn)程的 task 或 PID, 甚至進(jìn)行目標(biāo)進(jìn)程內(nèi)存的讀寫和調(diào)試;而 AMFI.kext
會(huì)在調(diào)用前檢查調(diào)用者的二進(jìn)制是否擁有包含 task_for_pid-allow
的合法簽名。
Sandbox 的實(shí)現(xiàn)與 AMFI.kext
類似,也是通過 Hook 一系列的 Mach Call 并檢查特定的 Policy 來保證訪問的合法性。根據(jù) Dionysus Blazakis 的 Paper: The Apple Sandbox 中的描述[2]:
Once the sandbox is initialized, function calls hooked by the TrustedBSD layer will passthrough Sandbox.kext for policy enforcement. Depending on the system call, the extensionwill consult the list of rules for the current process. Some rules (such as the example givenabove denying access to files under the /opt/sekret path) will require pattern matchingsupport. Sandbox.kext imports functions from AppleMatch.kext to perform regular expression matching on the system call argument and the policy rule that is being checked.For example, does the file being read match the denied path /opt/sekret/.*? The othersmall part of the system is the Mach messages used to carry tracing information (such aswhich operations are being checked) back to userspace for logging.
上述引用主要包含了 3 個(gè)關(guān)鍵點(diǎn):
當(dāng) Sandbox 被初始化后,被 TrustedBSD layer 所 Hook 的 Mach Call 會(huì)通過 Sandbox.kext
執(zhí)行權(quán)限檢查;
Sandbox.kext
會(huì)通過 AppleMatch.kext
解析規(guī)則 DSL,并生成 checklist;
通過 checklist 進(jìn)行檢查,例如被讀取的 file path 是否在 denied path 列表中等。
在進(jìn)程的 proc 結(jié)構(gòu)中有一個(gè) p_ucred 成員用于存儲進(jìn)程的 Identifier (Process owner's identity. (PUCL)),它相當(dāng)于進(jìn)程的 Passport:
struct proc { LIST_ENTRY(proc) p_list; /* List of all processes. */ void * task; /* corresponding task (static)*/ struct proc *p_pptr; /* Pointer to parent process.(LL) */ pid_t p_ppid; // ... /* substructures: */ kauth_cred_t p_ucred; /* Process owner's identity. (PUCL) */
PUCL 是一個(gè) ucred 對象:
struct ucred { TAILQ_ENTRY(ucred) cr_link; /* never modify this without KAUTH_CRED_HASH_LOCK */ u_long cr_ref; /* reference count */ // .. struct label *cr_label; /* MAC label */
其中 cr_label
成員指向了存儲 MAC Policies 的數(shù)據(jù)結(jié)構(gòu) label
:
struct label { int l_flags; union { void *l_ptr; long l_long; } l_perpolicy[MAC_MAX_SLOTS]; };
l_perpolicy
數(shù)組記錄了 MAC Policy 列表,AMFI 和 Sandbox 的 Policy 都會(huì)插入到相應(yīng)進(jìn)程的 l_perpolicy
中。
根據(jù) Quarkslab Blogs 中的文章 Modern Jailbreaks' Post-Exploitation,AMFI 和 Sandbox 分別插入到了 0 和 1 位置[3]:
Each l_perpolicy "slot" is used by a particular MACF module, the first one being AMFI and the second one the sandbox. LiberiOS calls ShaiHulud2ProcessAtAddr to put 0 in its second label l_perpolicy[1]. Being the label used by the sandbox (processed in the function sb_evaluate), this move will neutralize it while keeping the label used by AMFI (Apple Mobile File Integrity) l_perpolicy[0] untouched (it's more precise and prevent useful entitlement loss).
即每個(gè) l_perpolicy
插槽都被用于特定的 MACF 模塊,第一個(gè)插槽被用于 AMFI,第二個(gè)被用于 Sandbox。LiberiOS 通過調(diào)用 ShaiHulud2ProcessAtAddr
在不修改第一個(gè)插槽的情況下將第二個(gè)插槽的指針置 0 來實(shí)現(xiàn)更加精準(zhǔn)和穩(wěn)定的沙盒逃逸。
有了 tfp0 和上面的理論基礎(chǔ),實(shí)現(xiàn)沙盒逃逸的路徑變得清晰了起來,我們只需要將當(dāng)前進(jìn)程的 l_perpolicy[1]
修改為 0,即可逃出沙盒。
首先讀取到當(dāng)前進(jìn)程的 label,路徑為 proc->p_ucred->cr_label
,隨后將索引為 1 的 Policy Slot 置 0:
#define KSTRUCT_OFFSET_PROC_UCRED 0xf8 #define KSTRUCT_OFFSET_UCRED_CR_LABEL 0x78 kptr_t swap_sandbox_for_proc(kptr_t proc, kptr_t sandbox) { kptr_t ret = KPTR_NULL; _assert(KERN_POINTER_VALID(proc)); kptr_t const ucred = ReadKernel64(proc + koffset(KSTRUCT_OFFSET_PROC_UCRED)); _assert(KERN_POINTER_VALID(ucred)); kptr_t const cr_label = ReadKernel64(ucred + koffset(KSTRUCT_OFFSET_UCRED_CR_LABEL)); _assert(KERN_POINTER_VALID(cr_label)); kptr_t const sandbox_addr = cr_label + 0x8 + 0x8; kptr_t const current_sandbox = ReadKernel64(sandbox_addr); _assert(WriteKernel64(sandbox_addr, sandbox)); ret = current_sandbox; out:; return ret; }
這里說明一下 sandbox_addr
的計(jì)算:
kptr_t const sandbox_addr = cr_label + 0x8 + 0x8;
我們再回顧下 label 結(jié)構(gòu)體:
struct label { int l_flags; union { void *l_ptr; long l_long; } l_perpolicy[MAC_MAX_SLOTS]; };
雖然 l_flags
本身只有 4 字節(jié),但 l_perpolicy
占據(jù)了 8n 字節(jié),為了按照最大成員對齊,l_flags
也會(huì)占據(jù) 8B,因此 cr_label + 8
指向了 l_perpolicy
,再偏移 8B 則指向 Sandbox 的 Policy Slot。
通過上述操作我們便能躲過 Sandbox.kext
對進(jìn)程的沙盒相關(guān)檢查,實(shí)現(xiàn)沙盒逃逸,接下來無論是通過 C 還是 OC 的 File API 都可以對 rootfs 進(jìn)行讀寫。在 Undecimus Jailbreak 中以這種方式讀取了 kernelcache 并確定 Kernel Slide 和關(guān)鍵偏移量。
我們可以通過簡單實(shí)驗(yàn)驗(yàn)證沙盒逃逸成功,下面的代碼讀取了 kernelcache 和 Applications 目錄:
NSArray *extractDir(NSString *dirpath) { NSError *error = nil; NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dirpath error:&error]; if (error) { NSLog(@"failed to get application list"); return nil; } return contents; } void sandbox_escape_test() { NSError *error = nil; BOOL success = [NSData dataWithContentsOfFile:@"/System/Library/Caches/com.apple.kernelcaches/kernelcache" options:NSDataReadingMappedAlways error:&error]; if (!success) { NSLog(@"error occurred !!! %@", error); } // list applications dir error = nil; NSFileManager *mgr = [NSFileManager defaultManager]; NSString *applicationRoot = @"/var/containers/Bundle/Application/"; NSArray *uuids = [mgr contentsOfDirectoryAtPath:applicationRoot error:&error]; if (error) { NSLog(@"failed to get application list"); return; } for (NSString *uuid in uuids) { NSString *appPath = [applicationRoot stringByAppendingPathComponent:uuid]; NSArray *contents = extractDir(appPath); for (NSString *content in contents) { if ([content hasSuffix:@".app"]) { NSLog(@"find %@ at %@ !!!", content, appPath); } } } }
感謝各位的閱讀,以上就是“Policy的內(nèi)核表示方法是什么”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Policy的內(nèi)核表示方法是什么這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!