Difference between revisions of "Dangerous Function"

From OWASP
Jump to: navigation, search
(Examples)
(It doesn't seem the original author of this portion had good English so I rewrote it. My writing isn't that great but it's correct and more coherent.)
Line 27: Line 27:
 
}
 
}
  
it looks it is right , and yes it is  right program but it posses a sequrity problem.
+
It appears to be correct, but it posesses a security problem. If you enter a short piece of text such as "Hello", the above program works fine. But if the entered value is longer than the allocated buffer, such as the text "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", the program may crash due to a segfault, or otherwise behave inappropriately.
Lets see how ..
+
  
[running  1]
+
==What happens when the above program runs==
Enter Text : Hello
+
+
the above program works fine .
+
  
but if we give this :
+
cmain() calls the main() procedure.
  
[running 2]
+
'''In Assembly'''  
Enter Text : AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+
<pre>
 
+
_cmain proc far           /* In C, all function names begins with an underscore */
 
+
Here the program may crash  , as the text entered in of length 59 , but the array size is 25 .
+
as the "gets" function dosen't check for the length of array or entered text.
+
 
+
What happens when the above program runs
+
 
+
cmain() calls the main() .
+
'''in assembly'''  
+
 
+
_cmain proc far                   ; In C all function name begins with a "_"
+
xx
+
 
+
xx
+
  
 
xx
 
xx
  
 
call _main
 
call _main
 
xx
 
  
 
xx
 
xx
  
 
_cmain endp
 
_cmain endp
 
  
 
_main proc  
 
_main proc  
  
mov ebp,esp
+
mov ebp, esp
add ebp,19h ;''' 19h = 25 dec , *** thus it creates the buffer array in current stack frame   ***'''
+
add ebp, 19h               /* 19h = 25 dec.  Creates buffer array in the stack frame */
 +
lea eax, strEnterText      /* Gets the address of the string "\nEnter Text : " */
 +
push eax                  /* pushes this address to stack for printf function */
  
lea eax,strEnterText  ; gets the address of the string "\nEnter Text : "
+
call _printf              /* calls the printf function */
  
push eax                ;pushes this address to stack for printf function
 
  
call _printf        ; calls the printf function
+
push ebp                  /* pushes the address of the buffer array */
  
; next the interresting part
+
call _gets                /* calls gets()  */
  
push ebp         ; pushes the address of the buffer array
+
  /* The gets function stores the entered charecter from stdin to memory pointed by ebp */
  
call _gets        ;calls the gets() ,
+
   /*  if the number of charecter got from stdin is greater than the SIZE of the buffer */
 
+
  ;The gets function stores the entered charecter from stdin to memory pointed by ebp
+
   /* the buffer overflow occurs. */
 
+
   ; if the number of charecter got from stdin is greater than the SIZE of the buffer
+
 
+
   ; the buffer overflow occurs.  
+
  
 
xxx
 
xxx
  
xxx ; rest of the program
 
 
_main endp
 
_main endp
 +
</pre>
  
As all the return address of the functions are stored in stack , if "buffer" array is overflowed then the returned address of the functions are get OVERWRITTEN by the user inputed data.
+
As return addresses of all functions are stored on the stack, if the buffer array is overflowed, then the returned address of the functions may be overwritten by the user inputed data.
 
+
In ordinary case the input text is generaly text , so it normally crashes and terminates or behaves incorrectly.
+
 
+
'''Lets see how we can exploit this program ..'''
+
 
+
  
If we can some how know the bound of the array (size of the array) we can enter text as hex and give the text such a way when the array and stack overflows , then the returned address of the main function is overwritten to point to a code which is not intented to run , such as trojan code.
 
  
The len of the buffer is easy to know by trial and error .
+
==How this program can be exploited==
  
 +
Once the bounds of the array are known, text can be entered so that the return address on the stack is overwritten to point to a
 +
different location in memory which the program is not supposed to access at that time, such as other malicious code.
  
So u know how gets function can be exploited
+
The length of the buffer could be easily discovered through trial-and-error.
  
'''You should never use such a function which extracts data from users without a array bound check.
+
You should never use a function which extracts data from the user without no bounds checking.
A better alternative is "fgets".'''
+
A better alternative is "fgets".
  
To get input from stdin use this  
+
To get input from stdin use this:
  
 +
<pre>
 
fgets(buffer,25,stdin);
 
fgets(buffer,25,stdin);
 +
</pre>
  
 
==Related Threats==
 
==Related Threats==

Revision as of 05:48, 3 January 2007

This is a Vulnerability. To view all vulnerabilities, please see the Vulnerability Category page.

This article includes content generously donated to OWASP by Fortify.JPG.

Abstract

Functions that cannot be used safely should never be used.

Description

Certain functions behave in dangerous ways regardless of how they are used. Functions in this category were often implemented without taking security concerns into account.

Examples

  • The gets() function is unsafe because it does not perform bounds checking on the size of its input. An attacker can easily send arbitrarily-sized input to gets() and overflow the destination buffer.
  • The >> operator is unsafe to use when reading into a character buffer because it does not perform bounds checking on the size of its input. An attacker can easily send arbitrarily-sized input to the >> operator and overflow the destination buffer.

Examples

See this simple C program :

main() {

       char  buffer[25];
       
       printf("\nEnter Text : ");
       gets(buffer);

}

It appears to be correct, but it posesses a security problem. If you enter a short piece of text such as "Hello", the above program works fine. But if the entered value is longer than the allocated buffer, such as the text "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", the program may crash due to a segfault, or otherwise behave inappropriately.

What happens when the above program runs

cmain() calls the main() procedure.

In Assembly

_cmain proc far           /* In C, all function names begins with an underscore */

xx

call _main

xx

_cmain endp

_main proc 

mov ebp, esp
add ebp, 19h               /* 19h = 25 dec.  Creates buffer array in the stack frame */
lea eax, strEnterText      /* Gets the address of the string "\nEnter Text : " */
push eax                   /* pushes this address to stack for printf function */

call _printf               /* calls the printf function */


push ebp                   /* pushes the address of the buffer array */

call _gets                 /* calls gets()  */

   /* The gets function stores the entered charecter from stdin to memory pointed by ebp  */

   /*  if the number of charecter got from stdin is greater than the SIZE of the buffer */
 
   /* the buffer overflow occurs. */

xxx

_main endp

As return addresses of all functions are stored on the stack, if the buffer array is overflowed, then the returned address of the functions may be overwritten by the user inputed data.


How this program can be exploited

Once the bounds of the array are known, text can be entered so that the return address on the stack is overwritten to point to a different location in memory which the program is not supposed to access at that time, such as other malicious code.

The length of the buffer could be easily discovered through trial-and-error.

You should never use a function which extracts data from the user without no bounds checking. A better alternative is "fgets".

To get input from stdin use this:

fgets(buffer,25,stdin);

Related Threats

Related Attacks

Related Vulnerabilities

Related Countermeasures

Categories