macOS ์ปค๋„ ํ™•์žฅ(Kernel Extensions) & Kernelcaches

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 ์ง€์›ํ•˜๊ธฐ

๊ธฐ๋ณธ ์ •๋ณด

Kernel extensions (Kexts)๋Š” .kext ํ™•์žฅ์ž๋ฅผ ๊ฐ€์ง„ ํŒจํ‚ค์ง€๋กœ, macOS ์ปค๋„ ๊ณต๊ฐ„์— ์ง์ ‘ ๋กœ๋“œ๋˜์–ด ์šด์˜ ์ฒด์ œ์— ์ถ”๊ฐ€ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

์ง€์› ์ค‘๋‹จ ์ƒํƒœ & DriverKit / System Extensions

macOS Catalina (10.15)๋ถ€ํ„ฐ Apple์€ ๋Œ€๋ถ€๋ถ„์˜ ๋ ˆ๊ฑฐ์‹œ KPI๋ฅผ deprecated๋กœ ํ‘œ์‹œํ•˜๊ณ  user-space์—์„œ ์‹คํ–‰๋˜๋Š” System Extensions & DriverKit ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ๋„์ž…ํ–ˆ์Šต๋‹ˆ๋‹ค. macOS Big Sur (11)๋ถ€ํ„ฐ ์šด์˜ ์ฒด์ œ๋Š” Reduced Security ๋ชจ๋“œ๋กœ ๋ถ€ํŒ…๋˜์ง€ ์•Š์€ ํ•œ deprecated KPI์— ์˜์กดํ•˜๋Š” ํƒ€์‚ฌ kext์˜ ๋กœ๋“œ๋ฅผ ๊ฑฐ๋ถ€ํ•ฉ๋‹ˆ๋‹ค. Apple Silicon์—์„œ๋Š” kext๋ฅผ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด ์ถ”๊ฐ€๋กœ ์‚ฌ์šฉ์ž๊ฐ€ ๋‹ค์Œ์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค:

  1. Reboot into Recovery โ†’ Startup Security Utility.
  2. Select Reduced Security and tick โ€œAllow user management of kernel extensions from identified developersโ€.
  3. Reboot and approve the kext from System Settings โ†’ Privacy & Security.

DriverKit/System Extensions๋กœ ์ž‘์„ฑ๋œ user-land ๋“œ๋ผ์ด๋ฒ„๋Š” ์ถฉ๋Œ์ด๋‚˜ ๋ฉ”๋ชจ๋ฆฌ ์†์ƒ์ด ์ปค๋„ ๊ณต๊ฐ„์ด ์•„๋‹Œ ์ƒŒ๋“œ๋ฐ•์Šค๋œ ํ”„๋กœ์„ธ์Šค์— ๊ตญํ•œ๋˜๋ฏ€๋กœ ๊ณต๊ฒฉ ํ‘œ๋ฉด์„ ํฌ๊ฒŒ ๊ฐ์†Œ์‹œํ‚ต๋‹ˆ๋‹ค.

๐Ÿ“ macOS Sequoia (15)๋ถ€ํ„ฐ Apple์€ ๋ช‡๋ช‡ ๋ ˆ๊ฑฐ์‹œ ๋„คํŠธ์›Œํ‚น ๋ฐ USB KPI๋ฅผ ์™„์ „ํžˆ ์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค โ€“ ๋ฒค๋”๊ฐ€ ์•ž์œผ๋กœ ํ˜ธํ™˜์„ฑ์„ ์œ ์ง€ํ•˜๋ ค๋ฉด System Extensions๋กœ ์ด์ „ํ•˜๋Š” ๊ฒƒ์ด ์œ ์ผํ•œ ํ•ด๊ฒฐ์ฑ…์ž…๋‹ˆ๋‹ค.

