{"componentChunkName":"component---src-templates-post-template-js","path":"/ctf-angr-bigginer-en","result":{"data":{"markdownRemark":{"id":"2ea9c968-048b-5321-8ef8-37a9d57e93f1","html":"<blockquote>\n<p>This page has been machine-translated from the <a href=\"/ctf-angr-bigginer\">original page</a>.</p>\n</blockquote>\n<p>I participated in <a href=\"https://wanictf.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">WaniCTF’21-spring</a> the other day.</p>\n<p>Since I competed solo this time, I also solved some problems outside Reversing for the first time in a while.</p>\n<p>In the end, I only cleared Rev and Misc, and finished in 56th place.\nIt looks like I still have a long way to go before I outgrow the beginner stage.</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/a51e130b45a9d9e17daa790344b16069/0b533/wani.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/Ca1DAAAACXBIWXMAARlAAAEZQAGA43XUAAACZklEQVQoz2WTS2sTURTHs3Rdmnkkdx73zmQmj8mrNu9UA7ZNGw1pI40LC+JCcCMSKYjgQnHlphvXIlItitWPoFjqY2NrXVRFN+76ERSEv2dm0mpx8Z87995zfucxZyK6pkPXj4oxBsdxkc1m4XkeUqkUcrkcMpkMTNP8zz7QiBPxHwZJ0zQ6COW/W5YF13WRSCRg2zYFcALpI9t/dQSo0oHMDAgu6NIiOA8NmRZkerD6xkKIIJAQoWzLJj/+F+oDa4KjP5FEIuli0EmjkON0YR5mYgXOItxT4GzWwvqzNiaKlIDloHL/EazGFJgiQzcMRDKlMi5cHyCfVlE910K+2wf3sxyV7veMj7JQFYZC0cL3H0uoljlU4aK1/RWJdgcsGg2BjBtQ03WU+qu4e2qA05N5aoF22NcAaHKYtI9KcXg5gU9fzqJSEogR8MSbHdjTM4iPjxHQRERTJJTvPcD02m/sXruMvdU6Ug5HnKA6Af1yhSVwbFzB1cU8bl+axPvdRdQrBLQI+HYHznwPepyHGRqGjurWLha+fcT2uwH2P/dxsulCpvL8O0ZG1aSDm/kSnq408PzOFDa3F1A7zqEYSTRfvUZlZR3l5YdgcZkylGSkh1fweH+In7+WsLbRDvrnj4FftkrAeS+DvdkzeDJsYP1WE1sfephZSMFpTaD+chPVGy9QvbgxApIjUyV0u0mcXy5Qz3TEYgezRR+GVi9ho5fJYq6WwmwzSbYeSu00ip0i3E4Xbn0OTnGWes7CwdZ1A9Eow9hY7HCmtIM/iPaMJJEUCiSrDJLEEJNpRhUKKEvQYnIg3/4PL9JjpGnevnsAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/a51e130b45a9d9e17daa790344b16069/8ac56/wani.webp 240w,\n/static/a51e130b45a9d9e17daa790344b16069/d3be9/wani.webp 480w,\n/static/a51e130b45a9d9e17daa790344b16069/b0a15/wani.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/a51e130b45a9d9e17daa790344b16069/8ff5a/wani.png 240w,\n/static/a51e130b45a9d9e17daa790344b16069/e85cb/wani.png 480w,\n/static/a51e130b45a9d9e17daa790344b16069/0b533/wani.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/a51e130b45a9d9e17daa790344b16069/0b533/wani.png\"\n            alt=\"image-20210505010349068\"\n            title=\"image-20210505010349068\"\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>It was an extremely worthwhile opportunity, partly because it highlighted many new issues I need to work on, such as how much I struggled with Pwn beyond what I expected.</p>\n<p>This time, through the Very Hard Reversing challenge “licence,” I was able to learn how to use angr, a tool that analyzes complex processing with symbolic execution, so <strong>I would like to look back on that experience and summarize how to use angr.</strong></p>\n<h2 id=\"what-i-learned\" style=\"position:relative;\"><a href=\"#what-i-learned\" aria-label=\"what i learned permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What I Learned</h2>\n<ol>\n<li>How to use angr</li>\n<li>How angr works</li>\n</ol>\n<h2 id=\"solving-licence\" style=\"position:relative;\"><a href=\"#solving-licence\" aria-label=\"solving licence 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>Solving licence</h2>\n<p>The challenge I solved this time was “licence,” and it looked like this.</p>\n<p>Since only 36 people solved it, it may have been a fairly difficult problem.</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/2aa2919985cfc2d56e305f3b337bef9d/0b533/wani2png.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: 82.5%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAARCAYAAADdRIy+AAAACXBIWXMAARlAAAEZQAGA43XUAAACVElEQVQ4y41USY7bMBD0LyxZm7VYsvbdhjUeRJZyyCUGMpc8Iv9JkHMeWukmRduDyTKHQlNks9hVTWoVRRF834djO7Btm7DErQs7LGARnO0WSZIIuDz/mLfAcRyYpomVrunQNA2bzQaWZcGiRRHdgMhKmGEFm8a+54mD7WXdtpY8HtMck6VpitU8TxinCcP5LE5RCbKCOyzawJuY6LEyVZ2u65jnGaumrtHs9yjDEM6ySZwsovx+hKrKMq0buSIcx5EImxonw8BAkmOS5ZIsj6PrYkve8TgIAiGXwfMMz/Vkhdad8HK5YFWWJQoizaoKh+MRR0Lf96ip8jzPxZgTh2HA6XRC07YoigJciPLzFeGe5GZZiiSORRfZ2JjmoogREWFHmxtwXlHk4pCQ7DFI1Z8rFEmZSGRkWYbD4YC2a1ETEVfG37vd7mYDE8jqXnsoCKM4g0K4T2Wk6pRnyr/bDfhHl2WF/TOa4wd0pxHdcEH/NMOhe6fuJkvjqDqsus3zCvzN+YKwaQ+o2x5pXiLJSsRJjoR8ZOnsJ0tl/7hSBfaQ17k5bFNKPeAqxbU59hWKLKJG+NgFNqLQE4kVdZ0hN+eIqWncpJw8T9NMNKrrZMP4RvBLEoRJdUVSfUbWXJG3Lwj2Z5KowdjcpSpZj1J5TcE0TKzXayl5vH7H9OUnPr78wKevv9A8faMEjSRs35j/N3Curm8kYVWVNz+MpSJ1JeQz+390bAu6tlToL0/N9z1hrIC9wHlnDGLozg7TRD8H07w/dnnq/bf0PpDsqIYWVHgeL/gNml/o8Z+Nc+0AAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/2aa2919985cfc2d56e305f3b337bef9d/8ac56/wani2png.webp 240w,\n/static/2aa2919985cfc2d56e305f3b337bef9d/d3be9/wani2png.webp 480w,\n/static/2aa2919985cfc2d56e305f3b337bef9d/b0a15/wani2png.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/2aa2919985cfc2d56e305f3b337bef9d/8ff5a/wani2png.png 240w,\n/static/2aa2919985cfc2d56e305f3b337bef9d/e85cb/wani2png.png 480w,\n/static/2aa2919985cfc2d56e305f3b337bef9d/0b533/wani2png.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/2aa2919985cfc2d56e305f3b337bef9d/0b533/wani2png.png\"\n            alt=\"image-20210505011116132\"\n            title=\"image-20210505011116132\"\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 rough solution was as follows.</p>\n<ol>\n<li><strong>Decompile the binary and inspect the key file format and the behavior of the <code class=\"language-text\">check</code> function that validates the flag</strong></li>\n<li><strong>Identify the address where execution ultimately lands when the correct key is provided</strong></li>\n<li><strong>Use angr to inspect the data that appears when the <code class=\"language-text\">check</code> function’s validation is bypassed</strong></li>\n</ol>\n<p>Before diving into the solution, I will first introduce angr, which is the main theme of this article.</p>\n<h2 id=\"what-is-angr\" style=\"position:relative;\"><a href=\"#what-is-angr\" aria-label=\"what is angr 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 angr?</h2>\n<p><a href=\"https://angr.io/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">angr</a> is a Python framework for analyzing binaries.</p>\n<p>It is an open-source analysis tool that uses symbolic execution (a technique for exhaustively extracting the execution paths a program can take), and it was developed by the University of California’s SEFCOM lab and the Shellphish CTF team.</p>\n<p>Its source code is published on <a href=\"https://github.com/angr/angr\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">GitHub</a>.</p>\n<p>angr seems to have many functions, but this time I will use its symbolic execution features to recover the flag.</p>\n<h2 id=\"trying-out-angr\" style=\"position:relative;\"><a href=\"#trying-out-angr\" aria-label=\"trying out angr 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>Trying Out angr</h2>\n<p>What I struggled with most when using angr for the first time was simply how high the barrier to entry felt.</p>\n<p>Part of that was probably because I had almost no background in this area, but it still took me a very long time to understand which part of angr I needed in order to recover the flag.</p>\n<p>In any case, let’s first prepare an environment where angr can run.</p>\n<p>angr does not support being used outside a virtual environment.\nIn fact, even on the ParrotOS environment I use, it conflicts with other Python packages, so I think using a virtual environment is the best option.</p>\n<p>By the way, Docker containers are also provided.</p>\n<p>The officially recommended method is to use <code class=\"language-text\">mkvirtualenv</code>, but this time I used <code class=\"language-text\">pipenv</code>.\nIf you run the following commands, you will be able to use angr.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">pipenv <span class=\"token function\">install</span>\npipenv shell\npip <span class=\"token function\">install</span> angr</code></pre></div>\n<p>To make sure it works, let’s use angr to print some basic information about the binary file.\nI mostly ran it interactively.</p>\n<div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token operator\">>></span><span class=\"token operator\">></span> <span class=\"token keyword\">import</span> angr\n<span class=\"token operator\">>></span><span class=\"token operator\">></span> <span class=\"token keyword\">import</span> monkeyhex\n\n<span class=\"token operator\">>></span><span class=\"token operator\">></span> proj <span class=\"token operator\">=</span> angr<span class=\"token punctuation\">.</span>Project<span class=\"token punctuation\">(</span><span class=\"token string\">\"licence.exe\"</span><span class=\"token punctuation\">,</span> auto_load_libs<span class=\"token operator\">=</span><span class=\"token boolean\">False</span><span class=\"token punctuation\">)</span>\nWARNING <span class=\"token operator\">|</span> <span class=\"token number\">2021</span><span class=\"token operator\">-</span><span class=\"token number\">05</span><span class=\"token operator\">-</span><span class=\"token number\">05</span> <span class=\"token number\">02</span><span class=\"token punctuation\">:</span><span class=\"token number\">02</span><span class=\"token punctuation\">:</span><span class=\"token number\">01</span><span class=\"token punctuation\">,</span><span class=\"token number\">328</span> <span class=\"token operator\">|</span> cle<span class=\"token punctuation\">.</span>loader <span class=\"token operator\">|</span> The main binary <span class=\"token keyword\">is</span> a position<span class=\"token operator\">-</span>independent executable<span class=\"token punctuation\">.</span> It <span class=\"token keyword\">is</span> being loaded <span class=\"token keyword\">with</span> a base address of <span class=\"token number\">0x400000</span><span class=\"token punctuation\">.</span>\n\n<span class=\"token operator\">>></span><span class=\"token operator\">></span> <span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"ARCH\"</span><span class=\"token punctuation\">,</span> proj<span class=\"token punctuation\">.</span>arch<span class=\"token punctuation\">)</span>\nARCH <span class=\"token operator\">&lt;</span>Arch AMD64 <span class=\"token punctuation\">(</span>LE<span class=\"token punctuation\">)</span><span class=\"token operator\">></span>\n<span class=\"token operator\">>></span><span class=\"token operator\">></span> <span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"EntryPoint\"</span><span class=\"token punctuation\">,</span> proj<span class=\"token punctuation\">.</span>entry<span class=\"token punctuation\">)</span>\nEntryPoint <span class=\"token number\">4198656</span>\n<span class=\"token operator\">>></span><span class=\"token operator\">></span> <span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"FileName\"</span><span class=\"token punctuation\">,</span> proj<span class=\"token punctuation\">.</span>filename<span class=\"token punctuation\">)</span>\nFileName licence<span class=\"token punctuation\">.</span>exe</code></pre></div>\n<h2 id=\"symbolic-analysis-with-angr\" style=\"position:relative;\"><a href=\"#symbolic-analysis-with-angr\" aria-label=\"symbolic analysis with angr 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>Symbolic Analysis with angr</h2>\n<p>If you want to learn how to use angr, I recommend reading through the <a href=\"https://docs.angr.io/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">angr Documentation</a> once from start to finish.</p>\n<p>I skipped that step, and as a result I ended up wasting extra time.\nThe sections that were especially helpful for me this time were the following.</p>\n<ul>\n<li><a href=\"https://docs.angr.io/core-concepts/toplevel\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Top Level Interfaces - angr Documentation</a></li>\n<li><a href=\"https://docs.angr.io/core-concepts/loading\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Loading a Binary - angr Documentation</a></li>\n<li><a href=\"https://docs.angr.io/core-concepts/solver\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Solver Engine - angr Documentation</a></li>\n<li><a href=\"https://docs.angr.io/core-concepts/pathgroups\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Simulation Managers - angr Documentation</a></li>\n<li><a href=\"https://docs.angr.io/examples\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Examples - angr Documentation</a></li>\n</ul>\n<p>angr is not a binary emulator; it is a tool that analyzes programs through symbolic execution.</p>\n<p>Not having any real intuition for what symbolic execution actually was turned out to be the biggest hurdle to recovering the flag with angr.</p>\n<p>The following may not be perfectly accurate, but this is what I came to understand about symbolic execution.</p>\n<ul>\n<li><strong>It executes programs using symbolic variables</strong></li>\n<li><strong>A symbolic variable refers not to a concrete value such as 1, 2, or 3, but to a “symbol” (a name?) attached to variables and arguments used inside the program</strong></li>\n<li><strong>Arithmetic on symbolic variables produces an abstract syntax tree (AST)</strong></li>\n<li><strong>When the program evaluates branches involving symbols, it records and executes both the true and false cases as separate possibilities</strong></li>\n<li><strong>An SMT solver (used to solve constraint satisfaction problems quickly) determines whether each recorded case is actually possible</strong></li>\n<li><strong>In other words, by using ASTs, you can ask questions such as, “Given the output of this sequence of operations, what must the input have been?”</strong></li>\n<li><strong>angr is a tool for answering that question</strong></li>\n<li><strong>The only data types that can be used symbolically are numbers and strings, so it cannot handle variable-length data structures such as arrays</strong></li>\n</ul>\n<p>That still leaves the question of what this all actually means, of course, but the rough idea is that by using angr, you can identify what value the key must have in order to reach the output produced when the correct key is entered.</p>\n<p>Now let’s look at the basic way to use angr.</p>\n<h3 id=\"cle-loader\" style=\"position:relative;\"><a href=\"#cle-loader\" aria-label=\"cle loader 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>CLE loader</h3>\n<p>First, angr loads a binary file as a Project.\nThis creates <code class=\"language-text\">&lt;class 'angr.project.Project'></code>, which becomes the starting point for all subsequent processing.</p>\n<div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token keyword\">import</span> angr\nproj <span class=\"token operator\">=</span> angr<span class=\"token punctuation\">.</span>Project<span class=\"token punctuation\">(</span><span class=\"token string\">'licence.exe'</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p>The next important concept is the loader.</p>\n<p>The loader refers to the CLE module used to lay out the binary file in a virtual address space (I think), and you can use it through the <code class=\"language-text\">.loader</code> property.</p>\n<p>When you actually use it on the challenge binary, you get the following output.</p>\n<div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token operator\">>></span><span class=\"token operator\">></span> proj<span class=\"token punctuation\">.</span>loader\n<span class=\"token operator\">&lt;</span>Loaded licence<span class=\"token punctuation\">.</span>exe<span class=\"token punctuation\">,</span> maps <span class=\"token punctuation\">[</span><span class=\"token number\">0x400000</span><span class=\"token punctuation\">:</span><span class=\"token number\">0x807fff</span><span class=\"token punctuation\">]</span><span class=\"token operator\">></span>\n<span class=\"token operator\">>></span><span class=\"token operator\">></span> proj<span class=\"token punctuation\">.</span>loader<span class=\"token punctuation\">.</span>min_addr\n<span class=\"token number\">0x400000</span>\n<span class=\"token operator\">>></span><span class=\"token operator\">></span> proj<span class=\"token punctuation\">.</span>loader<span class=\"token punctuation\">.</span>max_addr\n<span class=\"token number\">0x807fff</span>\n<span class=\"token operator\">>></span><span class=\"token operator\">></span> proj<span class=\"token punctuation\">.</span>loader<span class=\"token punctuation\">.</span>main_object \n<span class=\"token operator\">&lt;</span>ELF Object licence<span class=\"token punctuation\">.</span>exe<span class=\"token punctuation\">,</span> maps <span class=\"token punctuation\">[</span><span class=\"token number\">0x400000</span><span class=\"token punctuation\">:</span><span class=\"token number\">0x408017</span><span class=\"token punctuation\">]</span><span class=\"token operator\">></span></code></pre></div>\n<p>Here, the loader (the CLE module) is responsible for loading the binary file in a form suitable for later processing by angr.</p>\n<p>More concretely, it is the module that obtains the executable program and libraries loaded as a Project and creates the address space into which the program is loaded so it can run.</p>\n<p>The binaries loaded by the loader are read by <code class=\"language-text\">cle.Backend</code> class modules corresponding to their format (ELF and so on) and expanded into a single memory space.</p>\n<p>The following documentation is helpful for the options available in the CLE module, though it seems extremely feature-rich and I have not managed to read all of it.</p>\n<ul>\n<li><a href=\"http://angr.io/api-doc/cle.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">cle — Binary Loader — angr 9.0.6885 documentation</a></li>\n</ul>\n<h1 id=\"simulation-managers\" style=\"position:relative;\"><a href=\"#simulation-managers\" aria-label=\"simulation managers 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>Simulation Managers</h1>\n<p>This time, in order to recover the flag, I wanted to determine what value the key must have in order to reach the output produced when the correct key is entered.</p>\n<p>For that, I use Simulation Managers.</p>\n<p>I would like to write a proper explanation of what Simulation Managers are, but unfortunately my understanding does not go much beyond “some amazing thing that can identify input values by symbolic execution,” so I will skip that part.</p>\n<p>For now, let’s use Simulation Managers with the Project we created from the challenge binary.</p>\n<div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token operator\">>></span><span class=\"token operator\">></span> init_state <span class=\"token operator\">=</span> proj<span class=\"token punctuation\">.</span>factory<span class=\"token punctuation\">.</span>entry_state<span class=\"token punctuation\">(</span>args <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token string\">'licence.exe'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'key.dat'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span>\n<span class=\"token operator\">>></span><span class=\"token operator\">></span> simgr <span class=\"token operator\">=</span> proj<span class=\"token punctuation\">.</span>factory<span class=\"token punctuation\">.</span>simgr<span class=\"token punctuation\">(</span>init_state<span class=\"token punctuation\">)</span>\n<span class=\"token operator\">>></span><span class=\"token operator\">></span> simgr<span class=\"token punctuation\">.</span>active\n<span class=\"token punctuation\">[</span><span class=\"token operator\">&lt;</span>SimState @ <span class=\"token number\">0x401100</span><span class=\"token operator\">></span><span class=\"token punctuation\">]</span></code></pre></div>\n<p><code class=\"language-text\">.factory.entry_state</code> is a state constructor that performs the initialization for symbolic execution.</p>\n<p>There are several variants, but <code class=\"language-text\">entry_state</code> seems to construct a state in which execution can begin from the binary’s entry point.</p>\n<p>If you want to perform symbolic execution while supplying runtime arguments, define those arguments here.</p>\n<p>At this point, you need to remember that the first argument must include the invocation of the binary itself.\nThe idea is that you provide the same arguments the program would receive when launched from the console with command-line arguments.</p>\n<p>With that, the symbolic-execution setup for recovering the flag is complete.</p>\n<p>Now it is finally time to get the flag.</p>\n<h2 id=\"about-the-challenge-binary\" style=\"position:relative;\"><a href=\"#about-the-challenge-binary\" aria-label=\"about the challenge 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>About the Challenge Binary</h2>\n<p>To recover the flag, I analyzed the challenge binary.</p>\n<p>There are a lot of branches inside the <code class=\"language-text\">main</code> function, and most of them lead to the <code class=\"language-text\">fail</code> 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: 492px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/3edb5e3c5ce987920fa3022f84b3f266/5c6e9/wani3.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: 122.08333333333334%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAYCAYAAAD6S912AAAACXBIWXMAARlAAAEZQAGA43XUAAADIElEQVQ4y21Va2/aQBD0/5fa7/2Sz41UtaqC2jQqSpQHCUkgqSIIEEqoEmxjwE/AgH07Xb99wIFl+x5ze7MzawWlRkT5xW8QAmi3fby8hPl4cSfsa4oMljwLkSxwHKDRXOH11eD3dTxeBs2eJcDtCQkYYNsEXRfptBBB8M5jyxy0HECMQaUIk4FiN8uKwEiKPLoHgco9bpkkbAepZHwEQQQk0O2GaLXEDljWNL0Lw3D4Aq+hFLQUYQa42QBXNRcnJ1PMZkiTQnkUGWijscLp6RhnZzaWS+wHzI7s8ml0HRKIzFeSeZVPXoDJJ1DKcvB9ipNR7su4KviimOMw3Jq3L0LTBEukkI6sgHQV//v9ZPO9EZaTclNf4PjYgjGhHaFni4bDEEeVGe6Zy316TCJMd1bNOXrjCYJVQTZJiyhOWLttw7JNOfoUR4kfRAK4whI2TMle2e5Zxg0j4i8WEPeJOEllapQy9T486BglACQkj0dtsQDTkfV7fL2XBE7pkdcEv8Z+bQF3zzPcPutQB9jhZzgMUK266PWSQjEaBXj808PT0xyaVhhBActkUfHRvg7x3FrgQ+UMP/udXAoZYLe3xsWFBVVLAFU1RK22xM2Nxe4R+48cIXx+/47q5irm0iJL0thkUq5GbFWba9BapiVPShhvssFo8RefjG+coDlMMqGSyoUrWWUYIndL1Ez2vu8LKYlKmadQMOvk4ED/gdugEfdtaMP5VKF5Hh6brIQ15c7pdAhvb7IFJesFYs4vHm6cDg6tn5iBS0oaTb3hsejH6KXVOzr+8S+Di8mEoywyLQlbRBGyHDxa40A7woB/GY+rFcHzRFqyEu4Gg4D7NFmH5QgFF8+VWgfma1Ssa1x5D5hgjDkttvQW+V7EuuTUsNCd4siUOSWKotqE+fEL69XAKxk41Ko8fQqNdIQUItP6iq2ZcZeYIKoowfY3RYCGU1Cfzer6/BUJ8VW/QHvdZxJc6CIulFwvQxa4hXo9Ji729t39PzSbWlztld3al+cB9/M2fk8bcDjOmTAZ1sZ0DJyfO1zCkmrjuoSHBx+Xly67aYP/JyxGZ3qt990AAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/3edb5e3c5ce987920fa3022f84b3f266/8ac56/wani3.webp 240w,\n/static/3edb5e3c5ce987920fa3022f84b3f266/d3be9/wani3.webp 480w,\n/static/3edb5e3c5ce987920fa3022f84b3f266/9d6da/wani3.webp 492w\"\n              sizes=\"(max-width: 492px) 100vw, 492px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/3edb5e3c5ce987920fa3022f84b3f266/8ff5a/wani3.png 240w,\n/static/3edb5e3c5ce987920fa3022f84b3f266/e85cb/wani3.png 480w,\n/static/3edb5e3c5ce987920fa3022f84b3f266/5c6e9/wani3.png 492w\"\n            sizes=\"(max-width: 492px) 100vw, 492px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/3edb5e3c5ce987920fa3022f84b3f266/5c6e9/wani3.png\"\n            alt=\"image-20210505100538601\"\n            title=\"image-20210505100538601\"\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<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">More concretely, you fail if even one of the following conditions is triggered.\n\n- Whether the runtime arguments include a file\n- Whether the file contents are not empty\n- Whether the first line of the file is `-----BEGIN LICENCE KEY-----</code></pre></div>\n<p><code class=\"language-text\">- Whether the return value of the</code>check<code class=\"language-text\">function is not</code>1<code class=\"language-text\">- Whether the end of the file is</code>-----END LICENCE KEY-----\n`</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">Everything except the return value of the `check` function is easy to satisfy.\n\nFrom this, we can see that the final key file must look like this.</code></pre></div>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">-----BEGIN LICENCE KEY-----\nFLAG{xxxxxxxxxx}\n-----END LICENCE KEY-----</code></pre></div>\n<p>In other words, the value of <code class=\"language-text\">FLAG{xxxxxxxxx}</code> that can get past the <code class=\"language-text\">check</code> function is the correct flag.</p>\n<p>So let’s look inside the <code class=\"language-text\">check</code> 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: 500px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/7311eea956d0d215209a64cfe15d4890/0b533/wani4.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/Ca1DAAAACXBIWXMAARlAAAEZQAGA43XUAAAB7ElEQVQoz3WTu28TQRDG/SciGoRERZWKAtFRITqEFEpoaEDUSAQJQRASUgL2xQkGBYySEJzEjnMv7712ffv6mN07G0SSOY32MavffjM713nZC/D44xqGW1PsDk+xvx9idBRjcpoiShjCJMVZmKC/PcCXwTds73zFj+Eefu4d4OBwhKPjCUYnY5xMz/D6zTo6gMZteQdr1StcZtZaMDbzczFXkORKGShpoI3ycWebnwMC0jyyEa5m1xEUOz4g20OLg0opRFHk52XFwStBMO3BtayhtPKxjc0uOpoUOuiu/Y4r2TWM5xMf1LbZXwLjuL3EnMvAmGZv41PPpUyKrPQbb/U6brCb4Eq0ULMEJkmC+VxCCAkpJc0p9VrC0JlzQHezso3sZ+oFVtgtWNPII00eGJNCQcCi4Kjr2oNrQelSeS4Eus+nSfZQruIuu+feq6mpA5JCpTWqqvIXKKV9HbUh1wtg9y8QtlHTvgPulw/wNH2+BLqUcUH9/rXmlRcKW3c1cXA3PkqfoFv0/frX4W+MxxOE9NphFHuP4oSUU7/GKcqyxLv3HxrgZSah0GMDzARDxjJqmcq7S5tzah8uwIXwo6tjsNVHpyhKZHmB/D/P8hwuxguBOE0xpT8hpzXLcn8+nTHvbu1Uuv7sBn38ASZ1Q7Yb30eEAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/7311eea956d0d215209a64cfe15d4890/8ac56/wani4.webp 240w,\n/static/7311eea956d0d215209a64cfe15d4890/d3be9/wani4.webp 480w,\n/static/7311eea956d0d215209a64cfe15d4890/b0a15/wani4.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/7311eea956d0d215209a64cfe15d4890/8ff5a/wani4.png 240w,\n/static/7311eea956d0d215209a64cfe15d4890/e85cb/wani4.png 480w,\n/static/7311eea956d0d215209a64cfe15d4890/0b533/wani4.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/7311eea956d0d215209a64cfe15d4890/0b533/wani4.png\"\n            alt=\"image-20210505101318726\"\n            title=\"image-20210505101318726\"\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>Unbelievably, the decompiled result contained 2,581 lines of conditional branches, and it was a function that failed if even one of them <strong>matched</strong>!</p>\n<p>If we were looking for a flag that had to match all of the conditions, we might be able to find it with enough grit and determination, but when the goal is to find a string that does <em>not</em> match them, it seems essentially impossible to do by hand.</p>\n<p>So from here on, the goal is to use angr to get past this <code class=\"language-text\">check</code> function.</p>\n<h2 id=\"retrieving-the-flag\" style=\"position:relative;\"><a href=\"#retrieving-the-flag\" aria-label=\"retrieving 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>Retrieving the Flag</h2>\n<p>As we saw earlier, angr can determine what value the key must have in order to reach the output produced when the correct key is entered.</p>\n<p>Therefore, to recover the flag, we need to give angr the following information.</p>\n<ol>\n<li>The virtual address reached when the correct key is entered</li>\n<li>The virtual address reached when an incorrect key is entered</li>\n</ol>\n<p>You can identify both of these from the disassembly results.</p>\n<p>First, when the correct key is entered, execution reaches the output at <code class=\"language-text\">0x5e57</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/77a2bf46ad81022706f9d755bc459eb1/0b533/wani5.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: 19.166666666666664%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAYAAACOXx+WAAAACXBIWXMAARlAAAEZQAGA43XUAAAA40lEQVQY0y2PwW6DMBAFwWCMjY0hwQRSksakbXpoK/X/P266lXIY7WpXT5pXpMWwLpaDaXifRz7OE7tzrGkipkSYFvy4YMKKG45o01I2nsIfKcpKKAX13GXq74j+ipRdID9ufD4yIUamfOX4tuPvGXu/016FZccdNly84N2J0PRYbfBC+U9tKPrzgH+dCd3AlDbW/IvLP+h0w6UzlZg2y0Y9zlTxhBlfsDFh2w4nGSu0TqT6hLI9hd1q7KWhURXzPHDICW0dLkggDeihRXcaJX9VFJi6xYtJkIqdYOVeq/pZW/EHfgVdEMHcI3UAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/77a2bf46ad81022706f9d755bc459eb1/8ac56/wani5.webp 240w,\n/static/77a2bf46ad81022706f9d755bc459eb1/d3be9/wani5.webp 480w,\n/static/77a2bf46ad81022706f9d755bc459eb1/b0a15/wani5.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/77a2bf46ad81022706f9d755bc459eb1/8ff5a/wani5.png 240w,\n/static/77a2bf46ad81022706f9d755bc459eb1/e85cb/wani5.png 480w,\n/static/77a2bf46ad81022706f9d755bc459eb1/0b533/wani5.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/77a2bf46ad81022706f9d755bc459eb1/0b533/wani5.png\"\n            alt=\"image-20210505103929092\"\n            title=\"image-20210505103929092\"\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>However, as we already saw, angr’s loader maps the challenge binary into the virtual address space <code class=\"language-text\">[0x400000:0x807fff]</code>, so we need to convert <code class=\"language-text\">0x5e57</code> to <code class=\"language-text\">0x405e57</code> before passing it in.</p>\n<p>Next, when an incorrect key is entered, the binary always calls the function at <code class=\"language-text\">0x5e86</code>.</p>\n<p>So we instruct angr not to reach that address.</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/e8856cfc280b6dde24d600e4d2e2325f/0b533/wani6.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: 12.083333333333332%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAACCAYAAABYBvyLAAAACXBIWXMAARlAAAEZQAGA43XUAAAAfklEQVQI1w2N2w6CMBBE227bXRAUgjcSH4gQxYT//73jvp3JTM6E/FLKXgmtcO6U9zJxaZRshTS35FmR2536OWiPL3EakCRoNqpkLGVnpdGeYoN7RuX6UDrLTHbi2feMsXD2UVUj+klYNsK6E7af84oURcV7qS4UakxYFIrnP0IkLVO00Nx3AAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/e8856cfc280b6dde24d600e4d2e2325f/8ac56/wani6.webp 240w,\n/static/e8856cfc280b6dde24d600e4d2e2325f/d3be9/wani6.webp 480w,\n/static/e8856cfc280b6dde24d600e4d2e2325f/b0a15/wani6.webp 500w\"\n              sizes=\"(max-width: 500px) 100vw, 500px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/e8856cfc280b6dde24d600e4d2e2325f/8ff5a/wani6.png 240w,\n/static/e8856cfc280b6dde24d600e4d2e2325f/e85cb/wani6.png 480w,\n/static/e8856cfc280b6dde24d600e4d2e2325f/0b533/wani6.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/e8856cfc280b6dde24d600e4d2e2325f/0b533/wani6.png\"\n            alt=\"image-20210505102848116\"\n            title=\"image-20210505102848116\"\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>With that, by passing the correct address to <code class=\"language-text\">find</code> and the failure address to <code class=\"language-text\">avoid</code> in the Simulation Manager and calling the <code class=\"language-text\">explore</code> function, I was able to obtain the symbolic data needed to reach <code class=\"language-text\">0x5e57</code>.</p>\n<div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token operator\">>></span><span class=\"token operator\">></span> simgr<span class=\"token punctuation\">.</span>explore<span class=\"token punctuation\">(</span>find<span class=\"token operator\">=</span><span class=\"token punctuation\">(</span><span class=\"token number\">0x405e57</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span> avoid<span class=\"token operator\">=</span><span class=\"token punctuation\">(</span><span class=\"token number\">0x405e86</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n・・・\n<span class=\"token operator\">&lt;</span>SimulationManager <span class=\"token keyword\">with</span> <span class=\"token number\">3</span> active<span class=\"token punctuation\">,</span> <span class=\"token number\">390</span> deadended<span class=\"token punctuation\">,</span> <span class=\"token number\">1</span> found<span class=\"token operator\">></span>\n\n<span class=\"token operator\">>></span><span class=\"token operator\">></span> simgr<span class=\"token punctuation\">.</span>found<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span>posix<span class=\"token punctuation\">.</span>dumps<span class=\"token punctuation\">(</span><span class=\"token number\">3</span><span class=\"token punctuation\">)</span>\n<span class=\"token string\">b'-----BEGIN LICENCE KEY-----\\nFLAG{4n6r_15_4_5up3r_p0w3rfu1_5ymb0l1c_xxxxxxx_xxxxxxxx}\\n-----END LICENCE KEY-----\\n'</span></code></pre></div>\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>Through the WaniCTF Reversing challenge “licence,” I looked back on what I learned about using angr, the tool that analyzes complex processing through symbolic execution.</p>\n<p>I am glad I was able to learn about a tool that will be extremely useful as I continue solving reversing challenges.</p>\n<p>At the same time, there are still many things I do not fully understand, so I plan to keep studying.\nIf there is anything inaccurate in this article, I would appreciate it if you point it out.</p>\n<p>Once again, thanks to the people who ran such a worthwhile CTF and to the players who competed with me.</p>\n<p>Next time I want to place higher…</p>\n<h2 id=\"solver-used-this-time\" style=\"position:relative;\"><a href=\"#solver-used-this-time\" aria-label=\"solver used this time 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>Solver Used This Time</h2>\n<div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token keyword\">import</span> angr\n<span class=\"token keyword\">import</span> monkeyhex\n\nproj <span class=\"token operator\">=</span> angr<span class=\"token punctuation\">.</span>Project<span class=\"token punctuation\">(</span><span class=\"token string\">\"licence.exe\"</span><span class=\"token punctuation\">,</span> auto_load_libs<span class=\"token operator\">=</span><span class=\"token boolean\">False</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token triple-quoted-string string\">\"\"\"\npipenv shell\npip install angr\n\"\"\"</span>\n\n<span class=\"token comment\"># create project</span>\nproj <span class=\"token operator\">=</span> angr<span class=\"token punctuation\">.</span>Project<span class=\"token punctuation\">(</span><span class=\"token string\">'licence.exe'</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token comment\"># initial_state at the entry point of the binary</span>\ninit_state <span class=\"token operator\">=</span> proj<span class=\"token punctuation\">.</span>factory<span class=\"token punctuation\">.</span>entry_state<span class=\"token punctuation\">(</span>args <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token string\">'licence.exe'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'key.dat'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token comment\"># create simulation</span>\nsimgr <span class=\"token operator\">=</span> proj<span class=\"token punctuation\">.</span>factory<span class=\"token punctuation\">.</span>simgr<span class=\"token punctuation\">(</span>init_state<span class=\"token punctuation\">)</span>\n\nsimgr<span class=\"token punctuation\">.</span>explore<span class=\"token punctuation\">(</span>find<span class=\"token operator\">=</span><span class=\"token punctuation\">(</span><span class=\"token number\">0x405e57</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span> avoid<span class=\"token operator\">=</span><span class=\"token punctuation\">(</span><span class=\"token number\">0x405e86</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">if</span> <span class=\"token builtin\">len</span><span class=\"token punctuation\">(</span>simgr<span class=\"token punctuation\">.</span>found<span class=\"token punctuation\">)</span> <span class=\"token operator\">></span> <span class=\"token number\">0</span><span class=\"token punctuation\">:</span>\n    <span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>simgr<span class=\"token punctuation\">.</span>found<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span>posix<span class=\"token punctuation\">.</span>dumps<span class=\"token punctuation\">(</span><span class=\"token number\">3</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span></code></pre></div>\n<h3 id=\"references\" style=\"position:relative;\"><a href=\"#references\" aria-label=\"references 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>References</h3>\n<ul>\n<li><a href=\"https://angr.io/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">angr Document</a></li>\n<li><a href=\"https://www.fujitsu.com/downloads/JP/archive/imgjp/jmag/vol66-5/paper05.pdf\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Exhaustive Test Case Generation Using Symbolic Execution</a></li>\n<li><a href=\"http://inaz2.hatenablog.com/entry/2016/03/16/190756\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Trying Symbolic Execution with angr - Momoiro Technology</a></li>\n<li><a href=\"https://github.com/angr/angr\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">GitHub - angr/angr: A powerful and user-friendly binary analysis platform!</a></li>\n<li><a href=\"http://angr.io/api-doc/cle.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">cle — Binary Loader — angr 9.0.6885 documentation</a></li>\n<li><a href=\"https://gist.github.com/tanishiking/1c2a9aa824cd715948cde37b1ea2c2ec\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Notes from Studying Symbolic Execution · GitHub</a></li>\n<li><a href=\"https://prepack.io/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Prepack · Partial evaluator for JavaScript</a></li>\n</ul>","fields":{"slug":"/ctf-angr-bigginer-en","tagSlugs":["/tag/ctf-en/","/tag/reversing-en/","/tag/angr-en/","/tag/english/"]},"frontmatter":{"date":"2021-10-03","description":"Through the Very Hard reversing challenge \"licence,\" I learned how to use angr, a tool that analyzes complex processing with symbolic execution, so this article looks back on that experience and summarizes how to use angr.","tags":["CTF (en)","Reversing (en)","Angr (en)","English"],"title":"Notes on Solving a Reversing Challenge with angr Symbolic Execution [WaniCTF2021]","socialImage":{"publicURL":"/static/334bd91f01f5d703919e643c6130dca6/ctf-elf-training.png"}}}},"pageContext":{"slug":"/ctf-angr-bigginer-en"}},"staticQueryHashes":["251939775","401334301","825871152"]}