<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://wuzhou.is-a.dev/feed.xml" rel="self" type="application/atom+xml" /><link href="https://wuzhou.is-a.dev/" rel="alternate" type="text/html" /><updated>2025-07-06T18:09:34-07:00</updated><id>https://wuzhou.is-a.dev/feed.xml</id><title type="html">Wuzhou Du</title><subtitle>Personal Website</subtitle><author><name>Wuzhou Du</name><email>wudu@ucsd.edu</email></author><entry><title type="html">CSAPP Bomb Lab - 2</title><link href="https://wuzhou.is-a.dev/posts/2024/07/csapp/bomblab-day3&4" rel="alternate" type="text/html" title="CSAPP Bomb Lab - 2" /><published>2024-07-04T00:00:00-07:00</published><updated>2024-07-04T00:00:00-07:00</updated><id>https://wuzhou.is-a.dev/posts/2024/07/csapp/CSAPP-bomblab2</id><content type="html" xml:base="https://wuzhou.is-a.dev/posts/2024/07/csapp/bomblab-day3&amp;4"><![CDATA[<h2 id="phase-4">Phase 4</h2>
<p>Dump of assembler code for function phase_4:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>   0x000000000040100c &lt;+0&gt;:	sub    $0x18,%rsp
   0x0000000000401010 &lt;+4&gt;:	lea    0xc(%rsp),%rcx
   0x0000000000401015 &lt;+9&gt;:	lea    0x8(%rsp),%rdx
   0x000000000040101a &lt;+14&gt;:	mov    $0x4025cf,%esi
   0x000000000040101f &lt;+19&gt;:	mov    $0x0,%eax
   0x0000000000401024 &lt;+24&gt;:	callq  0x400bf0 &lt;__isoc99_sscanf@plt&gt;
   0x0000000000401029 &lt;+29&gt;:	cmp    $0x2,%eax
   0x000000000040102c &lt;+32&gt;:	jne    0x401035 &lt;phase_4+41&gt;
   0x000000000040102e &lt;+34&gt;:	cmpl   $0xe,0x8(%rsp)
   0x0000000000401033 &lt;+39&gt;:	jbe    0x40103a &lt;phase_4+46&gt;
   0x0000000000401035 &lt;+41&gt;:	callq  0x40143a &lt;explode_bomb&gt;
   0x000000000040103a &lt;+46&gt;:	mov    $0xe,%edx
   0x000000000040103f &lt;+51&gt;:	mov    $0x0,%esi
   0x0000000000401044 &lt;+56&gt;:	mov    0x8(%rsp),%edi
   0x0000000000401048 &lt;+60&gt;:	callq  0x400fce &lt;func4&gt;
   0x000000000040104d &lt;+65&gt;:	test   %eax,%eax
   0x000000000040104f &lt;+67&gt;:	jne    0x401058 &lt;phase_4+76&gt;
   0x0000000000401051 &lt;+69&gt;:	cmpl   $0x0,0xc(%rsp)
   0x0000000000401056 &lt;+74&gt;:	je     0x40105d &lt;phase_4+81&gt;
   0x0000000000401058 &lt;+76&gt;:	callq  0x40143a &lt;explode_bomb&gt;
   0x000000000040105d &lt;+81&gt;:	add    $0x18,%rsp
   0x0000000000401061 &lt;+85&gt;:	retq   
</code></pre></div></div>
<p>The codes before <em>line +41</em> are not interesting, which indicates the first input must be below or eqaul to 0xe. Then, prepare three arguments for <code class="language-plaintext highlighter-rouge">func4</code> procedure. The first argument is the first input. The second argument is 0, and the third is 0xe. The procedure <code class="language-plaintext highlighter-rouge">func4</code> must return 0 otherwise the bomb explodes.</p>

<p>After returning from the procedure, it checks whether the second input is 0. So, the answer is <strong>7 0</strong>.</p>

<h3 id="func4">func4</h3>
<p>Dump of assembler code for function func4:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>   0x0000000000400fce &lt;+0&gt;:	sub    $0x8,%rsp
   0x0000000000400fd2 &lt;+4&gt;:	mov    %edx,%eax
   0x0000000000400fd4 &lt;+6&gt;:	sub    %esi,%eax
   0x0000000000400fd6 &lt;+8&gt;:	mov    %eax,%ecx
   0x0000000000400fd8 &lt;+10&gt;:	shr    $0x1f,%ecx
   0x0000000000400fdb &lt;+13&gt;:	add    %ecx,%eax
   0x0000000000400fdd &lt;+15&gt;:	sar    %eax
   0x0000000000400fdf &lt;+17&gt;:	lea    (%rax,%rsi,1),%ecx
   0x0000000000400fe2 &lt;+20&gt;:	cmp    %edi,%ecx
   0x0000000000400fe4 &lt;+22&gt;:	jle    0x400ff2 &lt;func4+36&gt;
   0x0000000000400fe6 &lt;+24&gt;:	lea    -0x1(%rcx),%edx
   0x0000000000400fe9 &lt;+27&gt;:	callq  0x400fce &lt;func4&gt;
   0x0000000000400fee &lt;+32&gt;:	add    %eax,%eax
   0x0000000000400ff0 &lt;+34&gt;:	jmp    0x401007 &lt;func4+57&gt;
   0x0000000000400ff2 &lt;+36&gt;:	mov    $0x0,%eax
   0x0000000000400ff7 &lt;+41&gt;:	cmp    %edi,%ecx
   0x0000000000400ff9 &lt;+43&gt;:	jge    0x401007 &lt;func4+57&gt;
   0x0000000000400ffb &lt;+45&gt;:	lea    0x1(%rcx),%esi
   0x0000000000400ffe &lt;+48&gt;:	callq  0x400fce &lt;func4&gt;
   0x0000000000401003 &lt;+53&gt;:	lea    0x1(%rax,%rax,1),%eax
   0x0000000000401007 &lt;+57&gt;:	add    $0x8,%rsp
   0x000000000040100b &lt;+61&gt;:	retq   
