Bruteforce 32bit Stack Cookie. stack0: part 3 - bin 0x23

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ต.ค. 2024
  • Bruteforcing stack canary, stack guard, stack cookie with a C program.
    brute_cookie.c: github.com/Liv...
    DigitalOcean (referral):* m.do.co/c/826f...
    =[ 🔴 Stuff I use ]=
    → Microphone:* geni.us/ntg3b
    → Graphics tablet:* geni.us/wacom-...
    → Camera#1 for streaming:* geni.us/sony-c...
    → Lens for streaming:* geni.us/sony-l...
    → Connect Camera#1 to PC:* geni.us/cam-link
    → Keyboard:* geni.us/mech-k...
    → Old Microphone:* geni.us/mic-at...
    US Store Front:* www.amazon.com...
    =[ ❤️ Support ]=
    → per Video: / liveoverflow
    → per Month: / @liveoverflow
    =[ 🐕 Social ]=
    → Twitter: / liveoverflow
    → Website: liveoverflow.com/
    → Subreddit: / liveoverflow
    → Facebook: / liveoverflow
    =[ 📄 P.S. ]=
    All links with "*" are affiliate links.
    LiveOverflow / Security Flag GmbH is part of the Amazon Affiliate Partner Programm.
    #BinaryEXploitation #BufferOverflow

