{"componentChunkName":"component---src-templates-post-template-js","path":"/ctf-arm-assembly-bigginer-en","result":{"data":{"markdownRemark":{"id":"4492a191-028e-5b98-b630-3743bc27a85f","html":"<blockquote>\n<p>This page has been machine-translated from the <a href=\"/ctf-arm-assembly-bigginer\">original page</a>.</p>\n</blockquote>\n<h2 id=\"introduction\" style=\"position:relative;\"><a href=\"#introduction\" aria-label=\"introduction permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Introduction</h2>\n<p>I was working on <a href=\"https://play.picoctf.org/practice\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">picoCTF 2021</a>, which ran until 2021/3/31.\nI was aiming to solve all the Reversing challenges, but unfortunately fell short.</p>\n<p>Among the picoCTF 2021 Reversing problems, the “ARMssembly” series was extremely educational, so I’ll write a writeup for it here.</p>\n<h2 id=\"what-i-learned\" style=\"position:relative;\"><a href=\"#what-i-learned\" aria-label=\"what i learned permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What I Learned</h2>\n<ol>\n<li>ARM assembly mnemonics</li>\n<li>Key points for manually decompiling assembly code</li>\n</ol>\n<h2 id=\"approach-for-the-armssembly-series\" style=\"position:relative;\"><a href=\"#approach-for-the-armssembly-series\" aria-label=\"approach for the armssembly series permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Approach for the ARMssembly Series</h2>\n<p>The “ARMssembly” series consisted of 5 problems in total, and the approach was the same for all of them, so I’ll describe it upfront.</p>\n<p>The “ARMssembly” series could be solved with the following steps:</p>\n<ol>\n<li>Read through the provided assembly code</li>\n<li>\n<p>Trace the flow from the main function</p>\n<ul>\n<li>Google things as needed</li>\n<li>Generate assembly from C source code on an ARM-based environment (a Raspberry Pi) and compare with the challenge code</li>\n</ul>\n</li>\n<li>Solve</li>\n</ol>\n<p>Let’s start with the first problem.</p>\n<h2 id=\"armssembly-0\" style=\"position:relative;\"><a href=\"#armssembly-0\" aria-label=\"armssembly 0 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>ARMssembly 0</h2>\n<h3 id=\"problem\" style=\"position:relative;\"><a href=\"#problem\" aria-label=\"problem permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Problem</h3>\n<p>This problem gives two variables and asks for the final output.</p>\n<blockquote>\n<p>Description</p>\n<p>What integer does this program print with arguments <code class=\"language-text\">4112417903</code> and <code class=\"language-text\">1169092511</code>?</p>\n</blockquote>\n<div class=\"gatsby-highlight\" data-language=\"assembly\"><pre class=\"language-assembly\"><code class=\"language-assembly\">; Challenge code\n.arch armv8-a\n.file&quot;chall.c&quot;\n.text\n.align2\n.globalfunc1\n.typefunc1, %function\nfunc1:\nsubsp, sp, #16\nstrw0, [sp, 12]\nstrw1, [sp, 8]\nldrw1, [sp, 12]\nldrw0, [sp, 8]\ncmpw1, w0\nbls.L2\nldrw0, [sp, 12]\nb.L3\n.L2:\nldrw0, [sp, 8]\n.L3:\naddsp, sp, 16\nret\n.sizefunc1, .-func1\n.section.rodata\n.align3\n.LC0:\n.string&quot;Result: %ld\\n&quot;\n.text\n.align2\n.globalmain\n.typemain, %function\nmain:\nstpx29, x30, [sp, -48]!\naddx29, sp, 0\nstrx19, [sp, 16]\nstrw0, [x29, 44]\nstrx1, [x29, 32]\nldrx0, [x29, 32]\naddx0, x0, 8\nldrx0, [x0]\nblatoi\nmovw19, w0\nldrx0, [x29, 32]\naddx0, x0, 16\nldrx0, [x0]\nblatoi\nmovw1, w0\nmovw0, w19\nblfunc1\nmovw1, w0\nadrpx0, .LC0\naddx0, x0, :lo12:.LC0\nblprintf\nmovw0, 0\nldrx19, [sp, 16]\nldpx29, x30, [sp], 48\nret\n.sizemain, .-main\n.ident&quot;GCC: (Ubuntu/Linaro 7.5.0-3ubuntu1~18.04) 7.5.0&quot;\n.section.note.GNU-stack,&quot;&quot;,@progbits</code></pre></div>\n<h3 id=\"reading-main\" style=\"position:relative;\"><a href=\"#reading-main\" aria-label=\"reading main permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Reading main</h3>\n<p>Let’s trace from main first.\nIt’s fairly long, but I focused on these items:</p>\n<ul>\n<li><code class=\"language-text\">bl atoi</code></li>\n<li><code class=\"language-text\">bl atoi</code></li>\n<li><code class=\"language-text\">bl func1</code></li>\n<li><code class=\"language-text\">bl printf</code></li>\n</ul>\n<p><code class=\"language-text\">bl</code> stands for “Branch with Link” and can be understood as something like a CALL instruction.\nFor details, see <a href=\"https://www.mztn.org/dragon/arm6408cond.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Arm64(ARMv8) Assembly Programming (08) Branch Instructions</a>.</p>\n<p>In short, it jumps to the address written right after <code class=\"language-text\">bl</code> and returns when it hits a <code class=\"language-text\">RET</code> instruction.</p>\n<p>Since there are two <code class=\"language-text\">atoi</code> calls, it’s clear that the program converts the received variables to numbers, passes them as arguments to <code class=\"language-text\">func1</code>, and displays the return value with <code class=\"language-text\">printf</code>.</p>\n<h3 id=\"verifying-with-c-code\" style=\"position:relative;\"><a href=\"#verifying-with-c-code\" aria-label=\"verifying with c code permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Verifying with C code</h3>\n<p>Having read this far, let me write the reverse-engineered C code for the main function and verify my understanding.</p>\n<p>Here’s what I came up with:</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">include</span> <span class=\"token string\">&lt;stdio.h></span></span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">include</span> <span class=\"token string\">&lt;stdlib.h></span></span>\n\n<span class=\"token keyword\">unsigned</span> <span class=\"token keyword\">int</span> <span class=\"token function\">func1</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">unsigned</span> <span class=\"token keyword\">int</span> n1<span class=\"token punctuation\">,</span> <span class=\"token keyword\">unsigned</span> <span class=\"token keyword\">int</span> n2<span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">int</span> <span class=\"token function\">main</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">char</span> a<span class=\"token punctuation\">[</span><span class=\"token number\">128</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">char</span> b<span class=\"token punctuation\">[</span><span class=\"token number\">128</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">unsigned</span> <span class=\"token keyword\">int</span> n1 <span class=\"token operator\">=</span> <span class=\"token function\">atoi</span><span class=\"token punctuation\">(</span>a<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">unsigned</span> <span class=\"token keyword\">int</span> n2 <span class=\"token operator\">=</span> <span class=\"token function\">atoi</span><span class=\"token punctuation\">(</span>b<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">unsigned</span> <span class=\"token keyword\">int</span> ans <span class=\"token operator\">=</span> <span class=\"token function\">func1</span><span class=\"token punctuation\">(</span>n1<span class=\"token punctuation\">,</span> n2<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token function\">printf</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"%u\"</span><span class=\"token punctuation\">,</span> ans<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n    <span class=\"token keyword\">return</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Compile this on a Raspberry Pi with GCC using <code class=\"language-text\">gcc -S sample.c -o sample.lst</code> to generate an object file.</p>\n<p>Extracting just the main function, the following assembly is generated.\nComparing with the challenge code, it’s nearly identical!</p>\n<div class=\"gatsby-highlight\" data-language=\"assembly\"><pre class=\"language-assembly\"><code class=\"language-assembly\">main:\n.LFB7:\n      .cfi_startproc\n      stp     x29, x30, [sp, -48]!\n      .cfi_def_cfa_offset 48\n      .cfi_offset 29, -48\n      .cfi_offset 30, -40\n      mov     x29, sp\n      str     x0, [sp, 24]\n      str     x1, [sp, 16]\n      ldr     x0, [sp, 24]\n      bl      atoi\n      str     w0, [sp, 36]\n      ldr     x0, [sp, 16]\n      bl      atoi\n      str     w0, [sp, 40]\n      ldr     w1, [sp, 40]\n      ldr     w0, [sp, 36]\n      bl      func1\n      str     w0, [sp, 44]\n      ldr     w1, [sp, 44]\n      adrp    x0, .LC0\n      add     x0, x0, :lo12:.LC0\n      bl      printf\n      mov     w0, 0\n      ldp     x29, x30, [sp], 48\n      .cfi_restore 30\n      .cfi_restore 29\n      .cfi_def_cfa_offset 0\n      ret\n      .cfi_endproc</code></pre></div>\n<h3 id=\"reading-func1\" style=\"position:relative;\"><a href=\"#reading-func1\" aria-label=\"reading func1 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Reading func1</h3>\n<p>Next, let’s look at func1, which receives the two arguments.\nThis is the relevant part of the challenge code:</p>\n<div class=\"gatsby-highlight\" data-language=\"assembly\"><pre class=\"language-assembly\"><code class=\"language-assembly\">func1:\nsubsp, sp, #16\nstrw0, [sp, 12]\nstrw1, [sp, 8]\nldrw1, [sp, 12]\nldrw0, [sp, 8]\ncmpw1, w0\nbls.L2\nldrw0, [sp, 12]\nb.L3\n.L2:\nldrw0, [sp, 8]\n.L3:\naddsp, sp, 16\nret\n.sizefunc1, .-func1\n.section.rodata\n.align3</code></pre></div>\n<p>First, let’s look at the <code class=\"language-text\">str</code> and <code class=\"language-text\">ldr</code> instructions.</p>\n<p>Simply put, <code class=\"language-text\">str</code> is a store instruction that writes a register’s value to a specified address.\n<code class=\"language-text\">ldr</code> is a load instruction that reads a value from a specified address into a register.</p>\n<p><code class=\"language-text\">[sp, 12]</code> uses register-indirect addressing—one of the CPU’s memory addressing modes—which specifies the address obtained by adding the given offset to the stack pointer.</p>\n<p>So <code class=\"language-text\">func1</code> loads the received argument values, compares them, and branches based on the result.</p>\n<p>Let’s look at the branch instruction <code class=\"language-text\">bls</code>.\n<code class=\"language-text\">ls</code> stands for “lower or same (&#x3C;=)“.</p>\n<p>From this, func1 can be written in C as:</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">include</span> <span class=\"token string\">&lt;stdio.h></span></span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">include</span> <span class=\"token string\">&lt;stdlib.h></span></span>\n\n<span class=\"token keyword\">unsigned</span> <span class=\"token keyword\">int</span> <span class=\"token function\">func1</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">unsigned</span> <span class=\"token keyword\">int</span> n1<span class=\"token punctuation\">,</span> <span class=\"token keyword\">unsigned</span> <span class=\"token keyword\">int</span> n2<span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>n2 <span class=\"token operator\">></span> n1<span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">{</span>\n        <span class=\"token keyword\">return</span> n1<span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n    <span class=\"token keyword\">else</span>\n    <span class=\"token punctuation\">{</span>\n        <span class=\"token keyword\">return</span> n2<span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>I wrote <code class=\"language-text\">bls</code> as “lower or same (&#x3C;=)”, but the jump to the branch target happens when the IF condition is <em>not</em> satisfied, so the C condition expression is <code class=\"language-text\">n1 > n2</code>.</p>\n<p>Also, function arguments are pushed onto the stack from the last one first, so the value stored in <code class=\"language-text\">[sp, 12]</code>, which is accessed first, corresponds to the first argument (n1).</p>\n<p>Let’s generate the object file from this code:</p>\n<div class=\"gatsby-highlight\" data-language=\"assembly\"><pre class=\"language-assembly\"><code class=\"language-assembly\">func1:\n.LFB6:\n.cfi_startproc\nsubsp, sp, #16\n.cfi_def_cfa_offset 16\nstrw0, [sp, 12]\nstrw1, [sp, 8]\nldrw1, [sp, 8]\nldrw0, [sp, 12]\ncmpw1, w0\nbls.L2\nldrw0, [sp, 12]\nb.L3\n.L2:\nldrw0, [sp, 8]\n.L3:\naddsp, sp, 16\n.cfi_def_cfa_offset 0\nret\n.cfi_endproc</code></pre></div>\n<p>The assembly nearly matches the challenge code, confirming our understanding is correct.</p>\n<p>Finally, running the compiled binary with the given arguments produces the number sequence that is the FLAG.</p>\n<h2 id=\"armssembly-1\" style=\"position:relative;\"><a href=\"#armssembly-1\" aria-label=\"armssembly 1 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>ARMssembly 1</h2>\n<h3 id=\"problem-1\" style=\"position:relative;\"><a href=\"#problem-1\" aria-label=\"problem 1 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Problem</h3>\n<p>This problem asks for the argument that makes the program print “win”.\nThe code is a bit longer than the first problem.</p>\n<blockquote>\n<p>Description</p>\n<p>For what argument does this program print <code class=\"language-text\">win</code> with variables <code class=\"language-text\">81</code>, <code class=\"language-text\">0</code> and <code class=\"language-text\">3</code>?</p>\n</blockquote>\n<div class=\"gatsby-highlight\" data-language=\"assembly\"><pre class=\"language-assembly\"><code class=\"language-assembly\">.arch armv8-a\n.file&quot;chall_1.c&quot;\n.text\n.align2\n.globalfunc\n.typefunc, %function\nfunc:\nsubsp, sp, #32\nstrw0, [sp, 12]\nmovw0, 81\nstrw0, [sp, 16]\nstrwzr, [sp, 20]\nmovw0, 3\nstrw0, [sp, 24]\nldrw0, [sp, 20]\nldrw1, [sp, 16]\nlslw0, w1, w0\nstrw0, [sp, 28]\nldrw1, [sp, 28]\nldrw0, [sp, 24]\nsdivw0, w1, w0\nstrw0, [sp, 28]\nldrw1, [sp, 28]\nldrw0, [sp, 12]\nsubw0, w1, w0\nstrw0, [sp, 28]\nldrw0, [sp, 28]\naddsp, sp, 32\nret\n.sizefunc, .-func\n.section.rodata\n.align3\n.LC0:\n.string&quot;You win!&quot;\n.align3\n.LC1:\n.string&quot;You Lose :(&quot;\n.text\n.align2\n.globalmain\n.typemain, %function\nmain:\nstpx29, x30, [sp, -48]!\naddx29, sp, 0\nstrw0, [x29, 28]\nstrx1, [x29, 16]\nldrx0, [x29, 16]\naddx0, x0, 8\nldrx0, [x0]\nblatoi\nstrw0, [x29, 44]\nldrw0, [x29, 44]\nblfunc\ncmpw0, 0\nbne.L4\nadrpx0, .LC0\naddx0, x0, :lo12:.LC0\nblputs\nb.L6\n.L4:\nadrpx0, .LC1\naddx0, x0, :lo12:.LC1\nblputs\n.L6:\nnop\nldpx29, x30, [sp], 48\nret\n.sizemain, .-main\n.ident&quot;GCC: (Ubuntu/Linaro 7.5.0-3ubuntu1~18.04) 7.5.0&quot;\n.section.note.GNU-stack,&quot;&quot;,@progbits</code></pre></div>\n<h3 id=\"reading-main-1\" style=\"position:relative;\"><a href=\"#reading-main-1\" aria-label=\"reading main 1 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Reading main</h3>\n<p>Let’s trace through main. Instructions introduced in previous problems are skipped.</p>\n<p>The important part in main is the last section.</p>\n<p>Just like the previous problem, it receives an argument, passes it to the func function, and then determines whether to display “win” or “lose” based on the return value compared to 0.</p>\n<p>Let me write this part out in C.</p>\n<p><code class=\"language-text\">bne .L4</code> means “not equal”.\nIn other words, if <code class=\"language-text\">func1's return value != 0</code>, it jumps to the address specified by <code class=\"language-text\">.L4</code>.</p>\n<h3 id=\"verifying-with-c-code-1\" style=\"position:relative;\"><a href=\"#verifying-with-c-code-1\" aria-label=\"verifying with c code 1 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Verifying with C code</h3>\n<p>Having read this far, let me write the reverse-engineered C code for main and verify.</p>\n<p>Here’s what I came up with:</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">include</span> <span class=\"token string\">&lt;stdio.h></span></span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">include</span> <span class=\"token string\">&lt;stdlib.h></span></span>\n\n<span class=\"token keyword\">unsigned</span> <span class=\"token keyword\">int</span> <span class=\"token function\">func1</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">unsigned</span> <span class=\"token keyword\">int</span> n1<span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">int</span> <span class=\"token function\">main</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">char</span> a<span class=\"token punctuation\">[</span><span class=\"token number\">128</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">unsigned</span> <span class=\"token keyword\">int</span> n1 <span class=\"token operator\">=</span> <span class=\"token function\">atoi</span><span class=\"token punctuation\">(</span>a<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">unsigned</span> <span class=\"token keyword\">int</span> ret <span class=\"token operator\">=</span> <span class=\"token function\">func1</span><span class=\"token punctuation\">(</span>n1<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>ret <span class=\"token operator\">==</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">{</span>\n        <span class=\"token function\">printf</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"win\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n    <span class=\"token keyword\">else</span>\n    <span class=\"token punctuation\">{</span>\n        <span class=\"token function\">printf</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"lose\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n\n    <span class=\"token keyword\">return</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Compiling this on a Raspberry Pi with <code class=\"language-text\">gcc -S sample.c -o sample.lst</code> generates the following assembly for main.\nComparing with the challenge code, they match.</p>\n<div class=\"gatsby-highlight\" data-language=\"assembly\"><pre class=\"language-assembly\"><code class=\"language-assembly\">main:\n.LFB7:\n.cfi_startproc\nstpx29, x30, [sp, -48]!\n.cfi_def_cfa_offset 48\n.cfi_offset 29, -48\n.cfi_offset 30, -40\nmovx29, sp\nstrx0, [sp, 24]\nldrx0, [sp, 24]\nblatoi\nstrw0, [sp, 40]\nldrw0, [sp, 40]\nblfunc1\nstrw0, [sp, 44]\nldrw0, [sp, 44]\ncmpw0, 0\nbne.L4\nadrpx0, .LC0\naddx0, x0, :lo12:.LC0\nblprintf\nb.L5\n.L4:\nadrpx0, .LC1\naddx0, x0, :lo12:.LC1\nblprintf\n.L5:\nmovw0, 0\nldpx29, x30, [sp], 48\n.cfi_restore 30\n.cfi_restore 29\n.cfi_def_cfa_offset 0\nret\n.cfi_endproc</code></pre></div>\n<h3 id=\"reading-func1-1\" style=\"position:relative;\"><a href=\"#reading-func1-1\" aria-label=\"reading func1 1 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Reading func1</h3>\n<p>Now I know the win condition: if the return value of func1 with the given argument is 0, “win” is displayed.</p>\n<p>Let’s look at func1:</p>\n<div class=\"gatsby-highlight\" data-language=\"assembly\"><pre class=\"language-assembly\"><code class=\"language-assembly\">func:\nsubsp, sp, #32\n; 1. Store the argument in [sp, 12]\nstrw0, [sp, 12]\n\n; 2. Store 81 in [sp, 16]\nmovw0, 81\nstrw0, [sp, 16]\n\n; 3. Store 0 in [sp, 20]\nstrwzr, [sp, 20]\n\n; 4. Store 3 in [sp, 24]\nmovw0, 3\nstrw0, [sp, 24]\n\n; 5. Load [sp, 20] and [sp, 16], then left-shift\nldrw0, [sp, 20]\nldrw1, [sp, 16]\nlslw0, w1, w0\nstrw0, [sp, 28]\n\n; 6. Divide result of 5 by [sp, 24]\nldrw1, [sp, 28]\nldrw0, [sp, 24]\nsdivw0, w1, w0\nstrw0, [sp, 28]\n\n; 7. Subtract the argument passed to func1 from result of 6, then return\nldrw1, [sp, 28]\nldrw0, [sp, 12]\nsubw0, w1, w0\n\nstrw0, [sp, 28]\nldrw0, [sp, 28]\naddsp, sp, 32\nret</code></pre></div>\n<p>I’ve annotated the challenge code to make the variable flow clearer.</p>\n<ol>\n<li>Store the argument in [sp, 12]</li>\n<li>Store 81 in [sp, 16]</li>\n<li>Store 0 in [sp, 20]</li>\n<li>Store 3 in [sp, 24]</li>\n</ol>\n<p>Steps 1, 2, and 4 were covered earlier, so I’ll skip them.</p>\n<p>For step 3, using <code class=\"language-text\">wzr</code> is a zero register expression.\nThis allows storing 0 directly at the specified address without going through another register.</p>\n<p>Reference: <a href=\"https://developer.arm.com/documentation/den0024/a/An-Introduction-to-the-ARMv8-Instruction-Sets/The-ARMv8-instruction-sets/Registers\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">ARM Cortex-A Series Programmer’s Guide for ARMv8-A</a></p>\n<p>Now let me look at the operations after the variable assignments.</p>\n<ol start=\"5\">\n<li>Load [sp, 20] and [sp, 16], then left-shift</li>\n</ol>\n<p><code class=\"language-text\">lsl</code> is a logical shift left instruction.\nData is shifted left by the specified amount, and vacated bits are filled with 0.</p>\n<p>Steps 6 and 7 take the result of this left shift, divide by 3, and subtract the argument.\nIn other words, the argument that makes this final expression equal to 0 is the flag.</p>\n<p>We can now solve this easily, but for completeness, let me write func1 in C too.\nWhen I compiled this, I couldn’t exactly reproduce the <code class=\"language-text\">sdiv</code> part for some reason, but it’s approximately correct:</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token keyword\">unsigned</span> <span class=\"token keyword\">int</span> <span class=\"token function\">func1</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">unsigned</span> <span class=\"token keyword\">int</span> n1<span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">unsigned</span> <span class=\"token keyword\">int</span> n2 <span class=\"token operator\">=</span> <span class=\"token number\">81</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">unsigned</span> <span class=\"token keyword\">int</span> n3 <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">unsigned</span> <span class=\"token keyword\">int</span> n4 <span class=\"token operator\">=</span> <span class=\"token number\">3</span><span class=\"token punctuation\">;</span>\n\n    <span class=\"token keyword\">int</span> ret<span class=\"token punctuation\">;</span>\n    ret <span class=\"token operator\">=</span> n2 <span class=\"token operator\">&lt;&lt;</span> n3<span class=\"token punctuation\">;</span>\n    ret <span class=\"token operator\">=</span> ret <span class=\"token operator\">/</span> <span class=\"token number\">3</span><span class=\"token punctuation\">;</span>\n    ret <span class=\"token operator\">=</span> ret <span class=\"token operator\">-</span> n1<span class=\"token punctuation\">;</span>\n\n    <span class=\"token keyword\">return</span> ret<span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>The argument that makes func1 return 0 is the number sequence that is the FLAG.</p>\n<h2 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h2>\n<p>Carefully working through assembly code and rewriting it in C by hand was a very enjoyable experience.\nThanks to the problem authors.</p>\n<p>I may add writeups for the remaining problems later.</p>\n<p>As a side note, here are some books I recommend for studying assembly:</p>\n<h3 id=\"recommended-books\" style=\"position:relative;\"><a href=\"#recommended-books\" aria-label=\"recommended books permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Recommended Books</h3>\n<ul>\n<li>\n<p><a href=\"https://amzn.to/3mceJHH\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">大熱血！ アセンブラ入門</a></p>\n<ul>\n<li>Pros: Covers pretty much everything. I used this book as a reference when reading ARM assembly.</li>\n<li>Cons: Thick, expensive, seems difficult. I haven’t finished it yet lol.</li>\n</ul>\n</li>\n<li>\n<p><a href=\"https://amzn.to/2PsWQbI\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">解析魔法少女 美咲ちゃん マジカル・オープン!</a></p>\n<ul>\n<li>Pros: Misaki-chan is cute. Reads like a story. Easy to understand for beginners.</li>\n<li>Cons: Old (Windows XP era)</li>\n</ul>\n</li>\n<li>\n<p><a href=\"https://amzn.to/3fyymZo\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">[改訂3版]基本情報技術者らくらく突破CASL II</a></p>\n<ul>\n<li>Pros: Recommended especially for people who have “absolutely no idea about assembly.” Easy to understand with 8-bit assembly, and lots of practice problems (for the basic IT engineer exam) and explanations are available.</li>\n<li>Cons: Can’t verify behavior by writing your own C source code. Differences from 32-bit assembly need to be studied separately.</li>\n</ul>\n</li>\n</ul>","fields":{"slug":"/ctf-arm-assembly-bigginer-en","tagSlugs":["/tag/ctf-en/","/tag/reversing-en/","/tag/pico-ctf-en/","/tag/english/"]},"frontmatter":{"date":"2021-10-04","description":"Learning ARM assembly through the ARMssembly series of Reversing challenges in picoCTF 2021.","tags":["CTF (en)","Reversing (en)","picoCTF (en)","English"],"title":"Learning ARM Assembly through CTF","socialImage":{"publicURL":"/static/dc4d8b7f8795f3c3d3489d9957d155f2/no-image.png"}}}},"pageContext":{"slug":"/ctf-arm-assembly-bigginer-en"}},"staticQueryHashes":["251939775","401334301","825871152"]}