</code></pre></div></div>
<p>This is a recursive function and we want it to return 0. After the simulation, we find that it is close to binary searching process, which searches the first argument between the range of the second and third arguments. If it returns 0, the first argument can be the middle of the second and the third, which is \(\frac{0 + e}{2} = 7\).</p>

<h2 id="phase-5">Phase 5</h2>
<p>Dump of assembler code for function phase_5:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>   0x0000000000401062 &lt;+0&gt;:	push   %rbx
   0x0000000000401063 &lt;+1&gt;:	sub    $0x20,%rsp
   0x0000000000401067 &lt;+5&gt;:	mov    %rdi,%rbx
   0x000000000040106a &lt;+8&gt;:	mov    %fs:0x28,%rax
   0x0000000000401073 &lt;+17&gt;:	mov    %rax,0x18(%rsp)
   0x0000000000401078 &lt;+22&gt;:	xor    %eax,%eax
   0x000000000040107a &lt;+24&gt;:	callq  0x40131b &lt;string_length&gt;
   0x000000000040107f &lt;+29&gt;:	cmp    $0x6,%eax
   0x0000000000401082 &lt;+32&gt;:	je     0x4010d2 &lt;phase_5+112&gt;
   0x0000000000401084 &lt;+34&gt;:	callq  0x40143a &lt;explode_bomb&gt;
   0x0000000000401089 &lt;+39&gt;:	jmp    0x4010d2 &lt;phase_5+112&gt;
   0x000000000040108b &lt;+41&gt;:	movzbl (%rbx,%rax,1),%ecx
   0x000000000040108f &lt;+45&gt;:	mov    %cl,(%rsp)
   0x0000000000401092 &lt;+48&gt;:	mov    (%rsp),%rdx
   0x0000000000401096 &lt;+52&gt;:	and    $0xf,%edx
   0x0000000000401099 &lt;+55&gt;:	movzbl 0x4024b0(%rdx),%edx
   0x00000000004010a0 &lt;+62&gt;:	mov    %dl,0x10(%rsp,%rax,1)
   0x00000000004010a4 &lt;+66&gt;:	add    $0x1,%rax
   0x00000000004010a8 &lt;+70&gt;:	cmp    $0x6,%rax
   0x00000000004010ac &lt;+74&gt;:	jne    0x40108b &lt;phase_5+41&gt;
   0x00000000004010ae &lt;+76&gt;:	movb   $0x0,0x16(%rsp)
   0x00000000004010b3 &lt;+81&gt;:	mov    $0x40245e,%esi
---Type &lt;return&gt; to continue, or q &lt;return&gt; to quit---
   0x00000000004010b8 &lt;+86&gt;:	lea    0x10(%rsp),%rdi
   0x00000000004010bd &lt;+91&gt;:	callq  0x401338 &lt;strings_not_equal&gt;
   0x00000000004010c2 &lt;+96&gt;:	test   %eax,%eax
   0x00000000004010c4 &lt;+98&gt;:	je     0x4010d9 &lt;phase_5+119&gt;
   0x00000000004010c6 &lt;+100&gt;:	callq  0x40143a &lt;explode_bomb&gt;
   0x00000000004010cb &lt;+105&gt;:	nopl   0x0(%rax,%rax,1)
   0x00000000004010d0 &lt;+110&gt;:	jmp    0x4010d9 &lt;phase_5+119&gt;
   0x00000000004010d2 &lt;+112&gt;:	mov    $0x0,%eax
   0x00000000004010d7 &lt;+117&gt;:	jmp    0x40108b &lt;phase_5+41&gt;
   0x00000000004010d9 &lt;+119&gt;:	mov    0x18(%rsp),%rax
   0x00000000004010de &lt;+124&gt;:	xor    %fs:0x28,%rax
   0x00000000004010e7 &lt;+133&gt;:	je     0x4010ee &lt;phase_5+140&gt;
   0x00000000004010e9 &lt;+135&gt;:	callq  0x400b30 &lt;__stack_chk_fail@plt&gt;
   0x00000000004010ee &lt;+140&gt;:	add    $0x20,%rsp
   0x00000000004010f2 &lt;+144&gt;:	pop    %rbx
   0x00000000004010f3 &lt;+145&gt;:	retq   
</code></pre></div></div>

<blockquote>
  <p>When encountering <em>line +8</em>, I have no idea what is <code class="language-plaintext highlighter-rouge">mov %fs:0x28,%rax</code>. I have to check all I have learnt.
I find <a href="https://stackoverflow.com/questions/10325713/why-does-this-memory-address-fs0x28-fs0x28-have-a-random-value">this</a> may help. Anyway, it seems to not relate to the bomb. So I will continue.</p>
</blockquote>

<p>In the following few lines, it ensures that the length of the input string is 6. The core part begins from <em>line +41</em>, which transforms the input string character by character. The conversion is <em>line +52</em>, which only stores the lower 1 byte of the character as the offset. In <em>line +55</em>, the offset plus the base address <code class="language-plaintext highlighter-rouge">0x4024b0</code> is the target characters. The base address have the string “maduiersnfotvbyl”, which is the alphabet of the target string. The output string is <em>line +81</em>, “fliyer”. So, to construct this output string using the alphabet, the offset should be <strong>9, f, e, 5, 6, 7</strong>. So, the characters of the input string must have the lower bytes in the offset series. After quering the ASCII table, one of the answer is <strong>ionuvw</strong>.</p>

