{"componentChunkName":"component---src-templates-post-template-js","path":"/ctf-elf-training-en","result":{"data":{"markdownRemark":{"id":"89ebc942-c73c-58a7-8590-9d6b83877d6a","html":"<blockquote>\n<p>This page has been machine-translated from the <a href=\"/ctf-elf-training\">original page</a>.</p>\n</blockquote>\n<p>This article explains basic ELF binary analysis techniques for CTF beginners.</p>\n<p>This article was written as study material for a workshop I personally host.</p>\n<!-- omit in toc -->\n<h2 id=\"table-of-contents\" style=\"position:relative;\"><a href=\"#table-of-contents\" aria-label=\"table of contents 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>Table of Contents</h2>\n<ul>\n<li><a href=\"#purpose-of-this-article\">Purpose of This Article</a></li>\n<li><a href=\"#target-audience\">Target Audience</a></li>\n<li>\n<p><a href=\"#prerequisites\">Prerequisites</a></p>\n<ul>\n<li><a href=\"#downloading-the-challenge-file\">Downloading the Challenge File</a></li>\n</ul>\n</li>\n<li>\n<p><a href=\"#performing-surface-level-analysis\">Performing Surface-Level Analysis</a></p>\n<ul>\n<li><a href=\"#file\">file</a></li>\n<li><a href=\"#strings\">strings</a></li>\n<li><a href=\"#readelf\">readelf</a></li>\n</ul>\n</li>\n<li>\n<p><a href=\"#running-the-binary\">Running the Binary</a></p>\n<ul>\n<li><a href=\"#what-is-an-elf-file\">What is an ELF File?</a></li>\n<li><a href=\"#granting-execute-permission\">Granting Execute Permission</a></li>\n</ul>\n</li>\n<li>\n<p><a href=\"#performing-static-analysis\">Performing Static Analysis</a></p>\n<ul>\n<li><a href=\"#radare2\">radare2</a></li>\n</ul>\n</li>\n<li>\n<p><a href=\"#analyzing-the-main-function-with-ghidra\">Analyzing the main Function with Ghidra</a></p>\n<ul>\n<li><a href=\"#finding-the-entry-point\">Finding the Entry Point</a></li>\n<li><a href=\"#about-rva--va--offset\">About RVA / VA / Offset</a></li>\n<li><a href=\"#identifying-the-main-function-from-the-entry-point\">Identifying the main Function from the Entry Point</a></li>\n<li><a href=\"#examining-the-decompiled-main-function\">Examining the Decompiled main Function</a></li>\n<li><a href=\"#reading-standard-input\">Reading Standard Input</a></li>\n<li><a href=\"#removing-the-newline-character\">Removing the Newline Character</a></li>\n<li><a href=\"#xor-encrypting-the-string-in-a-loop\">XOR-Encrypting the String in a Loop</a></li>\n<li><a href=\"#checking-the-encrypted-byte-sequence\">Checking the Encrypted Byte Sequence</a></li>\n<li><a href=\"#retrieving-values-from-the-data-section\">Retrieving Values from the Data Section</a></li>\n</ul>\n</li>\n<li>\n<p><a href=\"#analyzing-the-xor-encryption-function\">Analyzing the XOR Encryption Function</a></p>\n<ul>\n<li><a href=\"#using-ida-free\">Using IDA Free</a></li>\n<li><a href=\"#understanding-the-xor-encryption-behavior\">Understanding the XOR Encryption Behavior</a></li>\n</ul>\n</li>\n<li>\n<p><a href=\"#performing-dynamic-analysis-with-gdb\">Performing Dynamic Analysis with gdb</a></p>\n<ul>\n<li><a href=\"#finding-the-gdb-load-address\">Finding the gdb Load Address</a></li>\n<li><a href=\"#setting-breakpoints\">Setting Breakpoints</a></li>\n<li><a href=\"#changing-the-ghidra-image-base\">Changing the Ghidra Image Base</a></li>\n<li><a href=\"#commonly-used-gdb-commands-partial-list\">Commonly Used gdb Commands (Partial List)</a></li>\n<li><a href=\"#planning-the-analysis-approach\">Planning the Analysis Approach</a></li>\n<li><a href=\"#about-x86_64-architecture-registers\">About x86_64 Architecture Registers</a></li>\n<li><a href=\"#identifying-the-function-return-value\">Identifying the Function Return Value</a></li>\n</ul>\n</li>\n<li>\n<p><a href=\"#automating-gdb\">Automating gdb</a></p>\n<ul>\n<li><a href=\"#using-gdb-python\">Using gdb-python</a></li>\n<li><a href=\"#obtaining-the-flag\">Obtaining the Flag</a></li>\n</ul>\n</li>\n<li>\n<p><a href=\"#bonus-useful-gdb-techniques\">Bonus: Useful gdb Techniques</a></p>\n<ul>\n<li><a href=\"#bypassing-conditional-branches-by-modifying-eflags\">Bypassing Conditional Branches by Modifying EFLAGS</a></li>\n<li><a href=\"#extracting-information-from-memory\">Extracting Information from Memory</a></li>\n</ul>\n</li>\n<li><a href=\"#summary\">Summary</a></li>\n<li>\n<p><a href=\"#recommended-books--websites\">Recommended Books / Websites</a></p>\n<ul>\n<li><a href=\"#books\">Books</a></li>\n<li><a href=\"#websites\">Websites</a></li>\n</ul>\n</li>\n</ul>\n<h2 id=\"purpose-of-this-article\" style=\"position:relative;\"><a href=\"#purpose-of-this-article\" aria-label=\"purpose of this article 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>Purpose of This Article</h2>\n<ul>\n<li>This article introduces ELF binary analysis techniques using GDB and Ghidra, aimed at beginners interested in binary analysis.</li>\n</ul>\n<h2 id=\"target-audience\" style=\"position:relative;\"><a href=\"#target-audience\" aria-label=\"target audience 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>Target Audience</h2>\n<ul>\n<li>People interested in CTF or binary analysis</li>\n<li>\n<p>People with a basic understanding of computer architecture and ELF files</p>\n<p>※ This article focuses on how to use GDB and Ghidra, so detailed explanations of foundational concepts are not provided.</p>\n<p>※ The assumed level is roughly: you can read C and Python at a casual level, you know the terms CPU, registers, memory, etc. and their general purposes, and you can set up a Linux environment on your own.</p>\n</li>\n</ul>\n<h2 id=\"prerequisites\" style=\"position:relative;\"><a href=\"#prerequisites\" aria-label=\"prerequisites 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>Prerequisites</h2>\n<p>You need to have the following applications installed on a Linux environment with an x86_64 platform.</p>\n<ul>\n<li><a href=\"https://ghidra-sre.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Ghidra</a></li>\n<li><a href=\"https://hex-rays.com/ida-free/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">IDA Free</a></li>\n<li><a href=\"https://www.gnu.org/software/gdb/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">gdb</a></li>\n<li><a href=\"https://github.com/longld/peda\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">gdb-peda</a></li>\n<li><a href=\"https://github.com/radareorg/radare2/releases\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">radare2</a></li>\n</ul>\n<p>The steps in this article have been reproduced in the following environment, but minor differences in application versions should not be an issue.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token comment\"># Environment</span>\nUbuntu20.04 64bit\nGhidra <span class=\"token number\">10.1</span>-BETA\nIDA Free <span class=\"token number\">7.6</span>\ngdb <span class=\"token punctuation\">(</span>Ubuntu <span class=\"token number\">9.2</span>-0ubuntu1~20.04<span class=\"token punctuation\">)</span> <span class=\"token number\">9.2</span>\nradare2 <span class=\"token number\">4.2</span>.1</code></pre></div>\n<p>radare2 and IDA Free are installed for reference, but since they are only briefly introduced, it is fine if you do not install them.</p>\n<h3 id=\"downloading-the-challenge-file\" style=\"position:relative;\"><a href=\"#downloading-the-challenge-file\" aria-label=\"downloading the challenge file 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>Downloading the Challenge File</h3>\n<p>The binary used in this article can be downloaded from the link below.</p>\n<p>Challenge binary: <a href=\"/file/revvy_chevy\">revvy_chevy</a></p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token comment\"># Description</span>\n<span class=\"token number\">1</span> Flag, <span class=\"token number\">2</span> Flag, Red Flag, Blue Flag. Encrypting flags is as easy as making a rhyme</code></pre></div>\n<p>Note: I contacted the MetaCTF organizers and received their permission to redistribute the challenge binary on this blog, on the condition that the MetaCTF URL is included in the article.</p>\n<p>CTF link: <a href=\"https://metactf.com/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">MetaCTF | Cybersecurity Capture the Flag Competition</a></p>\n<p>If you are considering further redistribution, please remember to include the link above.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">wget</span> https://kashiwaba-yuki.com/file/revvy_chevy</code></pre></div>\n<p>Run the above command in your Linux environment to download the challenge binary.</p>\n<p>Now, let’s get started with the analysis.</p>\n<h2 id=\"performing-surface-level-analysis\" style=\"position:relative;\"><a href=\"#performing-surface-level-analysis\" aria-label=\"performing surface level analysis 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>Performing Surface-Level Analysis</h2>\n<p>First, let’s perform a surface-level analysis of the downloaded binary.</p>\n<p>Surface-level analysis is a technique for analyzing the information held by the file itself.</p>\n<p>We perform surface-level analysis to get an overview of a file before conducting static analysis (such as reverse engineering) or dynamic analysis (actually running the program).</p>\n<p>This time, we’ll use the <code class=\"language-text\">file</code> and <code class=\"language-text\">strings</code> commands to investigate the file type and readable strings in the binary.</p>\n<h3 id=\"file\" style=\"position:relative;\"><a href=\"#file\" aria-label=\"file 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>file</h3>\n<p>The <code class=\"language-text\">file</code> command retrieves the file type by performing the following checks in order, returning the result based on the first match:</p>\n<ul>\n<li>Filesystem tests</li>\n<li>Magic number tests</li>\n<li>Language tests</li>\n</ul>\n<p>The actual output looks like this.</p>\n<p>In this case, the binary was identified as a 64-bit ELF binary.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token comment\"># Use the file command to check the type of the binary</span>\n$ <span class=\"token function\">file</span> revvy_chevy \nrevvy_chevy: ELF <span class=\"token number\">64</span>-bit LSB shared object, x86-64, version <span class=\"token number\">1</span> <span class=\"token punctuation\">(</span>SYSV<span class=\"token punctuation\">)</span>, dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID<span class=\"token punctuation\">[</span>sha1<span class=\"token punctuation\">]</span><span class=\"token operator\">=</span>271c2040193241b806252d57ce67d110b6c8e78c, <span class=\"token keyword\">for</span> GNU/Linux <span class=\"token number\">3.2</span>.0, stripped</code></pre></div>\n<p>For details on the <code class=\"language-text\">file</code> command, refer to the following:</p>\n<p>Reference: <a href=\"https://linuxjm.osdn.jp/html/file/man1/file.1.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Man page of FILE</a></p>\n<p>In particular, the filesystem test, which has the highest priority among <code class=\"language-text\">file</code> command tests, is based on the result of the <code class=\"language-text\">stat</code> system call.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">$ <span class=\"token function\">stat</span> revvy_chevy \nFile: revvy_chevy\nSize: <span class=\"token number\">14480</span>     \nBlocks: <span class=\"token number\">32</span>         \nIO Block: <span class=\"token number\">4096</span>   \nregular <span class=\"token function\">file</span>\nDevice: fd00h/64768dInode: <span class=\"token number\">918235</span>      Links: <span class=\"token number\">1</span>\nAccess: <span class=\"token punctuation\">(</span>0664/-rw-rw-r--<span class=\"token punctuation\">)</span>  Uid: <span class=\"token punctuation\">(</span> <span class=\"token number\">1000</span>/  ubuntu<span class=\"token punctuation\">)</span>   Gid: <span class=\"token punctuation\">(</span> <span class=\"token number\">1000</span>/  ubuntu<span class=\"token punctuation\">)</span>\nAccess: <span class=\"token number\">2021</span>-12-11 <span class=\"token number\">12</span>:58:48.991810253 +0900\nModify: <span class=\"token number\">2021</span>-12-06 <span class=\"token number\">23</span>:58:52.000000000 +0900\nChange: <span class=\"token number\">2021</span>-12-11 <span class=\"token number\">12</span>:58:45.839677244 +0900\nBirth: -</code></pre></div>\n<p>Reference: <a href=\"https://linuxjm.osdn.jp/html/LDP_man-pages/man2/stat.2.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Man page of STAT</a></p>\n<h3 id=\"strings\" style=\"position:relative;\"><a href=\"#strings\" aria-label=\"strings 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>strings</h3>\n<p>The <code class=\"language-text\">strings</code> command lets you list all readable strings (printable byte values in the ASCII range) contained in the binary of the target file.</p>\n<p>By default, it outputs readable strings of 4 or more characters.</p>\n<p>In surface-level analysis, the output of the <code class=\"language-text\">strings</code> command can yield useful information for analysis, such as the names of libraries and functions being used, and any text defined in the binary.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token comment\"># Use the strings command to retrieve readable strings in the binary</span>\n$ strings revvy_chevy \n<span class=\"token punctuation\">{</span><span class=\"token punctuation\">{</span> omitted <span class=\"token punctuation\">}</span><span class=\"token punctuation\">}</span></code></pre></div>\n<p>For details, refer to the manual page.</p>\n<p>Reference: <a href=\"https://linuxjm.osdn.jp/html/GNU_binutils/man1/strings.1.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Man page of strings</a></p>\n<h3 id=\"readelf\" style=\"position:relative;\"><a href=\"#readelf\" aria-label=\"readelf 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>readelf</h3>\n<p>The <code class=\"language-text\">readelf</code> command is used to retrieve an overview of an ELF file.</p>\n<p>It can display information from the ELF header, section headers, segments, and more in a formatted way.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">$ readelf -a revvy_chevy</code></pre></div>\n<p>Reference: <a href=\"https://man7.org/linux/man-pages/man1/readelf.1.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">readelf(1) - Linux manual page</a></p>\n<h2 id=\"running-the-binary\" style=\"position:relative;\"><a href=\"#running-the-binary\" aria-label=\"running the binary 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>Running the Binary</h2>\n<p>From the surface-level analysis, we now know that the downloaded file is an executable in ELF format.</p>\n<h3 id=\"what-is-an-elf-file\" style=\"position:relative;\"><a href=\"#what-is-an-elf-file\" aria-label=\"what is an elf file 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 is an ELF File?</h3>\n<p>By the way, ELF stands for <code class=\"language-text\">Executable and Linkable Format</code>, an executable file format commonly used on Linux and UNIX systems.</p>\n<p>ELF binaries have an ELF header that is 52 bytes long (for 32-bit) or 64 bytes long (for 64-bit).</p>\n<p>Knowing the ELF header format can be very useful when analyzing ELF binaries.</p>\n<p>For details on the ELF header, the English Wikipedia article is very comprehensive and easy to understand, so it is highly recommended.</p>\n<p>Reference: <a href=\"https://en.wikipedia.org/wiki/Executable_and_Linkable_Format\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Executable and Linkable Format - Wikipedia</a></p>\n<h3 id=\"granting-execute-permission\" style=\"position:relative;\"><a href=\"#granting-execute-permission\" aria-label=\"granting execute permission 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>Granting Execute Permission</h3>\n<p>On Linux systems, files have permissions set, and access is restricted from two perspectives: the owner (user and group) and the permitted operations (read/write/execute).</p>\n<p>In a default Linux system configuration, the file we downloaded does not yet have execute permission, so we need to grant it first.</p>\n<p>The file owner and permissions can be confirmed with the <code class=\"language-text\">ls -l</code> command.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">$ <span class=\"token function\">ls</span> -l\ntotal <span class=\"token number\">16</span>\n-rw-rw-r-- <span class=\"token number\">1</span> ubuntu ubuntu <span class=\"token number\">14480</span> <span class=\"token number\">12</span>月  <span class=\"token number\">6</span> <span class=\"token number\">23</span>:58 revvy_chevy</code></pre></div>\n<p>For details, refer to the following:</p>\n<p>Reference: <a href=\"https://linuxjm.osdn.jp/html/gnumaniak/man1/ls.1.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Man page of LS</a></p>\n<p>Reference: <a href=\"https://linuxize.com/post/understanding-linux-file-permissions/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Understanding Linux File Permissions | Linuxize</a></p>\n<p>To grant execute permission to the file, use the <code class=\"language-text\">chmod +x</code> command.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">$ <span class=\"token function\">chmod</span> +x revvy_chevy \n$ <span class=\"token function\">ls</span> -l\ntotal <span class=\"token number\">16</span>\n-rwxrwxr-x <span class=\"token number\">1</span> ubuntu ubuntu <span class=\"token number\">14480</span> <span class=\"token number\">12</span>月  <span class=\"token number\">6</span> <span class=\"token number\">23</span>:58 revvy_chevy</code></pre></div>\n<p>Reference: <a href=\"https://linuxjm.osdn.jp/html/GNU_fileutils/man1/chmod.1.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Man page of CHMOD</a></p>\n<p>As shown above, when you check permissions with <code class=\"language-text\">ls -l</code> and see <code class=\"language-text\">x</code>, you know that execute permission has been granted.</p>\n<p>Now let’s run it.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">$ ./revvy_chevy \nWhat<span class=\"token string\">'s the flag? &lt;input text>\nThat'</span>s not it<span class=\"token punctuation\">..</span>.</code></pre></div>\n<p>Running the challenge binary prompts you for a string input.</p>\n<p>When we enter an arbitrary string, <code class=\"language-text\">That's not it...</code> is displayed.</p>\n<p>From this result, we can infer that the program is likely comparing the input string against the Flag internally.</p>\n<h2 id=\"performing-static-analysis\" style=\"position:relative;\"><a href=\"#performing-static-analysis\" aria-label=\"performing static analysis 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>Performing Static Analysis</h2>\n<h3 id=\"radare2\" style=\"position:relative;\"><a href=\"#radare2\" aria-label=\"radare2 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>radare2</h3>\n<p>radare2 is a feature-rich analysis tool that allows you to invoke various operations from the CUI, including disassembly, binary patching, data comparison and search, and decompilation.</p>\n<p>Launch radare2 with <code class=\"language-text\">radare2 revvy_chevy</code> and start analysis by calling the <code class=\"language-text\">aaa</code> command.</p>\n<p>After analysis completes, calling the <code class=\"language-text\">afl</code> command lists the functions in the binary.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">$ radare2 revvy_chevy\n<span class=\"token comment\"># aaa command</span>\n<span class=\"token punctuation\">[</span>0x00001100<span class=\"token punctuation\">]</span><span class=\"token operator\">></span> aaa\n<span class=\"token punctuation\">[</span>Cannot <span class=\"token function\">find</span> <span class=\"token keyword\">function</span> at 0x00001100 sym. and entry0 <span class=\"token punctuation\">(</span>aa<span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">[</span>x<span class=\"token punctuation\">]</span> Analyze all flags starting with sym. and entry0 <span class=\"token punctuation\">(</span>aa<span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">[</span>x<span class=\"token punctuation\">]</span> Analyze <span class=\"token keyword\">function</span> calls <span class=\"token punctuation\">(</span>aac<span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">[</span>x<span class=\"token punctuation\">]</span> Analyze len bytes of instructions <span class=\"token keyword\">for</span> references <span class=\"token punctuation\">(</span>aar<span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">[</span>x<span class=\"token punctuation\">]</span> Check <span class=\"token keyword\">for</span> objc references\n<span class=\"token punctuation\">[</span>x<span class=\"token punctuation\">]</span> Check <span class=\"token keyword\">for</span> vtables\n<span class=\"token punctuation\">[</span>x<span class=\"token punctuation\">]</span> Type matching analysis <span class=\"token keyword\">for</span> all functions <span class=\"token punctuation\">(</span>aaft<span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">[</span>x<span class=\"token punctuation\">]</span> Propagate noreturn information\n<span class=\"token punctuation\">[</span>x<span class=\"token punctuation\">]</span> Use -AA or aaaa to perform additional experimental analysis.\n\n<span class=\"token comment\"># List functions with afl command</span>\n<span class=\"token punctuation\">[</span>0x00001100<span class=\"token punctuation\">]</span><span class=\"token operator\">></span> afl\n0x00001130    <span class=\"token number\">4</span> <span class=\"token number\">41</span>   -<span class=\"token operator\">></span> <span class=\"token number\">34</span>   fcn.00001130</code></pre></div>\n<p>Note that the radare2 help is quite clear, so calling help with an option like <code class=\"language-text\">a -h</code> is a good idea.</p>\n<p>The following website is also helpful:</p>\n<p>Reference: <a href=\"https://book.rada.re/first_steps/commandline_flags.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Command-line Flags - The Official Radare2 Book</a></p>\n<p>To disassemble and decompile a function with radare2, run the following commands:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token comment\"># Running a function offset moves to that address</span>\n<span class=\"token punctuation\">[</span>0x00001100<span class=\"token punctuation\">]</span><span class=\"token operator\">></span> afl\n0x00001130    <span class=\"token number\">4</span> <span class=\"token number\">41</span>   -<span class=\"token operator\">></span> <span class=\"token number\">34</span>   fcn.00001130\n<span class=\"token punctuation\">[</span>0x00001100<span class=\"token punctuation\">]</span><span class=\"token operator\">></span> 0x00001130\n\n<span class=\"token comment\"># Running pdf at the function start address gives the disassembly result</span>\n<span class=\"token punctuation\">[</span>0x00001130<span class=\"token punctuation\">]</span><span class=\"token operator\">></span> pdf\n            <span class=\"token punctuation\">;</span> CALL XREF from entry.fini0 @ +0x27\n <span class=\"token number\">34</span>: fcn.00001130 <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n           0x00001130      488d3de12e00.  lea rdi, qword <span class=\"token punctuation\">[</span>0x00004018<span class=\"token punctuation\">]</span>\n           0x00001137      488d05da2e00.  lea rax, qword <span class=\"token punctuation\">[</span>0x00004018<span class=\"token punctuation\">]</span>\n           0x0000113e      4839f8         <span class=\"token function\">cmp</span> rax, rdi\n       ┌─<span class=\"token operator\">&lt;</span> 0x00001141      <span class=\"token number\">7415</span>           je 0x1158\n       │   0x00001143      488b058e2e00.  mov rax, qword <span class=\"token punctuation\">[</span>reloc._ITM_deregisterTMCloneTable<span class=\"token punctuation\">]</span> <span class=\"token punctuation\">;</span> <span class=\"token punctuation\">[</span>0x3fd8:8<span class=\"token punctuation\">]</span><span class=\"token operator\">=</span><span class=\"token number\">0</span>\n       │   0x0000114a      4885c0         <span class=\"token builtin class-name\">test</span> rax, rax\n      ┌──<span class=\"token operator\">&lt;</span> 0x0000114d      <span class=\"token number\">7409</span>           je 0x1158\n      ││   0x0000114f      ffe0           jmp rax\n<span class=\"token punctuation\">..</span>\n      ││   <span class=\"token punctuation\">;</span> CODE XREFS from fcn.00001130 @ 0x1141, 0x114d\n      └└─<span class=\"token operator\">></span> 0x00001158      c3             ret\n\n<span class=\"token comment\"># Running pdc at the function start address gives the decompiled result</span>\n<span class=\"token punctuation\">[</span>0x00001130<span class=\"token punctuation\">]</span><span class=\"token operator\">></span> pdc\n<span class=\"token keyword\">function</span> fcn.<span class=\"token function-name function\">00001130</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    //  <span class=\"token number\">4</span> basic blocks\n    loc_0x1130:\n         //CALL XREF from entry.fini0 @ +0x27\n       rdi <span class=\"token operator\">=</span> qword <span class=\"token punctuation\">[</span>0x00004018<span class=\"token punctuation\">]</span>\n       rax <span class=\"token operator\">=</span> qword <span class=\"token punctuation\">[</span>0x00004018<span class=\"token punctuation\">]</span>\n       var <span class=\"token operator\">=</span> rax - rdi\n       <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>var<span class=\"token punctuation\">)</span> goto 0x1158    //likely\n       <span class=\"token punctuation\">{</span>\n        loc_0x1158:\n           //CODE XREFS from fcn.00001130 @ 0x1141, 0x114d\n           <span class=\"token builtin class-name\">return</span>\n        loc_0x1143:\n           rax <span class=\"token operator\">=</span> qword <span class=\"token punctuation\">[</span>reloc._ITM_deregisterTMCloneTable<span class=\"token punctuation\">]</span> //<span class=\"token punctuation\">[</span>0x3fd8:8<span class=\"token punctuation\">]</span><span class=\"token operator\">=</span><span class=\"token number\">0</span>\n           var <span class=\"token operator\">=</span> rax <span class=\"token operator\">&amp;</span> rax\n           <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>var<span class=\"token punctuation\">)</span> goto 0x1158    //likely\n      <span class=\"token punctuation\">}</span>\n      <span class=\"token builtin class-name\">return</span><span class=\"token punctuation\">;</span>\n    loc_0x114f:\n       goto rax\n<span class=\"token punctuation\">(</span>break<span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Next, let’s check the disassembly and decompilation results from a GUI.</p>\n<h2 id=\"analyzing-the-main-function-with-ghidra\" style=\"position:relative;\"><a href=\"#analyzing-the-main-function-with-ghidra\" aria-label=\"analyzing the main function with ghidra 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>Analyzing the main Function with Ghidra</h2>\n<p>Ghidra is an open-source reverse engineering tool developed by the NSA.</p>\n<p>If you set it up using the official installation method, you can launch it with <code class=\"language-text\">ghidraRun</code>.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">$ ghidraRun</code></pre></div>\n<p>Reference: <a href=\"https://ghidra-sre.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Ghidra</a></p>\n<p>Once the Ghidra GUI launches, select [File] > [Import File] from the top left to load the challenge binary.</p>\n<p>Once loading is complete, click the imported filename to start analysis.</p>\n<p>For detailed usage of Ghidra, the help tool accessible via [Help] > [Content] is quite thorough and is highly recommended.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 500px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/d20ea907f1aa45ddb3cf7ab760d61380/0b533/image-31.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 68.33333333333333%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAARlAAAEZQAGA43XUAAADB0lEQVQ4y52TW2yURRTHv1dpfYGkkfiomNQnQKoPJrxKojEGY0KlIuVaLBCgKZKGAmIxhgcSFC3aQAiQEEhIAdOml+22QApYROXS7rL3r3vrfrvfdW/dZeHH7Nd1FfCJk/wyZ87MnJk5/xlp19YWdrRuoGXjOvbs2sa3nV9zuGsf3x3ooHP3zgo76NzTxqH2VrY3zKd18Ty2vlNL69Iatix+hS1L5rFZ0CKQPKEwiqrhCcpMujwkkjq6mbExrDl0M42VnSUWjXBk1SK+X/EqHe/X0vZuDW0NgvdqbbYvq0H6/f4Ubp/Mg0k3E7fv4AvIYgOdpGpUUVI6mpEhEAiw+5NFtCyv44uG12hcUkfj0grLFrKpYQFS8TGYmRzpdIZkUiWTzfGEZ+2fvm5YXOkbZNg5hsMxgtPpxOEcZWjYwdWbE/zQ/iXS3IpnUzwR/ecpWzqXx+WK4fUaRA3wRQqUHv277sqPeysJXzjT/5tlWiiaRc7KE/z1FwKXfmNWxEuPivb45aMdSEk9I2qkihqZmLYI6aoYZd8UpbAE6UKRsKi3p+sgkZ5ufM2fE1j1KXrvxeqGfcfECe8+9BGSw8QTSSGAECOlVUmJfjk+HYmLTTXCiop75Qd4279ClB5zfIjw2qbq/fp/EgmN/Cy6lUVRFBKJhM1//Ug0SiAkE4lEmVY0NM89oo2foZw4wnTzh2inTlYTDvzcidTXfZDLxw4wdf8uQbHQ5/Xhdj+0n4jf7ycYDBIKhexWdt0in/SRGx8htn4NVu+lioiP7Xawex/S8Y/q6P74dbSYbAdLpZKoWdb2i8WiTaFQbgtkA/0Y13pQe3swRgYoVubkc3Pzh4/vRzq7+i3ONNWjRYN2sDCbFwnFzzAN4rEouiauqaZsyu9QNdJoQm3NMEglZsS4ijITp/yeHeWE51a/wemmt8kYalWtbImXstGTh5DON73JhbX1TA6dQ74zhv/WIN7xftS/nSiuq0T+HCVxbwzdc4P4g5vIf4zYhG47CAr8E8N4bgwQ+us6F79p5ilpPrLirsV0LQAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/d20ea907f1aa45ddb3cf7ab760d61380/8ac56/image-31.webp 240w,\n/static/d20ea907f1aa45ddb3cf7ab760d61380/d3be9/image-31.webp 480w,\n/static/d20ea907f1aa45ddb3cf7ab760d61380/b0a15/image-31.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/d20ea907f1aa45ddb3cf7ab760d61380/8ff5a/image-31.png 240w,\n/static/d20ea907f1aa45ddb3cf7ab760d61380/e85cb/image-31.png 480w,\n/static/d20ea907f1aa45ddb3cf7ab760d61380/0b533/image-31.png 500w\"\n            sizes=\"(max-width: 500px) 100vw, 500px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/d20ea907f1aa45ddb3cf7ab760d61380/0b533/image-31.png\"\n            alt=\"2021/12/image-31.png\"\n            title=\"2021/12/image-31.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>If you want information in Japanese, there is not much systematically organized content on the web, so reading <a href=\"https://amzn.to/3robIsL\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Ghidra実践ガイド</a> is recommended.</p>\n<h3 id=\"finding-the-entry-point\" style=\"position:relative;\"><a href=\"#finding-the-entry-point\" aria-label=\"finding the entry point 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>Finding the Entry Point</h3>\n<p>Once the Ghidra analysis window is open, we first want to find the disassembly and decompilation results for the main function.</p>\n<p>However, when searching the Functions list in the Symbol Tree on the left side of the default screen, we could not find the main function.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 433px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/5ff45b653d46d7f7a557b5847605951b/55fc0/image-32.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 119.58333333333333%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAYCAYAAAD6S912AAAACXBIWXMAAAsTAAALEwEAmpwYAAACzElEQVQ4y52VWW/TQBRG83/hF7QgsbzBIzyCEA9QBAgQdEFsoq1Ek4pWaWkcx9mdZvMSp0lsJ8Qzh0mCaENS0mLpypY8c+a768SuXX/NlatvWV7aYGlpjVu337O8vMqNm+vcufuBe/e/8ODhFk+efuPV6wQb7/f4unlAInHEjyODVCrN1+1Ntr/H2dzcIvZufZ9Hj16y8iLByrMEaxtHfPyc49PnLFvbBXbiJb7vmSQPj0mlaxi5BsWyxXHVwXI8GpZNqVymaTuk9Qwx98SlXnqO5Vao14oMBh3+92m128QajotxsEpRy3Jca5IvVun0BvS6UsEFQsgpk3LWhBBjoOO2iPnhAKvVp1vp0LOhmpY0DUG9Keh2pFo22sRc0Fn7A+wFoZIa4NcDZD1CZiKoTE5k7sbTA8ZLYBroh30V3C6tkgc1tTilgKE8N06Tvaf/Z4BBf0DdamNlm4QW2ClBS7lr2wLHVu6rb88TWJbAbYnFCgMVw7rdoapbFCoD9lN9jpTKw8MITROqziJ0PSKZjNQGcUGXXZ8f+xYfdvJs7RVonwh6PUmoXI+iSXZH71O3/+GyH/Rx2yF9lZSKiVIzUTWyUimajteZjJ6vUAEdLyCo+ZimQFdZzijL5wVmJZopmwsqnADLZYFhTGKmZwTFkUI5C5zK+rwYOl6fsDpSKFU/ijFQ0yKKxWmFXATY9UNVDh5BYwQcklEKM8ZwrHAUgksDaw0bzXhD7jhLIVcgUJ0zk005xs4v9L+Bo/Gzn3zMgX5AXgFN01SF7DEcDs9A5Txx84G9IKDTMumcdFTdhXS7Xfye/2eCSDldyAuB/cHPf/StXDgDZ4Ch6uWJS/NH0qKxNTO+JsCLqVnkyRg4+B38y6g5b2J7LQU0cjly+TzVWgNbnWCrK+Gy5rguNcslqZeI7e7uEo/H0dK6ur0qqt3MS1vZrKBli3xKaPwCuqEqtxIWjbEAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/5ff45b653d46d7f7a557b5847605951b/8ac56/image-32.webp 240w,\n/static/5ff45b653d46d7f7a557b5847605951b/aff3a/image-32.webp 433w\"\n              sizes=\"(max-width: 433px) 100vw, 433px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/5ff45b653d46d7f7a557b5847605951b/8ff5a/image-32.png 240w,\n/static/5ff45b653d46d7f7a557b5847605951b/55fc0/image-32.png 433w\"\n            sizes=\"(max-width: 433px) 100vw, 433px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/5ff45b653d46d7f7a557b5847605951b/55fc0/image-32.png\"\n            alt=\"2021/12/image-32.png\"\n            title=\"2021/12/image-32.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>Therefore, we will identify the address of the main function from the disassembly of the entry point.</p>\n<p>The entry point is the function that is called first when an ELF binary is executed.</p>\n<p>The file offset of the entry point is defined using 8 bytes starting at byte 25 of the ELF header.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 500px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/c63d79134c96c957016e029ea8f76673/0b533/image-33.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 37.5%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAARlAAAEZQAGA43XUAAABwElEQVQoz22SS0/bUBCFvUA8V6hUYgOUR6I0sZ2EOsRJSBwnToJjO7FsJ+EpFjyE1EKR+AGsQaIVCwRqoRWb/s3TmRskWLC4Gt+ZO9+cObJU0xRUcnGUlBjWlTjK2RhKSxnoKxYK8Rr0uAUjYcKVq4jyDjy1BjtpwE4Z4ttVTax9SMJaKaIZK0Harw7QURrw0y20qTHItuGkTIRaC36mjkhr4uriEv/un/H7+g5/f/7Cnx8PeL59xNPNPYx5DZ9H5iCPfUJydAHSdsWHIxuI9DY6DNAddNJ19HSX7g0Emo09M8Rp7wjnmyf4Fh7ie/8YX4MDnFGuOJtGimDpqWWodKTdagBXMdEvMMB6AdUR5GwBDNfa8GgAr9ileitRFnWOfNemE5DHF6FOLosj7RhDIDe65EmPFbLSPCnONoRv/mpTHB7apdyg5InaVtmH/lEmhS/ACQLyOmxuSIr4ca/gicl9igzhAT7l3wI5umodW+td5GdS5N8bhXtmJFYKtA3yzsKg2Bk2FQn4pTUErjbIZ1PkhEKKHr3dfk/hJk1xaGX2yE5WyLsNaq6KAWwBQ3kDBnCOwWFuGCOyiX+ZVw+X8B+vXxjBc3ZbkgAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/c63d79134c96c957016e029ea8f76673/8ac56/image-33.webp 240w,\n/static/c63d79134c96c957016e029ea8f76673/d3be9/image-33.webp 480w,\n/static/c63d79134c96c957016e029ea8f76673/b0a15/image-33.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/c63d79134c96c957016e029ea8f76673/8ff5a/image-33.png 240w,\n/static/c63d79134c96c957016e029ea8f76673/e85cb/image-33.png 480w,\n/static/c63d79134c96c957016e029ea8f76673/0b533/image-33.png 500w\"\n            sizes=\"(max-width: 500px) 100vw, 500px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/c63d79134c96c957016e029ea8f76673/0b533/image-33.png\"\n            alt=\"2021/12/image-33.png\"\n            title=\"2021/12/image-33.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p> In little-endian format, <code class=\"language-text\">0x1100</code> is the entry point address.</p>\n<p>Using the <code class=\"language-text\">-h</code> option with the <code class=\"language-text\">readelf</code> command mentioned earlier, you can easily view the information in the ELF header.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">$ readelf -h revvy_chevy\nELF Header:\n  Magic:   7f <span class=\"token number\">45</span> 4c <span class=\"token number\">46</span> 02 01 01 00 00 00 00 00 00 00 00 00 \n  Class:                             ELF64\n  Data:                              <span class=\"token number\">2</span>'s complement, little endian\n  Version:                           <span class=\"token number\">1</span> <span class=\"token punctuation\">(</span>current<span class=\"token punctuation\">)</span>\n  OS/ABI:                            UNIX - System V\n  ABI Version:                       <span class=\"token number\">0</span>\n  Type:                              DYN <span class=\"token punctuation\">(</span>Shared object <span class=\"token function\">file</span><span class=\"token punctuation\">)</span>\n  Machine:                           Advanced Micro Devices X86-64\n  Version:                           0x1\n  Entry point address:               0x1100\n  Start of program headers:          <span class=\"token number\">64</span> <span class=\"token punctuation\">(</span>bytes into <span class=\"token function\">file</span><span class=\"token punctuation\">)</span>\n  Start of section headers:          <span class=\"token number\">12624</span> <span class=\"token punctuation\">(</span>bytes into <span class=\"token function\">file</span><span class=\"token punctuation\">)</span>\n  Flags:                             0x0\n  Size of this header:               <span class=\"token number\">64</span> <span class=\"token punctuation\">(</span>bytes<span class=\"token punctuation\">)</span>\n  Size of program headers:           <span class=\"token number\">56</span> <span class=\"token punctuation\">(</span>bytes<span class=\"token punctuation\">)</span>\n  Number of program headers:         <span class=\"token number\">13</span>\n  Size of section headers:           <span class=\"token number\">64</span> <span class=\"token punctuation\">(</span>bytes<span class=\"token punctuation\">)</span>\n  Number of section headers:         <span class=\"token number\">29</span>\n  Section header string table index: <span class=\"token number\">28</span></code></pre></div>\n<p>Now that we know the entry point address is <code class=\"language-text\">0x1100</code>, let’s open the <code class=\"language-text\">entry</code> function address from the Ghidra Symbol Tree.</p>\n<p>The disassembly and decompilation results of the <code class=\"language-text\">entry</code> function are displayed, but looking at the address, it shows <code class=\"language-text\">0x101100</code> instead of <code class=\"language-text\">0x1100</code>.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 500px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/7510255b616b62e8ea1b8b82301bd8f2/0b533/image-35.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 52.916666666666664%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAARlAAAEZQAGA43XUAAAB5UlEQVQoz32S627TQBCF/f6PhITKDwQqCClAaVDTNo5jO3Z8Xd/WXu/6Y9KmtEXASGdnbe8cn7Mz3tt3W95c3HLx3ufjp4Avq4j1JiWIS8IkZxul3O8PXF2vubz8wOrrmqtv19xsfOIwJUlKkjQjy3OiKMYzQDFCPUNcGilu8aMe/zAQK8uhsSSN43DM6HrLMBom07LgpHJhWSSfcA6vkkOhEEUnskSxCRV+1hNVo7zXhIVmX/TcbXdUlabtDEXWoaoa3Xborhe09NNEZyY8pR1+MbBNe4JkZBsPQnwiNBzUTCAkP9OCz6sVfaexi4WzqJf4rdDYhfXujs1OkVeWY9mjBsVgHMMkMAuNNnyXO7TWPhSJ0Yf1JZblEd7pQBiExHJvvdjvuoG2yXD2+bfOWX4IoTHzI+Gy/AUPH/CcLErVBMFErRyzNdT1yPHozkrAOsfN7S3z/Ez4Kp6enwiLosD3NU3rRIWhaTpyab0Wq1q6qvUkY/IH4SvHLwndQivdmqZZMMq+Z9CjFBtG6dw0SR5H9vtA1Ntz/fJPeE+qZ2lO04yidqRuR7H52ADjDGae2EfRWc7/w6tUy30Qc32T48sMJlnN/SFinx5RdUNRVWK/ZLsLSPNapqARKI7FGed9LrkoS34BMSBNxJrnD3wAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/7510255b616b62e8ea1b8b82301bd8f2/8ac56/image-35.webp 240w,\n/static/7510255b616b62e8ea1b8b82301bd8f2/d3be9/image-35.webp 480w,\n/static/7510255b616b62e8ea1b8b82301bd8f2/b0a15/image-35.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/7510255b616b62e8ea1b8b82301bd8f2/8ff5a/image-35.png 240w,\n/static/7510255b616b62e8ea1b8b82301bd8f2/e85cb/image-35.png 480w,\n/static/7510255b616b62e8ea1b8b82301bd8f2/0b533/image-35.png 500w\"\n            sizes=\"(max-width: 500px) 100vw, 500px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/7510255b616b62e8ea1b8b82301bd8f2/0b533/image-35.png\"\n            alt=\"2021/12/image-35.png\"\n            title=\"2021/12/image-35.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>This is because what Ghidra displays as an address is not the actual binary address, but rather an address called an RVA (Relative Virtual Address).</p>\n<p>An RVA is a virtual address with a base address (also called an image base) added to it.</p>\n<p>When loading an ELF file in Ghidra, if you check the [Image Base] field under [Options], you can see that the default is <code class=\"language-text\">0x100000</code>.</p>\n<p>Therefore, <code class=\"language-text\">0x101100</code>, which is the image base added to the actual virtual address <code class=\"language-text\">0x1100</code>, is what Ghidra displays as the RVA.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 500px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/83d8750d5cf9aaca6bb1b983c6853b55/0b533/image-34.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 55.00000000000001%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAARlAAAEZQAGA43XUAAACYUlEQVQoz11STW/TQBD1D6YSpWkT9SNNDy1N0gIn7hx75cSFMwRUaFrbseOPeP25tuOP2E6rIqHHeC2EhKWn9c7uzrx5b6SL8wvs7b7EYHCAQf8Aw+MjjE6HmE7GePvmGq8vzjE8OcZoeNKBzo6PDnFzc4MkTbEunlHVDR6fUjz/qiH9uL/Dz7kM22XwogDrdY4sL5BlRbf+RbvPcrHmRQk/jMDjGM0WqJvf2JRbpGUB6ev9F3ybq7DsFXTDwop5sFYubOd/eOKs/XdcH7Pvt5jNZkiTDdI8R7GuUKRbSH4QwuMZPn76jN7+Pi4vx5hMrwSmV9cYT6a4HE8wGp3h9HSE/X4fr3o9vNjZweHhAGdnI/RJqt7eLj68fwepqokzfZtyAx5FYI6DhaZj/qAQZMiqBnWhQdFNyLqFhapAJei6Dk2jM3UBz/fBefdWqpouYV3XcBwGVVFgWgZM20LEOXLSpShLJFkJ24vgex5c14VC92RZFsVbc7L1GhERkraPTyJh09SkR0JBDsZSeF5GVTe0J/ACKz+ERToGJBGnQrZlY0ksl4aBkBK1SYMggOQGMYqqQRSndMDpcUoGcWi6i7ni4E5hYG4ML+TCGM/zEZO7PrVp2zYM00RMRKqq7hi2Dt8qBgzLgUMu8iQlJGIkQoGEHlCMCjouTQAxY4zBpEQWJTRpz+lOlmUIwxDSkj0gSYhlQVrRfOV5O2eFWLtYF08oqb40BYuSDLTcAAF1w/wIFvOFzjFJIQWkTdtGW9Uhl/6trjCp/V+tViLe6ta22Wqlkuv3iyUeNIPmlgl2HnXwB7gBAouispHdAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/83d8750d5cf9aaca6bb1b983c6853b55/8ac56/image-34.webp 240w,\n/static/83d8750d5cf9aaca6bb1b983c6853b55/d3be9/image-34.webp 480w,\n/static/83d8750d5cf9aaca6bb1b983c6853b55/b0a15/image-34.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/83d8750d5cf9aaca6bb1b983c6853b55/8ff5a/image-34.png 240w,\n/static/83d8750d5cf9aaca6bb1b983c6853b55/e85cb/image-34.png 480w,\n/static/83d8750d5cf9aaca6bb1b983c6853b55/0b533/image-34.png 500w\"\n            sizes=\"(max-width: 500px) 100vw, 500px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/83d8750d5cf9aaca6bb1b983c6853b55/0b533/image-34.png\"\n            alt=\"2021/12/image-34.png\"\n            title=\"2021/12/image-34.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>By the way, the Ghidra image base setting can be changed arbitrarily.</p>\n<p>For example, setting it to <code class=\"language-text\">0x555555555000</code> allows you to align the displayed addresses with those shown when using tools like gdb.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 500px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/cf30037c5f57e03e2822dfb9977a3d08/0b533/image-36.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 47.5%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAARlAAAEZQAGA43XUAAABoElEQVQoz41TaW+bQBTk//+2qm1qR7HByxHHCMyxh4G9mT7jplWlNsqTht0PO/OuIUmzDE9PO9R1h77v0Q09hJSQQmAcR4ycgzGG/X6HY1ogz3KUZY22GdE0HEoo3OYbhFL0niPR1uFUXLBYQJsVUltoF+EjEFZsZ0/CalqxGIPJzXAxYg0WMTqsq4end++R8DmglgThkL0qnCqFtJAomxkXbnEeLV5SBik1Fu3BpaWqNLSaCAucsTAhINyTrCsSqSPeuCMYsFogbyZU/UJiBudhQUX4vn+B954IER/FJiiWsIm8tgZVrenUqClBqyJVbnG8Dviy+/Eg/CL9D1vL92F+/faM5howUFVqErDBbLPzNERDl/TEfhP+LbQ+st0F53nC8XBC3wXMs8Oy3NBdDaz90wZjxV+CH0ViaHN1fQXLPbQOMIZavhhw7hFo2M4FnN/OtNH4YbvvSCJ9hp6TEIhEVolhI8e4kuADbdvis5GwokKWV+TFCs+HDIesQMpK5EWJoryjouoLdN3D+MMwbIbnZHhO5hcEST+Col0IPuAnmRQCBVKSxxMAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/cf30037c5f57e03e2822dfb9977a3d08/8ac56/image-36.webp 240w,\n/static/cf30037c5f57e03e2822dfb9977a3d08/d3be9/image-36.webp 480w,\n/static/cf30037c5f57e03e2822dfb9977a3d08/b0a15/image-36.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/cf30037c5f57e03e2822dfb9977a3d08/8ff5a/image-36.png 240w,\n/static/cf30037c5f57e03e2822dfb9977a3d08/e85cb/image-36.png 480w,\n/static/cf30037c5f57e03e2822dfb9977a3d08/0b533/image-36.png 500w\"\n            sizes=\"(max-width: 500px) 100vw, 500px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/cf30037c5f57e03e2822dfb9977a3d08/0b533/image-36.png\"\n            alt=\"2021/12/image-36.png\"\n            title=\"2021/12/image-36.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>The image above shows the disassembly result of the entry point when the image base is set to <code class=\"language-text\">0x555555555000</code>.</p>\n<h3 id=\"about-rva--va--offset\" style=\"position:relative;\"><a href=\"#about-rva--va--offset\" aria-label=\"about rva  va  offset 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>About RVA / VA / Offset</h3>\n<p>We have been loosely using terms like RVA, address (virtual address), and offset, so let’s take a moment to organize them.</p>\n<p>First, the file offset simply represents the position as a number of bytes from the beginning of the binary.</p>\n<p>The file offset of data located at byte 0x100 when opened in a hex editor is likewise 0x100.</p>\n<p>Next, let’s look at the virtual address (VA).</p>\n<p>This article does not go into the details of virtual addresses, but in simple terms, the virtual address is obtained by adding the starting position of each section to the file offset.</p>\n<p>When a program is executed on an OS, it is naturally loaded into memory. However, if it were loaded at the actual memory address (physical address), various issues would arise in systems that need to run multiple applications concurrently, such as memory address conflicts.</p>\n<p>To avoid these problems, when applications running on operating systems such as Linux reference memory addresses, they reference a virtual address (VA) rather than a physical address.</p>\n<p>This virtual address is the offset added to the beginning of each section.</p>\n<p>For example, if the file offset of data in the .data section, whose section boundary is set at 0x1000, is 0x3000, the virtual address would be 0x4000.</p>\n<p>And finally, the RVA, as mentioned earlier, is the virtual address with the image base address further added.</p>\n<p>Reference: <a href=\"https://tech-zealots.com/malware-analysis/understanding-concepts-of-va-rva-and-offset/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Understanding Concepts Of VA, RVA and Offset | Tech Zealots</a></p>\n<p>The differences and uses of these addresses and offsets may be a bit confusing, but since they are rarely used for solving entry-level CTF problems, feel free to skip this section for now if it is too difficult.</p>\n<p>As you work with binaries more, you will develop a better intuition for these concepts.</p>\n<h3 id=\"identifying-the-main-function-from-the-entry-point\" style=\"position:relative;\"><a href=\"#identifying-the-main-function-from-the-entry-point\" aria-label=\"identifying the main function from the entry point 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>Identifying the main Function from the Entry Point</h3>\n<p>Let’s return to the analysis.</p>\n<p>Looking at the decompiled result of the entry point, we can see that <code class=\"language-text\">__libc_start_main</code> exists.</p>\n<p>This function is the initialization routine that is always called first when an ELF binary is executed.</p>\n<p>Furthermore, it is established that the first argument of <code class=\"language-text\">__libc_start_main</code> receives the address of the main function.</p>\n<p>In other words, by examining the first argument of <code class=\"language-text\">__libc_start_main</code>, we can identify the address of the main function even in a binary without symbol information, like the challenge binary in this case.</p>\n<p>Reference: <a href=\"https://refspecs.linuxbase.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/baselib---libc-start-main-.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">_<em>libc</em>start_main</a></p>\n<p>Reference: <a href=\"https://stackoverflow.com/questions/9885545/how-to-find-the-main-functions-entry-point-of-elf-executable-file-without-any-s\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">linux - How to find the main function’s entry point of elf executable file without any symbolic information? - Stack Overflow</a></p>\n<p>So, we have identified that <code class=\"language-text\">FUN_00101208</code> is the main function.</p>\n<p>Since this name is hard to read, let’s right-click <code class=\"language-text\">FUN_00101208</code> and use [Rename Function] to rename it to <code class=\"language-text\">main</code>.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 500px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/ab02e5c74c2e877478451166cc7a396c/0b533/image-37.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 115.41666666666669%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAXCAYAAAALHW+jAAAACXBIWXMAARlAAAEZQAGA43XUAAADcElEQVQ4y5VV246bVhT1d6fvjRSllRpFrfKS16QT9WGkPLWqmlZRlUx9w8bG4xv3O+ZyMBg4wOqGNFE7M/JMj7R1MJh11l577c3ghxfv8eirX/Hdsw94/vwjXr4c4tXrMS4uJri8nOHnX5Z49/s1/nh/jQ9XGwhzGbudBVGWICkbKJqN4XSO4WiKyVTA4HqtwHYSaLqAlTqDpGnY2kdYyRFmfESQ5khYhjwvURQVirJCVXEUVYmSorvOTyd6VkLaqhjougFe1yizCOLlTxi/foXJj28weXOB3du3sGYzBDED5zXuW1vdx8AwdIRpA9d0sHrxPa6+fgzh228wevIEi6dPof/2Dn6SoiRm3Wrb9s7oATUXA9MyESUchpdiqam4ktYYr3f4c7bAeCvDiRgsy6X0+BfAf6//AOoeBrIsI2I1FDOGKIoQ50sostaHrpkwLQ+q8VBAYigIApJjg8XuiMlMwnQ2h0pAfhDB80NYTgDN/B8MO8CYNJTkHOOpCEEQsSd2rhdQ+HBcHzrp+2DA6XSKuGeYYb4ysFgK2O03iOIUIenXRXfN6+YWwM3fa/WflEPWYLJKsdEYsmwHlip32uImu5uAm67Kw9GI0mkRJgmZ16NgqHmDpuFom4qiRlVzYsjvZdinPByNCaCC43jwyDq6m0DxTRxSC06yg5eo8FkAdmIPACSGfw3H1AUZga1g+xqORYam5ajbBryLhrRrH5Zyz1BcSNSPCaJoCT9SkFfF53/eeunzi2cZCjORAKmaoQSPAE/8dLZf77XNx6sR6pqsES3gHWwyeUEHNNS7JWl7Qkly5LTnnFOhblvnVpUF6oyy5GRgtQfUTQbTzWAdTMSJg7yIERcpkqIgrflZQEW3qZcVFVVZIwhChEcPZf0p5Rbt2ZTvAjQsAlwsJeRZBcYYpURzkWpS0gGcvMh5SfdooNYlVbu+d3zpJgF2w6Ale9iuQammdEoO3UpgeTHpqlDX6NTrFkVAwFVvobOAe1np2RxCMm+WIDvxnmWW11Ssqi9Ed2DHvkVz1gGG5XwC7FaShD2oZaZYb1O4bkhShFTpjDRm/TeDHen7kZ8QsxKnPCIijOQ50jPWS6NoMgazxQqGn2JPdK9Vk7ykQlJ1qI4PzT1AtoJ+V+wIeyum+xF2ZkR70IdNI84LfLi+j4Uk4W/Mb9XGocpyAQAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/ab02e5c74c2e877478451166cc7a396c/8ac56/image-37.webp 240w,\n/static/ab02e5c74c2e877478451166cc7a396c/d3be9/image-37.webp 480w,\n/static/ab02e5c74c2e877478451166cc7a396c/b0a15/image-37.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/ab02e5c74c2e877478451166cc7a396c/8ff5a/image-37.png 240w,\n/static/ab02e5c74c2e877478451166cc7a396c/e85cb/image-37.png 480w,\n/static/ab02e5c74c2e877478451166cc7a396c/0b533/image-37.png 500w\"\n            sizes=\"(max-width: 500px) 100vw, 500px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/ab02e5c74c2e877478451166cc7a396c/0b533/image-37.png\"\n            alt=\"2021/12/image-37.png\"\n            title=\"2021/12/image-37.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>When analyzing with Ghidra, you can rename function names and variable names at will, so renaming them to something meaningful each time will help you analyze more efficiently.</p>\n<h3 id=\"examining-the-decompiled-main-function\" style=\"position:relative;\"><a href=\"#examining-the-decompiled-main-function\" aria-label=\"examining the decompiled main function 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>Examining the Decompiled main Function</h3>\n<p>Let’s first look at the decompiled result of the main function. (The local variable definitions are cut for brevity.)</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token keyword\">int</span> <span class=\"token function\">main</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">void</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">{</span>\n  <span class=\"token comment\">/* omitted */</span> \n  local_20 <span class=\"token operator\">=</span> <span class=\"token operator\">*</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">long</span> <span class=\"token operator\">*</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span>in_FS_OFFSET <span class=\"token operator\">+</span> <span class=\"token number\">0x28</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    \n  <span class=\"token comment\">/* Receive standard input (stdin) from the user and store it in local_68 */</span> \n  <span class=\"token function\">__printf_chk</span><span class=\"token punctuation\">(</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span><span class=\"token string\">\"What\\'s the flag? \"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token comment\">/* omitted */</span> \n  pcVar3 <span class=\"token operator\">=</span> <span class=\"token function\">fgets</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">char</span> <span class=\"token operator\">*</span><span class=\"token punctuation\">)</span><span class=\"token operator\">&amp;</span>local_68<span class=\"token punctuation\">,</span><span class=\"token number\">0x40</span><span class=\"token punctuation\">,</span><span class=\"token constant\">stdin</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>pcVar3 <span class=\"token operator\">==</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">char</span> <span class=\"token operator\">*</span><span class=\"token punctuation\">)</span><span class=\"token number\">0x0</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>    \n    <span class=\"token function\">puts</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"no!!\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    iVar2 <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n    \n  <span class=\"token comment\">/* Find the newline character and replace it with a null character */</span> \n  <span class=\"token keyword\">else</span> <span class=\"token punctuation\">{</span>\n    sVar4 <span class=\"token operator\">=</span> <span class=\"token function\">strcspn</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">char</span> <span class=\"token operator\">*</span><span class=\"token punctuation\">)</span><span class=\"token operator\">&amp;</span>local_68<span class=\"token punctuation\">,</span><span class=\"token string\">\"\\n\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> \n    <span class=\"token operator\">*</span><span class=\"token punctuation\">(</span>undefined <span class=\"token operator\">*</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">long</span><span class=\"token punctuation\">)</span><span class=\"token operator\">&amp;</span>local_68 <span class=\"token operator\">+</span> sVar4<span class=\"token punctuation\">)</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span>\n    lVar5 <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span>\n  \n  <span class=\"token comment\">/* Mysterious loop processing */</span> \n    <span class=\"token keyword\">do</span> <span class=\"token punctuation\">{</span>\n      cVar1 <span class=\"token operator\">=</span> <span class=\"token function\">FUN_001011e9</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n      <span class=\"token operator\">*</span><span class=\"token punctuation\">(</span>byte <span class=\"token operator\">*</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">long</span><span class=\"token punctuation\">)</span><span class=\"token operator\">&amp;</span>local_68 <span class=\"token operator\">+</span> lVar5<span class=\"token punctuation\">)</span> <span class=\"token operator\">=</span> <span class=\"token operator\">*</span><span class=\"token punctuation\">(</span>byte <span class=\"token operator\">*</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">long</span><span class=\"token punctuation\">)</span><span class=\"token operator\">&amp;</span>local_68 <span class=\"token operator\">+</span> lVar5<span class=\"token punctuation\">)</span> <span class=\"token operator\">^</span> cVar1 <span class=\"token operator\">+</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">char</span><span class=\"token punctuation\">)</span>lVar5<span class=\"token punctuation\">;</span>\n      lVar5 <span class=\"token operator\">=</span> lVar5 <span class=\"token operator\">+</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span> <span class=\"token keyword\">while</span> <span class=\"token punctuation\">(</span>lVar5 <span class=\"token operator\">!=</span> <span class=\"token number\">0x40</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  \n  <span class=\"token comment\">/* Compare local_68 value with PTR_DAT_00104010 for 0x40 bytes */</span>\n    iVar2 <span class=\"token operator\">=</span> <span class=\"token function\">memcmp</span><span class=\"token punctuation\">(</span><span class=\"token operator\">&amp;</span>local_68<span class=\"token punctuation\">,</span>PTR_DAT_00104010<span class=\"token punctuation\">,</span><span class=\"token number\">0x40</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>iVar2 <span class=\"token operator\">==</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token function\">puts</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"You got it!\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n    <span class=\"token keyword\">else</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token function\">puts</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"That\\'s not it...\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n      iVar2 <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">}</span>\n  <span class=\"token comment\">/* omitted */</span> \n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>From the above, we can see that this main function is broadly divided into the following four processes:</p>\n<ul>\n<li>Receive standard input (stdin) from the user and store it in <code class=\"language-text\">local_68</code></li>\n<li>Find the newline character and replace it with a null character (when a byte sequence is evaluated as a string, 0 is treated as equivalent to <code class=\"language-text\">\\0</code>)\nReference: <a href=\"https://stackoverflow.com/questions/1296843/what-is-the-difference-between-null-0-and-0\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">c - What is the difference between NULL, ‘\\0’ and 0? - Stack Overflow</a></li>\n<li>Mysterious loop processing</li>\n<li>Compare the value of <code class=\"language-text\">local_68</code> with <code class=\"language-text\">PTR_DAT_00104010</code> for 0x40 bytes</li>\n</ul>\n<p>First, let’s rename the <code class=\"language-text\">local_68</code> variable to something like <code class=\"language-text\">input_text</code>, then proceed to analyze each step in order.</p>\n<h3 id=\"reading-standard-input\" style=\"position:relative;\"><a href=\"#reading-standard-input\" aria-label=\"reading standard input 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 Standard Input</h3>\n<p>The first part we’ll look at is the following code.</p>\n<p>The <code class=\"language-text\">fgets</code> function reads up to 0x40 characters of input from standard input and stores it in the variable <code class=\"language-text\">input_text</code>.</p>\n<p>If the read fails, it outputs the string <code class=\"language-text\">no!!</code> and exits.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\">pcVar3 <span class=\"token operator\">=</span> <span class=\"token function\">fgets</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">char</span> <span class=\"token operator\">*</span><span class=\"token punctuation\">)</span><span class=\"token operator\">&amp;</span>input_text<span class=\"token punctuation\">,</span><span class=\"token number\">0x40</span><span class=\"token punctuation\">,</span><span class=\"token constant\">stdin</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>pcVar3 <span class=\"token operator\">==</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">char</span> <span class=\"token operator\">*</span><span class=\"token punctuation\">)</span><span class=\"token number\">0x0</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>    \n<span class=\"token function\">puts</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"no!!\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\niVar2 <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>The <code class=\"language-text\">fgets</code> function is a function that can read a specified number of bytes from a stream (FILE object).</p>\n<p>Reference: <a href=\"https://www.tutorialspoint.com/c_standard_library/c_function_fgets.htm\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">C library function - fgets()</a></p>\n<p>The reason this function can receive user input is that on Linux and UNIX-based systems, most devices are abstracted and treated as files.</p>\n<p>On Linux systems, as described in the following manual, the FILE object <code class=\"language-text\">stdin</code> is defined as the input stream for receiving standard input.</p>\n<p>Reference: <a href=\"https://man7.org/linux/man-pages/man3/stdout.3.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">stdin(3) - Linux manual page</a></p>\n<p>That is why <code class=\"language-text\">fgets</code> can receive input values.</p>\n<p>For those who want to learn more, the following book is easy to understand and a good reference.</p>\n<p>Reference: <a href=\"https://amzn.to/3oMmsPY\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">動かしながらゼロから学ぶ Linuxカーネルの教科書</a></p>\n<h3 id=\"removing-the-newline-character\" style=\"position:relative;\"><a href=\"#removing-the-newline-character\" aria-label=\"removing the newline character 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>Removing the Newline Character</h3>\n<p>The next part to focus on is this:</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\">sVar4 <span class=\"token operator\">=</span> <span class=\"token function\">strcspn</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">char</span> <span class=\"token operator\">*</span><span class=\"token punctuation\">)</span><span class=\"token operator\">&amp;</span>input_text<span class=\"token punctuation\">,</span><span class=\"token string\">\"\\n\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> \n<span class=\"token operator\">*</span><span class=\"token punctuation\">(</span>undefined <span class=\"token operator\">*</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">long</span><span class=\"token punctuation\">)</span><span class=\"token operator\">&amp;</span>input_text <span class=\"token operator\">+</span> sVar4<span class=\"token punctuation\">)</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>The <code class=\"language-text\">strcspn</code> function returns the length of the initial segment of the first argument string that consists only of characters not in the second argument (reject) string.</p>\n<p>In other words, using <code class=\"language-text\">strcspn</code>, you can find the position where a given character first appears.</p>\n<p>Here, we are finding the position of the newline character <code class=\"language-text\">\\n</code> in the string received from standard input, and changing the byte at that position to 0.</p>\n<p>The reason for doing this is that the string received from standard input contains a newline character.</p>\n<p>If you actually look at the memory contents, you can see that the newline character <code class=\"language-text\">0x0a</code> follows the input characters, as shown below.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 500px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/672f64caed316a94d051034a865f6586/0b533/image-38.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 68.33333333333333%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAARlAAAEZQAGA43XUAAACWklEQVQ4y41TaVOjQBDNl1010dJcJMaVAAMknAOEEEhiNgc5XPcoa/f//5W3zaBWdN0qP7wamOl5069fdyWUGJbKBAt5BN5SEbRV8LaCoEvfUh9BR0PYM2hVkTRUHE5l5DW5jO0qIr44i7sD+DUdlcNwiXv7Kx7cFZb9BN6phqRpIagZyKQBYUj/BsJzDc5nBvvUgFs1welcoGrQHQbvTIdfZaj85DkeR3v8Tu6xM6ZwTxRktzbCho5UsTA3XMxNB6lsIaSsJjI91LcwkVykHQ/jpk1kGpFpYq388Df4FW6JdIcNS8XmisUYtywcvCm29gRTmUhlLnDHOPiVKpQUsSUZE/CKDAvCx2iHP+MDtnpGGarYaBnCCxP7wQI5y2B/UkiuUpKcaELiM8kxBOF3b4WVOUPuLDFTqIaXA6qZjcWXCCs1wbwXiO9ij9fKOomsnrM7gpBcGBG0DfjkdnBduGkibjjYmnNs9ClyyvIh2GNxW5TBRUgPelVdOPoaRmnKQknhNkPw7hh+h8ivbKy1CdbqhIgtZN0AKaHI7rhe/0NlyWZw2zH4dQJ+M0VQd3AY3mF2zeGQ46UUtbxwxo4kPktnryWvBCE1dTeB/0S4H8xF7YoWenn9Tb1ekx8RLtQMTpEhSea9lFrCoX6ck1QfNjnrPxnxUVSyXoS4xkSbeETG6zRqLYas42NUt+AW0t414R1TitHL+2PcNYcYN2g6pAgRdX/UcRFLARLJw+iSIbo0X6bhX+lvJK8ljsVNTBMww85a4puX495dI6rb8M9phi90BBfGhxwuYv4CuXyShJ/DfxwAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/672f64caed316a94d051034a865f6586/8ac56/image-38.webp 240w,\n/static/672f64caed316a94d051034a865f6586/d3be9/image-38.webp 480w,\n/static/672f64caed316a94d051034a865f6586/b0a15/image-38.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/672f64caed316a94d051034a865f6586/8ff5a/image-38.png 240w,\n/static/672f64caed316a94d051034a865f6586/e85cb/image-38.png 480w,\n/static/672f64caed316a94d051034a865f6586/0b533/image-38.png 500w\"\n            sizes=\"(max-width: 500px) 100vw, 500px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/672f64caed316a94d051034a865f6586/0b533/image-38.png\"\n            alt=\"2021/12/image-38.png\"\n            title=\"2021/12/image-38.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p><code class=\"language-text\">0x0a</code> represents a control character defined in ASCII called LF (Line Feed).</p>\n<p>Reference: <a href=\"https://www.tohoho-web.com/wwwxx011.htm\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">改行コードについて - とほほのWWW入門</a></p>\n<p>Next, if we advance execution to the line where the substitution is performed, we can see that the newline character has been erased from memory.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 500px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/dc631afeaeb42eb8be848e9f570f9cff/0b533/image-39.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 57.50000000000001%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAARlAAAEZQAGA43XUAAAB70lEQVQoz42T2XLaQBBF9RQbTAgYidVgbQghpGEktLODcIGdSuIk//8rN42UpJxKiP1walTqmTt9u3sErzXEahBgq0Zw2waiqozsRoUranBbtEoGZn2bvnXM6gr21/c4VFQEdG7WJdpDcEnJ47xqQDiZWzxO1vg43eOgpZhcUbBpUVCHL40R9WwkA4aoQ/9qOphEIj0LoWgjbNgIbidgJQ3ONVHWIDzZW3x2M3wNjjgMUwooCM631jSEA4MggcYIvmgioAvC5hjT92q+zykVsLJaUFJJcLLDJ77Hs3/EgzGHfSVjpXhkb4TdKEA2DmkzHSyfRQrOGbHyC0qFmJML2jt8cff4Hp5wMpew391jIXO4dR1rnWNjuphKMvit8rfQPxDWA2qGxOF3fHjtGVjVxPLOx0aOkHY41kqKoOnl9WE3+mUqw3wVHswNLJGEeinY3QKsbuNIme61JK+TR53jFT23w96AsFES2CLZ6kREDFazSCzGoufBIvvOz9q8ZvW35Wy4xET0MO3GRAL2wUJG2a36s1zwV9GdF4W/lF3elAXVyyGRaY3mqcFJ0MS85yJusf/X7ALCRl0grY0QSwy8FSDs0GvpG5h3PcRNp2jGm9ALywk1IW5yJNoKTyzDNxrwZ+8Avz6mVyP/MWevcbb8A3W3V/ixMCVVAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/dc631afeaeb42eb8be848e9f570f9cff/8ac56/image-39.webp 240w,\n/static/dc631afeaeb42eb8be848e9f570f9cff/d3be9/image-39.webp 480w,\n/static/dc631afeaeb42eb8be848e9f570f9cff/b0a15/image-39.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/dc631afeaeb42eb8be848e9f570f9cff/8ff5a/image-39.png 240w,\n/static/dc631afeaeb42eb8be848e9f570f9cff/e85cb/image-39.png 480w,\n/static/dc631afeaeb42eb8be848e9f570f9cff/0b533/image-39.png 500w\"\n            sizes=\"(max-width: 500px) 100vw, 500px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/dc631afeaeb42eb8be848e9f570f9cff/0b533/image-39.png\"\n            alt=\"2021/12/image-39.png\"\n            title=\"2021/12/image-39.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>How to use GDB is described later.</p>\n<h3 id=\"xor-encrypting-the-string-in-a-loop\" style=\"position:relative;\"><a href=\"#xor-encrypting-the-string-in-a-loop\" aria-label=\"xor encrypting the string in a loop 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>XOR-Encrypting the String in a Loop</h3>\n<p>Looking at the next process, we can see that it XOR-encrypts the string received from standard input using a value obtained by adding the loop counter <code class=\"language-text\">lVar5</code> to the return value of the mysterious function <code class=\"language-text\">FUN_001011e9</code>.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token keyword\">do</span> <span class=\"token punctuation\">{</span>\n    cVar1 <span class=\"token operator\">=</span> <span class=\"token function\">FUN_001011e9</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token operator\">*</span><span class=\"token punctuation\">(</span>byte <span class=\"token operator\">*</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">long</span><span class=\"token punctuation\">)</span><span class=\"token operator\">&amp;</span>input_text <span class=\"token operator\">+</span> lVar5<span class=\"token punctuation\">)</span> <span class=\"token operator\">=</span> <span class=\"token operator\">*</span><span class=\"token punctuation\">(</span>byte <span class=\"token operator\">*</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">long</span><span class=\"token punctuation\">)</span><span class=\"token operator\">&amp;</span>input_text <span class=\"token operator\">+</span> lVar5<span class=\"token punctuation\">)</span> <span class=\"token operator\">^</span> cVar1 <span class=\"token operator\">+</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">char</span><span class=\"token punctuation\">)</span>lVar5<span class=\"token punctuation\">;</span>\n    lVar5 <span class=\"token operator\">=</span> lVar5 <span class=\"token operator\">+</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span> <span class=\"token keyword\">while</span> <span class=\"token punctuation\">(</span>lVar5 <span class=\"token operator\">!=</span> <span class=\"token number\">0x40</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>Details about XOR cipher are omitted here.</p>\n<p>Reference: <a href=\"https://gist.github.com/matsubara0507/fada4760f42cd6a52c95\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">たのしいXOR暗号入門</a></p>\n<h3 id=\"checking-the-encrypted-byte-sequence\" style=\"position:relative;\"><a href=\"#checking-the-encrypted-byte-sequence\" aria-label=\"checking the encrypted byte sequence 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>Checking the Encrypted Byte Sequence</h3>\n<p>Here, the XOR-encrypted <code class=\"language-text\">input_text</code> is compared byte-by-byte against the byte sequence defined in <code class=\"language-text\">PTR_DAT_00104010</code> for 0x40 bytes to check whether they match.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\">iVar2 <span class=\"token operator\">=</span> <span class=\"token function\">memcmp</span><span class=\"token punctuation\">(</span><span class=\"token operator\">&amp;</span>input_text<span class=\"token punctuation\">,</span>PTR_DAT_00104010<span class=\"token punctuation\">,</span><span class=\"token number\">0x40</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>iVar2 <span class=\"token operator\">==</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n<span class=\"token function\">puts</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"You got it!\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n<span class=\"token keyword\">else</span> <span class=\"token punctuation\">{</span>\n<span class=\"token function\">puts</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"That\\'s not it...\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    iVar2 <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>It is presumed that if the string given as initial input is the correct flag, the XOR-encrypted result will match the byte values defined in <code class=\"language-text\">PTR_DAT_00104010</code>.</p>\n<h3 id=\"retrieving-values-from-the-data-section\" style=\"position:relative;\"><a href=\"#retrieving-values-from-the-data-section\" aria-label=\"retrieving values from the data section 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>Retrieving Values from the Data Section</h3>\n<p>Next, let’s examine the value defined in <code class=\"language-text\">PTR_DAT_00104010</code>.</p>\n<p>In an ELF binary, predefined data such as strings is stored in the <code class=\"language-text\">.data</code> section.</p>\n<p>Reference: <a href=\"https://en.wikipedia.org/wiki/Data_segment\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Data segment - Wikipedia</a></p>\n<p>The <code class=\"language-text\">.data</code> section is a read-write area, so writable variables and similar data are stored there.</p>\n<p>It is possible to jump to the section where this data is defined by clicking <code class=\"language-text\">PTR_DAT_00104010</code> in Ghidra’s decompilation result, but let’s first identify the offset of the <code class=\"language-text\">.data</code> section.</p>\n<p>First, let’s perform surface-level analysis using <code class=\"language-text\">readelf -S</code>.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">$ readelf -S revvy_chevy \nThere are <span class=\"token number\">29</span> section headers, starting at offset 0x3150:\n\nSection Headers:\n  <span class=\"token punctuation\">[</span>Nr<span class=\"token punctuation\">]</span> Name              Type             Address           Offset       Size              EntSize          Flags  Link  Info  Align\n  <span class=\"token punctuation\">[</span><span class=\"token number\">25</span><span class=\"token punctuation\">]</span> .data             PROGBITS         0000000000004000  00003000       0000000000000018  0000000000000000  WA       <span class=\"token number\">0</span>     <span class=\"token number\">0</span>     <span class=\"token number\">8</span></code></pre></div>\n<p>From this output, we can see that the <code class=\"language-text\">.data</code> section occupies 0x18 bytes starting from virtual address <code class=\"language-text\">0x4000</code>.</p>\n<p>Next, let’s use the <code class=\"language-text\">iS</code> command in radare2 analysis to retrieve the section table.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token punctuation\">[</span>0x00001100<span class=\"token punctuation\">]</span><span class=\"token operator\">></span> iS\n<span class=\"token punctuation\">[</span>Sections<span class=\"token punctuation\">]</span>\n\nnth paddr        size vaddr       vsize perm name\n\n<span class=\"token number\">0</span>   0x00000000    0x0 0x00000000    0x0 ---- \n<span class=\"token number\">1</span>   0x00000318   0x1c 0x00000318   0x1c -r-- .interp\n<span class=\"token number\">2</span>   0x00000338   0x20 0x00000338   0x20 -r-- .note.gnu.property\n<span class=\"token number\">3</span>   0x00000358   0x24 0x00000358   0x24 -r-- .note.gnu.build_id\n<span class=\"token number\">4</span>   0x0000037c   0x20 0x0000037c   0x20 -r-- .note.ABI_tag\n<span class=\"token number\">5</span>   0x000003a0   0x28 0x000003a0   0x28 -r-- .gnu.hash\n<span class=\"token number\">6</span>   0x000003c8  0x138 0x000003c8  0x138 -r-- .dynsym\n<span class=\"token number\">7</span>   0x00000500   0xd1 0x00000500   0xd1 -r-- .dynstr\n<span class=\"token number\">8</span>   0x000005d2   0x1a 0x000005d2   0x1a -r-- .gnu.version\n<span class=\"token number\">9</span>   0x000005f0   0x40 0x000005f0   0x40 -r-- .gnu.version_r\n<span class=\"token number\">10</span>  0x00000630   0xf0 0x00000630   0xf0 -r-- .rela.dyn\n<span class=\"token number\">11</span>  0x00000720   0x90 0x00000720   0x90 -r-- .rela.plt\n<span class=\"token number\">12</span>  0x00001000   0x1b 0x00001000   0x1b -r-x .init\n<span class=\"token number\">13</span>  0x00001020   0x70 0x00001020   0x70 -r-x .plt\n<span class=\"token number\">14</span>  0x00001090   0x10 0x00001090   0x10 -r-x .plt.got\n<span class=\"token number\">15</span>  0x000010a0   0x60 0x000010a0   0x60 -r-x .plt.sec\n<span class=\"token number\">16</span>  0x00001100  0x2b5 0x00001100  0x2b5 -r-x .text\n<span class=\"token number\">17</span>  0x000013b8    0xd 0x000013b8    0xd -r-x .fini\n<span class=\"token number\">18</span>  0x00002000   0x81 0x00002000   0x81 -r-- .rodata\n<span class=\"token number\">19</span>  0x00002084   0x4c 0x00002084   0x4c -r-- .eh_frame_hdr\n<span class=\"token number\">20</span>  0x000020d0  0x128 0x000020d0  0x128 -r-- .eh_frame\n<span class=\"token number\">21</span>  0x00002d90    0x8 0x00003d90    0x8 -rw- .init_array\n<span class=\"token number\">22</span>  0x00002d98    0x8 0x00003d98    0x8 -rw- .fini_array\n<span class=\"token number\">23</span>  0x00002da0  0x1f0 0x00003da0  0x1f0 -rw- .dynamic\n<span class=\"token number\">24</span>  0x00002f90   0x70 0x00003f90   0x70 -rw- .got\n<span class=\"token number\">25</span>  0x00003000   0x18 0x00004000   0x18 -rw- .data\n<span class=\"token number\">26</span>  0x00003018    0x0 0x00004020   0x10 -rw- .bss\n<span class=\"token number\">27</span>  0x00003018   0x2a 0x00000000   0x2a ---- .comment\n<span class=\"token number\">28</span>  0x00003042  0x10a 0x00000000  0x10a ---- .shstrtab</code></pre></div>\n<p>This result also shows that the <code class=\"language-text\">.data</code> section occupies 0x18 bytes starting from virtual address <code class=\"language-text\">0x4000</code>.</p>\n<p>So, let’s actually look at the disassembly result at RVA <code class=\"language-text\">0x104000</code> in Ghidra.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 500px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/962c13d85d306c44871ed4059475a46b/0b533/image-40.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 73.75%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAARlAAAEZQAGA43XUAAAB0klEQVQ4y61UaW/bMAzN//9nXVqgGBAUOdolzuX70GFJlOw3Wl4DDM2wDw3tB5qS9UQ+yl48v7zg6ccSy+UzVqsVNpst1ust3lYbvO8+sP91QHI44nQ8I80y5HmBsqzQNC3qukaaXuP44XhCkuyxwD0bgCZV6PIesjKQtUHwI/5n2lgsnHOwDPIEISSIBoZHVWkobUHBYxwnsjHe1lto0jxO6GwHYQVa00IaAaV6LLz38GHgQOH19SeKws27KULPGxW6jfFMOvthHGY/hPgc2I98KX2X0MaFzgXQ4FH27a2kT9J/2Y2QfMBU+uVygRQ+6iU6g04YNFLDWn6HQlzkeRMXHGfloZ3iKgwssWzecFJ6JrRECJzlpGWWB54YIJVlTR20Jn52MEw65edYO+Nd1FYYCWX6KI2mHmIm5Gb0hHNDSAqFdeKR5ITdWWF30TiwBKfKIZcBmQg4tT2SRrE3eC9r7EuDQo4o1IhLpWZCZQiFIFx5YHvwSJsQ41J6XGsXUevwBZXi03CLB2TNn5Idl/w9m5ulP5syEU4d/A7+6nIkxAMJrXtMhlKzhiGEeA4fYf30LRtjULcdrkWDtOq+ouS5srs/x8iqFlXdoOa/z/ma4jczAo61zxnO7QAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/962c13d85d306c44871ed4059475a46b/8ac56/image-40.webp 240w,\n/static/962c13d85d306c44871ed4059475a46b/d3be9/image-40.webp 480w,\n/static/962c13d85d306c44871ed4059475a46b/b0a15/image-40.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/962c13d85d306c44871ed4059475a46b/8ff5a/image-40.png 240w,\n/static/962c13d85d306c44871ed4059475a46b/e85cb/image-40.png 480w,\n/static/962c13d85d306c44871ed4059475a46b/0b533/image-40.png 500w\"\n            sizes=\"(max-width: 500px) 100vw, 500px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/962c13d85d306c44871ed4059475a46b/0b533/image-40.png\"\n            alt=\"2021/12/image-40.png\"\n            title=\"2021/12/image-40.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>Data is stored within the range of 0x18 bytes.</p>\n<p>Our target is the value at <code class=\"language-text\">PTR_DAT_00104010</code>, which appears to be stored as a pointer in the .data section.</p>\n<p>Therefore, let’s jump further to <code class=\"language-text\">DAT_00102040</code>, which this pointer points to.</p>\n<p>The byte sequence is stored there.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 500px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/9ad5b785a98ef92dd33995f354f871a0/0b533/image-41.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 85.83333333333334%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAARCAYAAADdRIy+AAAACXBIWXMAARlAAAEZQAGA43XUAAABzklEQVQ4y5WU227iMBCGef9n6QO0V7viplK1q24FSC0FtiTk4DhH0tiOc/g7MZQSiiAdaeRxrHz652CPxuM5fv1eYTxe4ObmHrd3czw8rI3//bfGn6f/eH1lWK0YLJvD82JwniBNM6RJF3MEYQjGfDy/zDFq6hKdpaqGHcSIigYZfdqwAgEFoaywLbvzFpkCvLwAe1dwtwWsJIObCdgEj0WLteNjlOfvKHWNNBd4fJrB8Qoo3SIhWCYUvG1m9lI3tDZmNXH1FUv6v6xaWI6HkRACdd2gLBVm0xmlIIziUlVQpD4SKYbaxt0DKwJKpTAlYBDsgFJUkBWlXGRm37btRe8BawOUBsi5NIeKaicqhUTmB+A5Owa+bfw+cHYElHKnMBJ9hWi/A7EHLm32XWEYfgEVAY8VnlN5AXhSwz3wtIaXFfpDgOlV4Kfy1dWUa20U7jhDUvYvA7umxGI7WKG1ORmbyWRKd1IcAVUPeG1seoOtqIYGGIj+2PxgsG3nAKwJsFPIebG/KfoAvAbrATtl3V5rbQY7CpU51GUD3Vb0yuSD77LjUVPiODZvm+UyPE6esXgL4NN7Z7sRbMaxpLp4QQKPn3fGI2pkBE4+XyzxAXQZI0VnO6DXAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/9ad5b785a98ef92dd33995f354f871a0/8ac56/image-41.webp 240w,\n/static/9ad5b785a98ef92dd33995f354f871a0/d3be9/image-41.webp 480w,\n/static/9ad5b785a98ef92dd33995f354f871a0/b0a15/image-41.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/9ad5b785a98ef92dd33995f354f871a0/8ff5a/image-41.png 240w,\n/static/9ad5b785a98ef92dd33995f354f871a0/e85cb/image-41.png 480w,\n/static/9ad5b785a98ef92dd33995f354f871a0/0b533/image-41.png 500w\"\n            sizes=\"(max-width: 500px) 100vw, 500px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/9ad5b785a98ef92dd33995f354f871a0/0b533/image-41.png\"\n            alt=\"2021/12/image-41.png\"\n            title=\"2021/12/image-41.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>Ultimately, the line <code class=\"language-text\">iVar2 = memcmp(&amp;input_text,PTR_DAT_00104010,0x40);</code> references 0x40 bytes of data starting from the address <code class=\"language-text\">0x104010</code>.</p>\n<p>Since this is hard to read as-is, let’s use Ghidra’s features to format and retrieve this data.</p>\n<p>This time, since we want to use it in a Python script later, we decided to retrieve it in Python array format.</p>\n<p>First, select the range of 0x40 bytes starting from <code class=\"language-text\">0x104000</code> and right-click.</p>\n<p>Then press [Copy Special] and select [Python List].</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 500px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/3f51f7a9ce08c748b38830f87a23237c/0b533/image-42.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 78.75000000000001%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAYAAAAWGF8bAAAACXBIWXMAARlAAAEZQAGA43XUAAACvElEQVQ4y3WT3VPaUBDF/f+nD31ApdM+tQrYqkASRFFxHAeFkAQIoK0KIiqEfEFAKAbJ6SYgiFbubDKX3PnNObvnLsW4CwQYAZ+5KD6FNvElsY/vqQOsnSbx45Te6X2E5WOw5yfYu04heZvGSYNHRhfB6zxSjROklDQObpPI1SQsgX4KGgg+rcNX9iFgMWDBYMuOYJvW2jBEu+hsRaYrPK2I95Whk2FkewQcDx3UR/cI9kJYlv0ImRwBOWwPWIRtBsHHTbBjDpwTmxXrcAvl/hchrNjNYcmxHdw9PyDwGMRK0Y+NzhT4l1Q6YVK44R0Oz7S5mth3UPe78BoYJKCrMGjOgXvNQyTujsiKCLGfQ8YSke4JiD3H51B8BOy7wFUEjMmhrQGD/O8iZKGEUuEcatOAqVpoqTp27YTXt9fAiAeUFoG+wir1kJ0BpasCLi+vULmpQlFb0HQDTb2F3dF7oKtQfKvQRwpDbXY6FAbC+jeUl/142NmBaRjQNBNNrfWBwii1g3/bQxc4tyz+kVGt1EmZSQp1KC0d9WYT8acE4RaB7v64d/Z+KK+BwoWEbEZALp/HdaUC3QXr6qJCZw486mUo2DYIeE+xCWClRLGxOG+CW4Mo+HIW2TQPURRRq9XIdhstQ531cHIFJhX1gGkK9tMY988NbPR+wl/8is1OHDHEEenHwJeykAQJpXIJiqLAIIUNGk7CPvTCHBvTWSfuvd39kcW/B/5qx7EzBYoXeQhZCQW5SH3UoakGGpr6IfC4y7uWnQXLoc6LZQa5ywLkvEwKy6hUq55lhWITt/cmlp2pZYf17nmym3lz9VxgOzaJzZBFuiSgKJ/junpDE1apDNSV5n+H4sXGomCP7BH69gDGyIQxNNAZWzBHbah9AzmymkqdQZTyUCk2KllWWhoMuw2LVseh50vR0roG/gHXUlMEvl5jCQAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/3f51f7a9ce08c748b38830f87a23237c/8ac56/image-42.webp 240w,\n/static/3f51f7a9ce08c748b38830f87a23237c/d3be9/image-42.webp 480w,\n/static/3f51f7a9ce08c748b38830f87a23237c/b0a15/image-42.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/3f51f7a9ce08c748b38830f87a23237c/8ff5a/image-42.png 240w,\n/static/3f51f7a9ce08c748b38830f87a23237c/e85cb/image-42.png 480w,\n/static/3f51f7a9ce08c748b38830f87a23237c/0b533/image-42.png 500w\"\n            sizes=\"(max-width: 500px) 100vw, 500px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/3f51f7a9ce08c748b38830f87a23237c/0b533/image-42.png\"\n            alt=\"2021/12/image-42.png\"\n            title=\"2021/12/image-42.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>This gave us the binary data in a format usable as a Python array, as shown below.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token punctuation\">[</span> 0x74, 0x1a, 0x95, 0x4e, 0xba, 0xdb, 0x47, 0x64, 0x09, 0x2d, 0xd1, 0xbf, 0x8a, 0x9d, 0xde, 0x5a, 0xd7, 0x5c, 0x93, 0x16, 0x09, 0x3b, 0x30, 0x6f, 0x97, 0x40, 0xd0, 0x7c, 0x57, 0xdb, 0xde, 0x0c, 0x09, 0xa0, 0x84, 0x9b, 0x8a, 0x76, 0x2f, 0xb1, 0x57, 0xa2, 0xe1, 0x4f, 0xb9, 0x6f, 0x81, 0xbf, 0xb9, 0xbf, 0xe1, 0xef, 0x79, 0xcf, 0x01, 0xdf, 0xf9, 0x9f, 0xe1, 0x8f, 0x39, 0x2f, 0x81, 0xff, 0x00 <span class=\"token punctuation\">]</span></code></pre></div>\n<p>There are various other ways to convert to different data types and copy, so using them as appropriate will allow you to proceed with analysis more smoothly.</p>\n<h2 id=\"analyzing-the-xor-encryption-function\" style=\"position:relative;\"><a href=\"#analyzing-the-xor-encryption-function\" aria-label=\"analyzing the xor encryption function 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>Analyzing the XOR Encryption Function</h2>\n<p>Let’s continue with the static analysis a bit more.</p>\n<p>In the XOR encryption process we analyzed earlier, there was a line that calls the function <code class=\"language-text\">FUN_001011e9</code>.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token keyword\">do</span> <span class=\"token punctuation\">{</span>\n    cVar1 <span class=\"token operator\">=</span> <span class=\"token function\">FUN_001011e9</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token operator\">*</span><span class=\"token punctuation\">(</span>byte <span class=\"token operator\">*</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">long</span><span class=\"token punctuation\">)</span><span class=\"token operator\">&amp;</span>input_text <span class=\"token operator\">+</span> lVar5<span class=\"token punctuation\">)</span> <span class=\"token operator\">=</span> <span class=\"token operator\">*</span><span class=\"token punctuation\">(</span>byte <span class=\"token operator\">*</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">long</span><span class=\"token punctuation\">)</span><span class=\"token operator\">&amp;</span>input_text <span class=\"token operator\">+</span> lVar5<span class=\"token punctuation\">)</span> <span class=\"token operator\">^</span> cVar1 <span class=\"token operator\">+</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">char</span><span class=\"token punctuation\">)</span>lVar5<span class=\"token punctuation\">;</span>\n    lVar5 <span class=\"token operator\">=</span> lVar5 <span class=\"token operator\">+</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span> <span class=\"token keyword\">while</span> <span class=\"token punctuation\">(</span>lVar5 <span class=\"token operator\">!=</span> <span class=\"token number\">0x40</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>From here, we’ll trace what this function does.</p>\n<p>Looking at the Ghidra decompilation result, it was a simple function with just a single line.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token keyword\">void</span> <span class=\"token function\">FUN_001011e9</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">void</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">{</span>\n  DAT_0010402c <span class=\"token operator\">=</span> DAT_0010402c <span class=\"token operator\">*</span> <span class=\"token number\">0x41c64e6d</span> <span class=\"token operator\">+</span> <span class=\"token number\">0x3039</span> <span class=\"token operator\">&amp;</span> <span class=\"token number\">0x7fffffff</span><span class=\"token punctuation\">;</span>\n  <span class=\"token keyword\">return</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p><code class=\"language-text\">DAT_0010402c</code> was an undefined variable, so let’s replace it with an appropriate name like <code class=\"language-text\">variable</code>.</p>\n<p>Now, one question has arisen.</p>\n<p>Looking at the decompiled result of the caller, <code class=\"language-text\">cVar1 = FUN_001011e9();</code>, it appears as though the return value of this function is stored in <code class=\"language-text\">cVar1</code>.</p>\n<p>However, looking at the actual decompiled result of this function, it appears to be a <code class=\"language-text\">void</code> function with no return value.</p>\n<p>Which one is correct?</p>\n<p>We could determine this by reading the assembly or through dynamic analysis, but this time let’s also look at the decompilation result from IDA Free.</p>\n<h3 id=\"using-ida-free\" style=\"position:relative;\"><a href=\"#using-ida-free\" aria-label=\"using ida free 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>Using IDA Free</h3>\n<p>Since we asked you to install it in advance, let’s look at the IDA Free analysis result as well.</p>\n<p>We’ll omit a detailed explanation of IDA, so please launch it with the following command and import the challenge binary.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">$ ida64</code></pre></div>\n<p>Unlike when we analyzed with Ghidra, it has identified the symbol for the main function from the start.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 500px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/68f905ac0b6132edf6784dd5638f4d55/0b533/image-43.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 57.50000000000001%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAARlAAAEZQAGA43XUAAACW0lEQVQoz11SSXKcQBDk/3c/wZJ10QPsgw8+edPIsmTPxqzsS9M0DTTNkk5G0kEmIqOAqsjKqixn7W5xv3Gx2blYr56wXC+xdTd4XD7i9leEq4cQ7x72uP29xXr9BdvdHU5egDA64/7bAu+vrnB9c4MPxPXNBzhFVKBRHbrKohE1SqnRdh1a0wBEX2vYuoA1NbQZoZsGx3iHw1lA6ArzMxGd7S/R8X6eMPkDpmAE/AmDGmGZ6pk1c+ELLL8HxrozWG0W8IIYumXz1qC3FlJK5jo4yhOY5ICx6AGilg30MKAdJzTj+AbtNKFsLcKw5opSRJkkocXI+rKqoKnS6fD2UaaD7Cw0BVfD9AY1VQoS+L7E4zrE2vfQ9c8MSmuUM2Hz/SPM4jNawv74BOH+geBsZT9CEq9R2hGKpFlDhUGFxWaFu/MDamMuhBV3KzoS9lGEIUkvmJIEBWNiBwguMSfRDEGiYlZH1REJj8ccy02MmCNXVXcZuVAKibFwjoszDE1ByjYJ15i2CNkppQsJVaUkDuoOqyBHolukUiGIMuxPMc6phE8ir1Q40pSgpSkpO43ZgCGl7UkPXVoEJAq7iRgR0asDjdrvU2w3AmlSQtVUIwX8l/yMkAK8WWEucmjeWsWlat5Vqip4JHuFT5zbEaswx9e/LnZxBk8UOBTq8v9SZ1hjBhxpmKM46dYPcBICinuKTI99M+DQPmN+P9HIJzE76yHLKyRlhd1cw0Ofa45zJOGO+3WSxsDlkZ5ziYQ7iJVGXMj/UCDMcqSZuCBj85gq51zC3eW8QcEJ80rjH5CFjbBayOzRAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/68f905ac0b6132edf6784dd5638f4d55/8ac56/image-43.webp 240w,\n/static/68f905ac0b6132edf6784dd5638f4d55/d3be9/image-43.webp 480w,\n/static/68f905ac0b6132edf6784dd5638f4d55/b0a15/image-43.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/68f905ac0b6132edf6784dd5638f4d55/8ff5a/image-43.png 240w,\n/static/68f905ac0b6132edf6784dd5638f4d55/e85cb/image-43.png 480w,\n/static/68f905ac0b6132edf6784dd5638f4d55/0b533/image-43.png 500w\"\n            sizes=\"(max-width: 500px) 100vw, 500px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/68f905ac0b6132edf6784dd5638f4d55/0b533/image-43.png\"\n            alt=\"2021/12/image-43.png\"\n            title=\"2021/12/image-43.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>In IDA, pressing the [F5] key on the disassembly output screen performs decompilation.</p>\n<p>When we identify the function called during XOR encryption from the same line as in Ghidra and check the decompiled result, we can see that it returns an int64 type value, as shown below.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 500px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/cdd4bfe4f7829d4cc5c5b5f7fb8885ba/0b533/image-44.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 32.49999999999999%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAARlAAAEZQAGA43XUAAABFUlEQVQoz6WQTW6DMBCFOXpv0HO09+ii3aWKsiAEsJ2Af8HQAOYnvE6pqkpdpiM9eeZJ82meI+UUGI+RpCcUUsJVDvlFgikPoxWEOG+yrsIgEsSZhnQNuOC0k+JSFqh8DV5KxGeHiIsnpNkDcvZC4IJ6hkuh4L2HI4h1DtY6VFUNT73WFsZaKEVgqaC03mZtDMkialuGaXrEEJ7R9x/oug7jCISAuyqqfYcsv1GECZx3OCYGu/cSx6NBlgUcDgv2+xsYC0hOFWLyufDY7Uq8vgnkeU0HLBtsXVdEUje01FDUHmW5oq4DxR0oYk/XjmjbG3nrphAWer/963Wkr+hgzJWA8y9wuDfbn/qCbZHnZd6G/+oH+glXMxfaQoF2fgAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/cdd4bfe4f7829d4cc5c5b5f7fb8885ba/8ac56/image-44.webp 240w,\n/static/cdd4bfe4f7829d4cc5c5b5f7fb8885ba/d3be9/image-44.webp 480w,\n/static/cdd4bfe4f7829d4cc5c5b5f7fb8885ba/b0a15/image-44.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/cdd4bfe4f7829d4cc5c5b5f7fb8885ba/8ff5a/image-44.png 240w,\n/static/cdd4bfe4f7829d4cc5c5b5f7fb8885ba/e85cb/image-44.png 480w,\n/static/cdd4bfe4f7829d4cc5c5b5f7fb8885ba/0b533/image-44.png 500w\"\n            sizes=\"(max-width: 500px) 100vw, 500px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/cdd4bfe4f7829d4cc5c5b5f7fb8885ba/0b533/image-44.png\"\n            alt=\"2021/12/image-44.png\"\n            title=\"2021/12/image-44.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>As shown here, decompilation results can differ between decompilers, and sometimes the results are outright incorrect.</p>\n<p>Therefore, rather than blindly trusting a decompiler, when in doubt it is recommended to carefully read the assembly or compare the results with other tools.</p>\n<h3 id=\"understanding-the-xor-encryption-behavior\" style=\"position:relative;\"><a href=\"#understanding-the-xor-encryption-behavior\" aria-label=\"understanding the xor encryption behavior 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>Understanding the XOR Encryption Behavior</h3>\n<p>Now we know that the return value <code class=\"language-text\">cVar1</code> plus the loop counter <code class=\"language-text\">lVar5</code> is used to XOR-encrypt <code class=\"language-text\">input_text</code> one character at a time from the beginning.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token operator\">*</span><span class=\"token punctuation\">(</span>byte <span class=\"token operator\">*</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">long</span><span class=\"token punctuation\">)</span><span class=\"token operator\">&amp;</span>input_text <span class=\"token operator\">+</span> lVar5<span class=\"token punctuation\">)</span> <span class=\"token operator\">=</span> <span class=\"token operator\">*</span><span class=\"token punctuation\">(</span>byte <span class=\"token operator\">*</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">long</span><span class=\"token punctuation\">)</span><span class=\"token operator\">&amp;</span>input_text <span class=\"token operator\">+</span> lVar5<span class=\"token punctuation\">)</span> <span class=\"token operator\">^</span> cVar1 <span class=\"token operator\">+</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">char</span><span class=\"token punctuation\">)</span>lVar5<span class=\"token punctuation\">;</span></code></pre></div>\n<p>If we can find the input that makes this encryption result equal to the following byte sequence, we should be able to obtain the Flag.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token punctuation\">[</span> 0x74, 0x1a, 0x95, 0x4e, 0xba, 0xdb, 0x47, 0x64, 0x09, 0x2d, 0xd1, 0xbf, 0x8a, 0x9d, 0xde, 0x5a, 0xd7, 0x5c, 0x93, 0x16, 0x09, 0x3b, 0x30, 0x6f, 0x97, 0x40, 0xd0, 0x7c, 0x57, 0xdb, 0xde, 0x0c, 0x09, 0xa0, 0x84, 0x9b, 0x8a, 0x76, 0x2f, 0xb1, 0x57, 0xa2, 0xe1, 0x4f, 0xb9, 0x6f, 0x81, 0xbf, 0xb9, 0xbf, 0xe1, 0xef, 0x79, 0xcf, 0x01, 0xdf, 0xf9, 0x9f, 0xe1, 0x8f, 0x39, 0x2f, 0x81, 0xff, 0x00 <span class=\"token punctuation\">]</span></code></pre></div>\n<p>It is also possible to identify the Flag through static analysis alone, but that’s quite tedious, so from here we’ll perform dynamic analysis.</p>\n<p>Dynamic analysis is a method of analysis performed while actually running the executable.</p>\n<p>This time, we’ll use a debugger called gdb to perform dynamic analysis and identify the Flag.</p>\n<h2 id=\"performing-dynamic-analysis-with-gdb\" style=\"position:relative;\"><a href=\"#performing-dynamic-analysis-with-gdb\" aria-label=\"performing dynamic analysis with gdb 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>Performing Dynamic Analysis with gdb</h2>\n<p>First, let’s open the challenge binary with gdb.</p>\n<p>If you have already installed gdb-peda, a color-highlighted console will open.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">$ gdb ./revvy_chevy</code></pre></div>\n<p>We’ll skip detailed explanation of gdb-peda, but think of it as an extension that nicely visualizes register and memory information in gdb.</p>\n<p>Reference: <a href=\"https://github.com/longld/peda\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">longld/peda: PEDA - Python Exploit Development Assistance for GDB</a></p>\n<p>The basic operations when solving CTF problems with gdb are as follows:</p>\n<ul>\n<li><strong>Set breakpoints at suspicious locations or places where you want to understand the behavior</strong></li>\n<li><strong>Stop processing at a breakpoint and reference memory and register information</strong></li>\n<li><strong>To obtain the Flag, tamper with the memory or register data of the running program to invoke processing that would not normally be executed</strong></li>\n</ul>\n<h3 id=\"finding-the-gdb-load-address\" style=\"position:relative;\"><a href=\"#finding-the-gdb-load-address\" aria-label=\"finding the gdb load address 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>Finding the gdb Load Address</h3>\n<p>First, let’s try setting a breakpoint at the main function.</p>\n<p>In gdb, breakpoints can be set with either of the following commands:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">b <span class=\"token operator\">&lt;</span>breakpoint target<span class=\"token operator\">></span>\n<span class=\"token builtin class-name\">break</span> <span class=\"token operator\">&lt;</span>breakpoint target<span class=\"token operator\">></span></code></pre></div>\n<p>For the breakpoint target, you can specify a function name, a line number in the current file, an offset from the current point, a memory address, etc.</p>\n<p>In CTF cases like this one where symbol information is often not provided, setting breakpoints by memory address will generally be the main approach.</p>\n<p>Earlier, when we identified the main function in Ghidra, the main function address was <code class=\"language-text\">0x1208</code>.</p>\n<p>However, specifying this address in gdb will not set a breakpoint at the main function.</p>\n<p>When setting a breakpoint in gdb, you need to specify the RVA that gdb loads when it runs the program.</p>\n<p>The main function address <code class=\"language-text\">0x1208</code> is a virtual address (VA), so to determine the RVA, we’ll identify the base address to which gdb maps memory when executed.</p>\n<p>To identify the base address, let’s run the challenge binary from gdb for now.</p>\n<p>Running with the <code class=\"language-text\">run</code> command prompts for standard input as before.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">$ run\nStarting program: /home/parrot/Downloads/revvy_chevy \nWhat's the flag? </code></pre></div>\n<p>Press [Ctrl+C] here to interrupt the program.</p>\n<p>Pressing [Ctrl+C] generates a keyboard interrupt SIGINT, which interrupts program execution and lets you interact with gdb.</p>\n<p>In this state, run the <code class=\"language-text\">info proc mappings</code> command.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">$ info proc mappings \nprocess <span class=\"token number\">1971</span>\nMapped address spaces:\n          Start Addr           End Addr       Size     Offset objfile\n      0x555555554000     0x555555555000     0x1000        0x0 /home/parrot/Downloads/revvy_chevy\n      0x555555555000     0x555555556000     0x1000     0x1000 /home/parrot/Downloads/revvy_chevy\n      0x555555556000     0x555555557000     0x1000     0x2000 /home/parrot/Downloads/revvy_chevy\n      0x555555557000     0x555555558000     0x1000     0x2000 /home/parrot/Downloads/revvy_chevy\n      0x555555558000     0x555555559000     0x1000     0x3000 /home/parrot/Downloads/revvy_chevy\n    /* omitted */</code></pre></div>\n<p>This gives you the mapping information between the challenge binary offsets and the memory addresses loaded by gdb.</p>\n<p>It appears that file offset <code class=\"language-text\">0x1000</code> is mapped to <code class=\"language-text\">0x555555555000</code>.</p>\n<p>From the surface-level analysis results with readelf and radare2, we know the <code class=\"language-text\">.text</code> section address is <code class=\"language-text\">0x1100</code>, so <code class=\"language-text\">0x1100</code> corresponds to <code class=\"language-text\">0x555555555100</code> at gdb runtime.</p>\n<p>It may be a bit confusing, but the fact that address <code class=\"language-text\">0x1100</code> is loaded to <code class=\"language-text\">0x555555555100</code> at gdb runtime means that the main function address <code class=\"language-text\">0x1208</code> is loaded to <code class=\"language-text\">0x555555555208</code> in gdb.</p>\n<h3 id=\"setting-breakpoints\" style=\"position:relative;\"><a href=\"#setting-breakpoints\" aria-label=\"setting breakpoints 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>Setting Breakpoints</h3>\n<p>Now that we’ve identified the RVA of the main function, let’s set a breakpoint and run it.</p>\n<p>Set the breakpoint with the following command.</p>\n<p>When specifying an address for a breakpoint, you need to prefix it with <code class=\"language-text\">*</code>.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">$ b *0x555555555208\nBreakpoint <span class=\"token number\">1</span> at 0x555555555208</code></pre></div>\n<p>Breakpoints can be confirmed with <code class=\"language-text\">i breakpoint</code>.</p>\n<p>We won’t use it this time, but the <code class=\"language-text\">Num</code> value is the breakpoint ID, which can be used to delete a breakpoint with <code class=\"language-text\">delete &lt;Num></code> or <code class=\"language-text\">d &lt;Num></code>.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">i breakpoints \nNum     Type           Disp     Enb Address            What\n<span class=\"token number\">1</span>       breakpoint     keep y   0x0000555555555208</code></pre></div>\n<p>Now that the breakpoint has been confirmed, call the <code class=\"language-text\">run</code> command.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 960px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/59ec8e486270cce47644255bfe5bfac0/2bef9/image-46-1024x914.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 89.16666666666667%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAASCAYAAABb0P4QAAAACXBIWXMAARlAAAEZQAGA43XUAAACrElEQVQ4y5VU2W7jMBDz1+SwY/mQrMNnnGt7oEVR9P9/hctR6jTZBRbYh4ElxxqRHDKJGQNaH6BrjaqsYIyGDwGK61wV2O12rAzr9RqbzYbPTVzLU/bb7fZWsk+auUe/PyFMR7T7M2pt4PoZHdfdfME0DTgeue8C+r5FVZUoyyJWUajvS9a35omdOwTnYWpDdA2C92iJMCcyledQSkXUUk1jYG0TG12blhG97G8I69bCNRaWzQzRheDQBY88y1hppCwHliaCcKEovymVo66rn4a6c/C8OeOLPE3jQa1r6pdjRSrSoCiu9Far1Y3efT1S3nfoSFMRkeKNuq4jvXQRm5fcC/+viggdG05jH1+kkUYWqWz/uP1ftTSLCCtStt5GVFmp0FrR0yCtC3TWofEOaaUoS4NT16GiHLu65Le0lOZQKM29fRLnHH49XfDy+gTHwzLVhoc91+fzEfNhhnMWmhdWVYU0zaKmhrrL91rrB5TJSLofH2/4+vqArFNq2Z73ML6Jtsk46cVz6hvNYhV5L+8eKLdtwPv7Kz4/3zEMXfx4Ooy83UbfLToudT/ZpRa6KQeYGNKb5z3TcIgNcqKSSYtpL5cT+nGIZhbaGdFn2e46eZbQl7ofXvLy+gzfT/DDAWE8orGe+gUM40STd9w7uNCi7QeEjjEdembdRw2FXde1USoxt3g2eSPdfj7F7A5HDoax63ngMJI20yPopmmkCwwHY+Iho+uon7Xcx/hdEyPsEpluOwq6Axs+wzLXA/8IrGSXBz2p1jEt1/hJxkUr8aw4QrT70ZOUp/0E4wKrhW1HVPW1UXBXPwotoSjabb6Ts2gX19R1u73zoTRsDCkUJZrQo+Kfg2YUnfdxCEv0Iqq/KovPB2NLAkQzmaIq2bjSKPhUvCDPd9GH/xO93yWjNWbpb/9EAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/59ec8e486270cce47644255bfe5bfac0/8ac56/image-46-1024x914.webp 240w,\n/static/59ec8e486270cce47644255bfe5bfac0/d3be9/image-46-1024x914.webp 480w,\n/static/59ec8e486270cce47644255bfe5bfac0/e46b2/image-46-1024x914.webp 960w,\n/static/59ec8e486270cce47644255bfe5bfac0/a9a89/image-46-1024x914.webp 1024w\"\n              sizes=\"(max-width: 960px) 100vw, 960px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/59ec8e486270cce47644255bfe5bfac0/8ff5a/image-46-1024x914.png 240w,\n/static/59ec8e486270cce47644255bfe5bfac0/e85cb/image-46-1024x914.png 480w,\n/static/59ec8e486270cce47644255bfe5bfac0/d9199/image-46-1024x914.png 960w,\n/static/59ec8e486270cce47644255bfe5bfac0/2bef9/image-46-1024x914.png 1024w\"\n            sizes=\"(max-width: 960px) 100vw, 960px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/59ec8e486270cce47644255bfe5bfac0/d9199/image-46-1024x914.png\"\n            alt=\"2021/12/image-46-1024x914.png\"\n            title=\"2021/12/image-46-1024x914.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>Processing stopped at the main function call timing, and gdb-peda displayed register and stack information.</p>\n<p>By the way, the <code class=\"language-text\">run</code> command launches a process from gdb; to pass command-line arguments at runtime, call it as <code class=\"language-text\">run &lt;command-line arguments></code>.</p>\n<h3 id=\"changing-the-ghidra-image-base\" style=\"position:relative;\"><a href=\"#changing-the-ghidra-image-base\" aria-label=\"changing the ghidra image base 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>Changing the Ghidra Image Base</h3>\n<p>From here, we’ll proceed with analysis by correlating Ghidra’s decompilation results with gdb, so let’s change Ghidra’s base address to <code class=\"language-text\">0x555555554000</code> to match gdb.</p>\n<p>Changing the Ghidra base address can be done from [Options] at file import time, or by opening [Window] > [Memory Map] and clicking the [Set Image Base] button on the right.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 500px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/67fe8c8bb0b1f268968790135c4af31e/0b533/image-47.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 91.25000000000001%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAASCAYAAABb0P4QAAAACXBIWXMAARlAAAEZQAGA43XUAAAEWElEQVQ4y12Ue0yTZxSHv6pTQQQp4AbqdPMu6Bx4WXTgyBxDRd1ccM4su2UxJmabxgtTUZEquika/zCZm85ApoCIlIsUKCi3OJ2ZmukWY7zgUIyTUlpov7a0PDttGTrf5OS8pz15ct7v/M5RFi/7lOQlK5n/7gfMT17OOwtSSUhcSvKCFSxcnMpbSe+TmLyElPc+IT5xEdPjEpk0ZQ4DtVNRhsWihMaJjxEfjRL4MkpJSQW379zjyo3b/HGzmSt/3qHx0nXqGq9Rd/GqLz50+DhbNu8m5+gpDh78kfR0HcO0kSj9tGgCRqIEjUMJnizQaSg1xnq6PWCyQ7vdjcUJD6xmbrU+4W+zFRU4mnuGigoj/x2LxUxk1GgUZQAaTYD4wSiaIJQBWpSqqnN02Z20ttu5ee8x1/6y0WqSuE2l1aziEsBPOYXo9RU+mMfj4f79FsLD5XnKIPr1D/LBNJohvliA53E6XdhVB/eam7F29hWCu9cXnTmLsaau73erJIUNf8UPUIKeVuj1fqAT1a6iLynlxEk9lVVGiovPoi8ro6Gxie0Zu8nau5+6+gaqjbXkF5xmzNgYgkMi0WqjCNWOJGzEq4QMl+9ZVd1boQA/W7WWCZNnMCV6JuMnzWDchDjGTZxB7MxEn02ZOsdn0VPnMj12HtNej+e12ASxecTNTmRizEw/0OVy0dVl4+jPuWzLzGZjxiEydfvZtSubHRnfkbric1KWfiT+CxYt/VBktoyvv9nKuvXbWZ+2gw1pmWTs3E+SSEyplKa43W7M5g4ePTZh6nTRYlJps9ixuzz8fuMObyelEp+Qwtw3FzHrjSQWpizn4vX7dNg9WLocmCw2RCjo9hzwd9kLNJnaefTELJAeOu0uOrqcCE+Ad5k1eyGxcfMFtoDomATi56bQcLWZDrUHq+S2d6r0CDBj177/Ay02p18az3T47mM7ur2HyfnlFLkniyg9WyvN+ZVbj1SkQF+u2pucmZXtf3J3t//J5YZzFJca0ZfX+MwbH8urIOvAEfILSzmWU4DB2Eh902WKDBeoPn+J8w2/YTx3gYuXr7N6zYanTfFaU9MlSksr0RcZOJ0vsik2UFNdz+YtOnZmfk9NTT3l5dXk5BYQFTWegIAwhgZHEhw+lqHa0QRKrBhl9Lyycbm6efY4rD24bP57Xn7Jc8K2EhY2QoTcX8YtVGY6GCUgXO4hfmF7YaqqSmKXmAebzTs5LswSez/P8dw8yssMfcCWlodEjJIN88JL9PNCZEI0SiCaAUNRDIZagTnERCYy0+3tHpwONz0ysw6nv0kn8gopewb44MFDwiNlwwSMQfOiiDlCVljoZDSDh6PUN1zg+eN2iBz+UelsU31xQWEJhsravv/bTCYiwr1PVtAMEsiQUSiDIuTpUqFOl80PR3JI26wjffsetqRn+e4bN+1k7bptbJIpWPnxar5ctY6t27LY9G0ma75KIygkqnc5DPStMZ/XBPIvaq/fVXR6p6gAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/67fe8c8bb0b1f268968790135c4af31e/8ac56/image-47.webp 240w,\n/static/67fe8c8bb0b1f268968790135c4af31e/d3be9/image-47.webp 480w,\n/static/67fe8c8bb0b1f268968790135c4af31e/b0a15/image-47.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/67fe8c8bb0b1f268968790135c4af31e/8ff5a/image-47.png 240w,\n/static/67fe8c8bb0b1f268968790135c4af31e/e85cb/image-47.png 480w,\n/static/67fe8c8bb0b1f268968790135c4af31e/0b533/image-47.png 500w\"\n            sizes=\"(max-width: 500px) 100vw, 500px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/67fe8c8bb0b1f268968790135c4af31e/0b533/image-47.png\"\n            alt=\"2021/12/image-47.png\"\n            title=\"2021/12/image-47.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>Now the main function address has also been changed to <code class=\"language-text\">0x555555555208</code>, which matches the address loaded in gdb, making the correspondence clearer.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 824px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/17181c775952044021d3f1860e572160/c1c45/image-48.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 109.16666666666669%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAWCAYAAADAQbwGAAAACXBIWXMAAAsTAAALEwEAmpwYAAAD0UlEQVQ4y31V227bRhTU7zZFYRSJnSIoegGKNmid3hDJMdAm6UP7Bf2GPiQP7oOR1HasUKQkikvxfucuOZ1dyrJUAxWwXmIvs3PmzDkefXDvB3z+xZ84PPwDR0e/46ODX3Hvw+c4OPgFDx6c4uP7Exx+MsbRwx9x/+gYDx8d49FnHJ8+xk8/n+Kbr5/gu+On+PKr7/Ht4ycYnTx7Cf1brwNkWYo0DeH7QJ6bZTRNb+a6LZA2BRql0CmgH5bv/EZPx8/Nx9L14HkC87kNZ14bUCkVkqRG13V8oEBZ1ny0RRBWXC8hVWOAe/7pNmM03gBac4E1D/tpjajoQRyEhcI6JUAuEfM7KiT3K4R5g6hUZn2dNeZeXPFOtQP43hHgwwjKHise0N+2n+F6sYYrEszWOZyggCMyLDh7SWPGKtYzz/NezDs7gB4iLujN6TwhE4VVVMNyYvheAcvN4PglYjLX5zTAzbDjBKJoYcXxPmDIw+tQYn5JwICvZh2WVoHFrIBrk5VLOeoBTIPeAJ+tplikBf5evf8PQx4WQQPrPITwqU0iYV/GmF+ncN6lWDrFFvAGVM+vlxewkwRv/CVtc/Jyj6Hn15ie+fBETcAO1psI9kVsgBdWvgeoNdPjL+ccTpLiXDgYTSYvtkmhvswmDLuA4QYUbE3GfiDJnA6gHGF1G7LIW7hZCSuKMKOOZ+701tiWvcLUFriYLSl+C5HCAL6zM/g5/dh0oIMMI20pPfyCGc5pI1roMnAZMhmOJ4OGLo399u0/ePX6FWazFPQxyoohWwnatkWSCVRSGvMqGl3PN9XS7ZTNFnC1Eqb8PG+J6bTB1ZWuFF0h1AE0bRow5AB1XSKtcoIqs94bsGHuTaVsAZnlKEYY+nAcaibM2yhYHbr00jRFVeZomwqSa3VRkJk0d/sN236XoUvA+XxBdle4vi6xXA4MI6rf9x1r2EcU56xnhYzJiKhvEJRkPIDqRxUbx17IYUj/CZfADTUdAAtWgH67yBNUFRuF6iAbCcU9vd91/ZalBh1NNj7UIbtEse0pGVbUE4aZZqgB67ULGQfoqhJdlqBXcq9taXaG4bPT37YMhfCxWDhkWBsNldLaNQNwHDLzKUPO0TDkvKygerVlt83y5OTFFrAsS46MtlEENUfZYJW5UBKgrpkQ2UBVClVdQ3ZqJyl3siwofMbGGTEhkkyHkKtKbpKS8btglmuTZf3ALuCtD8e3tgmCISmW1ZAlNp26NYAxW1NG7WoNyn6ZZQy9k/8HKBgOs8e27nmdsY0OWakhlGFPe1INmdb/W/r+DuC/tSpvqIth0MsAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/17181c775952044021d3f1860e572160/8ac56/image-48.webp 240w,\n/static/17181c775952044021d3f1860e572160/d3be9/image-48.webp 480w,\n/static/17181c775952044021d3f1860e572160/5758c/image-48.webp 824w\"\n              sizes=\"(max-width: 824px) 100vw, 824px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/17181c775952044021d3f1860e572160/8ff5a/image-48.png 240w,\n/static/17181c775952044021d3f1860e572160/e85cb/image-48.png 480w,\n/static/17181c775952044021d3f1860e572160/c1c45/image-48.png 824w\"\n            sizes=\"(max-width: 824px) 100vw, 824px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/17181c775952044021d3f1860e572160/c1c45/image-48.png\"\n            alt=\"2021/12/image-48.png\"\n            title=\"2021/12/image-48.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<h3 id=\"commonly-used-gdb-commands-partial-list\" style=\"position:relative;\"><a href=\"#commonly-used-gdb-commands-partial-list\" aria-label=\"commonly used gdb commands partial list 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>Commonly Used gdb Commands (Partial List)</h3>\n<p>From here we’ll proceed with dynamic analysis in earnest, but first let’s organize the commonly used gdb commands.</p>\n<p>Only a very limited set of commands are introduced here, but books such as <a href=\"https://amzn.to/3lkunSs\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Debug Hacks</a> are helpful for more detail.</p>\n<table>\n<thead>\n<tr>\n<th align=\"center\">Command</th>\n<th align=\"center\">Purpose</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td align=\"center\">break &#x3C;breakpoint><br />b &#x3C;breakpoint></td>\n<td align=\"center\">Set a breakpoint<br />Prefix with <code class=\"language-text\">*</code> when specifying an address</td>\n</tr>\n<tr>\n<td align=\"center\">info &#x3C;argument><br />i &#x3C;argument></td>\n<td align=\"center\">Display information about the running process<br />Running without arguments displays help</td>\n</tr>\n<tr>\n<td align=\"center\">run &#x3C;command-line arguments></td>\n<td align=\"center\">Run the process</td>\n</tr>\n<tr>\n<td align=\"center\">p/&#x3C;format> $eax<br />p/&#x3C;format> variable</td>\n<td align=\"center\">Display the value of a variable or register<br />Commonly used formats: x / d / c / s / i</td>\n</tr>\n<tr>\n<td align=\"center\">x/&#x3C;format> &#x3C;memory address></td>\n<td align=\"center\">Display the contents of memory<br />Can also reference the address pointed to by registers such as $ecx</td>\n</tr>\n<tr>\n<td align=\"center\">next<br />n</td>\n<td align=\"center\">Execute one line at a time<br />Does not jump into function calls</td>\n</tr>\n<tr>\n<td align=\"center\">step<br />s</td>\n<td align=\"center\">Execute one step at a time<br />Jumps into function calls</td>\n</tr>\n<tr>\n<td align=\"center\">continue<br />c</td>\n<td align=\"center\">Resume process execution</td>\n</tr>\n<tr>\n<td align=\"center\">finish</td>\n<td align=\"center\">Execute until the current function returns</td>\n</tr>\n<tr>\n<td align=\"center\">until<br />u</td>\n<td align=\"center\">Execute until the specified line</td>\n</tr>\n</tbody>\n</table>\n<p>The following cheat sheet is also useful in practice:</p>\n<p>Reference: <a href=\"https://darkdust.net/files/GDB%20Cheat%20Sheet.pdf\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">GDB Cheat Sheet</a></p>\n<h3 id=\"planning-the-analysis-approach\" style=\"position:relative;\"><a href=\"#planning-the-analysis-approach\" aria-label=\"planning the analysis approach 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>Planning the Analysis Approach</h3>\n<p>We can now set breakpoints with gdb, but setting breakpoints blindly makes it very difficult to identify the Flag.</p>\n<p>Therefore, let’s first plan an analysis strategy based on the static analysis results.</p>\n<p>What we know so far is as follows:</p>\n<ul>\n<li>The string input by the user is XOR-encrypted and compared against the byte sequence at PTR<em>DAT</em>00104010 (named when image base was <code class=\"language-text\">0x100000</code>)</li>\n<li>XOR encryption is performed one character at a time, and the key used is the return value of function FUN_001011e9 (named when image base was <code class=\"language-text\">0x100000</code>) plus the loop counter <code class=\"language-text\">lVar5</code></li>\n</ul>\n<p>XOR cipher uses the same key for both encryption and decryption.</p>\n<p>That is, if encryption is performed as <code class=\"language-text\">A ^ K = B</code>, the original data can be decrypted with <code class=\"language-text\">B ^ K = A</code>.</p>\n<p>For this reason, if we can identify the key used by the challenge binary for encryption, we can perform XOR operations on the byte sequence stored in PTR<em>DAT</em>00104010 (named when image base was <code class=\"language-text\">0x100000</code>) to recover the original Flag string.</p>\n<p>Here, the base value used to generate the XOR key per character was being produced by the following code:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">DAT_0010402c <span class=\"token operator\">=</span> DAT_0010402c * 0x41c64e6d + 0x3039 <span class=\"token operator\">&amp;</span> 0x7fffffff<span class=\"token punctuation\">;</span></code></pre></div>\n<p>Of course, it is possible to identify the key through static analysis as well, but since that is somewhat tedious, we’ll use dynamic analysis to identify the key.</p>\n<p>In other words, we’ll use dynamic analysis to identify the return value of function FUN_001011e9 (named when image base was <code class=\"language-text\">0x100000</code>).</p>\n<h3 id=\"about-x86_64-architecture-registers\" style=\"position:relative;\"><a href=\"#about-x86_64-architecture-registers\" aria-label=\"about x86_64 architecture registers 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>About x86_64 Architecture Registers</h3>\n<p>Before identifying the function return value with gdb, let’s briefly touch on registers.</p>\n<p>The x86_64 architecture is Intel’s x86 architecture extended to 64 bits.</p>\n<p>An x86_64 architecture CPU has 16 64-bit general-purpose registers, one 64-bit RPI register and one RFLAGS register, and 16 128-bit XMM registers.</p>\n<p>The main uses of the key registers are summarized below.</p>\n<table>\n<thead>\n<tr>\n<th align=\"center\">Register</th>\n<th align=\"center\">Purpose</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td align=\"center\">RAX<br />(Accumulator)</td>\n<td align=\"center\">A general-purpose register mainly storing arithmetic results and function return values<br />The lower 32 bits are used as the EAX register</td>\n</tr>\n<tr>\n<td align=\"center\">RBX<br />(Base Register)</td>\n<td align=\"center\">A general-purpose register mainly storing pointers to data<br />The lower 32 bits are used as the EBX register</td>\n</tr>\n<tr>\n<td align=\"center\">RCX<br />(Counter Register)</td>\n<td align=\"center\">A general-purpose register mainly storing string and loop counters<br />The lower 32 bits are used as the ECX register</td>\n</tr>\n<tr>\n<td align=\"center\">RDX<br />(Data Register)</td>\n<td align=\"center\">Mainly used as a variable in I/O pointer calculations<br />The lower 32 bits are used as the EDX register</td>\n</tr>\n<tr>\n<td align=\"center\">RSI<br />(Source Index)</td>\n<td align=\"center\">Mainly used for string copy destinations and similar<br />The lower 32 bits are used as the ESI register</td>\n</tr>\n<tr>\n<td align=\"center\">RDI<br />(Destination Index)</td>\n<td align=\"center\">Mainly used to specify the destination in string operations<br />The lower 32 bits are used as the EDI register</td>\n</tr>\n<tr>\n<td align=\"center\">RSP<br />(Stack Pointer Register)</td>\n<td align=\"center\">Used as a stack pointer<br />The lower 32 bits are used as the ESP register</td>\n</tr>\n<tr>\n<td align=\"center\">RBP<br />(Base Pointer Register)</td>\n<td align=\"center\">Used as a pointer to data on the stack<br />The lower 32 bits are used as the EBP register</td>\n</tr>\n<tr>\n<td align=\"center\">RIP<br />(Instruction Pointer Register)</td>\n<td align=\"center\">Stores the instruction set</td>\n</tr>\n<tr>\n<td align=\"center\">RFLAGS<br />(Flag Register)</td>\n<td align=\"center\">The lower 32 bits are used as the EFLAGS flag register</td>\n</tr>\n</tbody>\n</table>\n<p>Reference: <a href=\"https://amzn.to/3lkunSs\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Debug Hacks -デバッグを極めるテクニック&#x26;ツール</a></p>\n<p>Reference: <a href=\"https://amzn.to/3o7g5Gj\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">詳解セキュリティコンテスト</a></p>\n<p>Details of each register and architecture are omitted here, but since function return values after execution are stored in the RAX register, the basic approach to obtaining a function’s result is to reference the RAX register immediately after the CALL instruction.</p>\n<h3 id=\"identifying-the-function-return-value\" style=\"position:relative;\"><a href=\"#identifying-the-function-return-value\" aria-label=\"identifying the function return value 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>Identifying the Function Return Value</h3>\n<p>From the Ghidra result, we can see that the address calling the key-generating function is <code class=\"language-text\">0x5555555552b3</code>.</p>\n<p>That means the value stored in the RAX register at the next instruction, <code class=\"language-text\">0x5555555552b8</code>, is the return value of this function.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 960px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/95f8a3a684ff495a86e22079911cfeda/2bef9/image-49-1024x657.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 64.16666666666666%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAARlAAAEZQAGA43XUAAACoklEQVQ4y02TWW8TZxiF86t7hxICtCUIqf+lqrhBQhVSJVoSYgczjh2H2I6X8ezrt8w38/SdIQRGOtJc2GfO9h4ppQmCSLBjuVTsdqCUxbmGrEyoCkX/eNdznj8/I8rh9j7D6C1d2/H96bpv70dKKcIwJo5DtltNUYC1Dtc02CLD1dUj4emzM4xtieKYpMipTEqpE3SjH0mPeoXb7Z7JZMxikZGLgqLQaC3KfZ8wKIcfT4Xw+OkrGptxCNdCpLFOCVlN07ofhHWtOBx6dWtWq1qsQ1Vb+aOliiOyIKEVxdP5jSh8LVEkJNmSjh92f7Y9EPqHA6v1naAkToRQGWxjSNMAoyQDHGPP48Vvf2BNhh+siVKJp8rQtqSyhvZ7hlVVk4YZVVpSxB1WIitih8odbeMev764vuNELFvbkORSlhSnbW9b0DYDYY+B8Dy44J/wPX9vrzgvp5xXI0ZqxCT/wJfyIzfM+PP6DcenZ3SmI00UpWSta43TOVbLEh4SOFJi+XIz4s34L94vPC7DDVfpnBs945P/L9N8wh23vB2/45enJ1y3Hv9FH/kQeJxHEzwzFkz4rKd8Vl++zcbfBEyvJqwWtdiFMhG72klWEXXZPVhe8uT0d/IuYbFf4G1nrPI7dibiXrDRByIb9aVIs2EkZQQsV9K4tKxNP2xpWXxlkq2V7X0ajSVDsdxAFEneKqeV8Q9Wf8LQ8n7vc3s7Z72u5Er6HUrLxlCGPnWSPwx7xq8vzmhbyGTUqlFShpUiGuFxgnZgHErZ7Xzm8ylfvxYyFbFcGowQBvvgcdgXl96g0LmOuk5l+ApVyTWpUkTUKG3o3MNs+mHvdhvu7xWJ7LCWYTvZlsoiOb16IJx4M45PXiIXSZq11ELWSL5yKMjBSNsCWcD/JWXPeFf1UcgAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/95f8a3a684ff495a86e22079911cfeda/8ac56/image-49-1024x657.webp 240w,\n/static/95f8a3a684ff495a86e22079911cfeda/d3be9/image-49-1024x657.webp 480w,\n/static/95f8a3a684ff495a86e22079911cfeda/e46b2/image-49-1024x657.webp 960w,\n/static/95f8a3a684ff495a86e22079911cfeda/a9a89/image-49-1024x657.webp 1024w\"\n              sizes=\"(max-width: 960px) 100vw, 960px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/95f8a3a684ff495a86e22079911cfeda/8ff5a/image-49-1024x657.png 240w,\n/static/95f8a3a684ff495a86e22079911cfeda/e85cb/image-49-1024x657.png 480w,\n/static/95f8a3a684ff495a86e22079911cfeda/d9199/image-49-1024x657.png 960w,\n/static/95f8a3a684ff495a86e22079911cfeda/2bef9/image-49-1024x657.png 1024w\"\n            sizes=\"(max-width: 960px) 100vw, 960px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/95f8a3a684ff495a86e22079911cfeda/d9199/image-49-1024x657.png\"\n            alt=\"2021/12/image-49-1024x657.png\"\n            title=\"2021/12/image-49-1024x657.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>At <code class=\"language-text\">0x5555555552b8</code>, the return value of the key-generating function is further stored with the value in EBX.</p>\n<p>This is ultimately the key used for XOR encryption.</p>\n<p>The result of the ADD instruction, like a function return value, is stored in the accumulator (RAX).</p>\n<p>So, let’s set a breakpoint at <code class=\"language-text\">0x5555555552ba</code> in gdb and run it.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">$ b *0x5555555552ba\n$ run</code></pre></div>\n<p>We can see that the value of the RAX register is <code class=\"language-text\">0x3039</code>.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 882px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/2be958fb5687e7e5eee58a87bf7d710d/90712/image-51.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 71.25%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAAAsTAAALEwEAmpwYAAACFElEQVQ4y31TWZLiMBTLYSALZN+8ZHEgDTQNNNX3P4xGNgUzUPR8qOLYid6T9OzVc4ftdoKUAlmWIU1TRFGE1Wr1hCh6Xttv3sErjYIZe6z54XKxQOD7CMMQvh8gCP4iDOyef1vz/Dd41UajGw1kb1AL7ZDnOYahQ99rh65TKMsC0zQ6JbYT+/PbDq1koQd0wxa6n6D6Deq6wn4/43T6JA44HvfQWuF6Pbn9JIn/I7kXKEjQ9gpi0Gi0gBAtRjPgdP4i2QGHzz19NpjnDTYb4wpaL9916RWDgB5H6MmQdECltAumkRqyG9ixQSMkSUo0Tf2Qeyd7JfXKUWKkT6bTyOIYCSsXRf7o4l0Qv8l9EBoGkNtxYZIxSdIsRcEQbPI23cgSBTcyS75gkaWdhiD8B4GDV207GPpjfYtpdpisUTHlkgVWKc3nexSvsc4S917kGfq2RVuWbs8hJfIEMZ9eSaJWS46EwcAgCvokG4bSseumcmeCa+ttXGSo6hqGa0XShCrSO6oSGeHlrHi9nh1+fr6hlcRiuWRAEinPxh1v0dQhrnKs2G3g3yT7v0m2Ui+XrwehImEYhejnETmTnS57SK5T3aCg33YqqkkhYQHn72sotsPz+ehILdq2ccZLys3oqVECvWhQ5ini9fPsvY6Ou3oZCT8+tu4G7HazGxd7IEWNjCabTmJQ9LkqkDAc9/ObObw//wB+7blQheitHAAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/2be958fb5687e7e5eee58a87bf7d710d/8ac56/image-51.webp 240w,\n/static/2be958fb5687e7e5eee58a87bf7d710d/d3be9/image-51.webp 480w,\n/static/2be958fb5687e7e5eee58a87bf7d710d/9d646/image-51.webp 882w\"\n              sizes=\"(max-width: 882px) 100vw, 882px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/2be958fb5687e7e5eee58a87bf7d710d/8ff5a/image-51.png 240w,\n/static/2be958fb5687e7e5eee58a87bf7d710d/e85cb/image-51.png 480w,\n/static/2be958fb5687e7e5eee58a87bf7d710d/90712/image-51.png 882w\"\n            sizes=\"(max-width: 882px) 100vw, 882px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/2be958fb5687e7e5eee58a87bf7d710d/90712/image-51.png\"\n            alt=\"2021/12/image-51.png\"\n            title=\"2021/12/image-51.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>By the way, register values can also be obtained using the <code class=\"language-text\">p</code> command.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">$ p <span class=\"token variable\">$rax</span>\n<span class=\"token variable\">$2</span> <span class=\"token operator\">=</span> 0x3039</code></pre></div>\n<p>In particular, since the byte sequence after XOR encryption is of char type in this case, only the lower 8 bits of the RAX register value are used as the XOR encryption key.</p>\n<p>To extract only the lower 8 bits of a specific register, output the <code class=\"language-text\">$al</code> register value with the <code class=\"language-text\">p</code> command.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">$ p <span class=\"token variable\">$al</span>\n<span class=\"token variable\">$3</span> <span class=\"token operator\">=</span> 0x39</code></pre></div>\n<p>This means the key for encrypting the first character is <code class=\"language-text\">0x39</code>.</p>\n<p>Since this key is generated each time a character is encrypted, using the <code class=\"language-text\">c</code> command to resume execution will bring us to the next breakpoint at the time of encrypting the second character.</p>\n<p>Using this method, we identified the keys for the first four characters.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">1st character: 0x39\n2nd character: 0x7f\n3rd character: 0xe1\n4th character: 0x2f</code></pre></div>\n<p>Let’s try decrypting the first four characters of the Flag using this key and the byte sequence identified from Ghidra earlier.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token punctuation\">[</span> 0x74, 0x1a, 0x95, 0x4e, 0xba, 0xdb, 0x47, 0x64, 0x09, 0x2d, 0xd1, 0xbf, 0x8a, 0x9d, 0xde, 0x5a, 0xd7, 0x5c, 0x93, 0x16, 0x09, 0x3b, 0x30, 0x6f, 0x97, 0x40, 0xd0, 0x7c, 0x57, 0xdb, 0xde, 0x0c, 0x09, 0xa0, 0x84, 0x9b, 0x8a, 0x76, 0x2f, 0xb1, 0x57, 0xa2, 0xe1, 0x4f, 0xb9, 0x6f, 0x81, 0xbf, 0xb9, 0xbf, 0xe1, 0xef, 0x79, 0xcf, 0x01, 0xdf, 0xf9, 0x9f, 0xe1, 0x8f, 0x39, 0x2f, 0x81, 0xff, 0x00 <span class=\"token punctuation\">]</span></code></pre></div>\n<p>When we actually decrypted the first four characters, the output was <code class=\"language-text\">Meta</code>, which matches the MetaCTF flag format.</p>\n<div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\">enc <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span> <span class=\"token number\">0x74</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x1a</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x95</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x4e</span> <span class=\"token punctuation\">]</span>\nkey <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span> <span class=\"token number\">0x39</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x7f</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0xe1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x2f</span> <span class=\"token punctuation\">]</span>\n<span class=\"token keyword\">for</span> i <span class=\"token keyword\">in</span> <span class=\"token builtin\">range</span><span class=\"token punctuation\">(</span><span class=\"token number\">4</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n<span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span><span class=\"token builtin\">chr</span><span class=\"token punctuation\">(</span>enc<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span> <span class=\"token operator\">^</span> key<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">,</span>end<span class=\"token operator\">=</span><span class=\"token string\">\"\"</span><span class=\"token punctuation\">)</span>\n<span class=\"token operator\">>></span><span class=\"token operator\">></span> Meta</code></pre></div>\n<p>Now we just need to identify all 0x40 characters’ worth of keys to get the Flag.</p>\n<p>However, repeating this process 56 more times is quite tedious.</p>\n<p>So from here, we’ll automate the gdb processing to obtain the Flag all at once.</p>\n<h2 id=\"automating-gdb\" style=\"position:relative;\"><a href=\"#automating-gdb\" aria-label=\"automating gdb 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>Automating gdb</h2>\n<p>gdb can be automated using <code class=\"language-text\">.gdbinit</code> or <code class=\"language-text\">gdb-python</code>.</p>\n<p>Reference: <a href=\"https://stackoverflow.com/questions/10748501/what-are-the-best-ways-to-automate-a-gdb-debugging-session\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">scripting - What are the best ways to automate a GDB debugging session? - Stack Overflow</a></p>\n<p>Reference: <a href=\"https://sourceware.org/gdb/onlinedocs/gdb/Python.html#Python\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Python (Debugging with GDB)</a></p>\n<p><code class=\"language-text\">.gdbinit</code> is simpler for automating gdb command operations, but since we want to perform calculations based on the retrieved values this time, we’ll use <code class=\"language-text\">gdb-python</code>, which makes it easier to define more flexible processing.</p>\n<h3 id=\"using-gdb-python\" style=\"position:relative;\"><a href=\"#using-gdb-python\" aria-label=\"using gdb python 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>Using gdb-python</h3>\n<p>When debugging using <code class=\"language-text\">gdb-python</code>, the following Python script is the basic template.</p>\n<div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token keyword\">import</span> gdb\n\nBINDIR <span class=\"token operator\">=</span> <span class=\"token string\">\"~/Downloads\"</span>\nBIN <span class=\"token operator\">=</span> <span class=\"token string\">\"revvy_chevy\"</span>\nINPUT <span class=\"token operator\">=</span> <span class=\"token string\">\"./in.txt\"</span>\nBREAK <span class=\"token operator\">=</span> <span class=\"token string\">\"0x5555555552ba\"</span>\n\n<span class=\"token keyword\">with</span> <span class=\"token builtin\">open</span><span class=\"token punctuation\">(</span>INPUT<span class=\"token punctuation\">,</span> <span class=\"token string\">\"w\"</span><span class=\"token punctuation\">)</span> <span class=\"token keyword\">as</span> f<span class=\"token punctuation\">:</span>\n    f<span class=\"token punctuation\">.</span>write<span class=\"token punctuation\">(</span><span class=\"token string\">\"A\"</span><span class=\"token operator\">*</span><span class=\"token number\">0x40</span><span class=\"token punctuation\">)</span>\n\ngdb<span class=\"token punctuation\">.</span>execute<span class=\"token punctuation\">(</span><span class=\"token string\">'file {}/{}'</span><span class=\"token punctuation\">.</span><span class=\"token builtin\">format</span><span class=\"token punctuation\">(</span>BINDIR<span class=\"token punctuation\">,</span> BIN<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\ngdb<span class=\"token punctuation\">.</span>execute<span class=\"token punctuation\">(</span><span class=\"token string\">'b *{}'</span><span class=\"token punctuation\">.</span><span class=\"token builtin\">format</span><span class=\"token punctuation\">(</span>BREAK<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\ngdb<span class=\"token punctuation\">.</span>execute<span class=\"token punctuation\">(</span><span class=\"token string\">'run &lt; {}'</span><span class=\"token punctuation\">.</span><span class=\"token builtin\">format</span><span class=\"token punctuation\">(</span>INPUT<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n\ngdb<span class=\"token punctuation\">.</span>execute<span class=\"token punctuation\">(</span><span class=\"token string\">'quit'</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p><code class=\"language-text\">gdb.execute()</code> is the function that executes gdb commands from a Python script.</p>\n<p>The basic usage is the same as operating gdb by command, but one slightly tricky point is that input values during execution must be predefined in a file.</p>\n<p>Since this program requires input from standard input, we create a file called <code class=\"language-text\">./in.txt</code> before execution and pre-write 0x40 bytes worth of string to it.</p>\n<p>Running this automates the process of executing the program in gdb, entering 0x40 bytes of string, stopping at the breakpoint <code class=\"language-text\">0x5555555552ba</code>, and then ending the debug session.</p>\n<p>The call is made not from Python but using the <code class=\"language-text\">gdb -x</code> command, as follows:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">gdb -x solver.py</code></pre></div>\n<p>Finally, let’s add the key retrieval process and obtain the Flag.</p>\n<h3 id=\"obtaining-the-flag\" style=\"position:relative;\"><a href=\"#obtaining-the-flag\" aria-label=\"obtaining the flag 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>Obtaining the Flag</h3>\n<p>From here it’s simple.</p>\n<p>We automated the work of using the continue command to retrieve keys one character at a time, which we previously did manually.</p>\n<p>This is the solver script.</p>\n<div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token comment\"># gdb -x solver.py</span>\n<span class=\"token keyword\">import</span> gdb\n\nBINDIR <span class=\"token operator\">=</span> <span class=\"token string\">\"~/Downloads\"</span>\nBIN <span class=\"token operator\">=</span> <span class=\"token string\">\"revvy_chevy\"</span>\nINPUT <span class=\"token operator\">=</span> <span class=\"token string\">\"./in.txt\"</span>\nBREAK <span class=\"token operator\">=</span> <span class=\"token string\">\"0x5555555552ba\"</span>\n\n<span class=\"token comment\"># Byte sequence retrieved from Ghidra</span>\ndata <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span> <span class=\"token number\">0x74</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x1a</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x95</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x4e</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0xba</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0xdb</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x47</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x64</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x09</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x2d</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0xd1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0xbf</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x8a</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x9d</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0xde</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x5a</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0xd7</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x5c</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x93</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x16</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x09</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x3b</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x30</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x6f</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x97</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x40</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0xd0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x7c</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x57</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0xdb</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0xde</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x0c</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x09</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0xa0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x84</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x9b</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x8a</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x76</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x2f</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0xb1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x57</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0xa2</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0xe1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x4f</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0xb9</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x6f</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x81</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0xbf</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0xb9</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0xbf</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0xe1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0xef</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x79</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0xcf</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x01</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0xdf</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0xf9</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x9f</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0xe1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x8f</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x39</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x2f</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x81</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0xff</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0x00</span> <span class=\"token punctuation\">]</span>\nkey <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span>\n\n<span class=\"token keyword\">with</span> <span class=\"token builtin\">open</span><span class=\"token punctuation\">(</span>INPUT<span class=\"token punctuation\">,</span> <span class=\"token string\">\"w\"</span><span class=\"token punctuation\">)</span> <span class=\"token keyword\">as</span> f<span class=\"token punctuation\">:</span>\n    f<span class=\"token punctuation\">.</span>write<span class=\"token punctuation\">(</span><span class=\"token string\">\"A\"</span><span class=\"token operator\">*</span><span class=\"token number\">0x40</span><span class=\"token punctuation\">)</span>\n\ngdb<span class=\"token punctuation\">.</span>execute<span class=\"token punctuation\">(</span><span class=\"token string\">'file {}/{}'</span><span class=\"token punctuation\">.</span><span class=\"token builtin\">format</span><span class=\"token punctuation\">(</span>BINDIR<span class=\"token punctuation\">,</span> BIN<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\ngdb<span class=\"token punctuation\">.</span>execute<span class=\"token punctuation\">(</span><span class=\"token string\">'b *{}'</span><span class=\"token punctuation\">.</span><span class=\"token builtin\">format</span><span class=\"token punctuation\">(</span>BREAK<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\ngdb<span class=\"token punctuation\">.</span>execute<span class=\"token punctuation\">(</span><span class=\"token string\">'run &lt; {}'</span><span class=\"token punctuation\">.</span><span class=\"token builtin\">format</span><span class=\"token punctuation\">(</span>INPUT<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token comment\"># Retrieve 0x40 characters' worth of keys and store in key</span>\n<span class=\"token keyword\">for</span> i <span class=\"token keyword\">in</span> <span class=\"token builtin\">range</span><span class=\"token punctuation\">(</span><span class=\"token number\">0x40</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n    <span class=\"token comment\"># gdb.execute('p $al')</span>\n    r <span class=\"token operator\">=</span> gdb<span class=\"token punctuation\">.</span>parse_and_eval<span class=\"token punctuation\">(</span><span class=\"token string\">\"$al\"</span><span class=\"token punctuation\">)</span>\n    key<span class=\"token punctuation\">.</span>append<span class=\"token punctuation\">(</span><span class=\"token builtin\">int</span><span class=\"token punctuation\">(</span>r<span class=\"token punctuation\">.</span>format_string<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span> <span class=\"token number\">16</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n    gdb<span class=\"token punctuation\">.</span>execute<span class=\"token punctuation\">(</span><span class=\"token string\">'continue'</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token comment\"># Decrypt the Flag using the retrieved keys</span>\nflag <span class=\"token operator\">=</span> <span class=\"token string\">\"\"</span>\n<span class=\"token keyword\">for</span> i <span class=\"token keyword\">in</span> <span class=\"token builtin\">range</span><span class=\"token punctuation\">(</span><span class=\"token number\">0x40</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n    flag <span class=\"token operator\">+=</span> <span class=\"token builtin\">chr</span><span class=\"token punctuation\">(</span>data<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span> <span class=\"token operator\">^</span> key<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">if</span> <span class=\"token builtin\">chr</span><span class=\"token punctuation\">(</span>data<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span> <span class=\"token operator\">^</span> key<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">==</span> <span class=\"token string\">\"}\"</span><span class=\"token punctuation\">:</span>\n        <span class=\"token keyword\">break</span>\n\n<span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>flag<span class=\"token punctuation\">)</span>\ngdb<span class=\"token punctuation\">.</span>execute<span class=\"token punctuation\">(</span><span class=\"token string\">'quit'</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p>Running this will ultimately retrieve the Flag string.</p>\n<h2 id=\"bonus-useful-gdb-techniques\" style=\"position:relative;\"><a href=\"#bonus-useful-gdb-techniques\" aria-label=\"bonus useful gdb techniques 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>Bonus: Useful gdb Techniques</h2>\n<p>Finally, let’s supplement some techniques that were not used in this particular problem.</p>\n<p>For the analysis, we’ll use a program compiled from the following source code.</p>\n<p>This is a program where the key-creation loop is only executed when <code class=\"language-text\">is_vulun</code> is 1.</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\">define</span> <span class=\"token macro-name\">TEXT</span> <span class=\"token string\">\"Enjoy debug!\\n\"</span></span>\n\n<span class=\"token keyword\">char</span> key<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span><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 punctuation\">)</span> <span class=\"token punctuation\">{</span>\n<span class=\"token function\">printf</span><span class=\"token punctuation\">(</span>TEXT<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">int</span> is_vulun <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>is_vulun <span class=\"token operator\">==</span> <span class=\"token number\">1</span><span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">int</span> i <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> i <span class=\"token operator\">&lt;</span> <span class=\"token number\">10</span><span class=\"token punctuation\">;</span> i<span class=\"token operator\">++</span><span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">{</span>\n        key<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">char</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span><span class=\"token number\">0x41</span><span class=\"token operator\">+</span>i<span class=\"token punctuation\">)</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\">\"Key %s\\n\"</span><span class=\"token punctuation\">,</span> key<span class=\"token punctuation\">)</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\">\"Finish!!\\n\"</span><span class=\"token punctuation\">)</span><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></code></pre></div>\n<p>First, save this source code as <code class=\"language-text\">easy.c</code> and create the executable with <code class=\"language-text\">gcc easy.c -o easy</code>.</p>\n<p>However, when we ran the compiled program, the key generation loop did not execute because <code class=\"language-text\">is_vulun = 0</code>.</p>\n<h3 id=\"bypassing-conditional-branches-by-modifying-eflags\" style=\"position:relative;\"><a href=\"#bypassing-conditional-branches-by-modifying-eflags\" aria-label=\"bypassing conditional branches by modifying eflags 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>Bypassing Conditional Branches by Modifying EFLAGS</h3>\n<p>First, let’s look at the line that performs conditional branching based on the value of <code class=\"language-text\">is_vuln</code>.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 500px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/eb435fcc24e3245f59f2812000ec8048/0b533/image-52.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 65.41666666666667%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAARlAAAEZQAGA43XUAAACb0lEQVQ4y02T63aTQBRG+ypqE+bOZRgGCEkgiZdq1br0l77/c2wPTa35MQtYHPbsM+fjzhvHMRccTu847jYsRvOjDfxeIue95VQpvvYjT4cDX4eBvd7x0O55HxxnrWUZjHM4a3FyvUtl4mev+Li84fN4z6NW/GkNfzrDl1JgTvEzB37tS350nkdV8eRKvsvGT0rxTZa+Bbqmp2wtedjSp4JOCs8xcWojndcMzhJTIPaeqi0JrqYONZUPVC8Qews05R4jNm7e4AclLwzzkDlPLaWx1FI4DA22HWm6ljq1aN9hQ/sMsvYFJsteDWfUruD+4Z5iX6AFeJxnDodMobTs6lnmkXJcSNNE3a6giA+NvLtC9I2lAI+oacvmyz1qBRrD6VDz4XKgbjyuspwvn6iydNKM+G4Su4T1V+Dadqct/r/hC/DxHWrZPhuelx1P3z7LucpHlTzPA6elp5/2uHyhyWLa1DJdTy3H0t8CbVoI0ZCzDCUqWilYmoaPfWQSi35raGKg7SpibChML7BMVZrn81sjU9y2rMMkhobNw1vUvBpq9ruay2mgTZW0ZwhjRk89bkiYUlJR1QR/BZYCWSVeDYOvUNlTLFqGY1G2ZOwrPkmwD0stLcqU8xqtTGh7Qn/C1VnOMAjw2u77QtMINKyG3kc2ueXtsWIroMJ1xOFIs3h8L1MOnjR0MoyMbyU+tYCacM2fvULWwZQv93dJjGKjxGIjFkrUJcxdx27siRLyNYtusJhR1k6j5ffU/fY1yCtoHUz6Z1iWEtboGdOWLOAosZklKg/ZcRkqcu1ITSJUCS+1vh7wEp9/hlFAkzKvln8BnLGaRLPV0XEAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/eb435fcc24e3245f59f2812000ec8048/8ac56/image-52.webp 240w,\n/static/eb435fcc24e3245f59f2812000ec8048/d3be9/image-52.webp 480w,\n/static/eb435fcc24e3245f59f2812000ec8048/b0a15/image-52.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/eb435fcc24e3245f59f2812000ec8048/8ff5a/image-52.png 240w,\n/static/eb435fcc24e3245f59f2812000ec8048/e85cb/image-52.png 480w,\n/static/eb435fcc24e3245f59f2812000ec8048/0b533/image-52.png 500w\"\n            sizes=\"(max-width: 500px) 100vw, 500px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/eb435fcc24e3245f59f2812000ec8048/0b533/image-52.png\"\n            alt=\"2021/12/image-52.png\"\n            title=\"2021/12/image-52.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>Here, <code class=\"language-text\">var_8h</code> is the local variable where <code class=\"language-text\">is_vulun</code> is stored.</p>\n<p>The <code class=\"language-text\">cmp</code> instruction compares it with 1 as a 32-bit unsigned integer (<code class=\"language-text\">dword</code>).</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">0x00001160      837df801       <span class=\"token function\">cmp</span> dword <span class=\"token punctuation\">[</span>var_8h<span class=\"token punctuation\">]</span>, <span class=\"token number\">1</span>\n0x00001164      <span class=\"token number\">7542</span>           jne 0x11a8</code></pre></div>\n<p>The <code class=\"language-text\">cmp</code> instruction commonly appears when comparing two values in a conditional branch, but its essence is simply subtraction.</p>\n<p>However, unlike the <code class=\"language-text\">sub</code> instruction which performs subtraction, the result is not stored in a register.</p>\n<p>Reference: <a href=\"https://stackoverflow.com/questions/45898438/understanding-cmp-instruction\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">assembly - Understanding cmp instruction - Stack Overflow</a></p>\n<p>The reason a simple subtraction <code class=\"language-text\">cmp</code> instruction is used for conditional branching is that the arithmetic operation updates the flag register.</p>\n<p>The flag register is a register used by the CPU to indicate results and state when performing arithmetic operations.</p>\n<p>In the x86_64 architecture, the lower 32 bits of the RFLAGS register are used.</p>\n<p>Reference: <a href=\"https://ja.wikibooks.org/wiki/X86%E3%82%A2%E3%82%BB%E3%83%B3%E3%83%96%E3%83%A9/x86%E3%82%A2%E3%83%BC%E3%82%AD%E3%83%86%E3%82%AF%E3%83%81%E3%83%A3\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">X86アセンブラ/x86アーキテクチャ - Wikibooks</a></p>\n<p>Each bit of the 32-bit flag register has a specific meaning, and values are updated based on the arithmetic result.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 699px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/f296a5af214f2dff11cda42feb2ca683/3fe45/image-53.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 53.333333333333336%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAAAsTAAALEwEAmpwYAAABs0lEQVQoz4VTiY6iUBD0/39sTEREjes46CiHyC2K4AFY09WG2clusktSvKPfq66uhsH1ekVd19+43W643+8Kxi6XC6qq+g1Z1zL253mmB88O8OO5lCXiOMHxeFTEcQzP8xS+7yuiKER4OMgYYR8ECGQehiG6rlNSJXw+n0qY5zkMw1CMRiPMZnPYto35fK5Yva8QCIn98SGwJT6DZVkao3olJFnbNlpiluWaLUkSFEWhCbgmCRVx/3Q64SxI0hTn81nPcY8KacGAnuz3e1UyHA5hmhNMp1M4joNCDrJ0EvdeNk2jl0ux58+H8UFdV6rA2e1gTYRMylgsFtjJmt4xlmWZqiEpKyEp171dvWVKyEy8tN1+Yjweqy80n8pYIsn6JhH/JWRW3/ew2WykVEtVLpdLufzyhqBPJCAej8e/CdmZXDLTM9M0sV6v4bou0jRDKcnOUkEPVkMiJqnr618ealP4IoHnuYiko6+snRjfyqTF88fYydfQijpwD6JMmtO2rYKN+lZI0uZxhxsXMOwQ6yDHLzeFsY5gbl5YOCmsbYyJzN9WB7y9H1CUFR7iKf8ugnZ8AdENQZIJLbYoAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/f296a5af214f2dff11cda42feb2ca683/8ac56/image-53.webp 240w,\n/static/f296a5af214f2dff11cda42feb2ca683/d3be9/image-53.webp 480w,\n/static/f296a5af214f2dff11cda42feb2ca683/32d5d/image-53.webp 699w\"\n              sizes=\"(max-width: 699px) 100vw, 699px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/f296a5af214f2dff11cda42feb2ca683/8ff5a/image-53.png 240w,\n/static/f296a5af214f2dff11cda42feb2ca683/e85cb/image-53.png 480w,\n/static/f296a5af214f2dff11cda42feb2ca683/3fe45/image-53.png 699w\"\n            sizes=\"(max-width: 699px) 100vw, 699px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/f296a5af214f2dff11cda42feb2ca683/3fe45/image-53.png\"\n            alt=\"2021/12/image-53.png\"\n            title=\"2021/12/image-53.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p> Image from <a href=\"https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-vol-3a-part-1-manual.pdf\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Intel Developer Manual</a></p>\n<p>The flags most frequently used for conditional branching are as follows:</p>\n<table>\n<thead>\n<tr>\n<th align=\"center\">FLAG</th>\n<th align=\"center\">Purpose</th>\n<th align=\"center\">Bit number</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td align=\"center\">CF (Carry Flag)</td>\n<td align=\"center\">Set when a carry occurs during addition that exceeds the register size</td>\n<td align=\"center\">0</td>\n</tr>\n<tr>\n<td align=\"center\">ZF (Zero Flag)</td>\n<td align=\"center\">Set when the result of an operation is zero (0)</td>\n<td align=\"center\">6</td>\n</tr>\n<tr>\n<td align=\"center\">SF (Sign Flag)</td>\n<td align=\"center\">Set when the result of an operation is negative</td>\n<td align=\"center\">7</td>\n</tr>\n<tr>\n<td align=\"center\">OF (Overflow Flag)</td>\n<td align=\"center\">Set when the result of a signed arithmetic operation is too large to fit in a register</td>\n<td align=\"center\">11</td>\n</tr>\n</tbody>\n</table>\n<p>When branching with a <code class=\"language-text\">cmp</code> instruction, the branch is decided based on whether the subtraction result is 0, or positive, or negative.</p>\n<p>The actual branching decision based on flag register values is made by several jump instructions.</p>\n<table>\n<thead>\n<tr>\n<th align=\"center\">Instruction</th>\n<th align=\"center\">Jump Condition</th>\n<th align=\"center\">Opcode</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td align=\"center\">JE</td>\n<td align=\"center\">Equal (ZF = 1)</td>\n<td align=\"center\">74</td>\n</tr>\n<tr>\n<td align=\"center\">JNE</td>\n<td align=\"center\">Not equal (ZF = 0)</td>\n<td align=\"center\">75</td>\n</tr>\n<tr>\n<td align=\"center\">JG</td>\n<td align=\"center\">Greater than (ZF = 0 &#x26; SF = OF)</td>\n<td align=\"center\">7F</td>\n</tr>\n<tr>\n<td align=\"center\">JGE</td>\n<td align=\"center\">Greater than or equal (SF = OF)</td>\n<td align=\"center\">７D</td>\n</tr>\n<tr>\n<td align=\"center\">JNG</td>\n<td align=\"center\">Not greater than (ZF = 1 | SF ! OF)</td>\n<td align=\"center\">７E</td>\n</tr>\n<tr>\n<td align=\"center\">JL</td>\n<td align=\"center\">Less than (SF ! OF)</td>\n<td align=\"center\">７C</td>\n</tr>\n</tbody>\n</table>\n<p>Reference: <a href=\"https://codezine.jp/article/detail/485\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">インラインアセンブラで学ぶアセンブリ言語 第3回 (1/3)：CodeZine（コードジン）</a></p>\n<p>Keeping the opcodes (right column) at hand is convenient when patching to forcibly alter conditional branches.</p>\n<p>Opcodes can change depending on the operand, but in general searching the IDM below is a good approach.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 500px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/321e7818549221e66d148ffce09673e7/0b533/image-54.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 60.83333333333334%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAARlAAAEZQAGA43XUAAAB/ElEQVQoz02TiW7iUAxF8//fhaZCpWwBAlnYIWtD2Hc8Pm4ZTSTLz+/Z1/a148ySUoJVKbP8IP66ktEiVynk/njK8/mU6/Uql8tF7ve7aezj8WhnvtfrZYIv4kTRWNK8kJsGVNut+H4gcZzI7XaT0+kkYRhIvV6XNE3l4+OPuG5PoiiU5XIph8NB9vu97HY7OZ/P5u/MZjPZbDZmfH8X0uv1/tlVVVngYDCQPM9Nr9dr6ff7MlBxXVf66o9dlhtL4EwmEymKwowsy8RVB2wyA7xarSQIAnsLw1CSJDFgfzSSkcp4PLY7OijLUhwuqASOKgXodDr2QMtwlcSxBQPueZ4Bf301rLJOu22gvIFB647v+1YRwTg3m02tJDIbx/l8bkF5nhlgrAlICmBXdbfb/YlXirY6A2c6nVpFAMDTT1BiFJAVzmgVQPimNU9bjvRuqL68AQRFBjiOIgt8PB6myc4DFFjLyg9JSPp/y/ihh8OhyVorZ5UcVoDKqAjN5LIMe28gi8XCgKwy1VSMD9vwnjC0MDwbylzb4GBro1x4nq7IL6dUGitApF3AE/oNzJnpI1PdFLYFzp1QLwCkRXStVrMqmDK8cO79rhI6SzNpNBrS1gk3Pj+l1WoZl9xThENG2gWAiiCZTO9frChyW27eaAtamCxn6ECYPDj4/AWnPnaKK/hvZgAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/321e7818549221e66d148ffce09673e7/8ac56/image-54.webp 240w,\n/static/321e7818549221e66d148ffce09673e7/d3be9/image-54.webp 480w,\n/static/321e7818549221e66d148ffce09673e7/b0a15/image-54.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/321e7818549221e66d148ffce09673e7/8ff5a/image-54.png 240w,\n/static/321e7818549221e66d148ffce09673e7/e85cb/image-54.png 480w,\n/static/321e7818549221e66d148ffce09673e7/0b533/image-54.png 500w\"\n            sizes=\"(max-width: 500px) 100vw, 500px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/321e7818549221e66d148ffce09673e7/0b533/image-54.png\"\n            alt=\"2021/12/image-54.png\"\n            title=\"2021/12/image-54.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>Reference: <a href=\"http://sparksandflames.com/files/x86InstructionChart.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Intel x86 Assembler Instruction Set Opcode Table</a></p>\n<p>Refer to the <code class=\"language-text\">Jcc—Jump if Condition Is Met</code> table.</p>\n<p>Now that we’ve organized the flag register and jump instructions, let’s return to the main topic.</p>\n<p>Let’s bypass the following conditional branch that checks whether the value of <code class=\"language-text\">is_vulun</code> is 1.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">0x00001160      837df801       <span class=\"token function\">cmp</span> dword <span class=\"token punctuation\">[</span>var_8h<span class=\"token punctuation\">]</span>, <span class=\"token number\">1</span>\n0x00001164      <span class=\"token number\">7542</span>           jne 0x11a8</code></pre></div>\n<p>Since <code class=\"language-text\">var_8h</code> always holds 0, after the <code class=\"language-text\">cmp</code> instruction at <code class=\"language-text\">0x00001160</code> is executed, the flag register will have the <code class=\"language-text\">[ CF PF AF SF IF ]</code> flags set.</p>\n<p>Don’t worry about each flag in detail for now; just focus on the fact that <code class=\"language-text\">ZF</code>, which needs to be set to prevent <code class=\"language-text\">jne</code> from skipping the processing, is not set.</p>\n<p>The result of running in gdb is as follows:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">$ b *0x555555555164\n$ p <span class=\"token variable\">$eflags</span>\n<span class=\"token variable\">$5</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span> CF PF AF SF IF <span class=\"token punctuation\">]</span></code></pre></div>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 500px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/e84b8d431db46c8aa3cd53e1cc2abce6/0b533/image-55.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 63.33333333333333%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAARlAAAEZQAGA43XUAAACLklEQVQ4y2WTWXbrIBBEvZ1nWxNikhCaB8uOk/2vpl6BE8dJPjgg1Fyqq5vD7XbB+/sNl8uKaRqwbTOkLCFUiVJLtHOPqneohwZ1W8NxTrIUp9MJ5/P5Ob6+D9frhvv9in3fMM8DlmVCWRJmJIQU6BYCR0+QR901cFyf0+Qb+AIOe4eg8OPjDVfOAXi5LFQoUTsDbRRu9w37bcZ+nbGsA9cLsk+Fv0dUuO8r7m87Viobhi7OpRBwtYVi6muwgRdZJeEqg6ayT0BCQJokcTwVaqVwpX83ghceHrsW6zxCEZAwsHFVXA99i76lj9bAGI2K4JxKM8YUaRrhEShEEX1blhHD2KHnwVCcshTRH+8blFQaAIJ7KSF5nqMsCmQEpUn6M2VtK1azgjQ1pLaQSkNZxypbGNdBcS8U6d8xHKL5IT2OLM2QElgQ/APYjzOaYUU77XDdiMoyHa1jGrIQsYVq+qnZQpb/8jyL6qNKXhRAafriYdP1bIcpQpt+RvC0cR5t27EwTfTP9wM849p+5L5H13laYGhPR0tctMi5+qGwHSb22ENhO20wBI4MDAUK63DQWhvT04atpBW/DYL3QWVR5LF4yWelD7Z2UKaCqT1U5VAwnWC4YHBOnwwPWxYkQIOKMKtgCVsr/Avr1148NAwqeYu2NbRrIB2fmW9jcEG/Qo8dWZDHOD7m06NAx+PpzxM8eAI9U5z2CzzTL4RElgu+htAaWXwVrwee49eT+5r/A22gqEdyTBJ9AAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/e84b8d431db46c8aa3cd53e1cc2abce6/8ac56/image-55.webp 240w,\n/static/e84b8d431db46c8aa3cd53e1cc2abce6/d3be9/image-55.webp 480w,\n/static/e84b8d431db46c8aa3cd53e1cc2abce6/b0a15/image-55.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/e84b8d431db46c8aa3cd53e1cc2abce6/8ff5a/image-55.png 240w,\n/static/e84b8d431db46c8aa3cd53e1cc2abce6/e85cb/image-55.png 480w,\n/static/e84b8d431db46c8aa3cd53e1cc2abce6/0b533/image-55.png 500w\"\n            sizes=\"(max-width: 500px) 100vw, 500px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/e84b8d431db46c8aa3cd53e1cc2abce6/0b533/image-55.png\"\n            alt=\"2021/12/image-55.png\"\n            title=\"2021/12/image-55.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>We’ve confirmed that ZF is indeed not set.</p>\n<p>To bypass the conditional branch here, we need to set ZF.</p>\n<p>In gdb, memory data can be tampered with using the <code class=\"language-text\">set</code> command.</p>\n<p>As we confirmed earlier, ZF corresponds to bit 6 of the flag register.</p>\n<p>In other words, we can set ZF by forcibly writing 1 to bit 6 of the flag register.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token comment\"># Set bit 6 of $eflags to 1 using OR operation</span>\n$ <span class=\"token builtin class-name\">set</span> <span class=\"token variable\">$eflags</span> <span class=\"token operator\">|</span><span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token number\">1</span> <span class=\"token operator\">&lt;&lt;</span> <span class=\"token number\">6</span><span class=\"token punctuation\">)</span>\n$ p <span class=\"token variable\">$eflags</span>\n<span class=\"token variable\">$7</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span> CF PF AF ZF SF IF <span class=\"token punctuation\">]</span></code></pre></div>\n<p>As shown above, executing <code class=\"language-text\">set $eflags |= (1 &lt;&lt; 6)</code> set the ZF flag.</p>\n<p>With this state, advancing with the <code class=\"language-text\">n</code> command allowed us to proceed to <code class=\"language-text\">0x555555555166</code>, which would not normally be executed.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 500px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/e96edda2a2e25f369363488ec7a6b81e/0b533/image-56.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 69.16666666666667%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAARlAAAEZQAGA43XUAAACVklEQVQ4y3VU2W7bMBD050S27oMUJeqWjxx2m6LpQ/vQAilQtP//Pp2lLTsN0IcFSXE5nJ1ZaqWVhsoLZHmOMIoRhNElQkRxgqwoEaeZW1/3gvC85ugz5Jzv+y5W1dSiqSskSYwwCJgUIGSyRBzHyLIMURS5WL6laeryk4QXZimBAmw2G/iMlSGgrWvkZJlnOYoih9YKxmiUJdmr4gIkoKEDk30BklzJEWYOUBiauUczHjAcTi5KU2Hoz5dopRARTNgIoLCTQ57nuXnoyg6uYDKuyrGFKWtUlWU0MJVB23aoa0uWNWxjcThsua5gbXVlnaaJYyxSLIBnhr1F3w+wBFFl5UpSpYHmBYqAYpaALDoKMxnX67UDesvOMTRDg2noyVIjpasxkwUkpbNJQvEJIIyW0t6CvI2bhoNFwzLFoUBCDKBmvgAwQdZ5njnAO2rnkZnkbtZLrN9pKG3TWgdSsNySbZKTaUmWmnPDMSHrIA5haZJ89zkPU5rCM1GW8NKbMSslYncNxmlE37YYug5d06CijrUxmMcZ0zhSzxLzMLj9gvOQl0dsnZjsI8pyBRzbBi/PR3z5dMLT/Q73uwm7sUfCWwu2ilE59mODrtJojULL0fJb4F1MeV/yN7bF74c9/jwe8HM74nUe8Z19eEoivOgcn02OX08zXrcdfmxbF1+txnTnnUGcKTejVi0dPn58wofnE6bdjP3DAfN+RkqtLLXVLPt4emTDl0ipqeIriVmu9z+XlS75hAo+J+3erczlCcY0ZulJxT33Q5CfgR+4MQyjf2IB/AuN6cOdwAKN3AAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/e96edda2a2e25f369363488ec7a6b81e/8ac56/image-56.webp 240w,\n/static/e96edda2a2e25f369363488ec7a6b81e/d3be9/image-56.webp 480w,\n/static/e96edda2a2e25f369363488ec7a6b81e/b0a15/image-56.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/e96edda2a2e25f369363488ec7a6b81e/8ff5a/image-56.png 240w,\n/static/e96edda2a2e25f369363488ec7a6b81e/e85cb/image-56.png 480w,\n/static/e96edda2a2e25f369363488ec7a6b81e/0b533/image-56.png 500w\"\n            sizes=\"(max-width: 500px) 100vw, 500px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/e96edda2a2e25f369363488ec7a6b81e/0b533/image-56.png\"\n            alt=\"2021/12/image-56.png\"\n            title=\"2021/12/image-56.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>Next, let’s try bypassing the conditional branch by reading a variable’s value from memory and then tampering with it, rather than by modifying the flag register.</p>\n<h3 id=\"extracting-information-from-memory\" style=\"position:relative;\"><a href=\"#extracting-information-from-memory\" aria-label=\"extracting information from memory 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>Extracting Information from Memory</h3>\n<p>Let’s look at the same process as before.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">0x00001160      837df801       <span class=\"token function\">cmp</span> dword <span class=\"token punctuation\">[</span>var_8h<span class=\"token punctuation\">]</span>, <span class=\"token number\">1</span>\n0x00001164      <span class=\"token number\">7542</span>           jne 0x11a8</code></pre></div>\n<p>This time, let’s set a breakpoint at <code class=\"language-text\">0x00001160</code>.</p>\n<p>Running the <code class=\"language-text\">run</code> command stops execution at the <code class=\"language-text\">cmp</code> instruction call point.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">$ b *0x555555555160\n$ run\n   0x555555555159 <span class=\"token operator\">&lt;</span>main+2<span class=\"token operator\"><span class=\"token file-descriptor important\">0</span>></span>:    mov    DWORD PTR <span class=\"token punctuation\">[</span>rbp-0x8<span class=\"token punctuation\">]</span>,0x0\n<span class=\"token operator\">=</span><span class=\"token operator\">></span> 0x555555555160 <span class=\"token operator\">&lt;</span>main+2<span class=\"token operator\"><span class=\"token file-descriptor important\">7</span>></span>:    <span class=\"token function\">cmp</span>    DWORD PTR <span class=\"token punctuation\">[</span>rbp-0x8<span class=\"token punctuation\">]</span>,0x1\n   0x555555555164 <span class=\"token operator\">&lt;</span>main+3<span class=\"token operator\"><span class=\"token file-descriptor important\">1</span>></span>:    jne    0x5555555551a8 <span class=\"token operator\">&lt;</span>main+9<span class=\"token operator\"><span class=\"token file-descriptor important\">9</span>></span>\n   0x555555555166 <span class=\"token operator\">&lt;</span>main+3<span class=\"token operator\"><span class=\"token file-descriptor important\">3</span>></span>:    mov    DWORD PTR <span class=\"token punctuation\">[</span>rbp-0x4<span class=\"token punctuation\">]</span>,0x0</code></pre></div>\n<p>Here, <code class=\"language-text\">DWORD PTR [rbp-0x8]</code> references the value of the local variable <code class=\"language-text\">is_vulun</code>.</p>\n<p>The syntax <code class=\"language-text\">DWORD PTR [memory address]</code> is an instruction to retrieve the memory address defined inside <code class=\"language-text\">[]</code> as a DWORD (32-bit unsigned integer).</p>\n<p><code class=\"language-text\">$rbp-0x8</code> is the address of the stack where the local variable is stored, but when we check it, it appears to indirectly reference a memory address that holds the actual variable value.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">p <span class=\"token variable\">$rbp</span>-0x8\n<span class=\"token variable\">$16</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span>void *<span class=\"token punctuation\">)</span> 0x7fffffffdce8</code></pre></div>\n<p>This means the actual value of <code class=\"language-text\">is_vulun</code> is stored inside <code class=\"language-text\">0x7fffffffdce8</code>.</p>\n<p>In gdb, you can view the contents of memory using the <code class=\"language-text\">x/[format] &lt;address></code> command.</p>\n<p>Reference: <a href=\"https://visualgdb.com/gdbreference/commands/x\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">GDB Command Reference - x command</a></p>\n<p>Looking at the above documentation, you can see that specifying the format as <code class=\"language-text\">x/w &lt;address></code> retrieves the memory contents as a 32-bit unsigned integer.</p>\n<p>Therefore, running the following command shows that the value at memory address <code class=\"language-text\">0x7fffffffdce8</code> (variable <code class=\"language-text\">is_vulun</code>) is 0.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">$ x/w 0x7fffffffdce8\n0x7fffffffdce8: <span class=\"token number\">0</span></code></pre></div>\n<p>Let’s return to the conditional branch processing.</p>\n<p>Here we can see that the value of <code class=\"language-text\">dword [var_8h]</code> is 0, and the <code class=\"language-text\">cmp</code> instruction is checking whether it equals 1.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">0x00001160      837df801       <span class=\"token function\">cmp</span> dword <span class=\"token punctuation\">[</span>var_8h<span class=\"token punctuation\">]</span>, <span class=\"token number\">1</span>\n0x00001164      <span class=\"token number\">7542</span>           jne 0x11a8</code></pre></div>\n<p>Therefore, it appears we can bypass the conditional branch by tampering with the value of <code class=\"language-text\">dword [var_8h]</code> to 1.</p>\n<p>Here, the <code class=\"language-text\">set</code> command can also be used to tamper with a value at a specific memory location.</p>\n<p>When changing the value at a specific address, append <code class=\"language-text\">{data type}</code> as shown in the link below.</p>\n<p>Reference: <a href=\"https://sourceware.org/gdb/current/onlinedocs/gdb/Assignment.html#Assignment\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Assignment (Debugging with GDB)</a></p>\n<p>We were able to tamper with the memory data as follows:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">$ x/w 0x7fffffffdce8\n0x7fffffffdce8: 0x00000000\n\n<span class=\"token comment\"># Tamper the value</span>\n$ <span class=\"token builtin class-name\">set</span> <span class=\"token punctuation\">{</span>int<span class=\"token punctuation\">}</span>0x7fffffffdce8 <span class=\"token operator\">=</span> <span class=\"token number\">1</span>\n\n$ x/w 0x7fffffffdce8\n0x7fffffffdce8: 0x00000001</code></pre></div>\n<p>Advancing execution in this state means the <code class=\"language-text\">cmp</code> instruction comparison results in <code class=\"language-text\">is_vuln == 1</code>, and the conditional branch bypass succeeds.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 500px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/3dcd34a87afb4e114a4b55b19d5dc490/0b533/image-57.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 55.00000000000001%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAARlAAAEZQAGA43XUAAAB+0lEQVQoz02S65KqMBCEfR1F5GIISSCESwCxvOzWvv+z9Hai7jk/umak8KNnpnfnRqE2Cr1zKGuJpjGYbIdcVbH34wTZtRCqxjaOWKmzkihkhVwKlOzzskSSJDgej9jVUuJ223C/X7GuM8ZpwDD2yE8ZyjyHrAR8Z6EJUFSjaUAIJAcCCImVoKAkIbBtDZ7PG35+vnC9rvB+xLJ4ZEWOSglII7E8PPzi0M8dBtbG6T9H6Sn966NDxVG+v+8EPrFtCy6XOdaCwM4aKI7+9bxi8QNsqzGNHYyWOBwOOJ1OMFzXBxjqThvDMUf4eYbtOrjeUT1ExX1yl6bt6NojzwtCOOIx5Z9TpOlHJ4Kzfw7D7ho3oh1WaoEylsdoUFU1hJAE16i4OyHOhGZUHkGhBlBKl+FDwXF0eLtdYAcPO17g/AZjHXRdUwqtNqjpNEwx8LrjNEX3ztmYgKDQhzv0fRfXRIfbG7iiX25ougGN0ViXhZrpTBCo4wryokSteGXGS/PaARCcn88ldUaWZdiFmNS6YRYtVON4WfN6kTurKMGMWdtGQJBzHYE1JKFFUUDxWZbl2O/3r5EHP6PlmIpQYwln0LXV0VXGPYVsHULe3kuPPZ/F32n6is67xqO0hDStw7RsmHjNx+OKK6MTY8BFf148Hl9Bjn3yDvX/9R2bX1c8ajyUzL/UAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/3dcd34a87afb4e114a4b55b19d5dc490/8ac56/image-57.webp 240w,\n/static/3dcd34a87afb4e114a4b55b19d5dc490/d3be9/image-57.webp 480w,\n/static/3dcd34a87afb4e114a4b55b19d5dc490/b0a15/image-57.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/3dcd34a87afb4e114a4b55b19d5dc490/8ff5a/image-57.png 240w,\n/static/3dcd34a87afb4e114a4b55b19d5dc490/e85cb/image-57.png 480w,\n/static/3dcd34a87afb4e114a4b55b19d5dc490/0b533/image-57.png 500w\"\n            sizes=\"(max-width: 500px) 100vw, 500px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/3dcd34a87afb4e114a4b55b19d5dc490/0b533/image-57.png\"\n            alt=\"2021/12/image-57.png\"\n            title=\"2021/12/image-57.png\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>We have now been able to reference and tamper with memory information using gdb.</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>In this article, I summarized basic ELF binary analysis techniques for CTF beginners.</p>\n<p>This article was created for a workshop I personally host, so if you wish to reuse it in a workshop or similar setting, no special permission is required.</p>\n<p>Just include the URL as a reference, and feel free to use it as you like.</p>\n<p>If you have any questions or points to raise about this article or other content, please DM me on <a href=\"https://twitter.com/yuki_kashiwaba\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Twitter: yuki_kashiwaba</a>.</p>\n<p>Comments on this article are also welcome, but Twitter DMs get a faster response.</p>\n<p>I hope this article is helpful for those who are starting out with CTF.</p>\n<h2 id=\"recommended-books--websites\" style=\"position:relative;\"><a href=\"#recommended-books--websites\" aria-label=\"recommended books  websites 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 / Websites</h2>\n<p>Since this article only covers introductory ELF analysis topics, I’ll list the following books and websites for those who want to learn in more depth.</p>\n<h3 id=\"books\" style=\"position:relative;\"><a href=\"#books\" aria-label=\"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>Books</h3>\n<ul>\n<li><a href=\"https://amzn.to/3lkunSs\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Debug Hacks -デバッグを極めるテクニック&#x26;ツール</a>\nWhile it is a debugging book for developers, the first ~100 pages cover the basics of gdb usage very well and are highly informative.</li>\n<li><a href=\"https://amzn.to/3o7g5Gj\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">詳解セキュリティコンテスト</a>\nIf you’re getting started with CTF, this is a book worth reading first.\nIt’s a quite readable summary of analysis techniques and how to read assembly.\nNote that the Reversing section has a number of typos, so be sure to check the errata.</li>\n<li><a href=\"https://amzn.to/3robIsL\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">リバースエンジニアリングツールGhidra実践ガイド ~セキュリティコンテスト入門からマルウェア解析まで~ </a>\nProbably the only Ghidra book written in Japanese.\nIt is heavy on PE binary analysis content, but is very educational not only on how to use Ghidra but also on analysis techniques.\nNote that it was written for Ghidra prior to 10.0, so there is no coverage of the debugger.</li>\n<li><a href=\"https://amzn.to/3robIsL\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">リバースエンジニアリング ―Pythonによるバイナリ解析技法</a>\nThis book is entirely about PE binary analysis, but the analysis techniques have much in common with ELF.</li>\n<li><a href=\"https://booth.pm/ja/items/1058583\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">冴えないIDAの育てかた</a>\nCovers an overview and usage of IDA.\nFor some reason, despite being an IDA book, about a third of the pages are devoted to explaining radare2.\nI’ve never encountered a book with this much radare2 information in Japanese, so it is very helpful.\nos.environ[‘PYGAME<em>HIDE</em>SUPPORT_PROMPT’] = ‘Hide      Linuxカーネルの教科書](<a href=\"https://amzn.to/3oMmsPY\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">https://amzn.to/3oMmsPY</a>)’\nAnalyzing ELF binaries requires some understanding of how Linux works.\nPersonally, I think this is the most beginner-friendly book on the topic.</li>\n<li><a href=\"https://amzn.to/3yhiyBA\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">ptrace入門: ptraceの使い方</a>\nWe didn’t use it in this article, but it is an explanatory book on ptrace and ltrace.\nIt appears to be a book of lecture materials used by a University of Tsukuba professor, and is sold for just 100 yen — there is no reason not to buy it.</li>\n</ul>\n<h3 id=\"websites\" style=\"position:relative;\"><a href=\"#websites\" aria-label=\"websites 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>Websites</h3>\n<ul>\n<li><a href=\"https://cs.lmu.edu/~ray/notes/nasmtutorial/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">NASM Tutorial</a>\nIn English, but there is a lot of useful information as a first step toward being able to read Intel-syntax assembly.</li>\n<li>\n<p><a href=\"http://asmdebugger.com/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Assembly Debugger Online</a>\nYou can easily verify the behavior of Intel-syntax assembly from the web without running gdb locally.</p>\n<p>It is useful for checking whether your understanding of a certain behavior is correct.</p>\n</li>\n<li><a href=\"https://linuxjm.osdn.jp/index.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">JM Project (Japanese)</a>\nWhen doing ELF analysis you’ll often need to check man pages for library functions; this is a site with man pages and similar documentation translated into Japanese.</li>\n<li><a href=\"https://book.rada.re/first_steps/intro.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">The Official Radare2 Book</a>\nradare2 is feature-rich, isn’t it.\nI can’t fully master it yet…</li>\n</ul>\n<p> Reference information will be added someday when I feel like it. There is too much to write it all.</p>","fields":{"slug":"/ctf-elf-training-en","tagSlugs":["/tag/ctf-en/","/tag/elf-en/","/tag/reversing-en/","/tag/training-en/","/tag/english/"]},"frontmatter":{"date":"2021-12-12","description":"","tags":["CTF (en)","ELF (en)","Reversing (en)","Training (en)","English"],"title":"[CTF Beginner's Guide] Introduction to ELF Binary Reverse Engineering","socialImage":{"publicURL":"/static/334bd91f01f5d703919e643c6130dca6/ctf-elf-training.png"}}}},"pageContext":{"slug":"/ctf-elf-training-en"}},"staticQueryHashes":["251939775","401334301","825871152"]}