Article

OWASP Guide to Building Secure Web Applications and Web Services, Chapter 17: Buffer Overflows

OWASP

This article is provided by special arrangement with the Open Web Application Security Project (OWASP). This article is covered by the Creative Commons

Share-Alike Attribution 2.5 license. You can find the latest version of this article and more free and open application security tools and documentation at http://www.owasp.org.


Buffer Overflows

Objective
To ensure that:

Platforms Affected
Almost every platform, with the following notable exceptions:

Relevant COBIT Topics
DS11.9 – Data processing integrity

Description
Attackers use buffer overflows to corrupt the execution stack of a web application. By sending carefully crafted input to a web application, an attacker can cause the web application to execute arbitrary code - effectively taking over the machine. Attackers have managed to identify buffer overflows in a staggering array of products and components.

Buffer overflow flaws can be present in both the web server or application server products that serve the static and dynamic aspects of the site, or the web application itself. Buffer overflows found in widely used server products are likely to become widely known and can pose a significant risk to users of these products. When web applications use libraries, such as a graphics library to generate images, they open themselves to potential buffer overflow attacks. Literature on the topic of buffer overflows against widely used products is widely available.

Buffer overflows are found in custom web application code, and may even be more likely given the lack of scrutiny that web applications typically go through. Buffer overflow attacks against customized web applications can sometimes lead to interesting results. In some cases, we have discovered that sending large inputs can cause the web application or the back-end database to malfunction. It is possible to cause a denial of service attack against the web site, depending on the severity and type of the flaw. Over-large inputs may cause the application to output a detailed error message that may lead to a successful attack on the system.

Stack Overflow
Stack overflows are the best understood and the most common form of "buffer" overflows. The basics of stack overflows are simple:

How to determine if you are vulnerable
If your program:

It is likely that the application is vulnerable to attack.

How to protect yourself

Heap Overflow
Heap overflows are problematic are they are not necessarily protected by CPUs capable of configuring no execute stacks. A heap is an area of memory allocated by the application run time to store locally declared variables.

function foo(char *bar) {
char thingy[128];
…
}

"bar" is passed via the stack, whereas "thingy" is allocated on the heap. The overflow possibilities are exploitable in exactly the same fashion as stack overflows.

How to determine if you are vulnerable
If your program:

It is likely that the application is vulnerable to attack.

How to protect yourself

Format String
Format string buffer overflows are caused when the user inputs something similar to:

%08x.%08x.%08x.%08x.%08xn

The above attack string will print the first five entries on the stack. Format strings are highly specialized buffer overflows and can be used to perform all the same types of attacks, including complete remote compromise.

How to determine if you are vulnerable
If your program:

It is highly likely that the application is vulnerable to attack.

How to protect yourself

Unicode Overflow
Unicode exploits are a bit more difficult to do than typical buffer overflows as demonstrated in Anley's 2002 paper, but it is wrong to assume that by using Unicode, you are protected against buffer overflows. Examples of Unicode overflows include Code Red, which is a devastating Trojan.

How to determine if you are vulnerable
If your program:

It is likely that the application is vulnerable to attack.

How to protect yourself

For your custom application code, you need to review all code that accepts input from untrusted sources, and ensure that it provides appropriate size checking on all such inputs.

This should be done even for environments that are not susceptible to such attacks as overly large inputs that are uncaught may still cause denial of service or other operational problems.

Integer Overflow
When an application takes two numbers of fixed word size and perform an operation with them, the result may not fit within the same word size. For example, if two 8 bit numbers 192 and 208 are added together and stored into another 8-bit byte, the result will simply not fit into the 8 bit result:

 % 1100 0000
+ % 1101 0000
= % 0001 1001 0000

The top most half word is thrown away, and the remnant is not a valid result. This can be a problem for any language. For example, many hexadecimal conversions will "successfully" convert %M0 to 192. Other areas of concern include array indices and implicit short math.

How to determine if you are vulnerable

How to protect yourself

Further reading


Join the conversationComment

Share
Comments

    Results

    Contribute to the conversation

    All fields are required. Comments will appear at the bottom of the article.