<h2 id="phase-6">Phase 6</h2>
<p>Dump of assembler code for function phase_6:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>   0x00000000004010f4 &lt;+0&gt;:	push   %r14
   0x00000000004010f6 &lt;+2&gt;:	push   %r13
   0x00000000004010f8 &lt;+4&gt;:	push   %r12
   0x00000000004010fa &lt;+6&gt;:	push   %rbp
   0x00000000004010fb &lt;+7&gt;:	push   %rbx
   0x00000000004010fc &lt;+8&gt;:	sub    $0x50,%rsp
   0x0000000000401100 &lt;+12&gt;:	mov    %rsp,%r13
   0x0000000000401103 &lt;+15&gt;:	mov    %rsp,%rsi
   0x0000000000401106 &lt;+18&gt;:	callq  0x40145c &lt;read_six_numbers&gt;
   0x000000000040110b &lt;+23&gt;:	mov    %rsp,%r14
   0x000000000040110e &lt;+26&gt;:	mov    $0x0,%r12d
   0x0000000000401114 &lt;+32&gt;:	mov    %r13,%rbp
   0x0000000000401117 &lt;+35&gt;:	mov    0x0(%r13),%eax
   0x000000000040111b &lt;+39&gt;:	sub    $0x1,%eax
   0x000000000040111e &lt;+42&gt;:	cmp    $0x5,%eax
   0x0000000000401121 &lt;+45&gt;:	jbe    0x401128 &lt;phase_6+52&gt;
   0x0000000000401123 &lt;+47&gt;:	callq  0x40143a &lt;explode_bomb&gt;
   0x0000000000401128 &lt;+52&gt;:	add    $0x1,%r12d
   0x000000000040112c &lt;+56&gt;:	cmp    $0x6,%r12d
   0x0000000000401130 &lt;+60&gt;:	je     0x401153 &lt;phase_6+95&gt;
   0x0000000000401132 &lt;+62&gt;:	mov    %r12d,%ebx
   0x0000000000401135 &lt;+65&gt;:	movslq %ebx,%rax
---Type &lt;return&gt; to continue, or q &lt;return&gt; to quit---
   0x0000000000401138 &lt;+68&gt;:	mov    (%rsp,%rax,4),%eax
   0x000000000040113b &lt;+71&gt;:	cmp    %eax,0x0(%rbp)
   0x000000000040113e &lt;+74&gt;:	jne    0x401145 &lt;phase_6+81&gt;
   0x0000000000401140 &lt;+76&gt;:	callq  0x40143a &lt;explode_bomb&gt;
   0x0000000000401145 &lt;+81&gt;:	add    $0x1,%ebx
   0x0000000000401148 &lt;+84&gt;:	cmp    $0x5,%ebx
   0x000000000040114b &lt;+87&gt;:	jle    0x401135 &lt;phase_6+65&gt;
   0x000000000040114d &lt;+89&gt;:	add    $0x4,%r13
   0x0000000000401151 &lt;+93&gt;:	jmp    0x401114 &lt;phase_6+32&gt;
   0x0000000000401153 &lt;+95&gt;:	lea    0x18(%rsp),%rsi
   0x0000000000401158 &lt;+100&gt;:	mov    %r14,%rax
   0x000000000040115b &lt;+103&gt;:	mov    $0x7,%ecx
   0x0000000000401160 &lt;+108&gt;:	mov    %ecx,%edx
   0x0000000000401162 &lt;+110&gt;:	sub    (%rax),%edx
   0x0000000000401164 &lt;+112&gt;:	mov    %edx,(%rax)
   0x0000000000401166 &lt;+114&gt;:	add    $0x4,%rax
   0x000000000040116a &lt;+118&gt;:	cmp    %rsi,%rax
   0x000000000040116d &lt;+121&gt;:	jne    0x401160 &lt;phase_6+108&gt;
   0x000000000040116f &lt;+123&gt;:	mov    $0x0,%esi
   0x0000000000401174 &lt;+128&gt;:	jmp    0x401197 &lt;phase_6+163&gt;
   0x0000000000401176 &lt;+130&gt;:	mov    0x8(%rdx),%rdx
   0x000000000040117a &lt;+134&gt;:	add    $0x1,%eax
   0x000000000040117d &lt;+137&gt;:	cmp    %ecx,%eax
---Type &lt;return&gt; to continue, or q &lt;return&gt; to quit---
   0x000000000040117f &lt;+139&gt;:	jne    0x401176 &lt;phase_6+130&gt;
   0x0000000000401181 &lt;+141&gt;:	jmp    0x401188 &lt;phase_6+148&gt;
   0x0000000000401183 &lt;+143&gt;:	mov    $0x6032d0,%edx
   0x0000000000401188 &lt;+148&gt;:	mov    %rdx,0x20(%rsp,%rsi,2)
   0x000000000040118d &lt;+153&gt;:	add    $0x4,%rsi
   0x0000000000401191 &lt;+157&gt;:	cmp    $0x18,%rsi
   0x0000000000401195 &lt;+161&gt;:	je     0x4011ab &lt;phase_6+183&gt;
   0x0000000000401197 &lt;+163&gt;:	mov    (%rsp,%rsi,1),%ecx
   0x000000000040119a &lt;+166&gt;:	cmp    $0x1,%ecx
   0x000000000040119d &lt;+169&gt;:	jle    0x401183 &lt;phase_6+143&gt;
   0x000000000040119f &lt;+171&gt;:	mov    $0x1,%eax
   0x00000000004011a4 &lt;+176&gt;:	mov    $0x6032d0,%edx
   0x00000000004011a9 &lt;+181&gt;:	jmp    0x401176 &lt;phase_6+130&gt;
   0x00000000004011ab &lt;+183&gt;:	mov    0x20(%rsp),%rbx
   0x00000000004011b0 &lt;+188&gt;:	lea    0x28(%rsp),%rax
   0x00000000004011b5 &lt;+193&gt;:	lea    0x50(%rsp),%rsi
   0x00000000004011ba &lt;+198&gt;:	mov    %rbx,%rcx
   0x00000000004011bd &lt;+201&gt;:	mov    (%rax),%rdx
   0x00000000004011c0 &lt;+204&gt;:	mov    %rdx,0x8(%rcx)
   0x00000000004011c4 &lt;+208&gt;:	add    $0x8,%rax
   0x00000000004011c8 &lt;+212&gt;:	cmp    %rsi,%rax
   0x00000000004011cb &lt;+215&gt;:	je     0x4011d2 &lt;phase_6+222&gt;
   0x00000000004011cd &lt;+217&gt;:	mov    %rdx,%rcx
