int main (int argc, char **argv, char **envp, char **apple)
{
for (int i=0; apple[i]; i++)
printf("%d: %s\n", i, apple[i])
}
```
## macOS Dynamic Linker (dyld) Process
### macOS Library Injection
Library injection is a technique used to force a process to load a dynamic library. This can be achieved by manipulating the `DYLD_INSERT_LIBRARIES` environment variable or using tools like `insert_dylib` to inject a library into a process. Once the library is injected, it can intercept function calls, manipulate process behavior, and potentially escalate privileges. This technique is commonly used in malware and rootkit development.
### macOS Process Abuse
Process abuse involves manipulating processes to achieve malicious goals. This can include injecting code into a process, hijacking process execution flow, or abusing legitimate processes to gain unauthorized access or escalate privileges. Process abuse techniques are often used in privilege escalation attacks and malware development.
### macOS Library Injection Countermeasures
To defend against library injection attacks, macOS users and administrators can implement the following countermeasures:
- **Monitor Process Activity**: Regularly monitor process activity for suspicious behavior, such as unexpected libraries being loaded.
- **Restrict Library Loading**: Use tools like `csrutil` to restrict the loading of unsigned or unauthorized libraries.
- **Implement Code Signing**: Enforce code signing requirements to ensure that only trusted libraries are loaded into processes.
- **Update Security Patches**: Keep macOS systems up to date with the latest security patches to mitigate known vulnerabilities that could be exploited for library injection.
```
0: executable_path=./a
1:
2:
3:
4: ptr_munge=
5: main_stack=
6: executable_file=0x1a01000012,0x5105b6a
7: dyld_file=0x1a01000012,0xfffffff0009834a
8: executable_cdhash=757a1b08ab1a79c50a66610f3adbca86dfd3199b
9: executable_boothash=f32448504e788a2c5935e372d22b7b18372aa5aa
10: arm64e_abi=os
11: th_port=
```
{% hint style="success" %}
Quando esses valores chegam à função principal, as informações sensíveis já foram removidas deles ou teria ocorrido um vazamento de dados.
{% endhint %}
é possível ver todos esses valores interessantes depurando antes de entrar na função principal com:
lldb ./apple
(lldb) target create "./a"
Executable atual definido como '/tmp/a' (arm64).
(lldb) process launch -s
[..]
(lldb) mem read $sp
0x16fdff510: 00 00 00 00 01 00 00 00 01 00 00 00 00 00 00 00 ................
0x16fdff520: d8 f6 df 6f 01 00 00 00 00 00 00 00 00 00 00 00 ...o............
(lldb) x/55s 0x016fdff6d8
[...]
0x16fdffd6a: "TERM_PROGRAM=WarpTerminal"
0x16fdffd84: "WARP_USE_SSH_WRAPPER=1"
0x16fdffd9b: "WARP_IS_LOCAL_SHELL_SESSION=1"
0x16fdffdb9: "SDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.4.sdk"
0x16fdffe24: "NVM_DIR=/Users/carlospolop/.nvm"
0x16fdffe44: "CONDA_CHANGEPS1=false"
0x16fdffe5a: ""
0x16fdffe5b: ""
0x16fdffe5c: ""
0x16fdffe5d: ""
0x16fdffe5e: ""
0x16fdffe5f: ""
0x16fdffe60: "pfz=0xffeaf0000"
0x16fdffe70: "stack_guard=0x8af2b510e6b800b5"
0x16fdffe8f: "malloc_entropy=0xf2349fbdea53f1e4,0x3fd85d7dcf817101"
0x16fdffec4: "ptr_munge=0x983e2eebd2f3e746"
0x16fdffee1: "main_stack=0x16fe00000,0x7fc000,0x16be00000,0x4000000"
0x16fdfff17: "executable_file=0x1a01000012,0x5105b6a"
0x16fdfff3e: "dyld_file=0x1a01000012,0xfffffff0009834a"
0x16fdfff67: "executable_cdhash=757a1b08ab1a79c50a66610f3adbca86dfd3199b"
0x16fdfffa2: "executable_boothash=f32448504e788a2c5935e372d22b7b18372aa5aa"
0x16fdfffdf: "arm64e_abi=os"
0x16fdfffed: "th_port=0x103"
0x16fdffffb: ""
## dyld\_all\_image\_infos
Esta é uma estrutura exportada pelo dyld com informações sobre o estado do dyld que podem ser encontradas no [**código-fonte**](https://opensource.apple.com/source/dyld/dyld-852.2/include/mach-o/dyld\_images.h.auto.html) com informações como a versão, ponteiro para o array dyld\_image\_info, para dyld\_image\_notifier, se o proc está desanexado do cache compartilhado, se o inicializador do libSystem foi chamado, ponteiro para o próprio cabeçalho Mach do dyld, ponteiro para a string de versão do dyld...
## Variáveis de ambiente dyld
### debug dyld
Variáveis de ambiente interessantes que ajudam a entender o que o dyld está fazendo:
* **DYLD\_PRINT\_LIBRARIES**
Verifica cada biblioteca que é carregada:
```
DYLD_PRINT_LIBRARIES=1 ./apple
dyld[19948]: <9F848759-9AB8-3BD2-96A1-C069DC1FFD43> /private/tmp/a
dyld[19948]: /usr/lib/libSystem.B.dylib
dyld[19948]: /usr/lib/system/libcache.dylib
dyld[19948]: /usr/lib/system/libcommonCrypto.dylib
dyld[19948]: /usr/lib/system/libcompiler_rt.dylib
dyld[19948]: <65612C42-C5E4-3821-B71D-DDE620FB014C> /usr/lib/system/libcopyfile.dylib
dyld[19948]: /usr/lib/system/libcorecrypto.dylib
dyld[19948]: <8790BA20-19EC-3A36-8975-E34382D9747C> /usr/lib/system/libdispatch.dylib
dyld[19948]: <4BB77515-DBA8-3EDF-9AF7-3C9EAE959EA6> /usr/lib/system/libdyld.dylib
dyld[19948]: /usr/lib/system/libkeymgr.dylib
dyld[19948]: <1A7038EC-EE49-35AE-8A3C-C311083795FB> /usr/lib/system/libmacho.dylib
[...]
```
* **DYLD\_PRINT\_SEGMENTS**
Verifique como cada biblioteca é carregada:
```
DYLD_PRINT_SEGMENTS=1 ./apple
dyld[21147]: re-using existing shared cache (/System/Volumes/Preboot/Cryptexes/OS/System/Library/dyld/dyld_shared_cache_arm64e):
dyld[21147]: 0x181944000->0x1D5D4BFFF init=5, max=5 __TEXT
dyld[21147]: 0x1D5D4C000->0x1D5EC3FFF init=1, max=3 __DATA_CONST
dyld[21147]: 0x1D7EC4000->0x1D8E23FFF init=3, max=3 __DATA
dyld[21147]: 0x1D8E24000->0x1DCEBFFFF init=3, max=3 __AUTH
dyld[21147]: 0x1DCEC0000->0x1E22BFFFF init=1, max=3 __AUTH_CONST
dyld[21147]: 0x1E42C0000->0x1E5457FFF init=1, max=1 __LINKEDIT
dyld[21147]: 0x1E5458000->0x22D173FFF init=5, max=5 __TEXT
dyld[21147]: 0x22D174000->0x22D9E3FFF init=1, max=3 __DATA_CONST
dyld[21147]: 0x22F9E4000->0x230F87FFF init=3, max=3 __DATA
dyld[21147]: 0x230F88000->0x234EC3FFF init=3, max=3 __AUTH
dyld[21147]: 0x234EC4000->0x237573FFF init=1, max=3 __AUTH_CONST
dyld[21147]: 0x239574000->0x270BE3FFF init=1, max=1 __LINKEDIT
dyld[21147]: Kernel mapped /private/tmp/a
dyld[21147]: __PAGEZERO (...) 0x000000904000->0x000101208000
dyld[21147]: __TEXT (r.x) 0x000100904000->0x000100908000
dyld[21147]: __DATA_CONST (rw.) 0x000100908000->0x00010090C000
dyld[21147]: __LINKEDIT (r..) 0x00010090C000->0x000100910000
dyld[21147]: Using mapping in dyld cache for /usr/lib/libSystem.B.dylib
dyld[21147]: __TEXT (r.x) 0x00018E59D000->0x00018E59F000
dyld[21147]: __DATA_CONST (rw.) 0x0001D5DFDB98->0x0001D5DFDBA8
dyld[21147]: __AUTH_CONST (rw.) 0x0001DDE015A8->0x0001DDE01878
dyld[21147]: __AUTH (rw.) 0x0001D9688650->0x0001D9688658
dyld[21147]: __DATA (rw.) 0x0001D808AD60->0x0001D808AD68
dyld[21147]: __LINKEDIT (r..) 0x000239574000->0x000270BE4000
dyld[21147]: Using mapping in dyld cache for /usr/lib/system/libcache.dylib
dyld[21147]: __TEXT (r.x) 0x00018E597000->0x00018E59D000
dyld[21147]: __DATA_CONST (rw.) 0x0001D5DFDAF0->0x0001D5DFDB98
dyld[21147]: __AUTH_CONST (rw.) 0x0001DDE014D0->0x0001DDE015A8
dyld[21147]: __LINKEDIT (r..) 0x000239574000->0x000270BE4000
[...]
```
* **DYLD\_PRINT\_INITIALIZERS**
Imprime quando cada inicializador de biblioteca está sendo executado:
```
DYLD_PRINT_INITIALIZERS=1 ./apple
dyld[21623]: running initializer 0x18e59e5c0 in /usr/lib/libSystem.B.dylib
[...]
```
### Outros
* `DYLD_BIND_AT_LAUNCH`: As ligações preguiçosas são resolvidas com as não preguiçosas
* `DYLD_DISABLE_PREFETCH`: Desativar pré-busca de conteúdo \_\_DATA e \_\_LINKEDIT
* `DYLD_FORCE_FLAT_NAMESPACE`: Ligações de um único nível
* `DYLD_[FRAMEWORK/LIBRARY]_PATH | DYLD_FALLBACK_[FRAMEWORK/LIBRARY]_PATH | DYLD_VERSIONED_[FRAMEWORK/LIBRARY]_PATH`: Caminhos de resolução
* `DYLD_INSERT_LIBRARIES`: Carregar uma biblioteca específica
* `DYLD_PRINT_TO_FILE`: Escrever debug do dyld em um arquivo
* `DYLD_PRINT_APIS`: Imprimir chamadas de API do libdyld
* `DYLD_PRINT_APIS_APP`: Imprimir chamadas de API do libdyld feitas pelo main
* `DYLD_PRINT_BINDINGS`: Imprimir símbolos quando vinculados
* `DYLD_WEAK_BINDINGS`: Apenas imprimir símbolos fracos quando vinculados
* `DYLD_PRINT_CODE_SIGNATURES`: Imprimir operações de registro de assinatura de código
* `DYLD_PRINT_DOFS`: Imprimir seções de formato de objeto D-Trace carregadas
* `DYLD_PRINT_ENV`: Imprimir env visto pelo dyld
* `DYLD_PRINT_INTERPOSTING`: Imprimir operações de interposição
* `DYLD_PRINT_LIBRARIES`: Imprimir bibliotecas carregadas
* `DYLD_PRINT_OPTS`: Imprimir opções de carregamento
* `DYLD_REBASING`: Imprimir operações de rebase de símbolos
* `DYLD_RPATHS`: Imprimir expansões de @rpath
* `DYLD_PRINT_SEGMENTS`: Imprimir mapeamentos de segmentos Mach-O
* `DYLD_PRINT_STATISTICS`: Imprimir estatísticas de tempo
* `DYLD_PRINT_STATISTICS_DETAILS`: Imprimir estatísticas de tempo detalhadas
* `DYLD_PRINT_WARNINGS`: Imprimir mensagens de aviso
* `DYLD_SHARED_CACHE_DIR`: Caminho a ser usado para o cache de biblioteca compartilhada
* `DYLD_SHARED_REGION`: "use", "private", "avoid"
* `DYLD_USE_CLOSURES`: Habilitar closures
É possível encontrar mais com algo como:
```bash
strings /usr/lib/dyld | grep "^DYLD_" | sort -u
```
Ou baixando o projeto dyld de [https://opensource.apple.com/tarballs/dyld/dyld-852.2.tar.gz](https://opensource.apple.com/tarballs/dyld/dyld-852.2.tar.gz) e executando dentro da pasta:
```bash
find . -type f | xargs grep strcmp| grep key,\ \" | cut -d'"' -f2 | sort -u
```
## Referências
* [**\*OS Internals, Volume I: User Mode. Por Jonathan Levin**](https://www.amazon.com/MacOS-iOS-Internals-User-Mode/dp/099105556X)
Aprenda hacking AWS do zero ao herói com htARTE (HackTricks AWS Red Team Expert)!
Outras maneiras de apoiar o HackTricks:
* Se você deseja ver sua **empresa anunciada no HackTricks** ou **baixar o HackTricks em PDF** Confira os [**PLANOS DE ASSINATURA**](https://github.com/sponsors/carlospolop)!
* Adquira o [**swag oficial do PEASS & HackTricks**](https://peass.creator-spring.com)
* Descubra [**A Família PEASS**](https://opensea.io/collection/the-peass-family), nossa coleção exclusiva de [**NFTs**](https://opensea.io/collection/the-peass-family)
* **Junte-se ao** 💬 [**grupo Discord**](https://discord.gg/hRep4RUj7f) ou ao [**grupo telegram**](https://t.me/peass) ou **siga-nos** no **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks\_live)**.**
* **Compartilhe seus truques de hacking enviando PRs para os** [**HackTricks**](https://github.com/carlospolop/hacktricks) e [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) repositórios do github.