Format String Vulnerability
Introduction:
Format string vulnerability is a security flaw that can occur in applications written in C or C++ when improper use of format specifiers leads to unexpected behavior.
Format String:
Specific string used as an argument to a function in programming languages like C or C++. Example of format string is given below
Format Specifiers:
Special sequence in a format string that specifies how a particular argument should be formatted when it is printed or read. According to example given below, %d tells the function to print a digit from variable num.
Some common format specifiers are given below
%d — Decimal
%x — Hexadecimal
%p — Pointer
%s — String
%n — number of bytes
%c — Character
Note:
In %s and %n, arguments are passed as reference
%p vs %x:
%x prints hex values like 0ff23cd
%p prints hex values like 0xff23cd
Get A1iw4r3’s stories in your inbox
Join Medium for free to get updates from this writer.
Arguments:
Argument after the format string is a variable or a list of variables that are used to fill in the placeholders specified by the format specifiers in the format string.
Format String Vulnerability Exploitation:
Consider the following program segment: a character array named name of size 100 bytes is declared. The printf function is then invoked to display the prompt “Enter your name: “. Subsequently, the scanf function is utilized to obtain a string input. Finally, the printf function is employed to display the string that was taken as input.
#include <stdio.h>
void main() {
char name[100];
printf("Enter your name: ");
scanf("%s", name);
printf(name);
}Let us consider the hypothetical stack layout for the program described above. This representation is not an actual stack but is provided for the purpose of understanding format string vulnerabilities. Here *name is a pointer to string which will be given as input.
Memory Leak:
If we were to input %p %p into the name character array in the program described earlier, the printf function would interpret this input as format specifiers. Consequently, when printf is called, it will treat %p %p as formatting instructions and attempt to print memory addresses, as each %p specifier expects a pointer argument. This behavior can lead to the disclosure of sensitive memory addresses, illustrating a common pitfall in handling format strings.
Memory Overwrite:
Considering the following program
void main() {
int x=2;
char name[100];
printf("Enter your name: ");
scanf("%s", name);
printf(name);
}Let us consider again the hypothetical new stack layout for the program described above. In the provided program, if the input “ABCD%n” is entered, the %n format specifier in the printf function call will attempt to write the number of characters printed so far to an integer variable. as printf is executed, it may inadvertently modify the value of the variable x, replacing its value with 4.
Offset (Writing at Specific position):
For writing at specific position: %p$n.
where p is position.
Conclusion:
format string vulnerabilities pose a significant risk to the security and stability of software systems. These vulnerabilities can be exploited by attackers to manipulate program behavior, read sensitive information, or even execute arbitrary code. It is essential for developers to be aware of these vulnerabilities and to follow secure coding practices to mitigate them.
To prevent format string vulnerabilities, developers should always validate and sanitize user input before using it in format strings. Additionally, using functions that provide more control over formatting, such as snprintf instead of printf, can help reduce the risk of these vulnerabilities.