---Type &lt;return&gt; to continue, or q &lt;return&gt; to quit---
   0x00000000004011d0 &lt;+220&gt;:	jmp    0x4011bd &lt;phase_6+201&gt;
   0x00000000004011d2 &lt;+222&gt;:	movq   $0x0,0x8(%rdx)
   0x00000000004011da &lt;+230&gt;:	mov    $0x5,%ebp
   0x00000000004011df &lt;+235&gt;:	mov    0x8(%rbx),%rax
   0x00000000004011e3 &lt;+239&gt;:	mov    (%rax),%eax
   0x00000000004011e5 &lt;+241&gt;:	cmp    %eax,(%rbx)
   0x00000000004011e7 &lt;+243&gt;:	jge    0x4011ee &lt;phase_6+250&gt;
   0x00000000004011e9 &lt;+245&gt;:	callq  0x40143a &lt;explode_bomb&gt;
   0x00000000004011ee &lt;+250&gt;:	mov    0x8(%rbx),%rbx
   0x00000000004011f2 &lt;+254&gt;:	sub    $0x1,%ebp
   0x00000000004011f5 &lt;+257&gt;:	jne    0x4011df &lt;phase_6+235&gt;
   0x00000000004011f7 &lt;+259&gt;:	add    $0x50,%rsp
   0x00000000004011fb &lt;+263&gt;:	pop    %rbx
   0x00000000004011fc &lt;+264&gt;:	pop    %rbp
   0x00000000004011fd &lt;+265&gt;:	pop    %r12
   0x00000000004011ff &lt;+267&gt;:	pop    %r13
   0x0000000000401201 &lt;+269&gt;:	pop    %r14
   0x0000000000401203 &lt;+271&gt;:	retq   
</code></pre></div></div>

<p>The codes before <em>line +18</em> aim to store the six integer from the beginning of the stack pointer. The codes from <em>line +32 to +93</em> aim to check all the numbers are different, and they must be below or equal to 6. The codes from <em>line +95 to +121</em> aim to replace each input number with the <code class="language-plaintext highlighter-rouge">7-x</code> where <code class="language-plaintext highlighter-rouge">x</code> is each number.</p>

<p><em>line +123 - +181</em>: for every <code class="language-plaintext highlighter-rouge">7-x</code>, if it is equal to 1 (i.e. <code class="language-plaintext highlighter-rouge">x = 6</code>), put <code class="language-plaintext highlighter-rouge">0x6032d0</code> into <code class="language-plaintext highlighter-rouge">0x20 + %rsp + 8 * index of this element</code>. So, every element now sits on <strong>8 bytes</strong> starting from addresss <code class="language-plaintext highlighter-rouge">0x20 + %rsp</code>. If it is larger than 1, calculate the difference of <code class="language-plaintext highlighter-rouge">7-x</code> and 1 as <code class="language-plaintext highlighter-rouge">y</code>. There is a <strong>linked list</strong> in the stack, which starting from the address <code class="language-plaintext highlighter-rouge">0x6032d0</code>, since the following memory dump:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>   (gdb) x/2wx 0x6032d8
   0x6032d8 &lt;node1+8&gt;:	0x006032e0	0x00000000
   (gdb) x/2wx 0x6032e8
   0x6032e8 &lt;node2+8&gt;:	0x006032f0	0x00000000
   (gdb) x/2wx 0x6032f8
   0x6032f8 &lt;node3+8&gt;:	0x00603300	0x00000000
   (gdb) x/2wx 0x603308
   0x603308 &lt;node4+8&gt;:	0x00603310	0x00000000
   (gdb) x/2wx 0x603318
   0x603318 &lt;node5+8&gt;:	0x00603320	0x00000000
   (gdb) x/2wx 0x603328
   0x603328 &lt;node6+8&gt;:	0x00000000	0x00000000
</code></pre></div></div>
<p>The code will store the pointer to the linked list node at the <code class="language-plaintext highlighter-rouge">0x20 + %rsp + 8 * index of this element</code>. The node index is <code class="language-plaintext highlighter-rouge">y</code> for every element.</p>

<p><strong>line +183 - +222</strong>: starting from <code class="language-plaintext highlighter-rouge">0x20 + %rsp</code>, for every pointer, set the ith pointer’s next (in the memory) as the (i+1)th pointer. And set the 6th pointer’s next as <code class="language-plaintext highlighter-rouge">0x0</code>.</p>

