|
1 | 1 | # macOS Kernel Extensions
|
2 | 2 |
|
3 | 3 | {% hint style="success" %}
|
4 |
| -Learn & practice AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\ |
5 |
| -Learn & practice GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte) |
| 4 | +Learn & practice AWS Hacking:<img src="../../../.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="../../../.gitbook/assets/arte.png" alt="" data-size="line">\ |
| 5 | +Learn & practice GCP Hacking: <img src="../../../.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="../../../.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte) |
6 | 6 |
|
7 | 7 | <details>
|
8 | 8 |
|
@@ -47,14 +47,123 @@ In Catalina it was like this: It is interesting to note that the **verification*
|
47 | 47 |
|
48 | 48 | If **`kextd`** is not available, **`kextutil`** can perform the same checks.
|
49 | 49 |
|
| 50 | +### Enumeration (loaded kexts) |
| 51 | + |
| 52 | +```bash |
| 53 | +# Get loaded kernel extensions |
| 54 | +kextstat |
| 55 | + |
| 56 | +# Get dependencies of the kext number 22 |
| 57 | +kextstat | grep " 22 " | cut -c2-5,50- | cut -d '(' -f1 |
| 58 | +``` |
| 59 | + |
| 60 | +## Kernelcache |
| 61 | + |
| 62 | +{% hint style="danger" %} |
| 63 | +Even though the kernel extensions are expected to be in `/System/Library/Extensions/`, if you go to this folder you **won't find any binary**. This is because of the **kernelcache** and in order to reverse one `.kext` you need to find a way to obtain it. |
| 64 | +{% endhint %} |
| 65 | + |
| 66 | +The **kernelcache** is a **pre-compiled and pre-linked version of the XNU kernel**, along with essential device **drivers** and **kernel extensions**. It's stored in a **compressed** format and gets decompressed into memory during the boot-up process. The kernelcache facilitates a **faster boot time** by having a ready-to-run version of the kernel and crucial drivers available, reducing the time and resources that would otherwise be spent on dynamically loading and linking these components at boot time. |
| 67 | + |
| 68 | +### Local Kerlnelcache |
| 69 | + |
| 70 | +In iOS it's located in **`/System/Library/Caches/com.apple.kernelcaches/kernelcache`** in macOS you can find it with: **`find / -name "kernelcache" 2>/dev/null`** \ |
| 71 | +In my case in macOS I found it in: |
| 72 | + |
| 73 | +* `/System/Volumes/Preboot/1BAEB4B5-180B-4C46-BD53-51152B7D92DA/boot/DAD35E7BC0CDA79634C20BD1BD80678DFB510B2AAD3D25C1228BB34BCD0A711529D3D571C93E29E1D0C1264750FA043F/System/Library/Caches/com.apple.kernelcaches/kernelcache` |
| 74 | + |
| 75 | +#### IMG4 |
| 76 | + |
| 77 | +The IMG4 file format is a container format used by Apple in its iOS and macOS devices for securely **storing and verifying firmware** components (like **kernelcache**). The IMG4 format includes a header and several tags which encapsulate different pieces of data including the actual payload (like a kernel or bootloader), a signature, and a set of manifest properties. The format supports cryptographic verification, allowing the device to confirm the authenticity and integrity of the firmware component before executing it. |
| 78 | + |
| 79 | +It's usually composed of the following components: |
| 80 | + |
| 81 | +* **Payload (IM4P)**: |
| 82 | + * Often compressed (LZFSE4, LZSS, …) |
| 83 | + * Optionally encrypted |
| 84 | +* **Manifest (IM4M)**: |
| 85 | + * Contains Signature |
| 86 | + * Additional Key/Value dictionary |
| 87 | +* **Restore Info (IM4R)**: |
| 88 | + * Also known as APNonce |
| 89 | + * Prevents replaying of some updates |
| 90 | + * OPTIONAL: Usually this isn't found |
| 91 | + |
| 92 | +Decompress the Kernelcache: |
| 93 | + |
| 94 | +```bash |
| 95 | +# img4tool (https://github.com/tihmstar/img4tool |
| 96 | +img4tool -e kernelcache.release.iphone14 -o kernelcache.release.iphone14.e |
| 97 | + |
| 98 | +# pyimg4 (https://github.com/m1stadev/PyIMG4) |
| 99 | +pyimg4 im4p extract -i kernelcache.release.iphone14 -o kernelcache.release.iphone14.e |
| 100 | +``` |
| 101 | + |
| 102 | +### Download  |
| 103 | + |
| 104 | +* [**KernelDebugKit Github**](https://github.com/dortania/KdkSupportPkg/releases) |
| 105 | + |
| 106 | +In [https://github.com/dortania/KdkSupportPkg/releases](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](https://www.mothersruin.com/software/SuspiciousPackage/get.html) tool, access the **`.kext`** folder and **extract it**. |
| 107 | + |
| 108 | +Check it for symbols with: |
| 109 | + |
| 110 | +```bash |
| 111 | +nm -a ~/Downloads/Sandbox.kext/Contents/MacOS/Sandbox | wc -l |
| 112 | +``` |
| 113 | + |
| 114 | +* [**theapplewiki.com**](https://theapplewiki.com/wiki/Firmware/Mac/14.x)**,** [**ipsw.me**](https://ipsw.me/)**,** [**theiphonewiki.com**](https://www.theiphonewiki.com/) |
| 115 | + |
| 116 | +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. |
| 117 | + |
| 118 | +To **extract** the files start by changing the extension from `.ipsw` to `.zip` and **unzip** it. |
| 119 | + |
| 120 | +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: |
| 121 | + |
| 122 | +[**pyimg4**](https://github.com/m1stadev/PyIMG4)**:** |
| 123 | + |
| 124 | +{% code overflow="wrap" %} |
| 125 | +```bash |
| 126 | +pyimg4 im4p extract -i kernelcache.release.iphone14 -o kernelcache.release.iphone14.e |
| 127 | +``` |
| 128 | +{% endcode %} |
| 129 | + |
| 130 | +[**img4tool**](https://github.com/tihmstar/img4tool)**:** |
| 131 | + |
| 132 | +```bash |
| 133 | +img4tool -e kernelcache.release.iphone14 -o kernelcache.release.iphone14.e |
| 134 | +``` |
| 135 | + |
| 136 | +### Inspecting kernelcache |
| 137 | + |
| 138 | +Check if the kernelcache has symbols with |
| 139 | + |
| 140 | +```bash |
| 141 | +nm -a kernelcache.release.iphone14.e | wc -l |
| 142 | +``` |
| 143 | + |
| 144 | +With this we can now **extract all the extensions** or the **one you are interested in:** |
| 145 | + |
| 146 | +```bash |
| 147 | +# List all extensions |
| 148 | +kextex -l kernelcache.release.iphone14.e |
| 149 | +## Extract com.apple.security.sandbox |
| 150 | +kextex -e com.apple.security.sandbox kernelcache.release.iphone14.e |
| 151 | + |
| 152 | +# Extract all |
| 153 | +kextex_all kernelcache.release.iphone14.e |
| 154 | + |
| 155 | +# Check the extension for symbols |
| 156 | +nm -a binaries/com.apple.security.sandbox | wc -l |
| 157 | +``` |
| 158 | + |
50 | 159 | ## Referencias
|
51 | 160 |
|
52 | 161 | * [https://www.makeuseof.com/how-to-enable-third-party-kernel-extensions-apple-silicon-mac/](https://www.makeuseof.com/how-to-enable-third-party-kernel-extensions-apple-silicon-mac/)
|
53 | 162 | * [https://www.youtube.com/watch?v=hGKOskSiaQo](https://www.youtube.com/watch?v=hGKOskSiaQo)
|
54 | 163 |
|
55 | 164 | {% hint style="success" %}
|
56 |
| -Learn & practice AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\ |
57 |
| -Learn & practice GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte) |
| 165 | +Learn & practice AWS Hacking:<img src="../../../.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="../../../.gitbook/assets/arte.png" alt="" data-size="line">\ |
| 166 | +Learn & practice GCP Hacking: <img src="../../../.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="../../../.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte) |
58 | 167 |
|
59 | 168 | <details>
|
60 | 169 |
|
|
0 commit comments