ความคิดเห็น • 94

  • @thefrozenindie2056
    @thefrozenindie2056 7 ปีที่แล้ว +24

    You Asking for our ways ?! I mean before the video i though i was an expert at C but after it turned out i was still a baby in C :D

  • @travisjayday5757
    @travisjayday5757 4 ปีที่แล้ว +14

    “Ghetto parallelization” made my day xD

  • @typedeaf
    @typedeaf 5 ปีที่แล้ว +4

    Dude, that program is sick. I am absolutely going to steal that code and reuse bits and pieces. I love how you included some of the iterations of the code. I wish this video would have been longer and covered why you made the changes, your thought process, and the various trials and errors. Excellent video!

  • @tigoes
    @tigoes 3 ปีที่แล้ว +8

    if the cookie is already random, why do you have to randomize the input while brutforcing? Or am i missing something?
    If the cookie is really random, sending the same value as argument has same (or even more) chances find match, without considering the performance penalty of random generation that will slowdown the brutforce. Except if the generator has some kind of non uniform distribution.

    • @irmgardbaty288
      @irmgardbaty288 2 ปีที่แล้ว +1

      now THIS is what I'm looking for.
      programming might not be mathematics, but exploit development certainly is. Time to go learn some probability.

  • @alexhdz.9230
    @alexhdz.9230 7 ปีที่แล้ว +8

    Top tier videos as always, cheers.

  • @rootabeta9015
    @rootabeta9015 5 ปีที่แล้ว +9

    3:04 "And I was like, 'That process had a child'"

  • @TonelessR
    @TonelessR 7 ปีที่แล้ว +1

    Cool, actually had a good deal of info on things I was curious about! And I love C programs :P

  • @sahilsharma-hj4gq
    @sahilsharma-hj4gq 3 ปีที่แล้ว

    bro... this video is just WTF... I was like.. do I even know any programming at all? great job man

  • @NikiforGeorgiev
    @NikiforGeorgiev 4 ปีที่แล้ว +1

    My wife was like "What is this sh*t you're watching about killing children???" xD

  • @jauleris
    @jauleris 6 ปีที่แล้ว +1

    I have not tested this so I might be wrong, but I believe it would be possible to inspect "status" argument of waitpid call and determine what signal killed child process. I think stack smashing detection should use SIGABRT, and in the case when we correctly guess, SIGSEGV occurs.
    This might speed things a little bit as there is no need for string operations with standard streams.

  • @andreab5185
    @andreab5185 7 ปีที่แล้ว +22

    For all the comments "wooow" that's a super pro level of coding in C etc etc those are just normal skills you can acquire studying in university and in particular operative systems. For the rest, kudos!! I like all your videos :):)

    • @nombreapellidos1094
      @nombreapellidos1094 7 ปีที่แล้ว

      Recommend books

    • @chuckn2x
      @chuckn2x 6 ปีที่แล้ว +4

      Can confirm. Am enrolled in a mediocre CS program and don't think any of our undergraduate classes teach this stuff. Even our Operating Systems class uses some shitty Java OS emulator.

    • @douwehuysmans5959
      @douwehuysmans5959 5 ปีที่แล้ว

      @@nombreapellidos1094 "The C programming language" and "Hacking the art of exploitation"

    • @douwehuysmans5959
      @douwehuysmans5959 5 ปีที่แล้ว

      We had to program a shell for a course

    • @typedeaf
      @typedeaf 5 ปีที่แล้ว

      I agree that the code is 'wow'. I suspect that he has contacts, colleagues, ctf team members, etc. that he can lean on for some expert suggestions/advice. That program gives me something to study hours on. I totally don't get how he executed 8 of them with logical AND's (&) and somehow gathered results cumulatively.

  • @ndm13
    @ndm13 7 ปีที่แล้ว +4

    Could you try to find patterns or correlations in the stack cookie random data? Unless it's backed by a powerful RNG, there's gotta be some weakness, right? Maybe it's just me being ignorant.

    • @mina86
      @mina86 6 ปีที่แล้ว +6

      It’s backed by kernel’s RNG which is feed real entropy (from random hardware events) plus uses a cryptographically secure algorithm.

    • @Littlefighter1911
      @Littlefighter1911 2 ปีที่แล้ว

      You're probably right.
      Most devices do NOT come with a True RNG source, afaik
      and instead have to rely on daemons such as haveged to feed the RNG source.
      It's better than nothing and makes it a lot harder, but it's not unbeatable.
      There has been some research where PUFs have been "cracked" using AI learning.
      So maybe the same approach can be used for exploiting the RNG used in Linux kernel.
      And even if the kernel actually was to use "entropy" from the real world, it would be quite limited.
      Keyboard, mouse, camera, microphone, CPU temperature or usage and hard drive aren't good entropy sources to begin with.

  • @cyancoyote7366
    @cyancoyote7366 7 ปีที่แล้ว +3

    Another great and useful video (:

  • @triularity
    @triularity 2 ปีที่แล้ว

    @10:01 - That might not be completely true in this context. Typically (in the unix/posix world), the child will remain sitting around as a zombie process until the parent has reaped it (via wait*()). So until waitpid() returns > 0, the child should either still be running or in a zombie state. However, I didn't notice a condition where it exited the loop for such a condition, so would probably trigger the (wait_ret==0 ...) in a later loop, as the child would be gone then.
    Also, a pid is probably not reused right away (e.g. round-robin assignment). So unless there are tens of thousands (or billions with 32bit+ pids) of processes spawned on the system in that short window, chances are good it wouldn't collided with another process in any case.

  • @idkfkingknowlmao
    @idkfkingknowlmao 7 ปีที่แล้ว +1

    You amaze me every fucking time

  • @mrpi230
    @mrpi230 2 ปีที่แล้ว

    Thank You

  • @nic0latesla333
    @nic0latesla333 6 ปีที่แล้ว +1

    you're amazing

  • @mikemerinoff
    @mikemerinoff 4 ปีที่แล้ว +1

    Can we add some concurrent programming here? A pool of 50 processes may speed up things i think

    • @filipj4377
      @filipj4377 2 ปีที่แล้ว

      I think so, and it would be much easier to add that in python rather than fiddle with C

  • @jonatanahnh
    @jonatanahnh 7 ปีที่แล้ว +8

    I don’t understand why not using a const value for the stack cookie.
    For example if I’m dropping a coin and always picking tales the chances are 1/2 but if I’ll randomly pick the minimum chance will be 1/1 while the max will be 1/infinite is there some mathematical prof that supports picking a random value for this ? This is not a typical brutforce because the value is randomized.

    • @LiveOverflow
      @LiveOverflow  7 ปีที่แล้ว +2

      I don't understand what you are saying. If it's a constant value this exploit would have only taken like 256*3=768 executions to find the correct cookie. Because we would have been able to try byte by byte. Once we find a valid byte of the cookie, we move to the next one.

    • @LiveOverflow
      @LiveOverflow  7 ปีที่แล้ว +2

      also about your coin example, picking a random side will still have the chance of 1/2. The exact same chance as if you only pick tales. It's no different ;)

    • @d5511282
      @d5511282 7 ปีที่แล้ว +1

      I think I get what you are trying to say - why not use a valid, constant value for the cookie but randomize every byte while bruteforcing?
      If that's what you meant - it doesn't matter probability wise (you would still have the same chance of hitting the correct cookie because of symmetry), but it is a tiny bit easier to code :)

    • @jonatanahnh
      @jonatanahnh 7 ปีที่แล้ว

      Damn so if the probability remains the same then the optimization will be better using a constant value and i still don’t get it why it remains the same first of all it always tricks me how computers generates random numbers as far as I know in Linux system the random number generation is done by collecting allot of information about running programs which means in any given point at this space the value is not a pure random which makes sense because we know it’s pseudo random but that also means that it would be very hard to randomize the same number or rather very simple.
      So this solution is not that accurate and not that optimized as far as I understand

    • @LiveOverflow
      @LiveOverflow  7 ปีที่แล้ว +1

      +jonatan no a constant cookie value will be bad in this case, as you can then guess each single byte individually.
      And the value does not remain the same in our example, it's random each execution. I don't know what you mean :S

  • @ziul911
    @ziul911 7 ปีที่แล้ว +4

    I think there is an easier way to do that. The main idea is not to bruteforce the 3 bytes of the cannary (those differents from the lsB 0x00) at the same time, but one by one. for example, you have a buffer with 32 bytes then, if you send a message to this buffer with "A" * 32 and a 0x00 byte at the end to the "server", it won't return you "try again". Then, the next string you will send will be "A" * 32 + "\0x00" + . That way you'll move on bruteforcing the bytes and every time you get that try again you'll increment that byte if you don't receive it, you'll move to the next byte.
    So, instead of 256*256*256 possible combinations you will have 256 + 256 + 256.
    I'm working on an exploit that does this thing right now ; )
    Thanks for the great content as usual.

    • @d5511282
      @d5511282 7 ปีที่แล้ว +18

      It won't work, since the cookie is randomized on every run - if you found one of the bytes in a certain run, it won't be correct on the next one :

  • @bjarnestronstrup9122
    @bjarnestronstrup9122 5 ปีที่แล้ว

    If you can directly execute the program to brute force the stack cookie, you might as well write a debugger which would read the cookie from the memory. I guess brute force is a option in situations where you cannot execute the application directly .

    • @Marco-vv1pf
      @Marco-vv1pf 3 ปีที่แล้ว +1

      You need CAP_SYS_PTRACE for that. Executing another program != being able to debug it.

  • @bakali1999
    @bakali1999 7 ปีที่แล้ว +1

    I have all the time the feeling that you are from germany because you have a german like accent :D. But nice vids btw :D

  • @mina86
    @mina86 6 ปีที่แล้ว +4

    10:07 - that’s not accurate. When a process dies it becomes a zombie and hangs in that state until its parents collects its status through waitpid system call. Since you went with a signal handler, the correct way to approach this would be to have a single nanosleep in the main loop and a signal handler which would atomically set some global flag. The main loop would then wait for the sleep time to run out (in which case the process can be killed) or for it to be stopped with an interrupt (in which case check the global flag and collect the status of the process).

    • @pqnet84
      @pqnet84 5 ปีที่แล้ว

      so if you check for a random PID you may have race conditions, but if you check for a child PID you may be sure that there is no such problem because you are responsible for cleaning up before the same PID number can be reused right?

  • @nombreapellidos1094
    @nombreapellidos1094 7 ปีที่แล้ว +3

    Can you recommend me any low level C book?

    • @typedeaf
      @typedeaf 5 ปีที่แล้ว

      Start by reading the K&R C book, then get the book by Michael Kerrisk ; it is the modern day Stevens equivalent.

  • @finesseandstyle
    @finesseandstyle ปีที่แล้ว

    Couldn't you just use system() instead of execv since it creates a new process? Using execv and all the forking seems a bit of over-engineering to me unless I'm missing something about those 2

  • @fredxu9826
    @fredxu9826 6 ปีที่แล้ว +1

    Just want to ask: is this a random canary (random value everytime the program runs)? or is it a fixed canary value that you are trying to bruteforce?

    • @LiveOverflow
      @LiveOverflow  6 ปีที่แล้ว +3

      random with every program run

    • @pqnet84
      @pqnet84 5 ปีที่แล้ว

      @@LiveOverflow does it stays the sames throughout the whole program execution? Could it be possible to get the canary value at some point during program execution and exploit it later (with two different attack surfaces on the same executable)?

    • @EvanED
      @EvanED 5 ปีที่แล้ว +2

      @@pqnet84 This is an old comment, but yes, that's correct. I'm sure my knowledge here is incomplete and I worry I'm in "a little knowledge is a dangerous thing" territory, but my impression is the main ways of exploiting a stack canary rely on this. The impression I have is there are two common stack canary circumventions. The first is to use a memory disclosure vulnerability (e.g. a buffer overread) to just read the canary out of the process, then turn around and use that in a subsequent injection attempt.
      The second way actually uses an even stronger property -- canaries don't even change when a process is *forked*, just exec'ed. (The child process will continue running as if it were the parent, and that will have canaries sprinkled throughout the stack; if the canary changed, either the child would break when it returns from a function that uses the old canary value, or it would somehow have to know what stack addresses hold canary values that would need to be updated.) What this means is that if you have a server process that processes requests by forking and then handling the request in a subprocess, and if you can detect a subprocess crash (from a bad canary) from continuing execution, you can brute force the canary one byte at a time. Even on a 64-bit machine, that means a maximum of 8 * 256 = 1,024 attempts to carry out the brute force.

    • @purplelix8357
      @purplelix8357 3 ปีที่แล้ว

      @@EvanED This is an old comment, but are you sure about the brute forcing one byte at a time part? I mean most of the time the copying function will put a nullbyte at the end of your input. So the canary will most likely always be wrong unless you bruteforce the whole thing at once. Or maybe I'm missing something ...

    • @EvanED
      @EvanED 3 ปีที่แล้ว +1

      @@purplelix8357 I mean, that depends on what the underlying problem is. You're right that you wouldn't be able to do that if it's like a `strcpy` problem, but there are plenty of others. `memcpy` won't null terminate, `read`/`recv` etc. won't null terminate, or a hand-written loop in the code won't necessarily null terminate. I've *done* this with an nginx vulnerability. :-)

  • @aidenpearce5701
    @aidenpearce5701 6 ปีที่แล้ว

    Can you make a video about "Ret 2 dl-resolve" ?

  • @xcy0n
    @xcy0n 7 ปีที่แล้ว

    What's more promising for bruteforcing? 3 random bytes each run or enumerating the 3-byte number space?

    • @LiveOverflow
      @LiveOverflow  7 ปีที่แล้ว +5

      +Vanilla Thunder I think in this case I should have just used a static value. I didn't think :P

    • @xcy0n
      @xcy0n 7 ปีที่แล้ว

      Oooh.. yes, that's also possible >_>

  • @rushinc9512
    @rushinc9512 6 ปีที่แล้ว

    can we use structured exception handling!

  • @HugRunner
    @HugRunner 6 ปีที่แล้ว

    It was kind of hard to follow the code exactly, and yeah I know I could look at the github, but when you randomize the cookie guesses, I don't see anything that prevents the same combination being tried several times. Seems like you just try random combinations, but maybe I'm wrong. Edit: Or is that because of every time you try a combination you have to reset the program and thus have a new cookie, possibly the one that was wrong on last attempt?

    • @neloka4313
      @neloka4313 6 ปีที่แล้ว +1

      Why would he try unique combinations each time? We know that the stack cookie is randomly generated and we can think that the randomization is independant of the previous ones. Imagine flipping a coin and trying to guess what side you'll get, the best strategy would be to guess randomly each time without tracking previous guesses, it doesn't matter if the coin felt 1000 times on heads, the next time you will still have a half chance of getting heads or tails.

  • @alell1819
    @alell1819 7 ปีที่แล้ว +3

    Bro serious question where did you learn all that skills i'm really need to know ? hope you answer me of any topis or articles should i read it or practice it

    • @LiveOverflow
      @LiveOverflow  7 ปีที่แล้ว +5

      +Ale ll i learned most of the basics by playing exploit-exercises.com myself

    • @alell1819
      @alell1819 7 ปีที่แล้ว

      Thank you sir for your reply i really apprecaite it
      for me now the essential is how to find a 0day vulnerbalities in php scripts and in OS linux (no more questions sir thank you ) where i can get skills to get what i'm looking for

  • @igorgiuseppe1862
    @igorgiuseppe1862 6 ปีที่แล้ว

    10:00
    why not pkill?

  • @mahdiarfaramarzpoor
    @mahdiarfaramarzpoor 7 ปีที่แล้ว

    first: if you are not expert then who is?! really i mean whats that like ?
    second: really ? one video in week ? i know your kinda busy with work and this kinda tutorials take so mush time. but PLZ make more. i'm really wait for your videos and count days. so yeah. make more . thanks

    • @LiveOverflow
      @LiveOverflow  7 ปีที่แล้ว +1

      +mahdiar faramarzpoor "kinda busy with work"? Hahahha

    • @mahdiarfaramarzpoor
      @mahdiarfaramarzpoor 7 ปีที่แล้ว +1

      whats funny about that?! :|

  • @markw5520
    @markw5520 6 ปีที่แล้ว

    So if you have to brute force the stack cookie in order to get an exploit, and it seems that the 64 bit cookie is not brute forcible, is it even possible to exploit code on 64 bit systems? And if it is, how would you get around the stack cookie?

    • @LiveOverflow
      @LiveOverflow  6 ปีที่แล้ว +2

      This simple example is not possible. But I’m more complex applications you might find bugs to leak stackcookies or Just other vulns. Stack cookies protect only against one thing.

  • @otkchk
    @otkchk 7 ปีที่แล้ว

    What kind of calculator are you using on mac machine? Default doesn't understand signed values.

    • @otkchk
      @otkchk 7 ปีที่แล้ว

      Just found info about 2'nd completement, but still, what if I want to work with 32 or 16 bit integers.

    • @otkchk
      @otkchk 7 ปีที่แล้ว +1

      BTW, first line of your exploit on github got corrupted. Probably because of vim's "i"-input mode switching
      nclude

  • @filipj4377
    @filipj4377 2 ปีที่แล้ว

    Did anyone manage to do this against 64x system?

  • @007order007
    @007order007 7 ปีที่แล้ว

    [assembly noob here] Wouldn't a popad and pushad prevent the crash all together if you just inject a huge NOP sled? Or am I just that stupid

    • @LiveOverflow
      @LiveOverflow  7 ปีที่แล้ว

      +007order007 I'm not sure if I understand. What's up with the push and pop? Also why a nopslide? The stack is not executable, we can't inject instructions ;)

  • @luisito7018
    @luisito7018 6 ปีที่แล้ว

    WOW

  • @thezar86
    @thezar86 3 ปีที่แล้ว

    Got lost XD

  • @cjreek
    @cjreek 7 ปีที่แล้ว +1

    Stupid question: Do we even gain anything by doing it this way? we don't get any more privileges because we are launching the target executable from our own low-privilege code, do we? So the RESULT is essentially the same if we attach gdb and modify the cookie from inside the debugger, isn't it?
    Or am I missing something?

    • @LiveOverflow
      @LiveOverflow  7 ปีที่แล้ว +9

      no privileges are kept when doing exec. Otherwise you couldn't exec a setuid program from your shell ;)
      gdb requires the ptrace syscall (and more), which the kernel doesn't allow on processes you don't have privileges to. So gdb drops them before exec.

    • @cjreek
      @cjreek 7 ปีที่แล้ว

      That's a good point :D Thanks for the explanation!

  • @nombreapellidos1094
    @nombreapellidos1094 7 ปีที่แล้ว

    What's your job? And your mother lenguage?

    • @LiveOverflow
      @LiveOverflow  7 ปีที่แล้ว

      +Nombre Apellidos security stuff. German

  • @devotee9606
    @devotee9606 4 ปีที่แล้ว +1

    🤐🤐😥😥😮😮

  • @nathanmack10
    @nathanmack10 7 ปีที่แล้ว

    For brute forcing something like this, I would suggest using a GPU for speed

    • @LiveOverflow
      @LiveOverflow  7 ปีที่แล้ว +8

      +Nathan Mack doesn't make any sense because this is not something you can do on a GPU. We are not bruting a hash or so.

  • @0xp1nk66
    @0xp1nk66 7 ปีที่แล้ว

    First! :D