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?)
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;
}
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).
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.
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.
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
.
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.