์š”๊ตฌ์‚ฌํ•ญ

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

  • ๋ณต๊ตฌ ๋ชจ๋“œ๋กœ ์ง„์ž…ํ•  ๋•Œ, ์ปค๋„ ํ™•์žฅ์ด ๋กœ๋“œ๋˜๋„๋ก ํ—ˆ์šฉ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค:
  • ์ปค๋„ ํ™•์žฅ์€ kernel code signing certificate๋กœ ์„œ๋ช…๋˜์–ด์•ผ ํ•˜๋ฉฐ, ์ด๋Š” Apple๋งŒ ๋ฐœ๊ธ‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Apple์€ ํšŒ์‚ฌ์™€ ํ•„์š”ํ•œ ์ด์œ ๋ฅผ ์ƒ์„ธํžˆ ๊ฒ€ํ† ํ•ฉ๋‹ˆ๋‹ค.
  • ์ปค๋„ ํ™•์žฅ์€ ๋˜ํ•œ notarized๋˜์–ด์•ผ ํ•˜๋ฉฐ, Apple์€ ์ด๋ฅผ ์•…์„ฑ์ฝ”๋“œ ์—ฌ๋ถ€ ํ™•์ธ์— ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ปค๋„ ํ™•์žฅ์„ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ์€ root ์‚ฌ์šฉ์ž์—๊ฒŒ ์žˆ์œผ๋ฉฐ, ํŒจํ‚ค์ง€ ๋‚ด๋ถ€์˜ ํŒŒ์ผ๋“ค์€ root ์†Œ์œ ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ์—…๋กœ๋“œ ๊ณผ์ •์—์„œ๋Š” ํŒจํ‚ค์ง€๊ฐ€ ๋ณดํ˜ธ๋œ non-root ์œ„์น˜์— ์ค€๋น„๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: /Library/StagedExtensions (requires the com.apple.rootless.storage.KernelExtensionManagement grant).
  • ๋งˆ์ง€๋ง‰์œผ๋กœ, ๋กœ๋“œ๋ฅผ ์‹œ๋„ํ•  ๋•Œ ์‚ฌ์šฉ์ž๋Š” receive a confirmation request๋ฅผ ๋ฐ›๊ณ , ์Šน์ธ์ด ๋˜๋ฉด ๋กœ๋“œํ•˜๊ธฐ ์œ„ํ•ด ์ปดํ“จํ„ฐ๋ฅผ ์žฌ์‹œ๋™ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋กœ๋”ฉ ๊ณผ์ •

Catalina์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์•˜์Šต๋‹ˆ๋‹ค: ๊ฒ€์ฆ ๊ณผ์ •์ด userland์—์„œ ๋ฐœ์ƒํ•œ๋‹ค๋Š” ์ ์ด ํฅ๋ฏธ๋กญ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ com.apple.private.security.kext-management grant๋ฅผ ๊ฐ€์ง„ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜๋งŒ์ด ์ปค๋„์— ํ™•์žฅ ๋กœ๋“œ๋ฅผ ์š”์ฒญํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: kextcache, kextload, kextutil, kextd, syspolicyd

  1. kextutil cli๊ฐ€ ํ™•์žฅ ๋กœ๋“œ๋ฅผ ์œ„ํ•œ ๊ฒ€์ฆ ์ ˆ์ฐจ๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
  • kextutil์€ Mach ์„œ๋น„์Šค๋กœ ํ†ต์‹ ํ•˜์—ฌ kextd์™€ ๋Œ€ํ™”ํ•ฉ๋‹ˆ๋‹ค.
  1. kextd๋Š” ์„œ๋ช… ๋“ฑ ์—ฌ๋Ÿฌ ํ•ญ๋ชฉ์„ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค.
  • kextd๋Š” ํ™•์žฅ์„ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด syspolicyd์™€ ํ†ต์‹ ํ•ฉ๋‹ˆ๋‹ค.
  1. syspolicyd๋Š” ํ™•์žฅ์ด ์ด์ „์— ๋กœ๋“œ๋œ ์ ์ด ์—†๋‹ค๋ฉด ์‚ฌ์šฉ์ž์—๊ฒŒ ํ”„๋กฌํ”„ํŠธ๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.
  • syspolicyd๋Š” ๊ฒฐ๊ณผ๋ฅผ kextd์— ๋ณด๊ณ ํ•ฉ๋‹ˆ๋‹ค.
  1. kextd๋Š” ์ตœ์ข…์ ์œผ๋กœ ์ปค๋„์— ํ™•์žฅ์„ ๋กœ๋“œํ•˜๋ผ๊ณ  ์ง€์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

