??出于應(yīng)用安全防護(hù)的目的贡蓖,安全開(kāi)發(fā)人員為防止函數(shù)被HOOK(inlinehook/fishhook)遂黍,一般會(huì)對(duì)敏感函數(shù)做SVC調(diào)用娃兽,如access/stat/open等函數(shù)荔泳。做SVC調(diào)用的方式明顯由于直接調(diào)用和動(dòng)態(tài)dlsym調(diào)用红伦,隱蔽性得到增強(qiáng)激况,且Hook難度增加作彤。具體實(shí)施形式可分為為裸函數(shù)或內(nèi)聯(lián)匯編。裸函數(shù)方式乌逐,即為一個(gè)獨(dú)立函數(shù)竭讳,其他函數(shù)通過(guò)傳遞系統(tǒng)調(diào)用號(hào)和參數(shù)調(diào)用該函數(shù)內(nèi)聯(lián)匯編方式,會(huì)將SVC內(nèi)聯(lián)到每個(gè)調(diào)用函數(shù)中浙踢。這兩者相比較绢慢,各有優(yōu)勢(shì)。裸函數(shù)方式洛波,如果攻擊者對(duì)此函數(shù)進(jìn)行hook胰舆,可以很方便的通過(guò)內(nèi)存校驗(yàn)檢測(cè)出問(wèn)題(如果直接用變量訪問(wèn)這段內(nèi)存骚露,也是很容易逆向分析,因此實(shí)際操作時(shí)可以使用一些間接引用技巧)思瘟。但無(wú)論如何荸百,因?yàn)閕OS上非越獄環(huán)境無(wú)法動(dòng)態(tài)創(chuàng)建并執(zhí)行代碼,因此SVC這條匯編指令一定是存在于內(nèi)存中滨攻,最常見(jiàn)的就是svc 0x80
和svc 0x50
(注意這個(gè)指令的立即數(shù)是默認(rèn)忽略的够话,因此svc 0
或者svc 0xffff
效果都是一樣的),對(duì)應(yīng)機(jī)器碼為01 10 00 D4
光绕,通常在逆向分析時(shí)女嘲,直接搜此二進(jìn)制即可直達(dá)檢測(cè)的關(guān)鍵處。為了對(duì)抗svc指令的檢測(cè)和修改诞帐,緩解的方式也很多欣尼,比如將svc藏在特殊segment中,或者構(gòu)造出包含01 10 00 D4
的無(wú)效指令機(jī)器碼來(lái)進(jìn)行混淆停蕉。本文提出一種方式愕鼓,可以更好的隱藏svc調(diào)用。
??libsystem_kernel.dylib
是具體實(shí)現(xiàn)系統(tǒng)調(diào)用的動(dòng)態(tài)庫(kù)慧起,通過(guò)逆向可以發(fā)現(xiàn)其包含大量的svc指令菇晃,那取巧的辦法就是獲取到這里svc的地址,并包裝成函數(shù)進(jìn)行調(diào)用蚓挤,即可神不知鬼不覺(jué)達(dá)到隱藏svc并進(jìn)行系統(tǒng)調(diào)用的目的磺送。
__text:00000002382DA4AC EXPORT ___gettimeofday
__text:00000002382DA4AC ___gettimeofday ; CODE XREF: _prepare_times_array_and_attrs+74↑p
__text:00000002382DA4AC
__text:00000002382DA4AC var_10 = -0x10
__text:00000002382DA4AC
__text:00000002382DA4AC 02 00 80 D2 MOV X2, #0
__text:00000002382DA4B0 90 0E 80 D2 MOV X16, #0x74
__text:00000002382DA4B4 01 10 00 D4 SVC 0x80
__text:00000002382DA4B8 C3 00 00 54 B.CC locret_2382DA4D0
__text:00000002382DA4BC FD 7B BF A9 STP X29, X30, [SP,#var_10]!
__text:00000002382DA4C0 FD 03 00 91 MOV X29, SP
__text:00000002382DA4C4 5E D9 FF 97 BL _cerror_nocancel
__text:00000002382DA4C8 BF 03 00 91 MOV SP, X29
__text:00000002382DA4CC FD 7B C1 A8 LDP X29, X30, [SP+0x10+var_10],#0x10
__text:00000002382DA4D0
__text:00000002382DA4D0 locret_2382DA4D0 ; CODE XREF: ___gettimeofday+C↑j
__text:00000002382DA4D0 C0 03 5F D6 RET
??arm64上具體實(shí)現(xiàn)代碼如下,此代碼通過(guò)獲取libsystem_kernel模塊地址灿意,并爆搜指令的方式獲取svc估灿,并實(shí)現(xiàn)裸函數(shù)和內(nèi)聯(lián)函數(shù)兩種調(diào)用方式,通過(guò)逆向可以發(fā)現(xiàn)缤剧,程序中并沒(méi)有svc指令馅袁,完美達(dá)到目的!:
intptr_t get_text_range(void* base, intptr_t off, uint32_t* psize) {
mach_header_t* pmh = (mach_header_t*)base;
load_command* plc = (load_command*)(pmh + 1);
for (int i = 0; i < pmh->ncmds; i++) {
if (plc->cmd != LC_SEGMENT_CURARCH) {
plc = (struct load_command*)((unsigned char*)plc + plc->cmdsize);
continue;
}
segment_command_t* psc = (segment_command_t*)plc;
if (0 == strcmp(psc->segname, "__TEXT")) {
if (psize) *psize = (uint32_t)psc->vmsize;
return (intptr_t)(psc->vmaddr + off);
}
plc = (struct load_command*)((unsigned char*)plc + plc->cmdsize);
}
return -1;
}
static void* find_svc() {
for (int i = 0; i < _dyld_image_count(); i++) {
const char* path = _dyld_get_image_name(i);
void* base = (void*)_dyld_get_image_header(i);
intptr_t slide = _dyld_get_image_vmaddr_slide(i);
if (0 != strcmp(path, "/usr/lib/system/libsystem_kernel.dylib")) {
continue;
}
NSLog(@"%s", path);
intptr_t text_base = 0;
uint32_t text_size = 0;
text_base = get_text_range(base, slide, &text_size);
for (int i = 0; i < text_size / 4; i += 4) {
uint32_t* addr = (uint32_t*)(text_base + i * 4);
if (*addr == 0xd4001001) { // for arm64
return (void*)addr;
}
}
}
return 0;
}
static void* svc_addr = 0;
#define naked __attribute__((naked))
#define finline __inline__ __attribute__((always_inline))
naked int asm_getpid1() {
__asm("mov x16, 0x14 \n"
"mov x8, %[svc_addr] \n"
"br x8 \n"
::[svc_addr]"r"(svc_addr):
);
}
finline int asm_getpid2() {
int ret = 0;
__asm("mov x16, 0x14 \n"
"mov x8, %[svc_addr] \n"
"blr x8 \n"
"mov %[ret], x0 \n"
:[ret]"=r"(ret):[svc_addr]"r"(svc_addr):
);
return ret;
}
int main(int argc, char * argv[]) {
svc_addr = find_svc();
NSLog(@"asm_pid1=%d, asm_pid2=%d, real_pid=%d", asm_getpid1(), asm_getpid2(), getpid());
NSString * appDelegateClassName;
@autoreleasepool {
appDelegateClassName = NSStringFromClass([AppDelegate class]);
}
return UIApplicationMain(argc, argv, nil, appDelegateClassName);
}
??需要注意的是上述代碼沒(méi)有進(jìn)行錯(cuò)誤處理鞭执,這個(gè)實(shí)現(xiàn)是通過(guò)BCC指令司顿,具體可以參照l(shuí)ibsystem_kernel的反匯編。