Thursday, July 17, 2014

Buffer OverFlow Attacks!!!!

Let's see what a buffer attack is and the bad programming practices that can result in this attack

Let's dive right in now

Let's be clear with the following basics:
Statistical memory allocation is done on stack which happens during compilation of a program where as dynamic memory allocation is done on a heap which happens during run time
Now let's come to the segmentation fault.., this is one of the most common type of error that most of the programmer run into.., Let's checkout the reason for this error.., Operating System allocates some memory called stack(during compile time) and some memory called heap(during run time this usually happens in dynamic memory allocation) when you are trying to access a memory unit outside the heap memory or stack memory during compile time this results in a segmentation fault which is an error thrown by the OS
Now Let's come to Buffer Overflow Attacks.., Suppose if you use functions like gets(var) and many other which doesn't check for the overflow Lets check the following example which can tell you about the intensity of the attacks of Buffer Overflow

Include stdio.h and string.h libraries
int main(void) {
char buff[15];
int pass = 0;
printf("\n Enter the password : \n");
gets(buff); if(strcmp(buff, "thegeekstuff"))
{
printf ("\n Wrong Password \n");
}
else {

printf ("\n Correct Password \n");
pass = 1;
}
if(pass) {

/* Now Give root or admin rights to user*/
printf ("\n Root privileges given to the user \n");
}
return 0; }
Input 1
$ ./bfrovrflw
Enter the password :
thegeekstuff

Correct Password

Root privileges given to the user
Input 2
$ ./bfrovrflw
Enter the password :
hhhhhhhhhhhhhhhhhhhh
Wrong Password
Root privileges given to the user

By this time if you have gone through the above code carefully you should have got the intensity of Buffer Overflow attacks actually here in the second case though the statistical memory allocated on stack was only for 15 characters but you have overridden the next few characters in the heap with the string and these few locations happen to be the memory allocations of pass whose values are overridden hence though the password was incorrect pass has got a value which is non zero and the condition is passed and if this code actually grants users permissions then All Root Previleges must have gone to the malicious user


No comments:

Post a Comment