lab3-slides-format-string
lab3-slides-format-string
● Countermeasures
2
Format String
● printf()- To print out a string according to a
format.
int printf(const char *format,
…);
3
Access Optional Arguments
● myprint() shows how printf()
actually works.
4
Access Optional Arguments
● va_start() macro gets the start
address of Narg, finds the size
based on the data type and sets the
value for va_list pointer.
5
How printf() Access Optional Arguments
● Here, printf() has three optional arguments. Elements starting with “%” are
called format specifiers.
● printf() scans the format string and prints out each character until “%” is
encountered.
● printf() calls va_arg(), which returns the optional argument pointed by va_list
and advances it to the next argument.
6
How printf() Access Optional Arguments
● When printf() is invoked, the
arguments are pushed onto the
stack in reverse order.
7
Missing Optional Arguments
8
Format String Vulnerability
9
Vulnerable Code
10
Vulnerable Program’s Stack
11
What Can We Achieve?
● Attack 1 : Crash program
● Attack 2 : Print out data on the stack
● Attack 3 : Change the program’s data in the
memory
● Attack 4 : Change the program’s data to specific
value
● Attack 5 : Inject Malicious Code
12
Attack 1 : Crash Program
13
Attack 2 : Print Out Data on the Stack
14
Attack 3: Change Program’s Data in Memory
Goal: change the value of var variable from 0x11223344 to some other
value.
15
Attack 3: Change Program’s Data in Memory
● “\x04” : Indicates that “04” is an actual number and not as two ascii
characters.
16
Attack 3: Change Program’s Data in Memory
● var’s address (0xbffff304)
is on the stack.
17
Attack 3: Change Program’s Data in Memory
● Using trial and error, we check how many %x are needed to print out
0xbffff304.
● After the attack, data in the target address is modified to 0x2c (44 in
decimal).
18
Attack 4: Change Program’s Data to a Specific Value
%hn : Treats argument as a 2-byte short integer. Overwrites only 2 significant bytes of the argument.
%hhn : Treats argument as a 1-byte char type. Overwrites the least significant byte of the argument.
20
Attack 4 : A Faster Approach
Goal: change the value of var to 0x66887799
21
Attack 4 : A Faster Approach
● Overwrite the bytes at 0xbffff306 with 0x6688.
● Print some more characters so that when we reach
0xbffff304, the number of characters will be increased to
0x7799.
22
Attack 4 : A Faster Approach
24
Attack 5: Inject Malicious Code
Goal : To modify the return address of the vulnerable code
and let it point it to the malicious code (e.g., shellcode to
execute /bin/sh) . Get root access if vulnerable code is a SET-
UID program.
Challenges :
● Inject Malicious code in the stack
● Find starting address (A) of the injected code
● Find return address (B) of the vulnerable code
● Write value A to B
25
Attack 5 : Inject Malicious Code
● Using gdb to get the return address and start address of
the malicious code.
● Assume that the return address is 0xbffff38c
● Assume that the start address of the malicious code is
0xbfff358
26
Attack 5: Inject Malicious Code
27
Run the Exploit Code
28
Run the Exploit Code
We couldn’t get the shell using the malicious
shell to execute /bin/sh.
Hypothesis :
● We direct the standard input to a file called input while
running the vul program.
● When /bin/sh is triggered from the input file, it inherits the
standard input.
● But as we reach the end of the file, there is no more input
for the shell program and hence it exits.
● So, the shell program is triggered but exits too quickly
before we can see.
29
A Solution
● Create /tmp/bad as follows :
30
Countermeasures: Developer
● Avoid using untrusted user inputs for format strings
in functions like printf, sprintf, fprintf, vprintf, scanf,
vfscanf.
31
Countermeasures: Compiler
32
Countermeasures: Compiler
● With default settings, both compilers gave warning for the first printf().
33
Countermeasures: Compiler
● These warnings just act as reminders to the developers that there is a potential
problem but nevertheless compile the programs.
34
Countermeaseures
● Address randomization: Makes it difficult for the attackers to
guess the address of the address of the target memory (
return address, address of the malicious code)
35
Summary
● How format string works
36