What is a buffer overflow and how do I cause one?

前端 未结 12 1879
盖世英雄少女心
盖世英雄少女心 2020-11-30 00:43

I have heard about a buffer overflow and I would like to know how to cause one.

Can someone show me a small buffer overflow example? New(And what they are used for?)

相关标签:
12条回答
  • 2020-11-30 01:21

    A buffer overflow is just writing past the end of a buffer:

    int main(int argc, const char* argv[])
    {
        char buf[10];
        memset(buf, 0, 11);
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-30 01:21

    The "classic" buffer overflow example is:

    int main(int argc, char *argv[])
    {
        char buffer[10];
        strcpy(buffer, argv[1]);
    }
    

    That lets you play with the buffer overflow parameters and tweak them to your hearts content. The book "Hacking - The Art of Exploitation" (Link goes to Amazon) goes into great detail about how to play around with buffer overflows (purely as an intellectual exercise obviously).

    0 讨论(0)
  • 2020-11-30 01:24

    A buffer overflow is basically when a crafted section (or buffer) of memory is written outside of its intended bounds. If an attacker can manage to make this happen from outside of a program it can cause security problems as it could potentially allow them to manipulate arbitrary memory locations, although many modern operating systems protect against the worst cases of this.

    While both reading and writing outside of the intended bounds are generally considered a bad idea, the term "buffer overflow" is generally reserved for writing outside the bounds, as this can cause an attacker to easily modify the way your code runs. There is a good article on Wikipedia about buffer overflows and the various ways they can be used for exploits.

    In terms of how you could program one yourself, it would be a simple matter of:

    char a[4];
    strcpy(a,"a string longer than 4 characters"); // write past end of buffer (buffer overflow)
    printf("%s\n",a[6]); // read past end of buffer (also not a good idea)
    

    Whether that compiles and what happens when it runs would probably depend on your operating system and compiler.

    0 讨论(0)
  • 2020-11-30 01:27

    If you want to check you program for buffer overflows, you could run it with tools like Valgrind. They will find some memory management bugs for you.

    0 讨论(0)
  • 2020-11-30 01:28

    This is a general comment about the answers you received. For example:

    int main(int argc, char *argv[])
    {
        char buffer[10];
        strcpy(buffer, argv[1]);
    }
    

    And:

    int main(int argc, const char* argv[])
    {
        char buf[10];
        memset(buf, 0, 11);
        return 0;
    }
    

    On modern Linux platforms, this may not work as expected or intended. It may not work because of the FORTIFY_SOURCE security feature.

    FORTIFY_SOURCE uses "safer" variants of high risk functions like memcpy and strcpy. The compiler uses the safer variants when it can deduce the destination buffer size. If the copy would exceed the destination buffer size, then the program calls abort().

    To disable FORTIFY_SOURCE for your testing, you should compile the program with -U_FORTIFY_SOURCE or -D_FORTIFY_SOURCE=0.

    0 讨论(0)
  • 2020-11-30 01:30

    With the correct answers given: To get more into this topic, you might want to listen to the Podcast Security Now. In Episode 39 (a while back) they discussed this in depth. This is a quick way to get a deeper understanding without requiring to digest a whole book.

    (At the link you'll find the archive with multiple size versions as well as a transcript, if you're rather visually oriented). Audio is not the perfect medium for this topic but Steve is working wonders to deal with this.

    0 讨论(0)
提交回复
热议问题