<p><strong>line +230 - end</strong>: the value stored at the first pointer to the sixth pointer should be descending. So, we must check the values stored at each pointer.</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>(gdb) x/wx 0x6032d0
0x6032d0 &lt;node1&gt;:	0x0000014c
(gdb) x/wx 0x6032e0
0x6032e0 &lt;node2&gt;:	0x000000a8
(gdb) x/wx 0x6032f0
0x6032f0 &lt;node3&gt;:	0x0000039c
(gdb) x/wx 0x603300
0x603300 &lt;node4&gt;:	0x000002b3
(gdb) x/wx 0x603310
0x603310 &lt;node5&gt;:	0x000001dd
(gdb) x/wx 0x603320
0x603320 &lt;node6&gt;:	0x000001bb
</code></pre></div></div>
<p>node3 &gt; node4 &gt; node5 &gt; node6 &gt; node1 &gt; node2<br />
So, the first element has difference 2, the node4 pointer has difference 3, …<br />
So, \(7 - x - 1 = 2 =&gt; x = 4\), \(7 - x - 1 = 3 =&gt; x = 3\), …
The answer is <strong>4 3 2 1 6 5</strong>.</p>

<hr />
<h1 id="finally">Finally</h1>
<p>This lab is so painful… Tortures my mind and eyes to decode the assembly language.<br />
But congratulations to myself!
<img src="/files/CSAPP_labs/bomblab/doneRecord.png" alt="happy" /></p>]]></content><author><name>Wuzhou Du</name><email>wudu@ucsd.edu</email></author><category term="CSAPP" /><category term="bomb lab" /><category term="cool thing" /><summary type="html"><![CDATA[Phase 4 Dump of assembler code for function phase_4:]]></summary></entry><entry><title type="html">CSAPP Bomb Lab - 1</title><link href="https://wuzhou.is-a.dev/posts/2024/07/csapp/bomblab-day1&2" rel="alternate" type="text/html" title="CSAPP Bomb Lab - 1" /><published>2024-07-02T00:00:00-07:00</published><updated>2024-07-02T00:00:00-07:00</updated><id>https://wuzhou.is-a.dev/posts/2024/07/csapp/CSAPP-bomblab1</id><content type="html" xml:base="https://wuzhou.is-a.dev/posts/2024/07/csapp/bomblab-day1&amp;2"><![CDATA[<p>Recently I begin to write some posts to record my efforts (and pain) during the study.</p>

<h1 id="preparation">Preparation</h1>
<ol>
  <li>get the bomb lab resource from <a href="https://csapp.cs.cmu.edu/3e/labs.html">CMU CSAPP official website</a>. I download the <code class="language-plaintext highlighter-rouge">README.md</code>, <code class="language-plaintext highlighter-rouge">Writeup</code> and <code class="language-plaintext highlighter-rouge">Self-Study Handout</code>. The hints in the <code class="language-plaintext highlighter-rouge">Writeup</code> pdf is helpful, which directs me to browse some useful tools’ handbook in this lab, e.g. <a href="https://csapp.cs.cmu.edu/2e/docs/gdbnotes-x86-64.pdf">2-pages GDB handbook</a>.</li>
  <li>If you are a self-studier and non-CMU student like me, you can skip most of the README instructions about how CMU students should get their unique bomb and submit their project. For others, the only thing you have to do is to <strong>decompress the handout, find the binary exe file, and begin to defuse the bomb.</strong></li>
</ol>

<h1 id="try">Try</h1>
<p>After opening the <code class="language-plaintext highlighter-rouge">bomb.c</code> file and browsing the code structure, I find that the bomb has 6 phases, which menas 6 sub-challenges to conquer. So let’s do it step by step.</p>

<h2 id="phase-1">Phase 1</h2>
<p>For every phase, the first thing to do is <code class="language-plaintext highlighter-rouge">gdb bomb</code> to enter gdb mode. Then, <code class="language-plaintext highlighter-rouge">disas phase_{x}</code> to disassemble the phase_x function and decode it.</p>

<p>Dump of assembler code for function phase_1:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>   0x0000000000400ee0 &lt;+0&gt;:     sub    $0x8,%rsp
   0x0000000000400ee4 &lt;+4&gt;:     mov    $0x402400,%esi
   0x0000000000400ee9 &lt;+9&gt;:     callq  0x401338 &lt;strings_not_equal&gt;
   0x0000000000400eee &lt;+14&gt;:    test   %eax,%eax
   0x0000000000400ef0 &lt;+16&gt;:    je     0x400ef7 &lt;phase_1+23&gt;
   0x0000000000400ef2 &lt;+18&gt;:    callq  0x40143a &lt;explode_bomb&gt;
   0x0000000000400ef7 &lt;+23&gt;:    add    $0x8,%rsp
   0x0000000000400efb &lt;+27&gt;:    retq   
</code></pre></div></div>
<p>We can see the core part is the <code class="language-plaintext highlighter-rouge">strings_not_equal</code> function. If the return value of the function is zero, phase 1 will pass. So, lets dive into <code class="language-plaintext highlighter-rouge">strings_not_equal</code>.</p>

<blockquote>
  <p>Q: Why does the stack pointer deduced by 8 at first?<br />
A: For memory alignment. Every time before calling procedure, you have to ensure the stack pointer is <strong>16x</strong>. We assume that before entering <code class="language-plaintext highlighter-rouge">phase_1</code>, the stack pointer is aligned. After calling <code class="language-plaintext highlighter-rouge">phase_1</code>, the <code class="language-plaintext highlighter-rouge">%rsp</code> will be deduced by 8 because the returning address is pushed into stack. So, we need to deduct another 8 to align it.</p>
</blockquote>

