<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Security on Security Kid Blog</title><link>https://securitykid.com/tags/security/</link><description>Recent content in Security on Security Kid Blog</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Thu, 23 Jan 2025 00:00:00 +0200</lastBuildDate><atom:link href="https://securitykid.com/tags/security/index.xml" rel="self" type="application/rss+xml"/><item><title>MacOS Exploitation Notes: Week 1 - Mac Userland Internals</title><link>https://securitykid.com/posts/macos-exploitation-week1/</link><pubDate>Thu, 23 Jan 2025 00:00:00 +0200</pubDate><guid>https://securitykid.com/posts/macos-exploitation-week1/</guid><description>&lt;h3 id="task"&gt;Task:
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;presentation / Blog post for the following topics
&lt;ul&gt;
&lt;li&gt;macOS Arch
&lt;ul&gt;
&lt;li&gt;macOS Layers&lt;/li&gt;
&lt;li&gt;macOS System Directories&lt;/li&gt;
&lt;li&gt;Apple Properity File System ( APFS ) INCLUDING :
&lt;ul&gt;
&lt;li&gt;protection&lt;/li&gt;
&lt;li&gt;macOS filesystem,&lt;/li&gt;
&lt;li&gt;firmlinks&lt;/li&gt;
&lt;li&gt;PLIST Files&lt;/li&gt;
&lt;li&gt;Bundles&lt;/li&gt;
&lt;li&gt;dyld&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;mach-O file format&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="sources-"&gt;Sources :
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;General sources&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Books:
&lt;ul&gt;
&lt;li&gt;[MacOS and iOS Internals, Volume I](file://../MacOS and iOS Internals, Volume I.pdf)&lt;/li&gt;
&lt;li&gt;Advanced Apple Debugging &amp;amp; Reverse Engineering.pdf
&lt;ul&gt;
&lt;li&gt;mach-o&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;For518 &amp;lt;&amp;ndash; missing
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://hackmd.io/@M4shl3/Deep-Dive-Into-APFS-Structure" target="_blank" rel="noopener"
 &gt;blog with a sample from that course&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;APFS&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Apple official resourcces :
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://developer.apple.com/support/apple-file-system/Apple-File-System-Reference.pdf" target="_blank" rel="noopener"
 &gt;&lt;strong&gt;Apple File System Reference&lt;/strong&gt;&lt;/a&gt;: Official, but incomplete APFS spec&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://archive.org/details/wwdc2016videos/701_hd_introducing_apple_file_system.mp4" target="_blank" rel="noopener"
 &gt;introducing Apple File system &lt;/a&gt;(this video was deleted form apple docs) &amp;lt;- useless
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://devstreaming-cdn.apple.com/videos/wwdc/2016/701q0pnn0ietcautcrv/701/701_introducing_apple_file_system.pdf" target="_blank" rel="noopener"
 &gt;Slides&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="http://www.sciencedirect.com/science/article/pii/S1742287617301408" target="_blank" rel="noopener"
 &gt;&lt;strong&gt;Decoding the APFS file system&lt;/strong&gt;&lt;/a&gt;: Paper by Kurt H.Hansen and Fergus Toolan Fergus in Digital Investigation. Published: 2017-09-22.&lt;/li&gt;
&lt;li&gt;Vulns !
&lt;ul&gt;
&lt;li&gt;CVE-2017-7149
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://objective-see.org/blog/blog_0x23.html" target="_blank" rel="noopener"
 &gt;https://objective-see.org/blog/blog_0x23.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;{add more here}&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Plist&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/PropertyLists/Introduction/Introduction.html" target="_blank" rel="noopener"
 &gt;Property List Programming Guide&lt;/a&gt;&lt;/strong&gt; - Complete guide to plists&lt;/li&gt;
&lt;li&gt;info.plist
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Introduction/Introduction.html" target="_blank" rel="noopener"
 &gt;Information Property List Key Reference&lt;/a&gt;&lt;/strong&gt; - All standard keys&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://developer.apple.com/documentation/bundleresources/information_property_list" target="_blank" rel="noopener"
 &gt;Core Foundation Keys&lt;/a&gt;&lt;/strong&gt; - Modern key reference&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Advanced PLIST Topics
&lt;ul&gt;
&lt;li&gt;Code Signing &amp;amp; Entitlements
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://developer.apple.com/documentation/bundleresources/entitlements" target="_blank" rel="noopener"
 &gt;Entitlements Documentation&lt;/a&gt;&lt;/strong&gt; - Security entitlements in plists&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://developer.apple.com/library/archive/documentation/Security/Conceptual/CodeSigningGuide/" target="_blank" rel="noopener"
 &gt;Code Signing Guide&lt;/a&gt;&lt;/strong&gt; - Relationship with Info.plist&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Managed Preferences (MDM)
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://developer.apple.com/business/documentation/Configuration-Profile-Reference.pdf" target="_blank" rel="noopener"
 &gt;Configuration Profile Reference&lt;/a&gt;&lt;/strong&gt; - Enterprise plist management&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://developer.apple.com/documentation/devicemanagement" target="_blank" rel="noopener"
 &gt;MDM Protocol Reference&lt;/a&gt;&lt;/strong&gt; - Device management plists&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Bundles&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Official Apple Documentation
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFBundles/Introduction/Introduction.html" target="_blank" rel="noopener"
 &gt;Bundle Programming Guide&lt;/a&gt;&lt;/strong&gt; - Complete bundle architecture&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://developer.apple.com/documentation/bundleresources" target="_blank" rel="noopener"
 &gt;Bundle Resources&lt;/a&gt;&lt;/strong&gt; - Modern documentation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://developer.apple.com/documentation/foundation/nsbundle" target="_blank" rel="noopener"
 &gt;NSBundle Class Reference&lt;/a&gt;&lt;/strong&gt; - Bundle APIs&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://developer.apple.com/documentation/bundleresources/placing_content_in_a_bundle" target="_blank" rel="noopener"
 &gt;App Bundle Structure&lt;/a&gt;&lt;/strong&gt; - Modern bundle layout&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;dyld (Dynamic Linker)&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Official Apple Documentation
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://opensource.apple.com/source/dyld/" target="_blank" rel="noopener"
 &gt;dyld Source Code&lt;/a&gt;&lt;/strong&gt; - The actual source (best documentation)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://developer.apple.com/documentation/macos-release-notes/dynamic-loader-release-notes" target="_blank" rel="noopener"
 &gt;Dynamic Loader Release Notes&lt;/a&gt;&lt;/strong&gt; - Version changes&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/OverviewOfDynamicLibraries.html" target="_blank" rel="noopener"
 &gt;Dynamic Library Programming Topics&lt;/a&gt;&lt;/strong&gt; - Comprehensive guide&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Understanding the Cache
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://iphonedev.wiki/Dyld_shared_cache" target="_blank" rel="noopener"
 &gt;dyld Shared Cache Format&lt;/a&gt;&lt;/strong&gt; - Technical details&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://github.com/keith/dyld-shared-cache-extractor" target="_blank" rel="noopener"
 &gt;Extracting Libraries from dyld_shared_cache&lt;/a&gt;&lt;/strong&gt; - Extraction tool&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://pewpewthespells.com/blog/dyld_shared_cache.html" target="_blank" rel="noopener"
 &gt;The dyld Shared Cache&lt;/a&gt;&lt;/strong&gt; - Deep dive article&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Blog Posts &amp;amp; Articles
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://www.mikeash.com/pyblog/friday-qa-2012-11-09-dyld-dynamic-linking-on-os-x.html" target="_blank" rel="noopener"
 &gt;Mike Ash: dyld Dynamic Linking&lt;/a&gt;&lt;/strong&gt; - Excellent walkthrough&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://pewpewthespells.com/blog/dynamic_linking.html" target="_blank" rel="noopener"
 &gt;Dynamic Linking&lt;/a&gt;&lt;/strong&gt; - Detailed mechanics&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://www.emergetools.com/blog/posts/iOS-dyld-deep-dive" target="_blank" rel="noopener"
 &gt;Understanding dyld&lt;/a&gt;&lt;/strong&gt; - Modern deep dive&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Mach-O File Format&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Official Apple Documentation
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/MachOTopics/0-Introduction/introduction.html" target="_blank" rel="noopener"
 &gt;Mach-O Programming Topics&lt;/a&gt;&lt;/strong&gt; - Official guide&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://github.com/aidansteele/osx-abi-macho-file-format-reference" target="_blank" rel="noopener"
 &gt;OS X ABI Mach-O File Format&lt;/a&gt;&lt;/strong&gt; - Complete specification&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/MachORuntime/index.html" target="_blank" rel="noopener"
 &gt;Mach-O Runtime Architecture&lt;/a&gt;&lt;/strong&gt; - Runtime details&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Comprehensive Guides
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://lowlevelbits.org/parsing-mach-o-files/" target="_blank" rel="noopener"
 &gt;Parsing Mach-O Files&lt;/a&gt;&lt;/strong&gt; - Alex Denisov&amp;rsquo;s tutorial&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://blog.timac.org/2016/1018-analysis-of-the-facebook-app-for-ios/" target="_blank" rel="noopener"
 &gt;The Mach-O Executable Format&lt;/a&gt;&lt;/strong&gt; - Detailed walkthrough&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://x86matthew.com/view_post?id=11" target="_blank" rel="noopener"
 &gt;Let&amp;rsquo;s Build a Mach-O Executable&lt;/a&gt;&lt;/strong&gt; - From scratch tutorial&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Security Research
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://github.com/iosre/iOSAppReverseEngineering" target="_blank" rel="noopener"
 &gt;iOS App Reverse Engineering&lt;/a&gt;&lt;/strong&gt; - Mach-O in iOS context&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://knight.sc/reverse%20engineering/2019/04/15/macos-code-injection.html" target="_blank" rel="noopener"
 &gt;macOS Code Injection&lt;/a&gt;&lt;/strong&gt; - Security implications&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://blog.xpnsec.com/macos-injection-via-third-party-frameworks/" target="_blank" rel="noopener"
 &gt;Mach-O Tricks&lt;/a&gt;&lt;/strong&gt; - Advanced techniques&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Sample Projects
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://github.com/qyang-nj/llios/tree/main/macho_parser" target="_blank" rel="noopener"
 &gt;Write a Mach-O Loader&lt;/a&gt;&lt;/strong&gt; - Educational implementation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://github.com/flowerinthenight/single-macho" target="_blank" rel="noopener"
 &gt;Minimal Mach-O Executables&lt;/a&gt;&lt;/strong&gt; - Smallest valid files&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://github.com/DeVaukz/MachO-Kit" target="_blank" rel="noopener"
 &gt;Mach-O Kit&lt;/a&gt;&lt;/strong&gt; - Objective-C framework&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;GitHub Projects
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://github.com/blacktop/go-macho" target="_blank" rel="noopener"
 &gt;mach-o Parser in Go&lt;/a&gt;&lt;/strong&gt; - Go implementation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://github.com/lief-project/LIEF" target="_blank" rel="noopener"
 &gt;LIEF Project&lt;/a&gt;&lt;/strong&gt; - Multi-format binary parser&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://github.com/Tyilo/machomachomangler" target="_blank" rel="noopener"
 &gt;machomachomangler&lt;/a&gt;&lt;/strong&gt; - Mach-O manipulation&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Code Signing
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://developer.apple.com/library/archive/documentation/Security/Conceptual/CodeSigningGuide/" target="_blank" rel="noopener"
 &gt;Code Signing Guide&lt;/a&gt;&lt;/strong&gt; - Integration with Mach-O&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a class="link" href="https://github.com/thefloweringash/sigtool" target="_blank" rel="noopener"
 &gt;Code Signature Format&lt;/a&gt;&lt;/strong&gt; - Signature structure&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;extra:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;cryptex &lt;a class="link" href="https://eclecticlight.co/2022/11/16/cryptex-how-a-custom-iphone-is-changing-macos-updates/" target="_blank" rel="noopener"
 &gt;https://eclecticlight.co/2022/11/16/cryptex-how-a-custom-iphone-is-changing-macos-updates/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="topics"&gt;Topics
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;Mac OS architecture
&lt;ul&gt;
&lt;li&gt;history&lt;/li&gt;
&lt;li&gt;overall view&lt;/li&gt;
&lt;li&gt;Darwin&lt;/li&gt;
&lt;li&gt;XNU
&lt;ul&gt;
&lt;li&gt;MACH&lt;/li&gt;
&lt;li&gt;BSD&lt;/li&gt;
&lt;li&gt;kEXTs&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;APFS
&lt;ul&gt;
&lt;li&gt;general introduction
&lt;ul&gt;
&lt;li&gt;volumes &amp;amp; partitions&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;System volumes&lt;/li&gt;
&lt;li&gt;Signed System Volume&lt;/li&gt;
&lt;li&gt;SIP&lt;/li&gt;
&lt;li&gt;firmlinks&lt;/li&gt;
&lt;li&gt;Bundles
&lt;ul&gt;
&lt;li&gt;PLIST Files&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;dyld&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;mach-O
&lt;ul&gt;
&lt;li&gt;general intro&lt;/li&gt;
&lt;li&gt;mach-o header&lt;/li&gt;
&lt;li&gt;The Load Commands&lt;/li&gt;
&lt;li&gt;segments&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id="blog"&gt;Blog
&lt;/h1&gt;&lt;h2 id="1-mac-evolution-from-system-1-to-today"&gt;1. Mac evolution from system 1 to today
&lt;/h2&gt;&lt;p&gt;&lt;img alt="Pasted image 20251004221008" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://securitykid.com/Pasted%20image%2020251004221008.png"&gt;&lt;/p&gt;
&lt;p&gt;Understanding where macOS came from helps contextualize its current architecture:&lt;/p&gt;
&lt;p&gt;The NeXT Connection (why literally every thing starts with NS )
When Steve Jobs left Apple , he founded NeXT Computer and developed NeXTSTEP, a Unix-based operating system built on the Mach microkernel and BSD. This OS was revolutionary, featuring an object-oriented API and advanced development tools.&lt;/p&gt;
&lt;p&gt;Apple acquired NeXT , bringing Jobs back and inheriting NeXTSTEP&amp;rsquo;s technology. Apple combined NeXTSTEP with elements of the classic Mac OS to create Mac OS X as version 10.0 &amp;ldquo;Cheetah.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;Through versions 10.0 to 10.15, the system was called &amp;ldquo;OS X&amp;rdquo; or &amp;ldquo;Mac OS X.&amp;rdquo; In 2016, Apple rebranded it to &amp;ldquo;macOS&amp;rdquo; to align with iOS, watchOS, and tvOS. Today&amp;rsquo;s macOS has evolved significantly while maintaining its Unix foundation, especially with the transition to Apple Silicon&lt;/p&gt;
&lt;h2 id="2-mac-os-architecture"&gt;2. Mac OS architecture
&lt;/h2&gt;&lt;p&gt;macOS presents a beautifully layered architecture that combines Unix heritage with Apple&amp;rsquo;s innovative design. At its core, the system is built on several key layers:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Architecture Stack:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;User Experience Layer&lt;/strong&gt;: Aqua, Spotlight, and the applications we interact with daily&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Application Frameworks&lt;/strong&gt;: Cocoa, Carbon, and various APIs (AppKit, Foundation, Core Data)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Graphics and Media&lt;/strong&gt;: Core Graphics, Core Animation, Metal, AVFoundation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Core Services&lt;/strong&gt;: Launch Services, Core Foundation, System Configuration&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Darwin&lt;/strong&gt;: The Unix foundation (kernel, drivers, and core utilities)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Pasted image 20251004224356" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://securitykid.com/Pasted%20image%2020251004224356.png"&gt;&lt;/p&gt;
&lt;p&gt;Darwin is the Unix-based core of macOS, and it&amp;rsquo;s actually open source! This might surprise many, but you can browse Darwin&amp;rsquo;s source code at &lt;a class="link" href="https://opensource.apple.com/releases/" target="_blank" rel="noopener"
 &gt;opensource.apple.com&lt;/a&gt; (older versions contain much much more code/info that newer ones !)&lt;/p&gt;
&lt;p&gt;Darwin consists from :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;XNU kernel&lt;/li&gt;
&lt;li&gt;Device drivers and kernel extensions&lt;/li&gt;
&lt;li&gt;Standard Unix utilities and libraries&lt;/li&gt;
&lt;li&gt;Network stack and file systems&lt;/li&gt;
&lt;li&gt;Low level system daemons&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Pasted image 20251004224426" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://securitykid.com/Pasted%20image%2020251004224426.png"&gt;&lt;/p&gt;
&lt;h4 id="xnu-the-hybrid-kernel"&gt;XNU: The Hybrid Kernel
&lt;/h4&gt;&lt;p&gt;XNU is a hybrid design that combines two distinct kernel architectures.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;MACH The Microkernel Foundation
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Mach 3.0 forms the microkernel base of XNU, providing:&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Core Abstractions:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Tasks&lt;/strong&gt;: The unit of resource ownership (comparable to a process container)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Threads&lt;/strong&gt;: The unit of execution within tasks&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ports&lt;/strong&gt;: Communication endpoints for inter-process communication (IPC)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Messages&lt;/strong&gt;: Data exchanged through ports&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;BSD: The Unix Personality&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Layered on top of Mach is the BSD subsystem, derived from FreeBSD. This provides the POSIX-compliant Unix interface that applications expect:&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;What BSD Brings:&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;Process model (fork, exec, signals)&lt;/li&gt;
&lt;li&gt;File system interfaces (VFS layer)&lt;/li&gt;
&lt;li&gt;POSIX APIs (the standard Unix system calls)&lt;/li&gt;
&lt;li&gt;Network stack (TCP/IP, sockets)&lt;/li&gt;
&lt;li&gt;User and permission management&lt;/li&gt;
&lt;li&gt;Unix security model&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Pasted image 20251004221331" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://securitykid.com/Pasted%20image%2020251004221331.png"&gt;
img source: &lt;a class="link" href="https://github.com/Brandon7CC/mac-monitor/wiki/3.-macOS-System-Architecture" target="_blank" rel="noopener"
 &gt;https://github.com/Brandon7CC/mac-monitor/wiki/3.-macOS-System-Architecture&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="3-apfs-apples-modern-file-system"&gt;3. APFS: Apple&amp;rsquo;s Modern File System
&lt;/h2&gt;&lt;p&gt;&lt;img alt="Pasted image 20251004224151" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://securitykid.com/Pasted%20image%2020251004224151.png"&gt;
img source : &lt;a class="link" href="https://developer.apple.com/support/apple-file-system/Apple-File-System-Reference.pdf" target="_blank" rel="noopener"
 &gt;&lt;strong&gt;Apple File System Reference&lt;/strong&gt;&lt;/a&gt;: Official, but incomplete APFS spec&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Why APFS Matters:&lt;/strong&gt;&lt;br&gt;
APFS was built for SSDs (though it works on HDDs), with optimization for random access patterns and reduced write amplification—crucial for flash storage longevity.&lt;/p&gt;
&lt;h4 id="31-volumes"&gt;3.1 Volumes
&lt;/h4&gt;&lt;p&gt;APFS changes how we think about storage organization:
&lt;strong&gt;Container Model:&lt;/strong&gt;&lt;br&gt;
An APFS container is the outermost structure (essentially a partition) Within a container, you create multiple volumes that dynamically share space.&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;[APFS Container - 500GB]
├── Macintosh HD (200GB used)
├── Macintosh HD - Data (250GB used)
└── Time Machine (50GB used)
Total: 500GB used from shared pool
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;-&amp;gt; Unlike traditional partitioning where you must pre-allocate fixed sizes, APFS volumes grow and shrink as needed within the container&amp;rsquo;s total space.
Modern macOS uses a split-volume approach:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Macintosh HD (System Volume):&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;Contains the OS itself (/System, /Applications, /usr)&lt;/li&gt;
&lt;li&gt;Read-only and cryptographically signed&lt;/li&gt;
&lt;li&gt;Protected by Signed System Volume (SSV)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Macintosh HD - Data (Data Volume):&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;Contains user data (/Users, /Applications, /private/var)&lt;/li&gt;
&lt;li&gt;Writable and modifiable&lt;/li&gt;
&lt;li&gt;Persists across OS updates&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This separation allows macOS to update the entire system volume atomically while preserving user data.&lt;/p&gt;
&lt;h4 id="32-signed-system-volume-ssv"&gt;3.2 Signed System Volume (SSV)
&lt;/h4&gt;&lt;p&gt;&lt;strong&gt;How It Works:&lt;/strong&gt;&lt;br&gt;
The system volume is sealed with a cryptographic signature at the end of the OS installation. This creates a Merkle tree of hashes covering the entire volume. At boot, the system verifies:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The root hash matches the signature&lt;/li&gt;
&lt;li&gt;All files match their recorded hashes&lt;/li&gt;
&lt;li&gt;No unauthorized modifications occurred&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Benefits:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Malware cannot modify system files&lt;/li&gt;
&lt;li&gt;Ensures system file integrity&lt;/li&gt;
&lt;li&gt;Enables secure OS updates&lt;/li&gt;
&lt;li&gt;Works seamlessly with System Integrity Protection&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Under the Hood:&lt;/strong&gt;&lt;br&gt;
The snapshot mechanism in APFS makes this possible. The system boots &lt;em&gt;&lt;strong&gt;from a sealed snapshot&lt;/strong&gt;&lt;/em&gt;, and macOS creates a new snapshot with each system update&lt;/p&gt;
&lt;p&gt;testing its enforcement through Authenticated Root (an enforcement mechanism) :&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;yosifqassim@KosharyMac Downloads % csrutil authenticated-root status 
Authenticated Root status: enabled
&lt;/code&gt;&lt;/pre&gt;&lt;h4 id="33-firmlinks"&gt;3.3 Firmlinks
&lt;/h4&gt;&lt;p&gt;Firmlinks are APFS&amp;rsquo;s clever solution to the split-volume architecture:
&lt;strong&gt;What Are Firmlinks?&lt;/strong&gt;&lt;br&gt;
Firmlinks are bidirectional, kernel-level &amp;ldquo;wormholes&amp;rdquo; that seamlessly connect directories across volumes. They&amp;rsquo;re like symbolic links but handled at the file system level.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Common Firmlinks:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;/Users → /System/Volumes/Data/Users&lt;/li&gt;
&lt;li&gt;/private/var → /System/Volumes/Data/private/var&lt;/li&gt;
&lt;li&gt;/tmp → /System/Volumes/Data/tmp&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Why They Matter:&lt;/strong&gt;&lt;br&gt;
Applications expect to find user data at traditional Unix paths like &lt;code&gt;/Users&lt;/code&gt;. Firmlinks maintain this illusion while data actually resides on the separate Data volume. This happens transparently apps don&amp;rsquo;t know they&amp;rsquo;re crossing volume boundaries.&lt;/p&gt;
&lt;p&gt;we can view the system firmlinks through :&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;yosifqassim@KosharyMac Downloads % cat /usr/share/firmlinks 

/AppleInternal AppleInternal

/Applications Applications

/Library Library

/System/Library/Caches System/Library/Caches

/System/Library/Assets System/Library/Assets

/System/Library/PreinstalledAssets System/Library/PreinstalledAssets

/System/Library/AssetsV2 System/Library/AssetsV2

/System/Library/PreinstalledAssetsV2 System/Library/PreinstalledAssetsV2

/System/Library/CoreServices/CoreTypes.bundle/Contents/Library System/Library/CoreServices/CoreTypes.bundle/Contents/Library

/System/Library/Speech System/Library/Speech

/Users Users

/Volumes Volumes

/cores cores

/opt opt

/private private

/usr/local usr/local

/usr/libexec/cups usr/libexec/cups

/usr/share/snmp usr/share/snmp
&lt;/code&gt;&lt;/pre&gt;&lt;h4 id="34-bundles---its-just-a-zip-file-"&gt;3.4 Bundles - ITS JUST A ZIP FILE !!!!!!!!!!!!!!!!
&lt;/h4&gt;&lt;p&gt;Bundles are one of macOS&amp;rsquo;s most elegant concepts—they&amp;rsquo;re directories that the system treats as single files.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What Is a Bundle?&lt;/strong&gt;&lt;br&gt;
A bundle is a standardized directory structure containing an executable and its resources. The Finder displays bundles as single items, hiding their internal structure from users.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Common Bundle Types:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;.app (Applications):&lt;/strong&gt; &amp;lt;&amp;mdash;- ios works in the same way (you cant imagine how many bugs come out just from viewing the internals of these files )&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;MyApp.app/
├── Contents/
│ ├── Info.plist
│ ├── MacOS/
│ │ └── MyApp (executable)
│ ├── Resources/
│ │ ├── icon.icns
│ │ ├── MainMenu.nib
│ │ └── en.lproj/
│ └── Frameworks/
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;img alt="Pasted image 20251004231305" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://securitykid.com/Pasted%20image%2020251004231305.png"&gt;
&lt;img alt="Pasted image 20251004231232" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://securitykid.com/Pasted%20image%2020251004231232.png"&gt;
&lt;strong&gt;.framework (Shared Libraries):&lt;/strong&gt;&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;MyFramework.framework/
├── MyFramework (symlink to Versions/Current/MyFramework)
├── Resources (symlink)
├── Headers (symlink)
└── Versions/
 ├── A/
 │ ├── MyFramework (executable)
 │ ├── Resources/
 │ └── Headers/
 └── Current → A
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;Why Bundles?&lt;/strong&gt; (because it works !)&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Encapsulation: Everything an app needs is contained&lt;/li&gt;
&lt;li&gt;Localization: Resources for different languages live together&lt;/li&gt;
&lt;li&gt;Versioning: Frameworks support multiple versions&lt;/li&gt;
&lt;li&gt;Installation: Just drag and drop&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="35-plist-files-property-lists"&gt;3.5 PLIST Files: Property Lists
&lt;/h4&gt;&lt;p&gt;Property Lists (plists) are Apple&amp;rsquo;s configuration file format, used throughout macOS.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What Are Plists?&lt;/strong&gt;&lt;br&gt;
Plists store serialized data in key-value format. They&amp;rsquo;re XML by default but can be binary or JSON.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Common Uses:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Info.plist: Bundle metadata&lt;/li&gt;
&lt;li&gt;System configuration&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Pasted image 20251004232022" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://securitykid.com/Pasted%20image%2020251004232022.png"&gt;&lt;/p&gt;
&lt;h4 id="36-system-integrity-protection-sip---why-you-cant-run-debuggers-normally"&gt;3.6 System Integrity Protection (SIP) - why you cant run debuggers normally
&lt;/h4&gt;&lt;p&gt;Introduced in OS X El Capitan (10.11), SIP restricts what even the root user can do.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What SIP Protects:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;System files and directories&lt;/li&gt;
&lt;li&gt;Runtime process attachment and debugging &amp;lt;&amp;mdash;&amp;mdash;&amp;mdash; the issue for us&lt;/li&gt;
&lt;li&gt;Kernel extension loading &amp;lt;&amp;mdash;&amp;mdash;&amp;mdash;&amp;mdash; the issue for kernal exploitation&lt;/li&gt;
&lt;li&gt;System integrity (NVRAM variables, kernel memory)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;How It Works:&lt;/strong&gt;&lt;br&gt;
SIP is enforced by the kernel. Even process ID 0 (kernel_task) respects SIP restrictions. Certain operations are simply impossible while SIP is enabled, regardless of privileges.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Protected Paths:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;/System&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;/usr (except /usr/local)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;/bin, /sbin&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Pre-installed /Applications&lt;/p&gt;
&lt;p&gt;because of this neat protection you wont be able to use many features of dynamic debuggers easily (or at all) - i turned it off on my machine to be able to use radare on an executable once&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="37-dyld-the-dynamic-linker"&gt;3.7 dyld: The Dynamic Linker
&lt;/h4&gt;&lt;p&gt;Think of dyld as the &amp;ldquo;librarian&amp;rdquo; of the operating system. When you double-click an app, the kernel loads the Mach-O binary into memory, but that binary is incomplete—it references dozens or hundreds of external libraries and frameworks. dyld&amp;rsquo;s job is to find all those dependencies, load them into memory, connect everything together, and hand control over to your application.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Every single application on your Mac or iOS device goes through dyld. No exceptions.&lt;/li&gt;
&lt;li&gt;One of dyld&amp;rsquo;s most clever optimizations is the &lt;strong&gt;shared cache&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;WAS&lt;/em&gt; Located at &lt;code&gt;/System/Library/dyld/dyld_shared_cache_*&lt;/code&gt;, this file contains:
&lt;ul&gt;
&lt;li&gt;All system frameworks and libraries&lt;/li&gt;
&lt;li&gt;Pre-linked and pre-bound code&lt;/li&gt;
&lt;li&gt;Optimized memory layout&lt;/li&gt;
&lt;li&gt;Merged into a single, mappable file&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So Instead of loading 50 separate framework files, dyld can map one large region of the shared cache containing all 50 frameworks already linked together.&lt;/p&gt;
&lt;h2 id="4-mach-o-the-executable-format"&gt;4. Mach-O: The Executable Format
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;What is Mach-O?&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;Mach-O is the file format for:
&lt;ul&gt;
&lt;li&gt;Executables&lt;/li&gt;
&lt;li&gt;Dynamic libraries (.dylib)&lt;/li&gt;
&lt;li&gt;Bundles (loadable modules)&lt;/li&gt;
&lt;li&gt;Object files (.o)&lt;/li&gt;
&lt;li&gt;Core dumps&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;File Structure Overview:&lt;/strong&gt;&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;[Mach-O File]
├── Header
├── Load Commands
├── Segments
│ ├── __TEXT (code, read-only data)
│ ├── __DATA (initialized data)
│ ├── __LINKEDIT (linking information)
│ └── ...
└── Symbol/String Tables
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;Universal Binaries (Fat Files):&lt;/strong&gt;&lt;br&gt;
macOS supports &amp;ldquo;fat binaries&amp;rdquo; containing multiple architectures:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;[Fat Binary]
├── Fat Header
├── x86_64 Mach-O
├── arm64 Mach-O
└── arm64e Mach-O (Apple Silicon with PAC)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This allows a single executable to run on both Intel and Apple Silicon Macs.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Magic Numbers:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;0xFEEDFACE&lt;/code&gt;: 32-bit Mach-O&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0xFEEDFACF&lt;/code&gt;: 64-bit Mach-O&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0xCAFEBABE&lt;/code&gt;: Universal binary&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0xCAFEBABF&lt;/code&gt;: 64-bit universal binary&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Pasted image 20251004233246" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://securitykid.com/Pasted%20image%2020251004233246.png"&gt;&lt;/p&gt;
&lt;h2 id="4-mach-o-the-executable-format-1"&gt;4. Mach-O: The Executable Format
&lt;/h2&gt;&lt;p&gt;Every time you launch an application on macOS, you&amp;rsquo;re loading a Mach-O file. It&amp;rsquo;s the native executable format that Apple inherited from NeXT, and it&amp;rsquo;s been refined over decades to support everything from simple command-line tools to complex GUI applications.&lt;/p&gt;
&lt;h3 id="41-what-exactly-is-mach-o"&gt;4.1 What Exactly Is Mach-O?
&lt;/h3&gt;&lt;p&gt;Mach-O (Mach Object) is the container format for executable code on macOS and iOS. Think of it as a precisely structured package that tells the system:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What architecture this code runs on (Intel x86_64, Apple Silicon arm64)&lt;/li&gt;
&lt;li&gt;Where different parts of the program live in memory&lt;/li&gt;
&lt;li&gt;What libraries it needs&lt;/li&gt;
&lt;li&gt;How to set up memory protections&lt;/li&gt;
&lt;li&gt;Where to find symbols and debugging information&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Mach-O is used for:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Executables&lt;/strong&gt;: The actual applications you run&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamic Libraries&lt;/strong&gt; (.dylib): Shared code (like .dll on Windows or .so on Linux)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Bundles&lt;/strong&gt;: Loadable plugins and modules&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Object Files&lt;/strong&gt; (.o): Intermediate compilation output&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Core Dumps&lt;/strong&gt;: Memory snapshots for debugging crashes&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Kernel Extensions&lt;/strong&gt; (.kext): Kernel-mode drivers&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="42-the-magic-numbers---file-type-detection"&gt;4.2 The Magic Numbers - File Type Detection
&lt;/h3&gt;&lt;p&gt;Every Mach-O file starts with a &amp;ldquo;magic number&amp;rdquo; - a specific byte sequence that identifies the file type. It&amp;rsquo;s like a secret handshake:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;0xFEEDFACE → 32-bit Mach-O (little-endian)
0xFEEDFACF → 64-bit Mach-O (little-endian)
0xCAFEBABE → Universal/Fat binary (multiple architectures)
0xCAFEBABF → 64-bit Fat binary
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Notice the playful names? Apple engineers have a sense of humor. You can check any file&amp;rsquo;s magic number:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;xxd -l &lt;span class="m"&gt;4&lt;/span&gt; /bin/ls
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;00000000: cffa edfe ....
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# That&amp;#39;s 0xFEEDFACF in little-endian = 64-bit Mach-O&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="43-universal-binaries-one-file-multiple-architectures"&gt;4.3 Universal Binaries: One File, Multiple Architectures
&lt;/h3&gt;&lt;p&gt;Here&amp;rsquo;s where things get clever. Apple&amp;rsquo;s transition from PowerPC to Intel, and now from Intel to Apple Silicon, created a problem: how do you ship one app that runs on different CPU architectures?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution: Fat/Universal Binaries&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A universal binary is like a matryoshka doll - it&amp;rsquo;s a container holding multiple complete Mach-O binaries, one for each architecture:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;[Universal Binary Structure]
├── Fat Header (tells you what&amp;#39;s inside)
│ ├── Magic: 0xCAFEBABE
│ ├── Number of architectures: 2
│ └── Architecture descriptors
│ ├── [x86_64: offset 0x4000, size 0x50000]
│ └── [arm64: offset 0x54000, size 0x48000]
│
├── [Offset 0x4000] Complete x86_64 Mach-O executable
└── [Offset 0x54000] Complete arm64 Mach-O executable
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;When you launch the app, the system automatically picks the right architecture slice and ignores the rest. You can view what&amp;rsquo;s inside:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;file /Applications/Safari.app/Contents/MacOS/Safari
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# Output: Mach-O universal binary with 2 architectures&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# - x86_64&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# - arm64e (Apple Silicon with pointer authentication)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;lipo -info /Applications/Safari.app/Contents/MacOS/Safari
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# Lists all architectures in detail&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Why This Matters:&lt;/strong&gt;&lt;br&gt;
Universal binaries let Apple support both Intel and Apple Silicon Macs during the transition period. The downside? File sizes roughly double since you&amp;rsquo;re literally including two complete programs.&lt;/p&gt;
&lt;h3 id="44-the-mach-o-file-structure---three-main-parts"&gt;4.4 The Mach-O File Structure - Three Main Parts
&lt;/h3&gt;&lt;p&gt;Every Mach-O file (regardless of type) follows the same basic structure:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;┌─────────────────────────────────────┐
│ Mach-O Header │ ← Who am I? (architecture, file type)
├─────────────────────────────────────┤
│ Load Commands │ ← What do I need? (libraries, segments)
├─────────────────────────────────────┤
│ Segment Data │ ← Here&amp;#39;s the actual code and data
│ ┌───────────────────────────┐ │
│ │ __TEXT (code) │ │
│ ├───────────────────────────┤ │
│ │ __DATA (variables) │ │
│ ├───────────────────────────┤ │
│ │ __LINKEDIT (symbols) │ │
│ └───────────────────────────┘ │
└─────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Let&amp;rsquo;s break down each part:&lt;/p&gt;
&lt;h3 id="45-part-1-the-mach-o-header"&gt;4.5 Part 1: The Mach-O Header
&lt;/h3&gt;&lt;p&gt;The header is the file&amp;rsquo;s ID card. It&amp;rsquo;s exactly 32 bytes (on 64-bit systems) and contains:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;mach_header_64&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="n"&gt;magic&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 0xFEEDFACF for 64-bit
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;cpu_type_t&lt;/span&gt; &lt;span class="n"&gt;cputype&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// CPU architecture (x86_64, arm64)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;cpu_subtype_t&lt;/span&gt; &lt;span class="n"&gt;cpusubtype&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Specific CPU variant
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="n"&gt;filetype&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Executable? Library? Bundle?
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="n"&gt;ncmds&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Number of load commands
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="n"&gt;sizeofcmds&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Total size of load commands
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="n"&gt;flags&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Behavioral flags
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="n"&gt;reserved&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Reserved for future use
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Key Fields Explained:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;filetype&lt;/strong&gt;: Tells you what kind of Mach-O this is&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;MH_EXECUTE&lt;/code&gt; (0x2): Executable program&lt;/li&gt;
&lt;li&gt;&lt;code&gt;MH_DYLIB&lt;/code&gt; (0x6): Dynamic library&lt;/li&gt;
&lt;li&gt;&lt;code&gt;MH_BUNDLE&lt;/code&gt; (0x8): Loadable bundle&lt;/li&gt;
&lt;li&gt;&lt;code&gt;MH_KEXT_BUNDLE&lt;/code&gt; (0xB): Kernel extension&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;flags&lt;/strong&gt;: Behavioral switches (can have multiple)&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;MH_PIE&lt;/code&gt;: Position Independent Executable (ASLR-enabled)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;MH_TWOLEVEL&lt;/code&gt;: Uses two-level namespace for symbols&lt;/li&gt;
&lt;li&gt;&lt;code&gt;MH_NO_HEAP_EXECUTION&lt;/code&gt;: Heap isn&amp;rsquo;t executable (security)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can inspect headers with otool:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;otool -h /bin/ls
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# Mach header&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# magic cputype cpusubtype caps filetype ncmds sizeofcmds flags&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# 0xfeedfacf 16777223 3 0x00 2 19 1848 0x00a18085&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="46-part-2-the-load-commands---the-blueprint"&gt;4.6 Part 2: The Load Commands - The Blueprint
&lt;/h3&gt;&lt;p&gt;Load commands are instructions that tell dyld (the dynamic linker) how to set up the process. They immediately follow the header and consume the space specified by &lt;code&gt;sizeofcmds&lt;/code&gt; in the header.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Think of load commands as a construction blueprint:&lt;/strong&gt;&lt;br&gt;
&amp;ldquo;Put the code segment at this address. Load these libraries. Set up this entry point. Configure these memory protections.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Common Load Commands:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;LC_SEGMENT_64&lt;/strong&gt; - &amp;ldquo;Here&amp;rsquo;s a chunk of data to load into memory&amp;rdquo;&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;Command: LC_SEGMENT_64
Segment name: __TEXT
VM Address: 0x100000000
VM Size: 0x4000
File Offset: 0x0
File Size: 0x4000
Protection: r-x (read + execute, no write)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;LC_LOAD_DYLIB&lt;/strong&gt; - &amp;ldquo;I need this library&amp;rdquo;&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;Command: LC_LOAD_DYLIB
Library: /usr/lib/libSystem.B.dylib
Timestamp: 2 (Thursday, January 1, 1970 at 2:00:00 AM)
Current version: 1311.0.0
Compatibility version: 1.0.0
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;LC_MAIN&lt;/strong&gt; - &amp;ldquo;Start execution here&amp;rdquo; (modern executables)&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;Command: LC_MAIN
Entry point offset: 0x1bf0
Stack size: 0x0 (use default)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;LC_CODE_SIGNATURE&lt;/strong&gt; - &amp;ldquo;Here&amp;rsquo;s my cryptographic signature&amp;rdquo;&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;Command: LC_CODE_SIGNATURE
Data offset: 0x5000
Data size: 0x1a0
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;LC_UUID&lt;/strong&gt; - &amp;ldquo;My unique identifier&amp;rdquo;&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;Command: LC_UUID
UUID: E0B4A991-6F27-3B2C-A3D8-92F4B2AA1B4E
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;Why So Many Load Commands?&lt;/strong&gt;&lt;br&gt;
Modern executables can have 20-40+ load commands. Each one tells dyld something specific about how to prepare the process environment.&lt;/p&gt;
&lt;p&gt;View all load commands:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;otool -l /bin/ls &lt;span class="p"&gt;|&lt;/span&gt; less
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# or for better readability&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;otool -l /bin/ls &lt;span class="p"&gt;|&lt;/span&gt; grep &lt;span class="s2"&gt;&amp;#34;cmd\|segname\|sectname&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="47-part-3-segments-and-sections---where-everything-lives"&gt;4.7 Part 3: Segments and Sections - Where Everything Lives
&lt;/h3&gt;&lt;p&gt;Segments are large regions of memory with specific purposes. Inside segments, you have sections - smaller, more specific areas.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Major Segments:&lt;/strong&gt;&lt;/p&gt;
&lt;h4 id="__-read--execute-never-writable"&gt;&lt;strong&gt;__TEXT Segment&lt;/strong&gt; (Read + Execute, never writable)
&lt;/h4&gt;&lt;p&gt;This is where your actual code lives, along with read-only data:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;__TEXT Segment
├── __text → Your actual machine code
├── __stubs → Stubs for dynamic library calls
├── __stub_helper → Helper code for lazy binding
├── __cstring → C string literals (&amp;#34;Hello, World!&amp;#34;)
├── __const → Constant data
└── __unwind_info → Exception handling information
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;Why __TEXT is Read-Only:&lt;/strong&gt;&lt;br&gt;
Security. Modern systems use W^X (Write XOR Execute) - memory is either writable OR executable, never both. This prevents attackers from injecting and running malicious code.&lt;/p&gt;
&lt;h4 id="__-read--write-not-executable"&gt;&lt;strong&gt;__DATA Segment&lt;/strong&gt; (Read + Write, not executable)
&lt;/h4&gt;&lt;p&gt;This holds your program&amp;rsquo;s variables and mutable data:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;__DATA Segment
├── __data → Initialized global/static variables
├── __bss → Uninitialized variables (zeroed at load)
├── __common → Uninitialized external variables
├── __const → Data marked const but needs relocation
└── __objc_* → Objective-C runtime data (classes, methods)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;Memory Efficiency:&lt;/strong&gt;&lt;br&gt;
Multiple processes can share the same __TEXT segment in memory (it&amp;rsquo;s read-only), but each process gets its own __DATA segment (it&amp;rsquo;s writable and unique per process).&lt;/p&gt;
&lt;h4 id="__-read-only"&gt;&lt;strong&gt;__LINKEDIT Segment&lt;/strong&gt; (Read-only)
&lt;/h4&gt;&lt;p&gt;This is the &amp;ldquo;metadata&amp;rdquo; segment containing information for dyld and debuggers:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;__LINKEDIT Segment
├── Symbol table → Function and variable names
├── String table → Actual string data for symbols
├── Indirect symbols → Information for dynamic linking
├── Relocations → Address fixup information
└── Code signature → Cryptographic signature data
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;Fun Fact:&lt;/strong&gt;&lt;br&gt;
The __LINKEDIT segment can be stripped to reduce file size, but you lose debugging symbols and some dynamic linking capabilities.&lt;/p&gt;
&lt;h3 id="48-segments-vs-sections---the-hierarchy"&gt;4.8 Segments vs Sections - The Hierarchy
&lt;/h3&gt;&lt;p&gt;The relationship is straightforward:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Segments&lt;/strong&gt; define large memory regions with uniform protection&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sections&lt;/strong&gt; subdivide segments into specific data types&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Example structure:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;__TEXT Segment (r-x protection)
 ├── __text section (actual code)
 └── __cstring section (string literals)

__DATA Segment (rw- protection)
 ├── __data section (initialized vars)
 └── __bss section (uninitialized vars)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Inspect segments and sections:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;otool -l /bin/ls &lt;span class="p"&gt;|&lt;/span&gt; grep -A3 &lt;span class="s2"&gt;&amp;#34;sectname\|segname&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# or use a more user-friendly tool&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;jtool2 -l /bin/ls &lt;span class="c1"&gt;# if you have it installed&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="49-the-linking-process---connecting-the-dots"&gt;4.9 The Linking Process - Connecting the Dots
&lt;/h3&gt;&lt;p&gt;When dyld loads your executable, it has to resolve all external references. Your code calls functions in system libraries, but those aren&amp;rsquo;t embedded in your binary.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Two Types of Binding:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1. Lazy Binding&lt;/strong&gt; (the default)&lt;br&gt;
Functions are resolved only when first called. This speeds up launch time.&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;Your code calls printf()
 ↓
Jump to stub in __stubs
 ↓
Stub jumps to __stub_helper
 ↓
Helper calls dyld to resolve printf
 ↓
dyld finds printf in libSystem.dylib
 ↓
Updates the stub to point directly to printf
 ↓
Future calls go directly to printf (no dyld overhead)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;2. Eager Binding&lt;/strong&gt;&lt;br&gt;
All symbols resolved at launch (slower startup, but predictable behavior).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Lazy Symbol Pointer Table:&lt;/strong&gt;&lt;br&gt;
Located in __DATA, this table initially points to stub helpers. After first call, it&amp;rsquo;s updated to point directly to the resolved function. This is why the first call to a function can be slightly slower than subsequent calls.&lt;/p&gt;
&lt;h3 id="410-practical-example-dissecting-binls"&gt;4.10 Practical Example: Dissecting /bin/ls
&lt;/h3&gt;&lt;p&gt;Let&amp;rsquo;s analyze a real executable:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# What type of file?&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;file /bin/ls
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# Mach-O 64-bit executable arm64&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# Check the header&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;otool -h /bin/ls
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# Shows architecture, file type, and flags&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# What libraries does it need?&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;otool -L /bin/ls
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# /usr/lib/libutil.dylib&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# /usr/lib/libncurses.5.4.dylib&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# /usr/lib/libSystem.B.dylib&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# What segments exist?&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;otool -l /bin/ls &lt;span class="p"&gt;|&lt;/span&gt; grep -A3 &lt;span class="s2"&gt;&amp;#34;cmd LC_SEGMENT&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# __TEXT, __DATA, __LINKEDIT&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# Extract symbols&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;nm /bin/ls &lt;span class="p"&gt;|&lt;/span&gt; head -20
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# Shows function names, addresses, and types&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="411-code-signing-and-mach-o"&gt;4.11 Code Signing and Mach-O
&lt;/h3&gt;&lt;p&gt;Every Mach-O executable on modern macOS is cryptographically signed. The signature lives in the __LINKEDIT segment (specified by LC_CODE_SIGNATURE load command).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What Gets Signed:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The entire __TEXT segment (code)&lt;/li&gt;
&lt;li&gt;Critical parts of other segments&lt;/li&gt;
&lt;li&gt;Info.plist (for bundles)&lt;/li&gt;
&lt;li&gt;Entitlements (special permissions)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Verification at Runtime:&lt;/strong&gt;&lt;br&gt;
Before executing any page of code, the kernel verifies its signature. If anything has been modified, execution is blocked.&lt;/p&gt;
&lt;p&gt;Check a signature:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;codesign -dv /bin/ls
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# Shows signature status, team ID, signing date&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;codesign --verify --verbose&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="m"&gt;4&lt;/span&gt; /bin/ls
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# Detailed verification&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="412-security-features-in-modern-mach-o"&gt;4.12 Security Features in Modern Mach-O
&lt;/h3&gt;&lt;p&gt;Modern macOS executables include several security enhancements:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Address Space Layout Randomization (ASLR):&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Marked by MH_PIE flag in header&lt;/li&gt;
&lt;li&gt;Executable loads at a random base address each run&lt;/li&gt;
&lt;li&gt;Makes exploitation much harder (attacker can&amp;rsquo;t predict addresses)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Stack Canaries:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Compiler inserts random values on the stack&lt;/li&gt;
&lt;li&gt;Checked before function returns&lt;/li&gt;
&lt;li&gt;Detects buffer overflow attacks&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Pointer Authentication Codes (PAC)&lt;/strong&gt; - Apple Silicon Only:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;CPU-level feature that cryptographically signs pointers&lt;/li&gt;
&lt;li&gt;Indicated by arm64e architecture&lt;/li&gt;
&lt;li&gt;Makes code injection nearly impossible&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Library Validation:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;LC_VERSION_MIN_* commands specify minimum OS version&lt;/li&gt;
&lt;li&gt;Prevents loading on older, unpatched systems&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="413-tools-for-mach-o-analysis"&gt;4.13 Tools for Mach-O Analysis
&lt;/h3&gt;&lt;p&gt;Essential tools for working with Mach-O files:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Built-in Tools:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;file &lt;span class="c1"&gt;# Identify file type&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;otool &lt;span class="c1"&gt;# Object file displaying tool (Apple&amp;#39;s objdump)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;nm &lt;span class="c1"&gt;# List symbols&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;lipo &lt;span class="c1"&gt;# Manipulate universal binaries&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;codesign &lt;span class="c1"&gt;# Code signing operations&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;pagestuff &lt;span class="c1"&gt;# Display logical pages&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Third-Party Tools:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;jtool2&lt;/strong&gt;: Modern, powerful alternative to otool&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MachOView&lt;/strong&gt;: GUI app for visual exploration&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hopper/IDA Pro&lt;/strong&gt;: Disassemblers that understand Mach-O deeply&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LIEF&lt;/strong&gt;: Library for parsing/modifying Mach-O files programmatically&lt;/li&gt;
&lt;/ul&gt;</description></item></channel></rss>