<?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 ret2sugar on Medium]]></title>
        <description><![CDATA[Stories by ret2sugar on Medium]]></description>
        <link>https://medium.com/@sag07.t?source=rss-843b5e2f9d4a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*aZyWL5X7yQwtdQ4xI3V_Tw.jpeg</url>
            <title>Stories by ret2sugar on Medium</title>
            <link>https://medium.com/@sag07.t?source=rss-843b5e2f9d4a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 09:01:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sag07.t/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[Understanding Stack-Based Buffer Overflow]]></title>
            <link>https://medium.com/@sag07.t/understanding-stack-based-buffer-overflow-b6dd93d34f6a?source=rss-843b5e2f9d4a------2</link>
            <guid isPermaLink="false">https://medium.com/p/b6dd93d34f6a</guid>
            <category><![CDATA[reversing]]></category>
            <category><![CDATA[binary-exploitation]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <dc:creator><![CDATA[ret2sugar]]></dc:creator>
            <pubDate>Wed, 17 Sep 2025 13:44:54 GMT</pubDate>
            <atom:updated>2025-09-17T13:44:54.760Z</atom:updated>
            <content:encoded><![CDATA[<p>In previous blog we seen about prologue and epilogue which is essential to understand the stack based buffer overflow.</p><p>Buffer overflows are one of the most infamous vulnerabilities in software security. Among them, <strong>stack-based buffer overflow</strong> is particularly significant because it can allow attackers to execute arbitrary code and potentially take control of a system. In this blog, we’ll break down what it is, how it happens, and why it matters.</p><h3>What is a Stack-Based Buffer Overflow?</h3><p>A <strong>stack-based buffer overflow</strong> occurs when a program writes more data to a buffer (a block of memory) on the <strong>stack</strong> than it can hold. The stack is a special area in memory that stores temporary data such as function parameters, local variables, and return addresses.</p><p>When a buffer on the stack is overrun, the extra data can overwrite other important information, such as the <strong>function’s return address</strong>, potentially allowing an attacker to change the program’s control flow.</p><pre>#include &lt;stdio.h&gt;<br>#include &lt;string.h&gt;<br><br>void  check(char *input) {<br>    char buffer[8];<br>    strcpy(buffer, input);  // no boundary check <br>}<br><br>int main() {<br>    char input[64] = &quot;AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA&quot;;<br>    check(input);<br>    return 0;<br>}</pre><p><strong>Check()</strong></p><p>The check() function declares a small buffer of 8 bytes on the stack and uses strcpy() to copy the input into this buffer without checking its size. Since the input can be larger than 8 bytes, this operation can overwrite memory beyond the buffer, including critical data such as the function’s return address. This makes the function <strong>vulnerable to a stack-based buffer overflow</strong>, where malicious input can manipulate the program’s control flow.</p><p><strong>Main()</strong></p><p>In the main() function, a character array input[64] is initialized with more than 60 A. When check(input) is called, the large input exceeds the allocated buffer size in check(), triggering a stack overflow. This scenario illustrates how improperly handled input and unsafe functions like strcpy can lead to classic buffer overflow vulnerabilities, which attackers can exploit to execute arbitrary code or crash the program.</p><p><strong>Why This Is Dangerous ??</strong></p><p>Stack-based buffer overflows are dangerous because they allow attackers to manipulate a program’s memory in ways that were never intended. One major risk is <strong>memory corruption</strong>. When a buffer on the stack is overflowed, it can overwrite adjacent memory, including local variables, saved registers, or the function’s return address. This can cause the program to behave unpredictably, often resulting in crashes or unexpected behavior. Even small overflows can corrupt critical program data, leading to errors that are difficult to debug and can compromise the program’s integrity.</p><p>Another serious risk is <strong>hijacking control flow</strong>. By carefully crafting the input, an attacker can overwrite the return address of a function on the stack. When the function returns, instead of jumping back to the expected location, the program may jump to attacker-controlled code. For example, an attacker could redirect execution to a hidden win() function or inject malicious code that the program executes, effectively giving the attacker control over the system.</p><p><strong>Methods to Prevent Stack-Based Buffer Overflows</strong></p><p>Buffer overflows are a serious threat in software security, but developers can use several effective techniques to reduce the risk of these vulnerabilities. Implementing these methods not only protects applications but also ensures safer and more reliable code execution.</p><p>1. Bounds Checking: Always ensure that the amount of data copied into a buffer does not exceed its size. Functions like `strncpy()` or `snprintf()` should be preferred over `strcpy()` and `sprintf()` because they allow developers to specify the maximum number of bytes to copy or write. Proper bounds checking prevents excess data from overwriting adjacent memory on the stack or heap.</p><p>2. Input Validation and Sanitization: Every input coming from users or external sources should be carefully validated. Input that is too long or contains unexpected characters should be rejected or sanitized before being processed. This prevents malicious inputs from causing overflows or triggering undefined behavior.</p><p>3. Compiler Security Features: Modern compilers offer built-in protections against buffer overflows. Stack canaries, for example, insert random values between local variables and the return address. When a function returns, the compiler checks if the canary value is unchanged. If it has been altered, the program can safely terminate instead of executing potentially harmful code.</p><p>4. Memory Protection Techniques: Features like Non-Executable (NX) memory or Data Execution Prevention (DEP) prevent code from running in memory regions designated for data. This blocks classic buffer overflow attacks where injected code is executed from the stack or heap.</p><p>5. Use of Secure Libraries: Many programming environments provide secure libraries designed to prevent overflows. For instance, the Safe C Library provides functions that include automatic bounds checking and safer memory handling. Utilizing such libraries reduces the risk of accidental buffer overflow vulnerabilities.</p><p>6. Regular Code Reviews and Static Analysis: Conducting through code reviews helps identify potential buffer overflow risks early. Automated static or dynamic analysis tools can scan source code for unsafe function calls, improper memory access, or other vulnerabilities. Combining human review with automated tools significantly reduces the chance of missing dangerous code patterns.</p><p>7. Fuzz Testing and Penetration Testing:Rigorous testing, including fuzzing, exposes how software handles unexpected or malformed input. By simulating potential attacks in a controlled environment, developers can identify and patch vulnerabilities before they are exploited in production systems.</p><p>In the next blog, we’ll look at practical ways attackers can exploit vulnerabilities before they are patched. We’ll cover stack protections like canaries, PIE, ASLR, and Fortify, and explore techniques like ret2libc, building ROP chains, and format string vulnerabilities. This will give a more hands-on view of how attackers take advantage of weaknesses and how developers can respond. Stay tuned for it!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b6dd93d34f6a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Stack Conventions: Prologue and Epilogue]]></title>
            <link>https://medium.com/@sag07.t/stack-conventions-prologue-and-epilogue-c198806172c5?source=rss-843b5e2f9d4a------2</link>
            <guid isPermaLink="false">https://medium.com/p/c198806172c5</guid>
            <category><![CDATA[assembly]]></category>
            <category><![CDATA[low-level-programming]]></category>
            <category><![CDATA[reversing]]></category>
            <category><![CDATA[x86]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <dc:creator><![CDATA[ret2sugar]]></dc:creator>
            <pubDate>Wed, 17 Sep 2025 11:43:01 GMT</pubDate>
            <atom:updated>2025-09-17T12:55:57.279Z</atom:updated>
            <content:encoded><![CDATA[<p>In this blog, we will explore the function <strong>prologue</strong> and <strong>epilogue</strong>, how they manage the stack during a function call, and why they are essential in low-level programming.</p><p>The function prologue and epilogue are the key operations that occur on the stack when a function is called.</p><p>In the standard C calling convention, <strong>function parameter passing</strong> precedes the function jump. The calling function first pushes the <strong>arguments (parameters)</strong> onto the stack, typically proceeding from right to left. Immediately following this, the <strong>Call</strong> <strong>instruction</strong> executes, which automatically places the <strong>Return Address</strong> (the memory location of the instruction to be executed next) onto the stack. This address is critically important because it defines the exact point where program execution must resume after the called function has successfully completed its operations and begun its final cleanup (the <strong>epilogue</strong>).</p><p>BEFORE PROLOGUE:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/676/1*-to3jFCaZXphZTUEuz9Cyw.png" /></figure><p>Prologue :</p><p>The <strong>Prologue</strong> is a short sequence of assembly instructions that runs automatically at the very beginning of a function’s execution. Its sole purpose is to <strong>create and initialize a new, dedicated workspace</strong> on the stack called the <strong>Stack Frame</strong> for the current function.</p><pre>push ebp<br>mov ebp,esp<br>sub esp,0x20</pre><p>Before prologue:</p><p><strong>push ebp:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/488/1*iSK8OcZ0l8qPLl3ji-4Lsw.png" /></figure><p>The instruction saves the caller’s base pointer <strong>ebp</strong> onto the stack. This serves as a reference so that, when the function finishes, the previous stack frame can be restored correctly.</p><p><strong>mov ebp,esp:</strong></p><p>updates the base pointer to the current stack pointer. This establishes a new base for the current function’s stack frame. From this point onward, local variables and function arguments can be accessed relative to ebp.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/488/1*ea0KlNDT4klMWUDII-CQcg.png" /></figure><p><strong>sub esp, 20 :</strong></p><p>Allocates 20 bytes of space on the stack for local variables. Since the stack grows downward in memory — from higher addresses to lower addresses — subtracting from esp moves it further down, creating the required space for the function’s local data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/377/1*z9M9VVGTtlK37JP2Nazpxg.png" /></figure><p>Epilogue:</p><p>The <strong>function epilogue</strong> is the cleanup phase that happens at the end of a function. It reverses the steps done in the prologue: the stack pointer is reset back to the base pointer, the saved base pointer is restored, and then the function returns to the caller using the return address. This ensures the stack frame of the calling function is restored exactly as it was before the call.</p><pre>mov esp,ebp<br>pop ebp<br>ret</pre><p><strong>mov esp,ebp:</strong></p><p>The stack pointer is reset to the value of the base pointer. Earlier in the prologue, extra space was created for local variables by subtracting from esp , which moved it downward in memory. By restoring <strong>esp</strong> to <strong>ebp</strong>, that reserved region is immediately discarded. In other words, all the local variables that lived between <strong>ebp </strong>and <strong>esp</strong> are no longer part of the active stack frame. This simple instruction effectively cleans up the stack space used by the function’s local data, preparing it to return control safely to the caller.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/440/1*vC6Q9_Jm2sORHSH5W64O7w.png" /></figure><p><strong>pop ebp :</strong></p><p>The instruction pop ebp restores the caller’s saved base pointer. Earlier, at the start of the function, the old <strong>ebp </strong>was pushed onto the stack so it could be recovered later. By popping it back into ebp , the function reinstates the caller’s original stack frame. This step ensures that the calling function regains full control of its stack structure exactly as it was before the call.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/483/1*SgPewzmuI--d9jamDzmf1g.png" /><figcaption>r</figcaption></figure><p><strong>ret :</strong></p><p>The ret instruction completes the function’s execution by popping the return address from the stack into the instruction pointer <strong>eip</strong> . This return address was automatically placed on the stack when the function was called. Once <strong>ret </strong>loads it into <strong>eip</strong> , the processor continues execution from that exact point, resuming the caller’s code immediately after the function call.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/380/1*xD8oUly8WMfHnhK3sjX97Q.png" /></figure><p>In summary, the function prologue and epilogue work together to manage the stack frame of a function. The prologue begins by creating a new stack frame, saving the caller’s base pointer, and reserving space for local variables. The epilogue then performs the reverse process, releasing the local space, restoring the saved base pointer, and finally returning control to the caller through the return address. These conventions form the backbone of function execution at the assembly level and are fundamental concepts for anyone learning low-level programming, debugging compiled code, or studying reverse engineering.</p><p>More blogs like this coming soon — stay tuned!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c198806172c5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>