Format String Vulnerability

Definition : bug that take advantage of an easily avoidable programmer error. If the programmer passes an attacker-controlled buffer as an argument to a printf (or any of the related functions), the attacker can perform writes to arbitrary memory addresses

Format function

Definition : Is a function which converts a primitive variable of the programming language into a human-readable string representation

Below is an example of Format Function that vulnerable to Format String Attack :

Format Function

fprint

printf

sprintf

snprintf

vfprintf

vprintf

vsprintf

vsnprintf

Format Parameter

Definition : like %x %s defines the type of conversion of the format function

Below is an example of Format Parameter that can be useful for you :

Parameter
Output
Passed as

%%

character

reference

%p

External representation of a pointer to void

reference

%d

decimal

value

%c

character

%u

unsigned decimal

value

%x

hexa

value

%s

string

reference

%n

Writes the number of characters into a pointer

reference

Example

Let's take the example from PicoCTF.

Challenge Name : Stonk Market

Have a look at the code given. Do you notice something?

Line 88-92 is vulnerable to Format String Attack

So we need to enter the Format Parameter like %x-%x-%x-%x-%x-%x into the program to leak some of the memory

See where the memory got leaked?

Yeah that's how you use it 😎👍

Tips 📋

  1. If you don't get the leaked memory that you need, just spam the Format Parameter a lot. Yes, a lot. It can be up to hundreds but you should get what you want in just a few.

  2. Let say you want to display just a certain part of the memory, try using this %n$p . n is an index of the memory that you want. This certainly help you.

  3. When you got the memory leaked (some will be in hex or depends on what parameter you put), put that in Cyberchef and let it cook. If you don't see what you want yet, just remember that binary number can change the translated data. So, don't be shy to remove the binary one by one until it reveal itself.

  4. If i got another, i will write here 🤝

Last updated