kextd๊ฐ€ ์—†์„ ๊ฒฝ์šฐ kextutil์ด ๋™์ผํ•œ ๊ฒ€์‚ฌ๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์—ด๊ฑฐ ๋ฐ ๊ด€๋ฆฌ (loaded kexts)

kextstat๋Š” ์—ญ์‚ฌ์ ์ธ ๋„๊ตฌ์˜€์ง€๋งŒ ์ตœ๊ทผ macOS ๋ฆด๋ฆฌ์Šค์—์„œ๋Š” deprecated๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ˜„๋Œ€์ ์ธ ์ธํ„ฐํŽ˜์ด์Šค๋Š” kmutil์ž…๋‹ˆ๋‹ค:

# List every extension currently linked in the kernel, sorted by load address
sudo kmutil showloaded --sort

# Show only third-party / auxiliary collections
sudo kmutil showloaded --collection aux

# Unload a specific bundle
sudo kmutil unload -b com.example.mykext

์ด์ „ ๊ตฌ๋ฌธ์€ ์ฐธ์กฐ์šฉ์œผ๋กœ ์—ฌ์ „ํžˆ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

# (Deprecated) Get loaded kernel extensions
kextstat

# (Deprecated) Get dependencies of the kext number 22
kextstat | grep " 22 " | cut -c2-5,50- | cut -d '(' -f1

kmutil inspect๋Š” Kernel Collection (KC)์˜ ๋‚ด์šฉ์„ ๋คํ”„ํ•˜๊ฑฐ๋‚˜ kext๊ฐ€ ๋ชจ๋“  ์‹ฌ๋ณผ ์ข…์†์„ฑ์„ ํ•ด๊ฒฐํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฐ์—๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค:

# List fileset entries contained in the boot KC
kmutil inspect -B /System/Library/KernelCollections/BootKernelExtensions.kc --show-fileset-entries

# Check undefined symbols of a 3rd party kext before loading
kmutil libraries -p /Library/Extensions/FancyUSB.kext --undef-symbols

Kernelcache

Caution

/System/Library/Extensions/์— kernel extensions๊ฐ€ ์žˆ์–ด์•ผ ํ•˜์ง€๋งŒ, ์ด ํด๋”์— ๊ฐ€๋„ ๋ฐ”์ด๋„ˆ๋ฆฌ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ด๋Š” kernelcache ๋•Œ๋ฌธ์ด๋ฉฐ, .kext๋ฅผ ๋ฆฌ๋ฒ„์Šคํ•˜๋ ค๋ฉด ์ด๋ฅผ ์–ป๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.

kernelcache๋Š” XNU ์ปค๋„์˜ ์‚ฌ์ „ ์ปดํŒŒ์ผ๋˜๊ณ  ์‚ฌ์ „ ๋งํฌ๋œ ๋ฒ„์ „์œผ๋กœ, ํ•„์ˆ˜ ๋””๋ฐ”์ด์Šค drivers์™€ kernel extensions๊ฐ€ ํ•จ๊ป˜ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์••์ถ•๋œ ํ˜•ํƒœ๋กœ ์ €์žฅ๋˜์–ด ๋ถ€ํŒ… ๊ณผ์ •์—์„œ ๋ฉ”๋ชจ๋ฆฌ๋กœ ์••์ถ•์ด ํ’€๋ฆฝ๋‹ˆ๋‹ค. kernelcache๋Š” ์ปค๋„๊ณผ ์ค‘์š”ํ•œ ๋“œ๋ผ์ด๋ฒ„์˜ ์ฆ‰์‹œ ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ๋ฒ„์ „์„ ์ œ๊ณตํ•˜์—ฌ ๋™์ ์œผ๋กœ ์ด ์ปดํฌ๋„ŒํŠธ๋“ค์„ ๋ถ€ํŒ… ์‹œ ๋กœ๋“œํ•˜๊ณ  ๋งํฌํ•˜๋Š” ๋ฐ ์†Œ์š”๋˜๋Š” ์‹œ๊ฐ„๊ณผ ์ž์›์„ ์ค„์—ฌ ๋” ๋น ๋ฅธ ๋ถ€ํŒ… ์‹œ๊ฐ„์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

