{"id":125200,"date":"2021-08-10T16:00:54","date_gmt":"2021-08-10T16:00:54","guid":{"rendered":"https:\/\/developer.apple.com\/news\/?id=3st92oup"},"modified":"2021-08-10T16:00:54","modified_gmt":"2021-08-10T16:00:54","slug":"challenge-solution-to-memgraph-capture-the-flag","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2021\/08\/10\/challenge-solution-to-memgraph-capture-the-flag\/","title":{"rendered":"Challenge: Solution to \u201cMemgraph Capture The Flag\u201d"},"content":{"rendered":"<div class=\"inline-article-image\"><img decoding=\"async\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2022\/05\/challenge-solution-to-memgraph-capture-the-flag.jpg\" data-hires=\"false\" alt=\"Flag icon on grey background\"><\/div>\n<p>The \u201cMemgraph Capture the Flag\u201d challenge invites you to learn and practice memory debugging and symbolication with command line tools. If you haven\u2019t yet attempted the challenge or otherwise don\u2019t want to be spoiled on the necessary steps to complete it, we recommend returning to the original challenge page. Otherwise, read on!<\/p>\n<p><a href=\"https:\/\/developer.apple.com\/news\/?id=keebiiyl\" class=\"icon icon-after icon-chevronright\">Challenge: Memgraph Capture the Flag<\/a><\/p>\n<p>The challenge begins with the following: \u201cOne of our engineers has hidden a memory easter egg in our secret app. We&#8217;re trying to track it down but all we know is that it has format <code>flag_&lt;unknown_string_here&gt;@WWDC<\/code>. You\u2019ll have to use the command line tools offered by macOS to investigate the memory issue, recover missing symbols, and and capture the rogue flag.\u201d<\/p>\n<p>The following solution is one of the possible paths to capture the flag. To start, the challenge article supplies you with a memgraph file and a dSYM, along with the following hint: \u201cMemgraph is a special binary plist. What can you find in its properties?\u201d<\/p>\n<p>To look at the properties of a Memgraph, use <code>plutil<\/code>. In the output, you\u2019ll find more hints:<\/p>\n<pre class=\"code-source\"><code>$ plutil -p secret.memgraph\n... \"hint\" =&gt; \"the flag is hiding in a memory leak\"\n... \"one_more_hint\" =&gt; \"you might also want to explore the 'symbols' in the dSYM\u201d\n...<\/code><\/pre>\n<p>The hint invites you to investigate the memory leak, while <code>one_more_hint<\/code> directly encourages you to use the <code>symbols<\/code> CLI tool. In the WWDC21 session \u201cSymbolication: Beyond the basics,\u201d engineer Alejandro Lucena mentions that it\u2019s a good idea to specify the architecture with this tool. As such, you can use the memgraph to learn the \u201csecret\u201d app\u2019s architecture.<\/p>\n<p>You can try <code>heap<\/code>, <code>leaks<\/code> or <code>vmmap<\/code>. When used with a memgraph, the first few lines are the same in the output of each of these tools. This is how you learn which architecture the \u201csecret&#8221; process uses: <code>Code Type: X86-64<\/code>.<\/p>\n<p>As detailed in the WWDC21 session \u201cDetect and diagnose memory problems,\u201d you can use the vmmap command against memgraph files in addition to targeting the running process. Running it agaisnt this memgraph provides the following information: <\/p>\n<pre class=\"code-source\"><code>$ vmmap -summary secret.memgraph Process: secret [2901]\nPath: \/Users\/*\/secret\nLoad Address: 0x10d264000\nIdentifier: secret\nVersion: 0\nCode Type: X86-64\nPlatform: macOS\nParent Process: zsh [1438]\n...<\/code><\/pre>\n<p>Now you\u2019re ready to use the <code>symbols<\/code> command. Use the <code>-noSources<\/code> option to restrict the output to symbol names so you have less output to look through. Hidden amidst the symbols in the \u201csecret\u201d dSYM, you\u2019ll find another breadcrumb towards the solution:<\/p>\n<pre class=\"code-source\"><code>$ symbols -arch x86_64 -noSources secret.dSYM [macOS Monterey+, Xcode 13+]\nor\n$ symbols -arch x86_64 -noSources secret.dSYM\/Contents\/Resources\/DWARF\/secret [macOS before Monterey, Xcode before 13]\n...\nhint_find_the_secret_addresses_of_the_memory_leak\n...<\/code><\/pre>\n<p>The leaked memory in this memgraph is definitely starting to sound interesting \u2014 it\u2019s probably a specific leaked address. Let\u2019s see which secret addresses the hint is referring to. To determine if the \u201csecret\u201d app was leaking memory, you can check the memgraph for leaks with the <code>leaks<\/code> command line tool.<\/p>\n<p>You\u2019ll want to pay attention to this portion of the output: <\/p>\n<pre class=\"code-source\"><code>$ leaks secret.memgraph\n...\nSTACK OF 5 INSTANCES OF 'ROOT LEAK: <span class=\"syntax-tag\">&lt;<span class=\"syntax-name\">CFArray<\/span>&gt;<\/span>':\n6 hint 0x7fff204edf3d how would you translate secret Addresses TO Symbols? + 1\n5 secret 0x10d267ee8 0x10d264000 + 16104\n4 secret 0x10d267dc5 0x10d264000 + 15813\n3 secret 0x10d267ccf 0x10d264000 + 15567\n2 com.apple.CoreFoundation 0x7fff2059576f __CFArrayCreateInit + 190\n1 com.apple.CoreFoundation 0x7fff2054df07 _CFRuntimeCreateInstance + 587\n0 libsystem_malloc.dylib 0x7fff20314071 _malloc_zone_malloc + 242 ...<\/code><\/pre>\n<p>In this output, you\u2019ll spot five leaks, all of which originated from the same place in code. They are united by the same call stack backtrace each time a <code>CFArray<\/code> was allocated, but never freed. You\u2019ll find three secret addresses here \u2014 <code>0x10d267ee8<\/code>, <code>0x10d267dc5<\/code> and <code>0x10d267ccf<\/code> \u2014 along with a new hint sporting some interesting capitalization. This clue hints that you should try and use <code>atos<\/code> tool to symbolicate the secret addresses.<\/p>\n<p>To call <code>atos<\/code>, you need several components: The DWARF binary in the dSYM, architecture, and addresses to symbolicate. You\u2019re missing the load address, however, and can find it for the \u201csecret\u201d binary image within the call stack of the leaks next to all three secret addresses: <code>0x10d264000<\/code>. You can also find it in the the process description and list of binary images portions of the <code>leaks<\/code> output:<\/p>\n<pre class=\"code-source\"><code>$ leaks secret.memgraph\nProcess: secret [2901]\nPath: \/Users\/*\/secret\nLoad Address: 0x10d264000\n...\nBinary Images: 0x10d264000 - 0x10d267ff7 +secret (0) &lt;6676D338-8C26-3019-B919-88C1CB4AA324&gt; \/Users\/\/secret\n...<\/code><\/pre>\n<p>Now, you can use <code>atos<\/code> to translate the secret addresses to symbols:<\/p>\n<pre class=\"code-source\"><code>$ atos -o secret.dSYM\/Contents\/Resources\/DWARF\/secret -arch x86_64 -l 0x10d264000 0x10d267ee8 0x10d267dc5 0x10d267ccf\nmain (in secret) (main.m:226)\nvery_nice_function (in secret) (main.m:205)\ngood_job_but_the_flag_is_inlined (in secret) (main.m:186)<\/code><\/pre>\n<p>You\u2019re getting close: The flag is inlined, so you need to add the <code>-i<\/code> command line option while calling <code>atos<\/code> to display the inlined functions too:<\/p>\n<pre class=\"code-source\"><code>$ atos -o secret.dSYM\/Contents\/Resources\/DWARF\/secret -arch x86_64 -l 0x10d264000 0x10d267ee8 0x10d267dc5 0x10d267ccf -i\nmain (in secret) (main.m:413) very_nice_function (in secret) (main.m:392) IGZsYWdfbWVNMHJ5VDBPTHNEZWJ1R0cxbmdQcjBAV1dEQyAg (in secret) (main.m:86)\nwhats_wrong_with_encoding (in secret) (main.m:204)\nomg_you_found_it (in secret) (main.m:333)\ngood_job_but_the_flag_is_inlined (in secret) (main.m:373)<\/code><\/pre>\n<p>Closer still! Run this command and you\u2019ll get a string of gibberish that looks like it might be base64-encoded. Run that decode and you get:<\/p>\n<pre class=\"code-source\"><code>$ echo \"IGZsYWdfbWVNMHJ5VDBPTHNEZWJ1R0cxbmdQcjBAV1dEQyAg\" | base64 -d flag_meM0ryT0OLsDebuGG1ngPr0@WWDC %<\/code><\/pre>\n<p>As a note: Make sure you also pass the <code>-arch<\/code> flag to <code>atos<\/code>, because <code>atos<\/code> defaults to the architecture you&#8217;re actively using. For example, if you run this command from <code>x86_64<\/code> (either on x86 hardware or in Rosetta 2), you won&#8217;t see any change. But if you run the tool from an Apple Silicon machine without the <code>-arch<\/code> flag, you&#8217;ll get strings delivered for the wrong architecture: <\/p>\n<pre class=\"code-source\"><code>IOKdmuKWiOKVkOKVkOKWiOKdmiAg4p2a4paI4pWQ4pWQ4paI4p2a (in secret) (main.m:396)\nIOKWhyDiloUg4paIIOKWhSDilocg4paCIOKWgyDiloEg4paB (in secret) (main.m:381)<\/code><\/pre>\n<p>Convert those from their base64 encoding, and you&#8217;ll get some very pretty ASCII art weights. <\/p>\n<pre class=\"code-source\"><code>\u275a\u2588\u2550\u2550\u2588\u275a \u275a\u2588\u2550\u2550\u2588\u275a \u2587 \u2585 \u2588 \u2585 \u2587 \u2582 \u2583 \u2581 \u2581<\/code><\/pre>\n<p>While these might be helpful for filling the fitness rings on your Apple Watch, you need the <code>-arch x86_64<\/code> parameter to capture this particular flag.<\/p>\n<p>One last tip: The secret.memgraph was generated when the \u201csecret\u201d process was running with <code>MallocStackLogging<\/code> enabled; this allows you to see the call stack of the leak. Don\u2019t forget to enable <code>MallocStackLogging<\/code> for your own app when generating memgraphs. This can be done in the scheme settings in Xcode (Run &gt; Diagnostics &gt; MallocStackLogging = Live Allocations Only) or with the environment variable when launching from a terminal:<\/p>\n<pre class=\"code-source\"><code>`$ MallocStackLogging=lite <span class=\"syntax-tag\">&lt;<span class=\"syntax-name\">command<\/span>&gt;<\/span>`<\/code><\/pre>\n<hr>\n<p>This is just one path you can follow to get the flag using the command line tools built into macOS for memory debugging and symbolication. Check out the full repository of Coding and Design Challenges for other fun coding and design explorations, or dive deeper into debugging with our most recent WWDC21 videos.<\/p>\n<h3>Resources<\/h3>\n<section class=\"grid activity\">\n<section class=\"row\">\n<section class=\"column large-4 small-4 no-padding-top no-padding-bottom\"> <a href=\"https:\/\/developer.apple.com\/wwdc21\/10211\" class=\"activity-image-link\"> <img decoding=\"async\" class=\"actiity-image medium-scale\" width=\"250\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2022\/05\/challenge-solution-to-memgraph-capture-the-flag-1.jpg\" data-hires=\"false\" alt> <\/a> <\/section>\n<section class=\"column large-8 small-8 padding-left-small padding-top-small padding-bottom-small no-padding-top no-padding-bottom\"> <a href=\"https:\/\/developer.apple.com\/wwdc21\/10211\"> <\/p>\n<h4 class=\"no-margin-bottom activity-title\">Symbolication: Beyond the basics<\/h4>\n<p> <\/a> <\/section>\n<\/section>\n<\/section>\n<section class=\"grid activity\">\n<section class=\"row\">\n<section class=\"column large-4 small-4 no-padding-top no-padding-bottom\"> <a href=\"https:\/\/developer.apple.com\/wwdc21\/10180\" class=\"activity-image-link\"> <img decoding=\"async\" class=\"actiity-image medium-scale\" width=\"250\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2022\/05\/challenge-solution-to-memgraph-capture-the-flag-2.jpg\" data-hires=\"false\" alt> <\/a> <\/section>\n<section class=\"column large-8 small-8 padding-left-small padding-top-small padding-bottom-small no-padding-top no-padding-bottom\"> <a href=\"https:\/\/developer.apple.com\/wwdc21\/10180\"> <\/p>\n<h4 class=\"no-margin-bottom activity-title\">Detect and diagnose memory issues<\/h4>\n<p> <\/a> <\/section>\n<\/section>\n<\/section>\n<p><a href=\"https:\/\/developer.apple.com\/news\/?id=j6efg2g1\" class=\"icon icon-after icon-chevronright\">Explore more coding &amp; design challenges<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The \u201cMemgraph Capture the Flag\u201d challenge invites you to learn and practice memory debugging and symbolication with command line tools. If you haven\u2019t yet attempted the challenge or otherwise don\u2019t want to be spoiled on the necessary steps to complete it, we recommend returning to the original challenge page. Otherwise, read on! Challenge: Memgraph Capture [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":125201,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[55],"tags":[],"class_list":["post-125200","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apple-developer-news"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/125200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=125200"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/125200\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media\/125201"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=125200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=125200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=125200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}