<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sruthi K on Medium]]></title>
        <description><![CDATA[Stories by Sruthi K on Medium]]></description>
        <link>https://medium.com/@sruthk?source=rss-5b20471afcf3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GgtL3bliOpoUyaQ2c0xxWQ@2x.jpeg</url>
            <title>Stories by Sruthi K on Medium</title>
            <link>https://medium.com/@sruthk?source=rss-5b20471afcf3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 10:06:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sruthk/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Cracking Assembly — Reversing a simple function]]></title>
            <link>https://medium.com/@sruthk/cracking-assembly-reversing-a-simple-function-33c82d2bb759?source=rss-5b20471afcf3------2</link>
            <guid isPermaLink="false">https://medium.com/p/33c82d2bb759</guid>
            <category><![CDATA[x86]]></category>
            <category><![CDATA[assembly-language]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[reverse-engineering]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <dc:creator><![CDATA[Sruthi K]]></dc:creator>
            <pubDate>Mon, 27 Feb 2023 13:52:18 GMT</pubDate>
            <atom:updated>2023-02-27T13:52:51.644Z</atom:updated>
            <content:encoded><![CDATA[<h3>Cracking Assembly — Reversing a simple function</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*BTBSvd_zpnQzYygJ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@alexkixa?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Alexandre Debiève</a> on <a href="https://unsplash.com/s/photos/motherboard?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>This is the sixteenth article in the <a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-assembly-language-a4ad14e601a1">Cracking Assembly</a> series. Here is the list of published articles in the series. I would recommend you read them in the below mentioned order. I will be updating this list as new articles are published.</p><p><a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-assembly-language-a4ad14e601a1">Cracking Assembly — Introduction to Assembly Language</a><br><a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-windbg-preview-bd779ad841ed">Cracking Assembly — Introduction to Windbg Preview</a><br><a href="https://medium.com/@sruthk/cracking-assembly-x86-and-x64-architecture-3328484f67ae">Cracking Assembly — x86 and x64 architecture</a><br><a href="https://medium.com/@sruthk/cracking-assembly-cpu-registers-e385a67f6f11">Cracking Assembly — CPU Registers</a><br><a href="https://medium.com/@sruthk/cracking-assembly-x86-addressing-modes-ea8cc0d5bd12">Cracking Assembly — x86 addressing modes</a><br><a href="https://medium.com/@sruthk/cracking-assembly-functional-classification-of-assembly-instructions-78de2b613e94">Cracking Assembly — Functional classification of assembly instructions</a><br><a href="https://medium.com/@sruthk/cracking-assembly-stack-operations-63a279ae60d5">Cracking Assembly — Stack Operations</a><br><a href="https://medium.com/@sruthk/cracking-assembly-accessing-local-variables-in-x86-vs-x64-eb018ce1ef2a">Cracking Assembly — Accessing local variables in x86 vs x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-calling-conventions-in-x86-cc035d66f3f3">Cracking Assembly — Calling conventions in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-fastcall-calling-convention-in-x64-c6d77b51ea86">Cracking Assembly — fastcall calling convention in x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-function-prolog-and-epilog-in-x86-cb3c3461bcd3">Cracking Assembly — Function Prolog and Epilog in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-function-prolog-and-epilog-in-x64-ea1bdc50514c">Cracking Assembly — Function Prolog and Epilog in x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-stack-frame-layout-in-x86-3ac46fa59c">Cracking Assembly — Stack frame layout in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-walking-the-call-stack-in-x86-5693d836ffe">Cracking Assembly — Walking the call stack in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-stack-frame-layout-in-x64-75eb862dde08">Cracking Assembly — Stack Frame Layout in x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-reversing-a-simple-function-33c82d2bb759">Cracking Assembly — Reversing a simple function</a></p><p>So far in this series, we have discussed several concepts in x86 and x64 assembly. Let us now use all of these theoretical knowledge we have discussed and attempt to reverse a simple function from its assembly language code.</p><h3>A simple function</h3><p>Take a look at the below assembly language instructions. This is the assembly instructions for a function with the name <strong>func1 </strong>inside the exe sample.exe.</p><pre>0:000&gt; uf sample!func1<br>sample!func1:<br>00cc8300 55              push    ebp<br>00cc8301 8bec            mov     ebp,esp<br>00cc8303 83ec08          sub     esp,8<br>00cc8306 c745f801000000  mov     dword ptr [ebp-8],1<br>00cc830d 8b4508          mov     eax,dword ptr [ebp+8]<br>00cc8310 8945fc          mov     dword ptr [ebp-4],eax<br>00cc8313 eb09            jmp     sample!func1+0x1e (00cc831e)  Branch<br><br>sample!func1+0x15:<br>00cc8315 8b4dfc          mov     ecx,dword ptr [ebp-4]<br>00cc8318 83e901          sub     ecx,1<br>00cc831b 894dfc          mov     dword ptr [ebp-4],ecx<br><br>sample!func1+0x1e:<br>00cc831e 837dfc00        cmp     dword ptr [ebp-4],0<br>00cc8322 7e0c            jle     sample!func1+0x30 (00cc8330)  Branch<br><br>sample!func1+0x24:<br>00cc8324 8b55f8          mov     edx,dword ptr [ebp-8]<br>00cc8327 0faf55fc        imul    edx,dword ptr [ebp-4]<br>00cc832b 8955f8          mov     dword ptr [ebp-8],edx<br>00cc832e ebe5            jmp     sample!func1+0x15 (00cc8315)  Branch<br><br>sample!func1+0x30:<br>00cc8330 8b45f8          mov     eax,dword ptr [ebp-8]<br>00cc8333 8be5            mov     esp,ebp<br>00cc8335 5d              pop     ebp<br>00cc8336 c3              ret</pre><p>Our task is to figure out what this function does i.e to understand the function logic without access to the actual symbols or code itself.</p><p>Ready? Lets get started!</p><p>Let us first break down the assembly code for func1 into prolog, epilog and function body.</p><p>Here is the Prolog</p><pre>00cc8300 55              push    ebp<br>00cc8301 8bec            mov     ebp,esp<br>00cc8303 83ec08          sub     esp,8</pre><p>Here is the Epilog</p><pre>00cc8333 8be5            mov     esp,ebp<br>00cc8335 5d              pop     ebp<br>00cc8336 c3              ret</pre><p>And here is the function body:</p><pre>00cc8306 c745f801000000  mov     dword ptr [ebp-8],1<br>00cc830d 8b4508          mov     eax,dword ptr [ebp+8]<br>00cc8310 8945fc          mov     dword ptr [ebp-4],eax<br>00cc8313 eb09            jmp     sample!func1+0x1e (00cc831e)  Branch<br><br>sample!func1+0x15:<br>00cc8315 8b4dfc          mov     ecx,dword ptr [ebp-4]<br>00cc8318 83e901          sub     ecx,1<br>00cc831b 894dfc          mov     dword ptr [ebp-4],ecx<br><br>sample!func1+0x1e:<br>00cc831e 837dfc00        cmp     dword ptr [ebp-4],0<br>00cc8322 7e0c            jle     sample!func1+0x30 (00cc8330)  Branch<br><br>sample!func1+0x24:<br>00cc8324 8b55f8          mov     edx,dword ptr [ebp-8]<br>00cc8327 0faf55fc        imul    edx,dword ptr [ebp-4]<br>00cc832b 8955f8          mov     dword ptr [ebp-8],edx<br>00cc832e ebe5            jmp     sample!func1+0x15 (00cc8315)  Branch<br><br>sample!func1+0x30:<br>00cc8330 8b45f8          mov     eax,dword ptr [ebp-8]</pre><h3>Finding the architecture being used —Is it x86 or x64?</h3><p>Taking a look at the instructions, what do you think sample.exe is— is this an x86 executable or a x64 one?</p><p>As you might have noticed, all of the registers used in this sample are the x86 <a href="https://medium.com/@sruthk/cracking-assembly-cpu-registers-e385a67f6f11">registers </a>(EBP, ESP, ECX etc instead of RBP, RSP etc). Also, if you look at the function’s code above, you would notice that the <a href="https://medium.com/@sruthk/cracking-assembly-function-prolog-and-epilog-in-x86-cb3c3461bcd3">prolog and epilog</a> saves and restores the EBP register. Based on these two findings, we can say that this sample uses x86 architecture.</p><h3>Finding the calling convention used</h3><p>In <a href="https://medium.com/@sruthk/cracking-assembly-calling-conventions-in-x86-cc035d66f3f3">this </a>article, I explained the various calling conventions that can be used in x86 function calls. Can you now find out which calling convention func1() is using?</p><p>Take a look at the Epilog for func1. The last line in the Epilog is a simple RET instruction, without any numbers following it. This means that the caller function is responsible for cleaning up the stack space allocated for parameters passed to func1. What does that tell us? It means this function is using __cdecl calling convention. Refer <a href="https://medium.com/@sruthk/cracking-assembly-calling-conventions-in-x86-cc035d66f3f3">this </a>article if you have any doubts on this.</p><p>Summarizing the findings so far, we know that:</p><ul><li>func1() is an x86 function which implies, local variables are accessed as negative offset from EBP</li><li>func1() uses __cdecl calling convention which means parameters would be pushed to the stack before pushing the return address to the stack</li></ul><h3>Decoding the function body</h3><p>Let us now analyze the function body and see if we can decode its logic. Here are the first few lines in the function body :</p><pre>00cc8306 c745f801000000  mov     dword ptr [ebp-8],1<br>00cc830d 8b4508          mov     eax,dword ptr [ebp+8]<br>00cc8310 8945fc          mov     dword ptr [ebp-4],eax<br>00cc8313 eb09            jmp     sample!func1+0x1e (00cc831e)  Branch</pre><p>The first line in the above snippet moves the value 1 to the address EBP-8. Remember, in x86 local variables of a function are accessed as negative offsets from EBP. This means EBP-8 here is one of the local variables of func1 and we initialize it to 1.</p><p>The second line in the above snippet moves the value at address EBP+8 to EAX. Remember, since this is x86 and also since the function uses __cdecl convention, EBP+8 will contain the first parameter passed to a function. This means, here, EBP+8 contains the first parameter passed to func1 and we are moving it to EAX in this instruction.</p><p>The third instruction above copies this value of the first parameter to func1 to EBP-4 which is another local variable of func1. This means, we are copying the value of parameter to local variable.</p><p>Thus, now we know that func1 has two local variables, first one at EBP-8 (Let us call it ‘<strong>a</strong>’) and another at EBP-4 (let us call it ‘<strong>b</strong>’)</p><p>The 4th instruction above says we are now going to jump to the instruction at address 00cc831e. Remember, this is an unconditional jump, which means we are anyways going to jump to the address 00cc831e and execute the instructions.</p><p>Let us examine the block of instructions starting from address 00cc831e</p><pre>sample!func1+0x1e:<br>00cc831e 837dfc00        cmp     dword ptr [ebp-4],0<br>00cc8322 7e0c            jle     sample!func1+0x30 (00cc8330)  Branch<br><br>sample!func1+0x24:<br>00cc8324 8b55f8          mov     edx,dword ptr [ebp-8]<br>00cc8327 0faf55fc        imul    edx,dword ptr [ebp-4]<br>00cc832b 8955f8          mov     dword ptr [ebp-8],edx<br>00cc832e ebe5            jmp     sample!func1+0x15 (00cc8315)  Branch<br><br>sample!func1+0x30:<br>00cc8330 8b45f8          mov     eax,dword ptr [ebp-8]</pre><p>The first instruction (<strong>cmp dword ptr [ebp-4],0</strong>) is a comparison instruction. Here we are checking if the local variable <strong>b </strong>is equal to 0.</p><p>The next instruction (<strong>jle sample!func1+0x30 (00cc8330)</strong>) is a conditional jump. It means, if the destination operand of the <strong>CMP </strong>instruction before it is less than or equal to the source operand, then jump to the address specified<strong>. </strong>In this case it means, if b ≤ 0, then jump to <strong>00cc8330.</strong></p><p>Examining the instruction at address <strong>00cc8330, </strong>we see that it is a move statement which copies the value of local variable “a” to EAX and then we have the Epilog after that which means this is the exit path. That is, if b ≤0, the function does a “return a” and exits.</p><p>So what if b &gt; 0? For that, let us examine the next instruction after the conditional jump JLE. If b&gt;0, the next instruction i.e <strong>mov edx,dword ptr [ebp-8</strong>] would be executed. This instruction copies the value of “a” to EDX</p><p>The next instruction (<strong>imul edx,dword ptr [ebp-4]</strong>) multiplies value inside EDX with “b” and stores the result to EDX. So, we can say, its equivalent to something like a= a*b</p><p>The next instruction (<strong>mov dword ptr [ebp-8],edx</strong>) simply moves the new value of “a” to the stack location for the local variable.</p><p>The next instruction (<strong>jmp sample!func1+0x15 (00cc8315)</strong>) is again an unconditional jump to the address <strong>00cc8315</strong></p><p>Let us now check the next block of instructions from the address <strong>00cc8315</strong></p><pre>sample!func1+0x15:<br>00cc8315 8b4dfc          mov     ecx,dword ptr [ebp-4]<br>00cc8318 83e901          sub     ecx,1<br>00cc831b 894dfc          mov     dword ptr [ebp-4],ecx</pre><p>This block looks like the part of a loop. Want to know why? Let us check the instructions one by one.</p><p>The first instruction (<strong>mov ecx,dword ptr [ebp-4]</strong>) copies the value of ‘b’ to ECX. The second instruction (<strong>sub ecx,1</strong>) subtracts ECX by 1. <br>And the third instruction (<strong>mov dword ptr [ebp-4],ecx</strong>) copies the new value of “b” back to ECX. In effect, this is equivalent to “<strong>b = b -1</strong>”</p><p>After this the following block gets executed which we already saw:</p><pre>sample!func1+0x1e:<br>00cc831e 837dfc00        cmp     dword ptr [ebp-4],0<br>00cc8322 7e0c            jle     sample!func1+0x30 (00cc8330)  Branch</pre><p>This block compares if b ≥ 0 and decides whether to a with new value of b and continue or to go to the exit path.</p><p>Reading all this together, we can safely say that the function does the following logic:</p><pre>Set a = 1<br>Set b = parameter<br>Do b = b - 1<br>Check if b&gt;0<br>If yes, do a = a * b<br>Else, return a</pre><p>which is equivalent to:</p><pre>Set a = 1<br>for(b = parameter; b&gt; 0; b--) <br>    a = a * b<br>Return a</pre><p>So what does our function func1 do? Well, it calculates the factorial of a number and returns it to the caller. That is, if you pass number ’n’, it will calculate n! and return it.</p><p>Congratulations, you have successfully reversed a function :) Hope you found this useful.</p><p>If you found this article helpful, you can follow <a href="https://medium.com/@sruthk">me</a>/my series “<a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-assembly-language-a4ad14e601a1">Cracking Assembly</a>” for more such articles on assembly language.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=33c82d2bb759" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cracking Assembly — Stack Frame Layout in x64]]></title>
            <link>https://medium.com/@sruthk/cracking-assembly-stack-frame-layout-in-x64-75eb862dde08?source=rss-5b20471afcf3------2</link>
            <guid isPermaLink="false">https://medium.com/p/75eb862dde08</guid>
            <category><![CDATA[malware-analysis]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[assembly-language]]></category>
            <category><![CDATA[x64]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <dc:creator><![CDATA[Sruthi K]]></dc:creator>
            <pubDate>Mon, 20 Feb 2023 12:52:15 GMT</pubDate>
            <atom:updated>2023-03-04T06:52:42.427Z</atom:updated>
            <content:encoded><![CDATA[<h3>Cracking Assembly — Stack Frame Layout in x64</h3><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/0*Eh5ssNPtk8kpfrdw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@alexkixa?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Alexandre Debiève</a> on <a href="https://unsplash.com/s/photos/motherboard?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>This is the fifteenth article in the <a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-assembly-language-a4ad14e601a1">Cracking Assembly</a> series. Here is the list of published articles in the series. I would recommend you read them in the below mentioned order. I will be updating this list as new articles are published.</p><p><a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-assembly-language-a4ad14e601a1">Cracking Assembly — Introduction to Assembly Language</a><br><a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-windbg-preview-bd779ad841ed">Cracking Assembly — Introduction to Windbg Preview</a><br><a href="https://medium.com/@sruthk/cracking-assembly-x86-and-x64-architecture-3328484f67ae">Cracking Assembly — x86 and x64 architecture</a><br><a href="https://medium.com/@sruthk/cracking-assembly-cpu-registers-e385a67f6f11">Cracking Assembly — CPU Registers</a><br><a href="https://medium.com/@sruthk/cracking-assembly-x86-addressing-modes-ea8cc0d5bd12">Cracking Assembly — x86 addressing modes</a><br><a href="https://medium.com/@sruthk/cracking-assembly-functional-classification-of-assembly-instructions-78de2b613e94">Cracking Assembly — Functional classification of assembly instructions</a><br><a href="https://medium.com/@sruthk/cracking-assembly-stack-operations-63a279ae60d5">Cracking Assembly — Stack Operations</a><br><a href="https://medium.com/@sruthk/cracking-assembly-accessing-local-variables-in-x86-vs-x64-eb018ce1ef2a">Cracking Assembly — Accessing local variables in x86 vs x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-calling-conventions-in-x86-cc035d66f3f3">Cracking Assembly — Calling conventions in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-fastcall-calling-convention-in-x64-c6d77b51ea86">Cracking Assembly — fastcall calling convention in x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-function-prolog-and-epilog-in-x86-cb3c3461bcd3">Cracking Assembly — Function Prolog and Epilog in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-function-prolog-and-epilog-in-x64-ea1bdc50514c">Cracking Assembly — Function Prolog and Epilog in x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-stack-frame-layout-in-x86-3ac46fa59c">Cracking Assembly — Stack frame layout in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-walking-the-call-stack-in-x86-5693d836ffe">Cracking Assembly — Walking the call stack in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-stack-frame-layout-in-x64-75eb862dde08">Cracking Assembly — Stack Frame Layout in x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-reversing-a-simple-function-33c82d2bb759">Cracking Assembly — Reversing a simple function</a></p><p><a href="https://medium.com/@sruthk/cracking-assembly-stack-frame-layout-in-x86-3ac46fa59c">Here</a>, we discussed how stack frame is laid out in x86 environment during function calls. Today, we will discuss the details of stack frame layout in x64 and understand how it differs from that of x86.</p><p>In <a href="https://medium.com/@sruthk/cracking-assembly-function-prolog-and-epilog-in-x64-ea1bdc50514c">this </a>article, we explained the concept of parameter homing, volatile and non volatile registers as well as the function prolog for x64 systems. Here is a summary of the important points we have to remember when we talk about x64 stack frames:</p><ul><li>x64 omits Frame pointer usage and relies on RSP (Stack pointer) alone for stack operations</li><li>RSP does not change between prolog and epilog of the function</li><li>Local variables are accessed as positive offsets from the current RSP. Refer <a href="https://medium.com/@sruthk/cracking-assembly-accessing-local-variables-in-x86-vs-x64-eb018ce1ef2a">this </a>to know more.</li><li>RSP of the previous frame is not stored on the stack unlike EBP in the case of x86 systems.</li><li>Every non-leaf function will reserve Parameter homing space for four register parameters (RCX, RDX, R8, R9) even if the function passes fewer parameters. Refer <a href="https://medium.com/@sruthk/cracking-assembly-function-prolog-and-epilog-in-x64-ea1bdc50514c">this </a>to know more.</li></ul><p>Keeping all these in mind, let us unfold the stack frame layout in x64</p><h3>Stack Frame layout in x64</h3><p>Let us imagine we have two non-leaf functions A, B where A calls B inside its code, the stack frame layout for these two functions will be as shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/653/1*cp5dIeqM08CyApmrP9xn-A.png" /><figcaption>Fig 1 — Stack Frame Layout when a function A calls function B</figcaption></figure><p>Let us visualize this with the help of the debugger Windbg Preview. You can refer to <a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-windbg-preview-bd779ad841ed">this </a>article for details on how to use WinDbg Preview.</p><p>For our example here, I am using the below C program.</p><pre>#include &lt;stdio.h&gt;<br><br><br>int add(int num1, int num2, int num3, int num4, int num5)<br>{<br>   int sum = 0;<br>   <br>   sum = num1 + num2 + num3 + num4 + num5;<br>   <br>   return sum;<br>}<br><br>void main()<br>{<br>   int number1 = 100;<br>   int number2 = 200;<br>   int number3 = 300;<br>   int number4 = 400;<br>   int number5 = 500;<br>   <br>   int result = 0;<br>   <br>   result = add(number1,number2, number3, number4, number5);<br>   <br>   printf(&quot;Sum = %d\n&quot;,result);<br>   return;<br>}</pre><p>This program has a main() and add(). The main() passes 5 parameters to add(). The add() adds all these 5 numbers and returns the sum. Compile this program and generate the symbols (PDB files). Refer to <a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-windbg-preview-bd779ad841ed">this</a> article if you do not know how to do that. Launch the exe through Windbg Preview and give the relevant symbol file paths.</p><p>I saved this program as “callstackx64.exe”. Let us first un-assemble the add() using the uf command as follows:</p><pre>uf callstackx64!add</pre><p>And here is the output of the above command:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dhFm8UpcfM-h6yrcGJ1HWw.png" /><figcaption>Fig 2 — Dis-assembly of add()</figcaption></figure><p>Let us choose an instruction address after the prolog and set our breakpoint so that we can break in to the debugger when the instruction is encountered and check the stack frame. To set breakpoint, you can use the bp command as follows:</p><pre>bp &lt;address to break at&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NHQw2od8Klzcio_xDRzNPA.png" /><figcaption>Fig 3 — Setting breakpoint using bp command</figcaption></figure><p>Now run the program through the debugger using the ‘g’ command. It will break into the debugger when the breakpoint is hit as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pm5SHEwTMz77xMyR_S_B_w.png" /><figcaption>Fig 4 — Breakpoint hit in debugger</figcaption></figure><p>Use the “k” command to view the current call stack as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iINZ-RPjEHb7_hY0cEJdnA.png" /><figcaption>Fig 5 — Output of k command displaying stack details</figcaption></figure><p>The above output shows the RSP for each frame, the return addresses as well as the call stack. As per this output, we are currently inside the function add()</p><p>We can use the “r” command to view the value of each register for the current frame as below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PMUVwWRA_Al7pMqcTQbPCg.png" /><figcaption>Fig 6 — Dumping registers of current frame using ‘r’ command</figcaption></figure><p>The RSP for add() is 0000006eb92ff950 as per this. This is the top of the current stack frame for add().</p><h3>Retrieving Local Variables</h3><p>Remember the stack layout diagram in Fig 1 above. add() is a leaf function and hence it will not have any space allocation in the stack frame for add() for parameter homing as well as stack parameters. However it should have the space allocated for its local variable (In this case, add() has only one local variable i.e sum). Hence, the local variable ‘sum’ will be stored at the RSP for add().</p><p>Use the “dq” i.e Dump QuadWord command to dump the current value of Local Variable sum:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ux0ONPOyG2IzLN95k1coNw.png" /><figcaption>Fig 7 — Dumping Local variable of add()</figcaption></figure><p>Remember, we broke into the debugger before calculating the sum and hence the current value of the local variable sum is 0 as seen above.</p><h3>Retrieving the return address from stack</h3><p>From figure 2, we know that in the function prolog for add(), it subtracts 0x18 from the previous RSP to get the new RSP for add().</p><p>Return address of add() would hence be stored at the address current RBP + 0x18 (Why? Since CALL instruction does an implicit PUSH operation and pushed the return address to the stack before calling the function). Use dq command and dump the return address as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*urUB_GJ4rHFEhqJk4TteuQ.png" /><figcaption>Fig 8 — Dumping return address of add()</figcaption></figure><h3>Retrieving register and stack parameters passed by the caller</h3><p>Homing space for parameters passed through registers are seen in the stack area before the return address. Refer to Fig 1 to get more clarity.</p><p>Based on this, parameter passed in RCX would be seen at “current RSP + bytes allocated for stack frame for current function in its prolog + 8”. Why “+ 8”? That is the stack space taken to store Return address which we dumped above.</p><p>Using this, let us dump parameter homing space for parameter passed in RCX as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1013/1*mAZGu1E4MoTXbL3SdEGAdg.png" /><figcaption>Fig 9 — Retrieving value of first parameter at RCX homing space</figcaption></figure><p>0x64 is equivalent to decimal value 100 which is the first parameter passed to add() by main()</p><p>Similarly we can dump out the other register parameters RDX, R8 and R9 by adding 8 bytes each (each address location is 8 bytes in x64) as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AZllnuMTiQIjeNEPBiPLTA.png" /><figcaption>Fig 10 — Retrieving other register parameters by dumping the homing space from stack</figcaption></figure><p>That’s all about stack frame layout in x64. Stay Tuned for more topics on assembly language!</p><p>If you found this article helpful, you can follow <a href="https://medium.com/@sruthk">me</a>/my series “<a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-assembly-language-a4ad14e601a1">Cracking Assembly</a>” for more such articles on assembly language.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=75eb862dde08" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cracking Assembly — Walking the call stack in x86]]></title>
            <link>https://medium.com/@sruthk/cracking-assembly-walking-the-call-stack-in-x86-5693d836ffe?source=rss-5b20471afcf3------2</link>
            <guid isPermaLink="false">https://medium.com/p/5693d836ffe</guid>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[reverse-engineering]]></category>
            <category><![CDATA[assembly-language]]></category>
            <category><![CDATA[x86]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <dc:creator><![CDATA[Sruthi K]]></dc:creator>
            <pubDate>Mon, 13 Feb 2023 11:57:05 GMT</pubDate>
            <atom:updated>2023-03-04T06:52:18.053Z</atom:updated>
            <content:encoded><![CDATA[<h3>Cracking Assembly — Walking the call stack in x86</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*qd5aI45_d0qCtMQA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@alexkixa?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Alexandre Debiève</a> on <a href="https://unsplash.com/s/photos/motherboard?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>This is the fourteenth article in the <a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-assembly-language-a4ad14e601a1">Cracking Assembly</a> series. Here is the list of published articles in the series. I would recommend you read them in the below mentioned order. I will be updating this list as new articles are published.</p><p><a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-assembly-language-a4ad14e601a1">Cracking Assembly — Introduction to Assembly Language</a><br><a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-windbg-preview-bd779ad841ed">Cracking Assembly — Introduction to Windbg Preview</a><br><a href="https://medium.com/@sruthk/cracking-assembly-x86-and-x64-architecture-3328484f67ae">Cracking Assembly — x86 and x64 architecture</a><br><a href="https://medium.com/@sruthk/cracking-assembly-cpu-registers-e385a67f6f11">Cracking Assembly — CPU Registers</a><br><a href="https://medium.com/@sruthk/cracking-assembly-x86-addressing-modes-ea8cc0d5bd12">Cracking Assembly — x86 addressing modes</a><br><a href="https://medium.com/@sruthk/cracking-assembly-functional-classification-of-assembly-instructions-78de2b613e94">Cracking Assembly — Functional classification of assembly instructions</a><br><a href="https://medium.com/@sruthk/cracking-assembly-stack-operations-63a279ae60d5">Cracking Assembly — Stack Operations</a><br><a href="https://medium.com/@sruthk/cracking-assembly-accessing-local-variables-in-x86-vs-x64-eb018ce1ef2a">Cracking Assembly — Accessing local variables in x86 vs x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-calling-conventions-in-x86-cc035d66f3f3">Cracking Assembly — Calling conventions in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-fastcall-calling-convention-in-x64-c6d77b51ea86">Cracking Assembly — fastcall calling convention in x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-function-prolog-and-epilog-in-x86-cb3c3461bcd3">Cracking Assembly — Function Prolog and Epilog in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-function-prolog-and-epilog-in-x64-ea1bdc50514c">Cracking Assembly — Function Prolog and Epilog in x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-stack-frame-layout-in-x86-3ac46fa59c">Cracking Assembly — Stack frame layout in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-walking-the-call-stack-in-x86-5693d836ffe">Cracking Assembly — Walking the call stack in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-stack-frame-layout-in-x64-75eb862dde08">Cracking Assembly — Stack Frame Layout in x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-reversing-a-simple-function-33c82d2bb759">Cracking Assembly — Reversing a simple function</a></p><p>In the previous article, we discussed how stack frame layout in x86 looks like. In this article, we will talk about how to use this knowledge to build the call stack (its also termed as “walking the call stack”) manually.</p><p>A quick recap on some of the key points about x86 environment, that we have already discussed before in this series:</p><ul><li>EBP (Frame pointer) of the previous frame is stored at EBP of current frame.</li><li>Return address of a frame is stored at the stack address EBP(of the frame)+4</li></ul><p>Using this knowledge, let us now take a sample and see how we can walk the call stack for function calls.</p><p>NOTE: If a function is set to use FPO (Frame Pointer Omission, which is a compiler optimization technique), this technique of walking the call stack will break. We will discuss more about FPO later in this series to delve into this deeper.</p><h3>Manually walking the call stack in x86</h3><p>Let us consider the following sample code.</p><pre>#include &lt;stdio.h&gt;<br><br>void func4()<br>{<br> printf(&quot;Inside func4\n&quot;);<br> return;<br>}<br><br>void func3()<br>{<br> printf(&quot;Inside func3\n&quot;);<br> func4();<br> return;<br>}<br><br>void func2()<br>{<br> printf(&quot;Inside func2\n&quot;);<br> func3();<br> return;<br>}<br><br>void func1()<br>{<br> printf(&quot;Inside func1\n&quot;);<br> func2();<br> return;<br>}<br><br>void main()<br>{<br> func1();<br> return;<br>}</pre><p>This code is a sample to demonstrate function calls and call stacks. The main() calls func1() which calls func2(). func2() calls func3() which then calls func4(). I have saved this code as callstack.c and have compiled it to generate the executable named callstack.exe.</p><p>Let us now open this in Windbg Preview and see the function call stacks. Refer <a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-windbg-preview-bd779ad841ed">this </a>article to know how to create symbols, how to open the executable using Windbg Preview and how to set symbol paths for debugging.</p><p>Let us now use the uf command to un-assemble the leaf function func4 so that we can set a breakpoint somewhere inside func4 to visualize the whole call stack.</p><pre>uf &lt;module-name&gt;!&lt;function-name&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UQn6TpTNVT0j3gUnnjaYsA.png" /><figcaption>Fig 1 — using uf command to un-assemble func4</figcaption></figure><p>Let us pick the address of an instruction after the function prolog and set a breakpoint there. I am picking the address of the instruction which calls printf() in func4 to set out breakpoint as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VyFalMqrmD1mKTRkfYVR-Q.png" /><figcaption>Fig 2 — Setting breakpoint inside func4</figcaption></figure><p>Let us now run the program using the ‘g’ command and wait for the breakpoint to hit:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zrKCrHWssn1gB950sdHBaA.png" /><figcaption>Fig 3 — Breakpoint has been hit</figcaption></figure><p>Now that our breakpoint has been hit, let us examine the current callstack using the ‘k’ command:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xpBGfv3-wJVYuMRm0djK1Q.png" /><figcaption>Fig 4 — Dumping callstack using k command</figcaption></figure><p>As you can see in the call stack in the debugger output above, out main() called func1() which called func2() which called func3() and we are now at out breakpoint inside func4() before it calls printf()</p><p>In the above output, the debugger shows you the call stack i.e the functions being called and also shows you the return address for each function frame as well as the frame’s Frame Pointer (EBP). How does the debugger get this info? Let us do this manually using the information we have in hand.</p><p>We are now in frame 0 which is the frame for func4. Let us dump the value of registers in this frame using the .frame command. The .frame command sets the context to the frame that we specify. If you need help with any debugger command in Windbg, use the ‘.hh’ command to get help on any command you need as follows:</p><pre>.hh &lt;command you need help with&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KO2IWlZsUyBpH7cKo-pqBg.png" /><figcaption>Fig 5 — Setting the context to frame 0 using .frame command</figcaption></figure><p>The output above shows the values in all the registers. From the output, we get the value of EBP register for frame 0 i.e for func4. Let us use this EBP value to retrieve the EBP of the previous frame and the return address of each functions.</p><p>De-referencing EBP of frame 0 will give you the EBP for frame 1. You can use the dd command to dump the values at a 32 bit address. (dd stands for Dump DWORD)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/739/1*itpgVHWfO0iFQvTOWAO0kw.png" /><figcaption>Fig 6 — Dumping EBP of frame 0 to view EBP of frame 1</figcaption></figure><p>The address you see in the figure above ‘<strong>0030fc54</strong>&#39; is the EBP for frame 1.</p><p>Similarly de-referencing EBP+4 for frame 0 will give you the return address of frame 0 i.e the return address of func4. In the below output, the address <strong>003e71f5 </strong>is the return address for func4.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/771/1*9NFG1HjrxxqvwsGKyRgpUg.png" /><figcaption>Fig 7 — Using EBP+4 to get return address of func4</figcaption></figure><p>Un-assembling this return address, you will see that this address is from func3 and is the next instruction that would be executed when func4() returns to fun3.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/643/1*nUPMJ__2POxB1_uFUBP5ZA.png" /><figcaption>Fig 8 — un-assembling the instruction at return address</figcaption></figure><p>Now that you have EBP for frame 1, de-reference it to get the EBP for frame 2. As seen in the output below, the address <strong>0030fc5c </strong>is the EBP for frame 2</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/831/1*7hxznolsSd9a8EfJuMRMGw.png" /><figcaption>Fig 9 — Retrieving EBP of frame 2</figcaption></figure><p>De-referencing EBP(of frame 1)+4 will give you the return address for frame 1 i.e return address for func3.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/817/1*fzDYGaZ8ZMnXUgdrW8CI0w.png" /><figcaption>Fig 10 — Retrieving return address of func3</figcaption></figure><p>Un-assemble this return address and you will see that it is the next instruction in func2() that would be executed when func3() returns:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/761/1*7qYyXesR6lbonu-tmDLyqQ.png" /><figcaption>Fig 11 — un-assembling return address of func3 to see the instruction</figcaption></figure><p>Using the same concept we can dump EBP for frame 3(func1) and return address for func2():</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/827/1*ZeJRy2Yir_2JM2NQv7YITQ.png" /><figcaption>Fig 12 — Retrieving EBP of frame 3 and return address of func2</figcaption></figure><p>Use the EBP of frame 3 to dump the EBP for frame 4 and return address for func1 as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/883/1*6jDluQhT4G0MV3P6dgfdsw.png" /><figcaption>Fig 12 — Retrieving EBP of frame 4 and return address of func2</figcaption></figure><p>This way you can walk the call stack in x86 if you have the EBP of the topmost frame in the stack. Remember, as I said before, if any of these functions use FPO (Frame Pointer Omission) this method of stack walk will not work as EBP for those frames will be omitted. We will try to cover this in detail later in our series.</p><p>If you found this article helpful, you can follow <a href="https://medium.com/@sruthk">me</a>/my series “<a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-assembly-language-a4ad14e601a1">Cracking Assembly</a>” for more such articles on assembly language.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5693d836ffe" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cracking Assembly — Stack Frame Layout in x86]]></title>
            <link>https://medium.com/@sruthk/cracking-assembly-stack-frame-layout-in-x86-3ac46fa59c?source=rss-5b20471afcf3------2</link>
            <guid isPermaLink="false">https://medium.com/p/3ac46fa59c</guid>
            <category><![CDATA[x86]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[debugging]]></category>
            <category><![CDATA[assembly-language]]></category>
            <dc:creator><![CDATA[Sruthi K]]></dc:creator>
            <pubDate>Mon, 06 Feb 2023 16:40:42 GMT</pubDate>
            <atom:updated>2023-03-04T06:51:59.377Z</atom:updated>
            <content:encoded><![CDATA[<h3>Cracking Assembly — Stack Frame Layout in x86</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*Phio4npIJUMmASlN.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@alexkixa?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Alexandre Debiève</a> on <a href="https://unsplash.com/s/photos/motherboard?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>This is the thirteenth article in the <a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-assembly-language-a4ad14e601a1">Cracking Assembly</a> series. Here is the list of published articles in the series. I would recommend you read them in the below mentioned order. I will be updating this list as new articles are published.</p><p><a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-assembly-language-a4ad14e601a1">Cracking Assembly — Introduction to Assembly Language</a><br><a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-windbg-preview-bd779ad841ed">Cracking Assembly — Introduction to Windbg Preview</a><br><a href="https://medium.com/@sruthk/cracking-assembly-x86-and-x64-architecture-3328484f67ae">Cracking Assembly — x86 and x64 architecture</a><br><a href="https://medium.com/@sruthk/cracking-assembly-cpu-registers-e385a67f6f11">Cracking Assembly — CPU Registers</a><br><a href="https://medium.com/@sruthk/cracking-assembly-x86-addressing-modes-ea8cc0d5bd12">Cracking Assembly — x86 addressing modes</a><br><a href="https://medium.com/@sruthk/cracking-assembly-functional-classification-of-assembly-instructions-78de2b613e94">Cracking Assembly — Functional classification of assembly instructions</a><br><a href="https://medium.com/@sruthk/cracking-assembly-stack-operations-63a279ae60d5">Cracking Assembly — Stack Operations</a><br><a href="https://medium.com/@sruthk/cracking-assembly-accessing-local-variables-in-x86-vs-x64-eb018ce1ef2a">Cracking Assembly — Accessing local variables in x86 vs x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-calling-conventions-in-x86-cc035d66f3f3">Cracking Assembly — Calling conventions in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-fastcall-calling-convention-in-x64-c6d77b51ea86">Cracking Assembly — fastcall calling convention in x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-function-prolog-and-epilog-in-x86-cb3c3461bcd3">Cracking Assembly — Function Prolog and Epilog in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-function-prolog-and-epilog-in-x64-ea1bdc50514c">Cracking Assembly — Function Prolog and Epilog in x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-stack-frame-layout-in-x86-3ac46fa59c">Cracking Assembly — Stack frame layout in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-walking-the-call-stack-in-x86-5693d836ffe">Cracking Assembly — Walking the call stack in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-stack-frame-layout-in-x64-75eb862dde08">Cracking Assembly — Stack Frame Layout in x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-reversing-a-simple-function-33c82d2bb759">Cracking Assembly — Reversing a simple function</a></p><p>Here is a summary of the important points we have discussed so far in this series, related to stack frames on x86 systems:</p><ul><li>Stack grows from higher to lower addresses</li><li>x86 systems heavily depend on EBP (Frame Pointer) for stack frame operations</li><li>EBP of a frame points to the base of the stack frame</li><li>Local variables are accessed as negative offsets from the corresponding frame’s EBP</li><li>Return addresses and function parameters are accessed as positive offsets from EBP</li></ul><p>Let us now pick all these points together and take a look at how the overall stack frame looks like in x86 systems.</p><h3>Stack layout during function calls in x86</h3><p>The following image shows how a typical stack frame would look like during a function call.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x_iKL7wELK97w9OSD1MUWQ.png" /><figcaption>Fig 1 — Stack frame layout on function calls</figcaption></figure><p>As we know, stack grows from higher to lower addresses. When a function calls another function, it first pushes the parameters to the stack (depending on the calling convention). It then pushes the Return Address (CALL instruction implicitly does this as explained in <a href="https://medium.com/@sruthk/cracking-assembly-stack-operations-63a279ae60d5">this </a>article). Post this, the previous frame’s EBP value will be pushed to the stack as a part of the Function prolog we discussed <a href="https://medium.com/@sruthk/cracking-assembly-function-prolog-and-epilog-in-x86-cb3c3461bcd3">here</a>. This is where the called function’s stack frame starts and the new function’s EBP will point to this stack location.</p><p>x86 uses a Frame based Exception handling mechanism. If the function uses Structured Exception Handling (__try and __except blocks), the information about the frame’s exception handler is stored on the stack frame immediately below the new EBP. Below this comes the space for Local Variables and callee saved registers as per the <a href="https://medium.com/@sruthk/cracking-assembly-calling-conventions-in-x86-cc035d66f3f3">calling convention</a> used.</p><p>Keeping the above stack layout in mind, we can use the frame’s EBP to retrieve important information about each function in x86:</p><ul><li>De-referencing EBP will give you the previous EBP (i.e the value of EBP is the address where the previous frame’s EBP is stored)</li><li>EBP + 4 will be where the return address of the function is stored</li><li>EBP + 8 and above will be where the function’s parameters are saved (depending on calling convention)</li><li>Negative offsets from EBP is where the frame’s exception details, local variables and callee saved registers will be seen.</li></ul><p>Let us now visualize this with the help of a debugger, I am using Windbg Preview here. You can refer to <a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-windbg-preview-bd779ad841ed">this </a>article for details on how to use WinDbg Preview.</p><p>For our example here, I am using the below C program.</p><pre>#include &lt;stdio.h&gt;<br><br><br>int __fastcall add(int num1, int num2, int num3, int num4, int num5, int num6)<br>{<br>   int sum = 0;<br>   <br>   sum = num1 + num2 + num3 + num4 + num5 + num6;<br>   <br>   return sum;<br>}<br><br>void main()<br>{<br>   int number1 = 100;<br>   int number2 = 200;<br>   int number3 = 300;<br>   int number4 = 400;<br>   int number5 = 500;<br>   int number6 = 600;<br>   <br>   int result = 0;<br>   <br>   result = add(number1,number2, number3, number4, number5, number6);<br>   <br>   printf(&quot;Sum = %d\n&quot;,result);<br>   return;<br>}</pre><p>This program has a main() and add(). The main() passes 6 numbers to add(). The add() adds all these 6 numbers and returns the sum. The function add() here uses __fastcall convention. Compile this program and generate the symbols (PDB files). Refer to <a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-windbg-preview-bd779ad841ed">this</a> article if you do not know how to do that. Launch the exe through Windbg Preview and give the relevant symbol file paths.</p><h3>Setting a breakpoint in WinDbg Preview:</h3><p>Since we are launching the exe through the debugger, we have the option of breaking into the code based on the breakpoints we set and examine the registers and stack addresses at that point in time.</p><p>Use the following command to set a breakpoint at a specific function (upon entering a function)</p><pre>bp &lt;module or exe name&gt;!&lt;function name&gt;</pre><p>For example, I have saved the above program as x86fastcall.exe. I want to break in to the debugger as we enter the add(). For this, I will set the breakpoint as given below:</p><pre>bp x86fastcall!add</pre><p>Use the bl command to list all the breakpoints that are set in the debugger session. Here is the output of the bl command from my session:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*z2bPOUzhMjKHPVYDb34rlw.png" /><figcaption>Fig 2 — Listing breakpoints using bl command</figcaption></figure><p>Now let us run the application by typing the command ‘g’ and then hit enter. It will break in to the debugger when it hits the breakpoint we set above i.e upon entering the add():</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8kwzTGLGeQQbTrfEseq9BA.png" /><figcaption>Fig 3 —Encountered the breakpoint</figcaption></figure><p>As you see in the output above, we have now hit the breakpoint. It also dumps the register values at the same time. It also shows the current instruction that is executed, here, the instruction is PUSH EBP which is the first instruction of <a href="https://medium.com/@sruthk/cracking-assembly-function-prolog-and-epilog-in-x86-cb3c3461bcd3">Function Prolog</a> in x86. This means that we have broken into the debugger at the very beginning of the function add()</p><p>Use the command ‘k’ to view the call stack:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/782/1*-n57-wMcvOWFQFPwfVcvcg.png" /><figcaption>Fig 4 — Dumping call stack using k command</figcaption></figure><p>The output of ‘k’ command shows 4 columns.</p><ul><li># — The first column (#) is the stack frame number.</li><li>ChildEBP — This is the new EBP of the new frame</li><li>RetAddr — This is the return address for this function which means the address of the next instruction in the caller function that this function will return to upon its exit.</li><li>Function names with offset — The fourth column shows the function names which will give us an idea about the function call flow</li></ul><p>Let us move a bit further inside add() and break into the debugger. For that let us first dump the assembly code for add()</p><p>To dump the assembly code for a function, use the ‘uf’ command:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8T-olCJmn--x2ltSsH5kQg.png" /><figcaption>Fig 5 — Dumping disassembly of add() using uf command</figcaption></figure><p>Let us split this as follows:</p><p>Here is the function prolog for add():</p><pre>007771a0 55              push    ebp<br>007771a1 8bec            mov     ebp,esp<br>007771a3 83ec0c          sub     esp,0Ch</pre><p>Here is the Epilog</p><pre>007771cb 8be5            mov     esp,ebp<br>007771cd 5d              pop     ebp<br>007771ce c21000          ret     10h</pre><p>And here is the function body:</p><pre>007771a6 8955f4          mov     dword ptr [ebp-0Ch],edx<br>007771a9 894df8          mov     dword ptr [ebp-8],ecx<br>007771ac c745fc00000000  mov     dword ptr [ebp-4],0<br>007771b3 8b45f8          mov     eax,dword ptr [ebp-8]<br>007771b6 0345f4          add     eax,dword ptr [ebp-0Ch]<br>007771b9 034508          add     eax,dword ptr [ebp+8]<br>007771bc 03450c          add     eax,dword ptr [ebp+0Ch]<br>007771bf 034510          add     eax,dword ptr [ebp+10h]<br>007771c2 034514          add     eax,dword ptr [ebp+14h]<br>007771c5 8945fc          mov     dword ptr [ebp-4],eax<br>007771c8 8b45fc          mov     eax,dword ptr [ebp-4]</pre><p>The first column is the address of the instruction. We can set breakpoints on any of these addresses to break at that instruction execution. Let us pick an address inside the function body and dump the registers to see the stack layout we discussed before.</p><p>I am using the address of the instruction “mov eax,dword ptr [ebp-8]” here to set my next breakpoint.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fsvwx7Hs0HMg04qedfdZlg.png" /><figcaption>Fig 6 — Setting breakpoint at an address</figcaption></figure><p>Run the exe by running the command ‘g’ and hit enter. You will hit the breakpoint as below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MF-IgOcvTtOtq0juU7p66w.png" /><figcaption>Fig 7 — Output when second breakpoint is hit</figcaption></figure><p>And this is how the call stack looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EqiEK9H6CEyV3qXtiN22Ig.png" /><figcaption>Fig 8 — Call stack after second breakpoint</figcaption></figure><p>As discussed in the stack frame layout above, we can use this output to reconstruct the stack frame and retrieve the parameters, return addresses as well as local variables from the stack frame as below:</p><h3>Retrieving the previous frame’s EBP</h3><p>The Child EBP or EBP for the frame 0 (i.e for add()) is 012ffd9c which is nothing but the address of the base of the stack frame for add()</p><p>To retrieve the EBP of main(), dump the child EBP for add() using the ‘dd’ command as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B5HTjDRqqyFheNEuj9E2nw.png" /><figcaption>Fig 9 — Dumping value stored at EBP</figcaption></figure><p>This shows that the EBP for main() is <strong>012ffdd0 </strong>which is stored at the EBP for add() (This is what the first instruction in function Prolog does — PUSH EBP)</p><h3>Retrieving Return address</h3><p>As I mentioned before, in x86, return address of the function is stored at the address EBP + 4. Let us validate this using the debugger. Dump the contents of the address EBP + 4 for frame 0 using the dd command as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DBNX4iDrjBOEAAsDwU4tKg.png" /><figcaption>Fig 10 — Dumping return address</figcaption></figure><p>This output tells us that at the address EBP + 4 for frame 0, we have the address 00777232 stored.</p><p>Use the ‘u’ command on this address to see the dis-assembly instruction at that address:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OOjmpOtaeyDDdYwcqAvDsQ.png" /><figcaption>Fig 11 — Snip mapping an address to the corresponding offset and instruction in a function</figcaption></figure><p>As you see in the snippet above, it shows that this is the next instruction in main() that is to be executed upon returning from add()</p><p>Similarly, the return address for main() is stored at EBP of main() + 4:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ddB-n7IyYdLttjnYXoWORQ.png" /><figcaption>Fig 12 — Finding return address for main() i.e frame 1</figcaption></figure><h3>Retrieving parameters</h3><p>Here in our example, the add() uses __fastcall convention for x86 which means the first two parameters would be passed in ECX and EDX and the remaining 4 parameters would be passed through the stack. These stack parameters would be present from EBP+8 onwards</p><p>To see this, let us dump values at memory addresses starting from EBP + 8 using dd command as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OAtqD-MEFHDUV0IqM8ynMg.png" /><figcaption>Fig 13 — Retrieving stack parameters for add()</figcaption></figure><p>The values you see above at each location are in hex, converting these to decimal values, we can verify that these are indeed the 3rd, 4th, 5th and 6th parameters passed to add(). You can use the ‘?’ command to see the hex to decimal conversion as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RQLByTVbCL2p3-cpjfAZ7w.png" /><figcaption>Fig 14 — Output showing the decimal equivalent of hex values</figcaption></figure><h3>Dumping local variables and register parameters</h3><p>Local variables for a function (stack frame), exception handler info as well as parameters passed through registers would be seen at negative offsets from the current frame’s EBP.</p><p>We see the following in the function body immediately after the Prolog for add():</p><pre>007771a6 8955f4          mov     dword ptr [ebp-0Ch],edx<br>007771a9 894df8          mov     dword ptr [ebp-8],ecx<br>007771ac c745fc00000000  mov     dword ptr [ebp-4],0</pre><p>As you can see, these instructions use negative offset from EBP which means these can be for local variables, exception details or register parameters. In this case, our program is not using any structured exception handling methods (no _try and _except blocks in our code). Hence we do not have any exception handler info stored on the stack frame for add(). It stores the second parameter passed in EDX to EBP — 0xC and first parameter passed through ECX to EBP — 8. The function only has one local variable which is stored at EBP — 4.</p><p>Let us dump these using the EBP for add() as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iQJj64M9rNMLBQ0GtjFVYA.png" /><figcaption>Fig 15 — Retrieving local variables and register parameters for a function using EBP</figcaption></figure><p><strong>Note</strong>: If the function uses SEH (Structured Exception Handling), then the position of local variables and register parameters would vary as exception handler details would also be present at negative offset from EBP. You will have to check the assembly instructions for the function to find out if it uses SEH or not. We will try to cover this as well in the coming articles.</p><p>That’s all about stack frame layout in x86. Stay Tuned for more topics on assembly language!</p><p>If you found this article helpful, you can follow <a href="https://medium.com/@sruthk">me</a>/my series “<a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-assembly-language-a4ad14e601a1">Cracking Assembly</a>” for more such articles on assembly language.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3ac46fa59c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A ThankYou note!]]></title>
            <link>https://medium.com/@sruthk/a-thankyou-note-bc6568d308?source=rss-5b20471afcf3------2</link>
            <guid isPermaLink="false">https://medium.com/p/bc6568d308</guid>
            <dc:creator><![CDATA[Sruthi K]]></dc:creator>
            <pubDate>Mon, 30 Jan 2023 14:04:47 GMT</pubDate>
            <atom:updated>2023-01-30T14:04:47.655Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*YkYy9doWke3bAJZA" /><figcaption>Photo by <a href="https://unsplash.com/@howier?utm_source=medium&amp;utm_medium=referral">Howie R</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>This is a thank you note to all of my followers here on medium. I started the “Cracking Assembly” series as a way to help anyone who is curious to learn Assembly as when I was learning assembly years back, it was challenging to find content that was simple enough to understand as a beginner. My intention was to make the learning path easier for you all than what it was for me in this journey.</p><p>To make writing a habit, I decided that I would try to post one article every Monday. There were times when I was doubtful whether others found my articles useful as there hardly were any feedback on the articles I posted initially. To be honest, there were several times I felt that may be these articles are not reaching anyone or may be these are not so interesting for the crowd. I was on the verge of giving up the series but then I thought of you all who have subscribed to my stories. You guys are my motivation that keeps me going here.</p><p>Thank you all for the support so far. I hope these articles help you some way or the other in your journey too :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bc6568d308" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cracking Assembly — Function Prolog and Epilog in x64]]></title>
            <link>https://medium.com/@sruthk/cracking-assembly-function-prolog-and-epilog-in-x64-ea1bdc50514c?source=rss-5b20471afcf3------2</link>
            <guid isPermaLink="false">https://medium.com/p/ea1bdc50514c</guid>
            <category><![CDATA[x64]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[assembly-language]]></category>
            <category><![CDATA[c]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <dc:creator><![CDATA[Sruthi K]]></dc:creator>
            <pubDate>Mon, 30 Jan 2023 13:34:56 GMT</pubDate>
            <atom:updated>2023-03-04T06:51:39.410Z</atom:updated>
            <content:encoded><![CDATA[<h3>Cracking Assembly — Function Prolog and Epilog in x64</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*Phio4npIJUMmASlN.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@alexkixa?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Alexandre Debiève</a> on <a href="https://unsplash.com/s/photos/motherboard?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>This is the twelfth article in the <a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-assembly-language-a4ad14e601a1">Cracking Assembly</a> series. Here is the list of published articles in the series. I would recommend you read them in the below mentioned order. I will be updating this list as new articles are published.</p><p><a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-assembly-language-a4ad14e601a1">Cracking Assembly — Introduction to Assembly Language</a><br><a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-windbg-preview-bd779ad841ed">Cracking Assembly — Introduction to Windbg Preview</a><br><a href="https://medium.com/@sruthk/cracking-assembly-x86-and-x64-architecture-3328484f67ae">Cracking Assembly — x86 and x64 architecture</a><br><a href="https://medium.com/@sruthk/cracking-assembly-cpu-registers-e385a67f6f11">Cracking Assembly — CPU Registers</a><br><a href="https://medium.com/@sruthk/cracking-assembly-x86-addressing-modes-ea8cc0d5bd12">Cracking Assembly — x86 addressing modes</a><br><a href="https://medium.com/@sruthk/cracking-assembly-functional-classification-of-assembly-instructions-78de2b613e94">Cracking Assembly — Functional classification of assembly instructions</a><br><a href="https://medium.com/@sruthk/cracking-assembly-stack-operations-63a279ae60d5">Cracking Assembly — Stack Operations</a><br><a href="https://medium.com/@sruthk/cracking-assembly-accessing-local-variables-in-x86-vs-x64-eb018ce1ef2a">Cracking Assembly — Accessing local variables in x86 vs x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-calling-conventions-in-x86-cc035d66f3f3">Cracking Assembly — Calling conventions in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-fastcall-calling-convention-in-x64-c6d77b51ea86">Cracking Assembly — fastcall calling convention in x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-function-prolog-and-epilog-in-x86-cb3c3461bcd3">Cracking Assembly — Function Prolog and Epilog in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-function-prolog-and-epilog-in-x64-ea1bdc50514c">Cracking Assembly — Function Prolog and Epilog in x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-stack-frame-layout-in-x86-3ac46fa59c">Cracking Assembly — Stack frame layout in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-walking-the-call-stack-in-x86-5693d836ffe">Cracking Assembly — Walking the call stack in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-stack-frame-layout-in-x64-75eb862dde08">Cracking Assembly — Stack Frame Layout in x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-reversing-a-simple-function-33c82d2bb759">Cracking Assembly — Reversing a simple function</a></p><p>In the <a href="https://medium.com/@sruthk/cracking-assembly-function-prolog-and-epilog-in-x86-cb3c3461bcd3">previous article</a>, we discussed about function Prolog and Epilog in x86 and the instructions that you will see in these sections. In this article, we will be discussing more about Prolog and Epilog in x64 environment so that you understand how it differs from the same in x86.</p><p>Before we start delving into Prolog and Epilog in x64, we need to understand a few concepts in x64 architecture as a knowledge about these are required for you to understand the instructions and their relevance in x64 Prolog and Epilog.</p><h3>Parameter homing</h3><p>As we discussed <a href="https://medium.com/@sruthk/cracking-assembly-fastcall-calling-convention-in-x64-c6d77b51ea86">here</a>, since x64 uses __fastcall convention, the first 4 parameters are passed through registers RCX, RDX, R8 and R9 and the remaining parameters, if any, through stack. Apart from the space for a function’s local variable, in x64, <strong>every non-leaf function will still allocate space in the stack for the parameters that it will pass in these registers to the called function.</strong> The called function will copy the first 4 parameters it received through registers, to this stack space that the caller has already allocated. This stack space that a non-leaf function allocates for any parameters that are passed to a function through registers in x64 is called Parameter Homing Area and this concept is called Parameter Homing. The non-leaf caller function allocates this homing space for 4 register parameters even if the functions that it calls take less than 4 parameters.</p><p>Let me explain this with an example. Let us take the following C code where the main() calls another function add(). The add() takes 6 parameters num1 through num6. The main() passes 6 local variables number1 to number6 as parameters to add()</p><pre>#include &lt;stdio.h&gt;<br><br><br>int __fastcall add(int num1, int num2, int num3, int num4, int num5, int num6)<br>{<br>   int sum = 0;<br>   <br>   sum = num1 + num2 + num3 + num4 + num5 + num6;<br>   <br>   return sum;<br>}<br><br>void main()<br>{<br>   int number1 = 100;<br>   int number2 = 200;<br>   int number3 = 300;<br>   int number4 = 400;<br>   int number5 = 500;<br>   int number6 = 600;<br>   <br>   int result = 0;<br>   <br>   result = add(number1,number2, number3, number4, number5, number6);<br>   <br>   printf(&quot;Sum = %d\n&quot;,result);<br>   return;<br>}</pre><p>We all know that main() will allocate space for its 7 local variables (“number1” to “number6&quot; and “result”) in its stack. As per x64 __fastcall convention, when main() calls add(), it will copy the first 4 parameters number1 to number4 in registers RCX, RDX, R8 and R9. However, since x64 has the concept of parameter homing, when it allocates space in the stack frame for main(), it would allocate space for all 7 local variables plus space for the 4 parameters that it would pass in registers to add(). We will see this in action later in the article when we look at Prolog and Epilog for these functions.</p><h3>Volatile vs Non-Volatile registers</h3><p>The registers for which there is <strong>no </strong>guarantee that their values are going to be preserved inside a function are called Volatile registers. In other words, a function can modify the value of a volatile register without saving its value before the modification. Some of the common volatile registers in x64 are RAX, RCX, RDX, R8, R9, R10, R11.</p><p>Non-volatile registers on the other hand are the registers whose previous values must be saved before a function modifies them and restored before exiting the function. In x64, the general purpose registers RBX, RDI, RSI, RBP, RSP, R12, R13, R14 and R15 are considered as Non-volatile registers.</p><h3>Function Prolog in x64</h3><p>Note: As explained in <a href="https://medium.com/@sruthk/cracking-assembly-fastcall-calling-convention-in-x64-c6d77b51ea86">this </a>article, x64 restricts PUSH and POP instructions only to the function’s Prolog and Epilog section. Thus, in x64, stack pointer (RSP) does not change between the prolog and epilog of a function. Also, stack will always be maintained 16-byte aligned. (Refer <a href="https://learn.microsoft.com/en-us/cpp/build/stack-usage?view=msvc-170">this </a>article for more details)</p><p>In x64, the Prolog of a function typically does the following:</p><ol><li>Save the parameters passed through registers to the Parameter Homing Area allocated by the caller function</li><li>Saves the value of all non-volatile registers that the function modifies (if any)</li><li>Allocates space for the function’s local variables. It also allocates space for the parameter homing area if this is not a leaf function.</li></ol><p>Let us take a look at the assembly equivalent of the C program I explained above to get more clarity on this.</p><p>Here is the assembly equivalent for the function main() :</p><pre> sub     rsp,58h<br> mov     dword ptr [rsp+44h],64h<br> mov     dword ptr [rsp+40h],0C8h<br> mov     dword ptr [rsp+3Ch],12Ch<br> mov     dword ptr [rsp+38h],190h<br> mov     dword ptr [rsp+34h],1F4h<br> mov     dword ptr [rsp+30h],0<br> mov     eax,dword ptr [rsp+34h]<br> mov     dword ptr [rsp+20h],eax<br> mov     r9d,dword ptr [rsp+38h]<br> mov     r8d,dword ptr [rsp+3Ch]<br> mov     edx,dword ptr [rsp+40h]<br> mov     ecx,dword ptr [rsp+44h]<br> call    fastcallx64!ILT+8365(add) (00007ff7`bf2c30b2)<br> mov     dword ptr [rsp+30h],eax<br> mov     edx,dword ptr [rsp+30h]<br> lea     rcx,[fastcallx64!CT??_R0?AVexceptionstd &lt;PERF&gt; (fastcallx64+0x93000) (00007ff7`bf353000)]<br> call    fastcallx64!ILT+5370(printf) (00007ff7`bf2c24ff)<br> xor     eax,eax<br> add     rsp,58h<br> ret</pre><p>For this main(), the <strong>Function Prolog</strong> consists of only one instruction:</p><pre>sub     rsp,58h</pre><p>Since this main() does not take any input parameters through registers, its Prolog does not have any instructions to save the register parameters to the Parameter Homing Area. Also, since this main() does not alter any non-volatile registers in its body, its Prolog does not save non-volatile registers as well. This means, it does not do Step 1 and Step 2 mentioned above.</p><p>However, the prolog has the instruction SUB RSP, 58h which decrements the stack pointer by 0x58. By now, you should know that decrementing the stack pointer indicates that the stack is growing. (Refer <a href="https://medium.com/@sruthk/cracking-assembly-stack-operations-63a279ae60d5">this </a>article). But, why is it decrementing the RSP by 0x58 bytes? Read, step 3 that I mentioned above carefully. This main() is a non-leaf function which means it will be calling another function in its body. In this case, main() calls add(). Since it is a non-leaf function, main will have to allocate space for its 7 local variables (7 * 8 = 0n56 = 0x38) as well as the parameter homing area for the 4 parameters it will pass to add() through registers (4 * 8 = 0n28 = 0x20). This comes to a total of 0x58 bytes (0x38 + 0x20 = 0x58). Hence, it subtracts 0x58 from RSP to grow the stack by 0x58 bytes.</p><p>Let us now see the assembly equivalent of the add() in the same C sample I showed in the beginning of this article:</p><pre> mov     dword ptr [rsp+20h],r9d<br> mov     dword ptr [rsp+18h],r8d<br> mov     dword ptr [rsp+10h],edx<br> mov     dword ptr [rsp+8],ecx<br> sub     rsp,18h<br> mov     dword ptr [rsp],0<br> mov     eax,dword ptr [rsp+28h]<br> mov     ecx,dword ptr [rsp+20h]<br> add     ecx,eax<br> mov     eax,ecx<br> add     eax,dword ptr [rsp+30h]<br> add     eax,dword ptr [rsp+38h]<br> add     eax,dword ptr [rsp+40h]<br> mov     dword ptr [rsp],eax<br> mov     eax,dword ptr [rsp]<br> add     rsp,18h<br> ret</pre><p>Out of the above instructions, the following are the instructions that constitute the Function Prolog for this add():</p><pre> mov     dword ptr [rsp+20h],r9d<br> mov     dword ptr [rsp+18h],r8d<br> mov     dword ptr [rsp+10h],edx<br> mov     dword ptr [rsp+8],ecx<br> sub     rsp,18h</pre><p>The first 4 instructions in the Prolog for add(), saves the parameters that were passed to add() through registers into the Parameter Homing Area that was allocated by the caller function main(). This function does not modify any non-volatile registers and hence we do not have any instructions to save the non-volatile registers in this Prolog either.</p><p>The third instruction in the Prolog for add() decrements 0x18 from RSP. Since add() is a leaf function, it would not be calling any other functions. Hence, it does not need to allocate any space in stack for Parameter Homing. It just has to allocate space to save its local variables in this case and it decrements RSP by 0x18 (8 bytes for 1 local variable and 0x10 bytes for 16 byte alignment as stack needs to be 16 byte aligned)</p><h3>Function Epilog in x64</h3><p>In x64 environment, the Function Epilog does the following:</p><ol><li>Clean up the stack space allocated for its local variables as well as any parameter homing area, that the function allocated in the prolog</li><li>Restore any non-volatile registers that were modified to the previous value</li><li>Return to the caller function.</li></ol><p>Let us visualize this with an example. Consider the assembly equivalents of main() and add() I showed above in the Prolog section. The following instructions constitute the Epilog for function add():</p><pre>add     rsp,18h<br>ret</pre><p>Here the first instruction cleans up the stack space add() allocated in its prolog (for local variables and 16 byte padding). Since no non-volatile registers were saved in the prolog, the epilog does not contain any instructions to restore any non-volatile registers as well. The final instruction is the RET which pops the return address from the stack and returns to the next instruction in the caller function.</p><p>And the following is how the Epilog of main() looks like:</p><pre>add     rsp,58h<br>ret</pre><p>The first intruction in the Epilog of main() is ADD RSP, 58h which cleans up the stack space that main() allocated in its Prolog. There are no non-volatile registers modified, hence no instructions to restore any non-volatile registers exist in the Epilog of this main(). The last instruction is RET.</p><p>That’s all about Prolog and Epilog in x64. I will be coming up with another concept in assembly in the next article. Stay tuned!</p><p>If you found this article helpful, you can follow <a href="https://medium.com/@sruthk">me</a>/my series “<a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-assembly-language-a4ad14e601a1">Cracking Assembly</a>” for more such articles on assembly language.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ea1bdc50514c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cracking Assembly — Function Prolog and Epilog in x86]]></title>
            <link>https://medium.com/@sruthk/cracking-assembly-function-prolog-and-epilog-in-x86-cb3c3461bcd3?source=rss-5b20471afcf3------2</link>
            <guid isPermaLink="false">https://medium.com/p/cb3c3461bcd3</guid>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[assembly-language]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[c]]></category>
            <category><![CDATA[x86]]></category>
            <dc:creator><![CDATA[Sruthi K]]></dc:creator>
            <pubDate>Mon, 23 Jan 2023 16:29:05 GMT</pubDate>
            <atom:updated>2023-03-04T06:51:16.045Z</atom:updated>
            <content:encoded><![CDATA[<h3>Cracking Assembly — Function Prolog and Epilog in x86</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*0noeTPuC2Xlzg_YS.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@alexkixa?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Alexandre Debiève</a> on <a href="https://unsplash.com/s/photos/motherboard?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>This is the eleventh article in the <a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-assembly-language-a4ad14e601a1">Cracking Assembly</a> series. Here is the list of published articles in the series. I would recommend you read them in the below mentioned order. I will be updating this list as new articles are published.</p><p><a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-assembly-language-a4ad14e601a1">Cracking Assembly — Introduction to Assembly Language</a><br><a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-windbg-preview-bd779ad841ed">Cracking Assembly — Introduction to Windbg Preview</a><br><a href="https://medium.com/@sruthk/cracking-assembly-x86-and-x64-architecture-3328484f67ae">Cracking Assembly — x86 and x64 architecture</a><br><a href="https://medium.com/@sruthk/cracking-assembly-cpu-registers-e385a67f6f11">Cracking Assembly — CPU Registers</a><br><a href="https://medium.com/@sruthk/cracking-assembly-x86-addressing-modes-ea8cc0d5bd12">Cracking Assembly — x86 addressing modes</a><br><a href="https://medium.com/@sruthk/cracking-assembly-functional-classification-of-assembly-instructions-78de2b613e94">Cracking Assembly — Functional classification of assembly instructions</a><br><a href="https://medium.com/@sruthk/cracking-assembly-stack-operations-63a279ae60d5">Cracking Assembly — Stack Operations</a><br><a href="https://medium.com/@sruthk/cracking-assembly-accessing-local-variables-in-x86-vs-x64-eb018ce1ef2a">Cracking Assembly — Accessing local variables in x86 vs x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-calling-conventions-in-x86-cc035d66f3f3">Cracking Assembly — Calling conventions in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-fastcall-calling-convention-in-x64-c6d77b51ea86">Cracking Assembly — fastcall calling convention in x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-function-prolog-and-epilog-in-x86-cb3c3461bcd3">Cracking Assembly — Function Prolog and Epilog in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-function-prolog-and-epilog-in-x64-ea1bdc50514c">Cracking Assembly — Function Prolog and Epilog in x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-stack-frame-layout-in-x86-3ac46fa59c">Cracking Assembly — Stack frame layout in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-walking-the-call-stack-in-x86-5693d836ffe">Cracking Assembly — Walking the call stack in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-stack-frame-layout-in-x64-75eb862dde08">Cracking Assembly — Stack Frame Layout in x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-reversing-a-simple-function-33c82d2bb759">Cracking Assembly — Reversing a simple function</a></p><p>In the <a href="https://medium.com/@sruthk/cracking-assembly-fastcall-calling-convention-in-x64-c6d77b51ea86">previous article</a>, I briefly touched upon the basic layout of a function along with the concepts of prolog and epilog for a function. This article will explain these concepts elaborately and show how it looks like in x86 environment.</p><h3>Quick Recap — Layout of a function in assembly</h3><p>In assembly language, a function will have the following layout or format.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/901/0*W2OIX_IH435DDSOU.png" /><figcaption>Fig 1 — Layout of a function in assembly language</figcaption></figure><p>As the diagram shows, the implementation logic of the function i.e the Function Body, is preceded by a set of instructions called Function Prolog and succeeded by another set of instructions called Function Epilog. There are certain differences in the number and type of instructions seen in the function prolog and epilog in x86 and x64 environment. We will talk about Prolog and Epilog in x86 environment in this article. I will show you the differences that are seen in x64 in the next article.</p><h3>Function Prolog in x86</h3><p>Function Prolog contains the instructions that prepare the registers and stack space for the function before it starts execution.</p><p>In x86 assembly, Function Prolog typically does the following three steps:</p><ol><li>Pushes the current value of EBP to the stack</li><li>Loads the current value of ESP to EBP</li><li>Updates ESP to make space for the local variables of the function as well as the parameters passed through registers</li></ol><p>Let me explain these steps in detail before looking at a sample:</p><p>Step 1 — In x86, we rely on EBP for a lot of important functionalities. EBP can be used to walk the call stack. EBP is also used to find out the parameters passed to each function as well as the return address for each function in the current call stack. Also, EBP points to the base of the function’s stack in x86. (Refer <a href="https://medium.com/@sruthk/cracking-assembly-accessing-local-variables-in-x86-vs-x64-eb018ce1ef2a">this </a>article if you have not done already). Due to these reasons, we need to store the EBP value for the caller function’s frame (Previous EBP) before modifying it in the new function.</p><p>Step 2 — Once previous EBP is saved, the current value of ESP is copied to EBP. This will be the new stack base address for the new function that is called.</p><p>Step 3 — Make space for the parameters passed through registers as well as local parameters of the called function by updating ESP accordingly. Once this is done, the address range between the new EBP and new ESP would be the function’s stack frame where local variables of the function is stored.</p><p>Let us visualize this using the following sample C code for a function add which uses __fastcall calling convention in x86:</p><pre>int __fastcall add(int num1, int num2, int num3, int num4, int num5, int num6)<br>{<br>   int sum = 0;<br>   <br>   sum = num1 + num2 + num3 + num4 + num5 + num6;<br>   <br>   return sum;<br>}</pre><p>This function add will take 6 parameters and uses __fastcall convention. When this is executed in x86 environment, num1 and num2 will be passed through registers ECX and EDX. The remaining parameters will be passed through the stack. Refer __fastcall section under <a href="https://medium.com/@sruthk/cracking-assembly-calling-conventions-in-x86-cc035d66f3f3">this </a>article which explains this in detail.</p><p>Here is how the Prolog for function add looks like:</p><pre>push    ebp<br>mov     ebp,esp<br>sub     esp,0Ch</pre><p>The first instruction is PUSH EBP which pushes the value of EBP for the previous frame (caller function’s stack frame) to the stack which is equivalent to storing the caller function’s stack base address. The second instruction copies the ESP (stack pointer) for the called function to EBP which means this would be the new base address of the called function’s stack frame.</p><p>The third instruction is a SUB instruction which subtracts 0xC from current ESP. By now you should know that decrementing the stack pointer indicates that the stack is growing. (Refer <a href="https://medium.com/@sruthk/cracking-assembly-stack-operations-63a279ae60d5">this </a>article). This instruction makes space for the local variables and parameters passed through registers. In this case, add() has one local variable (i.e 4 bytes) and two parameters passed through registers RCX and RDX (i.e 4+4=8 bytes). Hence 0xC (i.e 4+4+4) is subtracted from the current ESP to form the new ESP (top of the stack).</p><h3>Function Epilog in x86</h3><p>Function Epilog contains instructions that help in restoring the stack and registers to their previous state, i.e before the function was called. The instructions seen in Epilog are the opposite of the instructions seen in Prolog, in reverse order.</p><p>In x86, Function Epilog does the following steps:</p><ol><li>Sets ESP to the current base pointer value</li><li>Pops the value of the previous EBP (caller’s EBP) stored on the stack to EBP register</li><li>Returns from the function</li></ol><p>Step 1 — The first operation done in Function Epilog is to drop the stack pointer to the base address (stack base). This, in effect, shrinks the stack thereby invalidating/removing the space that was allocated for local variables and parameters passed through registers.</p><p>Steps 2 — Remember, in the function Prolog, in step 1, the previous EBP (caller’s EBP) was pushed to the stack. In Epilog, the second step is to retrieve this previous EBP that was pushed to the stack in Prolog and copy it to the EBP register. This means we are restoring the EBP to the previous value.</p><p>Step 3 — Return from the called function which will implicitly cleanup the parameter space (based on the calling convention used) and pops the return address and copies it to the EIP (Instruction pointer) so that execution can continue.</p><p>Here is how the Function Epilog would look like for the same add():</p><pre>mov     esp,ebp<br>pop     ebp<br>ret     10h</pre><p>The first instruction is MOV ESP, EBP which shrinks the stack and invalidates/removes the space allocated for the local variable as well as the two parameters num1 and num2 passed through ECX and EDX registers (since this add() uses __fastcall)</p><p>The second instruction “POP EBP” pops the previous EBP that was stored in the stack to EBP register.</p><p>The third instruction is the RET instruction. Since this add() uses x86 __fastcall convention, it also cleans up the space that was allocated for the remaining four parameters, num3 to num6, which the caller would have allocated. This is why you see RET 10h (4 * 4 = 0x10) as the last instruction. If the add() was using a calling convention like __cdecl, you would only see RET instead, as the last instruction in Epilog as the caller cleans up the stack in such cases(Refer <a href="https://medium.com/@sruthk/cracking-assembly-calling-conventions-in-x86-cc035d66f3f3">this </a>article for details on x86 calling conventions)</p><p>That’s all about Prolog and Epilog in x86. I will walk you through Prolog and Epilog for x64 in the next article and explain how it differs from the x86 counterpart. Stay Tuned!</p><p>If you found this article helpful, you can follow <a href="https://medium.com/@sruthk">me</a>/my series “<a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-assembly-language-a4ad14e601a1">Cracking Assembly</a>” for more such articles on assembly language.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cb3c3461bcd3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cracking Assembly — fastcall calling convention in x64]]></title>
            <link>https://medium.com/@sruthk/cracking-assembly-fastcall-calling-convention-in-x64-c6d77b51ea86?source=rss-5b20471afcf3------2</link>
            <guid isPermaLink="false">https://medium.com/p/c6d77b51ea86</guid>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[calling-conventions]]></category>
            <category><![CDATA[assembly-language]]></category>
            <category><![CDATA[x64]]></category>
            <dc:creator><![CDATA[Sruthi K]]></dc:creator>
            <pubDate>Mon, 16 Jan 2023 11:54:35 GMT</pubDate>
            <atom:updated>2023-03-04T06:50:48.959Z</atom:updated>
            <content:encoded><![CDATA[<h3>Cracking Assembly — Fastcall calling convention in x64</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*VHJRnAOZ-77EESKB.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@alexkixa?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Alexandre Debiève</a> on <a href="https://unsplash.com/s/photos/motherboard?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>This is the tenth article in the <a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-assembly-language-a4ad14e601a1">Cracking Assembly</a> series. Here is the list of published articles in the series. I would recommend you read them in the below mentioned order. I will be updating this list as new articles are published.</p><p><a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-assembly-language-a4ad14e601a1">Cracking Assembly — Introduction to Assembly Language</a><br><a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-windbg-preview-bd779ad841ed">Cracking Assembly — Introduction to Windbg Preview</a><br><a href="https://medium.com/@sruthk/cracking-assembly-x86-and-x64-architecture-3328484f67ae">Cracking Assembly — x86 and x64 architecture</a><br><a href="https://medium.com/@sruthk/cracking-assembly-cpu-registers-e385a67f6f11">Cracking Assembly — CPU Registers</a><br><a href="https://medium.com/@sruthk/cracking-assembly-x86-addressing-modes-ea8cc0d5bd12">Cracking Assembly — x86 addressing modes</a><br><a href="https://medium.com/@sruthk/cracking-assembly-functional-classification-of-assembly-instructions-78de2b613e94">Cracking Assembly — Functional classification of assembly instructions</a><br><a href="https://medium.com/@sruthk/cracking-assembly-stack-operations-63a279ae60d5">Cracking Assembly — Stack Operations</a><br><a href="https://medium.com/@sruthk/cracking-assembly-accessing-local-variables-in-x86-vs-x64-eb018ce1ef2a">Cracking Assembly — Accessing local variables in x86 vs x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-calling-conventions-in-x86-cc035d66f3f3">Cracking Assembly — Calling conventions in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-fastcall-calling-convention-in-x64-c6d77b51ea86">Cracking Assembly — fastcall calling convention in x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-function-prolog-and-epilog-in-x86-cb3c3461bcd3">Cracking Assembly — Function Prolog and Epilog in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-function-prolog-and-epilog-in-x64-ea1bdc50514c">Cracking Assembly — Function Prolog and Epilog in x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-stack-frame-layout-in-x86-3ac46fa59c">Cracking Assembly — Stack frame layout in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-walking-the-call-stack-in-x86-5693d836ffe">Cracking Assembly — Walking the call stack in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-stack-frame-layout-in-x64-75eb862dde08">Cracking Assembly — Stack Frame Layout in x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-reversing-a-simple-function-33c82d2bb759">Cracking Assembly — Reversing a simple function</a></p><p>In the <a href="https://medium.com/@sruthk/cracking-assembly-calling-conventions-in-x86-cc035d66f3f3">previous article</a>, we discussed about some of the commonly used calling conventions in x86. The article also discussed about fastcall convention in x86. In this article, I will be discussing __fastcall calling convention in x64 and how it differs from __fastcall in x86.</p><h3>Layout of a function in assembly</h3><p>In assembly language, a function will have the following layout or format.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/901/1*SLnouHmEUSfFPUYZR2TpnQ.png" /><figcaption>Fig 1 — Layout of a function in assembly language</figcaption></figure><p>There are a few lines of assembly language instructions that are seen at the very beginning of a function. This forms the function’s ‘<strong>Prolog</strong>’. The actual logic of the function i.e the code that is written inside the function would be seen in the function body section. Once the function body finishes, there are a few lines of assembly language instructions at the end which form the function’s ‘<strong>Epilog</strong>’.</p><p>I will talk in detail about Prolog and Epilog in the next article. For now, just understand that Prolog contains the instructions that prepare the registers and stack space for the function before it starts execution and Epilog contains instructions that help in restoring the stack and registers to their previous state, i.e before the function was called.</p><h3>RSP based local variable access in x64</h3><p>Before we talk about __fastcall in x64, we need to brush a few points about the stack pointer in x64.</p><p>As I explained in <a href="https://medium.com/@sruthk/cracking-assembly-accessing-local-variables-in-x86-vs-x64-eb018ce1ef2a">this </a>article, x64 does not used RBP as frame pointer. RSP is instead used as both frame as well as stack pointer. Hence, in x64, RSP register is supposed to be static throughout the function body. Since PUSH and POP instructions modify the stack pointer (RSP), x64 restricts PUSH and POP instructions only to the function’s Prolog and Epilog section. Thus, <strong>in x64, stack pointer (RSP) does not change between the prolog and epilog of a function</strong>.</p><h3>__fastcall in x64</h3><p>x64 by default uses __fastcall calling convention. In x64 __fastcall convention, the first 4 parameters are passed using registers and the remaining parameters (if any) are passed by copying them to the stack in the right to left order. The first parameter is copied to RCX register, second one to RDX register, third parameter to R8 register and the fourth parameter to R9 register. In x64 __fastcall convention, it is the responsibility of the caller to cleanup the space allocated for the parameters (passed through register as well as stack). The caller does the stack cleanup in the function’s Epilog, not immediately after returning from the called function unlike in x86 mode.</p><p>Let us visualize this using an example. The following code is an example of x64 __fastcall convention. It shows a function add() which by default uses __fastcall convention and takes five parameters and returns the sum. (Since this is the default calling convention in x64, we can omit the “__fastcall” keyword in function definition)</p><pre>#include &lt;stdio.h&gt;<br><br><br>int add(int num1, int num2, int num3, int num4, int num5)<br>{<br>   int sum = 0;<br>   <br>   sum = num1 + num2 + num3 + num4 + num5;<br>   <br>   return sum;<br>}<br><br>void main()<br>{<br>   int number1 = 100;<br>   int number2 = 200;<br>   int number3 = 300;<br>   int number4 = 400;<br>   int number5 = 500;<br>   <br>   int result = 0;<br>   <br>   result = add(number1,number2, number3, number4, number5);<br>   <br>   printf(&quot;Sum = %d\n&quot;,result);<br>   return;<br>}</pre><p>The below snippet shows the assembly equivalent of the main() that calls add() which is using the x64 __fastcall convention:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*giQctkfru6-akm2qtCQ5JQ.png" /><figcaption>Fig2 — Assembly equivalent of main() which calls add() that uses __fastcall convention in x64</figcaption></figure><p>Note that the main() which is the caller does the stack cleanup in the end (inside function Epilog), instead of immediately cleaning it after returning from add(). Why? Because, of the reason we mentioned above, that in x64, RSP should not change in the function body. Also, note that parameter5 is copied (moved) to a stack location instead of using the PUSH instruction, again due to the same reason.</p><p>And the below snippet shows the assembly language equivalent of the add() which uses x64 __fastcall convention:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/748/1*vsMqrF5L2IBvl-ZPCO8xYA.png" /><figcaption>Fig3 — Assembly equivalent of add() in x64</figcaption></figure><h3>Differences between x86 and x64 fastcall conventions</h3><p>The below table summarizes the major differences between __fastcall convention in x86 and x64 CPU:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/798/1*MHP0UZ3pO2r6kzN6qCBheQ.png" /></figure><p>We will discuss more about assembly language concepts in the coming articles. Stay Tuned!</p><p>If you found this article helpful, you can follow <a href="https://medium.com/@sruthk">me</a>/my series “<a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-assembly-language-a4ad14e601a1">Cracking Assembly</a>” for more such articles on assembly language.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c6d77b51ea86" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cracking Assembly — Calling conventions in x86]]></title>
            <link>https://medium.com/@sruthk/cracking-assembly-calling-conventions-in-x86-cc035d66f3f3?source=rss-5b20471afcf3------2</link>
            <guid isPermaLink="false">https://medium.com/p/cc035d66f3f3</guid>
            <category><![CDATA[assembly-language]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[x86]]></category>
            <category><![CDATA[calling-conventions]]></category>
            <dc:creator><![CDATA[Sruthi K]]></dc:creator>
            <pubDate>Mon, 09 Jan 2023 13:20:42 GMT</pubDate>
            <atom:updated>2023-03-04T06:50:28.005Z</atom:updated>
            <content:encoded><![CDATA[<h3>Cracking Assembly — Calling conventions in x86</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*-a6EQFk1xpgDFPUE.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@alexkixa?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Alexandre Debiève</a> on <a href="https://unsplash.com/s/photos/motherboard?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>This is the ninth article in the <a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-assembly-language-a4ad14e601a1">Cracking Assembly</a> series. Here is the list of published articles in the series. I would recommend you read them in the below mentioned order. I will be updating this list as new articles are published.</p><p><a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-assembly-language-a4ad14e601a1">Cracking Assembly — Introduction to Assembly Language</a><br><a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-windbg-preview-bd779ad841ed">Cracking Assembly — Introduction to Windbg Preview</a><br><a href="https://medium.com/@sruthk/cracking-assembly-x86-and-x64-architecture-3328484f67ae">Cracking Assembly — x86 and x64 architecture</a><br><a href="https://medium.com/@sruthk/cracking-assembly-cpu-registers-e385a67f6f11">Cracking Assembly — CPU Registers</a><br><a href="https://medium.com/@sruthk/cracking-assembly-x86-addressing-modes-ea8cc0d5bd12">Cracking Assembly — x86 addressing modes</a><br><a href="https://medium.com/@sruthk/cracking-assembly-functional-classification-of-assembly-instructions-78de2b613e94">Cracking Assembly — Functional classification of assembly instructions</a><br><a href="https://medium.com/@sruthk/cracking-assembly-stack-operations-63a279ae60d5">Cracking Assembly — Stack Operations</a><br><a href="https://medium.com/@sruthk/cracking-assembly-accessing-local-variables-in-x86-vs-x64-eb018ce1ef2a">Cracking Assembly — Accessing local variables in x86 vs x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-calling-conventions-in-x86-cc035d66f3f3">Cracking Assembly — Calling conventions in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-fastcall-calling-convention-in-x64-c6d77b51ea86">Cracking Assembly — fastcall calling convention in x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-function-prolog-and-epilog-in-x86-cb3c3461bcd3">Cracking Assembly — Function Prolog and Epilog in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-function-prolog-and-epilog-in-x64-ea1bdc50514c">Cracking Assembly — Function Prolog and Epilog in x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-stack-frame-layout-in-x86-3ac46fa59c">Cracking Assembly — Stack frame layout in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-walking-the-call-stack-in-x86-5693d836ffe">Cracking Assembly — Walking the call stack in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-stack-frame-layout-in-x64-75eb862dde08">Cracking Assembly — Stack Frame Layout in x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-reversing-a-simple-function-33c82d2bb759">Cracking Assembly — Reversing a simple function</a></p><h3>Calling Conventions</h3><p>Let us start with a simple example. The below code snippet shows two functions — add() and main(). The main() calls add() in its code and passes two numbers to the add() and gets the sum of these two numbers in return. Here, the main() is the <strong>caller </strong>and the add() is the <strong>callee.</strong></p><pre>#include &lt;stdio.h&gt;<br><br>int add(int num1, int num2)<br>{<br>   int sum = 0;<br>   <br>   sum = num1 + num2;<br>   <br>   return sum;<br>}<br><br>void main()<br>{<br>   int number1 = 100;<br>   int number2 = 200;<br>   <br>   int result = 0;<br>   <br>   result = add(number1,number2);<br>   <br>   printf(&quot;Sum = %d\n&quot;,result);<br>   return;<br>}</pre><p>The explanation is very simple. However, there is a lot more happening in the back-end before the function add() can perform the addition, for instance, how are these two numbers passed to the function add(), where does the main() store these two numbers — is it on the registers or on the stack or somewhere else?, who cleans up the stack upon returning from add() etc. There are different ways in which these can be done and the rules that govern these back-end activities is what we call as calling conventions.</p><p><strong>Calling conventions describe the conventions or norms that functions use when it calls another function and when a function returns to its caller function</strong>.</p><p>There are various calling conventions available in x86 environment. I will be explaining three commonly used x86 calling conventions in this article.</p><h3>__stdcall</h3><p>The __stdcall calling convention is commonly used (but not limited to) to call Win32 API functions. In this convention, the parameters are passed by pushing them to the stack in right to left order. Here, the callee (called function) does the stack cleanup when returning from it.</p><p>For example, lets take the following C sample. The logic of the function add() is same as the code seen above. The difference is that in this sample, the callee function add() uses the __stdcall convention:</p><pre>#include &lt;stdio.h&gt;<br><br><br>int __stdcall add(int num1, int num2)<br>{<br>   int sum = 0;<br>   <br>   sum = num1 + num2;<br>   <br>   return sum;<br>}<br><br>void main()<br>{<br>   int number1 = 100;<br>   int number2 = 200;<br>   <br>   int result = 0;<br>   <br>   result = add(number1,number2);<br>   <br>   printf(&quot;Sum = %d\n&quot;,result);<br>   return;<br>}</pre><p>Here, main() is the caller and add() is the callee. main() passes two parameters number1 and number2 to add(). Note that we have specified the keyword __stdcall in the function definition for add() which means it would be using __stdcall calling convention. This means, the parameters number1 and number2 would be passed by pushing them to the stack in right to left order i.e number2 being the rightmost parameters would be pushed first to the stack and then number1 would be pushed. Also, it is the responsibility of add() to cleanup the stack space allocated for passing the two parameters, while returning to main().</p><p>The below snippet shows the assembly equivalent of the main(). Note how the parameters are pushed to the stack.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/859/1*km3PuIX1pTbu0eyh20jLUA.png" /><figcaption>Fig 1 — Assembly equivalent of main() when that calls add() which uses __stdcall convention</figcaption></figure><p>Now let us take a look at the assembly language equivalent of the callee function i.e add() which is using stdcall calling convention.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/872/1*EWyYqH__wjWJ4bzXq0My0w.png" /><figcaption>Fig 2— Assembly language equivalent for add() when it uses __stdcall calling convention</figcaption></figure><p>Note that the last line is RET 8. RET is the opcode for returning from a function. RET can have 0 or 1 operand. If you see RET followed by a number, for example: RET N, it does two operations:</p><ol><li>It does an implicit pop i.e it pops the value stored at the top of the stack expecting that we have the return address at the stack top by the time return is encountered, so that it can return to the popped address and continue execution</li><li>Increments ESP by N bytes which in effect is equivalent to cleaning up the stack space allocated before. (Remember, incrementing ESP is equivalent to shrinking the stack). Refer <a href="https://medium.com/@sruthk/cracking-assembly-stack-operations-63a279ae60d5">this </a>article for details on stack and stack operations</li></ol><p>In the above snippet, the last line (RET 8) shows that add() does the stack cleanup by incrementing ESP by 8 bytes which is the space allocated for the two parameters that were pushed to the stack by main().</p><h3>__cdecl</h3><p>This is the default calling convention used in C/C++. In __cdecl convention, parameters are pushed to stack in right to left order just as in __stdcall convention. However, the stack cleanup is done by the caller instead of the callee. This makes it suitable for functions that take variable arguments (Why? Functions that take variable arguments do not have a way to know how many parameters would be passed to them until the program is run and does not know how many bytes to cleanup until it is run. Hence these kind of functions cannot use __stdcall convention)</p><p>The following code is an example of __cdecl convention. It shows the same function add() which now uses __cdecl convention.</p><pre>#include &lt;stdio.h&gt;<br><br><br>int __cdecl add(int num1, int num2)<br>{<br>   int sum = 0;<br>   <br>   sum = num1 + num2;<br>   <br>   return sum;<br>}<br><br>void main()<br>{<br>   int number1 = 100;<br>   int number2 = 200;<br>   <br>   int result = 0;<br>   <br>   result = add(number1,number2);<br>   <br>   printf(&quot;Sum = %d\n&quot;,result);<br>   return;<br>}</pre><p>The below snippet shows the assembly language equivalent of the main(). Parameters are passed by pushing to stack in the right to left order.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8Yz5Xfiti7ZWPdCPH9i_1A.png" /><figcaption>Fig 3 — Assembly equivalent of main() when it calls add() which uses __cdecl convention</figcaption></figure><p>Notice the difference from __stdcall in Fig 1. Here since add() is using __cdecl convention, it is the responsibility of the caller function, main(), to cleanup the stack after returning from the function. Hence, you see an additional instruction ADD ESP, 8 in the main() immediately after the CALL instruction to call add(). This means the caller main() cleans up the stack by incrementing ESP by 8 bytes which is the space used by the two parameters pushed to the stack.</p><p>The below snippet shows the assembly language equivalent of the add() which is using __cdecl calling convention.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/790/1*RwJYXEFckygskNIiQ3L32A.png" /><figcaption>Fig 4— Assembly language equivalent for add() when it uses __stdcall calling convention</figcaption></figure><p>Notice the difference in RET instruction. Since it uses __cdecl convention, the add() does not do stack cleanup and hence the final instruction of the function is a simple RET unlike the __stdcall example.</p><h3>__fastcall</h3><p>This is another category of calling convention available in x86 environment. In __fastcall convention, the first two parameters are passed using registers and the remaining parameters (if any), would be pushed to stack in right to left order. The first parameter is copied to ECX register and the second parameter is copied to EDX register. In __fastcall convention, the callee does the stack cleanup, similar to __stdcall convention.</p><p>The following code is an example of __fastcall convention. It shows a function add() which uses __fastcall convention and takes five parameters and returns the sum.</p><pre>#include &lt;stdio.h&gt;<br><br><br>int __fastcall add(int num1, int num2, int num3, int num4, int num5)<br>{<br>   int sum = 0;<br>   <br>   sum = num1 + num2 + num3 + num4 + num5;<br>   <br>   return sum;<br>}<br><br>void main()<br>{<br>   int number1 = 100;<br>   int number2 = 200;<br>   int number3 = 300;<br>   int number4 = 400;<br>   int number5 = 500;<br>   <br>   int result = 0;<br>   <br>   result = add(number1,number2, number3, number4, number5);<br>   <br>   printf(&quot;Sum = %d\n&quot;,result);<br>   return;<br>}</pre><p>The below snippet shows the assembly equivalent of main() when it calls add() which is using fastcall convention in x86 system:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OBhLmdaYCjiipsG22hNKHw.png" /><figcaption>Fig 5— Assembly equivalent of main() when it calls add() which uses __fastcall convention</figcaption></figure><p>Notice how parameters are passed, the first two parameters are passed in ECX and EDX respectively. The remaining parameters are passed by pushing to the stack on the right to left order.</p><p>And the below snippet shows the assembly language equivalent of the add() which is using fastcall convention:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/661/1*1QtN1PKoPRDkaW7tIPo4_A.png" /><figcaption>Fig 6— Assembly equivalent of add() which uses __fastcall convention</figcaption></figure><p>Note that in add() does the stack cleanup here. The last instruction RET 0C will increment the ESP by 0xC bytes which is the space taken by the three parameters (parameter3, 4 and 5) that were pushed to the stack (4 bytes each since this is x86)</p><h3>Summary</h3><p>The below table summarizes the characteristics of these three calling conventions we discussed above:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/918/1*WN1Q2V6aNTp1SqT7bUhZGg.png" /><figcaption>Fig 7 — Summary</figcaption></figure><p>We will discuss more about assembly language concepts in the coming articles. Stay Tuned!</p><p>If you found this article helpful, you can follow <a href="https://medium.com/@sruthk">me</a>/my series “<a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-assembly-language-a4ad14e601a1">Cracking Assembly</a>” for more such articles on assembly language.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cc035d66f3f3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cracking Assembly — Accessing local variables in x86 vs x64]]></title>
            <link>https://medium.com/@sruthk/cracking-assembly-accessing-local-variables-in-x86-vs-x64-eb018ce1ef2a?source=rss-5b20471afcf3------2</link>
            <guid isPermaLink="false">https://medium.com/p/eb018ce1ef2a</guid>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[x86]]></category>
            <category><![CDATA[x64]]></category>
            <category><![CDATA[assembly-language]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <dc:creator><![CDATA[Sruthi K]]></dc:creator>
            <pubDate>Mon, 02 Jan 2023 14:08:35 GMT</pubDate>
            <atom:updated>2023-03-04T06:49:58.455Z</atom:updated>
            <content:encoded><![CDATA[<h3>Cracking Assembly — Accessing local variables in x86 vs x64</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*OnSG7zx8UGe-gFFA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@alexkixa?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Alexandre Debiève</a> on <a href="https://unsplash.com/s/photos/motherboard?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>This is the eighth article in the <a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-assembly-language-a4ad14e601a1">Cracking Assembly</a> series. Here is the list of published articles in the series. I would recommend you read them in the below mentioned order. I will be updating this list as new articles are published.</p><p><a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-assembly-language-a4ad14e601a1">Cracking Assembly — Introduction to Assembly Language</a><br><a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-windbg-preview-bd779ad841ed">Cracking Assembly — Introduction to Windbg Preview</a><br><a href="https://medium.com/@sruthk/cracking-assembly-x86-and-x64-architecture-3328484f67ae">Cracking Assembly — x86 and x64 architecture</a><br><a href="https://medium.com/@sruthk/cracking-assembly-cpu-registers-e385a67f6f11">Cracking Assembly — CPU Registers</a><br><a href="https://medium.com/@sruthk/cracking-assembly-x86-addressing-modes-ea8cc0d5bd12">Cracking Assembly — x86 addressing modes</a><br><a href="https://medium.com/@sruthk/cracking-assembly-functional-classification-of-assembly-instructions-78de2b613e94">Cracking Assembly — Functional classification of assembly instructions</a><br><a href="https://medium.com/@sruthk/cracking-assembly-stack-operations-63a279ae60d5">Cracking Assembly — Stack Operations</a><br><a href="https://medium.com/@sruthk/cracking-assembly-accessing-local-variables-in-x86-vs-x64-eb018ce1ef2a">Cracking Assembly — Accessing local variables in x86 vs x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-calling-conventions-in-x86-cc035d66f3f3">Cracking Assembly — Calling conventions in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-fastcall-calling-convention-in-x64-c6d77b51ea86">Cracking Assembly — fastcall calling convention in x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-function-prolog-and-epilog-in-x86-cb3c3461bcd3">Cracking Assembly — Function Prolog and Epilog in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-function-prolog-and-epilog-in-x64-ea1bdc50514c">Cracking Assembly — Function Prolog and Epilog in x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-stack-frame-layout-in-x86-3ac46fa59c">Cracking Assembly — Stack frame layout in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-walking-the-call-stack-in-x86-5693d836ffe">Cracking Assembly — Walking the call stack in x86</a><br><a href="https://medium.com/@sruthk/cracking-assembly-stack-frame-layout-in-x64-75eb862dde08">Cracking Assembly — Stack Frame Layout in x64</a><br><a href="https://medium.com/@sruthk/cracking-assembly-reversing-a-simple-function-33c82d2bb759">Cracking Assembly — Reversing a simple function</a></p><p>In the previous article, I introduced you all to the basics of stack, the importance of ESP/RSP register and the instructions that modify the stack pointer (value of ESP/RSP). In this article, I will take you a bit more deeper in stack where we would take a look at how stack is used to store local variables and how accessing these local variables differ in x86 and x64 versions.</p><h3>Local Variable — What is it?</h3><p>Local variables are variables defined inside a function. Let us take a look at the following code snippet.</p><pre>/*<br><br>  calculate: A function to print the sum and difference of two numbers<br><br>  Parameters:<br>    num1 : First operand<br>    num2 : Second operand<br><br>*/<br>void calculate(int num1, int num2)<br>{<br> int sum = 0;<br> int difference = 0;<br><br> sum = num1 + num2;<br>  <br> if(num1 &gt; num2)<br> {<br>    difference = num1 - num2;<br> }<br> else<br> {<br>    difference = num2 - num1;<br> }<br> <br> printf(&quot;Sum is %d\n&quot;,sum);<br> printf(&quot;Difference is %d\n&quot;,difference);<br><br> return;<br>}<br></pre><p>The above snippet shows the C language code for a function named ‘<strong>calculate</strong>’ which calculates the sum and difference of two given numbers. In this example, the variables <strong>num1 </strong>and <strong>num2 </strong>are called parameters of the function ‘<strong>calculate</strong>’. The variables ‘<strong>sum</strong>’ and ‘<strong>difference</strong>’ are called local variables of the function. Thus, this example function has 2 local variables.</p><h3>Significance of EBP and ESP registers</h3><p>Before we start looking at how local variables are accessed and stored, we need to talk a bit about the significance of EBP/RBP and ESP/RSP registers.</p><p>In the <a href="https://medium.com/@sruthk/cracking-assembly-stack-operations-63a279ae60d5">previous article</a>, I had explained the concept of stack and how it grows. Please go through it if you have not done that already. As we saw, stack grows from a higher address to a lower address. Also, elements are always added to and removed from the top of the stack. The ESP register (or RSP in x64) stores the address of the current top of the stack. There is another register, the EBP register (or RBP in x64) which points to the base of the stack (starting address of a stack).</p><p>Based on this explanation, the below diagram pictorially represents a stack and the significance of EBP and ESP registers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/814/1*XuVap6CKHkFbka-HeCzMug.png" /><figcaption>Fig 1: Stack growth and values of ESP, EBP registers in x86 environment</figcaption></figure><p>In the beginning, when the stack is empty, ESP and EBP both will point to the same address. As stack grows, ESP will be decremented to update it to the current top of the stack. Since stack grows from higher to lower address, when the stack is non-empty, EBP will always be greater than ESP.</p><p>In both x86 and x64 environments, local variables of a function is stored on the function’s stack frame. This means, the region between EBP and ESP in the above figure (Fig 1) is where you will find the local variables of that function. This region is called as the function’s ‘<strong>frame</strong>’. However the difference is in the way we access them. Read on to understand what this means.</p><h3>Accessing local variables in x86</h3><p>In x86 environment, we use the EBP register to access the local variables stored on the stack.</p><p>As I explained before, EBP register will point to the address of the stack base (starting address of the stack). In x86, EBP register is used as a frame pointer. Hence, in x86, local variables are accessed using EBP register. To be precise, local variables are accessed as negative offsets from EBP in x86 environment.</p><p>The below snippet shows the x86 assembly language equivalent of the calculate function we saw before.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9K6SJMsrbw__KoUchzbt6Q.png" /><figcaption>Fig 2: x86 assembly language equivalent of the sample function ‘calculate’</figcaption></figure><p><strong>Note</strong>: The above output is from Windbg Preview. Read <a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-windbg-preview-bd779ad841ed">this </a>article to know how to launch an executable under WinDbg Preview and view the assembly code for a function.</p><p>As you see in the Figure Fig 2, all the instructions inside red boxes represent instructions that access or modify local variables. As I mentioned above, in x86 systems, local variables are accessed as negative offsets from EBP. Here in this example, EBP-8 is where the first local variable ‘sum’ is stored. EBP — 4 is where the second local variable is stored, i.e ‘difference’.</p><h3>Accessing local variables in x64</h3><p>In x64 environment, on the other hand, we make use of the RSP register to access local variables of a function. In x64, RBP is not used as a frame pointer. RSP is used as both frame pointer as well as stack pointer. Since RSP always points to the current stack top address, local variables are accessed as positive offset from RSP (Why? Because stack grows from higher to lower addresses)</p><p>The below snippet shows the x64 assembly language equivalent of the sample ‘calculate’ function. See the differences in accessing local variables:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tJ_uW4PdKYe64Cj2QTFWVg.png" /><figcaption>Fig 3: x64 assembly language equivalent of the sample function ‘calculate’</figcaption></figure><p>As you see in the Figure Fig 3, all the instructions inside red boxes represent instructions that access or modify local variables. As I mentioned above, in x64 systems, local variables are accessed as positive offsets from ESP. Here in this example, ESP+0x24 is where the first local variable ‘sum’ is stored. ESP+0x20 is where the second local variable is stored, i.e ‘difference’.</p><p>That’s all about local variable access in x64 vs x86. We will talk about more such assembly language concepts in the coming articles. If you found this article helpful, you can follow <a href="https://medium.com/@sruthk">me</a>/my series “<a href="https://medium.com/@sruthk/cracking-assembly-introduction-to-assembly-language-a4ad14e601a1">Cracking Assembly</a>” for more such articles on assembly language.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eb018ce1ef2a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>