Format String Exploitation
Exploiting format string flaw
A Format String attack can occur when an input string data is processed by a vulnerable function so that attacker can pass the formats to exploit the stack values with the help of format string functions/printf() family functions.
Common Formats in Printf() Family
%c — Formats a single character
%d — Formats an integer in decimal value
%f — Formats float in decimal value
%p — Formats a pointer to address location
%s — Formats a string
%x — Formats a hexadecimal value
%n — Number of bytes written
Vulnerable Functions to Format String
printf()
fprintf()
sprintf()
vprintf()
snprintf()
vsnprintf()
vfprintf()
Vulnerable code example
Compiling this binary with GCC (no flags)
When we pass a normal string data, it prints it successfully
If we pass beyond the buffer it gets crashed. But if we pass string with format values, it gives us the random stack values which is being popped from it:
Other Scenarios
Mitigation
Use format strings corresponding to the assigned variables
Use of “%s” as format string can make the whole input as a single string
Use arguments to call values and functions
Defensive strategy like ‘format_gaurd’ can be used
References
Last updated