Testing for Format String

Go back to Testing_for_buffer_overflow

Introduction
Various C-Style languages provision formatting of output by means of functions like printf, fprintf etc.

Formatting is governed by a parameter to these functions termed as format type specifier, typically %s, %c etc.

The vulnerability arises on account of format functions being called with inadequate parameters and user controlled Data.

A simple example would be printf(argv[1]). In this case the type specifier has not been explicitly declared, allowing a user to pass characters such %s, %n, %x to the application by means of command line argument argv[1].

This situation tends to become precarious on account of the fact that a user who can supply format specifiers can perform the following malicious actions:

Enumerate process Stack: This allows an adversary to view stack organization of the vulnerable process by supplying format strings such as %x or %p, which can lead to leakage of sensitive information. It can also be used to extract canary values when the application is protected with a stack protection mechanism. Coupled with a stack overflow, this information can be used to bypass the stack protector.

Control Execution Flow: This vulnerability can also facilitate arbitrary code execution since it allows writing 4 bytes of data to an address supplied by the adversary. The specifier %n comes handy for overwriting various function pointers in memory with address of the malicious payload. When these overwritten function pointers get called, execution passes to the malicious code.

Denial of Service: In case the adversary is not in a position to supply malicious code for execution, the vulnerable application can be crashed by supplying a sequence of %x followed by %n.

Black Box
The key to testing format string vulnerabilities is supplying format type specifiers in application input.

For example, consider an application that processes the URL string http://xyzhost.com/html/en/index.htm or accepts inputs from forms. If format string vulnerability exists in one of the routines processing this information, supplying a URL like http://xyzhost.com/html/en/index.htm%n%n%n or passing %n in one of the form fields might crash the application creating a core dump in the hosting folder.

Format string vulnerabilities manifest mainly in web servers, application servers or web applications utilizing C/C++ based code or CGI scripts written in C. In most of these cases an error reporting or logging function like syslog has been called insecurely.

When testing CGI scripts for format string vulnerabilities, the input parameters can be manipulated to include %x or %n type specifiers. For example a legitimate request like

http://hostname/cgi-bin/query.cgi?name=john&code=45765 

can be altered to

http://hostname/cgi-bin/query.cgi?name=john%x.%x.%x&code=45765%x.%x

In case a format string vulnerability exists in the routine processing this request, the tester will be able to see stack data being printed out to browser. In case of unavailability of code, the process of reviewing assembly fragments (also known as reverse engineering binaries) would yield substantial information about format string bugs.

Take the instance of code (1) :

int main(int argc, char **argv) { printf("The string entered is\n"); printf(“%s”,argv[1]); return 0; }

when the disassembly is examined using IDA Pro, the address of a format type specifier being pushed on the stack is clearly visible before a call to printf is made.



On the other hand when the same code is compiled without “%s” as an argument, the variation in assembly is apparent. As seen below, there is no offset being pushed on the stack before calling printf.



White Box
While performing code reviews, nearly all format string vulnerabilities can be detected by use of static code analysis tools. Subjecting the code shown in (1) to ITS4, which is a static code analysis tool, gives the following output.



The functions that are primarily responsible for format string vulnerabilities are ones that treat format specifiers as optional. Therefore when manually reviewing code, emphasis can be given to functions such as:

Printf Fprintf Sprintf Snprintf Vfprintf Vprintf Vsprintf Vsnprintf

There can be several formatting functions that are specific to the development platform. These should also be reviewed for absence of format strings once their argument usage has been understood.

Whitepapers
http://comsec.theclerk.com/CISSP/FormatString.pdf
 * A paper on format string attacks by Tim Newsham

Tools
http://www.cigital.com/its4/ http://www.datarescue.com/idabase/ http://seclists.org/lists/pen-test/2001/Aug/0014.html
 * ITS4, A static code analysis tool for identifying format string vulnerabilities using source code
 * A disassembler for analyzing format bugs in assembly
 * An exploit string builder for format bugs

OWASP Testing Guide v2 Table of Contents