“These courses expects a high standard of professionalism from its students with regard to how security testing is conducted. We expect all students to act in good faith at all times […]”
TL;DR Don’t be mean
mycoolfunc(arg1, arg2, arg3);
| 0xFF
push arg3 | arg3
push arg2 | arg2
push arg1 | arg1
call mycoolfunc | ret_addr <-- esp
| 0x00
Used in C library functions
[s,sn,v,va,...]printf
[s,sn,v,va,...]scanf
printf(format, ...vargs)
printf("%s world!", "hello");
printf("👋 🌏!");
%<flags><width><precision><modifier>
<type>
%s
- print as string%x
- print as hex%p
- print as pointer%c
- print as char%d
- print as (signed) int%u
- print as (unsigned) int%n
- write the number of printed bytes…%<flags><width><precision>
<modifier>
<type>
%d
- print as (signed) int
%hd
- print as half (signed) int
%hhd
- print as half half (signed) int
%n
- write as int
%hn
- write as half int
%hhn
- write as half half int
%<flags><width>
<precision>
<modifier><type>
Output maximum limit
printf("%.3s", "abcdef");
$> "abc"
printf("%.5f", 4 * 1.0 / 3);
$> 1.33333
%<flags>
<width>
<precision><modifier><type>
Output minimum limit
printf("%d", 10);
$> 10
printf("%5d", 10);
$> 10
printf("%10d", 10);
$> 10
%
<flags>
<width><precision><modifier><type>
Modifiers, eh.
-
- Left-align the output+
- Prepends a plus for +ve signed-numeric types
- Prepends a space for +ve signed-numeric types0
- Prepends zeros'
- Show thousandths separators#
- misc.jks one more
%
<parameter>
<flags><width><precision><modifier><type>
Allows you to specify a certain index from the vargs.
%<idx>$...
printf("%3$c %1$s %2$d", "string", 50, 'c')
$> c string 50
What if you don’t provide any arguments?
printf("%d %d %d %d %s")
For numbers…
%d
will read the next item on the stack%20$d
will read the 20th item from the stackFor hex…
%p
will read the next item on the stack%20$p
will read the 20th item from the stackFor addresses…
%s
will read the next item (as a pointer) on the stack%20$s
will read the 20th item (pointer) on the stackDemo:
./leakValues
Your C code may not reflect the correct order of function locals (bc compiler optimisations)
Demo:
./varOrder
%n
format specifier
Writes the number of printed bytes to the next item (pointer) on the stack
%7$n
format specifier
Writes the number of printed bytes to the seventh item (pointer) on the stack
Sometimes our values aren’t aligned to the 4-byte boundary. We just need to pad our values then :)
Demo:
./printfRunner
Demo:
./setValue
i.e. write
0xdeadbeef
into our buffer, then write into that address
...
or %...$c
)%...$n / hn / hhn
)Craft your write payload wisely!
Sometimes you don’t have alot of buffer space
Demo:
./setValueBig
- better-ish
Demo:
./setValueBig
- better
GOT - Global Offset Table
PLT - Process Linkage Table
got
command - shows all of the linkable functionsvmmap
command - shows all the memory regionsPIE - Program can only contain relative jumps
FORTIFY - Stop string vulnerabilities
%n
if the fmtstr is in writeable memoryRELRO - Relocation Read-Only
0x565
- Binary base with PIE enabled0x804
- Binary base with PIE disabled0xf7f
- Library base0xff..
- Stack baseLab 3:
./format_prac
also, wargames 2 are due tonight at 5.59pm!