<h3 id="strings_not_eqaul">strings_not_eqaul</h3>
<p>Dump of assembler code for function strings_not_equal:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>   0x0000000000401338 &lt;+0&gt;:     push   %r12
   0x000000000040133a &lt;+2&gt;:     push   %rbp
   0x000000000040133b &lt;+3&gt;:     push   %rbx
   0x000000000040133c &lt;+4&gt;:     mov    %rdi,%rbx
   0x000000000040133f &lt;+7&gt;:     mov    %rsi,%rbp
   0x0000000000401342 &lt;+10&gt;:    callq  0x40131b &lt;string_length&gt;
   0x0000000000401347 &lt;+15&gt;:    mov    %eax,%r12d
   0x000000000040134a &lt;+18&gt;:    mov    %rbp,%rdi
   0x000000000040134d &lt;+21&gt;:    callq  0x40131b &lt;string_length&gt;
   0x0000000000401352 &lt;+26&gt;:    mov    $0x1,%edx
   0x0000000000401357 &lt;+31&gt;:    cmp    %eax,%r12d
   0x000000000040135a &lt;+34&gt;:    jne    0x40139b &lt;strings_not_equal+99&gt;
   0x000000000040135c &lt;+36&gt;:    movzbl (%rbx),%eax
   0x000000000040135f &lt;+39&gt;:    test   %al,%al
   0x0000000000401361 &lt;+41&gt;:    je     0x401388 &lt;strings_not_equal+80&gt;
   0x0000000000401363 &lt;+43&gt;:    cmp    0x0(%rbp),%al
   0x0000000000401366 &lt;+46&gt;:    je     0x401372 &lt;strings_not_equal+58&gt;
   0x0000000000401368 &lt;+48&gt;:    jmp    0x40138f &lt;strings_not_equal+87&gt;
   0x000000000040136a &lt;+50&gt;:    cmp    0x0(%rbp),%al
   0x000000000040136d &lt;+53&gt;:    nopl   (%rax)
   0x0000000000401370 &lt;+56&gt;:    jne    0x401396 &lt;strings_not_equal+94&gt;
   0x0000000000401372 &lt;+58&gt;:    add    $0x1,%rbx
   0x0000000000401376 &lt;+62&gt;:    add    $0x1,%rbp
   0x000000000040137a &lt;+66&gt;:    movzbl (%rbx),%eax
   0x000000000040137d &lt;+69&gt;:    test   %al,%al
   0x000000000040137f &lt;+71&gt;:    jne    0x40136a &lt;strings_not_equal+50&gt;
   0x0000000000401381 &lt;+73&gt;:    mov    $0x0,%edx
   0x0000000000401386 &lt;+78&gt;:    jmp    0x40139b &lt;strings_not_equal+99&gt;
   0x0000000000401388 &lt;+80&gt;:    mov    $0x0,%edx
   0x000000000040138d &lt;+85&gt;:    jmp    0x40139b &lt;strings_not_equal+99&gt;
   0x000000000040138f &lt;+87&gt;:    mov    $0x1,%edx
   0x0000000000401394 &lt;+92&gt;:    jmp    0x40139b &lt;strings_not_equal+99&gt;
   0x0000000000401396 &lt;+94&gt;:    mov    $0x1,%edx
   0x000000000040139b &lt;+99&gt;:    mov    %edx,%eax
   0x000000000040139d &lt;+101&gt;:   pop    %rbx
   0x000000000040139e &lt;+102&gt;:   pop    %rbp
   0x000000000040139f &lt;+103&gt;:   pop    %r12
   0x00000000004013a1 &lt;+105&gt;:   retq   
</code></pre></div></div>
<p>From the name of the function, we can guess it compares two strings. One parameter must be the string in <code class="language-plaintext highlighter-rouge">%rsi</code>, the other must be the string in <code class="language-plaintext highlighter-rouge">%rdi</code>.</p>

<p>For the <a href="#string_length"><code class="language-plaintext highlighter-rouge">string_length</code></a> function, we can guess it calculates the length of the string.</p>

<p>If the length of strings are not equal, return 1.</p>

<p>Then come to <em>line +36</em>: move the memory content in <code class="language-plaintext highlighter-rouge">%rbx</code> to <code class="language-plaintext highlighter-rouge">%eax</code> and zero-extended to 64 bits. Now <code class="language-plaintext highlighter-rouge">%rax</code> lies the firs 4 characters of the input string. The following code is just to ensure the input is not a <code class="language-plaintext highlighter-rouge">\n</code>, and the input must be the same as the string located at <code class="language-plaintext highlighter-rouge">0x402400</code>, which is indicated in the second line of <code class="language-plaintext highlighter-rouge">phase_1</code>.</p>

<p><strong>Answer: “Border relations with Canada have never been better.”</strong></p>

<h3 id="string_length">string_length</h3>
<p>Dump of assembler code for function string_length:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>   0x000000000040131b &lt;+0&gt;:     cmpb   $0x0,(%rdi)
   0x000000000040131e &lt;+3&gt;:     je     0x401332 &lt;string_length+23&gt;
   0x0000000000401320 &lt;+5&gt;:     mov    %rdi,%rdx
   0x0000000000401323 &lt;+8&gt;:     add    $0x1,%rdx
   0x0000000000401327 &lt;+12&gt;:    mov    %edx,%eax
   0x0000000000401329 &lt;+14&gt;:    sub    %edi,%eax
   0x000000000040132b &lt;+16&gt;:    cmpb   $0x0,(%rdx)
   0x000000000040132e &lt;+19&gt;:    jne    0x401323 &lt;string_length+8&gt;
   0x0000000000401330 &lt;+21&gt;:    repz retq 
   0x0000000000401332 &lt;+23&gt;:    mov    $0x0,%eax
   0x0000000000401337 &lt;+28&gt;:    retq   
