Buffer overflow is a very common and very dangerous loophole, which is widely used in various operating systems and applications. Buffer overflow attacks can cause programs to fail, system downtime, restart, and so on. What is more serious is that it can be used to execute unauthorized instructions, and even gain system privileges, thereby carrying out various illegal operations.
Encyclopedia Link Http://baike.baidu.com/link?url=TsOHEk1TuHmR7tRtNg0FN3bOWW5_AkUz-OJn_ 6lzbspit2qk3rtwot1i1smxmu2uvcm8snhs9d3b001x6fb0w7msm82pdrqcb4-krli7lu8xwjd8rufycb8lvmjtf1ezvr2emhf22dvmrspgy-cyqk
Here's an example to see in detail what a buffer overflow is. The buffer of the program is like a lattice, each of the different things in the lattice, some commands, some data, when the program needs to receive user data, the program is allocated 4 lattice (the yellow 0~3 in the following figure 1). According to the program design, the user is required to enter no more than 4 data. And when the user enters the data, assuming that 16 data is entered, and that the program does not check the number of user input data, it is stored in a pre-configured grid so that not only 4 of the allocated squares are used, but the contents of the adjacent 12 squares are overwritten by the new data. So the contents of the original 12 squares are lost. Then there is the buffer (0~3 number lattice) overflow.
On the basis of the example above, a code instance is as follows:
void function (char *str) {
Char buffer[16];
strcpy (BUFFER,STR);
}
The above strcpy () will copy the contents of STR directly into the buffer. So as long as the length of STR is greater than 16, it will overflow the buffer, causing the program to run wrong. The standard functions for problems such as strcpy are strcat (), sprintf (), vsprintf (), gets (), scanf (), and so on.
Of course, just fill in the buffer to make it overflow usually only "segmented error" (segmentation fault), but not to achieve the purpose of the attack. The most common means of making a program run a user shell by making a buffer overflow, and then executing other commands through the shell. If the program has root or suid execution privileges, the attacker would have a shell with root privileges, and the system could be manipulated arbitrarily.
Let's take a look at strcpy's implementation.
//C language standard library function strcpy A typical industrial-level minimalist implementation.
//Return value: The address of the destination string.
//The exception is not defined by the ANSI-C99 standard, so the implementation determines the return value, usually null.
//Parameter: des is the target string and source is the original string.
char* strcpy (char* des,const char* source)
{
char* r=des;
assert (des!= null) && (source!= null));
while ((*des++ = *source++)!= ' ");
return r;
}
while ((*des++=*source++)); interpretation: The assignment expression returns the left-hand operand, so after the assignment ', ' the loop stops.
In other words, the string Terminator ' "is also assigned to the DES's pointing
If we do this:
Char *str1= "0123456789";
Char string[10];
strcpy (STRING,STR1);
So in fact we're going to assign a ' string[11 ' to the
Because such operations do not affect the nature of a string character array, compilation does not complain. So we need to pay extra attention.
Otherwise, the consequences will be unimaginable ~