kernelcache์˜ ์ฃผ์š” ์žฅ์ ์€ ๋กœ๋”ฉ ์†๋„์™€ ๋ชจ๋“  ๋ชจ๋“ˆ์ด ์‚ฌ์ „ ๋งํฌ๋˜์–ด ์žˆ๋‹ค๋Š” ์ (๋กœ๋”ฉ ์‹œ๊ฐ„ ์ง€์—ฐ ์—†์Œ)์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ชจ๋“  ๋ชจ๋“ˆ์ด ์‚ฌ์ „ ๋งํฌ๋œ ์ดํ›„์—๋Š” KXLD๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์–ด XNU๊ฐ€ ์ƒˆ๋กœ์šด KEXTs๋ฅผ ๋กœ๋“œํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

Tip

https://github.com/dhinakg/aeota ๋„๊ตฌ๋Š” Apple์˜ AEA (Apple Encrypted Archive / AEA asset) ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋ณตํ˜ธํ™”ํ•ฉ๋‹ˆ๋‹ค โ€” Apple์ด OTA ์ž์‚ฐ ๋ฐ ์ผ๋ถ€ IPSW ์กฐ๊ฐ์— ์‚ฌ์šฉํ•˜๋Š” ์•”ํ˜ธํ™”๋œ ์ปจํ…Œ์ด๋„ˆ ํฌ๋งท โ€” ๊ทธ๋ฆฌ๊ณ  ์ œ๊ณต๋œ aastuff ๋„๊ตฌ๋กœ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋ณธ .dmg/asset ์•„์นด์ด๋ธŒ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋กœ์ปฌ kernelcache

iOS์—์„œ๋Š” **/System/Library/Caches/com.apple.kernelcaches/kernelcache**์— ์œ„์น˜ํ•˜๊ณ , macOS์—์„œ๋Š” ๋‹ค์Œ์œผ๋กœ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: find / -name "kernelcache" 2>/dev/null
์ œ ๊ฒฝ์šฐ macOS์—์„œ ์ฐพ์€ ๊ฒฝ๋กœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:

  • /System/Volumes/Preboot/1BAEB4B5-180B-4C46-BD53-51152B7D92DA/boot/DAD35E7BC0CDA79634C20BD1BD80678DFB510B2AAD3D25C1228BB34BCD0A711529D3D571C93E29E1D0C1264750FA043F/System/Library/Caches/com.apple.kernelcaches/kernelcache

๋˜ํ•œ ์—ฌ๊ธฐ์—์„œ kernelcache of version 14 with symbols๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

IMG4 / BVX2 (LZFSE) compressed

IMG4 ํŒŒ์ผ ํฌ๋งท์€ Apple์ด iOS์™€ macOS ์žฅ์น˜์—์„œ ํŽŒ์›จ์–ด ๊ตฌ์„ฑ์š”์†Œ(์˜ˆ: kernelcache)๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ์ €์žฅํ•˜๊ณ  ๊ฒ€์ฆํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ ํฌ๋งท์ž…๋‹ˆ๋‹ค. IMG4 ํฌ๋งท์€ ํ—ค๋”์™€ ์‹ค์ œ ํŽ˜์ด๋กœ๋“œ(์ปค๋„์ด๋‚˜ ๋ถ€ํŠธ๋กœ๋” ๋“ฑ), ์„œ๋ช…, ๋งค๋‹ˆํŽ˜์ŠคํŠธ ์†์„ฑ ์ง‘ํ•ฉ ๋“ฑ์„ ์บก์Аํ™”ํ•˜๋Š” ์—ฌ๋Ÿฌ ํƒœ๊ทธ๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. ์ด ํฌ๋งท์€ ์•”ํ˜ธํ™” ๊ฒ€์ฆ์„ ์ง€์›ํ•˜์—ฌ ์žฅ์น˜๊ฐ€ ํŽŒ์›จ์–ด ๊ตฌ์„ฑ์š”์†Œ์˜ ์ •๋‹น์„ฑ๊ณผ ๋ฌด๊ฒฐ์„ฑ์„ ์‹คํ–‰ ์ „์— ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

๋ณดํ†ต ๋‹ค์Œ ๊ตฌ์„ฑ์š”์†Œ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค:

  • Payload (IM4P):
  • Often compressed (LZFSE4, LZSS, โ€ฆ)
  • Optionally encrypted
  • Manifest (IM4M):
  • Contains Signature
  • Additional Key/Value dictionary
  • Restore Info (IM4R):
  • Also known as APNonce
  • Prevents replaying of some updates
  • OPTIONAL: Usually this isnโ€™t found