</code></pre></div></div>
<p><code class="language-plaintext highlighter-rouge">%rdi</code> must store the <strong>address of the beginning character of the string</strong>, because the memory accessing method. If this is a null ending in C, procedure ends and return 0. The following code is a loop, counting the number of non-null characters and returning the count.</p>

<h2 id="phase-2">Phase 2</h2>
<p>Dump of assembler code for function phase_2:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>   0x0000000000400efc &lt;+0&gt;:	push   %rbp
   0x0000000000400efd &lt;+1&gt;:	push   %rbx
   0x0000000000400efe &lt;+2&gt;:	sub    $0x28,%rsp
   0x0000000000400f02 &lt;+6&gt;:	mov    %rsp,%rsi
   0x0000000000400f05 &lt;+9&gt;:	callq  0x40145c &lt;read_six_numbers&gt;
   0x0000000000400f0a &lt;+14&gt;:	cmpl   $0x1,(%rsp)
   0x0000000000400f0e &lt;+18&gt;:	je     0x400f30 &lt;phase_2+52&gt;
   0x0000000000400f10 &lt;+20&gt;:	callq  0x40143a &lt;explode_bomb&gt;
   0x0000000000400f15 &lt;+25&gt;:	jmp    0x400f30 &lt;phase_2+52&gt;
   0x0000000000400f17 &lt;+27&gt;:	mov    -0x4(%rbx),%eax
   0x0000000000400f1a &lt;+30&gt;:	add    %eax,%eax
   0x0000000000400f1c &lt;+32&gt;:	cmp    %eax,(%rbx)
   0x0000000000400f1e &lt;+34&gt;:	je     0x400f25 &lt;phase_2+41&gt;
   0x0000000000400f20 &lt;+36&gt;:	callq  0x40143a &lt;explode_bomb&gt;
   0x0000000000400f25 &lt;+41&gt;:	add    $0x4,%rbx
   0x0000000000400f29 &lt;+45&gt;:	cmp    %rbp,%rbx
   0x0000000000400f2c &lt;+48&gt;:	jne    0x400f17 &lt;phase_2+27&gt;
   0x0000000000400f2e &lt;+50&gt;:	jmp    0x400f3c &lt;phase_2+64&gt;
   0x0000000000400f30 &lt;+52&gt;:	lea    0x4(%rsp),%rbx
   0x0000000000400f35 &lt;+57&gt;:	lea    0x18(%rsp),%rbp
   0x0000000000400f3a &lt;+62&gt;:	jmp    0x400f17 &lt;phase_2+27&gt;
   0x0000000000400f3c &lt;+64&gt;:	add    $0x28,%rsp
---Type &lt;return&gt; to continue, or q &lt;return&gt; to quit---
   0x0000000000400f40 &lt;+68&gt;:	pop    %rbx
   0x0000000000400f41 &lt;+69&gt;:	pop    %rbp
   0x0000000000400f42 &lt;+70&gt;:	retq   
</code></pre></div></div>
<p>Now we have to be familiar with some registers. <img src="https://wuzhoudu.github.io/files/CSAPP_labs/bomblab/commonRegisters.png" alt="image from textbook" /> The <code class="language-plaintext highlighter-rouge">%rdi</code> register is responsible for passing the argument, so other 5 registers! Here, the <code class="language-plaintext highlighter-rouge">%rsi</code> is the second argument, so we can dive into the <code class="language-plaintext highlighter-rouge">read_six_numbers</code> procedure to see how the stack pointer works.</p>

<p>After that, the only thing we need to do is simulation, and I find that the wanted input is <strong>1 2 4 8 16 32</strong>.</p>

<h3 id="read_six_numbers">read_six_numbers</h3>
<p>Dump of assembler code for function read_six_numbers:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>   0x000000000040145c &lt;+0&gt;:	sub    $0x18,%rsp
   0x0000000000401460 &lt;+4&gt;:	mov    %rsi,%rdx
   0x0000000000401463 &lt;+7&gt;:	lea    0x4(%rsi),%rcx
   0x0000000000401467 &lt;+11&gt;:	lea    0x14(%rsi),%rax
   0x000000000040146b &lt;+15&gt;:	mov    %rax,0x8(%rsp)
   0x0000000000401470 &lt;+20&gt;:	lea    0x10(%rsi),%rax
   0x0000000000401474 &lt;+24&gt;:	mov    %rax,(%rsp)
   0x0000000000401478 &lt;+28&gt;:	lea    0xc(%rsi),%r9
   0x000000000040147c &lt;+32&gt;:	lea    0x8(%rsi),%r8
   0x0000000000401480 &lt;+36&gt;:	mov    $0x4025c3,%esi
   0x0000000000401485 &lt;+41&gt;:	mov    $0x0,%eax
   0x000000000040148a &lt;+46&gt;:	callq  0x400bf0 &lt;__isoc99_sscanf@plt&gt;
   0x000000000040148f &lt;+51&gt;:	cmp    $0x5,%eax
   0x0000000000401492 &lt;+54&gt;:	jg     0x401499 &lt;read_six_numbers+61&gt;
   0x0000000000401494 &lt;+56&gt;:	callq  0x40143a &lt;explode_bomb&gt;
   0x0000000000401499 &lt;+61&gt;:	add    $0x18,%rsp
   0x000000000040149d &lt;+65&gt;:	retq   
</code></pre></div></div>
<p>The core part of this procedure is to understand what is <code class="language-plaintext highlighter-rouge">sscanf</code> in C. Here, we should pass 8 parameters to it. However, when the number of arugments is over 6, we have to store the extra parameters into the stack, like line <em>+15</em> and <em>+24</em>. You can find all the arguments based on the image above, accoridng to ABI (Application Binary Interface), the traditional usage of registers.</p>

