This level introduces format strings, and how attacker supplied format strings can modify the execution flow of programs.
Hints
This level should be done in less than 10 bytes of input.
“Exploiting format string vulnerabilities”
Source code
#include<stdlib.h>#include<unistd.h>#include<stdio.h>#include<string.h>voidvuln(char*string){volatileint target;char buffer[64]; target =0;sprintf(buffer, string);if(target ==0xdeadbeef) {printf("you have hit the target correctly :)\n"); }}intmain(int argc,char**argv){vuln(argv[1]);}
The program expects one user supplied argument as shown by vuln(argv[1]);.
The code then uses sprintf which is where the vulnerability lies.
## Bugs
Because **sprintf**() and **vsprintf**() assume an arbitrarily long string, callers must be careful not to overflow the actual space; this is often impossible to assure. Note that the length of the strings produced is locale-dependent and difficult to predict. Use **snprintf**() and **vsnprintf**() instead (or **[asprintf](https://linux.die.net/man/3/asprintf)**(3) and **[vasprintf](https://linux.die.net/man/3/vasprintf)**(3)).
Linux libc4.[45] does not have a **snprintf**(), but provides a libbsd that contains an **snprintf**() equivalent to **sprintf**(), that is, one that ignores the _size_ argument. Thus, the use of **snprintf**() with early libc4 leads to serious security problems.
Code such as **printf(**_foo_**);** often indicates a bug, since _foo_ may contain a % character. If _foo_ comes from untrusted user input, it may contain **%n**, causing the **printf**() call to write to memory and creating a security hole.
Before we exploit the program, we need to know how the stack is laid out.