Kernelcache ์••์ถ• ํ•ด์ œ:

# img4tool (https://github.com/tihmstar/img4tool)
img4tool -e kernelcache.release.iphone14 -o kernelcache.release.iphone14.e

# pyimg4 (https://github.com/m1stadev/PyIMG4)
pyimg4 im4p extract -i kernelcache.release.iphone14 -o kernelcache.release.iphone14.e

# imjtool (https://newandroidbook.com/tools/imjtool.html)
imjtool _img_name_ [extract]

# disarm (you can use it directly on the IMG4 file) - [https://newandroidbook.com/tools/disarm.html](https://newandroidbook.com/tools/disarm.html)
disarm -L kernelcache.release.v57 # From unzip ipsw

# disamer (extract specific parts, e.g. filesets) - [https://newandroidbook.com/tools/disarm.html](https://newandroidbook.com/tools/disarm.html)
disarm -e filesets kernelcache.release.d23

์ปค๋„์šฉ Disarm ์‹ฌ๋ณผ

**Disarm**๋Š” matchers๋ฅผ ์‚ฌ์šฉํ•ด kernelcache์—์„œ ํ•จ์ˆ˜๋ฅผ symbolicateํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค.

์ด matchers๋Š” ๋‹จ์ˆœํ•œ ํŒจํ„ด ๊ทœ์น™(ํ…์ŠคํŠธ ๋ผ์ธ)์œผ๋กœ, disarm์—๊ฒŒ binary ๋‚ด๋ถ€์˜ functions, arguments & panic/log strings์„ ์–ด๋–ป๊ฒŒ recognise & auto-symbolicateํ• ์ง€ ์•Œ๋ ค์ค๋‹ˆ๋‹ค.

์ฆ‰, ํ•จ์ˆ˜๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์ง€์ •ํ•˜๋ฉด disarm์ด ์ด๋ฅผ ์ฐพ์•„ symbolicate it.