<p>So, <code class="language-plaintext highlighter-rouge">read_six_numbers</code> procedure just parse 6 decimal numbers from the input string and stores them in the stack. After decoding what this procedure is about, we can go back to <code class="language-plaintext highlighter-rouge">phase_2</code> procedure.</p>

<h2 id="phase-3">Phase 3</h2>
<p>Dump of assembler code for function phase_3:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>   0x0000000000400f43 &lt;+0&gt;:	sub    $0x18,%rsp
   0x0000000000400f47 &lt;+4&gt;:	lea    0xc(%rsp),%rcx
   0x0000000000400f4c &lt;+9&gt;:	lea    0x8(%rsp),%rdx
   0x0000000000400f51 &lt;+14&gt;:	mov    $0x4025cf,%esi
   0x0000000000400f56 &lt;+19&gt;:	mov    $0x0,%eax
   0x0000000000400f5b &lt;+24&gt;:	callq  0x400bf0 &lt;__isoc99_sscanf@plt&gt;
   0x0000000000400f60 &lt;+29&gt;:	cmp    $0x1,%eax
   0x0000000000400f63 &lt;+32&gt;:	jg     0x400f6a &lt;phase_3+39&gt;
   0x0000000000400f65 &lt;+34&gt;:	callq  0x40143a &lt;explode_bomb&gt;
   0x0000000000400f6a &lt;+39&gt;:	cmpl   $0x7,0x8(%rsp)
   0x0000000000400f6f &lt;+44&gt;:	ja     0x400fad &lt;phase_3+106&gt;
   0x0000000000400f71 &lt;+46&gt;:	mov    0x8(%rsp),%eax
   0x0000000000400f75 &lt;+50&gt;:	jmpq   *0x402470(,%rax,8)
   0x0000000000400f7c &lt;+57&gt;:	mov    $0xcf,%eax
   0x0000000000400f81 &lt;+62&gt;:	jmp    0x400fbe &lt;phase_3+123&gt;
   0x0000000000400f83 &lt;+64&gt;:	mov    $0x2c3,%eax
   0x0000000000400f88 &lt;+69&gt;:	jmp    0x400fbe &lt;phase_3+123&gt;
   0x0000000000400f8a &lt;+71&gt;:	mov    $0x100,%eax
   0x0000000000400f8f &lt;+76&gt;:	jmp    0x400fbe &lt;phase_3+123&gt;
   0x0000000000400f91 &lt;+78&gt;:	mov    $0x185,%eax
   0x0000000000400f96 &lt;+83&gt;:	jmp    0x400fbe &lt;phase_3+123&gt;
   0x0000000000400f98 &lt;+85&gt;:	mov    $0xce,%eax
---Type &lt;return&gt; to continue, or q &lt;return&gt; to quit---
   0x0000000000400f9d &lt;+90&gt;:	jmp    0x400fbe &lt;phase_3+123&gt;
   0x0000000000400f9f &lt;+92&gt;:	mov    $0x2aa,%eax
   0x0000000000400fa4 &lt;+97&gt;:	jmp    0x400fbe &lt;phase_3+123&gt;
   0x0000000000400fa6 &lt;+99&gt;:	mov    $0x147,%eax
   0x0000000000400fab &lt;+104&gt;:	jmp    0x400fbe &lt;phase_3+123&gt;
   0x0000000000400fad &lt;+106&gt;:	callq  0x40143a &lt;explode_bomb&gt;
   0x0000000000400fb2 &lt;+111&gt;:	mov    $0x0,%eax
   0x0000000000400fb7 &lt;+116&gt;:	jmp    0x400fbe &lt;phase_3+123&gt;
   0x0000000000400fb9 &lt;+118&gt;:	mov    $0x137,%eax
   0x0000000000400fbe &lt;+123&gt;:	cmp    0xc(%rsp),%eax
   0x0000000000400fc2 &lt;+127&gt;:	je     0x400fc9 &lt;phase_3+134&gt;
   0x0000000000400fc4 &lt;+129&gt;:	callq  0x40143a &lt;explode_bomb&gt;
   0x0000000000400fc9 &lt;+134&gt;:	add    $0x18,%rsp
   0x0000000000400fcd &lt;+138&gt;:	retq   
</code></pre></div></div>
<p>This phase is interesting. The code before <em>line +24</em> is easy to understand, which is parsing two decimal from the input and storing them in the stack. The most interesting part is <em>line +50</em>, where the code unconditionally jumps to an address according to the first input number! I was stuck for quite a while to thinking. After some trials like the first number is 0, 1, …, I find that there are multiple groups of input leading to a valid result.</p>

<p>Have a quick browse on how this phase is defused successfully, we can find that there are many ways jumping to <em>line +123</em>, which compares the second input with <code class="language-plaintext highlighter-rouge">%eax</code>. What’s more, there are many lines moving immediate number to <code class="language-plaintext highlighter-rouge">%eax</code> like <em>line +57, +64</em>, etc. We can guess the interesting <em>line +50</em> is to jump to one of the line.</p>

<p>After trying <code class="language-plaintext highlighter-rouge">(gdb) x/w 0x402470: 0x00400f7c</code>, it refers to <em>line +57</em>, which means the base of the jumping is <em>line +57</em>. So, one of the answers: <strong>1 311</strong> comes out naturally.</p>]]></content><author><name>Wuzhou Du</name><email>wudu@ucsd.edu</email></author><category term="CSAPP" /><category term="bomb lab" /><category term="cool thing" /><summary type="html"><![CDATA[Recently I begin to write some posts to record my efforts (and pain) during the study.]]></summary></entry></feed>