Linux Kernel運(yùn)行時(shí)安全檢測(cè)之LKRG-實(shí)踐篇
掃描二維碼
隨時(shí)隨地手機(jī)看文章
一、背景
從文章Linux Kernel運(yùn)行時(shí)安全檢測(cè)之LKRG-原理篇可以看到,LKRG可以對(duì)正在運(yùn)行的Linux內(nèi)核進(jìn)行檢測(cè),并希望能夠及時(shí)響應(yīng)對(duì)正在運(yùn)行的進(jìn)程用戶id等憑證未經(jīng)授權(quán)的修改(完整性檢查)。對(duì)于進(jìn)程憑據(jù),LKRG嘗試檢測(cè)漏洞,并在內(nèi)核根據(jù)未經(jīng)授權(quán)的憑據(jù)授予訪問(wèn)權(quán)限(例如打開(kāi)文件)之前采取行動(dòng)。并且是以可加載的內(nèi)核模塊的形式,檢測(cè)正在運(yùn)行的內(nèi)核是否存在更改情況,以表明正在對(duì)其使用某種類(lèi)型的漏洞利用。除此之外,它還可以檢查系統(tǒng)上運(yùn)行的進(jìn)程,以查找對(duì)各種憑證的未經(jīng)授權(quán)修改,以防止這些更改授予額外的訪問(wèn)權(quán)限。
以上LKRG的特性,主要是為了保護(hù)運(yùn)行時(shí)內(nèi)核本身的完整性,但漏洞利用通常會(huì)針對(duì)系統(tǒng)上運(yùn)行的進(jìn)程,以提高特權(quán)等,這些信息保存在內(nèi)核的內(nèi)存中。因此LKRG還會(huì)跟蹤每個(gè)進(jìn)程的一系列不同屬性,并維護(hù)自己的任務(wù)列表,用于驗(yàn)證內(nèi)核的列表。如果兩個(gè)進(jìn)程發(fā)生分歧,則終止受影響的進(jìn)程,目的是在被漏洞利用差異之前進(jìn)行防御。
下面從具體實(shí)例的角度來(lái)具體分析LKRG,讓大家了解LKRG在內(nèi)核安全檢測(cè)方面所能實(shí)現(xiàn)的效果和其功能所體現(xiàn)出的價(jià)值。也是再次強(qiáng)調(diào),任何一項(xiàng)策略和方案,可以作為系統(tǒng)級(jí)別縱深防御策略的其中一道防線,但不是“一招勝天”的靈丹妙藥。
二、CVE-2017-1000112
(UDP路徑轉(zhuǎn)換異常,導(dǎo)致memory corruption)
內(nèi)核版本:Linux 4.12.6及以下存在此漏洞,內(nèi)核4.12.7版本上已修復(fù)UFO機(jī)制——UDP fragment offload
官方網(wǎng)站對(duì)這個(gè)漏洞的描述如下:
* 在Linux內(nèi)核中的UFO到Non-UFO的路徑轉(zhuǎn)換時(shí),存在異常內(nèi)存崩潰。在構(gòu)建一個(gè)UFO數(shù)據(jù)包時(shí),內(nèi)核會(huì)使用MSG_MORE __ip_append_data()函數(shù)來(lái)調(diào)用ip_ufo_append_data()并完成路徑的添加。
* 但是在這兩個(gè)send()調(diào)用的過(guò)程中,添加的路徑可以從UFO路徑轉(zhuǎn)換為非UFO路徑,而這將導(dǎo)致內(nèi)存崩潰的發(fā)生。為了防止UFO數(shù)據(jù)包長(zhǎng)度超過(guò)MTU,非UFO路徑的copy = maxfraglen – skb->len將會(huì)變成false,并分配新的skb。這將會(huì)出發(fā)程序計(jì)算fraggap = skb_prev->len – maxfraglen的值,并將copy = datalen – transhdrlen – fraggap設(shè)置為false。
Linux內(nèi)核UFO到非UFO路徑轉(zhuǎn)換時(shí)的內(nèi)存崩潰問(wèn)題,在構(gòu)建一個(gè)UFO數(shù)據(jù)包時(shí),內(nèi)核會(huì)使用MSG_MORE __ip_append_data()函數(shù)來(lái)調(diào)用ip_ufo_append_data()并完成路徑的添加。但是在這兩個(gè)send()調(diào)用的過(guò)程中,添加的路徑可以從UFO路徑轉(zhuǎn)換為非UFO路徑,而這將導(dǎo)致內(nèi)存崩潰的發(fā)生,這也是個(gè)Linux網(wǎng)絡(luò)子系統(tǒng)內(nèi)部漏洞的本地特權(quán)升級(jí)漏洞。
NIC (Network interface card) offload允許協(xié)議棧傳輸大于MTU(缺省為1500字節(jié))的報(bào)文。當(dāng)NIC offload時(shí),內(nèi)核將把多個(gè)數(shù)據(jù)包組裝成一個(gè)大數(shù)據(jù)包,并將其傳遞給硬件,由硬件處理IP碎片和分割成mtu大小的數(shù)據(jù)包。這種卸載通常用于高速網(wǎng)絡(luò)接口,以增加吞吐量,因?yàn)閁FO可以發(fā)送大的UDP包。
int s = socket(PF_INET, SOCK_DGRAM, 0); //創(chuàng)建UDP Socket if (s == -1) { perror("[-] socket()"); exit(EXIT_FAILURE); } struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(8000); addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); if (connect(s, (void*)&addr, sizeof(addr))) { perror("[-] connect()"); exit(EXIT_FAILURE); } int size = SHINFO_OFFSET + sizeof(struct skb_shared_info); int rv = send(s, buffer, size, MSG_MORE); //用MSG_MORE發(fā)送pocket,通知內(nèi)核我們稍后發(fā)送更多數(shù)據(jù) if (rv != size) { perror("[-] send()"); exit(EXIT_FAILURE); } int val = 1; rv = setsockopt(s, SOL_SOCKET, SO_NO_CHECK, &val, sizeof(val)); //關(guān)閉UDP checksum if (rv != 0) { perror("[-] setsockopt(SO_NO_CHECK)"); exit(EXIT_FAILURE); } send(s, buffer, 1, 0); //下一次發(fā)送Non-UFO數(shù)據(jù)將觸發(fā)異常 close(s);
以上部分實(shí)驗(yàn)代碼是說(shuō)明要在內(nèi)核中構(gòu)建UFO包,調(diào)用send/sendto/sendmsg時(shí)使用MSG_MORE標(biāo)志,它告訴內(nèi)核將這個(gè)套接字上的所有數(shù)據(jù)積累到single diagram中,以便在執(zhí)行沒(méi)有指定該標(biāo)志的調(diào)用時(shí)傳輸,然后觸發(fā)漏洞利用。
Linux內(nèi)核將信息包存儲(chǔ)在結(jié)構(gòu)sk_buff (socket緩沖區(qū))中,所有網(wǎng)絡(luò)層都使用該結(jié)構(gòu)存儲(chǔ)信息包的頭部、關(guān)于用戶數(shù)據(jù)的信息(負(fù)載)和其他內(nèi)部信息。
如上圖所示和poc.c可以看到,當(dāng)使用MSG_MORE標(biāo)志進(jìn)行第一個(gè)發(fā)送調(diào)用時(shí),__ip_append_data takes通過(guò)調(diào)用ip_ufo_append_data創(chuàng)建一個(gè)新的套接字緩沖區(qū),在循環(huán)的第一次迭代中,copy的值變?yōu)樨?fù)值,這將觸發(fā)新的套接字緩沖區(qū)分配。加上fraggap計(jì)算超過(guò)MTU而觸發(fā)分片,這將導(dǎo)致使用skb_copy_and_csum_bits函數(shù)從第一次發(fā)送調(diào)用創(chuàng)建的sk_buff中復(fù)制用戶負(fù)載到新分配的sk_buff。從源緩沖區(qū)復(fù)制指定數(shù)量的字節(jié)到目標(biāo)sk_buff,并計(jì)算校驗(yàn)。如果調(diào)用skb_copy_and_csum_bits的長(zhǎng)度大于新創(chuàng)建的sk_buff邊界結(jié)束限制,則會(huì)覆蓋套接字緩沖區(qū)之外的數(shù)據(jù),并破壞緊接在sk_buff前面的skb_shared_info結(jié)構(gòu)。
test@ubuntu:~/CVE-2017-1000112$ gcc poc.c -o poctest@ubuntu:~/CVE-2017-1000112$ iduid=1000(test) gid=1000(test) groups=1000(test),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),110(lxd),115(lpadmin),116(sambashare) context=system_u:system_r:kernel_t:s0test@ubuntu:~/CVE-2017-1000112$ ./poc[.] starting[.] checking distro and kernel versions[.] kernel version '4.8.0-52-generic' detected[~] done, versions looks good[.] checking SMEP and SMAP[~] done, looks good[.] setting up namespace sandbox[~] done, namespace sandbox set up[.] KASLR bypass enabled, getting kernel addr[~] done, kernel text: ffffffff82a00000[.] commit_creds: ffffffff82aa5d00[.] prepare_kernel_cred: ffffffff82aa60f0[.] SMEP bypass enabled, mmapping fake stack[~] done, fake stack mmapped[.] executing payload ffffffff82a17c55[~] done, should be root now[.] checking if we got root[+] got r00t ^_^root@ubuntu:/home/test/CVE-2017-1000112# iduid=0(root) gid=0(root) groups=0(root) context=system_u:system_r:kernel_t:s0root@ubuntu:/home/test/CVE-2017-1000112# exitexittest@ubuntu:~/CVE-2017-1000112$
以上結(jié)果顯示執(zhí)行成功,也就是漏洞利用成功,這類(lèi)問(wèn)題核心在于覆寫(xiě)了cred/read_cred結(jié)構(gòu)體。針對(duì)于以上漏洞利用的特點(diǎn),可以利用LKRG的pCFI來(lái)檢測(cè)相關(guān)數(shù)據(jù)完整性以及檢測(cè)覆寫(xiě)cred/read_cred結(jié)構(gòu)體的功能,檢測(cè)SEMP、SMAP、KALSR等的修改,來(lái)實(shí)現(xiàn)對(duì)此類(lèi)問(wèn)的防御,下面為部分檢測(cè)代碼,全部代碼在:https://github.com/lkrg-org/lkrg/tree/main/src/modules/exploit_detection/syscalls/pCFI和p_exploit_detection.c
//創(chuàng)建creds Hookstatic const struct p_functions_hooks { const char *name; int (*install)(int p_isra); void (*uninstall)(void); int p_fatal; const char *p_error_message; int is_isra_safe; } p_functions_hooks_array[] = { { "security_bprm_committing_creds", p_install_security_bprm_committing_creds_hook, p_uninstall_security_bprm_committing_creds_hook, 1, NULL, 1 -...... // dump creds并對(duì)比結(jié)果notrace void p_dump_creds(struct p_cred *p_where, const struct cred *p_from) { /* Get reference to cred */ get_cred(p_from); /* Track process's capabilities */ memcpy(&p_where->cap_inheritable, &p_from->cap_inheritable, sizeof(kernel_cap_t)); memcpy(&p_where->cap_permitted, &p_from->cap_permitted, sizeof(kernel_cap_t)); memcpy(&p_where->cap_effective, &p_from->cap_effective, sizeof(kernel_cap_t)); memcpy(&p_where->cap_bset, &p_from->cap_bset, sizeof(kernel_cap_t));#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0) memcpy(&p_where->cap_ambient, &p_from->cap_ambient, sizeof(kernel_cap_t));#endif /* Track process's IDs */ p_set_uid(&p_where->uid, p_get_uid(&p_from->uid)); p_set_gid(&p_where->gid, p_get_gid(&p_from->gid)); p_set_uid(&p_where->suid, p_get_uid(&p_from->suid)); p_set_gid(&p_where->sgid, p_get_gid(&p_from->sgid)); p_set_uid(&p_where->euid, p_get_uid(&p_from->euid)); p_set_gid(&p_where->egid, p_get_gid(&p_from->egid)); p_set_uid(&p_where->fsuid, p_get_uid(&p_from->fsuid)); p_set_gid(&p_where->fsgid, p_get_gid(&p_from->fsgid)); /* Track process's securebits - TODO: research */ p_where->securebits = p_from->securebits; /* Track process's critical pointers */ p_where->user = p_from->user; p_where->user_ns = p_from->user_ns; /* Release reference to cred */ put_cred(p_from);} },/
當(dāng)在此版本內(nèi)核中加載LKRG后,再次執(zhí)行PoC,可以看到該漏洞利用已經(jīng)被檢測(cè)并阻斷,從kernel log中可以清晰看到“Detected pointer swapping attack!process[2399 | poc] has different 'cred' pointer [0xffff8d1bab32ed80 vs 0xffff8d1bb1a50180]”:
test@ubuntu:~/CVE-2017-1000112$ iduid=1000(test) gid=1000(test) groups=1000(test),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),110(lxd),115(lpadmin),116(sambashare) context=system_u:system_r:kernel_t:s0test@ubuntu:~/CVE-2017-1000112$ ./poc[.] starting[.] checking distro and kernel versions[.] kernel version '4.8.0-52-generic' detected[~] done, versions looks good[.] checking SMEP and SMAP[~] done, looks good[.] setting up namespace sandbox[~] done, namespace sandbox set up[.] KASLR bypass enabled, getting kernel addr[~] done, kernel text: ffffffff82a00000[.] commit_creds: ffffffff82aa5d00[.] prepare_kernel_cred: ffffffff82aa60f0[.] SMEP bypass enabled, mmapping fake stack[~] done, fake stack mmapped[.] executing payload ffffffff82a17c55[~] done, should be root now[.] checking if we got rootKilledtest@ubuntu:~/CVE-2017-1000112$ iduid=1000(test) gid=1000(test) groups=1000(test),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),110(lxd),115(lpadmin),116(sambashare) context=system_u:system_r:kernel_t:s0test@ubuntu:~/CVE-2017-1000112$
ubuntu kernel: [ 228.864932] [p_lkrg] Loading LKRG...ubuntu kernel: [ 229.159677] [p_lkrg] LKRG initialized successfully!ubuntu kernel: [ 235.372066] [p_lkrg]Detected pointer swapping attack!process[2399 | poc] has different 'cred' pointer [0xffff8d1bab32ed80 vs 0xffff8d1bb1a50180]ubuntu kernel: [ 235.372104] [p_lkrg]Detected pointer swapping attack!process[2399 | poc] has different 'real_cred' pointer [0xffff8d1bab32ed80 vs 0xffff8d1bb1a50180]ubuntu kernel: [ 235.372137] [p_lkrg]process[2399 | poc] has different UID! 1000 vs 0ubuntu kernel: [ 235.372155] [p_lkrg]process[2399 | poc] has different EUID! 1000 vs 0ubuntu kernel: [ 235.372174] [p_lkrg]process[2399 | poc] has different SUID! 1000 vs 0ubuntu kernel: [ 235.372192] [p_lkrg]process[2399 | poc] has different FSUID! 1000 vs 0ubuntu kernel: [ 235.372210] [p_lkrg]process[2399 | poc] has different GID! 1000 vs 0ubuntu kernel: [ 235.372228] [p_lkrg]process[2399 | poc] has different EGID! 1000 vs 0ubuntu kernel: [ 235.372247] [p_lkrg]process[2399 | poc] has different SGID! 1000 vs 0ubuntu kernel: [ 235.372265] [p_lkrg]process[2399 | poc] has different FSGID! 1000 vs 0ubuntu kernel: [ 235.372284] [p_lkrg]Trying to kill process[poc | 2399]!
三、結(jié)論
從上述例子可以看到,LKRG在非法提權(quán)中的覆寫(xiě)cred/read_cred結(jié)構(gòu)體這個(gè)類(lèi)型的漏洞利用上,起到了關(guān)鍵的檢測(cè)和阻斷作用。當(dāng)然,如上篇所說(shuō),可以通過(guò)一些方法來(lái)繞過(guò)LKRG,但復(fù)雜度和難度會(huì)成倍的增加,這也就是為什么會(huì)一直強(qiáng)調(diào),任何一項(xiàng)策略和方案,可以作為系統(tǒng)級(jí)別縱深防御策略的其中一道防線,但不是“一招勝天”的靈丹妙藥,只有構(gòu)建多層級(jí)防御矩陣,以及多個(gè)角度來(lái)解析安全問(wèn)題,才會(huì)達(dá)到更好的效果。