You can find some `xnu.matchers` in [https://newosxbook.com/tools/disarm.html](https://newosxbook.com/tools/disarm.html) in the **`Matchers`** section. You can also create your own matchers.

```bash
# disarm๊ฐ€ filesets๋ฅผ ์ถ”์ถœํ•œ /tmp/extracted๋กœ ์ด๋™
disarm -e filesets kernelcache.release.d23 # Always extract to /tmp/extracted
cd /tmp/extracted
JMATCHERS=xnu.matchers disarm --analyze kernel.rebuilt  # Note that xnu.matchers is actually a file with the matchers

Download

An IPSW (iPhone/iPad Software) is Appleโ€™s firmware package format used for device restores, updates, and full firmware bundles. Among other things, it contains the kernelcache.

In https://github.com/dortania/KdkSupportPkg/releases itโ€™s possible to find all the kernel debug kits. You can download it, mount it, open it with Suspicious Package tool, access the .kext folder and extract it.

Check it for symbols with:

nm -a ~/Downloads/Sandbox.kext/Contents/MacOS/Sandbox | wc -l

Sometime Apple releases kernelcache with symbols. You can download some firmwares with symbols by following links on those pages. The firmwares will contain the kernelcache among other files.

To extract the kernel cache you can do:

# ipsw ๋„๊ตฌ ์„ค์น˜
brew install blacktop/tap/ipsw

# IPSW์—์„œ kernelcache๋งŒ ์ถ”์ถœ
ipsw extract --kernel /path/to/YourFirmware.ipsw -o out/

# ๋‹ค์Œ๊ณผ ์œ ์‚ฌํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค:
#   out/Firmware/kernelcache.release.iPhoneXX
#   or an IMG4 payload: out/Firmware/kernelcache.release.iPhoneXX.im4p

# IMG4 payload๋ฅผ ๋ฐ›์•˜๋‹ค๋ฉด:
ipsw img4 im4p extract out/Firmware/kernelcache*.im4p -o kcache.raw

Another option to extract the files start by changing the extension from .ipsw to .zip and unzip it.

After extracting the firmware you will get a file like: kernelcache.release.iphone14. Itโ€™s in IMG4 format, you can extract the interesting info with:

pyimg4:

pyimg4 im4p extract -i kernelcache.release.iphone14 -o kernelcache.release.iphone14.e

img4tool:

img4tool -e kernelcache.release.iphone14 -o kernelcache.release.iphone14.e
pyimg4 im4p extract -i kernelcache.release.iphone14 -o kernelcache.release.iphone14.e

img4tool:

img4tool -e kernelcache.release.iphone14 -o kernelcache.release.iphone14.e

Inspecting kernelcache

Check if the kernelcache has symbols with

nm -a kernelcache.release.iphone14.e | wc -l

With this we can now extract all the extensions or the one you are interested in:

# ๋ชจ๋“  ํ™•์žฅ ๋‚˜์—ด
kextex -l kernelcache.release.iphone14.e
## com.apple.security.sandbox ์ถ”์ถœ
kextex -e com.apple.security.sandbox kernelcache.release.iphone14.e

# ๋ชจ๋‘ ์ถ”์ถœ
kextex_all kernelcache.release.iphone14.e

# ํ™•์žฅ์˜ ์‹ฌ๋ณผ ํ™•์ธ
nm -a binaries/com.apple.security.sandbox | wc -l

Recent vulnerabilities & exploitation techniques

YearCVESummary
2024CVE-2024-44243Logic flaw in storagekitd allowed a root attacker to register a malicious file-system bundle that ultimately loaded an unsigned kext, bypassing System Integrity Protection (SIP) and enabling persistent rootkits. Patched in macOS 14.2 / 15.2.
2021CVE-2021-30892 (Shrootless)Installation daemon with the entitlement com.apple.rootless.install could be abused to execute arbitrary post-install scripts, disable SIP and load arbitrary kexts.

Take-aways for red-teamers

  1. Look for entitled daemons (codesign -dvv /path/bin | grep entitlements) that interact with Disk Arbitration, Installer or Kext Management.
  2. Abusing SIP bypasses almost always grants the ability to load a kext โ†’ kernel code execution.

Defensive tips

Keep SIP enabled, monitor for kmutil load/kmutil create -n aux invocations coming from non-Apple binaries and alert on any write to /Library/Extensions. Endpoint Security events ES_EVENT_TYPE_NOTIFY_KEXTLOAD provide near real-time visibility.

Debugging macOS kernel & kexts

Appleโ€™s recommended workflow is to build a Kernel Debug Kit (KDK) that matches the running build and then attach LLDB over a KDP (Kernel Debugging Protocol) network session.

One-shot local debug of a panic

# ์ตœ์‹  panic์— ๋Œ€ํ•œ ์‹ฌ๋ณผ๋ฆฌ์ผ€์ด์…˜ ๋ฒˆ๋“ค ์ƒ์„ฑ
sudo kdpwrit dump latest.kcdata
kmutil analyze-panic latest.kcdata -o ~/panic_report.txt

Live remote debugging from another Mac

  1. Download + install the exact KDK version for the target machine.
  2. Connect the target Mac and the host Mac with a USB-C or Thunderbolt cable.
  3. On the target:
sudo nvram boot-args="debug=0x100 kdp_match_name=macbook-target"
reboot
  1. On the host:
lldb
(lldb) kdp-remote "udp://macbook-target"
(lldb) bt  # get backtrace in kernel context

Attaching LLDB to a specific loaded kext

# kext์˜ ๋กœ๋“œ ์ฃผ์†Œ ์‹๋ณ„
ADDR=$(kmutil showloaded --bundle-identifier com.example.driver | awk '{print $4}')

# ์—ฐ๊ฒฐ
sudo lldb -n kernel_task -o "target modules load --file /Library/Extensions/Example.kext/Contents/MacOS/Example --slide $ADDR"

โ„น๏ธ KDP only exposes a read-only interface. For dynamic instrumentation you will need to patch the binary on-disk, leverage kernel function hooking (e.g. mach_override) or migrate the driver to a hypervisor for full read/write.

References

  • DriverKit Security โ€“ Apple Platform Security Guide
  • Microsoft Security Blog โ€“ Analyzing CVE-2024-44243 SIP bypass

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 ์ง€์›ํ•˜๊ธฐ