× SPECIAL FORCES      |     LOCKSPORT      |     SCI FI      |     INSECTS      |     RECIPE      |     RED DEAD REDEMPTION      |     WOODWORKING      |     FISHING      |     SCI FY      |     CRIME STORIES      |     BIRDS      |     STYLE      |     TV      |     INTERESTING      |     ENVIRONMENT      |     STAR WARS      |     OLYMPICS      |     EASTER      |     GOD OF WAR      |     FOOD & DRINK      |     PHOTOGRAPHY      |     WEDDING      |     COLD WAR      |     WAR      |     CHRISTMAS      |     ARCHITECTURE      |     FITNESS      |     ART      |     TOYS      |     SKATEBOARDING      |     YOUR HOME      |     PSYCHOLOGY      |     RELIGION      |     HEALTH & WELLNESS      |     PETS      |     GUNS      |     MATH      |     BIOLOGY      |     WW1      |     BUSINESS      |     WORLD WAR 2      |     AUTOMOTIVE      |     STEM      |     GUITAR      |     AQUARIUM      |     HOMEBREW      |     KITCHEN      |     SPEECHES      |     KIDS      |     ECONOMICS      |     PROGRAMMING      |     REMOTE CONTROL      |     MINECRAFT      |     MOTORBIKES      |     VALENTINE'S DAY      |     MULTIPLICATION      |     LAWNCARE      |     GARDENING      |     PI DAY      |     LEGO      |     BLOCKCHAIN      |     COMPUTER SECURITY      |     SPORTS      |     CATS      |     MUSIC      |     FORTNITE BATTLE ROYALE      |     FARMING      |     ARTIFICIAL INTELLIGENCE      |     ROCKCLIMBING      |     RELAXING      |     CSGO      |     BABY      |     AVIATION      |     MENS STYLE      |     ANIMALS      |     SCIENCE      |     DOGS      |     SHOOTING SPORTS      |     MOVIES      |     OUTDOORS      |     BIKING      |     HISTORY      |     FASHION      |     FAR CRY 5      |     HALLOWEEN      |     STPATRICKS      |     CRYPTOCURRENCY      |     ENGINEERING      |     GEOPOLITICS      |     POLITICS      |     TECHNOLOGY      |     FUNNY      |     TRAVEL      |     MARTIAL ARTS      |     GAMBLING      |     GAMING      |     KNITTING      |     BEAUTY      |     PHILOSOPHY      |     ANCIENT WORLD      |     POTTERY      |     ARCHERY      |     THANKSGIVING      |     HOME REPAIR      |     CRAFTS      |     ORGANIZATION      |     BIG THOUGHTS

  • 00:05
    It's the heap exploitation introductory challenge and is very easy.
  • 00:10
    We again compile this on a Ubuntu 16.04 LTS version, so basically a modern system, to see if or how it's still exploitable.
  • 00:20
    And spoiler alert, nothing changed for this challenge, it's super straight forward.
  • 00:25
    But I have a special idea for this video and even if it's a bit easy for you, you might want to checkout what else we learn at the end.
  • 00:39
    In the previous videos of this series I usually create the exploit and then think of a way how to explain and show it to you.
  • 00:46
    But this is so simple, that I thought it would be cool if I would instead record myself solving it, kind of like a blind solve or a speedrun.
  • 00:54
    But I didn't try to be super fast but it was quite straight forward and I include all the mistakes and pauses I made.
  • 01:01
    And now we will step through the video and I explain to you what I have been thinking in different moments and point out some other things.
  • 01:08
    In the top right corner you can also see a timer that will keep track of how long it took me in real time.
  • 01:14
    But before we start doing the exploit let's have a look at the code again.
  • 01:18
    There are two functions winner() and nowinner().
  • 01:21
    And obviously we have to somehow call winner().
  • 01:24
    We can also see there are two structs that get space allocated for them on the heap.
  • 01:30
    And this fp construct here looks complex, but you can ignore that weirdness because when you look in the code it's clear what it does.
  • 01:38
    We set fp to nowinner.
  • 01:41
    Notice how nowinner has no parantheses, this means it's not being called.
  • 01:45
    This is literally the function pointer and adding paraentheses would cause a call.
  • 01:51
    And then later we have those paraentheses for fp.
  • 01:55
    And fp has been set to point to nowinner, so nowinner is executed().
  • 02:00
    And our goal is it to somehow use the strcpy, which will overflow the name buffer which is only 64byte large and overwrite the function pointer.
  • 02:10
    So sounds easy.
  • 02:12
    I start by opening up the binary in gdb.
  • 02:15
    And do a first test execution, but I run into a segfault which startled me for a few seconds, but then I realized I forgot the argument parameter again.
  • 02:25
    The strcpy uses the first argument to copy into name.
  • 02:29
    Ok now we had a clean execution.
  • 02:32
    Now I want to set a good breakpoint so I disassemble main.
  • 02:36
    I'm quickly scanning the assembler code here, mainly looking at the different function calls to figure out what corresponds to what in the C code.
  • 02:44
    And at first I was thinking about setting a breakpoint before or after the strcpy, to catch the before and after of the overflow, but in the last moment then figured that I probably don't need to look at it this closely, and I can simply go to the magic position right away.
  • 03:01
    The call rdx.
  • 03:03
    This is calling the function pointer that contains nowinner().
  • 03:07
    Ok, so I execute it again and we hit the breakpoint.
  • 03:11
    Now this challenge is about a heap overflow, so I first check the virtual memory map of the process with vmmap.
  • 03:18
    Here you can see in which memory regions we have the binary itself with the code and data segments, we can also see where the stack is and where shared libraries like libc are loaded too, and we also have the heap here.
  • 03:31
    So obviously I want to check out how the heap looks like.
  • 03:34
    Examine 32 64bit hex values from the start of the heap.
  • 03:38
    I immediately look for the name we entered as an argument, which was “AAAA”, so here they are.
  • 03:45
    And I also immediately look for the function pointer.
  • 03:48
    This looks like an address.
  • 03:50
    Quick sanity check with the disassemble command.
  • 03:53
    Here is a puts call using this address as a paremter, and so that is our nowinner string.
  • 03:58
    So yep, that's nowinner.
  • 04:00
    So now we want to overwrite that with winner, so we need that address.
  • 04:05
    Here it is.
  • 04:06
    Next I need to figure out how much we have to overflow, to do that I simply look at the addresses to the left.
  • 04:13
    Address of the start of the name ends in 0x10, and the function pointer is ath 0x60.
  • 04:21
    So we have an offset of 0x50.
  • 04:24
    So now I'm feeling confident and actually drop out of gdb and hope to have a working exploit right away.
  • 04:30
    So I start by writing a short python inline script to print the exploit string.
  • 04:35
    Essentially we need a couple of characters as padding to reach the function pointer and so I print a few As.
  • 04:42
    Quick check again how many that was, 0x60-0x10 so we need 0x50.
  • 04:48
    After that we need the address of winner.
  • 04:50
    So 0x40, OOPS!
  • 04:53
    Almost made a mistake - this stil happens to me sometime, we obviously have to start with 0xf6, 0x05 and then 0x40.
  • 05:00
    Because of the endianess.
  • 05:02
    Now for a sanity and debugging step I pipe that output into hexdump to see if it is what I expect.
  • 05:08
    But then I notice a 0x0a at the end, and that's a newline.
  • 05:13
    Python print will add a newline at the end which we don't want.
  • 05:17
    So now I change the script to use the sys module instead in order to directly write a string to stdout, so we don't have a newline.
  • 05:25
    And I verify that again with hexdump.
  • 05:30
    And then I'm basically done and try it on the target binary.
  • 05:36
    So the input is passed as argument, so I use backticks to execute the inner python command, and the output is then basically replaced by it and placed here as the arguments.
  • 05:49
    Level passed!
  • 05:51
  • 05:52
    I executed the winner function().
  • 05:54
    You see this was super simple.
  • 05:57
    So when I was writing this script with the commentary of my recording, I noticed a small detail that I didn't think about.
  • 06:04
    And I actually never thought about before.
  • 06:06
    So here is the heap output again.
  • 06:09
    Do you see this data down here?
  • 06:10
    That is clearly ascii.
  • 06:12
    And that's weird, in our program we did not allocate any string like this on the heap.
  • 06:18
    So how did this happen?
  • 06:19
    When you look at this ascii text, then you will realize it's in fact the printf output.
  • 06:25
    But why is that on the heap?
  • 06:26
    Let's investigate.
  • 06:28
    First I thought we could checkout valgrind.
  • 06:31
    Valgrind is an instrumentation framework for building dynamic analysis tools.
  • 06:35
    There are Valgrind tools that can automatically detect many memory management and threading bugs, and profile your programs in detail.
  • 06:42
    I really should use valgrind more often, I use it wayy to little.
  • 06:46
    But here is for example the valgrind output with tracing mallocs enabled.
  • 06:50
    And then we run our heap0 level.
  • 06:53
    And we can indeed see here our two mallocs of the structs we do, but also a malloc we didn't do of 1024.
  • 07:00
    That's also the only memory that is freed again.
  • 07:04
    The mallocs we do have no free.
  • 07:07
    So why is that happening?
  • 07:09
    Another interesting output is strace.
  • 07:11
    Strace traces syscalls.
  • 07:13
    And while we don't see mallocs here, because malloc is just some algorithm and memory managment implemented in libc, we can see the brk syscall, which gets the memory from the operating system in the first place.
  • 07:26
    So this is where we get memory that will then be used by libc for the heap.
  • 07:31
    So if malloc is a libc function, we can also checkout ltrace, which traces linked dynamic library function calls.
  • 07:38
    But oddly enough we only see two mallocs for the two structs.
  • 07:43
    Nothing about the mysterious third malloc.
  • 07:46
    It might not be immediately obvious, but that is actually already a really good hint that the mysterious malloc did not happen from a dynamically linked library call.
  • 07:55
    Which means, this malloc must have been executed for example by libc itself.
  • 08:01
    And valgrind is a bit smarter and also traces these internal mallocs.
  • 08:06
    For the third test I create a simple program that calls puts, so it prints a string.
  • 08:12
    Because we know the heap did contain the printf output so it must have to do something with that.
  • 08:18
    And then we can debug that program and set a breakpoint on brk.
  • 08:22
    Remember brk is the syscall that is called when a program requests additional virtual memory, and so this is called when the heap is set up.
  • 08:30
    And the heap is not always setup, only if it is required.
  • 08:34
    So if we assume printf or puts calls malloc, it would have to setup the heap first.
  • 08:40
    Now that's also why I created this small test program, because the original heap0 has obviously regular mallocs before the printf, which makes it a bit annoying, so this is a clean example.
  • 08:51
    On a second note, when you set a breakpoint with a symbol name like brk, there has to be a symbol name for it.
  • 08:58
    And a syscall doesn't have a symbol name.
  • 09:01
    A syscall is an asembler interrup instruction with a number as paramter to indicate which syscall you want.
  • 09:08
    But there is a brk symbol, but it's not initially found.
  • 09:12
    You first have to execute the program in order to load the dynamic library libc, which does contain a brk symbol.
  • 09:20
    And infact that is a regular function as a wrapper around the brk syscall.
  • 09:25
    So anything inside of libc would not directly do the syscall interrupt, it would call the internal brk function.
  • 09:33
    So that;s why we can easily set a breakpoint like this.
  • 09:37
    Long story short we can now continue and hit that breakpoint and then examine the function backtrace which tells us which functions have been called that lead to this brk call.
  • 09:47
    I clean that up a bit.
  • 09:49
    So here we go.
  • 09:50
    And as you can see it starts with IO_puts.
  • 09:54
    You can also look at the libc code for that stuff, I just pulled up some mirror of libc on github, and you can read the code there.
  • 10:01
    The reason why the function is not called puts, but IO_puts, eventhough we only use puts when we call it, has to do with a lot of C macros in libc.
  • 10:11
    I find it really difficult to read that code.
  • 10:14
    For example we know that the next function has the symbol name _IO_new_file_xsputn, but that doesn't show up in the C code.
  • 10:23
    But there is this similarely called IO_sputn, which when you look that up leads to a macro that says that it's actually IO_Xsputn.
  • 10:33
    Which itself is another macro that is JUMP2 with __xsputn as the first argument , and JUMP2 is obviously another macro.
  • 10:42
    And it just keeps going like that.
  • 10:44
    Feel free to do that on your own.
  • 10:46
    But if we trust our trace we can see that at some point it calls doallocbuffer.
  • 10:51
    And there is also a comment saying: “Allocate a buffer if needed”.
  • 10:55
    So this 1024 byte malloc has to do with the standard output buffer.
  • 11:01
    A printf doesn't immediatly result in a syscall write, but libc implements a lot of stuff like this in order to achieve higher performances by buffering output instead of waiting for files, or writing a few bigger chunks instead of a lot of small pieces.
  • 11:17
  • 11:18
    I would consider this a solved mystery.
  • 11:21
    Just a little excursion into the inner workings of programs.
  • 11:24
    I hope you liked that.
  • 11:25
    See you next week.

heap0 exploit speedrun & weird ASCII string on the Heap - bin 0x28

The heap0 example is not affected by DEP or ASLR on Ubuntu 16.04, so it's super easy. But we use the opportunity to investigate another weird string that we found on the heap.

Community tags: computer_security     HOME     SIGN UP     CONTACT US