Buffer overflow examples 03-10-2013, 11:30 PM
#1
Since I see a lot of peoples to write tutorials in internet for buffer overflow, when they even aren't familiar with the stack and copy from other tutorials I decide to give some examples I hope that programmers will understand them. The example are on C language
Example 1.
In this example I have create a char variable "buffer" with 8 bits size, this program should run something.exe, in this case the program should be vulnerable.
Example 2
In this example again I have create a char with name bufffer and bla bla bla.The intresting here is the function she copies a supplied string without bounds checking by
using strcpy() instead of strncpy(). You may get segmentation violation.
Example 3
Here I have create char with name bla bla bla... This simple will get a message from the user and put it on the screen. If the message is to large your application will be vulnerable.
How to avoid buffer overflow?
Those examples are only on buffer overflow but there are more vulnerabilities in C language like heap overflow.
Example 1.
Spoiler:
Code:
#include <lib 1>
#include <lib 2>
#include <lib ...>
char buffer[8];
char *program_to_run= "something.exe";
void main( char *args[] ) {
gets( buffer );
exec( program_to_run );
}In this example I have create a char variable "buffer" with 8 bits size, this program should run something.exe, in this case the program should be vulnerable.
Example 2
Spoiler:
Code:
#include <lib 1>
#include <lib ...>
void function(char *str) {
char buffer[16];
strcpy(buffer,str);
}
void main() {
char large_string[256];
int i;
for( i = 0; i < 255; i++)
large_string[i] = 'A';
function(large_string);
}using strcpy() instead of strncpy(). You may get segmentation violation.
Example 3
Spoiler:
Code:
#include <lib 1>
#include <lib ...>
void getInput(){
char buffer[8];
gets(buffer);
puts(buffer);
}
void main(){
getinput();
return 0;
}How to avoid buffer overflow?
Spoiler:
1. Avoid using library files included with the compiler. If an hacker find vulnerability in those files all applications that include them will be vulnerable.
2.Strlcpy and Strlcat. An alternative, being employed by OpenBSD, is the strlcpy(3) and strlcat(3) they are a minimalist, statically-sized buffer approach that provides C string copying and concatenation with a different (and less error-prone) interface.
Those are the only recommendations I can do for now, I'm not very experience developer I like more exploting them. I'm sure that you will be able to find help in coding forums.
2.Strlcpy and Strlcat. An alternative, being employed by OpenBSD, is the strlcpy(3) and strlcat(3) they are a minimalist, statically-sized buffer approach that provides C string copying and concatenation with a different (and less error-prone) interface.
Those are the only recommendations I can do for now, I'm not very experience developer I like more exploting them. I'm sure that you will be able to find help in coding forums.
Those examples are only on buffer overflow but there are more vulnerabilities in C language like heap overflow.




![[+]](https://sinister.li/images/modern/collapse_collapsed.png)


![[Image: Ov15OiO.png]](https://i.imgur.com/Ov15OiO.png)
![[Image: jWSyE88.png]](http://i.imgur.com/jWSyE88.png)


























